wavestream.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2001 21:19:17 +0000
changeset 97 2bf733171d56
parent 95 871a7c1b93fb
child 109 6cbf105616e3
permissions -rw-r--r--
Sam Lantinga - Sun Aug 19 02:20:55 PDT 2001
* Added Project Builder projects for building MacOS X framework
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@64
   145
			theWave->cvt.len = original_len;
slouken@64
   146
			fread(theWave->cvt.buf,original_len,1,theWave->wavefp);
slouken@64
   147
			SDL_ConvertAudio(&theWave->cvt);
slouken@64
   148
			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
slouken@0
   149
		} else {
slouken@64
   150
			if ( (theWave->stop - pos) < len ) {
slouken@64
   151
				len = (theWave->stop - pos);
slouken@0
   152
			}
slouken@64
   153
			fread(stream, len, 1, theWave->wavefp);
slouken@0
   154
		}
slouken@0
   155
	}
slouken@64
   156
	SDL_mutexV(music_lock);
slouken@0
   157
}
slouken@0
   158
slouken@0
   159
/* Stop playback of a stream previously started with WAVStream_Start() */
slouken@0
   160
extern void WAVStream_Stop(void)
slouken@0
   161
{
slouken@64
   162
	SDL_mutexP(music_lock);
slouken@64
   163
	theWave = NULL;
slouken@64
   164
	SDL_mutexV(music_lock);
slouken@0
   165
}
slouken@0
   166
slouken@0
   167
/* Close the given WAV stream */
slouken@0
   168
extern void WAVStream_FreeSong(WAVStream *wave)
slouken@0
   169
{
slouken@0
   170
	if ( wave ) {
slouken@64
   171
		/* Remove song from the currently playing list */
slouken@64
   172
		SDL_mutexP(music_lock);
slouken@64
   173
		if ( wave == theWave ) {
slouken@64
   174
			theWave = NULL;
slouken@64
   175
		}
slouken@64
   176
		SDL_mutexV(music_lock);
slouken@64
   177
slouken@0
   178
		/* Clean up associated data */
slouken@0
   179
		if ( wave->wavefp ) {
slouken@0
   180
			fclose(wave->wavefp);
slouken@0
   181
		}
slouken@0
   182
		if ( wave->cvt.buf ) {
slouken@0
   183
			free(wave->cvt.buf);
slouken@0
   184
		}
slouken@0
   185
		free(wave);
slouken@0
   186
	}
slouken@0
   187
}
slouken@0
   188
slouken@0
   189
/* Return non-zero if a stream is currently playing */
slouken@64
   190
extern int WAVStream_Active(void)
slouken@0
   191
{
slouken@0
   192
	int active;
slouken@0
   193
slouken@64
   194
	SDL_mutexP(music_lock);
slouken@0
   195
	active = 0;
slouken@64
   196
	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
slouken@0
   197
		active = 1;
slouken@0
   198
	}
slouken@64
   199
	SDL_mutexV(music_lock);
slouken@0
   200
slouken@0
   201
	return(active);
slouken@0
   202
}
slouken@0
   203
slouken@0
   204
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
slouken@0
   205
{
slouken@0
   206
	chunk->magic	= SDL_ReadLE32(src);
slouken@0
   207
	chunk->length	= SDL_ReadLE32(src);
slouken@0
   208
	if ( read_data ) {
slouken@0
   209
		chunk->data = (Uint8 *)malloc(chunk->length);
slouken@0
   210
		if ( chunk->data == NULL ) {
slouken@0
   211
			SDL_SetError("Out of memory");
slouken@0
   212
			return(-1);
slouken@0
   213
		}
slouken@0
   214
		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
slouken@0
   215
			SDL_SetError("Couldn't read chunk");
slouken@0
   216
			free(chunk->data);
slouken@0
   217
			return(-1);
slouken@0
   218
		}
slouken@0
   219
	} else {
slouken@0
   220
		SDL_RWseek(src, chunk->length, SEEK_CUR);
slouken@0
   221
	}
slouken@0
   222
	return(chunk->length);
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   226
					long *start, long *stop)
