wavestream.c
changeset 617 87116a42526e
parent 601 05123263dab3
child 621 944412baab72
     1.1 --- a/wavestream.c	Tue May 21 21:09:26 2013 -0700
     1.2 +++ b/wavestream.c	Tue May 21 21:21:23 2013 -0700
     1.3 @@ -45,45 +45,45 @@
     1.4  /*******************************************/
     1.5  /* Define values for Microsoft WAVE format */
     1.6  /*******************************************/
     1.7 -#define RIFF		0x46464952		/* "RIFF" */
     1.8 -#define WAVE		0x45564157		/* "WAVE" */
     1.9 -#define FACT		0x74636166		/* "fact" */
    1.10 -#define LIST		0x5453494c		/* "LIST" */
    1.11 -#define FMT		0x20746D66		/* "fmt " */
    1.12 -#define DATA		0x61746164		/* "data" */
    1.13 -#define PCM_CODE	1
    1.14 -#define ADPCM_CODE	2
    1.15 -#define WAVE_MONO	1
    1.16 -#define WAVE_STEREO	2
    1.17 +#define RIFF        0x46464952      /* "RIFF" */
    1.18 +#define WAVE        0x45564157      /* "WAVE" */
    1.19 +#define FACT        0x74636166      /* "fact" */
    1.20 +#define LIST        0x5453494c      /* "LIST" */
    1.21 +#define FMT     0x20746D66      /* "fmt " */
    1.22 +#define DATA        0x61746164      /* "data" */
    1.23 +#define PCM_CODE    1
    1.24 +#define ADPCM_CODE  2
    1.25 +#define WAVE_MONO   1
    1.26 +#define WAVE_STEREO 2
    1.27  
    1.28  /* Normally, these three chunks come consecutively in a WAVE file */
    1.29  typedef struct WaveFMT {
    1.30  /* Not saved in the chunk we read:
    1.31 -	Uint32	FMTchunk;
    1.32 -	Uint32	fmtlen;
    1.33 +    Uint32  FMTchunk;
    1.34 +    Uint32  fmtlen;
    1.35  */
    1.36 -	Uint16	encoding;	
    1.37 -	Uint16	channels;		/* 1 = mono, 2 = stereo */
    1.38 -	Uint32	frequency;		/* One of 11025, 22050, or 44100 Hz */
    1.39 -	Uint32	byterate;		/* Average bytes per second */
    1.40 -	Uint16	blockalign;		/* Bytes per sample block */
    1.41 -	Uint16	bitspersample;		/* One of 8, 12, 16, or 4 for ADPCM */
    1.42 +    Uint16  encoding;
    1.43 +    Uint16  channels;       /* 1 = mono, 2 = stereo */
    1.44 +    Uint32  frequency;      /* One of 11025, 22050, or 44100 Hz */
    1.45 +    Uint32  byterate;       /* Average bytes per second */
    1.46 +    Uint16  blockalign;     /* Bytes per sample block */
    1.47 +    Uint16  bitspersample;      /* One of 8, 12, 16, or 4 for ADPCM */
    1.48  } WaveFMT;
    1.49  
    1.50  /* The general chunk found in the WAVE file */
    1.51  typedef struct Chunk {
    1.52 -	Uint32 magic;
    1.53 -	Uint32 length;
    1.54 -	Uint8 *data;			/* Data includes magic and length */
    1.55 +    Uint32 magic;
    1.56 +    Uint32 length;
    1.57 +    Uint8 *data;            /* Data includes magic and length */
    1.58  } Chunk;
    1.59  
    1.60  /*********************************************/
    1.61  /* Define values for AIFF (IFF audio) format */
    1.62  /*********************************************/
    1.63 -#define FORM		0x4d524f46		/* "FORM" */
    1.64 -#define AIFF		0x46464941		/* "AIFF" */
    1.65 -#define SSND		0x444e5353		/* "SSND" */
    1.66 -#define COMM		0x4d4d4f43		/* "COMM" */
    1.67 +#define FORM        0x4d524f46      /* "FORM" */
    1.68 +#define AIFF        0x46464941      /* "AIFF" */
    1.69 +#define SSND        0x444e5353      /* "SSND" */
    1.70 +#define COMM        0x4d4d4f43      /* "COMM" */
    1.71  
    1.72  
    1.73  /* Currently we only support a single stream at a time */
    1.74 @@ -95,286 +95,286 @@
    1.75  
    1.76  /* Function to load the WAV/AIFF stream */
    1.77  static SDL_RWops *LoadWAVStream (SDL_RWops *rw, SDL_AudioSpec *spec,
    1.78 -					long *start, long *stop);
    1.79 +                    long *start, long *stop);
    1.80  static SDL_RWops *LoadAIFFStream (SDL_RWops *rw, SDL_AudioSpec *spec,
    1.81 -					long *start, long *stop);
    1.82 +                    long *start, long *stop);
    1.83  
    1.84  /* Initialize the WAVStream player, with the given mixer settings
    1.85     This function returns 0, or -1 if there was an error.
    1.86   */
    1.87  int WAVStream_Init(SDL_AudioSpec *mixerfmt)
    1.88  {
    1.89 -	mixer = *mixerfmt;
    1.90 -	return(0);
    1.91 +    mixer = *mixerfmt;
    1.92 +    return(0);
    1.93  }
    1.94  
    1.95  void WAVStream_SetVolume(int volume)
    1.96  {
    1.97 -	wavestream_volume = volume;
    1.98 +    wavestream_volume = volume;
    1.99  }
   1.100  
   1.101  /* Load a WAV stream from the given RWops object */
   1.102  WAVStream *WAVStream_LoadSong_RW(SDL_RWops *rw, const char *magic, int freerw)
   1.103  {
   1.104 -	WAVStream *wave;
   1.105 -	SDL_AudioSpec wavespec;
   1.106 +    WAVStream *wave;
   1.107 +    SDL_AudioSpec wavespec;
   1.108  
   1.109 -	if ( ! mixer.format ) {
   1.110 -		Mix_SetError("WAV music output not started");
   1.111 -		if ( freerw ) {
   1.112 -			SDL_RWclose(rw);
   1.113 -		}
   1.114 -		return(NULL);
   1.115 -	}
   1.116 -	wave = (WAVStream *)SDL_malloc(sizeof *wave);
   1.117 -	if ( wave ) {
   1.118 -		memset(wave, 0, (sizeof *wave));
   1.119 -		wave->freerw = freerw;
   1.120 -		if ( strcmp(magic, "RIFF") == 0 ) {
   1.121 -			wave->rw = LoadWAVStream(rw, &wavespec,
   1.122 -					&wave->start, &wave->stop);
   1.123 -		} else
   1.124 -		if ( strcmp(magic, "FORM") == 0 ) {
   1.125 -			wave->rw = LoadAIFFStream(rw, &wavespec,
   1.126 -					&wave->start, &wave->stop);
   1.127 -		} else {
   1.128 -			Mix_SetError("Unknown WAVE format");
   1.129 -		}
   1.130 -		if ( wave->rw == NULL ) {
   1.131 -			SDL_free(wave);
   1.132 -			if ( freerw ) {
   1.133 -				SDL_RWclose(rw);
   1.134 -			}
   1.135 -			return(NULL);
   1.136 -		}
   1.137 -		SDL_BuildAudioCVT(&wave->cvt,
   1.138 -			wavespec.format, wavespec.channels, wavespec.freq,
   1.139 -			mixer.format, mixer.channels, mixer.freq);
   1.140 -	} else {
   1.141 -		SDL_OutOfMemory();
   1.142 -		if ( freerw ) {
   1.143 -			SDL_RWclose(rw);
   1.144 -		}
   1.145 -		return(NULL);
   1.146 -	}
   1.147 -	return(wave);
   1.148 +    if ( ! mixer.format ) {
   1.149 +        Mix_SetError("WAV music output not started");
   1.150 +        if ( freerw ) {
   1.151 +            SDL_RWclose(rw);
   1.152 +        }
   1.153 +        return(NULL);
   1.154 +    }
   1.155 +    wave = (WAVStream *)SDL_malloc(sizeof *wave);
   1.156 +    if ( wave ) {
   1.157 +        memset(wave, 0, (sizeof *wave));
   1.158 +        wave->freerw = freerw;
   1.159 +        if ( strcmp(magic, "RIFF") == 0 ) {
   1.160 +            wave->rw = LoadWAVStream(rw, &wavespec,
   1.161 +                    &wave->start, &wave->stop);
   1.162 +        } else
   1.163 +        if ( strcmp(magic, "FORM") == 0 ) {
   1.164 +            wave->rw = LoadAIFFStream(rw, &wavespec,
   1.165 +                    &wave->start, &wave->stop);
   1.166 +        } else {
   1.167 +            Mix_SetError("Unknown WAVE format");
   1.168 +        }
   1.169 +        if ( wave->rw == NULL ) {
   1.170 +            SDL_free(wave);
   1.171 +            if ( freerw ) {
   1.172 +                SDL_RWclose(rw);
   1.173 +            }
   1.174 +            return(NULL);
   1.175 +        }
   1.176 +        SDL_BuildAudioCVT(&wave->cvt,
   1.177 +            wavespec.format, wavespec.channels, wavespec.freq,
   1.178 +            mixer.format, mixer.channels, mixer.freq);
   1.179 +    } else {
   1.180 +        SDL_OutOfMemory();
   1.181 +        if ( freerw ) {
   1.182 +            SDL_RWclose(rw);
   1.183 +        }
   1.184 +        return(NULL);
   1.185 +    }
   1.186 +    return(wave);
   1.187  }
   1.188  
   1.189  /* Start playback of a given WAV stream */
   1.190  void WAVStream_Start(WAVStream *wave)
   1.191  {
   1.192 -	SDL_RWseek (wave->rw, wave->start, RW_SEEK_SET);
   1.193 -	music = wave;
   1.194 +    SDL_RWseek (wave->rw, wave->start, RW_SEEK_SET);
   1.195 +    music = wave;
   1.196  }
   1.197  
   1.198  /* Play some of a stream previously started with WAVStream_Start() */
   1.199  int WAVStream_PlaySome(Uint8 *stream, int len)
   1.200  {
   1.201 -	long pos;
   1.202 -	int left = 0;
   1.203 +    long pos;
   1.204 +    int left = 0;
   1.205  
   1.206 -	if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
   1.207 -		if ( music->cvt.needed ) {
   1.208 -			int original_len;
   1.209 +    if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
   1.210 +        if ( music->cvt.needed ) {
   1.211 +            int original_len;
   1.212  
   1.213 -			original_len=(int)((double)len/music->cvt.len_ratio);
   1.214 -			if ( music->cvt.len != original_len ) {
   1.215 -				int worksize;
   1.216 -				if ( music->cvt.buf != NULL ) {
   1.217 -					SDL_free(music->cvt.buf);
   1.218 -				}
   1.219 -				worksize = original_len*music->cvt.len_mult;
   1.220 -				music->cvt.buf=(Uint8 *)SDL_malloc(worksize);
   1.221 -				if ( music->cvt.buf == NULL ) {
   1.222 -					return 0;
   1.223 -				}
   1.224 -				music->cvt.len = original_len;
   1.225 -			}
   1.226 -			if ( (music->stop - pos) < original_len ) {
   1.227 -				left = (original_len - (music->stop - pos));
   1.228 -				original_len -= left;
   1.229 -				left = (int)((double)left*music->cvt.len_ratio);
   1.230 -			}
   1.231 -			original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
   1.232 -			/* At least at the time of writing, SDL_ConvertAudio()
   1.233 -			   does byte-order swapping starting at the end of the
   1.234 -			   buffer. Thus, if we are reading 16-bit samples, we
   1.235 -			   had better make damn sure that we get an even
   1.236 -			   number of bytes, or we'll get garbage.
   1.237 -			 */
   1.238 -			if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
   1.239 -				original_len--;
   1.240 -			}
   1.241 -			music->cvt.len = original_len;
   1.242 -			SDL_ConvertAudio(&music->cvt);
   1.243 -			SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
   1.244 -		} else {
   1.245 -			Uint8 *data;
   1.246 -			if ( (music->stop - pos) < len ) {
   1.247 -				left = (len - (music->stop - pos));
   1.248 -				len -= left;
   1.249 -			}
   1.250 -			data = SDL_stack_alloc(Uint8, len);
   1.251 -			if (data)
   1.252 -			{		
   1.253 -				SDL_RWread(music->rw, data, len, 1);
   1.254 -				SDL_MixAudio(stream, data, len, wavestream_volume);
   1.255 -				SDL_stack_free(data);
   1.256 -			}	
   1.257 -		}
   1.258 -	}
   1.259 -	return left;
   1.260 +            original_len=(int)((double)len/music->cvt.len_ratio);
   1.261 +            if ( music->cvt.len != original_len ) {
   1.262 +                int worksize;
   1.263 +                if ( music->cvt.buf != NULL ) {
   1.264 +                    SDL_free(music->cvt.buf);
   1.265 +                }
   1.266 +                worksize = original_len*music->cvt.len_mult;
   1.267 +                music->cvt.buf=(Uint8 *)SDL_malloc(worksize);
   1.268 +                if ( music->cvt.buf == NULL ) {
   1.269 +                    return 0;
   1.270 +                }
   1.271 +                music->cvt.len = original_len;
   1.272 +            }
   1.273 +            if ( (music->stop - pos) < original_len ) {
   1.274 +                left = (original_len - (music->stop - pos));
   1.275 +                original_len -= left;
   1.276 +                left = (int)((double)left*music->cvt.len_ratio);
   1.277 +            }
   1.278 +            original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
   1.279 +            /* At least at the time of writing, SDL_ConvertAudio()
   1.280 +               does byte-order swapping starting at the end of the
   1.281 +               buffer. Thus, if we are reading 16-bit samples, we
   1.282 +               had better make damn sure that we get an even
   1.283 +               number of bytes, or we'll get garbage.
   1.284 +             */
   1.285 +            if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
   1.286 +                original_len--;
   1.287 +            }
   1.288 +            music->cvt.len = original_len;
   1.289 +            SDL_ConvertAudio(&music->cvt);
   1.290 +            SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
   1.291 +        } else {
   1.292 +            Uint8 *data;
   1.293 +            if ( (music->stop - pos) < len ) {
   1.294 +                left = (len - (music->stop - pos));
   1.295 +                len -= left;
   1.296 +            }
   1.297 +            data = SDL_stack_alloc(Uint8, len);
   1.298 +            if (data)
   1.299 +            {
   1.300 +                SDL_RWread(music->rw, data, len, 1);
   1.301 +                SDL_MixAudio(stream, data, len, wavestream_volume);
   1.302 +                SDL_stack_free(data);
   1.303 +            }
   1.304 +        }
   1.305 +    }
   1.306 +    return left;
   1.307  }
   1.308  
   1.309  /* Stop playback of a stream previously started with WAVStream_Start() */
   1.310  void WAVStream_Stop(void)
   1.311  {
   1.312 -	music = NULL;
   1.313 +    music = NULL;
   1.314  }
   1.315  
   1.316  /* Close the given WAV stream */
   1.317  void WAVStream_FreeSong(WAVStream *wave)
   1.318  {
   1.319 -	if ( wave ) {
   1.320 -		/* Clean up associated data */
   1.321 -		if ( wave->cvt.buf ) {
   1.322 -			SDL_free(wave->cvt.buf);
   1.323 -		}
   1.324 -		if ( wave->freerw ) {
   1.325 -			SDL_RWclose(wave->rw);
   1.326 -		}
   1.327 -		SDL_free(wave);
   1.328 -	}
   1.329 +    if ( wave ) {
   1.330 +        /* Clean up associated data */
   1.331 +        if ( wave->cvt.buf ) {
   1.332 +            SDL_free(wave->cvt.buf);
   1.333 +        }
   1.334 +        if ( wave->freerw ) {
   1.335 +            SDL_RWclose(wave->rw);
   1.336 +        }
   1.337 +        SDL_free(wave);
   1.338 +    }
   1.339  }
   1.340  
   1.341  /* Return non-zero if a stream is currently playing */
   1.342  int WAVStream_Active(void)
   1.343  {
   1.344 -	int active;
   1.345 +    int active;
   1.346  
   1.347 -	active = 0;
   1.348 -	if ( music && (SDL_RWtell(music->rw) < music->stop) ) {
   1.349 -		active = 1;
   1.350 -	}
   1.351 -	return(active);
   1.352 +    active = 0;
   1.353 +    if ( music && (SDL_RWtell(music->rw) < music->stop) ) {
   1.354 +        active = 1;
   1.355 +    }
   1.356 +    return(active);
   1.357  }
   1.358  
   1.359  static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
   1.360  {
   1.361 -	chunk->magic	= SDL_ReadLE32(src);
   1.362 -	chunk->length	= SDL_ReadLE32(src);
   1.363 -	if ( read_data ) {
   1.364 -		chunk->data = (Uint8 *)SDL_malloc(chunk->length);
   1.365 -		if ( chunk->data == NULL ) {
   1.366 -			Mix_SetError("Out of memory");
   1.367 -			return(-1);
   1.368 -		}
   1.369 -		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
   1.370 -			Mix_SetError("Couldn't read chunk");
   1.371 -			SDL_free(chunk->data);
   1.372 -			return(-1);
   1.373 -		}
   1.374 -	} else {
   1.375 -		SDL_RWseek(src, chunk->length, RW_SEEK_CUR);
   1.376 -	}
   1.377 -	return(chunk->length);
   1.378 +    chunk->magic    = SDL_ReadLE32(src);
   1.379 +    chunk->length   = SDL_ReadLE32(src);
   1.380 +    if ( read_data ) {
   1.381 +        chunk->data = (Uint8 *)SDL_malloc(chunk->length);
   1.382 +        if ( chunk->data == NULL ) {
   1.383 +            Mix_SetError("Out of memory");
   1.384 +            return(-1);
   1.385 +        }
   1.386 +        if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
   1.387 +            Mix_SetError("Couldn't read chunk");
   1.388 +            SDL_free(chunk->data);
   1.389 +            return(-1);
   1.390 +        }
   1.391 +    } else {
   1.392 +        SDL_RWseek(src, chunk->length, RW_SEEK_CUR);
   1.393 +    }
   1.394 +    return(chunk->length);
   1.395  }
   1.396  
   1.397  static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec,
   1.398 -					long *start, long *stop)
   1.399 +                    long *start, long *stop)
   1.400  {
   1.401 -	int was_error;
   1.402 -	Chunk chunk;
   1.403 -	int lenread;
   1.404 +    int was_error;
   1.405 +    Chunk chunk;
   1.406 +    int lenread;
   1.407  
   1.408 -	/* WAV magic header */
   1.409 -	Uint32 RIFFchunk;
   1.410 -	Uint32 wavelen;
   1.411 -	Uint32 WAVEmagic;
   1.412 +    /* WAV magic header */
   1.413 +    Uint32 RIFFchunk;
   1.414 +    Uint32 wavelen;
   1.415 +    Uint32 WAVEmagic;
   1.416  
   1.417 -	/* FMT chunk */
   1.418 -	WaveFMT *format = NULL;
   1.419 +    /* FMT chunk */
   1.420 +    WaveFMT *format = NULL;
   1.421  
   1.422 -	was_error = 0;
   1.423 +    was_error = 0;
   1.424  
   1.425 -	/* Check the magic header */
   1.426 -	RIFFchunk	= SDL_ReadLE32(src);
   1.427 -	wavelen		= SDL_ReadLE32(src);
   1.428 -	WAVEmagic	= SDL_ReadLE32(src);
   1.429 -	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
   1.430 -		Mix_SetError("Unrecognized file type (not WAVE)");
   1.431 -		was_error = 1;
   1.432 -		goto done;
   1.433 -	}
   1.434 +    /* Check the magic header */
   1.435 +    RIFFchunk   = SDL_ReadLE32(src);
   1.436 +    wavelen     = SDL_ReadLE32(src);
   1.437 +    WAVEmagic   = SDL_ReadLE32(src);
   1.438 +    if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
   1.439 +        Mix_SetError("Unrecognized file type (not WAVE)");
   1.440 +        was_error = 1;
   1.441 +        goto done;
   1.442 +    }
   1.443  
   1.444 -	/* Read the audio data format chunk */
   1.445 -	chunk.data = NULL;
   1.446 -	do {
   1.447 -		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
   1.448 -		if ( chunk.data ) {
   1.449 -			SDL_free(chunk.data);
   1.450 -		}
   1.451 -		lenread = ReadChunk(src, &chunk, 1);
   1.452 -		if ( lenread < 0 ) {
   1.453 -			was_error = 1;
   1.454 -			goto done;
   1.455 -		}
   1.456 -	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
   1.457 +    /* Read the audio data format chunk */
   1.458 +    chunk.data = NULL;
   1.459 +    do {
   1.460 +        /* FIXME! Add this logic to SDL_LoadWAV_RW() */
   1.461 +        if ( chunk.data ) {
   1.462 +            SDL_free(chunk.data);
   1.463 +        }
   1.464 +        lenread = ReadChunk(src, &chunk, 1);
   1.465 +        if ( lenread < 0 ) {
   1.466 +            was_error = 1;
   1.467 +            goto done;
   1.468 +        }
   1.469 +    } while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
   1.470  
   1.471 -	/* Decode the audio data format */
   1.472 -	format = (WaveFMT *)chunk.data;
   1.473 -	if ( chunk.magic != FMT ) {
   1.474 -		SDL_free(chunk.data);
   1.475 -		Mix_SetError("Complex WAVE files not supported");
   1.476 -		was_error = 1;
   1.477 -		goto done;
   1.478 -	}
   1.479 -	switch (SDL_SwapLE16(format->encoding)) {
   1.480 -		case PCM_CODE:
   1.481 -			/* We can understand this */
   1.482 -			break;
   1.483 -		default:
   1.484 -			Mix_SetError("Unknown WAVE data format");
   1.485 -			was_error = 1;
   1.486 -			goto done;
   1.487 -	}
   1.488 -	memset(spec, 0, (sizeof *spec));
   1.489 -	spec->freq = SDL_SwapLE32(format->frequency);
   1.490 -	switch (SDL_SwapLE16(format->bitspersample)) {
   1.491 -		case 8:
   1.492 -			spec->format = AUDIO_U8;
   1.493 -			break;
   1.494 -		case 16:
   1.495 -			spec->format = AUDIO_S16;
   1.496 -			break;
   1.497 -		default:
   1.498 -			Mix_SetError("Unknown PCM data format");
   1.499 -			was_error = 1;
   1.500 -			goto done;
   1.501 -	}
   1.502 -	spec->channels = (Uint8) SDL_SwapLE16(format->channels);
   1.503 -	spec->samples = 4096;		/* Good default buffer size */
   1.504 +    /* Decode the audio data format */
   1.505 +    format = (WaveFMT *)chunk.data;
   1.506 +    if ( chunk.magic != FMT ) {
   1.507 +        SDL_free(chunk.data);
   1.508 +        Mix_SetError("Complex WAVE files not supported");
   1.509 +        was_error = 1;
   1.510 +        goto done;
   1.511 +    }
   1.512 +    switch (SDL_SwapLE16(format->encoding)) {
   1.513 +        case PCM_CODE:
   1.514 +            /* We can understand this */
   1.515 +            break;
   1.516 +        default:
   1.517 +            Mix_SetError("Unknown WAVE data format");
   1.518 +            was_error = 1;
   1.519 +            goto done;
   1.520 +    }
   1.521 +    memset(spec, 0, (sizeof *spec));
   1.522 +    spec->freq = SDL_SwapLE32(format->frequency);
   1.523 +    switch (SDL_SwapLE16(format->bitspersample)) {
   1.524 +        case 8:
   1.525 +            spec->format = AUDIO_U8;
   1.526 +            break;
   1.527 +        case 16:
   1.528 +            spec->format = AUDIO_S16;
   1.529 +            break;
   1.530 +        default:
   1.531 +            Mix_SetError("Unknown PCM data format");
   1.532 +            was_error = 1;
   1.533 +            goto done;
   1.534 +    }
   1.535 +    spec->channels = (Uint8) SDL_SwapLE16(format->channels);
   1.536 +    spec->samples = 4096;       /* Good default buffer size */
   1.537  
   1.538 -	/* Set the file offset to the DATA chunk data */
   1.539 -	chunk.data = NULL;
   1.540 -	do {
   1.541 -		*start = SDL_RWtell(src) + 2*sizeof(Uint32);
   1.542 -		lenread = ReadChunk(src, &chunk, 0);
   1.543 -		if ( lenread < 0 ) {
   1.544 -			was_error = 1;
   1.545 -			goto done;
   1.546 -		}
   1.547 -	} while ( chunk.magic != DATA );
   1.548 -	*stop = SDL_RWtell(src);
   1.549 +    /* Set the file offset to the DATA chunk data */
   1.550 +    chunk.data = NULL;
   1.551 +    do {
   1.552 +        *start = SDL_RWtell(src) + 2*sizeof(Uint32);
   1.553 +        lenread = ReadChunk(src, &chunk, 0);
   1.554 +        if ( lenread < 0 ) {
   1.555 +            was_error = 1;
   1.556 +            goto done;
   1.557 +        }
   1.558 +    } while ( chunk.magic != DATA );
   1.559 +    *stop = SDL_RWtell(src);
   1.560  
   1.561  done:
   1.562 -	if ( format != NULL ) {
   1.563 -		SDL_free(format);
   1.564 -	}
   1.565 -	if ( was_error ) {
   1.566 -		return NULL;
   1.567 -	}
   1.568 -	return(src);
   1.569 +    if ( format != NULL ) {
   1.570 +        SDL_free(format);
   1.571 +    }
   1.572 +    if ( was_error ) {
   1.573 +        return NULL;
   1.574 +    }
   1.575 +    return(src);
   1.576  }
   1.577  
   1.578  /* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
   1.579 @@ -383,142 +383,142 @@
   1.580  
   1.581  static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
   1.582  {
   1.583 -	/* Negative number? */
   1.584 -	if (sanebuf[0] & 0x80)
   1.585 -		return 0;
   1.586 +    /* Negative number? */
   1.587 +    if (sanebuf[0] & 0x80)
   1.588 +        return 0;
   1.589  
   1.590 -	/* Less than 1? */
   1.591 -	if (sanebuf[0] <= 0x3F)
   1.592 -		return 1;
   1.593 +    /* Less than 1? */
   1.594 +    if (sanebuf[0] <= 0x3F)
   1.595 +        return 1;
   1.596  
   1.597 -	/* Way too big? */
   1.598 -	if (sanebuf[0] > 0x40)
   1.599 -		return 0x4000000;
   1.600 +    /* Way too big? */
   1.601 +    if (sanebuf[0] > 0x40)
   1.602 +        return 0x4000000;
   1.603  
   1.604 -	/* Still too big? */
   1.605 -	if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
   1.606 -		return 800000000;
   1.607 +    /* Still too big? */
   1.608 +    if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
   1.609 +        return 800000000;
   1.610  
   1.611 -	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
   1.612 -		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
   1.613 +    return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
   1.614 +        | (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
   1.615  }
   1.616  
   1.617  static SDL_RWops *LoadAIFFStream (SDL_RWops *src, SDL_AudioSpec *spec,
   1.618 -					long *start, long *stop)
   1.619 +                    long *start, long *stop)
   1.620  {
   1.621 -	int was_error;
   1.622 -	int found_SSND;
   1.623 -	int found_COMM;
   1.624 +    int was_error;
   1.625 +    int found_SSND;
   1.626 +    int found_COMM;
   1.627  
   1.628 -	Uint32 chunk_type;
   1.629 -	Uint32 chunk_length;
   1.630 -	long next_chunk;
   1.631 +    Uint32 chunk_type;
   1.632 +    Uint32 chunk_length;
   1.633 +    long next_chunk;
   1.634  
   1.635 -	/* AIFF magic header */
   1.636 -	Uint32 FORMchunk;
   1.637 -	Uint32 AIFFmagic;
   1.638 -	/* SSND chunk        */
   1.639 -	Uint32 offset;
   1.640 -	Uint32 blocksize;
   1.641 -	/* COMM format chunk */
   1.642 -	Uint16 channels = 0;
   1.643 -	Uint32 numsamples = 0;
   1.644 -	Uint16 samplesize = 0;
   1.645 -	Uint8 sane_freq[10];
   1.646 -	Uint32 frequency = 0;
   1.647 +    /* AIFF magic header */
   1.648 +    Uint32 FORMchunk;
   1.649 +    Uint32 AIFFmagic;
   1.650 +    /* SSND chunk        */
   1.651 +    Uint32 offset;
   1.652 +    Uint32 blocksize;
   1.653 +    /* COMM format chunk */
   1.654 +    Uint16 channels = 0;
   1.655 +    Uint32 numsamples = 0;
   1.656 +    Uint16 samplesize = 0;
   1.657 +    Uint8 sane_freq[10];
   1.658 +    Uint32 frequency = 0;
   1.659  
   1.660 -	was_error = 0;
   1.661 +    was_error = 0;
   1.662  
   1.663 -	/* Check the magic header */
   1.664 -	FORMchunk	= SDL_ReadLE32(src);
   1.665 -	chunk_length	= SDL_ReadBE32(src);
   1.666 -	AIFFmagic	= SDL_ReadLE32(src);
   1.667 -	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
   1.668 -		Mix_SetError("Unrecognized file type (not AIFF)");
   1.669 -		was_error = 1;
   1.670 -		goto done;
   1.671 -	}
   1.672 +    /* Check the magic header */
   1.673 +    FORMchunk   = SDL_ReadLE32(src);
   1.674 +    chunk_length    = SDL_ReadBE32(src);
   1.675 +    AIFFmagic   = SDL_ReadLE32(src);
   1.676 +    if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
   1.677 +        Mix_SetError("Unrecognized file type (not AIFF)");
   1.678 +        was_error = 1;
   1.679 +        goto done;
   1.680 +    }
   1.681  
   1.682 -	/* From what I understand of the specification, chunks may appear in
   1.683 +    /* From what I understand of the specification, chunks may appear in
   1.684           * any order, and we should just ignore unknown ones.
   1.685 -	 *
   1.686 -	 * TODO: Better sanity-checking. E.g. what happens if the AIFF file
   1.687 -	 *       contains compressed sound data?
   1.688 +     *
   1.689 +     * TODO: Better sanity-checking. E.g. what happens if the AIFF file
   1.690 +     *       contains compressed sound data?
   1.691           */
   1.692  
   1.693 -	found_SSND = 0;
   1.694 -	found_COMM = 0;
   1.695 +    found_SSND = 0;
   1.696 +    found_COMM = 0;
   1.697  
   1.698 -	do {
   1.699 -	    chunk_type		= SDL_ReadLE32(src);
   1.700 -	    chunk_length	= SDL_ReadBE32(src);
   1.701 -	    next_chunk		= SDL_RWtell(src) + chunk_length;
   1.702 +    do {
   1.703 +        chunk_type      = SDL_ReadLE32(src);
   1.704 +        chunk_length    = SDL_ReadBE32(src);
   1.705 +        next_chunk      = SDL_RWtell(src) + chunk_length;
   1.706  
   1.707 -	    /* Paranoia to avoid infinite loops */
   1.708 -	    if (chunk_length == 0)
   1.709 -		break;
   1.710 +        /* Paranoia to avoid infinite loops */
   1.711 +        if (chunk_length == 0)
   1.712 +        break;
   1.713  
   1.714              switch (chunk_type) {
   1.715 -		case SSND:
   1.716 -		    found_SSND		= 1;
   1.717 -		    offset		= SDL_ReadBE32(src);
   1.718 -		    blocksize		= SDL_ReadBE32(src);
   1.719 -		    *start		= SDL_RWtell(src) + offset;
   1.720 -		    break;
   1.721 +        case SSND:
   1.722 +            found_SSND      = 1;
   1.723 +            offset      = SDL_ReadBE32(src);
   1.724 +            blocksize       = SDL_ReadBE32(src);
   1.725 +            *start      = SDL_RWtell(src) + offset;
   1.726 +            break;
   1.727  
   1.728 -		case COMM:
   1.729 -		    found_COMM		= 1;
   1.730 +        case COMM:
   1.731 +            found_COMM      = 1;
   1.732  
   1.733 -		    /* Read the audio data format chunk */
   1.734 -		    channels		= SDL_ReadBE16(src);
   1.735 -		    numsamples		= SDL_ReadBE32(src);
   1.736 -		    samplesize		= SDL_ReadBE16(src);
   1.737 -		    SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
   1.738 -		    frequency		= SANE_to_Uint32(sane_freq);
   1.739 -		    break;
   1.740 +            /* Read the audio data format chunk */
   1.741 +            channels        = SDL_ReadBE16(src);
   1.742 +            numsamples      = SDL_ReadBE32(src);
   1.743 +            samplesize      = SDL_ReadBE16(src);
   1.744 +            SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
   1.745 +            frequency       = SANE_to_Uint32(sane_freq);
   1.746 +            break;
   1.747  
   1.748 -		default:
   1.749 -		    break;
   1.750 -	    }
   1.751 -	} while ((!found_SSND || !found_COMM)
   1.752 -		 && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1);
   1.753 +        default:
   1.754 +            break;
   1.755 +        }
   1.756 +    } while ((!found_SSND || !found_COMM)
   1.757 +         && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1);
   1.758  
   1.759 -	if (!found_SSND) {
   1.760 -	    Mix_SetError("Bad AIFF file (no SSND chunk)");
   1.761 -	    was_error = 1;
   1.762 -	    goto done;
   1.763 -	}
   1.764 -		    
   1.765 -	if (!found_COMM) {
   1.766 -	    Mix_SetError("Bad AIFF file (no COMM chunk)");
   1.767 -	    was_error = 1;
   1.768 -	    goto done;
   1.769 -	}
   1.770 +    if (!found_SSND) {
   1.771 +        Mix_SetError("Bad AIFF file (no SSND chunk)");
   1.772 +        was_error = 1;
   1.773 +        goto done;
   1.774 +    }
   1.775  
   1.776 -	*stop = *start + channels * numsamples * (samplesize / 8);
   1.777 +    if (!found_COMM) {
   1.778 +        Mix_SetError("Bad AIFF file (no COMM chunk)");
   1.779 +        was_error = 1;
   1.780 +        goto done;
   1.781 +    }
   1.782  
   1.783 -	/* Decode the audio data format */
   1.784 -	memset(spec, 0, (sizeof *spec));
   1.785 -	spec->freq = frequency;
   1.786 -	switch (samplesize) {
   1.787 -		case 8:
   1.788 -			spec->format = AUDIO_S8;
   1.789 -			break;
   1.790 -		case 16:
   1.791 -			spec->format = AUDIO_S16MSB;
   1.792 -			break;
   1.793 -		default:
   1.794 -			Mix_SetError("Unknown samplesize in data format");
   1.795 -			was_error = 1;
   1.796 -			goto done;
   1.797 -	}
   1.798 -	spec->channels = (Uint8) channels;
   1.799 -	spec->samples = 4096;		/* Good default buffer size */
   1.800 +    *stop = *start + channels * numsamples * (samplesize / 8);
   1.801 +
   1.802 +    /* Decode the audio data format */
   1.803 +    memset(spec, 0, (sizeof *spec));
   1.804 +    spec->freq = frequency;
   1.805 +    switch (samplesize) {
   1.806 +        case 8:
   1.807 +            spec->format = AUDIO_S8;
   1.808 +            break;
   1.809 +        case 16:
   1.810 +            spec->format = AUDIO_S16MSB;
   1.811 +            break;
   1.812 +        default:
   1.813 +            Mix_SetError("Unknown samplesize in data format");
   1.814 +            was_error = 1;
   1.815 +            goto done;
   1.816 +    }
   1.817 +    spec->channels = (Uint8) channels;
   1.818 +    spec->samples = 4096;       /* Good default buffer size */
   1.819  
   1.820  done:
   1.821 -	if ( was_error ) {
   1.822 -		return NULL;
   1.823 -	}
   1.824 -	return(src);
   1.825 +    if ( was_error ) {
   1.826 +        return NULL;
   1.827 +    }
   1.828 +    return(src);
   1.829  }
   1.830