Skip to content

Latest commit

 

History

History
1110 lines (993 loc) · 33.9 KB

music_wav.c

File metadata and controls

1110 lines (993 loc) · 33.9 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 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 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
typedef struct {
SDL_bool active;
Uint32 start;
Uint32 stop;
Uint32 initial_play_count;
Uint32 current_play_count;
} WAVLoopPoint;
typedef struct {
SDL_RWops *src;
Nov 18, 2019
Nov 18, 2019
39
int freesrc;
Oct 17, 2017
Oct 17, 2017
40
41
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;
Dec 4, 2019
Dec 4, 2019
45
Sint64 samplesize;
Oct 21, 2017
Oct 21, 2017
46
47
Uint8 *buffer;
SDL_AudioStream *stream;
Nov 18, 2019
Nov 18, 2019
48
unsigned int numloops;
Oct 17, 2017
Oct 17, 2017
49
WAVLoopPoint *loops;
Dec 4, 2019
Dec 4, 2019
50
51
Uint16 encoding;
int (*decode)(void *music, int length);
Oct 21, 2017
Oct 21, 2017
52
} WAV_Music;
Oct 21, 1999
Oct 21, 1999
53
Sep 11, 2001
Sep 11, 2001
54
55
56
57
58
59
60
61
62
63
64
/*
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
65
66
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
Jun 22, 2014
Jun 22, 2014
67
#define FMT 0x20746D66 /* "fmt " */
May 22, 2013
May 22, 2013
68
#define DATA 0x61746164 /* "data" */
Jul 7, 2015
Jul 7, 2015
69
#define SMPL 0x6c706d73 /* "smpl" */
Dec 4, 2019
Dec 4, 2019
70
71
72
73
74
75
76
77
#define LIST 0x5453494c /* "LIST" */
#define ID3_ 0x20336469 /* "id3 " */
#define PCM_CODE 1 /* WAVE_FORMAT_PCM */
#define ADPCM_CODE 2 /* WAVE_FORMAT_ADPCM */
#define FLOAT_CODE 3 /* WAVE_FORMAT_IEEE_FLOAT */
#define ALAW_CODE 6 /* WAVE_FORMAT_ALAW */
#define uLAW_CODE 7 /* WAVE_FORMAT_MULAW */
#define EXT_CODE 0xFFFE /* WAVE_FORMAT_EXTENSIBLE */
May 22, 2013
May 22, 2013
78
79
#define WAVE_MONO 1
#define WAVE_STEREO 2
Sep 11, 2001
Sep 11, 2001
80
Jul 7, 2015
Jul 7, 2015
81
typedef struct {
Sep 11, 2001
Sep 11, 2001
82
/* Not saved in the chunk we read:
Jul 7, 2015
Jul 7, 2015
83
84
Uint32 chunkID;
Uint32 chunkLen;
Sep 11, 2001
Sep 11, 2001
85
*/
May 22, 2013
May 22, 2013
86
87
88
89
90
91
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
92
93
} WaveFMT;
Dec 4, 2019
Dec 4, 2019
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
typedef struct {
Uint16 cbSize;
union {
Uint16 validbitspersample; /* bits of precision */
Uint16 samplesperblock; /* valid if wBitsPerSample==0 */
Uint16 reserved; /* If neither applies, set to zero. */
} Samples;
Uint32 channelsmask;
/* GUID subFormat 16 bytes */
Uint32 subencoding;
Uint16 sub_data2;
Uint16 sub_data3;
Uint8 sub_data[8];
} WaveFMTex;
Jul 7, 2015
Jul 7, 2015
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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;
Oct 21, 2017
Oct 21, 2017
132
SampleLoop loops[1];
Jul 7, 2015
Jul 7, 2015
133
} SamplerChunk;
Sep 11, 2001
Sep 11, 2001
134
135
136
137
/*********************************************/
/* Define values for AIFF (IFF audio) format */
/*********************************************/
May 22, 2013
May 22, 2013
138
139
#define FORM 0x4d524f46 /* "FORM" */
#define AIFF 0x46464941 /* "AIFF" */
Dec 4, 2019
Dec 4, 2019
140
141
#define AIFC 0x43464941 /* "AIFС" */
#define FVER 0x52455646 /* "FVER" */
May 22, 2013
May 22, 2013
142
143
#define SSND 0x444e5353 /* "SSND" */
#define COMM 0x4d4d4f43 /* "COMM" */
Dec 4, 2019
Dec 4, 2019
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define AIFF_ID3_ 0x20334449 /* "ID3 " */
#define MARK 0x4B52414D /* "MARK" */
#define INST 0x54534E49 /* "INST" */
#define AUTH 0x48545541 /* "AUTH" */
#define NAME 0x454D414E /* "NAME" */
#define _c__ 0x20296328 /* "(c) " */
/* Supported compression types */
#define NONE 0x454E4F4E /* "NONE" */
#define sowt 0x74776F73 /* "sowt" */
#define raw_ 0x20776172 /* "raw " */
#define ulaw 0x77616C75 /* "ulaw" */
#define alaw 0x77616C61 /* "alaw" */
#define ULAW 0x57414C55 /* "ULAW" */
#define ALAW 0x57414C41 /* "ALAW" */
#define fl32 0x32336C66 /* "fl32" */
#define fl64 0x34366C66 /* "fl64" */
#define FL32 0x32334C46 /* "FL32" */
Sep 11, 2001
Sep 11, 2001
162
Oct 21, 1999
Oct 21, 1999
163
/* Function to load the WAV/AIFF stream */
Oct 21, 2017
Oct 21, 2017
164
165
static SDL_bool LoadWAVMusic(WAV_Music *wave);
static SDL_bool LoadAIFFMusic(WAV_Music *wave);
Oct 21, 1999
Oct 21, 1999
166
Oct 21, 2017
Oct 21, 2017
167
static void WAV_Delete(void *context);
Oct 21, 1999
Oct 21, 1999
168
Dec 4, 2019
Dec 4, 2019
169
170
static int fetch_pcm(void *context, int length);
Jan 4, 2012
Jan 4, 2012
171
/* Load a WAV stream from the given RWops object */
Oct 21, 2017
Oct 21, 2017
172
static void *WAV_CreateFromRW(SDL_RWops *src, int freesrc)
Oct 21, 1999
Oct 21, 1999
173
{
Oct 21, 2017
Oct 21, 2017
174
175
WAV_Music *music;
Uint32 magic;
Jul 7, 2015
Jul 7, 2015
176
SDL_bool loaded = SDL_FALSE;
May 22, 2013
May 22, 2013
177
Oct 21, 2017
Oct 21, 2017
178
179
180
181
182
music = (WAV_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
Oct 21, 2017
Oct 21, 2017
183
music->src = src;
Oct 21, 2017
Oct 21, 2017
184
music->volume = MIX_MAX_VOLUME;
Dec 4, 2019
Dec 4, 2019
185
186
187
/* Default decoder is PCM */
music->decode = fetch_pcm;
music->encoding = PCM_CODE;
Jun 2, 2013
Jun 2, 2013
188
Oct 21, 2017
Oct 21, 2017
189
190
191
192
193
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
194
} else {
Oct 21, 2017
Oct 21, 2017
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
212
}
Oct 21, 2017
Oct 21, 2017
213
Nov 18, 2019
Nov 18, 2019
214
music->freesrc = freesrc;
Oct 21, 2017
Oct 21, 2017
215
return music;
Oct 17, 2017
Oct 17, 2017
216
217
}
Oct 21, 2017
Oct 21, 2017
218
static void WAV_SetVolume(void *context, int volume)
Oct 17, 2017
Oct 17, 2017
219
{
Oct 21, 2017
Oct 21, 2017
220
221
WAV_Music *music = (WAV_Music *)context;
music->volume = volume;
Oct 21, 1999
Oct 21, 1999
222
223
224
}
/* Start playback of a given WAV stream */
Oct 21, 2017
Oct 21, 2017
225
static int WAV_Play(void *context, int play_count)
Oct 21, 1999
Oct 21, 1999
226
{
Oct 21, 2017
Oct 21, 2017
227
WAV_Music *music = (WAV_Music *)context;
Nov 18, 2019
Nov 18, 2019
228
unsigned int i;
Oct 21, 2017
Oct 21, 2017
229
230
for (i = 0; i < music->numloops; ++i) {
WAVLoopPoint *loop = &music->loops[i];
Jul 7, 2015
Jul 7, 2015
231
232
233
loop->active = SDL_TRUE;
loop->current_play_count = loop->initial_play_count;
}
Oct 21, 2017
Oct 21, 2017
234
music->play_count = play_count;
Oct 21, 2017
Oct 21, 2017
235
236
237
238
if (SDL_RWseek(music->src, music->start, RW_SEEK_SET) < 0) {
return -1;
}
return 0;
Oct 21, 1999
Oct 21, 1999
239
240
}
Dec 4, 2019
Dec 4, 2019
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
static int fetch_pcm(void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
return (int)SDL_RWread(music->src, music->buffer, 1, (size_t)length);
}
static Uint32 PCM_S24_to_S32_BE(Uint8 *x) {
const Uint32 bits = 24;
Uint32 in = (((Uint32)x[0] << 0) & 0x0000FF) |
(((Uint32)x[1] << 8) & 0x00FF00) |
(((Uint32)x[2] << 16) & 0xFF0000);
Uint32 m = 1u << (bits - 1);
return (in ^ m) - m;
}
static Uint32 PCM_S24_to_S32_LE(Uint8 *x) {
const Uint32 bits = 24;
Uint32 in = (((Uint32)x[2] << 0) & 0x0000FF) |
(((Uint32)x[1] << 8) & 0x00FF00) |
(((Uint32)x[0] << 16) & 0xFF0000);
Uint32 m = 1u << (bits - 1);
return (in ^ m) - m;
}
static int fetch_pcm24be(void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
int i = 0, o = 0;
length = (int)SDL_RWread(music->src, music->buffer, 1, (size_t)((length / 4) * 3));
if (length % music->samplesize != 0) {
length -= length % music->samplesize;
}
for (i = length - 3, o = ((length - 3) / 3) * 4; i >= 0; i -= 3, o -= 4) {
Uint32 decoded = PCM_S24_to_S32_BE(music->buffer + i);
music->buffer[o + 0] = (decoded >> 0) & 0xFF;
music->buffer[o + 1] = (decoded >> 8) & 0xFF;
music->buffer[o + 2] = (decoded >> 16) & 0xFF;
music->buffer[o + 3] = (decoded >> 24) & 0xFF;
}
return (length / 3) * 4;
}
static int fetch_pcm24le(void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
int i = 0, o = 0;
length = (int)SDL_RWread(music->src, music->buffer, 1, (size_t)((length / 4) * 3));
if (length % music->samplesize != 0) {
length -= length % music->samplesize;
}
for (i = length - 3, o = ((length - 3) / 3) * 4; i >= 0; i -= 3, o -= 4) {
Uint32 decoded = PCM_S24_to_S32_LE(music->buffer + i);
music->buffer[o + 3] = (decoded >> 0) & 0xFF;
music->buffer[o + 2] = (decoded >> 8) & 0xFF;
music->buffer[o + 1] = (decoded >> 16) & 0xFF;
music->buffer[o + 0] = (decoded >> 24) & 0xFF;
}
return (length / 3) * 4;
}
SDL_FORCE_INLINE double
Mix_SwapDouble(double x)
{
union
{
double f;
Uint64 ui64;
} swapper;
swapper.f = x;
swapper.ui64 = SDL_Swap64(swapper.ui64);
return swapper.f;
}
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define Mix_SwapDoubleLE(X) (X)
#define Mix_SwapDoubleBE(X) Mix_SwapDouble(X)
#else
#define Mix_SwapDoubleLE(X) Mix_SwapDouble(X)
#define Mix_SwapDoubleBE(X) (X)
#endif
static int fetch_float64be(void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
int i = 0, o = 0;
length = (int)SDL_RWread(music->src, music->buffer, 1, (size_t)(length));
if (length % music->samplesize != 0) {
length -= length % music->samplesize;
}
for (i = 0, o = 0; i <= length; i += 8, o += 4) {
union
{
float f;
Uint32 ui32;
} sample;
sample.f = (float)Mix_SwapDoubleBE(*(double*)(music->buffer + i));
music->buffer[o + 0] = (sample.ui32 >> 0) & 0xFF;
music->buffer[o + 1] = (sample.ui32 >> 8) & 0xFF;
music->buffer[o + 2] = (sample.ui32 >> 16) & 0xFF;
music->buffer[o + 3] = (sample.ui32 >> 24) & 0xFF;
}
return length / 2;
}
static int fetch_float64le(void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
int i = 0, o = 0;
length = (int)SDL_RWread(music->src, music->buffer, 1, (size_t)(length));
if (length % music->samplesize != 0) {
length -= length % music->samplesize;
}
for (i = 0, o = 0; i <= length; i += 8, o += 4) {
union
{
float f;
Uint32 ui32;
} sample;
sample.f = (float)Mix_SwapDoubleLE(*(double*)(music->buffer + i));
music->buffer[o + 0] = (sample.ui32 >> 0) & 0xFF;
music->buffer[o + 1] = (sample.ui32 >> 8) & 0xFF;
music->buffer[o + 2] = (sample.ui32 >> 16) & 0xFF;
music->buffer[o + 3] = (sample.ui32 >> 24) & 0xFF;
}
return length / 2;
}
/*
G711 decode tables taken from SDL2 (src/audio/SDL_wave.c)
*/
#ifdef SDL_WAVE_LAW_LUT
static const Sint16 alaw_lut[256] = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
-2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
-20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
-10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
-328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
-72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
-1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
-656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
};
static const Sint16 mulaw_lut[256] = {
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
-15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
-7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
-3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
-1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
-844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
-356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
-112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
};
#endif
static Sint16 uLAW_To_PCM16(Uint8 u_val)
{
#ifdef SDL_WAVE_LAW_LUT
return mulaw_lut[u_val];
#else
Uint8 nibble = ~u_val;
Sint16 mantissa = nibble & 0xf;
Uint8 exponent = (nibble >> 4) & 0x7;
Sint16 step = (Sint16)(4 << (exponent + 1));
mantissa = (Sint16)(0x80 << exponent) + step * mantissa + step / 2 - 132;
return nibble & 0x80 ? -mantissa : mantissa;
#endif
}
static Sint16 ALAW_To_PCM16(Uint8 a_val)
{
#ifdef SDL_WAVE_LAW_LUT
return alaw_lut[a_val];
#else
Uint8 nibble = a_val;
Uint8 exponent = (nibble & 0x7f) ^ 0x55;
Sint16 mantissa = exponent & 0xf;
exponent >>= 4;
if (exponent > 0) {
mantissa |= 0x10;
}
mantissa = (Sint16)(mantissa << 4) | 0x8;
if (exponent > 1) {
mantissa <<= exponent - 1;
}
return nibble & 0x80 ? mantissa : -mantissa;
#endif
}
static int fetch_xlaw(Sint16 (*decode_sample)(Uint8), void *context, int length)
{
WAV_Music *music = (WAV_Music *)context;
int i = 0, o = 0;
length = (int)SDL_RWread(music->src, music->buffer, 1, (size_t)(length / 2));
if (length % music->samplesize != 0) {
length -= length % music->samplesize;
}
for (i = length - 1, o = (length - 1) * 2; i >= 0; i--, o -= 2) {
Uint16 decoded = (Uint16)decode_sample(music->buffer[i]);
music->buffer[o] = decoded & 0xFF;
music->buffer[o + 1] = (decoded >> 8) & 0xFF;
}
return length * 2;
}
static int fetch_ulaw(void *context, int length)
{
return fetch_xlaw(uLAW_To_PCM16, context, length);
}
static int fetch_alaw(void *context, int length)
{
return fetch_xlaw(ALAW_To_PCM16, context, length);
}
Oct 21, 2017
Oct 21, 2017
475
476
/* 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
477
{
Oct 21, 2017
Oct 21, 2017
478
WAV_Music *music = (WAV_Music *)context;
Jul 7, 2015
Jul 7, 2015
479
480
Sint64 pos, stop;
WAVLoopPoint *loop;
Dec 4, 2019
Dec 4, 2019
481
482
Sint64 loop_start = music->start;
Sint64 loop_stop = music->stop;
Oct 21, 2017
Oct 21, 2017
483
SDL_bool looped = SDL_FALSE;
Dec 4, 2019
Dec 4, 2019
484
SDL_bool at_end = SDL_FALSE;
Nov 18, 2019
Nov 18, 2019
485
unsigned int i;
Oct 21, 2017
Oct 21, 2017
486
int filled, amount, result;
Jul 7, 2015
Jul 7, 2015
487
Oct 21, 2017
Oct 21, 2017
488
489
490
491
492
493
494
495
496
497
498
499
500
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
501
loop = NULL;
Oct 21, 2017
Oct 21, 2017
502
503
for (i = 0; i < music->numloops; ++i) {
loop = &music->loops[i];
Jul 7, 2015
Jul 7, 2015
504
if (loop->active) {
Oct 21, 2017
Oct 21, 2017
505
const int bytes_per_sample = (SDL_AUDIO_BITSIZE(music->spec.format) / 8) * music->spec.channels;
Dec 4, 2019
Dec 4, 2019
506
507
loop_start = music->start + loop->start * (Uint32)bytes_per_sample;
loop_stop = music->start + (loop->stop + 1) * (Uint32)bytes_per_sample;
Nov 18, 2019
Nov 18, 2019
508
if (pos >= loop_start && pos < loop_stop) {
Jul 7, 2015
Jul 7, 2015
509
510
stop = loop_stop;
break;
May 22, 2013
May 22, 2013
511
}
Jul 7, 2015
Jul 7, 2015
512
513
514
515
}
loop = NULL;
}
Dec 4, 2019
Dec 4, 2019
516
amount = (int)music->spec.size;
Oct 21, 2017
Oct 21, 2017
517
518
519
if ((stop - pos) < amount) {
amount = (int)(stop - pos);
}
Dec 4, 2019
Dec 4, 2019
520
521
amount = music->decode(music, amount);
Oct 21, 2017
Oct 21, 2017
522
523
524
525
if (amount > 0) {
result = SDL_AudioStreamPut(music->stream, music->buffer, amount);
if (result < 0) {
return -1;
Jul 7, 2015
Jul 7, 2015
526
527
}
} else {
Oct 21, 2017
Oct 21, 2017
528
/* We might be looping, continue */
Dec 4, 2019
Dec 4, 2019
529
at_end = SDL_TRUE;
Jul 7, 2015
Jul 7, 2015
530
531
}
Oct 21, 2017
Oct 21, 2017
532
if (loop && SDL_RWtell(music->src) >= stop) {
Jul 7, 2015
Jul 7, 2015
533
534
if (loop->current_play_count == 1) {
loop->active = SDL_FALSE;
May 22, 2013
May 22, 2013
535
} else {
Jul 7, 2015
Jul 7, 2015
536
537
if (loop->current_play_count > 0) {
--loop->current_play_count;
May 22, 2013
May 22, 2013
538
}
Nov 18, 2019
Nov 18, 2019
539
SDL_RWseek(music->src, loop_start, RW_SEEK_SET);
Oct 21, 2017
Oct 21, 2017
540
looped = SDL_TRUE;
May 22, 2013
May 22, 2013
541
542
}
}
Jul 7, 2015
Jul 7, 2015
543
Dec 4, 2019
Dec 4, 2019
544
if (!looped && (at_end || SDL_RWtell(music->src) >= music->stop)) {
Oct 21, 2017
Oct 21, 2017
545
546
547
548
549
550
551
552
553
554
555
556
557
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
558
Oct 21, 2017
Oct 21, 2017
559
560
561
/* We'll get called again in the case where we looped or have more data */
return 0;
}
Jul 7, 2015
Jul 7, 2015
562
Oct 21, 2017
Oct 21, 2017
563
564
565
566
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
567
568
}
Dec 4, 2019
Dec 4, 2019
569
570
571
572
573
574
575
576
577
578
579
580
581
static int WAV_Seek(void *context, double position)
{
WAV_Music *music = (WAV_Music *)context;
Sint64 sample_size = music->spec.freq * music->samplesize;
Sint64 dest_offset = (Sint64)(position * (double)music->spec.freq * music->samplesize);
Sint64 destpos = music->start + dest_offset;
destpos -= dest_offset % sample_size;
if (destpos > music->stop)
return -1;
SDL_RWseek(music->src, destpos, RW_SEEK_SET);
return 0;
}
Dec 17, 2019
Dec 17, 2019
582
583
584
585
586
587
588
589
/* Return music duration in seconds */
static double WAV_Duration(void *context)
{
WAV_Music *music = (WAV_Music *)context;
Sint64 sample_size = music->spec.freq * music->samplesize;
return (double)(music->stop - music->start) / sample_size;
}
Oct 21, 1999
Oct 21, 1999
590
/* Close the given WAV stream */
Oct 21, 2017
Oct 21, 2017
591
static void WAV_Delete(void *context)
Oct 21, 1999
Oct 21, 1999
592
{
Oct 21, 2017
Oct 21, 2017
593
WAV_Music *music = (WAV_Music *)context;
Oct 21, 1999
Oct 21, 1999
594
Oct 17, 2017
Oct 17, 2017
595
/* Clean up associated data */
Oct 21, 2017
Oct 21, 2017
596
597
598
599
600
if (music->loops) {
SDL_free(music->loops);
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
Oct 17, 2017
Oct 17, 2017
601
}
Oct 21, 2017
Oct 21, 2017
602
603
if (music->buffer) {
SDL_free(music->buffer);
May 22, 2013
May 22, 2013
604
}
Oct 21, 2017
Oct 21, 2017
605
606
if (music->freesrc) {
SDL_RWclose(music->src);
Oct 17, 2017
Oct 17, 2017
607
}
Oct 21, 2017
Oct 21, 2017
608
SDL_free(music);
Oct 21, 1999
Oct 21, 1999
609
610
}
Oct 21, 2017
Oct 21, 2017
611
static SDL_bool ParseFMT(WAV_Music *wave, Uint32 chunk_length)
Oct 21, 1999
Oct 21, 1999
612
{
Jul 7, 2015
Jul 7, 2015
613
614
SDL_AudioSpec *spec = &wave->spec;
WaveFMT *format;
Dec 4, 2019
Dec 4, 2019
615
WaveFMTex *formatEx = NULL;
Jul 7, 2015
Jul 7, 2015
616
Uint8 *data;
Dec 4, 2019
Dec 4, 2019
617
Uint16 bitsamplerate;
Jul 7, 2015
Jul 7, 2015
618
619
620
621
622
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
623
624
}
Jul 7, 2015
Jul 7, 2015
625
626
627
628
629
630
631
632
633
634
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
635
Dec 4, 2019
Dec 4, 2019
636
637
638
639
640
641
642
wave->encoding = SDL_SwapLE16(format->encoding);
if (wave->encoding == EXT_CODE) {
formatEx = (WaveFMTex*)(data + sizeof(WaveFMT));
wave->encoding = (Uint16)SDL_SwapLE32(formatEx->subencoding);
}
May 22, 2013
May 22, 2013
643
/* Decode the audio data format */
Dec 4, 2019
Dec 4, 2019
644
switch (wave->encoding) {
May 22, 2013
May 22, 2013
645
case PCM_CODE:
Dec 4, 2019
Dec 4, 2019
646
case FLOAT_CODE:
May 22, 2013
May 22, 2013
647
/* We can understand this */
Dec 4, 2019
Dec 4, 2019
648
649
650
651
652
653
654
655
656
wave->decode = fetch_pcm;
break;
case uLAW_CODE:
/* , this */
wave->decode = fetch_ulaw;
break;
case ALAW_CODE:
/* , and this */
wave->decode = fetch_alaw;
May 22, 2013
May 22, 2013
657
658
break;
default:
Dec 4, 2019
Dec 4, 2019
659
/* but NOT this */
May 22, 2013
May 22, 2013
660
661
662
Mix_SetError("Unknown WAVE data format");
goto done;
}
Dec 4, 2019
Dec 4, 2019
663
664
665
spec->freq = (int)SDL_SwapLE32(format->frequency);
bitsamplerate = SDL_SwapLE16(format->bitspersample);
switch (bitsamplerate) {
May 22, 2013
May 22, 2013
666
case 8:
Dec 4, 2019
Dec 4, 2019
667
668
669
670
671
672
switch(wave->encoding) {
case PCM_CODE: spec->format = AUDIO_U8; break;
case ALAW_CODE: spec->format = AUDIO_S16; break;
case uLAW_CODE: spec->format = AUDIO_S16; break;
default: goto unknown_length;
}
May 22, 2013
May 22, 2013
673
674
break;
case 16:
Dec 4, 2019
Dec 4, 2019
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
switch(wave->encoding) {
case PCM_CODE: spec->format = AUDIO_S16; break;
default: goto unknown_length;
}
break;
case 24:
switch(wave->encoding) {
case PCM_CODE:
wave->decode = fetch_pcm24le;
spec->format = AUDIO_S32;
break;
default: goto unknown_length;
}
case 32:
switch(wave->encoding) {
case PCM_CODE: spec->format = AUDIO_S32; break;
case FLOAT_CODE: spec->format = AUDIO_F32; break;
default: goto unknown_length;
}
break;
case 64:
switch(wave->encoding) {
case FLOAT_CODE:
wave->decode = fetch_float64le;
spec->format = AUDIO_F32;
break;
default: goto unknown_length;
}
May 22, 2013
May 22, 2013
703
704
break;
default:
Dec 4, 2019
Dec 4, 2019
705
706
unknown_length:
Mix_SetError("Unknown PCM data format of %d-bit length", (int)bitsamplerate);
May 22, 2013
May 22, 2013
707
708
709
710
goto done;
}
spec->channels = (Uint8) SDL_SwapLE16(format->channels);
spec->samples = 4096; /* Good default buffer size */
Dec 4, 2019
Dec 4, 2019
711
wave->samplesize = spec->channels * (bitsamplerate / 8);
Oct 21, 2017
Oct 21, 2017
712
713
714
715
/* SDL_CalculateAudioSpec */
spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
spec->size *= spec->channels;
spec->size *= spec->samples;
May 22, 2013
May 22, 2013
716
Jul 7, 2015
Jul 7, 2015
717
718
719
720
721
722
723
loaded = SDL_TRUE;
done:
SDL_free(data);
return loaded;
}
Oct 21, 2017
Oct 21, 2017
724
static SDL_bool ParseDATA(WAV_Music *wave, Uint32 chunk_length)
Jul 7, 2015
Jul 7, 2015
725
726
727
728
729
730
731
{
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
732
static SDL_bool AddLoopPoint(WAV_Music *wave, Uint32 play_count, Uint32 start, Uint32 stop)
Jul 7, 2015
Jul 7, 2015
733
734
{
WAVLoopPoint *loop;
Nov 18, 2019
Nov 18, 2019
735
WAVLoopPoint *loops = SDL_realloc(wave->loops, (wave->numloops + 1) * sizeof(*wave->loops));
Jul 7, 2015
Jul 7, 2015
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
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
752
static SDL_bool ParseSMPL(WAV_Music *wave, Uint32 chunk_length)
Jul 7, 2015
Jul 7, 2015
753
754
755
{
SamplerChunk *chunk;
Uint8 *data;
Oct 13, 2017
Oct 13, 2017
756
Uint32 i;
Jul 7, 2015
Jul 7, 2015
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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
775
}
Jul 7, 2015
Jul 7, 2015
776
777
778
779
780
781
782
}
loaded = SDL_TRUE;
SDL_free(data);
return loaded;
}
Oct 21, 2017
Oct 21, 2017
783
static SDL_bool LoadWAVMusic(WAV_Music *wave)
Jul 7, 2015
Jul 7, 2015
784
785
786
787
788
789
790
791
792
793
794
795
796
797
{
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);
Nov 18, 2019
Nov 18, 2019
798
799
800
(void)wavelen; /* unused */
(void)WAVEmagic; /* unused */
Jul 7, 2015
Jul 7, 2015
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* 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
829
}
Jul 7, 2015
Jul 7, 2015
830
831
832
833
if (!found_FMT) {
Mix_SetError("Bad WAV file (no FMT chunk)");
return SDL_FALSE;
May 22, 2013
May 22, 2013
834
}
Jul 7, 2015
Jul 7, 2015
835
836
837
838
839
840
841
if (!found_DATA) {
Mix_SetError("Bad WAV file (no DATA chunk)");
return SDL_FALSE;
}
return SDL_TRUE;
Oct 21, 1999
Oct 21, 1999
842
843
}
Aug 19, 2001
Aug 19, 2001
844
845
846
847
848
/* 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
849
{
May 22, 2013
May 22, 2013
850
851
852
/* Negative number? */
if (sanebuf[0] & 0x80)
return 0;
Aug 19, 2001
Aug 19, 2001
853
May 22, 2013
May 22, 2013
854
855
856
/* Less than 1? */
if (sanebuf[0] <= 0x3F)
return 1;
Aug 19, 2001
Aug 19, 2001
857
May 22, 2013
May 22, 2013
858
859
860
/* Way too big? */
if (sanebuf[0] > 0x40)
return 0x4000000;
Aug 19, 2001
Aug 19, 2001
861
May 22, 2013
May 22, 2013
862
863
864
/* Still too big? */
if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
return 800000000;
Aug 19, 2001
Aug 19, 2001
865
Nov 17, 2019
Nov 17, 2019
866
867
return (Uint32)(((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7) |
(sanebuf[5] >> 1)) >> (29 - sanebuf[1]));
Oct 21, 1999
Oct 21, 1999
868
869
}
Oct 21, 2017
Oct 21, 2017
870
static SDL_bool LoadAIFFMusic(WAV_Music *wave)
Oct 21, 1999
Oct 21, 1999
871
{
Jul 7, 2015
Jul 7, 2015
872
873
874
875
SDL_RWops *src = wave->src;
SDL_AudioSpec *spec = &wave->spec;
SDL_bool found_SSND = SDL_FALSE;
SDL_bool found_COMM = SDL_FALSE;
Dec 4, 2019
Dec 4, 2019
876
877
SDL_bool found_FVER = SDL_FALSE;
SDL_bool is_AIFC = SDL_FALSE;
May 22, 2013
May 22, 2013
878
879
880
Uint32 chunk_type;
Uint32 chunk_length;
Dec 4, 2019
Dec 4, 2019
881
882
883
Sint64 next_chunk = 0;
Sint64 file_length;
May 22, 2013
May 22, 2013
884
885
886
887
888
889
890
891
892
893
894
/* 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;
Dec 4, 2019
Dec 4, 2019
895
896
Uint32 AIFCVersion1 = 0;
Uint32 compressionType = 0;
May 22, 2013
May 22, 2013
897
Dec 18, 2019
Dec 18, 2019
898
899
file_length = SDL_RWsize(src);
May 22, 2013
May 22, 2013
900
/* Check the magic header */
Jul 7, 2015
Jul 7, 2015
901
902
chunk_length = SDL_ReadBE32(src);
AIFFmagic = SDL_ReadLE32(src);
Dec 4, 2019
Dec 4, 2019
903
904
if (AIFFmagic != AIFF && AIFFmagic != AIFC) {
Mix_SetError("Unrecognized file type (not AIFF or AIFC)");
Jul 7, 2015
Jul 7, 2015
905
return SDL_FALSE;
May 22, 2013
May 22, 2013
906
}
Dec 4, 2019
Dec 4, 2019
907
908
909
if (AIFFmagic == AIFC) {
is_AIFC = SDL_TRUE;
}
May 22, 2013
May 22, 2013
910
911
/* From what I understand of the specification, chunks may appear in
Jul 7, 2015
Jul 7, 2015
912
* any order, and we should just ignore unknown ones.
May 22, 2013
May 22, 2013
913
914
915
*
* TODO: Better sanity-checking. E.g. what happens if the AIFF file
* contains compressed sound data?
Jun 2, 2013
Jun 2, 2013
916
*/
May 22, 2013
May 22, 2013
917
918
919
920
do {
chunk_type = SDL_ReadLE32(src);
chunk_length = SDL_ReadBE32(src);
next_chunk = SDL_RWtell(src) + chunk_length;
Aug 19, 2001
Aug 19, 2001
921
Dec 4, 2019
Dec 4, 2019
922
923
924
if (chunk_length % 2) {
next_chunk++;
}
Aug 19, 2001
Aug 19, 2001
925
Jun 2, 2013
Jun 2, 2013
926
switch (chunk_type) {
May 22, 2013
May 22, 2013
927
case SSND:
Jul 7, 2015
Jul 7, 2015
928
929
930
931
found_SSND = SDL_TRUE;
offset = SDL_ReadBE32(src);
blocksize = SDL_ReadBE32(src);
wave->start = SDL_RWtell(src) + offset;
Dec 4, 2019
Dec 4, 2019
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
(void)blocksize; /* unused */
break;
case FVER:
found_FVER = SDL_TRUE;
AIFCVersion1 = SDL_ReadBE32(src);
(void)AIFCVersion1; /* unused */
break;
case MARK:
case INST:
/* Just skip those chunks */
break;
case NAME:
case AUTH:
case _c__:
/* Just skip those chunks */
May 22, 2013
May 22, 2013
950
951
952
break;
case COMM:
Jul 7, 2015
Jul 7, 2015
953
found_COMM = SDL_TRUE;
May 22, 2013
May 22, 2013
954
955
/* Read the audio data format chunk */
Jul 7, 2015
Jul 7, 2015
956
957
958
channels = SDL_ReadBE16(src);
numsamples = SDL_ReadBE32(src);
samplesize = SDL_ReadBE16(src);
May 22, 2013
May 22, 2013
959
SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
Jul 7, 2015
Jul 7, 2015
960
frequency = SANE_to_Uint32(sane_freq);
Dec 4, 2019
Dec 4, 2019
961
962
963
964
if (is_AIFC) {
compressionType = SDL_ReadLE32(src);
/* here must be a "compressionName" which is a padded string */
}
May 22, 2013
May 22, 2013
965
966
967
break;
default:
Dec 4, 2019
Dec 4, 2019
968
/* Unknown/unsupported chunk: we just skip over */
May 22, 2013
May 22, 2013
969
970
break;
}
Dec 4, 2019
Dec 4, 2019
971
} while (next_chunk < file_length && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1);
May 22, 2013
May 22, 2013
972
973
if (!found_SSND) {
Dec 4, 2019
Dec 4, 2019
974
Mix_SetError("Bad AIFF/AIFF-C file (no SSND chunk)");
Jul 7, 2015
Jul 7, 2015
975
return SDL_FALSE;
May 22, 2013
May 22, 2013
976
977
978
}
if (!found_COMM) {
Dec 4, 2019
Dec 4, 2019
979
Mix_SetError("Bad AIFF/AIFF-C file (no COMM chunk)");
Jul 7, 2015
Jul 7, 2015
980
return SDL_FALSE;
May 22, 2013
May 22, 2013
981
982
}
Dec 4, 2019
Dec 4, 2019
983
984
985
986
987
988
989
if (is_AIFC && !found_FVER) {
Mix_SetError("Bad AIFF-C file (no FVER chunk)");
return SDL_FALSE;
}
wave->samplesize = channels * (samplesize / 8);
Jul 7, 2015
Jul 7, 2015
990
wave->stop = wave->start + channels * numsamples * (samplesize / 8);
May 22, 2013
May 22, 2013
991
992
/* Decode the audio data format */
Jun 1, 2013
Jun 1, 2013
993
SDL_memset(spec, 0, (sizeof *spec));
Dec 4, 2019
Dec 4, 2019
994
spec->freq = (int)frequency;
May 22, 2013
May 22, 2013
995
switch (samplesize) {
Nov 18, 2019
Nov 18, 2019
996
case 8:
Dec 4, 2019
Dec 4, 2019
997
998
999
1000
if (!is_AIFC)
spec->format = AUDIO_S8;
else switch (compressionType) {
case raw_: spec->format = AUDIO_U8; break;