wavestream.c
changeset 64 00b28153c609
parent 63 92b13db7d0a5
child 95 871a7c1b93fb
     1.1 --- a/wavestream.c	Mon Jul 03 00:25:20 2000 +0000
     1.2 +++ b/wavestream.c	Mon Jul 03 02:27:34 2000 +0000
     1.3 @@ -35,6 +35,12 @@
     1.4  #include "wave.h"
     1.5  #include "wavestream.h"
     1.6  
     1.7 +/* Currently we only support a single stream at a time */
     1.8 +static WAVStream *theWave = NULL;
     1.9 +
    1.10 +/* This is initialized by the music mixer */
    1.11 +static SDL_mutex *music_lock = NULL;
    1.12 +
    1.13  /* This is the format of the audio mixer data */
    1.14  static SDL_AudioSpec mixer;
    1.15  
    1.16 @@ -49,6 +55,13 @@
    1.17   */
    1.18  int WAVStream_Init(SDL_AudioSpec *mixerfmt)
    1.19  {
    1.20 +	/* FIXME: clean up the mutex, or move it into music.c */
    1.21 +	music_lock = SDL_CreateMutex();
    1.22 +#ifndef macintosh /* Hmm.. */
    1.23 +	if ( music_lock == NULL ) {
    1.24 +		return(-1);
    1.25 +	}
    1.26 +#endif
    1.27  	mixer = *mixerfmt;
    1.28  	return(0);
    1.29  }
    1.30 @@ -93,58 +106,75 @@
    1.31  /* Start playback of a given WAV stream */
    1.32  extern void WAVStream_Start(WAVStream *wave)
    1.33  {
    1.34 +	SDL_mutexP(music_lock);
    1.35  	clearerr(wave->wavefp);
    1.36  	fseek(wave->wavefp, wave->start, SEEK_SET);
    1.37 +	theWave = wave;
    1.38 +	SDL_mutexV(music_lock);
    1.39  }
    1.40  
    1.41  /* Play some of a stream previously started with WAVStream_Start()
    1.42 +   The music_lock is held while this function is called.
    1.43   */
    1.44 -extern void WAVStream_PlaySome(WAVStream *wave, Uint8 *stream, int len)
    1.45 +extern void WAVStream_PlaySome(Uint8 *stream, int len)
    1.46  {
    1.47  	long pos;
    1.48  
    1.49 -	if ( (pos=ftell(wave->wavefp)) < wave->stop ) {
    1.50 -		if ( wave->cvt.needed ) {
    1.51 +	SDL_mutexP(music_lock);
    1.52 +	if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
    1.53 +		if ( theWave->cvt.needed ) {
    1.54  			int original_len;
    1.55  
    1.56 -			original_len=(int)((double)len/wave->cvt.len_ratio);
    1.57 -			if ( wave->cvt.len != original_len ) {
    1.58 +			original_len=(int)((double)len/theWave->cvt.len_ratio);
    1.59 +			if ( theWave->cvt.len != original_len ) {
    1.60  				int worksize;
    1.61 -				if ( wave->cvt.buf != NULL ) {
    1.62 -					free(wave->cvt.buf);
    1.63 +				if ( theWave->cvt.buf != NULL ) {
    1.64 +					free(theWave->cvt.buf);
    1.65  				}
    1.66 -				worksize = original_len*wave->cvt.len_mult;
    1.67 -				wave->cvt.buf=(Uint8 *)malloc(worksize);
    1.68 -				if ( wave->cvt.buf == NULL ) {
    1.69 +				worksize = original_len*theWave->cvt.len_mult;
    1.70 +				theWave->cvt.buf=(Uint8 *)malloc(worksize);
    1.71 +				if ( theWave->cvt.buf == NULL ) {
    1.72 +					SDL_mutexV(music_lock);
    1.73  					return;
    1.74  				}
    1.75 -				wave->cvt.len = original_len;
    1.76 +				theWave->cvt.len = original_len;
    1.77  			}
    1.78 -			if ( (wave->stop - pos) < original_len ) {
    1.79 -				original_len = (wave->stop - pos);
    1.80 +			if ( (theWave->stop - pos) < original_len ) {
    1.81 +				original_len = (theWave->stop - pos);
    1.82  			}
    1.83 -			wave->cvt.len = original_len;
    1.84 -			fread(wave->cvt.buf,original_len,1,wave->wavefp);
    1.85 -			SDL_ConvertAudio(&wave->cvt);
    1.86 -			memcpy(stream, wave->cvt.buf, wave->cvt.len_cvt);
    1.87 +			theWave->cvt.len = original_len;
    1.88 +			fread(theWave->cvt.buf,original_len,1,theWave->wavefp);
    1.89 +			SDL_ConvertAudio(&theWave->cvt);
    1.90 +			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
    1.91  		} else {
    1.92 -			if ( (wave->stop - pos) < len ) {
    1.93 -				len = (wave->stop - pos);
    1.94 +			if ( (theWave->stop - pos) < len ) {
    1.95 +				len = (theWave->stop - pos);
    1.96  			}
    1.97 -			fread(stream, len, 1, wave->wavefp);
    1.98 +			fread(stream, len, 1, theWave->wavefp);
    1.99  		}
   1.100  	}
   1.101 +	SDL_mutexV(music_lock);
   1.102  }
   1.103  
   1.104  /* Stop playback of a stream previously started with WAVStream_Start() */
   1.105  extern void WAVStream_Stop(void)
   1.106  {
   1.107 +	SDL_mutexP(music_lock);
   1.108 +	theWave = NULL;
   1.109 +	SDL_mutexV(music_lock);
   1.110  }
   1.111  
   1.112  /* Close the given WAV stream */
   1.113  extern void WAVStream_FreeSong(WAVStream *wave)
   1.114  {
   1.115  	if ( wave ) {
   1.116 +		/* Remove song from the currently playing list */
   1.117 +		SDL_mutexP(music_lock);
   1.118 +		if ( wave == theWave ) {
   1.119 +			theWave = NULL;
   1.120 +		}
   1.121 +		SDL_mutexV(music_lock);
   1.122 +
   1.123  		/* Clean up associated data */
   1.124  		if ( wave->wavefp ) {
   1.125  			fclose(wave->wavefp);
   1.126 @@ -157,16 +187,16 @@
   1.127  }
   1.128  
   1.129  /* Return non-zero if a stream is currently playing */
   1.130 -extern int WAVStream_Active(WAVStream *wave)
   1.131 +extern int WAVStream_Active(void)
   1.132  {
   1.133  	int active;
   1.134  
   1.135 -	SDL_LockAudio();
   1.136 +	SDL_mutexP(music_lock);
   1.137  	active = 0;
   1.138 -	if ( ftell(wave->wavefp) < wave->stop ) {
   1.139 +	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
   1.140  		active = 1;
   1.141  	}
   1.142 -	SDL_UnlockAudio();
   1.143 +	SDL_mutexV(music_lock);
   1.144  
   1.145  	return(active);
   1.146  }