wavestream.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 18:32:49 -0500
changeset 521 565549e046b0
parent 518 8bc9b5fd2aae
child 542 3de4970b36d4
permissions -rw-r--r--
Fixed bugs 1003, 1021, 1168 - fixed memory leak loading music

jjs@jjs.at 2011-03-11 11:37:57 PST
When using SDL_Mixer to play WAVE music, the call to Mix_FreeMusic does not
close the associated file handle.

There is a check in WAVStream_FreeSong() of wavestream.c like this:

if ( wave->freerw ) {
SDL_FreeRW(wave->rw);
}

But the variable freerw is not referenced anywhere else. SDL_FreeRW would also
not close the file from what I can tell.
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@0
    24
/* This file supports streaming WAV files, without volume adjustment */
slouken@0
    25
slouken@0
    26
#include <stdlib.h>
slouken@0
    27
#include <string.h>
slouken@0
    28
slouken@24
    29
#include "SDL_audio.h"
slouken@24
    30
#include "SDL_mutex.h"
slouken@24
    31
#include "SDL_rwops.h"
slouken@24
    32
#include "SDL_endian.h"
slouken@0
    33
slouken@135
    34
#include "SDL_mixer.h"
slouken@0
    35
#include "wavestream.h"
slouken@0
    36
slouken@111
    37
/*
slouken@111
    38
    Taken with permission from SDL_wave.h, part of the SDL library,
slouken@111
    39
    available at: http://www.libsdl.org/
slouken@111
    40
    and placed under the same license as this mixer library.
slouken@111
    41
*/
slouken@111
    42
slouken@111
    43
/* WAVE files are little-endian */
slouken@111
    44
slouken@111
    45
/*******************************************/
slouken@111
    46
/* Define values for Microsoft WAVE format */
slouken@111
    47
/*******************************************/
slouken@111
    48
#define RIFF		0x46464952		/* "RIFF" */
slouken@111
    49
#define WAVE		0x45564157		/* "WAVE" */
slouken@111
    50
#define FACT		0x74636166		/* "fact" */
slouken@111
    51
#define LIST		0x5453494c		/* "LIST" */
slouken@111
    52
#define FMT		0x20746D66		/* "fmt " */
slouken@111
    53
#define DATA		0x61746164		/* "data" */
slouken@111
    54
#define PCM_CODE	1
slouken@111
    55
#define ADPCM_CODE	2
slouken@111
    56
#define WAVE_MONO	1
slouken@111
    57
#define WAVE_STEREO	2
slouken@111
    58
slouken@111
    59
/* Normally, these three chunks come consecutively in a WAVE file */
slouken@111
    60
typedef struct WaveFMT {
slouken@111
    61
/* Not saved in the chunk we read:
slouken@111
    62
	Uint32	FMTchunk;
slouken@111
    63
	Uint32	fmtlen;
slouken@111
    64
*/
slouken@111
    65
	Uint16	encoding;	
slouken@111
    66
	Uint16	channels;		/* 1 = mono, 2 = stereo */
slouken@111
    67
	Uint32	frequency;		/* One of 11025, 22050, or 44100 Hz */
slouken@111
    68
	Uint32	byterate;		/* Average bytes per second */
slouken@111
    69
	Uint16	blockalign;		/* Bytes per sample block */
slouken@111
    70
	Uint16	bitspersample;		/* One of 8, 12, 16, or 4 for ADPCM */
slouken@111
    71
} WaveFMT;
slouken@111
    72
slouken@111
    73
/* The general chunk found in the WAVE file */
slouken@111
    74
typedef struct Chunk {
slouken@111
    75
	Uint32 magic;
slouken@111
    76
	Uint32 length;
slouken@111
    77
	Uint8 *data;			/* Data includes magic and length */
slouken@111
    78
} Chunk;
slouken@111
    79
slouken@111
    80
/*********************************************/
slouken@111
    81
/* Define values for AIFF (IFF audio) format */
slouken@111
    82
/*********************************************/
slouken@111
    83
#define FORM		0x4d524f46		/* "FORM" */
slouken@111
    84
#define AIFF		0x46464941		/* "AIFF" */
slouken@111
    85
#define SSND		0x444e5353		/* "SSND" */
slouken@111
    86
#define COMM		0x4d4d4f43		/* "COMM" */
slouken@111
    87
slouken@111
    88
slouken@64
    89
/* Currently we only support a single stream at a time */
slouken@173
    90
