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