wavestream.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 30 Nov 2001 16:48:33 +0000
changeset 134 2d3417d1860a
parent 111 3b709e102787
child 135 b9cc87fba5ac
permissions -rw-r--r--
Use Mix_SetError() instead of SDL_SetError()
slouken@0
     1
/*
slouken@0
     2
    MIXERLIB:  An audio mixer library based on the SDL library
slouken@0
     3
    Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
/* This file supports streaming WAV files, without volume adjustment */
slouken@0
    26
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <string.h>
slouken@0
    29
slouken@24
    30
#include "SDL_audio.h"
slouken@24
    31
#include "SDL_mutex.h"
slouken@24
    32
#include "SDL_rwops.h"
slouken@24
    33
#include "SDL_endian.h"
slouken@0
    34
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@64
    90
static WAVStream *theWave = NULL;
slouken@64
    91
slouken@64
    92
/* This is initialized by the music mixer */
slouken@64
    93
static SDL_mutex *music_lock = NULL;
slouken@64
    94
slouken@0
    95
/* This is the format of the audio mixer data */
slouken@0
    96
static SDL_AudioSpec mixer;
slouken@0
    97
slouken@0
    98
/* Function to load the WAV/AIFF stream */
slouken@0
    99
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   100
					long *start, long *stop);
slouken@0
   101
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   102
					long *start, long *stop);
slouken@0
   103
slouken@0
   104
/* Initialize the WAVStream player, with the given mixer settings
slouken@0
   105
   This function returns 0, or -1 if there was an error.
slouken@0
   106
 */
slouken@0
   107
