comparison src/audio/SDL_wave.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
25 25
26 #include "SDL_audio.h" 26 #include "SDL_audio.h"
27 #include "SDL_wave.h" 27 #include "SDL_wave.h"
28 28
29 29
30 static int ReadChunk (SDL_RWops * src, Chunk * chunk); 30 static int ReadChunk(SDL_RWops * src, Chunk * chunk);
31 31
32 struct MS_ADPCM_decodestate 32 struct MS_ADPCM_decodestate
33 { 33 {
34 Uint8 hPredictor; 34 Uint8 hPredictor;
35 Uint16 iDelta; 35 Uint16 iDelta;
45 /* * * */ 45 /* * * */
46 struct MS_ADPCM_decodestate state[2]; 46 struct MS_ADPCM_decodestate state[2];
47 } MS_ADPCM_state; 47 } MS_ADPCM_state;
48 48
49 static int 49 static int
50 InitMS_ADPCM (WaveFMT * format) 50 InitMS_ADPCM(WaveFMT * format)
51 { 51 {
52 Uint8 *rogue_feel; 52 Uint8 *rogue_feel;
53 Uint16 extra_info; 53 Uint16 extra_info;
54 int i; 54 int i;
55 55
56 /* Set the rogue pointer to the MS_ADPCM specific data */ 56 /* Set the rogue pointer to the MS_ADPCM specific data */
57 MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16 (format->encoding); 57 MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
58 MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16 (format->channels); 58 MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
59 MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32 (format->frequency); 59 MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
60 MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32 (format->byterate); 60 MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
61 MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16 (format->blockalign); 61 MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
62 MS_ADPCM_state.wavefmt.bitspersample = 62 MS_ADPCM_state.wavefmt.bitspersample =
63 SDL_SwapLE16 (format->bitspersample); 63 SDL_SwapLE16(format->bitspersample);
64 rogue_feel = (Uint8 *) format + sizeof (*format); 64 rogue_feel = (Uint8 *) format + sizeof(*format);
65 if (sizeof (*format) == 16) { 65 if (sizeof(*format) == 16) {
66 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); 66 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]);
67 rogue_feel += sizeof (Uint16); 67 rogue_feel += sizeof(Uint16);
68 } 68 }
69 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]); 69 MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
70 rogue_feel += sizeof (Uint16); 70 rogue_feel += sizeof(Uint16);
71 MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]); 71 MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]);
72 rogue_feel += sizeof (Uint16); 72 rogue_feel += sizeof(Uint16);
73 if (MS_ADPCM_state.wNumCoef != 7) { 73 if (MS_ADPCM_state.wNumCoef != 7) {
74 SDL_SetError ("Unknown set of MS_ADPCM coefficients"); 74 SDL_SetError("Unknown set of MS_ADPCM coefficients");
75 return (-1); 75 return (-1);
76 } 76 }
77 for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) { 77 for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) {
78 MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]); 78 MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]);
79 rogue_feel += sizeof (Uint16); 79 rogue_feel += sizeof(Uint16);
80 MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]); 80 MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]);
81 rogue_feel += sizeof (Uint16); 81 rogue_feel += sizeof(Uint16);
82 } 82 }
83 return (0); 83 return (0);
84 } 84 }
85 85
86 static Sint32 86 static Sint32
87 MS_ADPCM_nibble (struct MS_ADPCM_decodestate *state, 87 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
88 Uint8 nybble, Sint16 * coeff) 88 Uint8 nybble, Sint16 * coeff)
89 { 89 {
90 const Sint32 max_audioval = ((1 << (16 - 1)) - 1); 90 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
91 const Sint32 min_audioval = -(1 << (16 - 1)); 91 const Sint32 min_audioval = -(1 << (16 - 1));
92 const Sint32 adaptive[] = { 92 const Sint32 adaptive[] = {
93 230, 230, 230, 230, 307, 409, 512, 614, 93 230, 230, 230, 230, 307, 409, 512, 614,
116 state->iSamp1 = (Sint16) new_sample; 116 state->iSamp1 = (Sint16) new_sample;
117 return (new_sample); 117 return (new_sample);
118 } 118 }
119 119
120 static int 120 static int
121 MS_ADPCM_decode (Uint8 ** audio_buf, Uint32 * audio_len) 121 MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
122 { 122 {
123 struct MS_ADPCM_decodestate *state[2]; 123 struct MS_ADPCM_decodestate *state[2];
124 Uint8 *freeable, *encoded, *decoded; 124 Uint8 *freeable, *encoded, *decoded;
125 Sint32 encoded_len, samplesleft; 125 Sint32 encoded_len, samplesleft;
126 Sint8 nybble, stereo; 126 Sint8 nybble, stereo;
131 encoded_len = *audio_len; 131 encoded_len = *audio_len;
132 encoded = *audio_buf; 132 encoded = *audio_buf;
133 freeable = *audio_buf; 133 freeable = *audio_buf;
134 *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) * 134 *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) *
135 MS_ADPCM_state.wSamplesPerBlock * 135 MS_ADPCM_state.wSamplesPerBlock *
136 MS_ADPCM_state.wavefmt.channels * sizeof (Sint16); 136 MS_ADPCM_state.wavefmt.channels * sizeof(Sint16);
137 *audio_buf = (Uint8 *) SDL_malloc (*audio_len); 137 *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
138 if (*audio_buf == NULL) { 138 if (*audio_buf == NULL) {
139 SDL_Error (SDL_ENOMEM); 139 SDL_Error(SDL_ENOMEM);
140 return (-1); 140 return (-1);
141 } 141 }
142 decoded = *audio_buf; 142 decoded = *audio_buf;
143 143
144 /* Get ready... Go! */ 144 /* Get ready... Go! */
150 state[0]->hPredictor = *encoded++; 150 state[0]->hPredictor = *encoded++;
151 if (stereo) { 151 if (stereo) {
152 state[1]->hPredictor = *encoded++; 152 state[1]->hPredictor = *encoded++;
153 } 153 }
154 state[0]->iDelta = ((encoded[1] << 8) | encoded[0]); 154 state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
155 encoded += sizeof (Sint16); 155 encoded += sizeof(Sint16);
156 if (stereo) { 156 if (stereo) {
157 state[1]->iDelta = ((encoded[1] << 8) | encoded[0]); 157 state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
158 encoded += sizeof (Sint16); 158 encoded += sizeof(Sint16);
159 } 159 }
160 state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]); 160 state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
161 encoded += sizeof (Sint16); 161 encoded += sizeof(Sint16);
162 if (stereo) { 162 if (stereo) {
163 state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]); 163 state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
164 encoded += sizeof (Sint16); 164 encoded += sizeof(Sint16);
165 } 165 }
166 state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]); 166 state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
167 encoded += sizeof (Sint16); 167 encoded += sizeof(Sint16);
168 if (stereo) { 168 if (stereo) {
169 state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]); 169 state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
170 encoded += sizeof (Sint16); 170 encoded += sizeof(Sint16);
171 } 171 }
172 coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor]; 172 coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
173 coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor]; 173 coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
174 174
175 /* Store the two initial samples we start with */ 175 /* Store the two initial samples we start with */
193 /* Decode and store the other samples in this block */ 193 /* Decode and store the other samples in this block */
194 samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) * 194 samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) *
195 MS_ADPCM_state.wavefmt.channels; 195 MS_ADPCM_state.wavefmt.channels;
196 while (samplesleft > 0) { 196 while (samplesleft > 0) {
197 nybble = (*encoded) >> 4; 197 nybble = (*encoded) >> 4;
198 new_sample = MS_ADPCM_nibble (state[0], nybble, coeff[0]); 198 new_sample = MS_ADPCM_nibble(state[0], nybble, coeff[0]);
199 decoded[0] = new_sample & 0xFF; 199 decoded[0] = new_sample & 0xFF;
200 new_sample >>= 8; 200 new_sample >>= 8;
201 decoded[1] = new_sample & 0xFF; 201 decoded[1] = new_sample & 0xFF;
202 decoded += 2; 202 decoded += 2;
203 203
204 nybble = (*encoded) & 0x0F; 204 nybble = (*encoded) & 0x0F;
205 new_sample = MS_ADPCM_nibble (state[1], nybble, coeff[1]); 205 new_sample = MS_ADPCM_nibble(state[1], nybble, coeff[1]);
206 decoded[0] = new_sample & 0xFF; 206 decoded[0] = new_sample & 0xFF;
207 new_sample >>= 8; 207 new_sample >>= 8;
208 decoded[1] = new_sample & 0xFF; 208 decoded[1] = new_sample & 0xFF;
209 decoded += 2; 209 decoded += 2;
210 210
211 ++encoded; 211 ++encoded;
212 samplesleft -= 2; 212 samplesleft -= 2;
213 } 213 }
214 encoded_len -= MS_ADPCM_state.wavefmt.blockalign; 214 encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
215 } 215 }
216 SDL_free (freeable); 216 SDL_free(freeable);
217 return (0); 217 return (0);
218 } 218 }
219 219
220 struct IMA_ADPCM_decodestate 220 struct IMA_ADPCM_decodestate
221 { 221 {
229 /* * * */ 229 /* * * */
230 struct IMA_ADPCM_decodestate state[2]; 230 struct IMA_ADPCM_decodestate state[2];
231 } IMA_ADPCM_state; 231 } IMA_ADPCM_state;
232 232
233 static int 233 static int
234 InitIMA_ADPCM (WaveFMT * format) 234 InitIMA_ADPCM(WaveFMT * format)
235 { 235 {
236 Uint8 *rogue_feel; 236 Uint8 *rogue_feel;
237 Uint16 extra_info; 237 Uint16 extra_info;
238 238
239 /* Set the rogue pointer to the IMA_ADPCM specific data */ 239 /* Set the rogue pointer to the IMA_ADPCM specific data */
240 IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16 (format->encoding); 240 IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
241 IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16 (format->channels); 241 IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
242 IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32 (format->frequency); 242 IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
243 IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32 (format->byterate); 243 IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
244 IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16 (format->blockalign); 244 IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
245 IMA_ADPCM_state.wavefmt.bitspersample = 245 IMA_ADPCM_state.wavefmt.bitspersample =
246 SDL_SwapLE16 (format->bitspersample); 246 SDL_SwapLE16(format->bitspersample);
247 rogue_feel = (Uint8 *) format + sizeof (*format); 247 rogue_feel = (Uint8 *) format + sizeof(*format);
248 if (sizeof (*format) == 16) { 248 if (sizeof(*format) == 16) {
249 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); 249 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]);
250 rogue_feel += sizeof (Uint16); 250 rogue_feel += sizeof(Uint16);
251 } 251 }
252 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]); 252 IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
253 return (0); 253 return (0);
254 } 254 }
255 255
256 static Sint32 256 static Sint32
257 IMA_ADPCM_nibble (struct IMA_ADPCM_decodestate *state, Uint8 nybble) 257 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
258 { 258 {
259 const Sint32 max_audioval = ((1 << (16 - 1)) - 1); 259 const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
260 const Sint32 min_audioval = -(1 << (16 - 1)); 260 const Sint32 min_audioval = -(1 << (16 - 1));
261 const int index_table[16] = { 261 const int index_table[16] = {
262 -1, -1, -1, -1, 262 -1, -1, -1, -1,
306 return (state->sample); 306 return (state->sample);
307 } 307 }
308 308
309 /* Fill the decode buffer with a channel block of data (8 samples) */ 309 /* Fill the decode buffer with a channel block of data (8 samples) */
310 static void 310 static void
311 Fill_IMA_ADPCM_block (Uint8 * decoded, Uint8 * encoded, 311 Fill_IMA_ADPCM_block(Uint8 * decoded, Uint8 * encoded,
312 int channel, int numchannels, 312 int channel, int numchannels,
313 struct IMA_ADPCM_decodestate *state) 313 struct IMA_ADPCM_decodestate *state)
314 { 314 {
315 int i; 315 int i;
316 Sint8 nybble; 316 Sint8 nybble;
317 Sint32 new_sample; 317 Sint32 new_sample;
318 318
319 decoded += (channel * 2); 319 decoded += (channel * 2);
320 for (i = 0; i < 4; ++i) { 320 for (i = 0; i < 4; ++i) {
321 nybble = (*encoded) & 0x0F; 321 nybble = (*encoded) & 0x0F;
322 new_sample = IMA_ADPCM_nibble (state, nybble); 322 new_sample = IMA_ADPCM_nibble(state, nybble);
323 decoded[0] = new_sample & 0xFF; 323 decoded[0] = new_sample & 0xFF;
324 new_sample >>= 8; 324 new_sample >>= 8;
325 decoded[1] = new_sample & 0xFF; 325 decoded[1] = new_sample & 0xFF;
326 decoded += 2 * numchannels; 326 decoded += 2 * numchannels;
327 327
328 nybble = (*encoded) >> 4; 328 nybble = (*encoded) >> 4;
329 new_sample = IMA_ADPCM_nibble (state, nybble); 329 new_sample = IMA_ADPCM_nibble(state, nybble);
330 decoded[0] = new_sample & 0xFF; 330 decoded[0] = new_sample & 0xFF;
331 new_sample >>= 8; 331 new_sample >>= 8;
332 decoded[1] = new_sample & 0xFF; 332 decoded[1] = new_sample & 0xFF;
333 decoded += 2 * numchannels; 333 decoded += 2 * numchannels;
334 334
335 ++encoded; 335 ++encoded;
336 } 336 }
337 } 337 }
338 338
339 static int 339 static int
340 IMA_ADPCM_decode (Uint8 ** audio_buf, Uint32 * audio_len) 340 IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
341 { 341 {
342 struct IMA_ADPCM_decodestate *state; 342 struct IMA_ADPCM_decodestate *state;
343 Uint8 *freeable, *encoded, *decoded; 343 Uint8 *freeable, *encoded, *decoded;
344 Sint32 encoded_len, samplesleft; 344 Sint32 encoded_len, samplesleft;
345 unsigned int c, channels; 345 unsigned int c, channels;
346 346
347 /* Check to make sure we have enough variables in the state array */ 347 /* Check to make sure we have enough variables in the state array */
348 channels = IMA_ADPCM_state.wavefmt.channels; 348 channels = IMA_ADPCM_state.wavefmt.channels;
349 if (channels > SDL_arraysize (IMA_ADPCM_state.state)) { 349 if (channels > SDL_arraysize(IMA_ADPCM_state.state)) {
350 SDL_SetError ("IMA ADPCM decoder can only handle %d channels", 350 SDL_SetError("IMA ADPCM decoder can only handle %d channels",
351 SDL_arraysize (IMA_ADPCM_state.state)); 351 SDL_arraysize(IMA_ADPCM_state.state));
352 return (-1); 352 return (-1);
353 } 353 }
354 state = IMA_ADPCM_state.state; 354 state = IMA_ADPCM_state.state;
355 355
356 /* Allocate the proper sized output buffer */ 356 /* Allocate the proper sized output buffer */
357 encoded_len = *audio_len; 357 encoded_len = *audio_len;
358 encoded = *audio_buf; 358 encoded = *audio_buf;
359 freeable = *audio_buf; 359 freeable = *audio_buf;
360 *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) * 360 *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) *
361 IMA_ADPCM_state.wSamplesPerBlock * 361 IMA_ADPCM_state.wSamplesPerBlock *
362 IMA_ADPCM_state.wavefmt.channels * sizeof (Sint16); 362 IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16);
363 *audio_buf = (Uint8 *) SDL_malloc (*audio_len); 363 *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
364 if (*audio_buf == NULL) { 364 if (*audio_buf == NULL) {
365 SDL_Error (SDL_ENOMEM); 365 SDL_Error(SDL_ENOMEM);
366 return (-1); 366 return (-1);
367 } 367 }
368 decoded = *audio_buf; 368 decoded = *audio_buf;
369 369
370 /* Get ready... Go! */ 370 /* Get ready... Go! */
391 391
392 /* Decode and store the other samples in this block */ 392 /* Decode and store the other samples in this block */
393 samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels; 393 samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels;
394 while (samplesleft > 0) { 394 while (samplesleft > 0) {
395 for (c = 0; c < channels; ++c) { 395 for (c = 0; c < channels; ++c) {
396 Fill_IMA_ADPCM_block (decoded, encoded, 396 Fill_IMA_ADPCM_block(decoded, encoded,
397 c, channels, &state[c]); 397 c, channels, &state[c]);
398 encoded += 4; 398 encoded += 4;
399 samplesleft -= 8; 399 samplesleft -= 8;
400 } 400 }
401 decoded += (channels * 8 * 2); 401 decoded += (channels * 8 * 2);
402 } 402 }
403 encoded_len -= IMA_ADPCM_state.wavefmt.blockalign; 403 encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
404 } 404 }
405 SDL_free (freeable); 405 SDL_free(freeable);
406 return (0); 406 return (0);
407 } 407 }
408 408
409 SDL_AudioSpec * 409 SDL_AudioSpec *
410 SDL_LoadWAV_RW (SDL_RWops * src, int freesrc, 410 SDL_LoadWAV_RW(SDL_RWops * src, int freesrc,
411 SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len) 411 SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
412 { 412 {
413 int was_error; 413 int was_error;
414 Chunk chunk; 414 Chunk chunk;
415 int lenread; 415 int lenread;
416 int MS_ADPCM_encoded, IMA_ADPCM_encoded; 416 int MS_ADPCM_encoded, IMA_ADPCM_encoded;
431 was_error = 1; 431 was_error = 1;
432 goto done; 432 goto done;
433 } 433 }
434 434
435 /* Check the magic header */ 435 /* Check the magic header */
436 RIFFchunk = SDL_ReadLE32 (src); 436 RIFFchunk = SDL_ReadLE32(src);
437 wavelen = SDL_ReadLE32 (src); 437 wavelen = SDL_ReadLE32(src);
438 if (wavelen == WAVE) { /* The RIFFchunk has already been read */ 438 if (wavelen == WAVE) { /* The RIFFchunk has already been read */
439 WAVEmagic = wavelen; 439 WAVEmagic = wavelen;
440 wavelen = RIFFchunk; 440 wavelen = RIFFchunk;
441 RIFFchunk = RIFF; 441 RIFFchunk = RIFF;
442 } else { 442 } else {
443 WAVEmagic = SDL_ReadLE32 (src); 443 WAVEmagic = SDL_ReadLE32(src);
444 } 444 }
445 if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) { 445 if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
446 SDL_SetError ("Unrecognized file type (not WAVE)"); 446 SDL_SetError("Unrecognized file type (not WAVE)");
447 was_error = 1; 447 was_error = 1;
448 goto done; 448 goto done;
449 } 449 }
450 headerDiff += sizeof (Uint32); /* for WAVE */ 450 headerDiff += sizeof(Uint32); /* for WAVE */
451 451
452 /* Read the audio data format chunk */ 452 /* Read the audio data format chunk */
453 chunk.data = NULL; 453 chunk.data = NULL;
454 do { 454 do {
455 if (chunk.data != NULL) { 455 if (chunk.data != NULL) {
456 SDL_free (chunk.data); 456 SDL_free(chunk.data);
457 } 457 }
458 lenread = ReadChunk (src, &chunk); 458 lenread = ReadChunk(src, &chunk);
459 if (lenread < 0) { 459 if (lenread < 0) {
460 was_error = 1; 460 was_error = 1;
461 goto done; 461 goto done;
462 } 462 }
463 /* 2 Uint32's for chunk header+len, plus the lenread */ 463 /* 2 Uint32's for chunk header+len, plus the lenread */
464 headerDiff += lenread + 2 * sizeof (Uint32); 464 headerDiff += lenread + 2 * sizeof(Uint32);
465 } 465 }
466 while ((chunk.magic == FACT) || (chunk.magic == LIST)); 466 while ((chunk.magic == FACT) || (chunk.magic == LIST));
467 467
468 /* Decode the audio data format */ 468 /* Decode the audio data format */
469 format = (WaveFMT *) chunk.data; 469 format = (WaveFMT *) chunk.data;
470 if (chunk.magic != FMT) { 470 if (chunk.magic != FMT) {
471 SDL_SetError ("Complex WAVE files not supported"); 471 SDL_SetError("Complex WAVE files not supported");
472 was_error = 1; 472 was_error = 1;
473 goto done; 473 goto done;
474 } 474 }
475 MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; 475 MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
476 switch (SDL_SwapLE16 (format->encoding)) { 476 switch (SDL_SwapLE16(format->encoding)) {
477 case PCM_CODE: 477 case PCM_CODE:
478 /* We can understand this */ 478 /* We can understand this */
479 break; 479 break;
480 case MS_ADPCM_CODE: 480 case MS_ADPCM_CODE:
481 /* Try to understand this */ 481 /* Try to understand this */
482 if (InitMS_ADPCM (format) < 0) { 482 if (InitMS_ADPCM(format) < 0) {
483 was_error = 1; 483 was_error = 1;
484 goto done; 484 goto done;
485 } 485 }
486 MS_ADPCM_encoded = 1; 486 MS_ADPCM_encoded = 1;
487 break; 487 break;
488 case IMA_ADPCM_CODE: 488 case IMA_ADPCM_CODE:
489 /* Try to understand this */ 489 /* Try to understand this */
490 if (InitIMA_ADPCM (format) < 0) { 490 if (InitIMA_ADPCM(format) < 0) {
491 was_error = 1; 491 was_error = 1;
492 goto done; 492 goto done;
493 } 493 }
494 IMA_ADPCM_encoded = 1; 494 IMA_ADPCM_encoded = 1;
495 break; 495 break;
496 case MP3_CODE: 496 case MP3_CODE:
497 SDL_SetError ("MPEG Layer 3 data not supported", 497 SDL_SetError("MPEG Layer 3 data not supported",
498 SDL_SwapLE16 (format->encoding)); 498 SDL_SwapLE16(format->encoding));
499 was_error = 1; 499 was_error = 1;
500 goto done; 500 goto done;
501 default: 501 default:
502 SDL_SetError ("Unknown WAVE data format: 0x%.4x", 502 SDL_SetError("Unknown WAVE data format: 0x%.4x",
503 SDL_SwapLE16 (format->encoding)); 503 SDL_SwapLE16(format->encoding));
504 was_error = 1; 504 was_error = 1;
505 goto done; 505 goto done;
506 } 506 }
507 SDL_memset (spec, 0, (sizeof *spec)); 507 SDL_memset(spec, 0, (sizeof *spec));
508 spec->freq = SDL_SwapLE32 (format->frequency); 508 spec->freq = SDL_SwapLE32(format->frequency);
509 switch (SDL_SwapLE16 (format->bitspersample)) { 509 switch (SDL_SwapLE16(format->bitspersample)) {
510 case 4: 510 case 4:
511 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) { 511 if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
512 spec->format = AUDIO_S16; 512 spec->format = AUDIO_S16;
513 } else { 513 } else {
514 was_error = 1; 514 was_error = 1;
523 default: 523 default:
524 was_error = 1; 524 was_error = 1;
525 break; 525 break;
526 } 526 }
527 if (was_error) { 527 if (was_error) {
528 SDL_SetError ("Unknown %d-bit PCM data format", 528 SDL_SetError("Unknown %d-bit PCM data format",
529 SDL_SwapLE16 (format->bitspersample)); 529 SDL_SwapLE16(format->bitspersample));
530 goto done; 530 goto done;
531 } 531 }
532 spec->channels = (Uint8) SDL_SwapLE16 (format->channels); 532 spec->channels = (Uint8) SDL_SwapLE16(format->channels);
533 spec->samples = 4096; /* Good default buffer size */ 533 spec->samples = 4096; /* Good default buffer size */
534 534
535 /* Read the audio data chunk */ 535 /* Read the audio data chunk */
536 *audio_buf = NULL; 536 *audio_buf = NULL;
537 do { 537 do {
538 if (*audio_buf != NULL) { 538 if (*audio_buf != NULL) {
539 SDL_free (*audio_buf); 539 SDL_free(*audio_buf);
540 } 540 }
541 lenread = ReadChunk (src, &chunk); 541 lenread = ReadChunk(src, &chunk);
542 if (lenread < 0) { 542 if (lenread < 0) {
543 was_error = 1; 543 was_error = 1;
544 goto done; 544 goto done;
545 } 545 }
546 *audio_len = lenread; 546 *audio_len = lenread;
547 *audio_buf = chunk.data; 547 *audio_buf = chunk.data;
548 if (chunk.magic != DATA) 548 if (chunk.magic != DATA)
549 headerDiff += lenread + 2 * sizeof (Uint32); 549 headerDiff += lenread + 2 * sizeof(Uint32);
550 } 550 }
551 while (chunk.magic != DATA); 551 while (chunk.magic != DATA);
552 headerDiff += 2 * sizeof (Uint32); /* for the data chunk and len */ 552 headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
553 553
554 if (MS_ADPCM_encoded) { 554 if (MS_ADPCM_encoded) {
555 if (MS_ADPCM_decode (audio_buf, audio_len) < 0) { 555 if (MS_ADPCM_decode(audio_buf, audio_len) < 0) {
556 was_error = 1; 556 was_error = 1;
557 goto done; 557 goto done;
558 } 558 }
559 } 559 }
560 if (IMA_ADPCM_encoded) { 560 if (IMA_ADPCM_encoded) {
561 if (IMA_ADPCM_decode (audio_buf, audio_len) < 0) { 561 if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) {
562 was_error = 1; 562 was_error = 1;
563 goto done; 563 goto done;
564 } 564 }
565 } 565 }
566 566
568 samplesize = ((spec->format & 0xFF) / 8) * spec->channels; 568 samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
569 *audio_len &= ~(samplesize - 1); 569 *audio_len &= ~(samplesize - 1);
570 570
571 done: 571 done:
572 if (format != NULL) { 572 if (format != NULL) {
573 SDL_free (format); 573 SDL_free(format);
574 } 574 }
575 if (src) { 575 if (src) {
576 if (freesrc) { 576 if (freesrc) {
577 SDL_RWclose (src); 577 SDL_RWclose(src);
578 } else { 578 } else {
579 /* seek to the end of the file (given by the RIFF chunk) */ 579 /* seek to the end of the file (given by the RIFF chunk) */
580 SDL_RWseek (src, wavelen - chunk.length - headerDiff, 580 SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
581 RW_SEEK_CUR);
582 } 581 }
583 } 582 }
584 if (was_error) { 583 if (was_error) {
585 spec = NULL; 584 spec = NULL;
586 } 585 }
589 588
590 /* Since the WAV memory is allocated in the shared library, it must also 589 /* Since the WAV memory is allocated in the shared library, it must also
591 be freed here. (Necessary under Win32, VC++) 590 be freed here. (Necessary under Win32, VC++)
592 */ 591 */
593 void 592 void
594 SDL_FreeWAV (Uint8 * audio_buf) 593 SDL_FreeWAV(Uint8 * audio_buf)
595 { 594 {
596 if (audio_buf != NULL) { 595 if (audio_buf != NULL) {
597 SDL_free (audio_buf); 596 SDL_free(audio_buf);
598 } 597 }
599 } 598 }
600 599
601 static int 600 static int
602 ReadChunk (SDL_RWops * src, Chunk * chunk) 601 ReadChunk(SDL_RWops * src, Chunk * chunk)
603 { 602 {
604 chunk->magic = SDL_ReadLE32 (src); 603 chunk->magic = SDL_ReadLE32(src);
605 chunk->length = SDL_ReadLE32 (src); 604 chunk->length = SDL_ReadLE32(src);
606 chunk->data = (Uint8 *) SDL_malloc (chunk->length); 605 chunk->data = (Uint8 *) SDL_malloc(chunk->length);
607 if (chunk->data == NULL) { 606 if (chunk->data == NULL) {
608 SDL_Error (SDL_ENOMEM); 607 SDL_Error(SDL_ENOMEM);
609 return (-1); 608 return (-1);
610 } 609 }
611 if (SDL_RWread (src, chunk->data, chunk->length, 1) != 1) { 610 if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) {
612 SDL_Error (SDL_EFREAD); 611 SDL_Error(SDL_EFREAD);
613 SDL_free (chunk->data); 612 SDL_free(chunk->data);
614 return (-1); 613 return (-1);
615 } 614 }
616 return (chunk->length); 615 return (chunk->length);
617 } 616 }
618 617