static WAVStream *music = NULL;
slouken@64
    91
slouken@0
    92
/* This is the format of the audio mixer data */
slouken@0
    93
static SDL_AudioSpec mixer;
slouken@214
    94
static int wavestream_volume = MIX_MAX_VOLUME;
slouken@0
    95
slouken@0
    96
/* Function to load the WAV/AIFF stream */
slouken@381
    97
static SDL_RWops *LoadWAVStream (SDL_RWops *rw, SDL_AudioSpec *spec,
slouken@0
    98
					long *start, long *stop);
slouken@381
    99
static SDL_RWops *LoadAIFFStream (SDL_RWops *rw, SDL_AudioSpec *spec,
slouken@0
   100
					long *start, long *stop);
slouken@0
   101
slouken@0
   102
/* Initialize the WAVStream player, with the given mixer settings
slouken@0
   103
   This function returns 0, or -1 if there was an error.
slouken@0
   104
 */
slouken@0
   105
int WAVStream_Init(SDL_AudioSpec *mixerfmt)
slouken@0
   106
{
slouken@0
   107
	mixer = *mixerfmt;
slouken@0
   108
	return(0);
slouken@0
   109
}
slouken@0
   110
slouken@214
   111
void WAVStream_SetVolume(int volume)
slouken@0
   112
{
slouken@214
   113
	wavestream_volume = volume;
slouken@0
   114
}
slouken@0
   115
slouken@381
   116
WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
slouken@381
   117
{
slouken@381
   118
	SDL_RWops *rw;
slouken@381
   119
slouken@381
   120
	rw = SDL_RWFromFile(file, "rb");
slouken@381
   121
	if ( rw == NULL ) {
slouken@381
   122
		SDL_SetError("Couldn't open %s", file);
slouken@381
   123
		return NULL;
slouken@381
   124
	}
slouken@521
   125
	return WAVStream_LoadSong_RW(rw, magic, 1);
slouken@381
   126
}
slouken@381
   127
slouken@0
   128
/* Load a WAV stream from the given file */
slouken@521
   129
WAVStream *WAVStream_LoadSong_RW(SDL_RWops *rw, const char *magic, int freerw)
slouken@0
   130
{
slouken@0
   131
	WAVStream *wave;
slouken@0
   132
	SDL_AudioSpec wavespec;
slouken@0
   133
slouken@0
   134
	if ( ! mixer.format ) {
slouken@134
   135
		Mix_SetError("WAV music output not started");
slouken@521
   136
		if ( freerw ) {
slouken@521
   137
			SDL_RWclose(rw);
slouken@521
   138
		}
slouken@0
   139
		return(NULL);
slouken@0
   140
	}
slouken@0
   141
	wave = (WAVStream *)malloc(sizeof *wave);
slouken@0
   142
	if ( wave ) {
slouken@0
   143
		memset(wave, 0, (sizeof *wave));
slouken@521
   144
		wave->freerw = freerw;
slouken@0
   145
		if ( strcmp(magic, "RIFF") == 0 ) {
slouken@381
   146
			wave->rw = LoadWAVStream(rw, &wavespec,
slouken@0
   147
					&wave->start, &wave->stop);
slouken@0
   148
		} else
slouken@0
   149
		if ( strcmp(magic, "FORM") == 0 ) {
slouken@381
   150
			wave->rw = LoadAIFFStream(rw, &wavespec,
slouken@0
   151
					&wave->start, &wave->stop);
slouken@135
   152
		} else {
slouken@135
   153
			Mix_SetError("Unknown WAVE format");
slouken@0
   154
		}
slouken@381
   155
		if ( wave->rw == NULL ) {
slouken@0
   156
			free(wave);
slouken@521
   157
			if ( freerw ) {
slouken@521
   158
				SDL_RWclose(rw);
slouken@521
   159
			}
slouken@0
   160
			return(NULL);
slouken@0
   161
		}
slouken@0
   162
		SDL_BuildAudioCVT(&wave->cvt,
slouken@0
   163
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   164
			mixer.format, mixer.channels, mixer.freq);
slouken@521
   165
	} else {
slouken@521
   166
		SDL_OutOfMemory();
slouken@521
   167
		if ( freerw ) {
slouken@521
   168
			SDL_RWclose(rw);
slouken@521
   169
		}
slouken@521
   170
		return(NULL);
slouken@0
   171
	}
slouken@0
   172
	return(wave);
slouken@0
   173
}
slouken@0
   174
