19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Microsoft WAVE file loading routines */ 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h" 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_wave.h" 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ReadChunk(SDL_RWops *src, Chunk *chunk); 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct MS_ADPCM_decodestate { 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 hPredictor; 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 iDelta; 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint16 iSamp1; 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint16 iSamp2; 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct MS_ADPCM_decoder { 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WaveFMT wavefmt; 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 wSamplesPerBlock; 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 wNumCoef; 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint16 aCoeff[7][2]; 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* * * */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct MS_ADPCM_decodestate state[2]; 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} MS_ADPCM_state; 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int InitMS_ADPCM(WaveFMT *format) 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *rogue_feel; 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the rogue pointer to the MS_ADPCM specific data */ 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.bitspersample = 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SwapLE16(format->bitspersample); 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel = (Uint8 *)format+sizeof(*format); 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( sizeof(*format) == 16 ) { 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]); 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wNumCoef = ((rogue_feel[1]<<8)|rogue_feel[0]); 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( MS_ADPCM_state.wNumCoef != 7 ) { 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unknown set of MS_ADPCM coefficients"); 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( i=0; i<MS_ADPCM_state.wNumCoef; ++i ) { 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1]<<8)|rogue_feel[0]); 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1]<<8)|rogue_feel[0]); 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state, 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 nybble, Sint16 *coeff) 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 max_audioval = ((1<<(16-1))-1); 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 min_audioval = -(1<<(16-1)); 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 adaptive[] = { 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 230, 230, 230, 230, 307, 409, 512, 614, 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 768, 614, 512, 409, 307, 230, 230, 230 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }; 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 new_sample, delta; 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = ((state->iSamp1 * coeff[0]) + 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (state->iSamp2 * coeff[1]))/256; 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( nybble & 0x08 ) { 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample += state->iDelta * (nybble-0x10); 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample += state->iDelta * nybble; 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( new_sample < min_audioval ) { 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = min_audioval; 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( new_sample > max_audioval ) { 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = max_audioval; 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall delta = ((Sint32)state->iDelta * adaptive[nybble])/256; 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( delta < 16 ) { 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall delta = 16; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->iDelta = (Uint16)delta; 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->iSamp2 = state->iSamp1; 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->iSamp1 = (Sint16)new_sample; 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(new_sample); 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len) 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct MS_ADPCM_decodestate *state[2]; 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *freeable, *encoded, *decoded; 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 encoded_len, samplesleft; 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint8 nybble, stereo; 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint16 *coeff[2]; 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 new_sample; 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allocate the proper sized output buffer */ 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded_len = *audio_len; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded = *audio_buf; 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall freeable = *audio_buf; 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_len = (encoded_len/MS_ADPCM_state.wavefmt.blockalign) * 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wSamplesPerBlock* 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.channels*sizeof(Sint16); 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_buf = (Uint8 *)SDL_malloc(*audio_len); 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( *audio_buf == NULL ) { 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Error(SDL_ENOMEM); 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded = *audio_buf; 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get ready... Go! */ 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall stereo = (MS_ADPCM_state.wavefmt.channels == 2); 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[0] = &MS_ADPCM_state.state[0]; 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[1] = &MS_ADPCM_state.state[stereo]; 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) { 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Grab the initial information for this block */ 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[0]->hPredictor = *encoded++; 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[1]->hPredictor = *encoded++; 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[0]->iDelta = ((encoded[1]<<8)|encoded[0]); 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[1]->iDelta = ((encoded[1]<<8)|encoded[0]); 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[0]->iSamp1 = ((encoded[1]<<8)|encoded[0]); 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[1]->iSamp1 = ((encoded[1]<<8)|encoded[0]); 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[0]->iSamp2 = ((encoded[1]<<8)|encoded[0]); 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[1]->iSamp2 = ((encoded[1]<<8)|encoded[0]); 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += sizeof(Sint16); 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor]; 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor]; 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Store the two initial samples we start with */ 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = state[0]->iSamp2&0xFF; 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = state[0]->iSamp2>>8; 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = state[1]->iSamp2&0xFF; 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = state[1]->iSamp2>>8; 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = state[0]->iSamp1&0xFF; 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = state[0]->iSamp1>>8; 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( stereo ) { 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = state[1]->iSamp1&0xFF; 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = state[1]->iSamp1>>8; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Decode and store the other samples in this block */ 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)* 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_state.wavefmt.channels; 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( samplesleft > 0 ) { 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nybble = (*encoded)>>4; 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]); 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = new_sample&0xFF; 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample >>= 8; 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = new_sample&0xFF; 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nybble = (*encoded)&0x0F; 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = MS_ADPCM_nibble(state[1],nybble,coeff[1]); 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = new_sample&0xFF; 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample >>= 8; 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = new_sample&0xFF; 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ++encoded; 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall samplesleft -= 2; 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded_len -= MS_ADPCM_state.wavefmt.blockalign; 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(freeable); 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct IMA_ADPCM_decodestate { 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 sample; 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint8 index; 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct IMA_ADPCM_decoder { 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WaveFMT wavefmt; 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint16 wSamplesPerBlock; 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* * * */ 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct IMA_ADPCM_decodestate state[2]; 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} IMA_ADPCM_state; 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int InitIMA_ADPCM(WaveFMT *format) 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *rogue_feel; 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the rogue pointer to the IMA_ADPCM specific data */ 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding); 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels); 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency); 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate); 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign); 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.bitspersample = 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SwapLE16(format->bitspersample); 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel = (Uint8 *)format+sizeof(*format); 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( sizeof(*format) == 16 ) { 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rogue_feel += sizeof(Uint16); 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]); 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state,Uint8 nybble) 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 max_audioval = ((1<<(16-1))-1); 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 min_audioval = -(1<<(16-1)); 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const int index_table[16] = { 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall -1, -1, -1, -1, 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2, 4, 6, 8, 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall -1, -1, -1, -1, 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2, 4, 6, 8 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }; 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Sint32 step_table[89] = { 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 22385, 24623, 27086, 29794, 32767 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }; 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 delta, step; 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Compute difference and new sample value */ 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall step = step_table[state->index]; 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall delta = step >> 3; 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( nybble & 0x04 ) delta += step; 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( nybble & 0x02 ) delta += (step >> 1); 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( nybble & 0x01 ) delta += (step >> 2); 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( nybble & 0x08 ) delta = -delta; 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->sample += delta; 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Update index value */ 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->index += index_table[nybble]; 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( state->index > 88 ) { 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->index = 88; 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( state->index < 0 ) { 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->index = 0; 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Clamp output sample */ 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( state->sample > max_audioval ) { 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->sample = max_audioval; 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( state->sample < min_audioval ) { 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state->sample = min_audioval; 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(state->sample); 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Fill the decode buffer with a channel block of data (8 samples) */ 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded, 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int channel, int numchannels, struct IMA_ADPCM_decodestate *state) 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint8 nybble; 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 new_sample; 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += (channel * 2); 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( i=0; i<4; ++i ) { 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nybble = (*encoded)&0x0F; 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = IMA_ADPCM_nibble(state, nybble); 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = new_sample&0xFF; 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample >>= 8; 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = new_sample&0xFF; 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2 * numchannels; 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nybble = (*encoded)>>4; 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample = IMA_ADPCM_nibble(state, nybble); 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = new_sample&0xFF; 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall new_sample >>= 8; 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = new_sample&0xFF; 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2 * numchannels; 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ++encoded; 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len) 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct IMA_ADPCM_decodestate *state; 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *freeable, *encoded, *decoded; 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 encoded_len, samplesleft; 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned int c, channels; 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Check to make sure we have enough variables in the state array */ 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall channels = IMA_ADPCM_state.wavefmt.channels; 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( channels > SDL_arraysize(IMA_ADPCM_state.state) ) { 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("IMA ADPCM decoder can only handle %d channels", 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_arraysize(IMA_ADPCM_state.state)); 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state = IMA_ADPCM_state.state; 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allocate the proper sized output buffer */ 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded_len = *audio_len; 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded = *audio_buf; 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall freeable = *audio_buf; 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_len = (encoded_len/IMA_ADPCM_state.wavefmt.blockalign) * 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wSamplesPerBlock* 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_state.wavefmt.channels*sizeof(Sint16); 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_buf = (Uint8 *)SDL_malloc(*audio_len); 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( *audio_buf == NULL ) { 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Error(SDL_ENOMEM); 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded = *audio_buf; 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Get ready... Go! */ 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( encoded_len >= IMA_ADPCM_state.wavefmt.blockalign ) { 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Grab the initial information for this block */ 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( c=0; c<channels; ++c ) { 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Fill the state information for this block */ 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[c].sample = ((encoded[1]<<8)|encoded[0]); 3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += 2; 3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( state[c].sample & 0x8000 ) { 3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[c].sample -= 0x10000; 3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall state[c].index = *encoded++; 3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Reserved byte in buffer header, should be 0 */ 3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( *encoded++ != 0 ) { 3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Uh oh, corrupt data? Buggy code? */; 3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Store the initial sample we start with */ 3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[0] = (Uint8)(state[c].sample&0xFF); 3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded[1] = (Uint8)(state[c].sample>>8); 3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += 2; 3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Decode and store the other samples in this block */ 3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall samplesleft = (IMA_ADPCM_state.wSamplesPerBlock-1)*channels; 3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( samplesleft > 0 ) { 3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( c=0; c<channels; ++c ) { 3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Fill_IMA_ADPCM_block(decoded, encoded, 3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall c, channels, &state[c]); 3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded += 4; 3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall samplesleft -= 8; 3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall decoded += (channels * 8 * 2); 3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall encoded_len -= IMA_ADPCM_state.wavefmt.blockalign; 3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(freeable); 3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc, 3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) 3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int was_error; 3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Chunk chunk; 3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int lenread; 3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int MS_ADPCM_encoded, IMA_ADPCM_encoded; 3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int samplesize; 4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* WAV magic header */ 4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint32 RIFFchunk; 4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint32 wavelen = 0; 4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint32 WAVEmagic; 4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint32 headerDiff = 0; 4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* FMT chunk */ 4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WaveFMT *format = NULL; 4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Make sure we are passed a valid data source */ 4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 0; 4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( src == NULL ) { 4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Check the magic header */ 4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall RIFFchunk = SDL_ReadLE32(src); 4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall wavelen = SDL_ReadLE32(src); 4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */ 4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WAVEmagic = wavelen; 4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall wavelen = RIFFchunk; 4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall RIFFchunk = RIFF; 4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall WAVEmagic = SDL_ReadLE32(src); 4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) { 4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unrecognized file type (not WAVE)"); 4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall headerDiff += sizeof(Uint32); /* for WAVE */ 4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Read the audio data format chunk */ 4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk.data = NULL; 4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall do { 4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( chunk.data != NULL ) { 4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(chunk.data); 4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk.data = NULL; 4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall lenread = ReadChunk(src, &chunk); 4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( lenread < 0 ) { 4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* 2 Uint32's for chunk header+len, plus the lenread */ 4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall headerDiff += lenread + 2 * sizeof(Uint32); 4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); 4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Decode the audio data format */ 4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall format = (WaveFMT *)chunk.data; 4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( chunk.magic != FMT ) { 4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Complex WAVE files not supported"); 4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; 4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (SDL_SwapLE16(format->encoding)) { 4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case PCM_CODE: 4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We can understand this */ 4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case MS_ADPCM_CODE: 4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Try to understand this */ 4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( InitMS_ADPCM(format) < 0 ) { 4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MS_ADPCM_encoded = 1; 4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case IMA_ADPCM_CODE: 4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Try to understand this */ 4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( InitIMA_ADPCM(format) < 0 ) { 4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall IMA_ADPCM_encoded = 1; 4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case MP3_CODE: 4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("MPEG Layer 3 data not supported", 4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SwapLE16(format->encoding)); 4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unknown WAVE data format: 0x%.4x", 4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SwapLE16(format->encoding)); 4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(spec, 0, (sizeof *spec)); 4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->freq = SDL_SwapLE32(format->frequency); 4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (SDL_SwapLE16(format->bitspersample)) { 4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 4: 4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) { 4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format = AUDIO_S16; 4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 8: 5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format = AUDIO_U8; 5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 16: 5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->format = AUDIO_S16; 5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( was_error ) { 5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unknown %d-bit PCM data format", 5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SwapLE16(format->bitspersample)); 5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->channels = (Uint8)SDL_SwapLE16(format->channels); 5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec->samples = 4096; /* Good default buffer size */ 5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Read the audio data chunk */ 5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_buf = NULL; 5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall do { 5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( *audio_buf != NULL ) { 5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(*audio_buf); 5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_buf = NULL; 5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall lenread = ReadChunk(src, &chunk); 5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( lenread < 0 ) { 5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_len = lenread; 5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_buf = chunk.data; 5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32); 5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } while ( chunk.magic != DATA ); 5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */ 5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( MS_ADPCM_encoded ) { 5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) { 5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( IMA_ADPCM_encoded ) { 5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) { 5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall was_error = 1; 5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto done; 5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Don't return a buffer that isn't a multiple of samplesize */ 5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall samplesize = ((spec->format & 0xFF)/8)*spec->channels; 5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *audio_len &= ~(samplesize-1); 5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone: 5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( format != NULL ) { 5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(format); 5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( src ) { 5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( freesrc ) { 5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_RWclose(src); 5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* seek to the end of the file (given by the RIFF chunk) */ 5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR); 5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( was_error ) { 5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall spec = NULL; 5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(spec); 5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Since the WAV memory is allocated in the shared library, it must also 5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall be freed here. (Necessary under Win32, VC++) 5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FreeWAV(Uint8 *audio_buf) 5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( audio_buf != NULL ) { 5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(audio_buf); 5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ReadChunk(SDL_RWops *src, Chunk *chunk) 5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk->magic = SDL_ReadLE32(src); 5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk->length = SDL_ReadLE32(src); 5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk->data = (Uint8 *)SDL_malloc(chunk->length); 5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( chunk->data == NULL ) { 5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Error(SDL_ENOMEM); 5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) { 5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Error(SDL_EFREAD); 5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(chunk->data); 5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall chunk->data = NULL; 5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(chunk->length); 5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 597