wavestream.c
author Sam Lantinga <slouken@lokigames.com>
Tue, 21 Dec 1999 17:13:05 +0000
changeset 24 a25bb2d59ce8
parent 0 4ce2db4db959
child 29 44b09d6e1170
permissions -rw-r--r--
Updated for SDL 1.0.1
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@0
    38
/* Currently we only support a single stream at a time */
slouken@0
    39
static WAVStream *theWave = NULL;
slouken@0
    40
slouken@0
    41
/* This is initialized by the music mixer */
slouken@0
    42
SDL_mutex *music_lock = NULL;
slouken@0
    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@0
    58
	/* FIXME: clean up the mutex, or move it into music.c */
slouken@0
    59
	music_lock = SDL_CreateMutex();
slouken@0
    60
	if ( music_lock == NULL ) {
slouken@0
    61
		return(-1);
slouken@0
    62
	}
slouken@0
    63
	mixer = *mixerfmt;
slouken@0
    64
	return(0);
slouken@0
    65
}
slouken@0
    66
slouken@0
    67
/* Unimplemented */
slouken@0
    68
extern void WAVStream_SetVolume(int volume)
slouken@0
    69
{
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
/* Load a WAV stream from the given file */
slouken@0
    73
extern WAVStream *WAVStream_LoadSong(const char *file, const char *magic)
slouken@0
    74
{
slouken@0
    75
	WAVStream *wave;
slouken@0
    76
	SDL_AudioSpec wavespec;
slouken@0
    77
slouken@0
    78
	if ( ! mixer.format ) {
slouken@0
    79
		SDL_SetError("WAV music output not started");
slouken@0
    80
		return(NULL);
slouken@0
    81
	}
slouken@0
    82
	wave = (WAVStream *)malloc(sizeof *wave);
slouken@0
    83
	if ( wave ) {
slouken@0
    84
		memset(wave, 0, (sizeof *wave));
slouken@0
    85
		if ( strcmp(magic, "RIFF") == 0 ) {
slouken@0
    86
			wave->wavefp = LoadWAVStream(file, &wavespec,
slouken@0
    87
					&wave->start, &wave->stop);
slouken@0
    88
		} else
slouken@0
    89
		if ( strcmp(magic, "FORM") == 0 ) {
slouken@0
    90
			wave->wavefp = LoadAIFFStream(file, &wavespec,
slouken@0
    91
					&wave->start, &wave->stop);
slouken@0
    92
		}
slouken@0
    93
		if ( wave->wavefp == NULL ) {
slouken@0
    94
			free(wave);
slouken@0
    95
			return(NULL);
slouken@0
    96
		}
slouken@0
    97
		SDL_BuildAudioCVT(&wave->cvt,
slouken@0
    98
			wavespec.format, wavespec.channels, wavespec.freq,
slouken@0
    99
			mixer.format, mixer.channels, mixer.freq);
slouken@0
   100
	}
slouken@0
   101
	return(wave);
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
/* Start playback of a given WAV stream */
slouken@0
   105
extern void WAVStream_Start(WAVStream *wave)
slouken@0
   106
{
slouken@0
   107
	SDL_mutexP(music_lock);
slouken@0
   108
	clearerr(wave->wavefp);
slouken@0
   109
	fseek(wave->wavefp, wave->start, SEEK_SET);
slouken@0
   110
	theWave = wave;
slouken@0
   111
	SDL_mutexV(music_lock);
slouken@0
   112
}
slouken@0
   113
slouken@0
   114
/* Play some of a stream previously started with WAVStream_Start()
slouken@0
   115
   The music_lock is held while this function is called.
slouken@0
   116
 */
slouken@0
   117
extern void WAVStream_PlaySome(Uint8 *stream, int len)
slouken@0
   118
{
slouken@0
   119
	long pos;
slouken@0
   120
slouken@0
   121
	SDL_mutexP(music_lock);
slouken@0
   122
	if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
slouken@0
   123
		if ( theWave->cvt.needed ) {
slouken@0
   124
			int original_len;
slouken@0
   125
slouken@0
   126
			original_len=(int)((double)len/theWave->cvt.len_ratio);
slouken@0
   127
			if ( theWave->cvt.len != original_len ) {
slouken@0
   128
				int worksize;
slouken@0
   129
				if ( theWave->cvt.buf != NULL ) {
slouken@0
   130
					free(theWave->cvt.buf);
slouken@0
   131
				}
slouken@0
   132
				worksize = original_len*theWave->cvt.len_mult;
slouken@0
   133
				theWave->cvt.buf=(Uint8 *)malloc(worksize);
slouken@0
   134
				if ( theWave->cvt.buf == NULL ) {
slouken@0
   135
					SDL_mutexV(music_lock);
slouken@0
   136
					return;
slouken@0
   137
				}
slouken@0
   138
				theWave->cvt.len = original_len;
slouken@0
   139
			}
slouken@0
   140
			if ( (theWave->stop - pos) < original_len ) {
slouken@0
   141
				original_len = (theWave->stop - pos);
slouken@0
   142
			}
slouken@0
   143
			theWave->cvt.len = original_len;
slouken@0
   144
			fread(theWave->cvt.buf,original_len,1,theWave->wavefp);
slouken@0
   145
			SDL_ConvertAudio(&theWave->cvt);
slouken@0
   146
			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
slouken@0
   147
		} else {
slouken@0
   148
			if ( (theWave->stop - pos) < len ) {
slouken@0
   149
				len = (theWave->stop - pos);
slouken@0
   150
			}
slouken@0
   151
			fread(stream, len, 1, theWave->wavefp);
slouken@0
   152
		}
slouken@0
   153
	}
slouken@0
   154
	SDL_mutexV(music_lock);
slouken@0
   155
}
slouken@0
   156
slouken@0
   157
/* Stop playback of a stream previously started with WAVStream_Start() */
slouken@0
   158
extern void WAVStream_Stop(void)
slouken@0
   159
{
slouken@0
   160
	SDL_mutexP(music_lock);
slouken@0
   161
	theWave = NULL;
slouken@0
   162
	SDL_mutexV(music_lock);
slouken@0
   163
}
slouken@0
   164
slouken@0
   165
/* Close the given WAV stream */
slouken@0
   166
extern void WAVStream_FreeSong(WAVStream *wave)
slouken@0
   167
{
slouken@0
   168
	if ( wave ) {
slouken@0
   169
		/* Remove song from the currently playing list */
slouken@0
   170
		SDL_mutexP(music_lock);
slouken@0
   171
		if ( wave == theWave ) {
slouken@0
   172
			theWave = NULL;
slouken@0
   173
		}
slouken@0
   174
		SDL_mutexV(music_lock);
slouken@0
   175
slouken@0
   176
		/* Clean up associated data */
slouken@0
   177
		if ( wave->wavefp ) {
slouken@0
   178
			fclose(wave->wavefp);
slouken@0
   179
		}
slouken@0
   180
		if ( wave->cvt.buf ) {
slouken@0
   181
			free(wave->cvt.buf);
slouken@0
   182
		}
slouken@0
   183
		free(wave);
slouken@0
   184
	}
slouken@0
   185
}
slouken@0
   186
slouken@0
   187
/* Return non-zero if a stream is currently playing */
slouken@0
   188
extern int WAVStream_Active(void)
slouken@0
   189
{
slouken@0
   190
	int active;
slouken@0
   191
slouken@0
   192
	SDL_mutexP(music_lock);
slouken@0
   193
	active = 0;
slouken@0
   194
	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
slouken@0
   195
		active = 1;
slouken@0
   196
	}
slouken@0
   197
	SDL_mutexV(music_lock);
slouken@0
   198
slouken@0
   199
	return(active);
slouken@0
   200
}
slouken@0
   201