slouken@0
   175
/* Start playback of a given WAV stream */
slouken@214
   176
void WAVStream_Start(WAVStream *wave)
slouken@0
   177
{
slouken@473
   178
	SDL_RWseek (wave->rw, wave->start, RW_SEEK_SET);
slouken@173
   179
	music = wave;
slouken@0
   180
}
slouken@0
   181
slouken@173
   182
/* Play some of a stream previously started with WAVStream_Start() */
slouken@407
   183
int WAVStream_PlaySome(Uint8 *stream, int len)
slouken@0
   184
{
slouken@0
   185
	long pos;
slouken@407
   186
	int left = 0;
slouken@0
   187
slouken@381
   188
	if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
slouken@173
   189
		if ( music->cvt.needed ) {
slouken@0
   190
			int original_len;
slouken@0
   191
slouken@173
   192
			original_len=(int)((double)len/music->cvt.len_ratio);
slouken@173
   193
			if ( music->cvt.len != original_len ) {
slouken@0
   194
				int worksize;
slouken@173
   195
				if ( music->cvt.buf != NULL ) {
slouken@173
   196
					free(music->cvt.buf);
slouken@0
   197
				}
slouken@173
   198
				worksize = original_len*music->cvt.len_mult;
slouken@173
   199
				music->cvt.buf=(Uint8 *)malloc(worksize);
slouken@173
   200
				if ( music->cvt.buf == NULL ) {
slouken@408
   201
					return 0;
slouken@0
   202
				}
slouken@173
   203
				music->cvt.len = original_len;
slouken@0
   204
			}
slouken@173
   205
			if ( (music->stop - pos) < original_len ) {
slouken@407
   206
				left = (original_len - (music->stop - pos));
slouken@407
   207
				original_len -= left;
slouken@407
   208
				left = (int)((double)left*music->cvt.len_ratio);
slouken@0
   209
			}
slouken@381
   210
			original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
slouken@109
   211
			/* At least at the time of writing, SDL_ConvertAudio()
slouken@109
   212
			   does byte-order swapping starting at the end of the
slouken@109
   213
			   buffer. Thus, if we are reading 16-bit samples, we
slouken@109
   214
			   had better make damn sure that we get an even
slouken@109
   215
			   number of bytes, or we'll get garbage.
slouken@109
   216
			 */
slouken@173
   217
			if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
slouken@109
   218
				original_len--;
slouken@109
   219
			}
slouken@173
   220
			music->cvt.len = original_len;
slouken@173
   221
			SDL_ConvertAudio(&music->cvt);
slouken@214
   222
			SDL_MixAudio(stream, music->cvt.buf, music->cvt.len_cvt, wavestream_volume);
slouken@0
   223
		} else {
slouken@308
   224
			Uint8 *data;
slouken@173
   225
			if ( (music->stop - pos) < len ) {
slouken@407
   226
				left = (len - (music->stop - pos));
slouken@407
   227
				len -= left;
slouken@0
   228
			}
slouken@308
   229
			data = SDL_stack_alloc(Uint8, len);
slouken@308
   230
			if (data)
slouken@308
   231
			{		
slouken@381
   232
				SDL_RWread(music->rw, data, len, 1);
slouken@308
   233
				SDL_MixAudio(stream, data, len, wavestream_volume);
slouken@308
   234
				SDL_stack_free(data);
slouken@308
   235
			}	
slouken@0
   236
		}
slouken@0
   237
	}
slouken@407
   238
	return left;
slouken@0
   239
}
slouken@0
   240
slouken@0
   241
/* Stop playback of a stream previously started with WAVStream_Start() */
slouken@214
   242
void WAVStream_Stop(void)
slouken@0
   243
{
slouken@173
   244
	music = NULL;
slouken@0
   245
}
slouken@0
   246
slouken@0
   247
/* Close the given WAV stream */
slouken@214
   248
void WAVStream_FreeSong(WAVStream *wave)
slouken@0
   249
{
slouken@0
   250
	if ( wave ) {
slouken@0
   251
		/* Clean up associated data */
slouken@0
   252
		if ( wave->cvt.buf ) {
slouken@0
   253
			free(wave->cvt.buf);
slouken@0
   254
		}
slouken@521
   255
		if ( wave->freerw ) {
slouken@521
   256
			SDL_RWclose(wave->rw);
slouken@521
   257
		}
slouken@0
   258
		free(wave);
slouken@0
   259
	}
slouken@0
   260
}
slouken@0
   261
