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