slouken@0
   202
static int ReadChunk(SDL_RWops *src, Chunk *chunk, int read_data)
slouken@0
   203
{
slouken@0
   204
	chunk->magic	= SDL_ReadLE32(src);
slouken@0
   205
	chunk->length	= SDL_ReadLE32(src);
slouken@0
   206
	if ( read_data ) {
slouken@0
   207
		chunk->data = (Uint8 *)malloc(chunk->length);
slouken@0
   208
		if ( chunk->data == NULL ) {
slouken@0
   209
			SDL_SetError("Out of memory");
slouken@0
   210
			return(-1);
slouken@0
   211
		}
slouken@0
   212
		if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
slouken@0
   213
			SDL_SetError("Couldn't read chunk");
slouken@0
   214
			free(chunk->data);
slouken@0
   215
			return(-1);
slouken@0
   216
		}
slouken@0
   217
	} else {
slouken@0
   218
		SDL_RWseek(src, chunk->length, SEEK_CUR);
slouken@0
   219
	}
slouken@0
   220
	return(chunk->length);
slouken@0
   221
}
slouken@0
   222
slouken@0
   223
static FILE *LoadWAVStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   224
					long *start, long *stop)
slouken@0
   225
{
slouken@0
   226
	int was_error;
slouken@0
   227
	FILE *wavefp;
slouken@0
   228
	SDL_RWops *src;
slouken@0
   229
	Chunk chunk;
slouken@0
   230
	int lenread;
slouken@0
   231
slouken@0
   232
	/* WAV magic header */
slouken@0
   233
	Uint32 RIFFchunk;
slouken@0
   234
	Uint32 wavelen;
slouken@0
   235
	Uint32 WAVEmagic;
slouken@0
   236
slouken@0
   237
	/* FMT chunk */
slouken@0
   238
	WaveFMT *format = NULL;
slouken@0
   239
slouken@0
   240
	/* Make sure we are passed a valid data source */
slouken@0
   241
	was_error = 0;
slouken@0
   242
	wavefp = fopen(file, "rb");
slouken@0
   243
	src = NULL;
slouken@0
   244
	if ( wavefp ) {
slouken@0
   245
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   246
	}
slouken@0
   247
	if ( src == NULL ) {
slouken@0
   248
		was_error = 1;
slouken@0
   249
		goto done;
slouken@0
   250
	}
slouken@0
   251
		
slouken@0
   252
	/* Check the magic header */
slouken@0
   253
	RIFFchunk	= SDL_ReadLE32(src);
slouken@0
   254
	wavelen		= SDL_ReadLE32(src);
slouken@0
   255
	WAVEmagic	= SDL_ReadLE32(src);
slouken@0
   256
	if ( (RIFFchunk != RIFF) || (WAVEmagic != WAVE) ) {
slouken@0
   257
		SDL_SetError("Unrecognized file type (not WAVE)");
slouken@0
   258
		was_error = 1;
slouken@0
   259
		goto done;
slouken@0
   260
	}
slouken@0
   261
slouken@0
   262
	/* Read the audio data format chunk */
slouken@0
   263
	chunk.data = NULL;
slouken@0
   264
	do {
slouken@0
   265
		/* FIXME! Add this logic to SDL_LoadWAV_RW() */
slouken@0
   266
		if ( chunk.data ) {
slouken@0
   267
			free(chunk.data);
slouken@0
   268
		}
slouken@0
   269
		lenread = ReadChunk(src, &chunk, 1);
slouken@0
   270
		if ( lenread < 0 ) {
slouken@0
   271
			was_error = 1;
slouken@0
   272
			goto done;
slouken@0
   273
		}
slouken@0
   274
	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
slouken@0
   275
slouken@0
   276
	/* Decode the audio data format */
slouken@0
   277
	format = (WaveFMT *)chunk.data;
slouken@0
   278
	if ( chunk.magic != FMT ) {
slouken@0
   279
		free(chunk.data);
slouken@0
   280
		SDL_SetError("Complex WAVE files not supported");
slouken@0
   281
		was_error = 1;
slouken@0
   282
		goto done;
slouken@0
   283
	}
slouken@0
   284
	switch (SDL_SwapLE16(format->encoding)) {
slouken@0
   285
		case PCM_CODE:
slouken@0
   286
			/* We can understand this */
slouken@0
   287
			break;
slouken@0
   288
		default:
slouken@0
   289
			SDL_SetError("Unknown WAVE data format");
slouken@0
   290
			was_error = 1;
slouken@0
   291
			goto done;
slouken@0
   292
	}
slouken@0
   293
	memset(spec, 0, (sizeof *spec));
slouken@0
   294
	spec->freq = SDL_SwapLE32(format->frequency);
slouken@0
   295
	switch (SDL_SwapLE16(format->bitspersample)) {
slouken@0
   296
		case 8:
slouken@0
   297
			spec->format = AUDIO_U8;
slouken@0
   298
			break;
slouken@0
   299
		case 16:
slouken@0
   300
			spec->format = AUDIO_S16;
slouken@0
   301
			break;
slouken@0
   302
		default:
slouken@0
   303
			SDL_SetError("Unknown PCM data format");
slouken@0
   304
			was_error = 1;
slouken@0
   305
			goto done;
slouken@0
   306
	}
slouken@0
   307
	spec->channels = SDL_SwapLE16(format->channels);
slouken@0
   308
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   309
slouken@0
   310
	/* Set the file offset to the DATA chunk data */
slouken@0
   311
	chunk.data = NULL;
slouken@0
   312
	do {
slouken@0
   313
		*start = SDL_RWtell(src) + 2*sizeof(Uint32);
slouken@0
   314
		lenread = ReadChunk(src, &chunk, 0);
slouken@0
   315
		if ( lenread < 0 ) {
slouken@0
   316
			was_error = 1;
slouken@0
   317
			goto done;
slouken@0
   318
		}
slouken@0
   319
	} while ( chunk.magic != DATA );
slouken@0
   320
	*stop = SDL_RWtell(src);
slouken@0
   321
slouken@0
   322
done:
slouken@0
   323
	if ( format != NULL ) {
slouken@0
   324
		free(format);
slouken@0
   325
	}
slouken@0
   326
	if ( src ) {
slouken@0
   327
		SDL_RWclose(src);
slouken@0
   328
	}
slouken@0
   329
	if ( was_error ) {
slouken@0
   330
		if ( wavefp ) {
slouken@0
   331
			fclose(wavefp);
slouken@0
   332
			wavefp = NULL;
slouken@0
   333
		}
slouken@0
   334
	}
slouken@0
   335
	return(wavefp);
slouken@0
   336
}
slouken@0
   337
slouken@0
   338
static double SANE_to_double(Uint32 l1, Uint32 l2, Uint16 s1)
slouken@0
   339
{
slouken@0
   340
	double d;
slouken@0
   341
	struct almost_double {
slouken@0
   342
		Uint32 hi, lo;
slouken@0
   343
	} *dp = (struct almost_double *)&d;
slouken@0
   344
slouken@0
   345
	dp->hi = ((l1 << 4) & 0x3ff00000) | (l1 & 0xc0000000);
slouken@0
   346
	dp->hi |= (l1 << 5) & 0xffff0;
slouken@0
   347
	dp->hi |= (l2 >> 27) & 0x1f;
slouken@0
   348
	dp->lo = (l2 << 5) & 0xffffffe0;
slouken@0
   349
	dp->lo |= ((s1 >> 11) & 0x1f);
slouken@0
   350
	return(d);
slouken@0
   351
}
slouken@0
   352
slouken@0
   353
static FILE *LoadAIFFStream (const char *file, SDL_AudioSpec *spec,
slouken@0
   354
					long *start, long *stop)
slouken@0
   355
{
slouken@0
   356
	int was_error;
slouken@0
   357
	FILE *wavefp;
slouken@0
   358
	SDL_RWops *src;
slouken@0
   359
	Chunk chunk;
slouken@0
   360
	int lenread;
slouken@0
   361
slouken@0
   362
	/* AIFF magic header */
slouken@0
   363
	Uint32 FORMchunk;
slouken@0
   364
	Uint32 chunklen;
slouken@0
   365
	Uint32 AIFFmagic;
slouken@0
   366
	/* SSND chunk        */
slouken@0
   367
	Uint32 SSNDchunk;
slouken@0
   368
	Uint32 ssndlen;
slouken@0
   369
	Uint32 offset;
slouken@0
   370
	Uint32 blocksize;
slouken@0
   371
	/* COMM format chunk */
slouken@0
   372
	Uint32 COMMchunk;
slouken@0
   373
	Uint32 commlen;
slouken@0
   374
	Uint16 channels;
slouken@0
   375
	Uint32 numsamples;
slouken@0
   376
	Uint16 samplesize;
slouken@0
   377
	struct { /* plus a SANE format double precision number */
slouken@0
   378
		Uint32 l1;
slouken@0
   379
		Uint32 l2;
slouken@0
   380
		Uint16 s1;
slouken@0
   381
	} sane_freq;
slouken@0
   382
	
slouken@0
   383
	Uint32 frequency;
slouken@0
   384
slouken@0
   385
slouken@0
   386
	/* Make sure we are passed a valid data source */
slouken@0
   387
	was_error = 0;
slouken@0
   388
	wavefp = fopen(file, "rb");
slouken@0
   389
	src = NULL;
slouken@0
   390
	if ( wavefp ) {
slouken@0
   391
		src = SDL_RWFromFP(wavefp, 0);
slouken@0
   392
	}
slouken@0
   393
	if ( src == NULL ) {
slouken@0
   394
		was_error = 1;
slouken@0
   395
		goto done;
slouken@0
   396
	}
slouken@0
   397
		
slouken@0
   398
	/* Check the magic header */
slouken@0
   399
	FORMchunk	= SDL_ReadLE32(src);
slouken@0
   400
	chunklen	= SDL_ReadLE32(src);
slouken@0
   401
	AIFFmagic	= SDL_ReadLE32(src);
slouken@0
   402
	if ( (FORMchunk != FORM) || (AIFFmagic != AIFF) ) {
slouken@0
   403
		SDL_SetError("Unrecognized file type (not AIFF)");
slouken@0
   404
		was_error = 1;
slouken@0
   405
		goto done;
slouken@0
   406
	}
slouken@0
   407
slouken@0
   408
	/* Read the SSND data chunk */
slouken@0
   409
	SSNDchunk	= SDL_ReadLE32(src);
slouken@0
   410
	if ( SSNDchunk != SSND ) {
slouken@0
   411
		SDL_SetError("Unrecognized AIFF chunk (not SSND)");
slouken@0
   412
		was_error = 1;
slouken@0
   413
		goto done;
slouken@0
   414
	}
slouken@0
   415
	ssndlen		= SDL_ReadLE32(src);
slouken@0
   416
	offset		= SDL_ReadLE32(src);
slouken@0
   417
	blocksize	= SDL_ReadLE32(src);
slouken@0
   418
slouken@0
   419
	/* Fill in start and stop pointers, then seek to format chunk */
slouken@0
   420
	ssndlen -= (2*sizeof(Uint32));
slouken@0
   421
	*start = SDL_RWtell(src) + offset;
slouken@0
   422
	*stop = SDL_RWtell(src) + ssndlen;
slouken@0
   423
	SDL_RWseek(src, *stop, SEEK_SET);
slouken@0
   424
slouken@0
   425
	/* Read the audio data format chunk */
slouken@0
   426
	COMMchunk	= SDL_ReadLE32(src);
slouken@0
   427
	if ( COMMchunk != COMM ) {
slouken@0
   428
		SDL_SetError("Unrecognized AIFF chunk (not COMM)");
slouken@0
   429
		was_error = 1;
slouken@0
   430
		goto done;
slouken@0
   431
	}
slouken@0
   432
	commlen		= SDL_ReadLE32(src);
slouken@0
   433
	channels	= SDL_ReadLE16(src);
slouken@0
   434
	numsamples	= SDL_ReadLE32(src);
slouken@0
   435
	samplesize	= SDL_ReadLE16(src);
slouken@0
   436
	sane_freq.l1	= SDL_ReadLE32(src);
slouken@0
   437
	sane_freq.l2	= SDL_ReadLE32(src);
slouken@0
   438
	sane_freq.s1	= SDL_ReadLE16(src);
slouken@0
   439
	frequency	= (Uint32)SANE_to_double(sane_freq.l1, sane_freq.l2,
slouken@0
   440
								sane_freq.s1);
slouken@0
   441
slouken@0
   442
	/* Decode the audio data format */
slouken@0
   443
	memset(spec, 0, (sizeof *spec));
slouken@0
   444
	spec->freq = frequency;
slouken@0
   445
	switch (samplesize) {
slouken@0
   446
		case 8:
slouken@0
   447
			spec->format = AUDIO_U8;
slouken@0
   448
			break;
slouken@0
   449
		case 16:
slouken@0
   450
			spec->format = AUDIO_S16;
slouken@0
   451
			break;
slouken@0
   452
		default:
slouken@0
   453
			SDL_SetError("Unknown samplesize in data format");
slouken@0
   454
			was_error = 1;
slouken@0
   455
			goto done;
slouken@0
   456
	}
slouken@0
   457
	spec->channels = channels;
slouken@0
   458
	spec->samples = 4096;		/* Good default buffer size */
slouken@0
   459
slouken@0
   460
done:
slouken@0
   461
	if ( src ) {
slouken@0
   462
		SDL_RWclose(src);
slouken@0
   463
	}
slouken@0
   464
	if ( was_error ) {
slouken@0
   465
		if ( wavefp ) {
slouken@0
   466
			fclose(wavefp);
slouken@0
   467
			wavefp = NULL;
slouken@0
   468
		}
slouken@0
   469
	}
slouken@0
   470
	return(wavefp);
slouken@0
   471
}