slouken@0
   262
/* Return non-zero if a stream is currently playing */
slouken@214
   263
int WAVStream_Active(void)
slouken@0
   264
{
slouken@0
   265
	int active;
slouken@0
   266
slouken@0
   267
	active = 0;
slouken@381
   268
	if ( music && (SDL_RWtell(music->rw) < music->stop) ) {
slouken@0
   269
		active = 1;
slouken@0
   270
	}
slouken@0
   271
	return(active);
slouken@0
   272
}
slouken@0
   273
slouken@0
   274
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
slouken@0
   275
{
slouken@0
   276
	chunk->magic	= SDL_ReadLE32(src);
slouken@0
   277
	chunk->length	= SDL_ReadLE32(src);
slouken@0
   278
	if ( read_data ) {
slouken@0
   279
		chunk->data = (Uint8 *)malloc(chunk->length);
slouken@0
   280
		if ( chunk->data == NULL ) {
slouken@134
   281
			Mix_SetError("Out of memory");
slouken@0
   282
			return(-1);
slouken@0
   283
		}
slouken@0
   284
		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
slouken@134
   285
			Mix_SetError("Couldn't read chunk");
slouken@0
   286
			free(chunk->data);
slouken@0
   287
			return(-1);
slouken@0
   288
		}
slouken@0
   289
	} else {
slouken@473
   290
		SDL_RWseek(src, chunk->length, RW_SEEK_CUR);
slouken@0
   291
	}
slouken@0
   292
	return(chunk->length);
slouken@0
   293
}
slouken@0
   294
slouken@381
   295
static SDL_RWops *LoadWAVStream (SDL_RWops *src, SDL_AudioSpec *spec,
slouken@0
   296
					long *start, long *stop)
slouken@0
   297
{
slouken@0
   298
	int was_error;
slouken@0
   299
	Chunk chunk;
slouken@0
   300
	int lenread;
slouken@0
   301
slouken@0
   302
	/* WAV magic header */
slouken@0
   303
	Uint32 RIFFchunk;
slouken@0
   304
	Uint32 wavelen;
slouken@0
   305
	Uint32 WAVEmagic;
slouken@0
   306
slouken@0
   307
	/* FMT chunk */
slouken@0
   308
	WaveFMT *format = NULL;
slouken@0
   309
slouken@0
   310
	was_error = 0;
slouken@29
   311
slouken@0
   312
	/* Check the magic header */
slouken@0
   313
	RIFFchunk	= SDL_ReadLE32(src);
slouken@0
   314
	wavelen		= SDL_ReadLE32(src);
slouken@0
   315
	WAVEmagic	= SDL_ReadLE32(src);
slouken@0
   316
	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
slouken@134
   317
		Mix_SetError("Unrecognized file type (not WAVE)");
slouken@0
   318
		was_error = 1;
slouken@0
   319
		goto done;
slouken@0
   320
	}
slouken@0
   321
slouken@0
   322
	/* Read the audio data format chunk */
slouken@0
   323
	chunk.data = NULL;
