wavestream.c
changeset 173 0821a4ea0008
parent 142 59564a17ae18
child 214 ab571e095c22
     1.1 --- a/wavestream.c	Fri May 03 04:09:27 2002 +0000
     1.2 +++ b/wavestream.c	Thu May 16 18:28:06 2002 +0000
     1.3 @@ -88,10 +88,7 @@
     1.4  
     1.5  
     1.6  /* Currently we only support a single stream at a time */
     1.7 -static WAVStream *theWave = NULL;
     1.8 -
     1.9 -/* This is initialized by the music mixer */
    1.10 -static SDL_mutex *music_lock = NULL;
    1.11 +static WAVStream *music = NULL;
    1.12  
    1.13  /* This is the format of the audio mixer data */
    1.14  static SDL_AudioSpec mixer;
    1.15 @@ -107,13 +104,6 @@
    1.16   */
    1.17  int WAVStream_Init(SDL_AudioSpec *mixerfmt)
    1.18  {
    1.19 -	/* FIXME: clean up the mutex, or move it into music.c */
    1.20 -	music_lock = SDL_CreateMutex();
    1.21 -#ifndef macintosh /* Hmm.. */
    1.22 -	if ( music_lock == NULL ) {
    1.23 -		return(-1);
    1.24 -	}
    1.25 -#endif
    1.26  	mixer = *mixerfmt;
    1.27  	return(0);
    1.28  }
    1.29 @@ -160,84 +150,68 @@
    1.30  /* Start playback of a given WAV stream */
    1.31  extern void WAVStream_Start(WAVStream *wave)
    1.32  {
    1.33 -	SDL_mutexP(music_lock);
    1.34  	clearerr(wave->wavefp);
    1.35  	fseek(wave->wavefp, wave->start, SEEK_SET);
    1.36 -	theWave = wave;
    1.37 -	SDL_mutexV(music_lock);
    1.38 +	music = wave;
    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 +/* Play some of a stream previously started with WAVStream_Start() */
    1.45  extern void WAVStream_PlaySome(Uint8 *stream, int len)
    1.46  {
    1.47  	long pos;
    1.48  
    1.49 -	SDL_mutexP(music_lock);
    1.50 -	if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
    1.51 -		if ( theWave->cvt.needed ) {
    1.52 +	if ( music && ((pos=ftell(music->wavefp)) < music->stop) ) {
    1.53 +		if ( music->cvt.needed ) {
    1.54  			int original_len;
    1.55  
    1.56 -			original_len=(int)((double)len/theWave->cvt.len_ratio);
    1.57 -			if ( theWave->cvt.len != original_len ) {
    1.58 +			original_len=(int)((double)len/music->cvt.len_ratio);
    1.59 +			if ( music->cvt.len != original_len ) {
    1.60  				int worksize;
    1.61 -				if ( theWave->cvt.buf != NULL ) {
    1.62 -					free(theWave->cvt.buf);
    1.63 +				if ( music->cvt.buf != NULL ) {
    1.64 +					free(music->cvt.buf);
    1.65  				}
    1.66 -				worksize = original_len*theWave->cvt.len_mult;
    1.67 -				theWave->cvt.buf=(Uint8 *)malloc(worksize);
    1.68 -				if ( theWave->cvt.buf == NULL ) {
    1.69 -					SDL_mutexV(music_lock);
    1.70 +				worksize = original_len*music->cvt.len_mult;
    1.71 +				music->cvt.buf=(Uint8 *)malloc(worksize);
    1.72 +				if ( music->cvt.buf == NULL ) {
    1.73  					return;
    1.74  				}
    1.75 -				theWave->cvt.len = original_len;
    1.76 +				music->cvt.len = original_len;
    1.77  			}
    1.78 -			if ( (theWave->stop - pos) < original_len ) {
    1.79 -				original_len = (theWave->stop - pos);
    1.80 +			if ( (music->stop - pos) < original_len ) {
    1.81 +				original_len = (music->stop - pos);
    1.82  			}
    1.83 -			original_len = fread(theWave->cvt.buf,1,original_len,theWave->wavefp);
    1.84 +			original_len = fread(music->cvt.buf,1,original_len,music->wavefp);
    1.85  			/* At least at the time of writing, SDL_ConvertAudio()
    1.86  			   does byte-order swapping starting at the end of the
    1.87  			   buffer. Thus, if we are reading 16-bit samples, we
    1.88  			   had better make damn sure that we get an even
    1.89  			   number of bytes, or we'll get garbage.
    1.90  			 */
    1.91 -			if ( (theWave->cvt.src_format & 0x0010) && (original_len & 1) ) {
    1.92 +			if ( (music->cvt.src_format & 0x0010) && (original_len & 1) ) {
    1.93  				original_len--;
    1.94  			}
    1.95 -			theWave->cvt.len = original_len;
    1.96 -			SDL_ConvertAudio(&theWave->cvt);
    1.97 -			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
    1.98 +			music->cvt.len = original_len;
    1.99 +			SDL_ConvertAudio(&music->cvt);
   1.100 +			memcpy(stream, music->cvt.buf, music->cvt.len_cvt);
   1.101  		} else {
   1.102 -			if ( (theWave->stop - pos) < len ) {
   1.103 -				len = (theWave->stop - pos);
   1.104 +			if ( (music->stop - pos) < len ) {
   1.105 +				len = (music->stop - pos);
   1.106  			}
   1.107 -			fread(stream, len, 1, theWave->wavefp);
   1.108 +			fread(stream, len, 1, music->wavefp);
   1.109  		}
   1.110  	}
   1.111 -	SDL_mutexV(music_lock);
   1.112  }
   1.113  
   1.114  /* Stop playback of a stream previously started with WAVStream_Start() */
   1.115  extern void WAVStream_Stop(void)
   1.116  {
   1.117 -	SDL_mutexP(music_lock);
   1.118 -	theWave = NULL;
   1.119 -	SDL_mutexV(music_lock);
   1.120 +	music = NULL;
   1.121  }
   1.122  
   1.123  /* Close the given WAV stream */
   1.124  extern void WAVStream_FreeSong(WAVStream *wave)
   1.125  {
   1.126  	if ( wave ) {
   1.127 -		/* Remove song from the currently playing list */
   1.128 -		SDL_mutexP(music_lock);
   1.129 -		if ( wave == theWave ) {
   1.130 -			theWave = NULL;
   1.131 -		}
   1.132 -		SDL_mutexV(music_lock);
   1.133 -
   1.134  		/* Clean up associated data */
   1.135  		if ( wave->wavefp ) {
   1.136  			fclose(wave->wavefp);
   1.137 @@ -254,13 +228,10 @@
   1.138  {
   1.139  	int active;
   1.140  
   1.141 -	SDL_mutexP(music_lock);
   1.142  	active = 0;
   1.143 -	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
   1.144 +	if ( music && (ftell(music->wavefp) < music->stop) ) {
   1.145  		active = 1;
   1.146  	}
   1.147 -	SDL_mutexV(music_lock);
   1.148 -
   1.149  	return(active);
   1.150  }
   1.151