int WAVStream_Init(SDL_AudioSpec *mixerfmt)
slouken@0
   108
{
slouken@64
   109
	/* FIXME: clean up the mutex, or move it into music.c */
slouken@64
   110
	music_lock = SDL_CreateMutex();
slouken@64
   111
#ifndef macintosh /* Hmm.. */
slouken@64
   112
	if ( music_lock == NULL ) {
slouken@64
   113
		return(-1);
slouken@64
   114
	}
slouken@64
   115
#endif
slouken@0
   116
	mixer = *mixerfmt;
slouken@0
   117
	return(0);
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
/* Unimplemented */
slouken@0
   121
extern void WAVStream_SetVolume(int volume)
slouken@0
   122
{
slouken@0
   123
}
slouken@0
   124
slouken@0
   125
/* Load a WAV stream from the given file */
slouken@0
   126
extern WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
slouken@0
   127
{
slouken@0
   128
	WAVStream *wave;
slouken@0
   129
	SDL_AudioSpec wavespec;
slouken@0
   130
slouken@0
   131
	if ( ! mixer.format ) {
slouken@134
   132
		Mix_SetError("WAV music output not started");
slouken@0
   133
		return(NULL);
slouken@0
   134
	}
slouken@0
   135
	wave = (WAVStream *)malloc(sizeof *wave);
slouken@0
   136
	if ( wave ) {
slouken@0
   137
		memset(wave, 0, (sizeof *wave));
slouken@0
   138
		if ( strcmp(magic, "RIFF") == 0 ) {
slouken@0
   139
			wave->wavefp = LoadWAVStream(file, &wavespec,
slouken@0
   140
					&wave->start, &wave->stop);
slouken@0
   141
		} else
slouken@0
   142
		if ( strcmp(magic, "FORM") == 0 ) {
slouken@0
   143
			wave->wavefp = LoadAIFFStream(file, &wavespec,
slouken@0
   144
					&wave->start, &wave->stop);
slouken@0
   145
		}
slouken@0
   146
		if ( wave->wavefp == NULL ) {
slouken@0
   147
			free(wave);
slouken@0
   148
			return(NULL);
slouken@0
   149
		}
slouken@0
   150
		SDL_BuildAudioCVT(&wave->cvt,
slouken@0
   151
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
   152
			mixer.format, mixer.channels, mixer.freq);
slouken@0
   153
	}
slouken@0
   154
	return(wave);
slouken@0
   155
}
slouken@0
   156
slouken@0
   157
/* Start playback of a given WAV stream */
slouken@0
   158
extern void WAVStream_Start(WAVStream *wave)
slouken@0
   159
{
slouken@64
   160
	SDL_mutexP(music_lock);
slouken@0
   161
	clearerr(wave->wavefp);
slouken@0
   162
	fseek(wave->wavefp, wave->start, SEEK_SET);
slouken@64
   163
	theWave = wave;
slouken@64
   164
	SDL_mutexV(music_lock);
slouken@0
   165
}
slouken@0
   166
slouken@0
   167
/* Play some of a stream previously started with WAVStream_Start()
slouken@64
   168
   The music_lock is held while this function is called.
slouken@0
   169
 */
slouken@64
   170
extern void WAVStream_PlaySome(Uint8 *stream, int len)
slouken@0
   171
{
slouken@0
   172
	long pos;
slouken@0
   173
slouken@64
   174
	SDL_mutexP(music_lock);
slouken@64
   175
	if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
slouken@64
   176
		if ( theWave->cvt.needed ) {
slouken@0
   177
			int original_len;
slouken@0
   178
slouken@64
   179
			original_len=(int)((double)len/theWave->cvt.len_ratio);
slouken@64
   180
			if ( theWave->cvt.len != original_len ) {
slouken@0
   181
				int worksize;
slouken@64
   182
				if ( theWave->cvt.buf != NULL ) {
slouken@64
   183
					free(theWave->cvt.buf);
slouken@0
   184
				}
slouken@64
   185
				worksize = original_len*theWave->cvt.len_mult;
slouken@64
   186
				theWave->cvt.buf=(Uint8 *)malloc(worksize);
slouken@64
   187
				if ( theWave->cvt.buf == NULL ) {
slouken@64
   188
					SDL_mutexV(music_lock);
slouken@0
   189
					return;
slouken@0
   190
				}
slouken@64
   191
				theWave->cvt.len = original_len;
slouken@0
   192
			}
slouken@64
   193
			if ( (theWave->stop - pos) < original_len ) {
slouken@64
   194
				original_len = (theWave->stop - pos);
slouken@0
   195
			}
slouken@109
   196
			original_len = fread(theWave->cvt.buf,1,original_len,theWave->wavefp);
slouken@109
   197
			/* At least at the time of writing, SDL_ConvertAudio()
slouken@109
   198
			   does byte-order swapping starting at the end of the
slouken@109
   199
			   buffer. Thus, if we are reading 16-bit samples, we
slouken@109
   200
			   had better make damn sure that we get an even
slouken@109
   201
			   number of bytes, or we'll get garbage.
slouken@109
   202
			 */
slouken@109
   203
			if ( (theWave->cvt.src_format & 0x0010) && (original_len & 1) ) {
slouken@109
   204
				original_len--;
slouken@109
   205
			}
slouken@64
   206
			theWave->cvt.len = original_len;
slouken@64
   207
			SDL_ConvertAudio(&theWave->cvt);
slouken@64
   208
			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
slouken@0
   209
		} else {
slouken@64
   210
			if ( (theWave->stop - pos) < len ) {
slouken@64
   211
				len = (theWave->stop - pos);
slouken@0
   212
			}
slouken@64
   213
			fread(stream, len, 1, theWave->wavefp);
slouken@0
   214
		}
slouken@0
   215
	}
slouken@64
   216
	SDL_mutexV(music_lock);
slouken@0
   217
}
slouken@0
   218
slouken@0
   219
/* Stop playback of a stream previously started with WAVStream_Start() */
slouken@0
   220
extern void WAVStream_Stop(void)
slouken@0
   221
{
slouken@64
   222
	SDL_mutexP(music_lock);
slouken@64
   223
	theWave = NULL;
slouken@64
   224
	SDL_mutexV(music_lock);
slouken@0
   225
}
slouken@0
   226
