Skip to content

Latest commit

 

History

History
638 lines (554 loc) · 16.8 KB

music_wav.c

File metadata and controls

638 lines (554 loc) · 16.8 KB
 
Oct 21, 1999
Oct 21, 1999
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Oct 21, 1999
Oct 21, 1999
20
21
*/
Oct 17, 2017
Oct 17, 2017
22
#ifdef MUSIC_WAV
Dec 14, 2001
Dec 14, 2001
23
Oct 17, 2017
Oct 17, 2017
24
/* This file supports streaming WAV files */
Oct 21, 1999
Oct 21, 1999
25
Oct 17, 2017
Oct 17, 2017
26
#include "music_wav.h"
Oct 21, 1999
Oct 21, 1999
27
28
Oct 17, 2017
Oct 17, 2017
29
30
31
32
33
34
35
36
37
38
39
40
41
typedef struct {
SDL_bool active;
Uint32 start;
Uint32 stop;
Uint32 initial_play_count;
Uint32 current_play_count;
} WAVLoopPoint;
typedef struct {
SDL_RWops *src;
SDL_bool freesrc;
SDL_AudioSpec spec;
int volume;
Oct 21, 2017
Oct 21, 2017
42
int play_count;
Oct 17, 2017
Oct 17, 2017
43
44
Sint64 start;
Sint64 stop;
Oct 21, 2017
Oct 21, 2017
45
46
Uint8 *buffer;
SDL_AudioStream *stream;
Oct 17, 2017
Oct 17, 2017
47
48
int numloops;
WAVLoopPoint *loops;
Oct 21, 2017
Oct 21, 2017
49
} WAV_Music;
Oct 21, 1999
Oct 21, 1999
50
Sep 11, 2001
Sep 11, 2001
51
52
53
54
55
56
57
58
59
60
61
/*
Taken with permission from SDL_wave.h, part of the SDL library,
available at: http://www.libsdl.org/
and placed under the same license as this mixer library.
*/
/* WAVE files are little-endian */
/*******************************************/
/* Define values for Microsoft WAVE format */
/*******************************************/
May 22, 2013
May 22, 2013
62
63
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
Jun 22, 2014
Jun 22, 2014
64
#define FMT 0x20746D66 /* "fmt " */
May 22, 2013
May 22, 2013
65
#define DATA 0x61746164 /* "data" */
Jul 7, 2015
Jul 7, 2015
66
#define SMPL 0x6c706d73 /* "smpl" */
May 22, 2013
May 22, 2013
67
68
69
70
#define PCM_CODE 1
#define ADPCM_CODE 2
#define WAVE_MONO 1
#define WAVE_STEREO 2
Sep 11, 2001
Sep 11, 2001
71
Jul 7, 2015
Jul 7, 2015
72
typedef struct {
Sep 11, 2001
Sep 11, 2001
73
/* Not saved in the chunk we read:
Jul 7, 2015
Jul 7, 2015
74
75
Uint32 chunkID;
Uint32 chunkLen;
Sep 11, 2001
Sep 11, 2001
76
*/
May 22, 2013
May 22, 2013
77
78
79
80
81
82
Uint16 encoding;
Uint16 channels; /* 1 = mono, 2 = stereo */
Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */
Uint32 byterate; /* Average bytes per second */
Uint16 blockalign; /* Bytes per sample block */
Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */
Sep 11, 2001
Sep 11, 2001
83
84
} WaveFMT;
Jul 7, 2015
Jul 7, 2015
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
typedef struct {
Uint32 identifier;
Uint32 type;
Uint32 start;
Uint32 end;
Uint32 fraction;
Uint32 play_count;
} SampleLoop;
typedef struct {
/* Not saved in the chunk we read:
Uint32 chunkID;
Uint32 chunkLen;
*/
Uint32 manufacturer;
Uint32 product;
Uint32 sample_period;
Uint32 MIDI_unity_note;
Uint32 MIDI_pitch_fraction;
Uint32 SMTPE_format;
Uint32 SMTPE_offset;
Uint32 sample_loops;
Uint32 sampler_data;
SampleLoop loops[];
} SamplerChunk;
Sep 11, 2001
Sep 11, 2001
110
111
112
113
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
May 22, 2013
May 22, 2013
114
115
116
117
#define FORM 0x4d524f46 /* "FORM" */
#define AIFF 0x46464941 /* "AIFF" */
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
Sep 11, 2001
Sep 11, 2001
118
119
Oct 21, 1999
Oct 21, 1999
120
/* Function to load the WAV/AIFF stream */
Oct 21, 2017
Oct 21, 2017
121
122
static SDL_bool LoadWAVMusic(WAV_Music *wave);
static SDL_bool LoadAIFFMusic(WAV_Music *wave);
Oct 21, 1999
Oct 21, 1999
123
Oct 21, 2017
Oct 21, 2017
124
static void WAV_Delete(void *context);
Oct 21, 1999
Oct 21, 1999
125
Jan 4, 2012
Jan 4, 2012
126
/* Load a WAV stream from the given RWops object */
Oct 21, 2017
Oct 21, 2017
127
static void *WAV_CreateFromRW(SDL_RWops *src, int freesrc)
Oct 21, 1999
Oct 21, 1999
128
{
Oct 21, 2017
Oct 21, 2017
129
130
WAV_Music *music;
Uint32 magic;
Jul 7, 2015
Jul 7, 2015
131
SDL_bool loaded = SDL_FALSE;
May 22, 2013
May 22, 2013
132
Oct 21, 2017
Oct 21, 2017
133
134
135
136
137
138
music = (WAV_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->volume = MIX_MAX_VOLUME;
Jun 2, 2013
Jun 2, 2013
139
Oct 21, 2017
Oct 21, 2017
140
141
142
143
144
magic = SDL_ReadLE32(src);
if (magic == RIFF || magic == WAVE) {
loaded = LoadWAVMusic(music);
} else if (magic == FORM) {
loaded = LoadAIFFMusic(music);
May 22, 2013
May 22, 2013
145
} else {
Oct 21, 2017
Oct 21, 2017
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Mix_SetError("Unknown WAVE format");
}
if (!loaded) {
SDL_free(music);
return NULL;
}
music->buffer = (Uint8*)SDL_malloc(music->spec.size);
if (!music->buffer) {
WAV_Delete(music);
return NULL;
}
music->stream = SDL_NewAudioStream(
music->spec.format, music->spec.channels, music->spec.freq,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
WAV_Delete(music);
return NULL;
May 22, 2013
May 22, 2013
163
}
Oct 21, 2017
Oct 21, 2017
164
165
166
167
music->src = src;
music->freesrc = freesrc;
return music;
Oct 17, 2017
Oct 17, 2017
168
169
}
Oct 21, 2017
Oct 21, 2017
170
static void WAV_SetVolume(void *context, int volume)
Oct 17, 2017
Oct 17, 2017
171
{
Oct 21, 2017
Oct 21, 2017
172
173
WAV_Music *music = (WAV_Music *)context;
music->volume = volume;
Oct 21, 1999
Oct 21, 1999
174
175
176
}
/* Start playback of a given WAV stream */
Oct 21, 2017
Oct 21, 2017
177
static int WAV_Play(void *context, int play_count)
Oct 21, 1999
Oct 21, 1999
178
{
Oct 21, 2017
Oct 21, 2017
179
WAV_Music *music = (WAV_Music *)context;
Jul 7, 2015
Jul 7, 2015
180
int i;
Oct 21, 2017
Oct 21, 2017
181
182
for (i = 0; i < music->numloops; ++i) {
WAVLoopPoint *loop = &music->loops[i];
Jul 7, 2015
Jul 7, 2015
183
184
185
loop->active = SDL_TRUE;
loop->current_play_count = loop->initial_play_count;
}
Oct 21, 2017
Oct 21, 2017
186
music->play_count = play_count;
Oct 21, 2017
Oct 21, 2017
187
188
189
190
if (SDL_RWseek(music->src, music->start, RW_SEEK_SET) < 0) {
return -1;
}
return 0;
Oct 21, 1999
Oct 21, 1999
191
192
}
Oct 21, 2017
Oct 21, 2017
193
194
/* Play some of a stream previously started with WAV_Play() */
static int WAV_GetSome(void *context, void *data, int bytes, SDL_bool *done)
Oct 21, 1999
Oct 21, 1999
195
{
Oct 21, 2017
Oct 21, 2017
196
WAV_Music *music = (WAV_Music *)context;
Jul 7, 2015
Jul 7, 2015
197
198
199
200
Sint64 pos, stop;
WAVLoopPoint *loop;
Sint64 loop_start;
Sint64 loop_stop;
Oct 21, 2017
Oct 21, 2017
201
SDL_bool looped = SDL_FALSE;
Jul 7, 2015
Jul 7, 2015
202
int i;
Oct 21, 2017
Oct 21, 2017
203
int filled, amount, result;
Jul 7, 2015
Jul 7, 2015
204
Oct 21, 2017
Oct 21, 2017
205
206
207
208
209
210
211
212
213
214
215
216
217
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
}
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
}
pos = SDL_RWtell(music->src);
stop = music->stop;
Jul 7, 2015
Jul 7, 2015
218
loop = NULL;
Oct 21, 2017
Oct 21, 2017
219
220
for (i = 0; i < music->numloops; ++i) {
loop = &music->loops[i];
Jul 7, 2015
Jul 7, 2015
221
if (loop->active) {
Oct 21, 2017
Oct 21, 2017
222
223
224
const int bytes_per_sample = (SDL_AUDIO_BITSIZE(music->spec.format) / 8) * music->spec.channels;
loop_start = music->start + loop->start * bytes_per_sample;
loop_stop = music->start + (loop->stop + 1) * bytes_per_sample;
Jul 7, 2015
Jul 7, 2015
225
226
227
228
if (pos >= loop_start && pos < loop_stop)
{
stop = loop_stop;
break;
May 22, 2013
May 22, 2013
229
}
Jul 7, 2015
Jul 7, 2015
230
231
232
233
}
loop = NULL;
}
Oct 21, 2017
Oct 21, 2017
234
235
236
237
238
239
240
241
242
amount = music->spec.size;
if ((stop - pos) < amount) {
amount = (int)(stop - pos);
}
amount = (int)SDL_RWread(music->src, music->buffer, 1, amount);
if (amount > 0) {
result = SDL_AudioStreamPut(music->stream, music->buffer, amount);
if (result < 0) {
return -1;
Jul 7, 2015
Jul 7, 2015
243
244
}
} else {
Oct 21, 2017
Oct 21, 2017
245
/* We might be looping, continue */
Jul 7, 2015
Jul 7, 2015
246
247
}
Oct 21, 2017
Oct 21, 2017
248
if (loop && SDL_RWtell(music->src) >= stop) {
Jul 7, 2015
Jul 7, 2015
249
250
if (loop->current_play_count == 1) {
loop->active = SDL_FALSE;
May 22, 2013
May 22, 2013
251
} else {
Jul 7, 2015
Jul 7, 2015
252
253
if (loop->current_play_count > 0) {
--loop->current_play_count;
May 22, 2013
May 22, 2013
254
}
Oct 21, 2017
Oct 21, 2017
255
256
SDL_RWseek(music->src, loop_start, RW_SEEK_SET);
looped = SDL_TRUE;
May 22, 2013
May 22, 2013
257
258
}
}
Jul 7, 2015
Jul 7, 2015
259
Oct 21, 2017
Oct 21, 2017
260
261
262
263
264
265
266
267
268
269
270
271
272
273
if (!looped && SDL_RWtell(music->src) >= music->stop) {
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (WAV_Play(music, play_count) < 0) {
return -1;
}
}
}
Jul 7, 2015
Jul 7, 2015
274
Oct 21, 2017
Oct 21, 2017
275
276
277
/* We'll get called again in the case where we looped or have more data */
return 0;
}
Jul 7, 2015
Jul 7, 2015
278
Oct 21, 2017
Oct 21, 2017
279
280
281
282
static int WAV_GetAudio(void *context, void *data, int bytes)
{
WAV_Music *music = (WAV_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, WAV_GetSome);
Oct 21, 1999
Oct 21, 1999
283
284
285
}
/* Close the given WAV stream */
Oct 21, 2017
Oct 21, 2017
286
static void WAV_Delete(void *context)
Oct 21, 1999
Oct 21, 1999
287
{
Oct 21, 2017
Oct 21, 2017
288
WAV_Music *music = (WAV_Music *)context;
Oct 21, 1999
Oct 21, 1999
289
Oct 17, 2017
Oct 17, 2017
290
/* Clean up associated data */
Oct 21, 2017
Oct 21, 2017
291
292
293
294
295
if (music->loops) {
SDL_free(music->loops);
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
Oct 17, 2017
Oct 17, 2017
296
}
Oct 21, 2017
Oct 21, 2017
297
298
if (music->buffer) {
SDL_free(music->buffer);
May 22, 2013
May 22, 2013
299
}
Oct 21, 2017
Oct 21, 2017
300
301
if (music->freesrc) {
SDL_RWclose(music->src);
Oct 17, 2017
Oct 17, 2017
302
}
Oct 21, 2017
Oct 21, 2017
303
SDL_free(music);
Oct 21, 1999
Oct 21, 1999
304
305
}
Oct 21, 2017
Oct 21, 2017
306
static SDL_bool ParseFMT(WAV_Music *wave, Uint32 chunk_length)
Oct 21, 1999
Oct 21, 1999
307
{
Jul 7, 2015
Jul 7, 2015
308
309
310
311
312
313
314
315
SDL_AudioSpec *spec = &wave->spec;
WaveFMT *format;
Uint8 *data;
SDL_bool loaded = SDL_FALSE;
if (chunk_length < sizeof(*format)) {
Mix_SetError("Wave format chunk too small");
return SDL_FALSE;
May 22, 2013
May 22, 2013
316
317
}
Jul 7, 2015
Jul 7, 2015
318
319
320
321
322
323
324
325
326
327
data = (Uint8 *)SDL_malloc(chunk_length);
if (!data) {
Mix_SetError("Out of memory");
return SDL_FALSE;
}
if (!SDL_RWread(wave->src, data, chunk_length, 1)) {
Mix_SetError("Couldn't read %d bytes from WAV file", chunk_length);
return SDL_FALSE;
}
format = (WaveFMT *)data;
May 22, 2013
May 22, 2013
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* Decode the audio data format */
switch (SDL_SwapLE16(format->encoding)) {
case PCM_CODE:
/* We can understand this */
break;
default:
Mix_SetError("Unknown WAVE data format");
goto done;
}
spec->freq = SDL_SwapLE32(format->frequency);
switch (SDL_SwapLE16(format->bitspersample)) {
case 8:
spec->format = AUDIO_U8;
break;
case 16:
spec->format = AUDIO_S16;
break;
default:
Mix_SetError("Unknown PCM data format");
goto done;
}
spec->channels = (Uint8) SDL_SwapLE16(format->channels);
spec->samples = 4096; /* Good default buffer size */
Oct 21, 2017
Oct 21, 2017
352
353
354
355
/* SDL_CalculateAudioSpec */
spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
spec->size *= spec->channels;
spec->size *= spec->samples;
May 22, 2013
May 22, 2013
356
Jul 7, 2015
Jul 7, 2015
357
358
359
360
361
362
363
loaded = SDL_TRUE;
done:
SDL_free(data);
return loaded;
}
Oct 21, 2017
Oct 21, 2017
364
static SDL_bool ParseDATA(WAV_Music *wave, Uint32 chunk_length)
Jul 7, 2015
Jul 7, 2015
365
366
367
368
369
370
371
{
wave->start = SDL_RWtell(wave->src);
wave->stop = wave->start + chunk_length;
SDL_RWseek(wave->src, chunk_length, RW_SEEK_CUR);
return SDL_TRUE;
}
Oct 21, 2017
Oct 21, 2017
372
static SDL_bool AddLoopPoint(WAV_Music *wave, Uint32 play_count, Uint32 start, Uint32 stop)
Jul 7, 2015
Jul 7, 2015
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{
WAVLoopPoint *loop;
WAVLoopPoint *loops = SDL_realloc(wave->loops, (wave->numloops + 1)*sizeof(*wave->loops));
if (!loops) {
Mix_SetError("Out of memory");
return SDL_FALSE;
}
loop = &loops[ wave->numloops ];
loop->start = start;
loop->stop = stop;
loop->initial_play_count = play_count;
loop->current_play_count = play_count;
wave->loops = loops;
++wave->numloops;
return SDL_TRUE;
}
Oct 21, 2017
Oct 21, 2017
392
static SDL_bool ParseSMPL(WAV_Music *wave, Uint32 chunk_length)
Jul 7, 2015
Jul 7, 2015
393
394
395
{
SamplerChunk *chunk;
Uint8 *data;
Oct 13, 2017
Oct 13, 2017
396
Uint32 i;
Jul 7, 2015
Jul 7, 2015
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
SDL_bool loaded = SDL_FALSE;
data = (Uint8 *)SDL_malloc(chunk_length);
if (!data) {
Mix_SetError("Out of memory");
return SDL_FALSE;
}
if (!SDL_RWread(wave->src, data, chunk_length, 1)) {
Mix_SetError("Couldn't read %d bytes from WAV file", chunk_length);
return SDL_FALSE;
}
chunk = (SamplerChunk *)data;
for (i = 0; i < SDL_SwapLE32(chunk->sample_loops); ++i) {
const Uint32 LOOP_TYPE_FORWARD = 0;
Uint32 loop_type = SDL_SwapLE32(chunk->loops[i].type);
if (loop_type == LOOP_TYPE_FORWARD) {
AddLoopPoint(wave, SDL_SwapLE32(chunk->loops[i].play_count), SDL_SwapLE32(chunk->loops[i].start), SDL_SwapLE32(chunk->loops[i].end));
May 22, 2013
May 22, 2013
415
}
Jul 7, 2015
Jul 7, 2015
416
417
418
419
420
421
422
}
loaded = SDL_TRUE;
SDL_free(data);
return loaded;
}
Oct 21, 2017
Oct 21, 2017
423
static SDL_bool LoadWAVMusic(WAV_Music *wave)
Jul 7, 2015
Jul 7, 2015
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
{
SDL_RWops *src = wave->src;
Uint32 chunk_type;
Uint32 chunk_length;
SDL_bool found_FMT = SDL_FALSE;
SDL_bool found_DATA = SDL_FALSE;
/* WAV magic header */
Uint32 wavelen;
Uint32 WAVEmagic;
/* Check the magic header */
wavelen = SDL_ReadLE32(src);
WAVEmagic = SDL_ReadLE32(src);
/* Read the chunks */
for (; ;) {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadLE32(src);
if (chunk_length == 0)
break;
switch (chunk_type)
{
case FMT:
found_FMT = SDL_TRUE;
if (!ParseFMT(wave, chunk_length))
return SDL_FALSE;
break;
case DATA:
found_DATA = SDL_TRUE;
if (!ParseDATA(wave, chunk_length))
return SDL_FALSE;
break;
case SMPL:
if (!ParseSMPL(wave, chunk_length))
return SDL_FALSE;
break;
default:
SDL_RWseek(src, chunk_length, RW_SEEK_CUR);
break;
}
May 22, 2013
May 22, 2013
467
}
Jul 7, 2015
Jul 7, 2015
468
469
470
471
if (!found_FMT) {
Mix_SetError("Bad WAV file (no FMT chunk)");
return SDL_FALSE;
May 22, 2013
May 22, 2013
472
}
Jul 7, 2015
Jul 7, 2015
473
474
475
476
477
478
479
if (!found_DATA) {
Mix_SetError("Bad WAV file (no DATA chunk)");
return SDL_FALSE;
}
return SDL_TRUE;
Oct 21, 1999
Oct 21, 1999
480
481
}
Aug 19, 2001
Aug 19, 2001
482
483
484
485
486
/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
* I don't pretend to fully understand it.
*/
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
Oct 21, 1999
Oct 21, 1999
487
{
May 22, 2013
May 22, 2013
488
489
490
/* Negative number? */
if (sanebuf[0] & 0x80)
return 0;
Aug 19, 2001
Aug 19, 2001
491
May 22, 2013
May 22, 2013
492
493
494
/* Less than 1? */
if (sanebuf[0] <= 0x3F)
return 1;
Aug 19, 2001
Aug 19, 2001
495
May 22, 2013
May 22, 2013
496
497
498
/* Way too big? */
if (sanebuf[0] > 0x40)
return 0x4000000;
Aug 19, 2001
Aug 19, 2001
499
May 22, 2013
May 22, 2013
500
501
502
/* Still too big? */
if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
return 800000000;
Aug 19, 2001
Aug 19, 2001
503
Jul 7, 2015
Jul 7, 2015
504
505
return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) |
(sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
Oct 21, 1999
Oct 21, 1999
506
507
}
Oct 21, 2017
Oct 21, 2017
508
static SDL_bool LoadAIFFMusic(WAV_Music *wave)
Oct 21, 1999
Oct 21, 1999
509
{
Jul 7, 2015
Jul 7, 2015
510
511
512
513
SDL_RWops *src = wave->src;
SDL_AudioSpec *spec = &wave->spec;
SDL_bool found_SSND = SDL_FALSE;
SDL_bool found_COMM = SDL_FALSE;
May 22, 2013
May 22, 2013
514
515
516
Uint32 chunk_type;
Uint32 chunk_length;
Jun 1, 2013
Jun 1, 2013
517
Sint64 next_chunk;
May 22, 2013
May 22, 2013
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* AIFF magic header */
Uint32 AIFFmagic;
/* SSND chunk */
Uint32 offset;
Uint32 blocksize;
/* COMM format chunk */
Uint16 channels = 0;
Uint32 numsamples = 0;
Uint16 samplesize = 0;
Uint8 sane_freq[10];
Uint32 frequency = 0;
/* Check the magic header */
Jul 7, 2015
Jul 7, 2015
532
533
534
chunk_length = SDL_ReadBE32(src);
AIFFmagic = SDL_ReadLE32(src);
if (AIFFmagic != AIFF) {
May 22, 2013
May 22, 2013
535
Mix_SetError("Unrecognized file type (not AIFF)");
Jul 7, 2015
Jul 7, 2015
536
return SDL_FALSE;
May 22, 2013
May 22, 2013
537
538
539
}
/* From what I understand of the specification, chunks may appear in
Jul 7, 2015
Jul 7, 2015
540
* any order, and we should just ignore unknown ones.
May 22, 2013
May 22, 2013
541
542
543
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
Jun 2, 2013
Jun 2, 2013
544
*/
May 22, 2013
May 22, 2013
545
546
547
548
do {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
next_chunk = SDL_RWtell(src) + chunk_length;
Aug 19, 2001
Aug 19, 2001
549
May 22, 2013
May 22, 2013
550
551
/* Paranoia to avoid infinite loops */
if (chunk_length == 0)
Jul 7, 2015
Jul 7, 2015
552
break;
Aug 19, 2001
Aug 19, 2001
553
Jun 2, 2013
Jun 2, 2013
554
switch (chunk_type) {
May 22, 2013
May 22, 2013
555
case SSND:
Jul 7, 2015
Jul 7, 2015
556
557
558
559
found_SSND = SDL_TRUE;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
wave->start = SDL_RWtell(src) + offset;
May 22, 2013
May 22, 2013
560
561
562
break;
case COMM:
Jul 7, 2015
Jul 7, 2015
563
found_COMM = SDL_TRUE;
May 22, 2013
May 22, 2013
564
565
/* Read the audio data format chunk */
Jul 7, 2015
Jul 7, 2015
566
567
568
channels = SDL_ReadBE16(src);
numsamples = SDL_ReadBE32(src);
samplesize = SDL_ReadBE16(src);
May 22, 2013
May 22, 2013
569
SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
Jul 7, 2015
Jul 7, 2015
570
frequency = SANE_to_Uint32(sane_freq);
May 22, 2013
May 22, 2013
571
572
573
574
575
576
577
578
579
580
break;
default:
break;
}
} while ((!found_SSND || !found_COMM)
&& SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1);
if (!found_SSND) {
Mix_SetError("Bad AIFF file (no SSND chunk)");
Jul 7, 2015
Jul 7, 2015
581
return SDL_FALSE;
May 22, 2013
May 22, 2013
582
583
584
585
}
if (!found_COMM) {
Mix_SetError("Bad AIFF file (no COMM chunk)");
Jul 7, 2015
Jul 7, 2015
586
return SDL_FALSE;
May 22, 2013
May 22, 2013
587
588
}
Jul 7, 2015
Jul 7, 2015
589
wave->stop = wave->start + channels * numsamples * (samplesize / 8);
May 22, 2013
May 22, 2013
590
591
/* Decode the audio data format */
Jun 1, 2013
Jun 1, 2013
592
SDL_memset(spec, 0, (sizeof *spec));
May 22, 2013
May 22, 2013
593
594
595
596
597
598
599
600
601
602
spec->freq = frequency;
switch (samplesize) {
case 8:
spec->format = AUDIO_S8;
break;
case 16:
spec->format = AUDIO_S16MSB;
break;
default:
Mix_SetError("Unknown samplesize in data format");
Jul 7, 2015
Jul 7, 2015
603
return SDL_FALSE;
May 22, 2013
May 22, 2013
604
605
606
}
spec->channels = (Uint8) channels;
spec->samples = 4096; /* Good default buffer size */
Oct 21, 1999
Oct 21, 1999
607
Jul 7, 2015
Jul 7, 2015
608
return SDL_TRUE;
Oct 21, 1999
Oct 21, 1999
609
}
May 9, 2006
May 9, 2006
610
Oct 17, 2017
Oct 17, 2017
611
612
613
614
615
616
617
618
619
620
Mix_MusicInterface Mix_MusicInterface_WAV =
{
"WAVE",
MIX_MUSIC_WAVE,
MUS_WAV,
SDL_FALSE,
SDL_FALSE,
NULL, /* Load */
NULL, /* Open */
Oct 21, 2017
Oct 21, 2017
621
WAV_CreateFromRW,
Oct 17, 2017
Oct 17, 2017
622
NULL, /* CreateFromFile */
Oct 21, 2017
Oct 21, 2017
623
624
WAV_SetVolume,
WAV_Play,
Oct 17, 2017
Oct 17, 2017
625
NULL, /* IsPlaying */
Oct 21, 2017
Oct 21, 2017
626
WAV_GetAudio,
Oct 17, 2017
Oct 17, 2017
627
628
629
630
NULL, /* Seek */
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
Oct 21, 2017
Oct 21, 2017
631
WAV_Delete,
Oct 17, 2017
Oct 17, 2017
632
633
634
635
636
637
638
NULL, /* Close */
NULL, /* Unload */
};
#endif /* MUSIC_WAV */
/* vi: set ts=4 sw=4 expandtab: */