slouken@0
   324
	do {
slouken@0
   325
		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
slouken@0
   326
		if ( chunk.data ) {
slouken@0
   327
			free(chunk.data);
slouken@0
   328
		}
slouken@0
   329
		lenread = ReadChunk(src, &chunk, 1);
slouken@0
   330
		if ( lenread < 0 ) {
slouken@0
   331
			was_error = 1;
slouken@0
   332
			goto done;
slouken@0
   333
		}
slouken@0
   334
	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
slouken@0
   335
slouken@0
   336
	/* Decode the audio data format */
slouken@0
   337
	format = (WaveFMT *)chunk.data;
slouken@0
   338
	if ( chunk.magic != FMT ) {
slouken@0
   339
		free(chunk.data);
slouken@134
   340
		Mix_SetError("Complex WAVE files not supported");
slouken@0
   341
		was_error = 1;
slouken@0
   342
		goto done;
slouken@0
   343
	}
slouken@0
   344
	switch (SDL_SwapLE16(format->encoding)) {
slouken@0
   345
		case PCM_CODE:
slouken@0
   346
			/* We can understand this */
slouken@0
   347
			break;
slouken@0
   348
		default:
slouken@134
   349
			Mix_SetError("Unknown WAVE data format");
slouken@0
   350
			was_error = 1;
slouken@0
   351
			goto done;
slouken@0
   352
	}
slouken@0
   353
	memset(spec, 0, (sizeof *spec));
slouken@0
   354
	spec->freq = SDL_SwapLE32(format->frequency);
slouken@0
   355
	switch (SDL_SwapLE16(format->bitspersample)) {
slouken@0
   356
		case 8:
slouken@0
   357
			spec->format = AUDIO_U8;
slouken@0
   358
			break;
slouken@0
   359
		case 16:
slouken@0
   360
			spec->format = AUDIO_S16;
slouken@0
   361
			break;
slouken@0
   362
		default:
slouken@134
   363
			Mix_SetError("Unknown PCM data format");
slouken@0
   364
			was_error = 1;
slouken@0
   365
			goto done;
slouken@0
   366
	}
slouken@42
   367
	spec->channels = (Uint8) SDL_SwapLE16(format->channels);
slouken@0
   368
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   369
slouken@0
   370
	/* Set the file offset to the DATA chunk data */
slouken@0
   371
	chunk.data = NULL;
slouken@0
   372
	do {
slouken@0
   373
		*start = SDL_RWtell(src) + 2*sizeof(Uint32);
slouken@0
   374
		lenread = ReadChunk(src, &chunk, 0);
slouken@0
   375
		if ( lenread < 0 ) {
slouken@0
   376
			was_error = 1;
slouken@0
   377
			goto done;
slouken@0
   378
		}
slouken@0
   379
	} while ( chunk.magic != DATA );
slouken@0
   380
	*stop = SDL_RWtell(src);
slouken@0
   381
slouken@0
   382
done:
slouken@0
   383
	if ( format != NULL ) {
slouken@0
   384
		free(format);
slouken@0
   385
	}
slouken@381
   386
	if ( was_error ) {
slouken@381
   387
		return NULL;
slouken@0
   388
	}
slouken@381
   389
	return(src);
slouken@0
   390
}
slouken@0
   391
slouken@95
   392
/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
slouken@95
   393
 * I don't pretend to fully understand it.
slouken@95
   394
 */
slouken@95
   395
slouken@95
   396
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
slouken@0
   397
{
slouken@95
   398
	/* Negative number? */
slouken@95
   399
	if (sanebuf[0] & 0x80)
slouken@95
   400
		return 0;
slouken@0
   401
slouken@95
   402
	/* Less than 1? */
slouken@95
   403
	if (sanebuf[0] <= 0x3F)
slouken@95
   404
		return 1;
slouken@95
   405
slouken@95
   406
	/* Way too big? */
slouken@95
   407
	if (sanebuf[0] > 0x40)
slouken@95
   408
		return 0x4000000;
slouken@95
   409
slouken@95
   410
	/* Still too big? */
slouken@95
   411
	if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
slouken@95
   412
		return 800000000;
slouken@95
   413
slouken@95
   414
	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
slouken@95
   415
		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
slouken@0
   416
}
slouken@0
   417
slouken@381
   418
static SDL_RWops *LoadAIFFStream (SDL_RWops *src, SDL_AudioSpec *spec,
slouken@0
   419
					long *start, long *stop)
slouken@0
   420
{
slouken@0
   421
	int was_error;
slouken@95
   422
	int found_SSND;
slouken@95
   423
	int found_COMM;
slouken@0
   424
slouken@95
   425
	Uint32 chunk_type;
slouken@95
   426
	Uint32 chunk_length;
slouken@95
   427
	long next_chunk;
slouken@95
   428
slouken@0
   429
	/* AIFF magic header */
slouken@0
   430
	Uint32 FORMchunk;
slouken@0
   431
	Uint32 AIFFmagic;
slouken@0
   432
	/* SSND chunk        */
slouken@0
   433
	Uint32 offset;
slouken@0
   434
	Uint32 blocksize;
slouken@0
   435
	/* COMM format chunk */
slouken@142
   436
	Uint16 channels = 0;
slouken@142
   437
	Uint32 numsamples = 0;
slouken@142
   438
	Uint16 samplesize = 0;
slouken@95
   439
	Uint8 sane_freq[10];
slouken@142
   440
	Uint32 frequency = 0;
slouken@0
   441
slouken@0
   442
	was_error = 0;
slouken@29
   443
slouken@0
   444
	/* Check the magic header */
slouken@0
   445
	FORMchunk	= SDL_ReadLE32(src);
slouken@95
   446
	chunk_length	= SDL_ReadBE32(src);
slouken@0
   447
	AIFFmagic	= SDL_ReadLE32(src);
slouken@0
   448
	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
slouken@134
   449
		Mix_SetError("Unrecognized file type (not AIFF)");
slouken@0
   450
		was_error = 1;
slouken@0
   451
		goto done;
slouken@0
   452
	}