slouken@0
   227
/* Close the given WAV stream */
slouken@0
   228
extern void WAVStream_FreeSong(WAVStream *wave)
slouken@0
   229
{
slouken@0
   230
	if ( wave ) {
slouken@64
   231
		/* Remove song from the currently playing list */
slouken@64
   232
		SDL_mutexP(music_lock);
slouken@64
   233
		if ( wave == theWave ) {
slouken@64
   234
			theWave = NULL;
slouken@64
   235
		}
slouken@64
   236
		SDL_mutexV(music_lock);
slouken@64
   237
slouken@0
   238
		/* Clean up associated data */
slouken@0
   239
		if ( wave->wavefp ) {
slouken@0
   240
			fclose(wave->wavefp);
slouken@0
   241
		}
slouken@0
   242
		if ( wave->cvt.buf ) {
slouken@0
   243
			free(wave->cvt.buf);
slouken@0
   244
		}
slouken@0
   245
		free(wave);
slouken@0
   246
	}
slouken@0
   247
}
slouken@0
   248
slouken@0
   249
/* Return non-zero if a stream is currently playing */
slouken@64
   250
extern int WAVStream_Active(void)
slouken@0
   251
{
slouken@0
   252
	int active;
slouken@0
   253
slouken@64
   254
	SDL_mutexP(music_lock);
slouken@0
   255
	active = 0;
slouken@64
   256
	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
slouken@0
   257
		active = 1;
slouken@0
   258
	}
slouken@64
   259
	SDL_mutexV(music_lock);
slouken@0
   260
slouken@0
   261
	return(active);
slouken@0
   262
}
slouken@0
   263
slouken@0
   264
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
slouken@0
   265
{
slouken@0
   266
	chunk->magic	= SDL_ReadLE32(src);
slouken@0
   267
	chunk->length	= SDL_ReadLE32(src);
slouken@0
   268
	if ( read_data ) {
slouken@0
   269
		chunk->data = (Uint8 *)malloc(chunk->length);
slouken@0
   270
		if ( chunk->data == NULL ) {
slouken@134
   271
			Mix_SetError("Out of memory");
slouken@0
   272
			return(-1);
slouken@0
   273
		}
slouken@0
   274
		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
slouken@134
   275
			Mix_SetError("Couldn't read chunk");
slouken@0
   276
			free(chunk->data);
slouken@0
   277
			return(-1);
slouken@0
   278
		}
slouken@0
   279
	} else {
slouken@0
   280
		SDL_RWseek(src, chunk->length, SEEK_CUR);
slouken@0
   281
	}
slouken@0
   282
	return(chunk->length);
slouken@0
   283
}
slouken@0
   284
slouken@0
   285
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   286
					long *start, long *stop)
slouken@0
   287
{
slouken@0
   288
	int was_error;
slouken@0
   289
	FILE *wavefp;
slouken@0
   290
	SDL_RWops *src;
slouken@0
   291
	Chunk chunk;
slouken@0
   292
	int lenread;
slouken@0
   293
slouken@0
   294
	/* WAV magic header */
slouken@0
   295
	Uint32 RIFFchunk;
slouken@0
   296
	Uint32 wavelen;
slouken@0
   297
	Uint32 WAVEmagic;
slouken@0
   298
slouken@0
   299
	/* FMT chunk */
slouken@0
   300
	WaveFMT *format = NULL;
slouken@0
   301
slouken@0
   302
	/* Make sure we are passed a valid data source */
slouken@0
   303
	was_error = 0;
slouken@0
   304
	wavefp = fopen(file, "rb");
slouken@0
   305
	src = NULL;
slouken@0
   306
	if ( wavefp ) {
slouken@0
   307
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   308
	}
slouken@0
   309
	if ( src == NULL ) {
slouken@0
   310
		was_error = 1;
slouken@0
   311
		goto done;
slouken@0
   312
	}
slouken@29
   313
slouken@0
   314
	/* Check the magic header */
slouken@0
   315
	RIFFchunk	= SDL_ReadLE32(src);
slouken@0
   316
	wavelen		= SDL_ReadLE32(src);
slouken@0
   317
	WAVEmagic	= SDL_ReadLE32(src);
slouken@0
   318
	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
slouken@134
   319
		Mix_SetError("Unrecognized file type (not WAVE)");
slouken@0
   320
		was_error = 1;
slouken@0
   321
		goto done;
slouken@0
   322
	}
