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