SDL_wave.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* Microsoft WAVE file loading routines */
  20. #include "SDL_audio.h"
  21. #include "SDL_wave.h"
  22. static int ReadChunk(SDL_RWops * src, Chunk * chunk);
  23. struct MS_ADPCM_decodestate
  24. {
  25. Uint8 hPredictor;
  26. Uint16 iDelta;
  27. Sint16 iSamp1;
  28. Sint16 iSamp2;
  29. };
  30. static struct MS_ADPCM_decoder
  31. {
  32. WaveFMT wavefmt;
  33. Uint16 wSamplesPerBlock;
  34. Uint16 wNumCoef;
  35. Sint16 aCoeff[7][2];
  36. /* * * */
  37. struct MS_ADPCM_decodestate state[2];
  38. } MS_ADPCM_state;
  39. static int
  40. InitMS_ADPCM(WaveFMT * format)
  41. {
  42. Uint8 *rogue_feel;
  43. int i;
  44. /* Set the rogue pointer to the MS_ADPCM specific data */
  45. MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
  46. MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
  47. MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
  48. MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
  49. MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
  50. MS_ADPCM_state.wavefmt.bitspersample =
  51. SDL_SwapLE16(format->bitspersample);
  52. rogue_feel = (Uint8 *) format + sizeof(*format);
  53. if (sizeof(*format) == 16) {
  54. /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
  55. rogue_feel += sizeof(Uint16);
  56. }
  57. MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
  58. rogue_feel += sizeof(Uint16);
  59. MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]);
  60. rogue_feel += sizeof(Uint16);
  61. if (MS_ADPCM_state.wNumCoef != 7) {
  62. SDL_SetError("Unknown set of MS_ADPCM coefficients");
  63. return (-1);
  64. }
  65. for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) {
  66. MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]);
  67. rogue_feel += sizeof(Uint16);
  68. MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]);
  69. rogue_feel += sizeof(Uint16);
  70. }
  71. return (0);
  72. }
  73. static Sint32
  74. MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
  75. Uint8 nybble, Sint16 * coeff)
  76. {
  77. const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
  78. const Sint32 min_audioval = -(1 << (16 - 1));
  79. const Sint32 adaptive[] = {
  80. 230, 230, 230, 230, 307, 409, 512, 614,
  81. 768, 614, 512, 409, 307, 230, 230, 230
  82. };
  83. Sint32 new_sample, delta;
  84. new_sample = ((state->iSamp1 * coeff[0]) +
  85. (state->iSamp2 * coeff[1])) / 256;
  86. if (nybble & 0x08) {
  87. new_sample += state->iDelta * (nybble - 0x10);
  88. } else {
  89. new_sample += state->iDelta * nybble;
  90. }
  91. if (new_sample < min_audioval) {
  92. new_sample = min_audioval;
  93. } else if (new_sample > max_audioval) {
  94. new_sample = max_audioval;
  95. }
  96. delta = ((Sint32) state->iDelta * adaptive[nybble]) / 256;
  97. if (delta < 16) {
  98. delta = 16;
  99. }
  100. state->iDelta = (Uint16) delta;
  101. state->iSamp2 = state->iSamp1;
  102. state->iSamp1 = (Sint16) new_sample;
  103. return (new_sample);
  104. }
  105. static int
  106. MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
  107. {
  108. struct MS_ADPCM_decodestate *state[2];
  109. Uint8 *freeable, *encoded, *decoded;
  110. Sint32 encoded_len, samplesleft;
  111. Sint8 nybble;
  112. Uint8 stereo;
  113. Sint16 *coeff[2];
  114. Sint32 new_sample;
  115. /* Allocate the proper sized output buffer */
  116. encoded_len = *audio_len;
  117. encoded = *audio_buf;
  118. freeable = *audio_buf;
  119. *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) *
  120. MS_ADPCM_state.wSamplesPerBlock *
  121. MS_ADPCM_state.wavefmt.channels * sizeof(Sint16);
  122. *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
  123. if (*audio_buf == NULL) {
  124. return SDL_OutOfMemory();
  125. }
  126. decoded = *audio_buf;
  127. /* Get ready... Go! */
  128. stereo = (MS_ADPCM_state.wavefmt.channels == 2);
  129. state[0] = &MS_ADPCM_state.state[0];
  130. state[1] = &MS_ADPCM_state.state[stereo];
  131. while (encoded_len >= MS_ADPCM_state.wavefmt.blockalign) {
  132. /* Grab the initial information for this block */
  133. state[0]->hPredictor = *encoded++;
  134. if (stereo) {
  135. state[1]->hPredictor = *encoded++;
  136. }
  137. state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
  138. encoded += sizeof(Sint16);
  139. if (stereo) {
  140. state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
  141. encoded += sizeof(Sint16);
  142. }
  143. state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
  144. encoded += sizeof(Sint16);
  145. if (stereo) {
  146. state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
  147. encoded += sizeof(Sint16);
  148. }
  149. state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
  150. encoded += sizeof(Sint16);
  151. if (stereo) {
  152. state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
  153. encoded += sizeof(Sint16);
  154. }
  155. coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
  156. coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
  157. /* Store the two initial samples we start with */
  158. decoded[0] = state[0]->iSamp2 & 0xFF;
  159. decoded[1] = state[0]->iSamp2 >> 8;
  160. decoded += 2;
  161. if (stereo) {
  162. decoded[0] = state[1]->iSamp2 & 0xFF;
  163. decoded[1] = state[1]->iSamp2 >> 8;
  164. decoded += 2;
  165. }
  166. decoded[0] = state[0]->iSamp1 & 0xFF;
  167. decoded[1] = state[0]->iSamp1 >> 8;
  168. decoded += 2;
  169. if (stereo) {
  170. decoded[0] = state[1]->iSamp1 & 0xFF;
  171. decoded[1] = state[1]->iSamp1 >> 8;
  172. decoded += 2;
  173. }
  174. /* Decode and store the other samples in this block */
  175. samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) *
  176. MS_ADPCM_state.wavefmt.channels;
  177. while (samplesleft > 0) {
  178. nybble = (*encoded) >> 4;
  179. new_sample = MS_ADPCM_nibble(state[0], nybble, coeff[0]);
  180. decoded[0] = new_sample & 0xFF;
  181. new_sample >>= 8;
  182. decoded[1] = new_sample & 0xFF;
  183. decoded += 2;
  184. nybble = (*encoded) & 0x0F;
  185. new_sample = MS_ADPCM_nibble(state[1], nybble, coeff[1]);
  186. decoded[0] = new_sample & 0xFF;
  187. new_sample >>= 8;
  188. decoded[1] = new_sample & 0xFF;
  189. decoded += 2;
  190. ++encoded;
  191. samplesleft -= 2;
  192. }
  193. encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
  194. }
  195. SDL_free(freeable);
  196. return (0);
  197. }
  198. struct IMA_ADPCM_decodestate
  199. {
  200. Sint32 sample;
  201. Sint8 index;
  202. };
  203. static struct IMA_ADPCM_decoder
  204. {
  205. WaveFMT wavefmt;
  206. Uint16 wSamplesPerBlock;
  207. /* * * */
  208. struct IMA_ADPCM_decodestate state[2];
  209. } IMA_ADPCM_state;
  210. static int
  211. InitIMA_ADPCM(WaveFMT * format)
  212. {
  213. Uint8 *rogue_feel;
  214. /* Set the rogue pointer to the IMA_ADPCM specific data */
  215. IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
  216. IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
  217. IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
  218. IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
  219. IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
  220. IMA_ADPCM_state.wavefmt.bitspersample =
  221. SDL_SwapLE16(format->bitspersample);
  222. rogue_feel = (Uint8 *) format + sizeof(*format);
  223. if (sizeof(*format) == 16) {
  224. /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
  225. rogue_feel += sizeof(Uint16);
  226. }
  227. IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
  228. return (0);
  229. }
  230. static Sint32
  231. IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
  232. {
  233. const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
  234. const Sint32 min_audioval = -(1 << (16 - 1));
  235. const int index_table[16] = {
  236. -1, -1, -1, -1,
  237. 2, 4, 6, 8,
  238. -1, -1, -1, -1,
  239. 2, 4, 6, 8
  240. };
  241. const Sint32 step_table[89] = {
  242. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
  243. 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
  244. 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
  245. 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
  246. 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
  247. 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
  248. 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
  249. 22385, 24623, 27086, 29794, 32767
  250. };
  251. Sint32 delta, step;
  252. /* Compute difference and new sample value */
  253. if (state->index > 88) {
  254. state->index = 88;
  255. } else if (state->index < 0) {
  256. state->index = 0;
  257. }
  258. /* explicit cast to avoid gcc warning about using 'char' as array index */
  259. step = step_table[(int)state->index];
  260. delta = step >> 3;
  261. if (nybble & 0x04)
  262. delta += step;
  263. if (nybble & 0x02)
  264. delta += (step >> 1);
  265. if (nybble & 0x01)
  266. delta += (step >> 2);
  267. if (nybble & 0x08)
  268. delta = -delta;
  269. state->sample += delta;
  270. /* Update index value */
  271. state->index += index_table[nybble];
  272. /* Clamp output sample */
  273. if (state->sample > max_audioval) {
  274. state->sample = max_audioval;
  275. } else if (state->sample < min_audioval) {
  276. state->sample = min_audioval;
  277. }
  278. return (state->sample);
  279. }
  280. /* Fill the decode buffer with a channel block of data (8 samples) */
  281. static void
  282. Fill_IMA_ADPCM_block(Uint8 * decoded, Uint8 * encoded,
  283. int channel, int numchannels,
  284. struct IMA_ADPCM_decodestate *state)
  285. {
  286. int i;
  287. Sint8 nybble;
  288. Sint32 new_sample;
  289. decoded += (channel * 2);
  290. for (i = 0; i < 4; ++i) {
  291. nybble = (*encoded) & 0x0F;
  292. new_sample = IMA_ADPCM_nibble(state, nybble);
  293. decoded[0] = new_sample & 0xFF;
  294. new_sample >>= 8;
  295. decoded[1] = new_sample & 0xFF;
  296. decoded += 2 * numchannels;
  297. nybble = (*encoded) >> 4;
  298. new_sample = IMA_ADPCM_nibble(state, nybble);
  299. decoded[0] = new_sample & 0xFF;
  300. new_sample >>= 8;
  301. decoded[1] = new_sample & 0xFF;
  302. decoded += 2 * numchannels;
  303. ++encoded;
  304. }
  305. }
  306. static int
  307. IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
  308. {
  309. struct IMA_ADPCM_decodestate *state;
  310. Uint8 *freeable, *encoded, *decoded;
  311. Sint32 encoded_len, samplesleft;
  312. unsigned int c, channels;
  313. /* Check to make sure we have enough variables in the state array */
  314. channels = IMA_ADPCM_state.wavefmt.channels;
  315. if (channels > SDL_arraysize(IMA_ADPCM_state.state)) {
  316. SDL_SetError("IMA ADPCM decoder can only handle %u channels",
  317. (unsigned int)SDL_arraysize(IMA_ADPCM_state.state));
  318. return (-1);
  319. }
  320. state = IMA_ADPCM_state.state;
  321. /* Allocate the proper sized output buffer */
  322. encoded_len = *audio_len;
  323. encoded = *audio_buf;
  324. freeable = *audio_buf;
  325. *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) *
  326. IMA_ADPCM_state.wSamplesPerBlock *
  327. IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16);
  328. *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
  329. if (*audio_buf == NULL) {
  330. return SDL_OutOfMemory();
  331. }
  332. decoded = *audio_buf;
  333. /* Get ready... Go! */
  334. while (encoded_len >= IMA_ADPCM_state.wavefmt.blockalign) {
  335. /* Grab the initial information for this block */
  336. for (c = 0; c < channels; ++c) {
  337. /* Fill the state information for this block */
  338. state[c].sample = ((encoded[1] << 8) | encoded[0]);
  339. encoded += 2;
  340. if (state[c].sample & 0x8000) {
  341. state[c].sample -= 0x10000;
  342. }
  343. state[c].index = *encoded++;
  344. /* Reserved byte in buffer header, should be 0 */
  345. if (*encoded++ != 0) {
  346. /* Uh oh, corrupt data? Buggy code? */ ;
  347. }
  348. /* Store the initial sample we start with */
  349. decoded[0] = (Uint8) (state[c].sample & 0xFF);
  350. decoded[1] = (Uint8) (state[c].sample >> 8);
  351. decoded += 2;
  352. }
  353. /* Decode and store the other samples in this block */
  354. samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels;
  355. while (samplesleft > 0) {
  356. for (c = 0; c < channels; ++c) {
  357. Fill_IMA_ADPCM_block(decoded, encoded,
  358. c, channels, &state[c]);
  359. encoded += 4;
  360. samplesleft -= 8;
  361. }
  362. decoded += (channels * 8 * 2);
  363. }
  364. encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
  365. }
  366. SDL_free(freeable);
  367. return (0);
  368. }
  369. SDL_AudioSpec *
  370. SDL_LoadWAV_RW(SDL_RWops * src, int freesrc,
  371. SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
  372. {
  373. int was_error;
  374. Chunk chunk;
  375. int lenread;
  376. int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
  377. int samplesize;
  378. /* WAV magic header */
  379. Uint32 RIFFchunk;
  380. Uint32 wavelen = 0;
  381. Uint32 WAVEmagic;
  382. Uint32 headerDiff = 0;
  383. /* FMT chunk */
  384. WaveFMT *format = NULL;
  385. SDL_zero(chunk);
  386. /* Make sure we are passed a valid data source */
  387. was_error = 0;
  388. if (src == NULL) {
  389. was_error = 1;
  390. goto done;
  391. }
  392. /* Check the magic header */
  393. RIFFchunk = SDL_ReadLE32(src);
  394. wavelen = SDL_ReadLE32(src);
  395. if (wavelen == WAVE) { /* The RIFFchunk has already been read */
  396. WAVEmagic = wavelen;
  397. wavelen = RIFFchunk;
  398. RIFFchunk = RIFF;
  399. } else {
  400. WAVEmagic = SDL_ReadLE32(src);
  401. }
  402. if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
  403. SDL_SetError("Unrecognized file type (not WAVE)");
  404. was_error = 1;
  405. goto done;
  406. }
  407. headerDiff += sizeof(Uint32); /* for WAVE */
  408. /* Read the audio data format chunk */
  409. chunk.data = NULL;
  410. do {
  411. SDL_free(chunk.data);
  412. chunk.data = NULL;
  413. lenread = ReadChunk(src, &chunk);
  414. if (lenread < 0) {
  415. was_error = 1;
  416. goto done;
  417. }
  418. /* 2 Uint32's for chunk header+len, plus the lenread */
  419. headerDiff += lenread + 2 * sizeof(Uint32);
  420. } while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT) || (chunk.magic == JUNK));
  421. /* Decode the audio data format */
  422. format = (WaveFMT *) chunk.data;
  423. if (chunk.magic != FMT) {
  424. SDL_SetError("Complex WAVE files not supported");
  425. was_error = 1;
  426. goto done;
  427. }
  428. IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
  429. switch (SDL_SwapLE16(format->encoding)) {
  430. case PCM_CODE:
  431. /* We can understand this */
  432. break;
  433. case IEEE_FLOAT_CODE:
  434. IEEE_float_encoded = 1;
  435. /* We can understand this */
  436. break;
  437. case MS_ADPCM_CODE:
  438. /* Try to understand this */
  439. if (InitMS_ADPCM(format) < 0) {
  440. was_error = 1;
  441. goto done;
  442. }
  443. MS_ADPCM_encoded = 1;
  444. break;
  445. case IMA_ADPCM_CODE:
  446. /* Try to understand this */
  447. if (InitIMA_ADPCM(format) < 0) {
  448. was_error = 1;
  449. goto done;
  450. }
  451. IMA_ADPCM_encoded = 1;
  452. break;
  453. case MP3_CODE:
  454. SDL_SetError("MPEG Layer 3 data not supported");
  455. was_error = 1;
  456. goto done;
  457. default:
  458. SDL_SetError("Unknown WAVE data format: 0x%.4x",
  459. SDL_SwapLE16(format->encoding));
  460. was_error = 1;
  461. goto done;
  462. }
  463. SDL_zerop(spec);
  464. spec->freq = SDL_SwapLE32(format->frequency);
  465. if (IEEE_float_encoded) {
  466. if ((SDL_SwapLE16(format->bitspersample)) != 32) {
  467. was_error = 1;
  468. } else {
  469. spec->format = AUDIO_F32;
  470. }
  471. } else {
  472. switch (SDL_SwapLE16(format->bitspersample)) {
  473. case 4:
  474. if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
  475. spec->format = AUDIO_S16;
  476. } else {
  477. was_error = 1;
  478. }
  479. break;
  480. case 8:
  481. spec->format = AUDIO_U8;
  482. break;
  483. case 16:
  484. spec->format = AUDIO_S16;
  485. break;
  486. case 32:
  487. spec->format = AUDIO_S32;
  488. break;
  489. default:
  490. was_error = 1;
  491. break;
  492. }
  493. }
  494. if (was_error) {
  495. SDL_SetError("Unknown %d-bit PCM data format",
  496. SDL_SwapLE16(format->bitspersample));
  497. goto done;
  498. }
  499. spec->channels = (Uint8) SDL_SwapLE16(format->channels);
  500. spec->samples = 4096; /* Good default buffer size */
  501. /* Read the audio data chunk */
  502. *audio_buf = NULL;
  503. do {
  504. SDL_free(*audio_buf);
  505. *audio_buf = NULL;
  506. lenread = ReadChunk(src, &chunk);
  507. if (lenread < 0) {
  508. was_error = 1;
  509. goto done;
  510. }
  511. *audio_len = lenread;
  512. *audio_buf = chunk.data;
  513. if (chunk.magic != DATA)
  514. headerDiff += lenread + 2 * sizeof(Uint32);
  515. } while (chunk.magic != DATA);
  516. headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
  517. if (MS_ADPCM_encoded) {
  518. if (MS_ADPCM_decode(audio_buf, audio_len) < 0) {
  519. was_error = 1;
  520. goto done;
  521. }
  522. }
  523. if (IMA_ADPCM_encoded) {
  524. if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) {
  525. was_error = 1;
  526. goto done;
  527. }
  528. }
  529. /* Don't return a buffer that isn't a multiple of samplesize */
  530. samplesize = ((SDL_AUDIO_BITSIZE(spec->format)) / 8) * spec->channels;
  531. *audio_len &= ~(samplesize - 1);
  532. done:
  533. SDL_free(format);
  534. if (src) {
  535. if (freesrc) {
  536. SDL_RWclose(src);
  537. } else {
  538. /* seek to the end of the file (given by the RIFF chunk) */
  539. SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
  540. }
  541. }
  542. if (was_error) {
  543. spec = NULL;
  544. }
  545. return (spec);
  546. }
  547. /* Since the WAV memory is allocated in the shared library, it must also
  548. be freed here. (Necessary under Win32, VC++)
  549. */
  550. void
  551. SDL_FreeWAV(Uint8 * audio_buf)
  552. {
  553. SDL_free(audio_buf);
  554. }
  555. static int
  556. ReadChunk(SDL_RWops * src, Chunk * chunk)
  557. {
  558. chunk->magic = SDL_ReadLE32(src);
  559. chunk->length = SDL_ReadLE32(src);
  560. chunk->data = (Uint8 *) SDL_malloc(chunk->length);
  561. if (chunk->data == NULL) {
  562. return SDL_OutOfMemory();
  563. }
  564. if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) {
  565. SDL_free(chunk->data);
  566. chunk->data = NULL;
  567. return SDL_Error(SDL_EFREAD);
  568. }
  569. return (chunk->length);
  570. }
  571. /* vi: set ts=4 sw=4 expandtab: */