slouken@0
   323
slouken@0
   324
	/* Read the audio data format chunk */
slouken@0
   325
	chunk.data = NULL;
slouken@0
   326
	do {
slouken@0
   327
		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
slouken@0
   328
		if ( chunk.data ) {
slouken@0
   329
			free(chunk.data);
slouken@0
   330
		}
slouken@0
   331
		lenread = ReadChunk(src, &chunk, 1);
slouken@0
   332
		if ( lenread < 0 ) {
slouken@0
   333
			was_error = 1;
slouken@0
   334
			goto done;
slouken@0
   335
		}
slouken@0
   336
	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
slouken@0
   337
slouken@0
   338
	/* Decode the audio data format */
slouken@0
   339
	format = (WaveFMT *)chunk.data;
slouken@0
   340
	if ( chunk.magic != FMT ) {
slouken@0
   341
		free(chunk.data);
slouken@134
   342
		Mix_SetError("Complex WAVE files not supported");
slouken@0
   343
		was_error = 1;
slouken@0
   344
		goto done;
slouken@0
   345
	}
slouken@0
   346
	switch (SDL_SwapLE16(format->encoding)) {
slouken@0
   347
		case PCM_CODE:
slouken@0
   348
			/* We can understand this */
slouken@0
   349
			break;
slouken@0
   350
		default:
slouken@134
   351
			Mix_SetError("Unknown WAVE data format");
slouken@0
   352
			was_error = 1;
slouken@0
   353
			goto done;
slouken@0
   354
	}
slouken@0
   355
	memset(spec, 0, (sizeof *spec));
slouken@0
   356
	spec->freq = SDL_SwapLE32(format->frequency);
slouken@0
   357
	switch (SDL_SwapLE16(format->bitspersample)) {
slouken@0
   358
		case 8:
slouken@0
   359
			spec->format = AUDIO_U8;
slouken@0
   360
			break;
slouken@0
   361
		case 16:
slouken@0
   362
			spec->format = AUDIO_S16;
slouken@0
   363
			break;
slouken@0
   364
		default:
slouken@134
   365
			Mix_SetError("Unknown PCM data format");
slouken@0
   366
			was_error = 1;
slouken@0
   367
			goto done;
slouken@0
   368
	}
slouken@42
   369
	spec->channels = (Uint8) SDL_SwapLE16(format->channels);
slouken@0
   370
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   371
slouken@0
   372
	/* Set the file offset to the DATA chunk data */
slouken@0
   373
	chunk.data = NULL;
slouken@0
   374
	do {
slouken@0
   375
		*start = SDL_RWtell(src) + 2*sizeof(Uint32);
slouken@0
   376
		lenread = ReadChunk(src, &chunk, 0);
slouken@0
   377
		if ( lenread < 0 ) {
slouken@0
   378
			was_error = 1;
slouken@0
   379
			goto done;
slouken@0
   380
		}
slouken@0
   381
	} while ( chunk.magic != DATA );
slouken@0
   382
	*stop = SDL_RWtell(src);
slouken@0
   383
slouken@0
   384
done:
slouken@0
   385
	if ( format != NULL ) {
slouken@0
   386
		free(format);
slouken@0
   387
	}
slouken@0
   388
	if ( src ) {
slouken@0
   389
		SDL_RWclose(src);
slouken@0
   390
	}
slouken@0
   391
	if ( was_error ) {
slouken@0
   392
		if ( wavefp ) {
slouken@0
   393
			fclose(wavefp);
slouken@0
   394
			wavefp = NULL;
slouken@0
   395
		}
slouken@0
   396
	}
slouken@0
   397
	return(wavefp);
slouken@0
   398
}
slouken@0
   399