slouken@0
   227
{
slouken@0
   228
	int was_error;
slouken@0
   229
	FILE *wavefp;
slouken@0
   230
	SDL_RWops *src;
slouken@0
   231
	Chunk chunk;
slouken@0
   232
	int lenread;
slouken@0
   233
slouken@0
   234
	/* WAV magic header */
slouken@0
   235
	Uint32 RIFFchunk;
slouken@0
   236
	Uint32 wavelen;
slouken@0
   237
	Uint32 WAVEmagic;
slouken@0
   238
slouken@0
   239
	/* FMT chunk */
slouken@0
   240
	WaveFMT *format = NULL;
slouken@0
   241
slouken@0
   242
	/* Make sure we are passed a valid data source */
slouken@0
   243
	was_error = 0;
slouken@0
   244
	wavefp = fopen(file, "rb");
slouken@0
   245
	src = NULL;
slouken@0
   246
	if ( wavefp ) {
slouken@0
   247
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   248
	}
slouken@0
   249
	if ( src == NULL ) {
slouken@0
   250
		was_error = 1;
slouken@0
   251
		goto done;
slouken@0
   252
	}
slouken@29
   253
slouken@0
   254
	/* Check the magic header */
slouken@0
   255
	RIFFchunk	= SDL_ReadLE32(src);
slouken@0
   256
	wavelen		= SDL_ReadLE32(src);
slouken@0
   257
	WAVEmagic	= SDL_ReadLE32(src);
slouken@0
   258
	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
slouken@0
   259
		SDL_SetError("Unrecognized file type (not WAVE)");
slouken@0
   260
		was_error = 1;
slouken@0
   261
		goto done;
slouken@0
   262
	}
slouken@0
   263
slouken@0
   264
	/* Read the audio data format chunk */
slouken@0
   265
	chunk.data = NULL;
slouken@0
   266
	do {
slouken@0
   267
		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
slouken@0
   268
		if ( chunk.data ) {
slouken@0
   269
			free(chunk.data);
slouken@0
   270
		}
slouken@0
   271
		lenread = ReadChunk(src, &chunk, 1);
slouken@0
   272
		if ( lenread < 0 ) {
slouken@0
   273
			was_error = 1;
slouken@0
   274
			goto done;
slouken@0
   275
		}
slouken@0
   276
	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
slouken@0
   277
slouken@0
   278
	/* Decode the audio data format */
slouken@0
   279
	format = (WaveFMT *)chunk.data;
slouken@0
   280
	if ( chunk.magic != FMT ) {
slouken@0
   281
		free(chunk.data);
slouken@0
   282
		SDL_SetError("Complex WAVE files not supported");
slouken@0
   283
		was_error = 1;
slouken@0
   284
		goto done;
slouken@0
   285
	}
slouken@0
   286
	switch (SDL_SwapLE16(format->encoding)) {
slouken@0
   287
		case PCM_CODE:
slouken@0
   288
			/* We can understand this */
slouken@0
   289
			break;
slouken@0
   290
		default:
slouken@0
   291
			SDL_SetError("Unknown WAVE data format");
slouken@0
   292
			was_error = 1;
slouken@0
   293
			goto done;
slouken@0
   294
	}
slouken@0
   295
	memset(spec, 0, (sizeof *spec));
slouken@0
   296
	spec->freq = SDL_SwapLE32(format->frequency);
slouken@0
   297
	switch (SDL_SwapLE16(format->bitspersample)) {
slouken@0
   298
		case 8:
slouken@0
   299
			spec->format = AUDIO_U8;
slouken@0
   300
			break;
slouken@0
   301
		case 16:
slouken@0
   302
			spec->format = AUDIO_S16;
slouken@0
   303
			break;
slouken@0
   304
		default:
slouken@0
   305
			SDL_SetError("Unknown PCM data format");
slouken@0
   306
			was_error = 1;
slouken@0
   307
			goto done;
slouken@0
   308
	}
slouken@42
   309
	spec->channels = (Uint8) SDL_SwapLE16(format->channels);
slouken@0
   310
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   311
slouken@0
   312
	/* Set the file offset to the DATA chunk data */
slouken@0
   313
	chunk.data = NULL;
slouken@0
   314
	do {
slouken@0
   315
		*start = SDL_RWtell(src) + 2*sizeof(Uint32);
slouken@0
   316
		lenread = ReadChunk(src, &chunk, 0);
slouken@0
   317
		if ( lenread < 0 ) {
slouken@0
   318
			was_error = 1;
slouken@0
   319
			goto done;
slouken@0
   320
		}
slouken@0
   321
	} while ( chunk.magic != DATA );
slouken@0
   322
	*stop = SDL_RWtell(src);
slouken@0
   323
slouken@0
   324
done:
slouken@0
   325
	if ( format != NULL ) {
slouken@0
   326
		free(format);
slouken@0
   327
	}
slouken@0
   328
	if ( src ) {
slouken@0
   329
		SDL_RWclose(src);
slouken@0
   330
	}
slouken@0
   331
	if ( was_error ) {
slouken@0
   332
		if ( wavefp ) {
slouken@0
   333
			fclose(wavefp);
slouken@0
   334
			wavefp = NULL;
slouken@0
   335
		}
slouken@0
   336
	}
slouken@0
   337
	return(wavefp);
slouken@0
   338
}
slouken@0
   339
