/
music_wav.c
624 lines (542 loc) · 16.8 KB
1
/*
2
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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.
20
21
*/
22
#ifdef MUSIC_WAV
23
24
/* This file supports streaming WAV files */
25
26
#include "music_wav.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
Sint64 start;
Sint64 stop;
SDL_AudioCVT cvt;
int numloops;
WAVLoopPoint *loops;
} WAVStream;
48
49
50
51
52
53
54
55
56
57
58
59
/*
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 */
/*******************************************/
60
61
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
62
#define FMT 0x20746D66 /* "fmt " */
63
#define DATA 0x61746164 /* "data" */
64
#define SMPL 0x6c706d73 /* "smpl" */
65
66
67
68
#define PCM_CODE 1
#define ADPCM_CODE 2
#define WAVE_MONO 1
#define WAVE_STEREO 2
69
70
typedef struct {
71
/* Not saved in the chunk we read:
72
73
Uint32 chunkID;
Uint32 chunkLen;
74
*/
75
76
77
78
79
80
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 */
81
82
} WaveFMT;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
108
109
110
111
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
112
113
114
115
#define FORM 0x4d524f46 /* "FORM" */
#define AIFF 0x46464941 /* "AIFF" */
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
116
117
118
/* Function to load the WAV/AIFF stream */
119
120
static SDL_bool LoadWAVStream(WAVStream *wave);
static SDL_bool LoadAIFFStream(WAVStream *wave);
121
122
123
/* Load a WAV stream from the given RWops object */
124
static void *WAVStream_CreateFromRW(SDL_RWops *src, int freesrc)
125
{
126
WAVStream *wave;
127
SDL_bool loaded = SDL_FALSE;
128
129
wave = (WAVStream *)SDL_calloc(1, sizeof(*wave));
130
if (wave) {
131
132
Uint32 magic;
133
wave->src = src;
134
135
136
wave->freesrc = freesrc;
magic = SDL_ReadLE32(src);
137
138
139
140
if (magic == RIFF || magic == WAVE) {
loaded = LoadWAVStream(wave);
} else if (magic == FORM) {
loaded = LoadAIFFStream(wave);
141
142
143
} else {
Mix_SetError("Unknown WAVE format");
}
144
if (!loaded) {
145
SDL_free(wave);
146
147
148
return(NULL);
}
SDL_BuildAudioCVT(&wave->cvt,
149
wave->spec.format, wave->spec.channels, wave->spec.freq,
150
151
music_spec.format, music_spec.channels, music_spec.freq);
wave->volume = MIX_MAX_VOLUME;
152
153
154
} else {
SDL_OutOfMemory();
}
155
156
157
158
159
160
161
return wave;
}
static void WAVStream_SetVolume(void *context, int volume)
{
WAVStream *wave = (WAVStream *)context;
wave->volume = volume;
162
163
164
}
/* Start playback of a given WAV stream */
165
static int WAVStream_Play(void *context)
166
{
167
WAVStream *wave = (WAVStream *)context;
168
169
170
171
172
173
174
int i;
for (i = 0; i < wave->numloops; ++i) {
WAVLoopPoint *loop = &wave->loops[i];
loop->active = SDL_TRUE;
loop->current_play_count = loop->initial_play_count;
}
SDL_RWseek(wave->src, wave->start, RW_SEEK_SET);
175
return 0;
176
177
}
178
179
/* Play some of a stream previously started with WAVStream_Play() */
static int PlaySome(WAVStream *wave, Uint8 *stream, int len)
180
{
181
182
183
184
185
186
187
Sint64 pos, stop;
WAVLoopPoint *loop;
Sint64 loop_start;
Sint64 loop_stop;
int i;
int consumed;
188
189
pos = SDL_RWtell(wave->src);
stop = wave->stop;
190
loop = NULL;
191
192
for (i = 0; i < wave->numloops; ++i) {
loop = &wave->loops[i];
193
if (loop->active) {
194
195
196
const int bytes_per_sample = (SDL_AUDIO_BITSIZE(wave->spec.format) / 8) * wave->spec.channels;
loop_start = wave->start + loop->start * bytes_per_sample;
loop_stop = wave->start + (loop->stop + 1) * bytes_per_sample;
197
198
199
200
if (pos >= loop_start && pos < loop_stop)
{
stop = loop_stop;
break;
201
}
202
203
204
205
}
loop = NULL;
}
206
207
if (wave->cvt.needed) {
int original_len = (int)((double)len/wave->cvt.len_ratio);
208
209
/* Make sure the length is a multiple of the sample size */
{
210
const int bytes_per_sample = (SDL_AUDIO_BITSIZE(wave->spec.format) / 8) * wave->spec.channels;
211
212
213
const int alignment_mask = (bytes_per_sample - 1);
original_len &= ~alignment_mask;
}
214
if (wave->cvt.len != original_len) {
215
int worksize;
216
217
if (wave->cvt.buf != NULL) {
SDL_free(wave->cvt.buf);
218
}
219
220
221
worksize = original_len*wave->cvt.len_mult;
wave->cvt.buf=(Uint8 *)SDL_malloc(worksize);
if (wave->cvt.buf == NULL) {
222
return 0;
223
}
224
wave->cvt.len = original_len;
225
226
227
228
}
if ((stop - pos) < original_len) {
original_len = (int)(stop - pos);
}
229
230
231
232
233
original_len = (int)SDL_RWread(wave->src, wave->cvt.buf, 1, original_len);
wave->cvt.len = original_len;
SDL_ConvertAudio(&wave->cvt);
SDL_MixAudioFormat(stream, wave->cvt.buf, music_spec.format, wave->cvt.len_cvt, wave->volume);
consumed = wave->cvt.len_cvt;
234
235
236
237
238
239
240
} else {
Uint8 *data;
if ((stop - pos) < len) {
len = (int)(stop - pos);
}
data = SDL_stack_alloc(Uint8, len);
if (data) {
241
242
len = (int)SDL_RWread(wave->src, data, 1, len);
SDL_MixAudioFormat(stream, data, music_spec.format, len, wave->volume);
243
244
245
246
247
SDL_stack_free(data);
}
consumed = len;
}
248
if (loop && SDL_RWtell(wave->src) >= stop) {
249
250
if (loop->current_play_count == 1) {
loop->active = SDL_FALSE;
251
} else {
252
253
if (loop->current_play_count > 0) {
--loop->current_play_count;
254
}
255
SDL_RWseek(wave->src, loop_start, RW_SEEK_SET);
256
257
}
}
258
259
260
return consumed;
}
261
static int WAVStream_GetAudio(void *context, void *data, int bytes)
262
{
263
264
265
WAVStream *wave = (WAVStream *)context;
Uint8 *stream = (Uint8 *)data;
int len = bytes;
266
267
268
while ((SDL_RWtell(wave->src) < wave->stop) && (len > 0)) {
int consumed = PlaySome(wave, stream, len);
269
270
271
272
273
274
275
if (!consumed)
break;
stream += consumed;
len -= consumed;
}
return len;
276
277
278
}
/* Close the given WAV stream */
279
static void WAVStream_Delete(void *context)
280
{
281
WAVStream *wave = (WAVStream *)context;
282
283
284
285
286
287
288
/* Clean up associated data */
if (wave->loops) {
SDL_free(wave->loops);
}
if (wave->cvt.buf) {
SDL_free(wave->cvt.buf);
289
}
290
291
292
293
if (wave->freesrc) {
SDL_RWclose(wave->src);
}
SDL_free(wave);
294
295
}
296
static SDL_bool ParseFMT(WAVStream *wave, Uint32 chunk_length)
297
{
298
299
300
301
302
303
304
305
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;
306
307
}
308
309
310
311
312
313
314
315
316
317
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;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* 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 */
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
loaded = SDL_TRUE;
done:
SDL_free(data);
return loaded;
}
static SDL_bool ParseDATA(WAVStream *wave, Uint32 chunk_length)
{
wave->start = SDL_RWtell(wave->src);
wave->stop = wave->start + chunk_length;
SDL_RWseek(wave->src, chunk_length, RW_SEEK_CUR);
return SDL_TRUE;
}
static SDL_bool AddLoopPoint(WAVStream *wave, Uint32 play_count, Uint32 start, Uint32 stop)
{
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;
}
static SDL_bool ParseSMPL(WAVStream *wave, Uint32 chunk_length)
{
SamplerChunk *chunk;
Uint8 *data;
382
Uint32 i;
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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));
401
}
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
}
loaded = SDL_TRUE;
SDL_free(data);
return loaded;
}
static SDL_bool LoadWAVStream(WAVStream *wave)
{
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;
}
453
}
454
455
456
457
if (!found_FMT) {
Mix_SetError("Bad WAV file (no FMT chunk)");
return SDL_FALSE;
458
}
459
460
461
462
463
464
465
if (!found_DATA) {
Mix_SetError("Bad WAV file (no DATA chunk)");
return SDL_FALSE;
}
return SDL_TRUE;
466
467
}
468
469
470
471
472
/* 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)
473
{
474
475
476
/* Negative number? */
if (sanebuf[0] & 0x80)
return 0;
477
478
479
480
/* Less than 1? */
if (sanebuf[0] <= 0x3F)
return 1;
481
482
483
484
/* Way too big? */
if (sanebuf[0] > 0x40)
return 0x4000000;
485
486
487
488
/* Still too big? */
if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
return 800000000;
489
490
491
return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) |
(sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
492
493
}
494
static SDL_bool LoadAIFFStream(WAVStream *wave)
495
{
496
497
498
499
SDL_RWops *src = wave->src;
SDL_AudioSpec *spec = &wave->spec;
SDL_bool found_SSND = SDL_FALSE;
SDL_bool found_COMM = SDL_FALSE;
500
501
502
Uint32 chunk_type;
Uint32 chunk_length;
503
Sint64 next_chunk;
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* 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 */
518
519
520
chunk_length = SDL_ReadBE32(src);
AIFFmagic = SDL_ReadLE32(src);
if (AIFFmagic != AIFF) {
521
Mix_SetError("Unrecognized file type (not AIFF)");
522
return SDL_FALSE;
523
524
525
}
/* From what I understand of the specification, chunks may appear in
526
* any order, and we should just ignore unknown ones.
527
528
529
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
530
*/
531
532
533
534
do {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
next_chunk = SDL_RWtell(src) + chunk_length;
535
536
537
/* Paranoia to avoid infinite loops */
if (chunk_length == 0)
538
break;
539
540
switch (chunk_type) {
541
case SSND:
542
543
544
545
found_SSND = SDL_TRUE;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
wave->start = SDL_RWtell(src) + offset;
546
547
548
break;
case COMM:
549
found_COMM = SDL_TRUE;
550
551
/* Read the audio data format chunk */
552
553
554
channels = SDL_ReadBE16(src);
numsamples = SDL_ReadBE32(src);
samplesize = SDL_ReadBE16(src);
555
SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
556
frequency = SANE_to_Uint32(sane_freq);
557
558
559
560
561
562
563
564
565
566
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)");
567
return SDL_FALSE;
568
569
570
571
}
if (!found_COMM) {
Mix_SetError("Bad AIFF file (no COMM chunk)");
572
return SDL_FALSE;
573
574
}
575
wave->stop = wave->start + channels * numsamples * (samplesize / 8);
576
577
/* Decode the audio data format */
578
SDL_memset(spec, 0, (sizeof *spec));
579
580
581
582
583
584
585
586
587
588
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");
589
return SDL_FALSE;
590
591
592
}
spec->channels = (Uint8) channels;
spec->samples = 4096; /* Good default buffer size */
593
594
return SDL_TRUE;
595
}
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
Mix_MusicInterface Mix_MusicInterface_WAV =
{
"WAVE",
MIX_MUSIC_WAVE,
MUS_WAV,
SDL_FALSE,
SDL_FALSE,
NULL, /* Load */
NULL, /* Open */
WAVStream_CreateFromRW,
NULL, /* CreateFromFile */
WAVStream_SetVolume,
WAVStream_Play,
NULL, /* IsPlaying */
WAVStream_GetAudio,
NULL, /* Seek */
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
WAVStream_Delete,
NULL, /* Close */
NULL, /* Unload */
};
#endif /* MUSIC_WAV */
/* vi: set ts=4 sw=4 expandtab: */