slouken@95
   400
/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
slouken@95
   401
 * I don't pretend to fully understand it.
slouken@95
   402
 */
slouken@95
   403
slouken@95
   404
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
slouken@0
   405
{
slouken@95
   406
	/* Negative number? */
slouken@95
   407
	if (sanebuf[0] & 0x80)
slouken@95
   408
		return 0;
slouken@0
   409
slouken@95
   410
	/* Less than 1? */
slouken@95
   411
	if (sanebuf[0] <= 0x3F)
slouken@95
   412
		return 1;
slouken@95
   413
slouken@95
   414
	/* Way too big? */
slouken@95
   415
	if (sanebuf[0] > 0x40)
slouken@95
   416
		return 0x4000000;
slouken@95
   417
slouken@95
   418
	/* Still too big? */
slouken@95
   419
	if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
slouken@95
   420
		return 800000000;
slouken@95
   421
slouken@95
   422
	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
slouken@95
   423
		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
slouken@0
   424
}
slouken@0
   425
slouken@0
   426
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   427
					long *start, long *stop)
slouken@0
   428
{
slouken@0
   429
	int was_error;
slouken@95
   430
	int found_SSND;
slouken@95
   431
	int found_COMM;
slouken@0
   432
	FILE *wavefp;
slouken@0
   433
	SDL_RWops *src;
slouken@0
   434
slouken@95
   435
	Uint32 chunk_type;
slouken@95
   436
	Uint32 chunk_length;
slouken@95
   437
	long next_chunk;
slouken@95
   438
slouken@0
   439
	/* AIFF magic header */
slouken@0
   440
	Uint32 FORMchunk;
slouken@0
   441
	Uint32 AIFFmagic;
slouken@0
   442
	/* SSND chunk        */
slouken@0
   443
	Uint32 offset;
slouken@0
   444
	Uint32 blocksize;
slouken@0
   445
	/* COMM format chunk */
slouken@0
   446
	Uint16 channels;
slouken@0
   447
	Uint32 numsamples;
slouken@0
   448
	Uint16 samplesize;
slouken@95
   449
	Uint8 sane_freq[10];
slouken@0
   450
	Uint32 frequency;
slouken@0
   451
slouken@0
   452
slouken@0
   453
	/* Make sure we are passed a valid data source */
slouken@0
   454
	was_error = 0;
slouken@0
   455
	wavefp = fopen(file, "rb");
slouken@0
   456
	src = NULL;
slouken@0
   457
	if ( wavefp ) {
slouken@0
   458
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   459
	}
slouken@0
   460
	if ( src == NULL ) {
slouken@0
   461
		was_error = 1;
slouken@0
   462
		goto done;
slouken@0
   463
	}
slouken@29
   464
slouken@0
   465
	/* Check the magic header */
slouken@0
   466
	FORMchunk	= SDL_ReadLE32(src);
slouken@95
   467
	chunk_length	= SDL_ReadBE32(src);
slouken@0
   468
	AIFFmagic	= SDL_ReadLE32(src);
slouken@0
   469
	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
slouken@134
   470
		Mix_SetError("Unrecognized file type (not AIFF)");
slouken@0
   471
		was_error = 1;
slouken@0
   472
		goto done;
slouken@0
   473
	}
slouken@0
   474
slouken@95
   475
	/* From what I understand of the specification, chunks may appear in
slouken@95
   476
         * any order, and we should just ignore unknown ones.
slouken@109
   477
	 *
slouken@109
   478
	 * TODO: Better sanity-checking. E.g. what happens if the AIFF file
slouken@109
   479
	 *       contains compressed sound data?
slouken@95
   480
         */
slouken@95
   481
slouken@95
   482
	found_SSND = 0;
