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