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