slouken@95
   483
	found_COMM = 0;
slouken@95
   484
slouken@95
   485
	do {
slouken@95
   486
	    chunk_type		= SDL_ReadLE32(src);
slouken@95
   487
	    chunk_length	= SDL_ReadBE32(src);
slouken@95
   488
	    next_chunk		= SDL_RWtell(src) + chunk_length;
slouken@95
   489
slouken@95
   490
	    /* Paranoia to avoid infinite loops */
slouken@95
   491
	    if (chunk_length == 0)
slouken@95
   492
		break;
slouken@95
   493
slouken@95
   494
            switch (chunk_type) {
slouken@95
   495
		case SSND:
slouken@95
   496
		    found_SSND		= 1;
slouken@95
   497
		    offset		= SDL_ReadBE32(src);
slouken@95
   498
		    blocksize		= SDL_ReadBE32(src);
slouken@95
   499
		    *start		= SDL_RWtell(src) + offset;
slouken@95
   500
		    break;
slouken@95
   501
slouken@95
   502
		case COMM:
slouken@95
   503
		    found_COMM		= 1;
slouken@95
   504
slouken@95
   505
		    /* Read the audio data format chunk */
slouken@95
   506
		    channels		= SDL_ReadBE16(src);
slouken@95
   507
		    numsamples		= SDL_ReadBE32(src);
slouken@95
   508
		    samplesize		= SDL_ReadBE16(src);
slouken@95
   509
		    SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
slouken@95
   510
		    frequency		= SANE_to_Uint32(sane_freq);
slouken@95
   511
		    break;
slouken@95
   512
slouken@95
   513
		default:
slouken@95
   514
		    break;
slouken@95
   515
	    }
slouken@95
   516
	} while ((!found_SSND || !found_COMM)
slouken@95
   517
		 && SDL_RWseek(src, next_chunk, SEEK_SET) != -1);
slouken@95
   518
slouken@95
   519
	if (!found_SSND) {
slouken@134
   520
	    Mix_SetError("Bad AIFF file (no SSND chunk)");
slouken@95
   521
	    was_error = 1;
slouken@95
   522
	    goto done;
slouken@0
   523
	}
slouken@95
   524
		    
slouken@95
   525
	if (!found_COMM) {
slouken@134
   526
	    Mix_SetError("Bad AIFF file (no COMM chunk)");
slouken@95
   527
	    was_error = 1;
slouken@95
   528
	    goto done;
slouken@95
   529
	}
slouken@0
   530
slouken@109
   531
	*stop = *start + channels * numsamples * (samplesize / 8);
slouken@0
   532
slouken@0
   533
	/* Decode the audio data format */
slouken@0
   534
	memset(spec, 0, (sizeof *spec));
slouken@0
   535
	spec->freq = frequency;
slouken@0
   536
	switch (samplesize) {
slouken@0
   537
		case 8:
slouken@109
   538
			spec->format = AUDIO_S8;
slouken@0
   539
			break;
slouken@0
   540
		case 16:
slouken@95
   541
			spec->format = AUDIO_S16MSB;
slouken@0
   542
			break;
slouken@0
   543
		default:
slouken@134
   544
			Mix_SetError("Unknown samplesize in data format");
slouken@0
   545
			was_error = 1;
slouken@0
   546
			goto done;
slouken@0
   547
	}
slouken@42
   548
	spec->channels = (Uint8) channels;
slouken@0
   549
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   550
slouken@0
   551
done:
slouken@0
   552
	if ( src ) {
slouken@0
   553
		SDL_RWclose(src);
slouken@0
   554
	}
slouken@0
   555
	if ( was_error ) {
slouken@0
   556
		if ( wavefp ) {
slouken@0
   557
			fclose(wavefp);
slouken@0
   558
			wavefp = NULL;
slouken@0
   559
		}
slouken@0
   560
	}
slouken@0
   561
	return(wavefp);
slouken@0
   562
}