slouken@0
   453
slouken@95
   454
	/* From what I understand of the specification, chunks may appear in
slouken@95
   455
         * any order, and we should just ignore unknown ones.
slouken@109
   456
	 *
slouken@109
   457
	 * TODO: Better sanity-checking. E.g. what happens if the AIFF file
slouken@109
   458
	 *       contains compressed sound data?
slouken@95
   459
         */
slouken@95
   460
slouken@95
   461
	found_SSND = 0;
slouken@95
   462
	found_COMM = 0;
slouken@95
   463
slouken@95
   464
	do {
slouken@95
   465
	    chunk_type		= SDL_ReadLE32(src);
slouken@95
   466
	    chunk_length	= SDL_ReadBE32(src);
slouken@95
   467
	    next_chunk		= SDL_RWtell(src) + chunk_length;
slouken@95
   468
slouken@95
   469
	    /* Paranoia to avoid infinite loops */
slouken@95
   470
	    if (chunk_length == 0)
slouken@95
   471
		break;
slouken@95
   472
slouken@95
   473
            switch (chunk_type) {
slouken@95
   474
		case SSND:
slouken@95
   475
		    found_SSND		= 1;
slouken@95
   476
		    offset		= SDL_ReadBE32(src);
slouken@95
   477
		    blocksize		= SDL_ReadBE32(src);
slouken@95
   478
		    *start		= SDL_RWtell(src) + offset;
slouken@95
   479
		    break;
slouken@95
   480
slouken@95
   481
		case COMM:
slouken@95
   482
		    found_COMM		= 1;
slouken@95
   483
slouken@95
   484
		    /* Read the audio data format chunk */
slouken@95
   485
		    channels		= SDL_ReadBE16(src);
slouken@95
   486
		    numsamples		= SDL_ReadBE32(src);
slouken@95
   487
		    samplesize		= SDL_ReadBE16(src);
slouken@95
   488
		    SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
slouken@95
   489
		    frequency		= SANE_to_Uint32(sane_freq);
slouken@95
   490
		    break;
slouken@95
   491
slouken@95
   492
		default:
slouken@95
   493
		    break;
slouken@95
   494
	    }
slouken@95
   495
	} while ((!found_SSND || !found_COMM)
slouken@473
   496
		 && SDL_RWseek(src, next_chunk, RW_SEEK_SET) != -1);
slouken@95
   497
slouken@95
   498
	if (!found_SSND) {
slouken@134
   499
	    Mix_SetError("Bad AIFF file (no SSND chunk)");
slouken@95
   500
	    was_error = 1;
slouken@95
   501
	    goto done;
slouken@0
   502
	}
slouken@95
   503
		    
slouken@95
   504
	if (!found_COMM) {
slouken@134
   505
	    Mix_SetError("Bad AIFF file (no COMM chunk)");
slouken@95
   506
	    was_error = 1;
slouken@95
   507
	    goto done;
slouken@95
   508
	}
slouken@0
   509
slouken@109
   510
	*stop = *start + channels * numsamples * (samplesize / 8);
slouken@0
   511
slouken@0
   512
	/* Decode the audio data format */
slouken@0
   513
	memset(spec, 0, (sizeof *spec));
slouken@0
   514
	spec->freq = frequency;
slouken@0
   515
	switch (samplesize) {
slouken@0
   516
		case 8:
slouken@109
   517
			spec->format = AUDIO_S8;
slouken@0
   518
			break;
slouken@0
   519
		case 16:
slouken@95
   520
			spec->format = AUDIO_S16MSB;
slouken@0
   521
			break;
slouken@0
   522
		default:
slouken@134
   523
			Mix_SetError("Unknown samplesize in data format");
slouken@0
   524
			was_error = 1;
slouken@0
   525
			goto done;
slouken@0
   526
	}
slouken@42
   527
	spec->channels = (Uint8) channels;
slouken@0
   528
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   529
slouken@0
   530
done:
slouken@381
   531
	if ( was_error ) {
slouken@381
   532
		return NULL;
slouken@0
   533
	}
slouken@381
   534
	return(src);
slouken@0
   535
}
slouken@308
   536