Skip to content

Latest commit

 

History

History
640 lines (588 loc) · 17.7 KB

SDL_wave.c

File metadata and controls

640 lines (588 loc) · 17.7 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
/* Microsoft WAVE file loading routines */
Feb 7, 2006
Feb 7, 2006
26
#include "SDL_audio.h"
Apr 26, 2001
Apr 26, 2001
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "SDL_wave.h"
static int ReadChunk(SDL_RWops *src, Chunk *chunk);
struct MS_ADPCM_decodestate {
Uint8 hPredictor;
Uint16 iDelta;
Sint16 iSamp1;
Sint16 iSamp2;
};
static struct MS_ADPCM_decoder {
WaveFMT wavefmt;
Uint16 wSamplesPerBlock;
Uint16 wNumCoef;
Sint16 aCoeff[7][2];
/* * * */
struct MS_ADPCM_decodestate state[2];
} MS_ADPCM_state;
Jun 10, 2019
Jun 10, 2019
47
static int InitMS_ADPCM(WaveFMT *format, int length)
Apr 26, 2001
Apr 26, 2001
48
{
Jun 10, 2019
Jun 10, 2019
49
Uint8 *rogue_feel, *rogue_feel_end;
Apr 26, 2001
Apr 26, 2001
50
51
52
int i;
/* Set the rogue pointer to the MS_ADPCM specific data */
Jun 10, 2019
Jun 10, 2019
53
if (length < sizeof(*format)) goto too_short;
Apr 26, 2001
Apr 26, 2001
54
55
56
57
58
59
60
61
MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
MS_ADPCM_state.wavefmt.bitspersample =
SDL_SwapLE16(format->bitspersample);
rogue_feel = (Uint8 *)format+sizeof(*format);
Jun 10, 2019
Jun 10, 2019
62
rogue_feel_end = (Uint8 *)format + length;
Apr 26, 2001
Apr 26, 2001
63
64
65
if ( sizeof(*format) == 16 ) {
rogue_feel += sizeof(Uint16);
}
Jun 10, 2019
Jun 10, 2019
66
if (rogue_feel + 4 > rogue_feel_end) goto too_short;
Apr 26, 2001
Apr 26, 2001
67
68
69
70
71
72
73
74
75
MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
rogue_feel += sizeof(Uint16);
MS_ADPCM_state.wNumCoef = ((rogue_feel[1]<<8)|rogue_feel[0]);
rogue_feel += sizeof(Uint16);
if ( MS_ADPCM_state.wNumCoef != 7 ) {
SDL_SetError("Unknown set of MS_ADPCM coefficients");
return(-1);
}
for ( i=0; i<MS_ADPCM_state.wNumCoef; ++i ) {
Jun 10, 2019
Jun 10, 2019
76
if (rogue_feel + 4 > rogue_feel_end) goto too_short;
Apr 26, 2001
Apr 26, 2001
77
78
79
80
81
82
MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1]<<8)|rogue_feel[0]);
rogue_feel += sizeof(Uint16);
MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1]<<8)|rogue_feel[0]);
rogue_feel += sizeof(Uint16);
}
return(0);
Jun 10, 2019
Jun 10, 2019
83
84
85
too_short:
SDL_SetError("Unexpected length of a chunk with a MS ADPCM format");
return(-1);
Apr 26, 2001
Apr 26, 2001
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
}
static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
Uint8 nybble, Sint16 *coeff)
{
const Sint32 max_audioval = ((1<<(16-1))-1);
const Sint32 min_audioval = -(1<<(16-1));
const Sint32 adaptive[] = {
230, 230, 230, 230, 307, 409, 512, 614,
768, 614, 512, 409, 307, 230, 230, 230
};
Sint32 new_sample, delta;
new_sample = ((state->iSamp1 * coeff[0]) +
(state->iSamp2 * coeff[1]))/256;
if ( nybble & 0x08 ) {
new_sample += state->iDelta * (nybble-0x10);
} else {
new_sample += state->iDelta * nybble;
}
if ( new_sample < min_audioval ) {
new_sample = min_audioval;
} else
if ( new_sample > max_audioval ) {
new_sample = max_audioval;
}
delta = ((Sint32)state->iDelta * adaptive[nybble])/256;
if ( delta < 16 ) {
delta = 16;
}
Feb 24, 2006
Feb 24, 2006
116
state->iDelta = (Uint16)delta;
Apr 26, 2001
Apr 26, 2001
117
state->iSamp2 = state->iSamp1;
Feb 24, 2006
Feb 24, 2006
118
state->iSamp1 = (Sint16)new_sample;
Apr 26, 2001
Apr 26, 2001
119
120
121
122
123
124
return(new_sample);
}
static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
{
struct MS_ADPCM_decodestate *state[2];
Jun 10, 2019
Jun 10, 2019
125
Uint8 *freeable, *encoded, *encoded_end, *decoded, *decoded_end;
Apr 26, 2001
Apr 26, 2001
126
127
128
129
130
131
132
133
Sint32 encoded_len, samplesleft;
Sint8 nybble, stereo;
Sint16 *coeff[2];
Sint32 new_sample;
/* Allocate the proper sized output buffer */
encoded_len = *audio_len;
encoded = *audio_buf;
Jun 10, 2019
Jun 10, 2019
134
encoded_end = encoded + encoded_len;
Apr 26, 2001
Apr 26, 2001
135
136
137
138
freeable = *audio_buf;
*audio_len = (encoded_len/MS_ADPCM_state.wavefmt.blockalign) *
MS_ADPCM_state.wSamplesPerBlock*
MS_ADPCM_state.wavefmt.channels*sizeof(Sint16);
Feb 7, 2006
Feb 7, 2006
139
*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
Apr 26, 2001
Apr 26, 2001
140
141
142
143
144
if ( *audio_buf == NULL ) {
SDL_Error(SDL_ENOMEM);
return(-1);
}
decoded = *audio_buf;
Jun 10, 2019
Jun 10, 2019
145
decoded_end = decoded + *audio_len;
Apr 26, 2001
Apr 26, 2001
146
147
148
149
150
151
152
/* Get ready... Go! */
stereo = (MS_ADPCM_state.wavefmt.channels == 2);
state[0] = &MS_ADPCM_state.state[0];
state[1] = &MS_ADPCM_state.state[stereo];
while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) {
/* Grab the initial information for this block */
Jun 10, 2019
Jun 10, 2019
153
if (encoded + 7 + (stereo ? 7 : 0) > encoded_end) goto invalid_size;
Apr 26, 2001
Apr 26, 2001
154
155
156
157
state[0]->hPredictor = *encoded++;
if ( stereo ) {
state[1]->hPredictor = *encoded++;
}
Jun 10, 2019
Jun 10, 2019
158
159
160
if (state[0]->hPredictor >= 7 || state[1]->hPredictor >= 7) {
goto invalid_predictor;
}
Apr 26, 2001
Apr 26, 2001
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
state[0]->iDelta = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
if ( stereo ) {
state[1]->iDelta = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
}
state[0]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
if ( stereo ) {
state[1]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
}
state[0]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
if ( stereo ) {
state[1]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
encoded += sizeof(Sint16);
}
coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
/* Store the two initial samples we start with */
Jun 10, 2019
Jun 10, 2019
183
if (decoded + 4 + (stereo ? 4 : 0) > decoded_end) goto invalid_size;
Apr 26, 2001
Apr 26, 2001
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
decoded[0] = state[0]->iSamp2&0xFF;
decoded[1] = state[0]->iSamp2>>8;
decoded += 2;
if ( stereo ) {
decoded[0] = state[1]->iSamp2&0xFF;
decoded[1] = state[1]->iSamp2>>8;
decoded += 2;
}
decoded[0] = state[0]->iSamp1&0xFF;
decoded[1] = state[0]->iSamp1>>8;
decoded += 2;
if ( stereo ) {
decoded[0] = state[1]->iSamp1&0xFF;
decoded[1] = state[1]->iSamp1>>8;
decoded += 2;
}
/* Decode and store the other samples in this block */
samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)*
MS_ADPCM_state.wavefmt.channels;
while ( samplesleft > 0 ) {
Jun 10, 2019
Jun 10, 2019
205
206
if (encoded + 1 > encoded_end) goto invalid_size;
if (decoded + 4 > decoded_end) goto invalid_size;
Jun 10, 2019
Jun 10, 2019
207
Apr 26, 2001
Apr 26, 2001
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
nybble = (*encoded)>>4;
new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]);
decoded[0] = new_sample&0xFF;
new_sample >>= 8;
decoded[1] = new_sample&0xFF;
decoded += 2;
nybble = (*encoded)&0x0F;
new_sample = MS_ADPCM_nibble(state[1],nybble,coeff[1]);
decoded[0] = new_sample&0xFF;
new_sample >>= 8;
decoded[1] = new_sample&0xFF;
decoded += 2;
++encoded;
samplesleft -= 2;
}
encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
}
Feb 7, 2006
Feb 7, 2006
227
SDL_free(freeable);
Apr 26, 2001
Apr 26, 2001
228
return(0);
Jun 10, 2019
Jun 10, 2019
229
230
invalid_size:
SDL_SetError("Unexpected chunk length for a MS ADPCM decoder");
Jun 10, 2019
Jun 10, 2019
231
232
SDL_free(freeable);
return(-1);
Jun 10, 2019
Jun 10, 2019
233
234
235
236
invalid_predictor:
SDL_SetError("Invalid predictor value for a MS ADPCM decoder");
SDL_free(freeable);
return(-1);
Apr 26, 2001
Apr 26, 2001
237
238
239
240
241
242
243
244
245
246
247
248
249
}
struct IMA_ADPCM_decodestate {
Sint32 sample;
Sint8 index;
};
static struct IMA_ADPCM_decoder {
WaveFMT wavefmt;
Uint16 wSamplesPerBlock;
/* * * */
struct IMA_ADPCM_decodestate state[2];
} IMA_ADPCM_state;
Jun 9, 2019
Jun 9, 2019
250
static int InitIMA_ADPCM(WaveFMT *format, int length)
Apr 26, 2001
Apr 26, 2001
251
{
Jun 9, 2019
Jun 9, 2019
252
Uint8 *rogue_feel, *rogue_feel_end;
Apr 26, 2001
Apr 26, 2001
253
254
/* Set the rogue pointer to the IMA_ADPCM specific data */
Jun 9, 2019
Jun 9, 2019
255
if (length < sizeof(*format)) goto too_short;
Apr 26, 2001
Apr 26, 2001
256
257
258
259
260
261
262
263
IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
IMA_ADPCM_state.wavefmt.bitspersample =
SDL_SwapLE16(format->bitspersample);
rogue_feel = (Uint8 *)format+sizeof(*format);
Jun 9, 2019
Jun 9, 2019
264
rogue_feel_end = (Uint8 *)format + length;
Apr 26, 2001
Apr 26, 2001
265
266
267
if ( sizeof(*format) == 16 ) {
rogue_feel += sizeof(Uint16);
}
Jun 9, 2019
Jun 9, 2019
268
if (rogue_feel + 2 > rogue_feel_end) goto too_short;
Apr 26, 2001
Apr 26, 2001
269
270
IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
return(0);
Jun 9, 2019
Jun 9, 2019
271
272
273
too_short:
SDL_SetError("Unexpected length of a chunk with an IMA ADPCM format");
return(-1);
Apr 26, 2001
Apr 26, 2001
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
}
static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state,Uint8 nybble)
{
const Sint32 max_audioval = ((1<<(16-1))-1);
const Sint32 min_audioval = -(1<<(16-1));
const int index_table[16] = {
-1, -1, -1, -1,
2, 4, 6, 8,
-1, -1, -1, -1,
2, 4, 6, 8
};
const Sint32 step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
22385, 24623, 27086, 29794, 32767
};
Sint32 delta, step;
Jun 9, 2019
Jun 9, 2019
298
299
300
301
302
303
304
305
/* Clamp index value. The inital value can be invalid. */
if ( state->index > 88 ) {
state->index = 88;
} else
if ( state->index < 0 ) {
state->index = 0;
}
Apr 26, 2001
Apr 26, 2001
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
/* Compute difference and new sample value */
step = step_table[state->index];
delta = step >> 3;
if ( nybble & 0x04 ) delta += step;
if ( nybble & 0x02 ) delta += (step >> 1);
if ( nybble & 0x01 ) delta += (step >> 2);
if ( nybble & 0x08 ) delta = -delta;
state->sample += delta;
/* Update index value */
state->index += index_table[nybble];
/* Clamp output sample */
if ( state->sample > max_audioval ) {
state->sample = max_audioval;
} else
if ( state->sample < min_audioval ) {
state->sample = min_audioval;
}
return(state->sample);
}
/* Fill the decode buffer with a channel block of data (8 samples) */
static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded,
int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
{
int i;
Sint8 nybble;
Sint32 new_sample;
decoded += (channel * 2);
for ( i=0; i<4; ++i ) {
nybble = (*encoded)&0x0F;
new_sample = IMA_ADPCM_nibble(state, nybble);
decoded[0] = new_sample&0xFF;
new_sample >>= 8;
decoded[1] = new_sample&0xFF;
decoded += 2 * numchannels;
nybble = (*encoded)>>4;
new_sample = IMA_ADPCM_nibble(state, nybble);
decoded[0] = new_sample&0xFF;
new_sample >>= 8;
decoded[1] = new_sample&0xFF;
decoded += 2 * numchannels;
++encoded;
}
}
static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
{
struct IMA_ADPCM_decodestate *state;
Jun 10, 2019
Jun 10, 2019
359
Uint8 *freeable, *encoded, *encoded_end, *decoded, *decoded_end;
Apr 26, 2001
Apr 26, 2001
360
Sint32 encoded_len, samplesleft;
Mar 24, 2006
Mar 24, 2006
361
unsigned int c, channels;
Apr 26, 2001
Apr 26, 2001
362
363
364
/* Check to make sure we have enough variables in the state array */
channels = IMA_ADPCM_state.wavefmt.channels;
Feb 6, 2006
Feb 6, 2006
365
if ( channels > SDL_arraysize(IMA_ADPCM_state.state) ) {
Apr 26, 2001
Apr 26, 2001
366
SDL_SetError("IMA ADPCM decoder can only handle %d channels",
Feb 6, 2006
Feb 6, 2006
367
SDL_arraysize(IMA_ADPCM_state.state));
Apr 26, 2001
Apr 26, 2001
368
369
370
371
372
373
374
return(-1);
}
state = IMA_ADPCM_state.state;
/* Allocate the proper sized output buffer */
encoded_len = *audio_len;
encoded = *audio_buf;
Jun 10, 2019
Jun 10, 2019
375
encoded_end = encoded + encoded_len;
Apr 26, 2001
Apr 26, 2001
376
377
378
379
freeable = *audio_buf;
*audio_len = (encoded_len/IMA_ADPCM_state.wavefmt.blockalign) *
IMA_ADPCM_state.wSamplesPerBlock*
IMA_ADPCM_state.wavefmt.channels*sizeof(Sint16);
Feb 7, 2006
Feb 7, 2006
380
*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
Apr 26, 2001
Apr 26, 2001
381
382
383
384
385
if ( *audio_buf == NULL ) {
SDL_Error(SDL_ENOMEM);
return(-1);
}
decoded = *audio_buf;
Jun 10, 2019
Jun 10, 2019
386
decoded_end = decoded + *audio_len;
Apr 26, 2001
Apr 26, 2001
387
388
389
390
391
/* Get ready... Go! */
while ( encoded_len >= IMA_ADPCM_state.wavefmt.blockalign ) {
/* Grab the initial information for this block */
for ( c=0; c<channels; ++c ) {
Jun 10, 2019
Jun 10, 2019
392
if (encoded + 4 > encoded_end) goto invalid_size;
Apr 26, 2001
Apr 26, 2001
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Fill the state information for this block */
state[c].sample = ((encoded[1]<<8)|encoded[0]);
encoded += 2;
if ( state[c].sample & 0x8000 ) {
state[c].sample -= 0x10000;
}
state[c].index = *encoded++;
/* Reserved byte in buffer header, should be 0 */
if ( *encoded++ != 0 ) {
/* Uh oh, corrupt data? Buggy code? */;
}
/* Store the initial sample we start with */
Jun 10, 2019
Jun 10, 2019
406
if (decoded + 2 > decoded_end) goto invalid_size;
Feb 24, 2006
Feb 24, 2006
407
408
decoded[0] = (Uint8)(state[c].sample&0xFF);
decoded[1] = (Uint8)(state[c].sample>>8);
Apr 26, 2001
Apr 26, 2001
409
410
411
412
413
414
415
decoded += 2;
}
/* Decode and store the other samples in this block */
samplesleft = (IMA_ADPCM_state.wSamplesPerBlock-1)*channels;
while ( samplesleft > 0 ) {
for ( c=0; c<channels; ++c ) {
Jun 10, 2019
Jun 10, 2019
416
if (encoded + 4 > encoded_end) goto invalid_size;
Jun 10, 2019
Jun 10, 2019
417
418
if (decoded + 4 * 4 * channels > decoded_end)
goto invalid_size;
Apr 26, 2001
Apr 26, 2001
419
420
421
422
423
424
425
426
427
Fill_IMA_ADPCM_block(decoded, encoded,
c, channels, &state[c]);
encoded += 4;
samplesleft -= 8;
}
decoded += (channels * 8 * 2);
}
encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
}
Feb 7, 2006
Feb 7, 2006
428
SDL_free(freeable);
Apr 26, 2001
Apr 26, 2001
429
return(0);
Jun 10, 2019
Jun 10, 2019
430
431
432
433
invalid_size:
SDL_SetError("Unexpected chunk length for an IMA ADPCM decoder");
SDL_free(freeable);
return(-1);
Apr 26, 2001
Apr 26, 2001
434
435
436
437
438
439
440
441
442
443
444
445
446
}
SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc,
SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
{
int was_error;
Chunk chunk;
int lenread;
int MS_ADPCM_encoded, IMA_ADPCM_encoded;
int samplesize;
/* WAV magic header */
Uint32 RIFFchunk;
Jan 24, 2006
Jan 24, 2006
447
Uint32 wavelen = 0;
Apr 26, 2001
Apr 26, 2001
448
Uint32 WAVEmagic;
Jan 24, 2006
Jan 24, 2006
449
Uint32 headerDiff = 0;
Apr 26, 2001
Apr 26, 2001
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* FMT chunk */
WaveFMT *format = NULL;
/* Make sure we are passed a valid data source */
was_error = 0;
if ( src == NULL ) {
was_error = 1;
goto done;
}
/* Check the magic header */
RIFFchunk = SDL_ReadLE32(src);
wavelen = SDL_ReadLE32(src);
Sep 11, 2001
Sep 11, 2001
464
465
466
467
468
469
470
if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */
WAVEmagic = wavelen;
wavelen = RIFFchunk;
RIFFchunk = RIFF;
} else {
WAVEmagic = SDL_ReadLE32(src);
}
Apr 26, 2001
Apr 26, 2001
471
472
473
474
475
if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
SDL_SetError("Unrecognized file type (not WAVE)");
was_error = 1;
goto done;
}
Mar 9, 2006
Mar 9, 2006
476
headerDiff += sizeof(Uint32); /* for WAVE */
Apr 26, 2001
Apr 26, 2001
477
478
479
480
481
/* Read the audio data format chunk */
chunk.data = NULL;
do {
if ( chunk.data != NULL ) {
Feb 7, 2006
Feb 7, 2006
482
SDL_free(chunk.data);
Nov 12, 2008
Nov 12, 2008
483
chunk.data = NULL;
Apr 26, 2001
Apr 26, 2001
484
485
486
487
488
489
}
lenread = ReadChunk(src, &chunk);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
Mar 9, 2006
Mar 9, 2006
490
/* 2 Uint32's for chunk header+len, plus the lenread */
Jan 24, 2006
Jan 24, 2006
491
headerDiff += lenread + 2 * sizeof(Uint32);
Apr 26, 2001
Apr 26, 2001
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
/* Decode the audio data format */
format = (WaveFMT *)chunk.data;
if ( chunk.magic != FMT ) {
SDL_SetError("Complex WAVE files not supported");
was_error = 1;
goto done;
}
MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
switch (SDL_SwapLE16(format->encoding)) {
case PCM_CODE:
/* We can understand this */
break;
case MS_ADPCM_CODE:
/* Try to understand this */
Jun 10, 2019
Jun 10, 2019
508
if ( InitMS_ADPCM(format, lenread) < 0 ) {
Apr 26, 2001
Apr 26, 2001
509
510
511
512
513
514
515
was_error = 1;
goto done;
}
MS_ADPCM_encoded = 1;
break;
case IMA_ADPCM_CODE:
/* Try to understand this */
Jun 9, 2019
Jun 9, 2019
516
if ( InitIMA_ADPCM(format, lenread) < 0 ) {
Apr 26, 2001
Apr 26, 2001
517
518
519
520
521
was_error = 1;
goto done;
}
IMA_ADPCM_encoded = 1;
break;
May 11, 2006
May 11, 2006
522
523
524
525
526
case MP3_CODE:
SDL_SetError("MPEG Layer 3 data not supported",
SDL_SwapLE16(format->encoding));
was_error = 1;
goto done;
Apr 26, 2001
Apr 26, 2001
527
528
529
530
531
532
default:
SDL_SetError("Unknown WAVE data format: 0x%.4x",
SDL_SwapLE16(format->encoding));
was_error = 1;
goto done;
}
Feb 7, 2006
Feb 7, 2006
533
SDL_memset(spec, 0, (sizeof *spec));
Apr 26, 2001
Apr 26, 2001
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
spec->freq = SDL_SwapLE32(format->frequency);
switch (SDL_SwapLE16(format->bitspersample)) {
case 4:
if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) {
spec->format = AUDIO_S16;
} else {
was_error = 1;
}
break;
case 8:
spec->format = AUDIO_U8;
break;
case 16:
spec->format = AUDIO_S16;
break;
default:
was_error = 1;
break;
}
if ( was_error ) {
SDL_SetError("Unknown %d-bit PCM data format",
SDL_SwapLE16(format->bitspersample));
goto done;
}
spec->channels = (Uint8)SDL_SwapLE16(format->channels);
spec->samples = 4096; /* Good default buffer size */
/* Read the audio data chunk */
*audio_buf = NULL;
do {
if ( *audio_buf != NULL ) {
Feb 7, 2006
Feb 7, 2006
565
SDL_free(*audio_buf);
Nov 12, 2008
Nov 12, 2008
566
*audio_buf = NULL;
Apr 26, 2001
Apr 26, 2001
567
568
569
570
571
572
573
574
}
lenread = ReadChunk(src, &chunk);
if ( lenread < 0 ) {
was_error = 1;
goto done;
}
*audio_len = lenread;
*audio_buf = chunk.data;
Jan 24, 2006
Jan 24, 2006
575
if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32);
Apr 26, 2001
Apr 26, 2001
576
} while ( chunk.magic != DATA );
Mar 9, 2006
Mar 9, 2006
577
headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
Apr 26, 2001
Apr 26, 2001
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
if ( MS_ADPCM_encoded ) {
if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) {
was_error = 1;
goto done;
}
}
if ( IMA_ADPCM_encoded ) {
if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) {
was_error = 1;
goto done;
}
}
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF)/8)*spec->channels;
*audio_len &= ~(samplesize-1);
done:
if ( format != NULL ) {
Feb 7, 2006
Feb 7, 2006
598
SDL_free(format);
Apr 26, 2001
Apr 26, 2001
599
}
Mar 4, 2006
Mar 4, 2006
600
601
602
603
if ( src ) {
if ( freesrc ) {
SDL_RWclose(src);
} else {
Mar 9, 2006
Mar 9, 2006
604
/* seek to the end of the file (given by the RIFF chunk) */
Mar 4, 2006
Mar 4, 2006
605
606
SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
}
Jan 24, 2006
Jan 24, 2006
607
}
Apr 26, 2001
Apr 26, 2001
608
609
610
611
612
613
614
615
616
617
618
619
if ( was_error ) {
spec = NULL;
}
return(spec);
}
/* Since the WAV memory is allocated in the shared library, it must also
be freed here. (Necessary under Win32, VC++)
*/
void SDL_FreeWAV(Uint8 *audio_buf)
{
if ( audio_buf != NULL ) {
Feb 7, 2006
Feb 7, 2006
620
SDL_free(audio_buf);
Apr 26, 2001
Apr 26, 2001
621
622
623
624
625
626
627
}
}
static int ReadChunk(SDL_RWops *src, Chunk *chunk)
{
chunk->magic = SDL_ReadLE32(src);
chunk->length = SDL_ReadLE32(src);
Feb 7, 2006
Feb 7, 2006
628
chunk->data = (Uint8 *)SDL_malloc(chunk->length);
Apr 26, 2001
Apr 26, 2001
629
630
631
632
633
634
if ( chunk->data == NULL ) {
SDL_Error(SDL_ENOMEM);
return(-1);
}
if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
SDL_Error(SDL_EFREAD);
Feb 7, 2006
Feb 7, 2006
635
SDL_free(chunk->data);
Nov 12, 2008
Nov 12, 2008
636
chunk->data = NULL;
Apr 26, 2001
Apr 26, 2001
637
638
639
640
return(-1);
}
return(chunk->length);
}