2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Microsoft WAVE file loading routines */
26 #include "SDL_audio.h"
30 static int ReadChunk(SDL_RWops *src, Chunk *chunk);
32 struct MS_ADPCM_decodestate {
38 static struct MS_ADPCM_decoder {
40 Uint16 wSamplesPerBlock;
44 struct MS_ADPCM_decodestate state[2];
47 static int InitMS_ADPCM(WaveFMT *format)
52 /* Set the rogue pointer to the MS_ADPCM specific data */
53 MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
54 MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
55 MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
56 MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
57 MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
58 MS_ADPCM_state.wavefmt.bitspersample =
59 SDL_SwapLE16(format->bitspersample);
60 rogue_feel = (Uint8 *)format+sizeof(*format);
61 if ( sizeof(*format) == 16 ) {
62 rogue_feel += sizeof(Uint16);
64 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
65 rogue_feel += sizeof(Uint16);
66 MS_ADPCM_state.wNumCoef = ((rogue_feel[1]<<8)|rogue_feel[0]);
67 rogue_feel += sizeof(Uint16);
68 if ( MS_ADPCM_state.wNumCoef != 7 ) {
69 SDL_SetError("Unknown set of MS_ADPCM coefficients");
72 for ( i=0; i<MS_ADPCM_state.wNumCoef; ++i ) {
73 MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1]<<8)|rogue_feel[0]);
74 rogue_feel += sizeof(Uint16);
75 MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1]<<8)|rogue_feel[0]);
76 rogue_feel += sizeof(Uint16);
81 static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
82 Uint8 nybble, Sint16 *coeff)
84 const Sint32 max_audioval = ((1<<(16-1))-1);
85 const Sint32 min_audioval = -(1<<(16-1));
86 const Sint32 adaptive[] = {
87 230, 230, 230, 230, 307, 409, 512, 614,
88 768, 614, 512, 409, 307, 230, 230, 230
90 Sint32 new_sample, delta;
92 new_sample = ((state->iSamp1 * coeff[0]) +
93 (state->iSamp2 * coeff[1]))/256;
94 if ( nybble & 0x08 ) {
95 new_sample += state->iDelta * (nybble-0x10);
97 new_sample += state->iDelta * nybble;
99 if ( new_sample < min_audioval ) {
100 new_sample = min_audioval;
102 if ( new_sample > max_audioval ) {
103 new_sample = max_audioval;
105 delta = ((Sint32)state->iDelta * adaptive[nybble])/256;
109 state->iDelta = (Uint16)delta;
110 state->iSamp2 = state->iSamp1;
111 state->iSamp1 = (Sint16)new_sample;
115 static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
117 struct MS_ADPCM_decodestate *state[2];
118 Uint8 *freeable, *encoded, *decoded;
119 Sint32 encoded_len, samplesleft;
120 Sint8 nybble, stereo;
124 /* Allocate the proper sized output buffer */
125 encoded_len = *audio_len;
126 encoded = *audio_buf;
127 freeable = *audio_buf;
128 *audio_len = (encoded_len/MS_ADPCM_state.wavefmt.blockalign) *
129 MS_ADPCM_state.wSamplesPerBlock*
130 MS_ADPCM_state.wavefmt.channels*sizeof(Sint16);
131 *audio_buf = (Uint8 *)SDL_malloc(*audio_len);
132 if ( *audio_buf == NULL ) {
133 SDL_Error(SDL_ENOMEM);
136 decoded = *audio_buf;
138 /* Get ready... Go! */
139 stereo = (MS_ADPCM_state.wavefmt.channels == 2);
140 state[0] = &MS_ADPCM_state.state[0];
141 state[1] = &MS_ADPCM_state.state[stereo];
142 while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) {
143 /* Grab the initial information for this block */
144 state[0]->hPredictor = *encoded++;
146 state[1]->hPredictor = *encoded++;
148 state[0]->iDelta = ((encoded[1]<<8)|encoded[0]);
149 encoded += sizeof(Sint16);
151 state[1]->iDelta = ((encoded[1]<<8)|encoded[0]);
152 encoded += sizeof(Sint16);
154 state[0]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
155 encoded += sizeof(Sint16);
157 state[1]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
158 encoded += sizeof(Sint16);
160 state[0]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
161 encoded += sizeof(Sint16);
163 state[1]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
164 encoded += sizeof(Sint16);
166 coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
167 coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
169 /* Store the two initial samples we start with */
170 decoded[0] = state[0]->iSamp2&0xFF;
171 decoded[1] = state[0]->iSamp2>>8;
174 decoded[0] = state[1]->iSamp2&0xFF;
175 decoded[1] = state[1]->iSamp2>>8;
178 decoded[0] = state[0]->iSamp1&0xFF;
179 decoded[1] = state[0]->iSamp1>>8;
182 decoded[0] = state[1]->iSamp1&0xFF;
183 decoded[1] = state[1]->iSamp1>>8;
187 /* Decode and store the other samples in this block */
188 samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)*
189 MS_ADPCM_state.wavefmt.channels;
190 while ( samplesleft > 0 ) {
191 nybble = (*encoded)>>4;
192 new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]);
193 decoded[0] = new_sample&0xFF;
195 decoded[1] = new_sample&0xFF;
198 nybble = (*encoded)&0x0F;
199 new_sample = MS_ADPCM_nibble(state[1],nybble,coeff[1]);
200 decoded[0] = new_sample&0xFF;
202 decoded[1] = new_sample&0xFF;
208 encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
214 struct IMA_ADPCM_decodestate {
218 static struct IMA_ADPCM_decoder {
220 Uint16 wSamplesPerBlock;
222 struct IMA_ADPCM_decodestate state[2];
225 static int InitIMA_ADPCM(WaveFMT *format)
229 /* Set the rogue pointer to the IMA_ADPCM specific data */
230 IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
231 IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
232 IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
233 IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
234 IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
235 IMA_ADPCM_state.wavefmt.bitspersample =
236 SDL_SwapLE16(format->bitspersample);
237 rogue_feel = (Uint8 *)format+sizeof(*format);
238 if ( sizeof(*format) == 16 ) {
239 rogue_feel += sizeof(Uint16);
241 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
245 static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state,Uint8 nybble)
247 const Sint32 max_audioval = ((1<<(16-1))-1);
248 const Sint32 min_audioval = -(1<<(16-1));
249 const int index_table[16] = {
255 const Sint32 step_table[89] = {
256 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
257 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
258 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
259 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
260 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
261 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
262 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
263 22385, 24623, 27086, 29794, 32767
267 /* Compute difference and new sample value */
268 step = step_table[state->index];
270 if ( nybble & 0x04 ) delta += step;
271 if ( nybble & 0x02 ) delta += (step >> 1);
272 if ( nybble & 0x01 ) delta += (step >> 2);
273 if ( nybble & 0x08 ) delta = -delta;
274 state->sample += delta;
276 /* Update index value */
277 state->index += index_table[nybble];
278 if ( state->index > 88 ) {
281 if ( state->index < 0 ) {
285 /* Clamp output sample */
286 if ( state->sample > max_audioval ) {
287 state->sample = max_audioval;
289 if ( state->sample < min_audioval ) {
290 state->sample = min_audioval;
292 return(state->sample);
295 /* Fill the decode buffer with a channel block of data (8 samples) */
296 static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded,
297 int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
303 decoded += (channel * 2);
304 for ( i=0; i<4; ++i ) {
305 nybble = (*encoded)&0x0F;
306 new_sample = IMA_ADPCM_nibble(state, nybble);
307 decoded[0] = new_sample&0xFF;
309 decoded[1] = new_sample&0xFF;
310 decoded += 2 * numchannels;
312 nybble = (*encoded)>>4;
313 new_sample = IMA_ADPCM_nibble(state, nybble);
314 decoded[0] = new_sample&0xFF;
316 decoded[1] = new_sample&0xFF;
317 decoded += 2 * numchannels;
323 static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
325 struct IMA_ADPCM_decodestate *state;
326 Uint8 *freeable, *encoded, *decoded;
327 Sint32 encoded_len, samplesleft;
328 unsigned int c, channels;
330 /* Check to make sure we have enough variables in the state array */
331 channels = IMA_ADPCM_state.wavefmt.channels;
332 if ( channels > SDL_arraysize(IMA_ADPCM_state.state) ) {
333 SDL_SetError("IMA ADPCM decoder can only handle %d channels",
334 SDL_arraysize(IMA_ADPCM_state.state));
337 state = IMA_ADPCM_state.state;
339 /* Allocate the proper sized output buffer */
340 encoded_len = *audio_len;
341 encoded = *audio_buf;
342 freeable = *audio_buf;
343 *audio_len = (encoded_len/IMA_ADPCM_state.wavefmt.blockalign) *
344 IMA_ADPCM_state.wSamplesPerBlock*
345 IMA_ADPCM_state.wavefmt.channels*sizeof(Sint16);
346 *audio_buf = (Uint8 *)SDL_malloc(*audio_len);
347 if ( *audio_buf == NULL ) {
348 SDL_Error(SDL_ENOMEM);
351 decoded = *audio_buf;
353 /* Get ready... Go! */
354 while ( encoded_len >= IMA_ADPCM_state.wavefmt.blockalign ) {
355 /* Grab the initial information for this block */
356 for ( c=0; c<channels; ++c ) {
357 /* Fill the state information for this block */
358 state[c].sample = ((encoded[1]<<8)|encoded[0]);
360 if ( state[c].sample & 0x8000 ) {
361 state[c].sample -= 0x10000;
363 state[c].index = *encoded++;
364 /* Reserved byte in buffer header, should be 0 */
365 if ( *encoded++ != 0 ) {
366 /* Uh oh, corrupt data? Buggy code? */;
369 /* Store the initial sample we start with */
370 decoded[0] = (Uint8)(state[c].sample&0xFF);
371 decoded[1] = (Uint8)(state[c].sample>>8);
375 /* Decode and store the other samples in this block */
376 samplesleft = (IMA_ADPCM_state.wSamplesPerBlock-1)*channels;
377 while ( samplesleft > 0 ) {
378 for ( c=0; c<channels; ++c ) {
379 Fill_IMA_ADPCM_block(decoded, encoded,
380 c, channels, &state[c]);
384 decoded += (channels * 8 * 2);
386 encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
392 SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc,
393 SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
398 int MS_ADPCM_encoded, IMA_ADPCM_encoded;
401 /* WAV magic header */
405 Uint32 headerDiff = 0;
408 WaveFMT *format = NULL;
410 /* Make sure we are passed a valid data source */
417 /* Check the magic header */
418 RIFFchunk = SDL_ReadLE32(src);
419 wavelen = SDL_ReadLE32(src);
420 if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */
425 WAVEmagic = SDL_ReadLE32(src);
427 if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
428 SDL_SetError("Unrecognized file type (not WAVE)");
432 headerDiff += sizeof(Uint32); /* for WAVE */
434 /* Read the audio data format chunk */
437 if ( chunk.data != NULL ) {
438 SDL_free(chunk.data);
441 lenread = ReadChunk(src, &chunk);
446 /* 2 Uint32's for chunk header+len, plus the lenread */
447 headerDiff += lenread + 2 * sizeof(Uint32);
448 } while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
450 /* Decode the audio data format */
451 format = (WaveFMT *)chunk.data;
452 if ( chunk.magic != FMT ) {
453 SDL_SetError("Complex WAVE files not supported");
457 MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
458 switch (SDL_SwapLE16(format->encoding)) {
460 /* We can understand this */
463 /* Try to understand this */
464 if ( InitMS_ADPCM(format) < 0 ) {
468 MS_ADPCM_encoded = 1;
471 /* Try to understand this */
472 if ( InitIMA_ADPCM(format) < 0 ) {
476 IMA_ADPCM_encoded = 1;
479 SDL_SetError("MPEG Layer 3 data not supported",
480 SDL_SwapLE16(format->encoding));
484 SDL_SetError("Unknown WAVE data format: 0x%.4x",
485 SDL_SwapLE16(format->encoding));
489 SDL_memset(spec, 0, (sizeof *spec));
490 spec->freq = SDL_SwapLE32(format->frequency);
491 switch (SDL_SwapLE16(format->bitspersample)) {
493 if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) {
494 spec->format = AUDIO_S16;
500 spec->format = AUDIO_U8;
503 spec->format = AUDIO_S16;
510 SDL_SetError("Unknown %d-bit PCM data format",
511 SDL_SwapLE16(format->bitspersample));
514 spec->channels = (Uint8)SDL_SwapLE16(format->channels);
515 spec->samples = 4096; /* Good default buffer size */
517 /* Read the audio data chunk */
520 if ( *audio_buf != NULL ) {
521 SDL_free(*audio_buf);
524 lenread = ReadChunk(src, &chunk);
529 *audio_len = lenread;
530 *audio_buf = chunk.data;
531 if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32);
532 } while ( chunk.magic != DATA );
533 headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
535 if ( MS_ADPCM_encoded ) {
536 if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) {
541 if ( IMA_ADPCM_encoded ) {
542 if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) {
548 /* Don't return a buffer that isn't a multiple of samplesize */
549 samplesize = ((spec->format & 0xFF)/8)*spec->channels;
550 *audio_len &= ~(samplesize-1);
553 if ( format != NULL ) {
560 /* seek to the end of the file (given by the RIFF chunk) */
561 SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
570 /* Since the WAV memory is allocated in the shared library, it must also
571 be freed here. (Necessary under Win32, VC++)
573 void SDL_FreeWAV(Uint8 *audio_buf)
575 if ( audio_buf != NULL ) {
580 static int ReadChunk(SDL_RWops *src, Chunk *chunk)
582 chunk->magic = SDL_ReadLE32(src);
583 chunk->length = SDL_ReadLE32(src);
584 chunk->data = (Uint8 *)SDL_malloc(chunk->length);
585 if ( chunk->data == NULL ) {
586 SDL_Error(SDL_ENOMEM);
589 if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
590 SDL_Error(SDL_EFREAD);
591 SDL_free(chunk->data);
595 return(chunk->length);