slouken@95
   340
/* I couldn't get SANE_to_double() to work, so I stole this from libsndfile.
slouken@95
   341
 * I don't pretend to fully understand it.
slouken@95
   342
 */
slouken@95
   343
slouken@95
   344
static Uint32 SANE_to_Uint32 (Uint8 *sanebuf)
slouken@0
   345
{
slouken@95
   346
	/* Negative number? */
slouken@95
   347
	if (sanebuf[0] & 0x80)
slouken@95
   348
		return 0;
slouken@0
   349
slouken@95
   350
	/* Less than 1? */
slouken@95
   351
	if (sanebuf[0] <= 0x3F)
slouken@95
   352
		return 1;
slouken@95
   353
slouken@95
   354
	/* Way too big? */
slouken@95
   355
	if (sanebuf[0] > 0x40)
slouken@95
   356
		return 0x4000000;
slouken@95
   357
slouken@95
   358
	/* Still too big? */
slouken@95
   359
	if (sanebuf[0] == 0x40 && sanebuf[1] > 0x1C)
slouken@95
   360
		return 800000000;
slouken@95
   361
slouken@95
   362
	return ((sanebuf[2] << 23) | (sanebuf[3] << 15) | (sanebuf[4] << 7)
slouken@95
   363
		| (sanebuf[5] >> 1)) >> (29 - sanebuf[1]);
slouken@0
   364
}
slouken@0
   365
slouken@0
   366
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   367
					long *start, long *stop)
slouken@0
   368
{
slouken@0
   369
	int was_error;
slouken@95
   370
	int found_SSND;
slouken@95
   371
	int found_COMM;
slouken@0
   372
	FILE *wavefp;
slouken@0
   373
	SDL_RWops *src;
slouken@0
   374
slouken@95
   375
	Uint32 chunk_type;
slouken@95
   376
	Uint32 chunk_length;
slouken@95
   377
	long next_chunk;
slouken@95
   378
slouken@0
   379
	/* AIFF magic header */
slouken@0
   380
	Uint32 FORMchunk;
slouken@0
   381
	Uint32 AIFFmagic;
slouken@0
   382
	/* SSND chunk        */
slouken@0
   383
	Uint32 offset;
slouken@0
   384
	Uint32 blocksize;
slouken@0
   385
	/* COMM format chunk */
slouken@0
   386
	Uint16 channels;
slouken@0
   387
	Uint32 numsamples;
slouken@0
   388
	Uint16 samplesize;
slouken@95
   389
	Uint8 sane_freq[10];
slouken@0
   390
	Uint32 frequency;
slouken@0
   391
slouken@0
   392
slouken@0
   393
	/* Make sure we are passed a valid data source */
slouken@0
   394
	was_error = 0;
slouken@0
   395
	wavefp = fopen(file, "rb");
slouken@0
   396
	src = NULL;
slouken@0
   397
	if ( wavefp ) {
slouken@0
   398
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   399
	}
slouken@0
   400
	if ( src == NULL ) {
slouken@0
   401
		was_error = 1;
slouken@0
   402
		goto done;
slouken@0
   403
	}
slouken@29
   404
slouken@0
   405
	/* Check the magic header */
slouken@0
   406
	FORMchunk	= SDL_ReadLE32(src);
slouken@95
   407
	chunk_length	= SDL_ReadBE32(src);
slouken@0
   408
	AIFFmagic	= SDL_ReadLE32(src);
slouken@0
   409
	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
slouken@0
   410
		SDL_SetError("Unrecognized file type (not AIFF)");
slouken@0
   411
		was_error = 1;
slouken@0
   412
		goto done;
slouken@0
   413
	}
