src/audio/SDL_wave.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/src/audio/SDL_wave.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/audio/SDL_wave.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -27,571 +27,593 @@
     1.4  #include "SDL_wave.h"
     1.5  
     1.6  
     1.7 -static int ReadChunk(SDL_RWops *src, Chunk *chunk);
     1.8 +static int ReadChunk (SDL_RWops * src, Chunk * chunk);
     1.9  
    1.10 -struct MS_ADPCM_decodestate {
    1.11 -	Uint8 hPredictor;
    1.12 -	Uint16 iDelta;
    1.13 -	Sint16 iSamp1;
    1.14 -	Sint16 iSamp2;
    1.15 +struct MS_ADPCM_decodestate
    1.16 +{
    1.17 +    Uint8 hPredictor;
    1.18 +    Uint16 iDelta;
    1.19 +    Sint16 iSamp1;
    1.20 +    Sint16 iSamp2;
    1.21  };
    1.22 -static struct MS_ADPCM_decoder {
    1.23 -	WaveFMT wavefmt;
    1.24 -	Uint16 wSamplesPerBlock;
    1.25 -	Uint16 wNumCoef;
    1.26 -	Sint16 aCoeff[7][2];
    1.27 -	/* * * */
    1.28 -	struct MS_ADPCM_decodestate state[2];
    1.29 +static struct MS_ADPCM_decoder
    1.30 +{
    1.31 +    WaveFMT wavefmt;
    1.32 +    Uint16 wSamplesPerBlock;
    1.33 +    Uint16 wNumCoef;
    1.34 +    Sint16 aCoeff[7][2];
    1.35 +    /* * * */
    1.36 +    struct MS_ADPCM_decodestate state[2];
    1.37  } MS_ADPCM_state;
    1.38  
    1.39 -static int InitMS_ADPCM(WaveFMT *format)
    1.40 +static int
    1.41 +InitMS_ADPCM (WaveFMT * format)
    1.42  {
    1.43 -	Uint8 *rogue_feel;
    1.44 -	Uint16 extra_info;
    1.45 -	int i;
    1.46 +    Uint8 *rogue_feel;
    1.47 +    Uint16 extra_info;
    1.48 +    int i;
    1.49  
    1.50 -	/* Set the rogue pointer to the MS_ADPCM specific data */
    1.51 -	MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
    1.52 -	MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
    1.53 -	MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
    1.54 -	MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
    1.55 -	MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
    1.56 -	MS_ADPCM_state.wavefmt.bitspersample =
    1.57 -					 SDL_SwapLE16(format->bitspersample);
    1.58 -	rogue_feel = (Uint8 *)format+sizeof(*format);
    1.59 -	if ( sizeof(*format) == 16 ) {
    1.60 -		extra_info = ((rogue_feel[1]<<8)|rogue_feel[0]);
    1.61 -		rogue_feel += sizeof(Uint16);
    1.62 -	}
    1.63 -	MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
    1.64 -	rogue_feel += sizeof(Uint16);
    1.65 -	MS_ADPCM_state.wNumCoef = ((rogue_feel[1]<<8)|rogue_feel[0]);
    1.66 -	rogue_feel += sizeof(Uint16);
    1.67 -	if ( MS_ADPCM_state.wNumCoef != 7 ) {
    1.68 -		SDL_SetError("Unknown set of MS_ADPCM coefficients");
    1.69 -		return(-1);
    1.70 -	}
    1.71 -	for ( i=0; i<MS_ADPCM_state.wNumCoef; ++i ) {
    1.72 -		MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1]<<8)|rogue_feel[0]);
    1.73 -		rogue_feel += sizeof(Uint16);
    1.74 -		MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1]<<8)|rogue_feel[0]);
    1.75 -		rogue_feel += sizeof(Uint16);
    1.76 -	}
    1.77 -	return(0);
    1.78 +    /* Set the rogue pointer to the MS_ADPCM specific data */
    1.79 +    MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16 (format->encoding);
    1.80 +    MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16 (format->channels);
    1.81 +    MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32 (format->frequency);
    1.82 +    MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32 (format->byterate);
    1.83 +    MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16 (format->blockalign);
    1.84 +    MS_ADPCM_state.wavefmt.bitspersample =
    1.85 +        SDL_SwapLE16 (format->bitspersample);
    1.86 +    rogue_feel = (Uint8 *) format + sizeof (*format);
    1.87 +    if (sizeof (*format) == 16) {
    1.88 +        extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]);
    1.89 +        rogue_feel += sizeof (Uint16);
    1.90 +    }
    1.91 +    MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
    1.92 +    rogue_feel += sizeof (Uint16);
    1.93 +    MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]);
    1.94 +    rogue_feel += sizeof (Uint16);
    1.95 +    if (MS_ADPCM_state.wNumCoef != 7) {
    1.96 +        SDL_SetError ("Unknown set of MS_ADPCM coefficients");
    1.97 +        return (-1);
    1.98 +    }
    1.99 +    for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) {
   1.100 +        MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]);
   1.101 +        rogue_feel += sizeof (Uint16);
   1.102 +        MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]);
   1.103 +        rogue_feel += sizeof (Uint16);
   1.104 +    }
   1.105 +    return (0);
   1.106  }
   1.107  
   1.108 -static Sint32 MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
   1.109 -					Uint8 nybble, Sint16 *coeff)
   1.110 +static Sint32
   1.111 +MS_ADPCM_nibble (struct MS_ADPCM_decodestate *state,
   1.112 +                 Uint8 nybble, Sint16 * coeff)
   1.113  {
   1.114 -	const Sint32 max_audioval = ((1<<(16-1))-1);
   1.115 -	const Sint32 min_audioval = -(1<<(16-1));
   1.116 -	const Sint32 adaptive[] = {
   1.117 -		230, 230, 230, 230, 307, 409, 512, 614,
   1.118 -		768, 614, 512, 409, 307, 230, 230, 230
   1.119 -	};
   1.120 -	Sint32 new_sample, delta;
   1.121 +    const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
   1.122 +    const Sint32 min_audioval = -(1 << (16 - 1));
   1.123 +    const Sint32 adaptive[] = {
   1.124 +        230, 230, 230, 230, 307, 409, 512, 614,
   1.125 +        768, 614, 512, 409, 307, 230, 230, 230
   1.126 +    };
   1.127 +    Sint32 new_sample, delta;
   1.128  
   1.129 -	new_sample = ((state->iSamp1 * coeff[0]) +
   1.130 -		      (state->iSamp2 * coeff[1]))/256;
   1.131 -	if ( nybble & 0x08 ) {
   1.132 -		new_sample += state->iDelta * (nybble-0x10);
   1.133 -	} else {
   1.134 -		new_sample += state->iDelta * nybble;
   1.135 -	}
   1.136 -	if ( new_sample < min_audioval ) {
   1.137 -		new_sample = min_audioval;
   1.138 -	} else
   1.139 -	if ( new_sample > max_audioval ) {
   1.140 -		new_sample = max_audioval;
   1.141 -	}
   1.142 -	delta = ((Sint32)state->iDelta * adaptive[nybble])/256;
   1.143 -	if ( delta < 16 ) {
   1.144 -		delta = 16;
   1.145 -	}
   1.146 -	state->iDelta = (Uint16)delta;
   1.147 -	state->iSamp2 = state->iSamp1;
   1.148 -	state->iSamp1 = (Sint16)new_sample;
   1.149 -	return(new_sample);
   1.150 +    new_sample = ((state->iSamp1 * coeff[0]) +
   1.151 +                  (state->iSamp2 * coeff[1])) / 256;
   1.152 +    if (nybble & 0x08) {
   1.153 +        new_sample += state->iDelta * (nybble - 0x10);
   1.154 +    } else {
   1.155 +        new_sample += state->iDelta * nybble;
   1.156 +    }
   1.157 +    if (new_sample < min_audioval) {
   1.158 +        new_sample = min_audioval;
   1.159 +    } else if (new_sample > max_audioval) {
   1.160 +        new_sample = max_audioval;
   1.161 +    }
   1.162 +    delta = ((Sint32) state->iDelta * adaptive[nybble]) / 256;
   1.163 +    if (delta < 16) {
   1.164 +        delta = 16;
   1.165 +    }
   1.166 +    state->iDelta = (Uint16) delta;
   1.167 +    state->iSamp2 = state->iSamp1;
   1.168 +    state->iSamp1 = (Sint16) new_sample;
   1.169 +    return (new_sample);
   1.170  }
   1.171  
   1.172 -static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
   1.173 +static int
   1.174 +MS_ADPCM_decode (Uint8 ** audio_buf, Uint32 * audio_len)
   1.175  {
   1.176 -	struct MS_ADPCM_decodestate *state[2];
   1.177 -	Uint8 *freeable, *encoded, *decoded;
   1.178 -	Sint32 encoded_len, samplesleft;
   1.179 -	Sint8 nybble, stereo;
   1.180 -	Sint16 *coeff[2];
   1.181 -	Sint32 new_sample;
   1.182 +    struct MS_ADPCM_decodestate *state[2];
   1.183 +    Uint8 *freeable, *encoded, *decoded;
   1.184 +    Sint32 encoded_len, samplesleft;
   1.185 +    Sint8 nybble, stereo;
   1.186 +    Sint16 *coeff[2];
   1.187 +    Sint32 new_sample;
   1.188  
   1.189 -	/* Allocate the proper sized output buffer */
   1.190 -	encoded_len = *audio_len;
   1.191 -	encoded = *audio_buf;
   1.192 -	freeable = *audio_buf;
   1.193 -	*audio_len = (encoded_len/MS_ADPCM_state.wavefmt.blockalign) * 
   1.194 -				MS_ADPCM_state.wSamplesPerBlock*
   1.195 -				MS_ADPCM_state.wavefmt.channels*sizeof(Sint16);
   1.196 -	*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
   1.197 -	if ( *audio_buf == NULL ) {
   1.198 -		SDL_Error(SDL_ENOMEM);
   1.199 -		return(-1);
   1.200 -	}
   1.201 -	decoded = *audio_buf;
   1.202 +    /* Allocate the proper sized output buffer */
   1.203 +    encoded_len = *audio_len;
   1.204 +    encoded = *audio_buf;
   1.205 +    freeable = *audio_buf;
   1.206 +    *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) *
   1.207 +        MS_ADPCM_state.wSamplesPerBlock *
   1.208 +        MS_ADPCM_state.wavefmt.channels * sizeof (Sint16);
   1.209 +    *audio_buf = (Uint8 *) SDL_malloc (*audio_len);
   1.210 +    if (*audio_buf == NULL) {
   1.211 +        SDL_Error (SDL_ENOMEM);
   1.212 +        return (-1);
   1.213 +    }
   1.214 +    decoded = *audio_buf;
   1.215  
   1.216 -	/* Get ready... Go! */
   1.217 -	stereo = (MS_ADPCM_state.wavefmt.channels == 2);
   1.218 -	state[0] = &MS_ADPCM_state.state[0];
   1.219 -	state[1] = &MS_ADPCM_state.state[stereo];
   1.220 -	while ( encoded_len >= MS_ADPCM_state.wavefmt.blockalign ) {
   1.221 -		/* Grab the initial information for this block */
   1.222 -		state[0]->hPredictor = *encoded++;
   1.223 -		if ( stereo ) {
   1.224 -			state[1]->hPredictor = *encoded++;
   1.225 -		}
   1.226 -		state[0]->iDelta = ((encoded[1]<<8)|encoded[0]);
   1.227 -		encoded += sizeof(Sint16);
   1.228 -		if ( stereo ) {
   1.229 -			state[1]->iDelta = ((encoded[1]<<8)|encoded[0]);
   1.230 -			encoded += sizeof(Sint16);
   1.231 -		}
   1.232 -		state[0]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
   1.233 -		encoded += sizeof(Sint16);
   1.234 -		if ( stereo ) {
   1.235 -			state[1]->iSamp1 = ((encoded[1]<<8)|encoded[0]);
   1.236 -			encoded += sizeof(Sint16);
   1.237 -		}
   1.238 -		state[0]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
   1.239 -		encoded += sizeof(Sint16);
   1.240 -		if ( stereo ) {
   1.241 -			state[1]->iSamp2 = ((encoded[1]<<8)|encoded[0]);
   1.242 -			encoded += sizeof(Sint16);
   1.243 -		}
   1.244 -		coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
   1.245 -		coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
   1.246 +    /* Get ready... Go! */
   1.247 +    stereo = (MS_ADPCM_state.wavefmt.channels == 2);
   1.248 +    state[0] = &MS_ADPCM_state.state[0];
   1.249 +    state[1] = &MS_ADPCM_state.state[stereo];
   1.250 +    while (encoded_len >= MS_ADPCM_state.wavefmt.blockalign) {
   1.251 +        /* Grab the initial information for this block */
   1.252 +        state[0]->hPredictor = *encoded++;
   1.253 +        if (stereo) {
   1.254 +            state[1]->hPredictor = *encoded++;
   1.255 +        }
   1.256 +        state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
   1.257 +        encoded += sizeof (Sint16);
   1.258 +        if (stereo) {
   1.259 +            state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
   1.260 +            encoded += sizeof (Sint16);
   1.261 +        }
   1.262 +        state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
   1.263 +        encoded += sizeof (Sint16);
   1.264 +        if (stereo) {
   1.265 +            state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
   1.266 +            encoded += sizeof (Sint16);
   1.267 +        }
   1.268 +        state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
   1.269 +        encoded += sizeof (Sint16);
   1.270 +        if (stereo) {
   1.271 +            state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
   1.272 +            encoded += sizeof (Sint16);
   1.273 +        }
   1.274 +        coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
   1.275 +        coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
   1.276  
   1.277 -		/* Store the two initial samples we start with */
   1.278 -		decoded[0] = state[0]->iSamp2&0xFF;
   1.279 -		decoded[1] = state[0]->iSamp2>>8;
   1.280 -		decoded += 2;
   1.281 -		if ( stereo ) {
   1.282 -			decoded[0] = state[1]->iSamp2&0xFF;
   1.283 -			decoded[1] = state[1]->iSamp2>>8;
   1.284 -			decoded += 2;
   1.285 -		}
   1.286 -		decoded[0] = state[0]->iSamp1&0xFF;
   1.287 -		decoded[1] = state[0]->iSamp1>>8;
   1.288 -		decoded += 2;
   1.289 -		if ( stereo ) {
   1.290 -			decoded[0] = state[1]->iSamp1&0xFF;
   1.291 -			decoded[1] = state[1]->iSamp1>>8;
   1.292 -			decoded += 2;
   1.293 -		}
   1.294 +        /* Store the two initial samples we start with */
   1.295 +        decoded[0] = state[0]->iSamp2 & 0xFF;
   1.296 +        decoded[1] = state[0]->iSamp2 >> 8;
   1.297 +        decoded += 2;
   1.298 +        if (stereo) {
   1.299 +            decoded[0] = state[1]->iSamp2 & 0xFF;
   1.300 +            decoded[1] = state[1]->iSamp2 >> 8;
   1.301 +            decoded += 2;
   1.302 +        }
   1.303 +        decoded[0] = state[0]->iSamp1 & 0xFF;
   1.304 +        decoded[1] = state[0]->iSamp1 >> 8;
   1.305 +        decoded += 2;
   1.306 +        if (stereo) {
   1.307 +            decoded[0] = state[1]->iSamp1 & 0xFF;
   1.308 +            decoded[1] = state[1]->iSamp1 >> 8;
   1.309 +            decoded += 2;
   1.310 +        }
   1.311  
   1.312 -		/* Decode and store the other samples in this block */
   1.313 -		samplesleft = (MS_ADPCM_state.wSamplesPerBlock-2)*
   1.314 -					MS_ADPCM_state.wavefmt.channels;
   1.315 -		while ( samplesleft > 0 ) {
   1.316 -			nybble = (*encoded)>>4;
   1.317 -			new_sample = MS_ADPCM_nibble(state[0],nybble,coeff[0]);
   1.318 -			decoded[0] = new_sample&0xFF;
   1.319 -			new_sample >>= 8;
   1.320 -			decoded[1] = new_sample&0xFF;
   1.321 -			decoded += 2;
   1.322 +        /* Decode and store the other samples in this block */
   1.323 +        samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) *
   1.324 +            MS_ADPCM_state.wavefmt.channels;
   1.325 +        while (samplesleft > 0) {
   1.326 +            nybble = (*encoded) >> 4;
   1.327 +            new_sample = MS_ADPCM_nibble (state[0], nybble, coeff[0]);
   1.328 +            decoded[0] = new_sample & 0xFF;
   1.329 +            new_sample >>= 8;
   1.330 +            decoded[1] = new_sample & 0xFF;
   1.331 +            decoded += 2;
   1.332  
   1.333 -			nybble = (*encoded)&0x0F;
   1.334 -			new_sample = MS_ADPCM_nibble(state[1],nybble,coeff[1]);
   1.335 -			decoded[0] = new_sample&0xFF;
   1.336 -			new_sample >>= 8;
   1.337 -			decoded[1] = new_sample&0xFF;
   1.338 -			decoded += 2;
   1.339 +            nybble = (*encoded) & 0x0F;
   1.340 +            new_sample = MS_ADPCM_nibble (state[1], nybble, coeff[1]);
   1.341 +            decoded[0] = new_sample & 0xFF;
   1.342 +            new_sample >>= 8;
   1.343 +            decoded[1] = new_sample & 0xFF;
   1.344 +            decoded += 2;
   1.345  
   1.346 -			++encoded;
   1.347 -			samplesleft -= 2;
   1.348 -		}
   1.349 -		encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
   1.350 -	}
   1.351 -	SDL_free(freeable);
   1.352 -	return(0);
   1.353 +            ++encoded;
   1.354 +            samplesleft -= 2;
   1.355 +        }
   1.356 +        encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
   1.357 +    }
   1.358 +    SDL_free (freeable);
   1.359 +    return (0);
   1.360  }
   1.361  
   1.362 -struct IMA_ADPCM_decodestate {
   1.363 -	Sint32 sample;
   1.364 -	Sint8 index;
   1.365 +struct IMA_ADPCM_decodestate
   1.366 +{
   1.367 +    Sint32 sample;
   1.368 +    Sint8 index;
   1.369  };
   1.370 -static struct IMA_ADPCM_decoder {
   1.371 -	WaveFMT wavefmt;
   1.372 -	Uint16 wSamplesPerBlock;
   1.373 -	/* * * */
   1.374 -	struct IMA_ADPCM_decodestate state[2];
   1.375 +static struct IMA_ADPCM_decoder
   1.376 +{
   1.377 +    WaveFMT wavefmt;
   1.378 +    Uint16 wSamplesPerBlock;
   1.379 +    /* * * */
   1.380 +    struct IMA_ADPCM_decodestate state[2];
   1.381  } IMA_ADPCM_state;
   1.382  
   1.383 -static int InitIMA_ADPCM(WaveFMT *format)
   1.384 +static int
   1.385 +InitIMA_ADPCM (WaveFMT * format)
   1.386  {
   1.387 -	Uint8 *rogue_feel;
   1.388 -	Uint16 extra_info;
   1.389 +    Uint8 *rogue_feel;
   1.390 +    Uint16 extra_info;
   1.391  
   1.392 -	/* Set the rogue pointer to the IMA_ADPCM specific data */
   1.393 -	IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
   1.394 -	IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
   1.395 -	IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
   1.396 -	IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
   1.397 -	IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
   1.398 -	IMA_ADPCM_state.wavefmt.bitspersample =
   1.399 -					 SDL_SwapLE16(format->bitspersample);
   1.400 -	rogue_feel = (Uint8 *)format+sizeof(*format);
   1.401 -	if ( sizeof(*format) == 16 ) {
   1.402 -		extra_info = ((rogue_feel[1]<<8)|rogue_feel[0]);
   1.403 -		rogue_feel += sizeof(Uint16);
   1.404 -	}
   1.405 -	IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1]<<8)|rogue_feel[0]);
   1.406 -	return(0);
   1.407 +    /* Set the rogue pointer to the IMA_ADPCM specific data */
   1.408 +    IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16 (format->encoding);
   1.409 +    IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16 (format->channels);
   1.410 +    IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32 (format->frequency);
   1.411 +    IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32 (format->byterate);
   1.412 +    IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16 (format->blockalign);
   1.413 +    IMA_ADPCM_state.wavefmt.bitspersample =
   1.414 +        SDL_SwapLE16 (format->bitspersample);
   1.415 +    rogue_feel = (Uint8 *) format + sizeof (*format);
   1.416 +    if (sizeof (*format) == 16) {
   1.417 +        extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]);
   1.418 +        rogue_feel += sizeof (Uint16);
   1.419 +    }
   1.420 +    IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
   1.421 +    return (0);
   1.422  }
   1.423  
   1.424 -static Sint32 IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state,Uint8 nybble)
   1.425 +static Sint32
   1.426 +IMA_ADPCM_nibble (struct IMA_ADPCM_decodestate *state, Uint8 nybble)
   1.427  {
   1.428 -	const Sint32 max_audioval = ((1<<(16-1))-1);
   1.429 -	const Sint32 min_audioval = -(1<<(16-1));
   1.430 -	const int index_table[16] = {
   1.431 -		-1, -1, -1, -1,
   1.432 -		 2,  4,  6,  8,
   1.433 -		-1, -1, -1, -1,
   1.434 -		 2,  4,  6,  8
   1.435 -	};
   1.436 -	const Sint32 step_table[89] = {
   1.437 -		7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
   1.438 -		34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
   1.439 -		143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
   1.440 -		449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
   1.441 -		1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
   1.442 -		3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
   1.443 -		9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
   1.444 -		22385, 24623, 27086, 29794, 32767
   1.445 -	};
   1.446 -	Sint32 delta, step;
   1.447 +    const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
   1.448 +    const Sint32 min_audioval = -(1 << (16 - 1));
   1.449 +    const int index_table[16] = {
   1.450 +        -1, -1, -1, -1,
   1.451 +        2, 4, 6, 8,
   1.452 +        -1, -1, -1, -1,
   1.453 +        2, 4, 6, 8
   1.454 +    };
   1.455 +    const Sint32 step_table[89] = {
   1.456 +        7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
   1.457 +        34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
   1.458 +        143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
   1.459 +        449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
   1.460 +        1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
   1.461 +        3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
   1.462 +        9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
   1.463 +        22385, 24623, 27086, 29794, 32767
   1.464 +    };
   1.465 +    Sint32 delta, step;
   1.466  
   1.467 -	/* Compute difference and new sample value */
   1.468 -	step = step_table[state->index];
   1.469 -	delta = step >> 3;
   1.470 -	if ( nybble & 0x04 ) delta += step;
   1.471 -	if ( nybble & 0x02 ) delta += (step >> 1);
   1.472 -	if ( nybble & 0x01 ) delta += (step >> 2);
   1.473 -	if ( nybble & 0x08 ) delta = -delta;
   1.474 -	state->sample += delta;
   1.475 +    /* Compute difference and new sample value */
   1.476 +    step = step_table[state->index];
   1.477 +    delta = step >> 3;
   1.478 +    if (nybble & 0x04)
   1.479 +        delta += step;
   1.480 +    if (nybble & 0x02)
   1.481 +        delta += (step >> 1);
   1.482 +    if (nybble & 0x01)
   1.483 +        delta += (step >> 2);
   1.484 +    if (nybble & 0x08)
   1.485 +        delta = -delta;
   1.486 +    state->sample += delta;
   1.487  
   1.488 -	/* Update index value */
   1.489 -	state->index += index_table[nybble];
   1.490 -	if ( state->index > 88 ) {
   1.491 -		state->index = 88;
   1.492 -	} else
   1.493 -	if ( state->index < 0 ) {
   1.494 -		state->index = 0;
   1.495 -	}
   1.496 +    /* Update index value */
   1.497 +    state->index += index_table[nybble];
   1.498 +    if (state->index > 88) {
   1.499 +        state->index = 88;
   1.500 +    } else if (state->index < 0) {
   1.501 +        state->index = 0;
   1.502 +    }
   1.503  
   1.504 -	/* Clamp output sample */
   1.505 -	if ( state->sample > max_audioval ) {
   1.506 -		state->sample = max_audioval;
   1.507 -	} else
   1.508 -	if ( state->sample < min_audioval ) {
   1.509 -		state->sample = min_audioval;
   1.510 -	}
   1.511 -	return(state->sample);
   1.512 +    /* Clamp output sample */
   1.513 +    if (state->sample > max_audioval) {
   1.514 +        state->sample = max_audioval;
   1.515 +    } else if (state->sample < min_audioval) {
   1.516 +        state->sample = min_audioval;
   1.517 +    }
   1.518 +    return (state->sample);
   1.519  }
   1.520  
   1.521  /* Fill the decode buffer with a channel block of data (8 samples) */
   1.522 -static void Fill_IMA_ADPCM_block(Uint8 *decoded, Uint8 *encoded,
   1.523 -	int channel, int numchannels, struct IMA_ADPCM_decodestate *state)
   1.524 +static void
   1.525 +Fill_IMA_ADPCM_block (Uint8 * decoded, Uint8 * encoded,
   1.526 +                      int channel, int numchannels,
   1.527 +                      struct IMA_ADPCM_decodestate *state)
   1.528  {
   1.529 -	int i;
   1.530 -	Sint8 nybble;
   1.531 -	Sint32 new_sample;
   1.532 +    int i;
   1.533 +    Sint8 nybble;
   1.534 +    Sint32 new_sample;
   1.535  
   1.536 -	decoded += (channel * 2);
   1.537 -	for ( i=0; i<4; ++i ) {
   1.538 -		nybble = (*encoded)&0x0F;
   1.539 -		new_sample = IMA_ADPCM_nibble(state, nybble);
   1.540 -		decoded[0] = new_sample&0xFF;
   1.541 -		new_sample >>= 8;
   1.542 -		decoded[1] = new_sample&0xFF;
   1.543 -		decoded += 2 * numchannels;
   1.544 +    decoded += (channel * 2);
   1.545 +    for (i = 0; i < 4; ++i) {
   1.546 +        nybble = (*encoded) & 0x0F;
   1.547 +        new_sample = IMA_ADPCM_nibble (state, nybble);
   1.548 +        decoded[0] = new_sample & 0xFF;
   1.549 +        new_sample >>= 8;
   1.550 +        decoded[1] = new_sample & 0xFF;
   1.551 +        decoded += 2 * numchannels;
   1.552  
   1.553 -		nybble = (*encoded)>>4;
   1.554 -		new_sample = IMA_ADPCM_nibble(state, nybble);
   1.555 -		decoded[0] = new_sample&0xFF;
   1.556 -		new_sample >>= 8;
   1.557 -		decoded[1] = new_sample&0xFF;
   1.558 -		decoded += 2 * numchannels;
   1.559 +        nybble = (*encoded) >> 4;
   1.560 +        new_sample = IMA_ADPCM_nibble (state, nybble);
   1.561 +        decoded[0] = new_sample & 0xFF;
   1.562 +        new_sample >>= 8;
   1.563 +        decoded[1] = new_sample & 0xFF;
   1.564 +        decoded += 2 * numchannels;
   1.565  
   1.566 -		++encoded;
   1.567 -	}
   1.568 +        ++encoded;
   1.569 +    }
   1.570  }
   1.571  
   1.572 -static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
   1.573 +static int
   1.574 +IMA_ADPCM_decode (Uint8 ** audio_buf, Uint32 * audio_len)
   1.575  {
   1.576 -	struct IMA_ADPCM_decodestate *state;
   1.577 -	Uint8 *freeable, *encoded, *decoded;
   1.578 -	Sint32 encoded_len, samplesleft;
   1.579 -	unsigned int c, channels;
   1.580 +    struct IMA_ADPCM_decodestate *state;
   1.581 +    Uint8 *freeable, *encoded, *decoded;
   1.582 +    Sint32 encoded_len, samplesleft;
   1.583 +    unsigned int c, channels;
   1.584  
   1.585 -	/* Check to make sure we have enough variables in the state array */
   1.586 -	channels = IMA_ADPCM_state.wavefmt.channels;
   1.587 -	if ( channels > SDL_arraysize(IMA_ADPCM_state.state) ) {
   1.588 -		SDL_SetError("IMA ADPCM decoder can only handle %d channels",
   1.589 -					SDL_arraysize(IMA_ADPCM_state.state));
   1.590 -		return(-1);
   1.591 -	}
   1.592 -	state = IMA_ADPCM_state.state;
   1.593 +    /* Check to make sure we have enough variables in the state array */
   1.594 +    channels = IMA_ADPCM_state.wavefmt.channels;
   1.595 +    if (channels > SDL_arraysize (IMA_ADPCM_state.state)) {
   1.596 +        SDL_SetError ("IMA ADPCM decoder can only handle %d channels",
   1.597 +                      SDL_arraysize (IMA_ADPCM_state.state));
   1.598 +        return (-1);
   1.599 +    }
   1.600 +    state = IMA_ADPCM_state.state;
   1.601  
   1.602 -	/* Allocate the proper sized output buffer */
   1.603 -	encoded_len = *audio_len;
   1.604 -	encoded = *audio_buf;
   1.605 -	freeable = *audio_buf;
   1.606 -	*audio_len = (encoded_len/IMA_ADPCM_state.wavefmt.blockalign) * 
   1.607 -				IMA_ADPCM_state.wSamplesPerBlock*
   1.608 -				IMA_ADPCM_state.wavefmt.channels*sizeof(Sint16);
   1.609 -	*audio_buf = (Uint8 *)SDL_malloc(*audio_len);
   1.610 -	if ( *audio_buf == NULL ) {
   1.611 -		SDL_Error(SDL_ENOMEM);
   1.612 -		return(-1);
   1.613 -	}
   1.614 -	decoded = *audio_buf;
   1.615 +    /* Allocate the proper sized output buffer */
   1.616 +    encoded_len = *audio_len;
   1.617 +    encoded = *audio_buf;
   1.618 +    freeable = *audio_buf;
   1.619 +    *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) *
   1.620 +        IMA_ADPCM_state.wSamplesPerBlock *
   1.621 +        IMA_ADPCM_state.wavefmt.channels * sizeof (Sint16);
   1.622 +    *audio_buf = (Uint8 *) SDL_malloc (*audio_len);
   1.623 +    if (*audio_buf == NULL) {
   1.624 +        SDL_Error (SDL_ENOMEM);
   1.625 +        return (-1);
   1.626 +    }
   1.627 +    decoded = *audio_buf;
   1.628  
   1.629 -	/* Get ready... Go! */
   1.630 -	while ( encoded_len >= IMA_ADPCM_state.wavefmt.blockalign ) {
   1.631 -		/* Grab the initial information for this block */
   1.632 -		for ( c=0; c<channels; ++c ) {
   1.633 -			/* Fill the state information for this block */
   1.634 -			state[c].sample = ((encoded[1]<<8)|encoded[0]);
   1.635 -			encoded += 2;
   1.636 -			if ( state[c].sample & 0x8000 ) {
   1.637 -				state[c].sample -= 0x10000;
   1.638 -			}
   1.639 -			state[c].index = *encoded++;
   1.640 -			/* Reserved byte in buffer header, should be 0 */
   1.641 -			if ( *encoded++ != 0 ) {
   1.642 -				/* Uh oh, corrupt data?  Buggy code? */;
   1.643 -			}
   1.644 +    /* Get ready... Go! */
   1.645 +    while (encoded_len >= IMA_ADPCM_state.wavefmt.blockalign) {
   1.646 +        /* Grab the initial information for this block */
   1.647 +        for (c = 0; c < channels; ++c) {
   1.648 +            /* Fill the state information for this block */
   1.649 +            state[c].sample = ((encoded[1] << 8) | encoded[0]);
   1.650 +            encoded += 2;
   1.651 +            if (state[c].sample & 0x8000) {
   1.652 +                state[c].sample -= 0x10000;
   1.653 +            }
   1.654 +            state[c].index = *encoded++;
   1.655 +            /* Reserved byte in buffer header, should be 0 */
   1.656 +            if (*encoded++ != 0) {
   1.657 +                /* Uh oh, corrupt data?  Buggy code? */ ;
   1.658 +            }
   1.659  
   1.660 -			/* Store the initial sample we start with */
   1.661 -			decoded[0] = (Uint8)(state[c].sample&0xFF);
   1.662 -			decoded[1] = (Uint8)(state[c].sample>>8);
   1.663 -			decoded += 2;
   1.664 -		}
   1.665 +            /* Store the initial sample we start with */
   1.666 +            decoded[0] = (Uint8) (state[c].sample & 0xFF);
   1.667 +            decoded[1] = (Uint8) (state[c].sample >> 8);
   1.668 +            decoded += 2;
   1.669 +        }
   1.670  
   1.671 -		/* Decode and store the other samples in this block */
   1.672 -		samplesleft = (IMA_ADPCM_state.wSamplesPerBlock-1)*channels;
   1.673 -		while ( samplesleft > 0 ) {
   1.674 -			for ( c=0; c<channels; ++c ) {
   1.675 -				Fill_IMA_ADPCM_block(decoded, encoded,
   1.676 -						c, channels, &state[c]);
   1.677 -				encoded += 4;
   1.678 -				samplesleft -= 8;
   1.679 -			}
   1.680 -			decoded += (channels * 8 * 2);
   1.681 -		}
   1.682 -		encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
   1.683 -	}
   1.684 -	SDL_free(freeable);
   1.685 -	return(0);
   1.686 +        /* Decode and store the other samples in this block */
   1.687 +        samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels;
   1.688 +        while (samplesleft > 0) {
   1.689 +            for (c = 0; c < channels; ++c) {
   1.690 +                Fill_IMA_ADPCM_block (decoded, encoded,
   1.691 +                                      c, channels, &state[c]);
   1.692 +                encoded += 4;
   1.693 +                samplesleft -= 8;
   1.694 +            }
   1.695 +            decoded += (channels * 8 * 2);
   1.696 +        }
   1.697 +        encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
   1.698 +    }
   1.699 +    SDL_free (freeable);
   1.700 +    return (0);
   1.701  }
   1.702  
   1.703 -SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc,
   1.704 -		SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
   1.705 +SDL_AudioSpec *
   1.706 +SDL_LoadWAV_RW (SDL_RWops * src, int freesrc,
   1.707 +                SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
   1.708  {
   1.709 -	int was_error;
   1.710 -	Chunk chunk;
   1.711 -	int lenread;
   1.712 -	int MS_ADPCM_encoded, IMA_ADPCM_encoded;
   1.713 -	int samplesize;
   1.714 +    int was_error;
   1.715 +    Chunk chunk;
   1.716 +    int lenread;
   1.717 +    int MS_ADPCM_encoded, IMA_ADPCM_encoded;
   1.718 +    int samplesize;
   1.719  
   1.720 -	/* WAV magic header */
   1.721 -	Uint32 RIFFchunk;
   1.722 -	Uint32 wavelen = 0;
   1.723 -	Uint32 WAVEmagic;
   1.724 -	Uint32 headerDiff = 0;
   1.725 +    /* WAV magic header */
   1.726 +    Uint32 RIFFchunk;
   1.727 +    Uint32 wavelen = 0;
   1.728 +    Uint32 WAVEmagic;
   1.729 +    Uint32 headerDiff = 0;
   1.730  
   1.731 -	/* FMT chunk */
   1.732 -	WaveFMT *format = NULL;
   1.733 +    /* FMT chunk */
   1.734 +    WaveFMT *format = NULL;
   1.735 +
   1.736 +    /* Make sure we are passed a valid data source */
   1.737 +    was_error = 0;
   1.738 +    if (src == NULL) {
   1.739 +        was_error = 1;
   1.740 +        goto done;
   1.741 +    }
   1.742  
   1.743 -	/* Make sure we are passed a valid data source */
   1.744 -	was_error = 0;
   1.745 -	if ( src == NULL ) {
   1.746 -		was_error = 1;
   1.747 -		goto done;
   1.748 -	}
   1.749 -		
   1.750 -	/* Check the magic header */
   1.751 -	RIFFchunk	= SDL_ReadLE32(src);
   1.752 -	wavelen		= SDL_ReadLE32(src);
   1.753 -	if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */
   1.754 -		WAVEmagic = wavelen;
   1.755 -		wavelen   = RIFFchunk;
   1.756 -		RIFFchunk = RIFF;
   1.757 -	} else {
   1.758 -		WAVEmagic = SDL_ReadLE32(src);
   1.759 -	}
   1.760 -	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
   1.761 -		SDL_SetError("Unrecognized file type (not WAVE)");
   1.762 -		was_error = 1;
   1.763 -		goto done;
   1.764 -	}
   1.765 -	headerDiff += sizeof(Uint32); /* for WAVE */
   1.766 +    /* Check the magic header */
   1.767 +    RIFFchunk = SDL_ReadLE32 (src);
   1.768 +    wavelen = SDL_ReadLE32 (src);
   1.769 +    if (wavelen == WAVE) {      /* The RIFFchunk has already been read */
   1.770 +        WAVEmagic = wavelen;
   1.771 +        wavelen = RIFFchunk;
   1.772 +        RIFFchunk = RIFF;
   1.773 +    } else {
   1.774 +        WAVEmagic = SDL_ReadLE32 (src);
   1.775 +    }
   1.776 +    if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
   1.777 +        SDL_SetError ("Unrecognized file type (not WAVE)");
   1.778 +        was_error = 1;
   1.779 +        goto done;
   1.780 +    }
   1.781 +    headerDiff += sizeof (Uint32);      /* for WAVE */
   1.782  
   1.783 -	/* Read the audio data format chunk */
   1.784 -	chunk.data = NULL;
   1.785 -	do {
   1.786 -		if ( chunk.data != NULL ) {
   1.787 -			SDL_free(chunk.data);
   1.788 -		}
   1.789 -		lenread = ReadChunk(src, &chunk);
   1.790 -		if ( lenread < 0 ) {
   1.791 -			was_error = 1;
   1.792 -			goto done;
   1.793 -		}
   1.794 -		/* 2 Uint32's for chunk header+len, plus the lenread */
   1.795 -		headerDiff += lenread + 2 * sizeof(Uint32);
   1.796 -	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
   1.797 +    /* Read the audio data format chunk */
   1.798 +    chunk.data = NULL;
   1.799 +    do {
   1.800 +        if (chunk.data != NULL) {
   1.801 +            SDL_free (chunk.data);
   1.802 +        }
   1.803 +        lenread = ReadChunk (src, &chunk);
   1.804 +        if (lenread < 0) {
   1.805 +            was_error = 1;
   1.806 +            goto done;
   1.807 +        }
   1.808 +        /* 2 Uint32's for chunk header+len, plus the lenread */
   1.809 +        headerDiff += lenread + 2 * sizeof (Uint32);
   1.810 +    }
   1.811 +    while ((chunk.magic == FACT) || (chunk.magic == LIST));
   1.812  
   1.813 -	/* Decode the audio data format */
   1.814 -	format = (WaveFMT *)chunk.data;
   1.815 -	if ( chunk.magic != FMT ) {
   1.816 -		SDL_SetError("Complex WAVE files not supported");
   1.817 -		was_error = 1;
   1.818 -		goto done;
   1.819 -	}
   1.820 -	MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
   1.821 -	switch (SDL_SwapLE16(format->encoding)) {
   1.822 -		case PCM_CODE:
   1.823 -			/* We can understand this */
   1.824 -			break;
   1.825 -		case MS_ADPCM_CODE:
   1.826 -			/* Try to understand this */
   1.827 -			if ( InitMS_ADPCM(format) < 0 ) {
   1.828 -				was_error = 1;
   1.829 -				goto done;
   1.830 -			}
   1.831 -			MS_ADPCM_encoded = 1;
   1.832 -			break;
   1.833 -		case IMA_ADPCM_CODE:
   1.834 -			/* Try to understand this */
   1.835 -			if ( InitIMA_ADPCM(format) < 0 ) {
   1.836 -				was_error = 1;
   1.837 -				goto done;
   1.838 -			}
   1.839 -			IMA_ADPCM_encoded = 1;
   1.840 -			break;
   1.841 -		case MP3_CODE:
   1.842 -			SDL_SetError("MPEG Layer 3 data not supported",
   1.843 -					SDL_SwapLE16(format->encoding));
   1.844 -			was_error = 1;
   1.845 -			goto done;
   1.846 -		default:
   1.847 -			SDL_SetError("Unknown WAVE data format: 0x%.4x",
   1.848 -					SDL_SwapLE16(format->encoding));
   1.849 -			was_error = 1;
   1.850 -			goto done;
   1.851 -	}
   1.852 -	SDL_memset(spec, 0, (sizeof *spec));
   1.853 -	spec->freq = SDL_SwapLE32(format->frequency);
   1.854 -	switch (SDL_SwapLE16(format->bitspersample)) {
   1.855 -		case 4:
   1.856 -			if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) {
   1.857 -				spec->format = AUDIO_S16;
   1.858 -			} else {
   1.859 -				was_error = 1;
   1.860 -			}
   1.861 -			break;
   1.862 -		case 8:
   1.863 -			spec->format = AUDIO_U8;
   1.864 -			break;
   1.865 -		case 16:
   1.866 -			spec->format = AUDIO_S16;
   1.867 -			break;
   1.868 -		default:
   1.869 -			was_error = 1;
   1.870 -			break;
   1.871 -	}
   1.872 -	if ( was_error ) {
   1.873 -		SDL_SetError("Unknown %d-bit PCM data format",
   1.874 -			SDL_SwapLE16(format->bitspersample));
   1.875 -		goto done;
   1.876 -	}
   1.877 -	spec->channels = (Uint8)SDL_SwapLE16(format->channels);
   1.878 -	spec->samples = 4096;		/* Good default buffer size */
   1.879 +    /* Decode the audio data format */
   1.880 +    format = (WaveFMT *) chunk.data;
   1.881 +    if (chunk.magic != FMT) {
   1.882 +        SDL_SetError ("Complex WAVE files not supported");
   1.883 +        was_error = 1;
   1.884 +        goto done;
   1.885 +    }
   1.886 +    MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
   1.887 +    switch (SDL_SwapLE16 (format->encoding)) {
   1.888 +    case PCM_CODE:
   1.889 +        /* We can understand this */
   1.890 +        break;
   1.891 +    case MS_ADPCM_CODE:
   1.892 +        /* Try to understand this */
   1.893 +        if (InitMS_ADPCM (format) < 0) {
   1.894 +            was_error = 1;
   1.895 +            goto done;
   1.896 +        }
   1.897 +        MS_ADPCM_encoded = 1;
   1.898 +        break;
   1.899 +    case IMA_ADPCM_CODE:
   1.900 +        /* Try to understand this */
   1.901 +        if (InitIMA_ADPCM (format) < 0) {
   1.902 +            was_error = 1;
   1.903 +            goto done;
   1.904 +        }
   1.905 +        IMA_ADPCM_encoded = 1;
   1.906 +        break;
   1.907 +    case MP3_CODE:
   1.908 +        SDL_SetError ("MPEG Layer 3 data not supported",
   1.909 +                      SDL_SwapLE16 (format->encoding));
   1.910 +        was_error = 1;
   1.911 +        goto done;
   1.912 +    default:
   1.913 +        SDL_SetError ("Unknown WAVE data format: 0x%.4x",
   1.914 +                      SDL_SwapLE16 (format->encoding));
   1.915 +        was_error = 1;
   1.916 +        goto done;
   1.917 +    }
   1.918 +    SDL_memset (spec, 0, (sizeof *spec));
   1.919 +    spec->freq = SDL_SwapLE32 (format->frequency);
   1.920 +    switch (SDL_SwapLE16 (format->bitspersample)) {
   1.921 +    case 4:
   1.922 +        if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
   1.923 +            spec->format = AUDIO_S16;
   1.924 +        } else {
   1.925 +            was_error = 1;
   1.926 +        }
   1.927 +        break;
   1.928 +    case 8:
   1.929 +        spec->format = AUDIO_U8;
   1.930 +        break;
   1.931 +    case 16:
   1.932 +        spec->format = AUDIO_S16;
   1.933 +        break;
   1.934 +    default:
   1.935 +        was_error = 1;
   1.936 +        break;
   1.937 +    }
   1.938 +    if (was_error) {
   1.939 +        SDL_SetError ("Unknown %d-bit PCM data format",
   1.940 +                      SDL_SwapLE16 (format->bitspersample));
   1.941 +        goto done;
   1.942 +    }
   1.943 +    spec->channels = (Uint8) SDL_SwapLE16 (format->channels);
   1.944 +    spec->samples = 4096;       /* Good default buffer size */
   1.945  
   1.946 -	/* Read the audio data chunk */
   1.947 -	*audio_buf = NULL;
   1.948 -	do {
   1.949 -		if ( *audio_buf != NULL ) {
   1.950 -			SDL_free(*audio_buf);
   1.951 -		}
   1.952 -		lenread = ReadChunk(src, &chunk);
   1.953 -		if ( lenread < 0 ) {
   1.954 -			was_error = 1;
   1.955 -			goto done;
   1.956 -		}
   1.957 -		*audio_len = lenread;
   1.958 -		*audio_buf = chunk.data;
   1.959 -		if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32);
   1.960 -	} while ( chunk.magic != DATA );
   1.961 -	headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
   1.962 +    /* Read the audio data chunk */
   1.963 +    *audio_buf = NULL;
   1.964 +    do {
   1.965 +        if (*audio_buf != NULL) {
   1.966 +            SDL_free (*audio_buf);
   1.967 +        }
   1.968 +        lenread = ReadChunk (src, &chunk);
   1.969 +        if (lenread < 0) {
   1.970 +            was_error = 1;
   1.971 +            goto done;
   1.972 +        }
   1.973 +        *audio_len = lenread;
   1.974 +        *audio_buf = chunk.data;
   1.975 +        if (chunk.magic != DATA)
   1.976 +            headerDiff += lenread + 2 * sizeof (Uint32);
   1.977 +    }
   1.978 +    while (chunk.magic != DATA);
   1.979 +    headerDiff += 2 * sizeof (Uint32);  /* for the data chunk and len */
   1.980  
   1.981 -	if ( MS_ADPCM_encoded ) {
   1.982 -		if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) {
   1.983 -			was_error = 1;
   1.984 -			goto done;
   1.985 -		}
   1.986 -	}
   1.987 -	if ( IMA_ADPCM_encoded ) {
   1.988 -		if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) {
   1.989 -			was_error = 1;
   1.990 -			goto done;
   1.991 -		}
   1.992 -	}
   1.993 +    if (MS_ADPCM_encoded) {
   1.994 +        if (MS_ADPCM_decode (audio_buf, audio_len) < 0) {
   1.995 +            was_error = 1;
   1.996 +            goto done;
   1.997 +        }
   1.998 +    }
   1.999 +    if (IMA_ADPCM_encoded) {
  1.1000 +        if (IMA_ADPCM_decode (audio_buf, audio_len) < 0) {
  1.1001 +            was_error = 1;
  1.1002 +            goto done;
  1.1003 +        }
  1.1004 +    }
  1.1005  
  1.1006 -	/* Don't return a buffer that isn't a multiple of samplesize */
  1.1007 -	samplesize = ((spec->format & 0xFF)/8)*spec->channels;
  1.1008 -	*audio_len &= ~(samplesize-1);
  1.1009 +    /* Don't return a buffer that isn't a multiple of samplesize */
  1.1010 +    samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
  1.1011 +    *audio_len &= ~(samplesize - 1);
  1.1012  
  1.1013 -done:
  1.1014 -	if ( format != NULL ) {
  1.1015 -		SDL_free(format);
  1.1016 -	}
  1.1017 -	if ( src ) {
  1.1018 -		if ( freesrc ) {
  1.1019 -			SDL_RWclose(src);
  1.1020 -		} else {
  1.1021 -			/* seek to the end of the file (given by the RIFF chunk) */
  1.1022 -			SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
  1.1023 -		}
  1.1024 -	}
  1.1025 -	if ( was_error ) {
  1.1026 -		spec = NULL;
  1.1027 -	}
  1.1028 -	return(spec);
  1.1029 +  done:
  1.1030 +    if (format != NULL) {
  1.1031 +        SDL_free (format);
  1.1032 +    }
  1.1033 +    if (src) {
  1.1034 +        if (freesrc) {
  1.1035 +            SDL_RWclose (src);
  1.1036 +        } else {
  1.1037 +            /* seek to the end of the file (given by the RIFF chunk) */
  1.1038 +            SDL_RWseek (src, wavelen - chunk.length - headerDiff,
  1.1039 +                        RW_SEEK_CUR);
  1.1040 +        }
  1.1041 +    }
  1.1042 +    if (was_error) {
  1.1043 +        spec = NULL;
  1.1044 +    }
  1.1045 +    return (spec);
  1.1046  }
  1.1047  
  1.1048  /* Since the WAV memory is allocated in the shared library, it must also
  1.1049     be freed here.  (Necessary under Win32, VC++)
  1.1050   */
  1.1051 -void SDL_FreeWAV(Uint8 *audio_buf)
  1.1052 +void
  1.1053 +SDL_FreeWAV (Uint8 * audio_buf)
  1.1054  {
  1.1055 -	if ( audio_buf != NULL ) {
  1.1056 -		SDL_free(audio_buf);
  1.1057 -	}
  1.1058 +    if (audio_buf != NULL) {
  1.1059 +        SDL_free (audio_buf);
  1.1060 +    }
  1.1061  }
  1.1062  
  1.1063 -static int ReadChunk(SDL_RWops *src, Chunk *chunk)
  1.1064 +static int
  1.1065 +ReadChunk (SDL_RWops * src, Chunk * chunk)
  1.1066  {
  1.1067 -	chunk->magic	= SDL_ReadLE32(src);
  1.1068 -	chunk->length	= SDL_ReadLE32(src);
  1.1069 -	chunk->data = (Uint8 *)SDL_malloc(chunk->length);
  1.1070 -	if ( chunk->data == NULL ) {
  1.1071 -		SDL_Error(SDL_ENOMEM);
  1.1072 -		return(-1);
  1.1073 -	}
  1.1074 -	if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
  1.1075 -		SDL_Error(SDL_EFREAD);
  1.1076 -		SDL_free(chunk->data);
  1.1077 -		return(-1);
  1.1078 -	}
  1.1079 -	return(chunk->length);
  1.1080 +    chunk->magic = SDL_ReadLE32 (src);
  1.1081 +    chunk->length = SDL_ReadLE32 (src);
  1.1082 +    chunk->data = (Uint8 *) SDL_malloc (chunk->length);
  1.1083 +    if (chunk->data == NULL) {
  1.1084 +        SDL_Error (SDL_ENOMEM);
  1.1085 +        return (-1);
  1.1086 +    }
  1.1087 +    if (SDL_RWread (src, chunk->data, chunk->length, 1) != 1) {
  1.1088 +        SDL_Error (SDL_EFREAD);
  1.1089 +        SDL_free (chunk->data);
  1.1090 +        return (-1);
  1.1091 +    }
  1.1092 +    return (chunk->length);
  1.1093  }
  1.1094 +
  1.1095 +/* vi: set ts=4 sw=4 expandtab: */