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