slouken@0
   414
slouken@95
   415
	/* From what I understand of the specification, chunks may appear in
slouken@95
   416
         * any order, and we should just ignore unknown ones.
slouken@95
   417
         */
slouken@95
   418
slouken@95
   419
	found_SSND = 0;
slouken@95
   420
	found_COMM = 0;
slouken@95
   421
slouken@95
   422
	do {
slouken@95
   423
	    chunk_type		= SDL_ReadLE32(src);
slouken@95
   424
	    chunk_length	= SDL_ReadBE32(src);
slouken@95
   425
	    next_chunk		= SDL_RWtell(src) + chunk_length;
slouken@95
   426
slouken@95
   427
	    /* Paranoia to avoid infinite loops */
slouken@95
   428
	    if (chunk_length == 0)
slouken@95
   429
		break;
slouken@95
   430
slouken@95
   431
            switch (chunk_type) {
slouken@95
   432
		case SSND:
slouken@95
   433
		    found_SSND		= 1;
slouken@95
   434
		    offset		= SDL_ReadBE32(src);
slouken@95
   435
		    blocksize		= SDL_ReadBE32(src);
slouken@95
   436
		    *start		= SDL_RWtell(src) + offset;
slouken@95
   437
		    break;
slouken@95
   438
slouken@95
   439
		case COMM:
slouken@95
   440
		    found_COMM		= 1;
slouken@95
   441
slouken@95
   442
		    /* Read the audio data format chunk */
slouken@95
   443
		    channels		= SDL_ReadBE16(src);
slouken@95
   444
		    numsamples		= SDL_ReadBE32(src);
slouken@95
   445
		    samplesize		= SDL_ReadBE16(src);
slouken@95
   446
		    SDL_RWread(src, sane_freq, sizeof(sane_freq), 1);
slouken@95
   447
		    frequency		= SANE_to_Uint32(sane_freq);
slouken@95
   448
		    break;
slouken@95
   449
slouken@95
   450
		default:
slouken@95
   451
		    break;
slouken@95
   452
	    }
slouken@95
   453
	} while ((!found_SSND || !found_COMM)
slouken@95
   454
		 && SDL_RWseek(src, next_chunk, SEEK_SET) != -1);
slouken@95
   455
slouken@95
   456
	if (!found_SSND) {
slouken@95
   457
	    SDL_SetError("Bad AIFF file (no SSND chunk)");
slouken@95
   458
	    was_error = 1;
slouken@95
   459
	    goto done;
slouken@0
   460
	}
slouken@95
   461
		    
slouken@95
   462
	if (!found_COMM) {
slouken@95
   463
	    SDL_SetError("Bad AIFF file (no COMM chunk)");
slouken@95
   464
	    was_error = 1;
slouken@95
   465
	    goto done;
slouken@95
   466
	}
slouken@0
   467
slouken@95
   468
	*stop = *start + channels * numsamples * (samplesize / 8) - 1;
slouken@0
   469
slouken@0
   470
	/* Decode the audio data format */
slouken@0
   471
	memset(spec, 0, (sizeof *spec));
slouken@0
   472
	spec->freq = frequency;
slouken@0
   473
	switch (samplesize) {
slouken@0
   474
		case 8:
slouken@0
   475
			spec->format = AUDIO_U8;
slouken@0
   476
			break;
slouken@0
   477
		case 16:
slouken@95
   478
			spec->format = AUDIO_S16MSB;
slouken@0
   479
			break;
slouken@0
   480
		default:
slouken@0
   481
			SDL_SetError("Unknown samplesize in data format");
slouken@0
   482
			was_error = 1;
slouken@0
   483
			goto done;
slouken@0
   484
	}
slouken@42
   485
	spec->channels = (Uint8) channels;
slouken@0
   486
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   487
slouken@0
   488
done:
slouken@0
   489
	if ( src ) {
slouken@0
   490
		SDL_RWclose(src);
slouken@0
   491
	}
slouken@0
   492
	if ( was_error ) {
slouken@0
   493
		if ( wavefp ) {
slouken@0
   494
			fclose(wavefp);
slouken@0
   495
			wavefp = NULL;
slouken@0
   496
		}
slouken@0
   497
	}
slouken@0
   498
	return(wavefp);
slouken@0
   499
}