Backing out changes to wave music. It would take a lot of work to do it right.
authorSam Lantinga <slouken@lokigames.com>
Mon, 03 Jul 2000 02:27:34 +0000
changeset 6400b28153c609
parent 63 92b13db7d0a5
child 65 a1112fd776b2
Backing out changes to wave music. It would take a lot of work to do it right.
music.c
wavestream.c
wavestream.h
     1.1 --- a/music.c	Mon Jul 03 00:25:20 2000 +0000
     1.2 +++ b/music.c	Mon Jul 03 02:27:34 2000 +0000
     1.3 @@ -193,7 +193,7 @@
     1.4  #endif
     1.5  #ifdef WAV_MUSIC
     1.6  			case MUS_WAV:
     1.7 -				WAVStream_PlaySome(music_playing->data.wave, stream, len);
     1.8 +				WAVStream_PlaySome(stream, len);
     1.9  				break;
    1.10  #endif
    1.11  #ifdef MOD_MUSIC
    1.12 @@ -788,7 +788,7 @@
    1.13  #endif
    1.14  #ifdef WAV_MUSIC
    1.15  			case MUS_WAV:
    1.16 -				if ( ! WAVStream_Active(music_playing->data.wave) ) {
    1.17 +				if ( ! WAVStream_Active() ) {
    1.18  					return(0);
    1.19  				}
    1.20  				break;
     2.1 --- a/wavestream.c	Mon Jul 03 00:25:20 2000 +0000
     2.2 +++ b/wavestream.c	Mon Jul 03 02:27:34 2000 +0000
     2.3 @@ -35,6 +35,12 @@
     2.4  #include "wave.h"
     2.5  #include "wavestream.h"
     2.6  
     2.7 +/* Currently we only support a single stream at a time */
     2.8 +static WAVStream *theWave = NULL;
     2.9 +
    2.10 +/* This is initialized by the music mixer */
    2.11 +static SDL_mutex *music_lock = NULL;
    2.12 +
    2.13  /* This is the format of the audio mixer data */
    2.14  static SDL_AudioSpec mixer;
    2.15  
    2.16 @@ -49,6 +55,13 @@
    2.17   */
    2.18  int WAVStream_Init(SDL_AudioSpec *mixerfmt)
    2.19  {
    2.20 +	/* FIXME: clean up the mutex, or move it into music.c */
    2.21 +	music_lock = SDL_CreateMutex();
    2.22 +#ifndef macintosh /* Hmm.. */
    2.23 +	if ( music_lock == NULL ) {
    2.24 +		return(-1);
    2.25 +	}
    2.26 +#endif
    2.27  	mixer = *mixerfmt;
    2.28  	return(0);
    2.29  }
    2.30 @@ -93,58 +106,75 @@
    2.31  /* Start playback of a given WAV stream */
    2.32  extern void WAVStream_Start(WAVStream *wave)
    2.33  {
    2.34 +	SDL_mutexP(music_lock);
    2.35  	clearerr(wave->wavefp);
    2.36  	fseek(wave->wavefp, wave->start, SEEK_SET);
    2.37 +	theWave = wave;
    2.38 +	SDL_mutexV(music_lock);
    2.39  }
    2.40  
    2.41  /* Play some of a stream previously started with WAVStream_Start()
    2.42 +   The music_lock is held while this function is called.
    2.43   */
    2.44 -extern void WAVStream_PlaySome(WAVStream *wave, Uint8 *stream, int len)
    2.45 +extern void WAVStream_PlaySome(Uint8 *stream, int len)
    2.46  {
    2.47  	long pos;
    2.48  
    2.49 -	if ( (pos=ftell(wave->wavefp)) < wave->stop ) {
    2.50 -		if ( wave->cvt.needed ) {
    2.51 +	SDL_mutexP(music_lock);
    2.52 +	if ( theWave && ((pos=ftell(theWave->wavefp)) < theWave->stop) ) {
    2.53 +		if ( theWave->cvt.needed ) {
    2.54  			int original_len;
    2.55  
    2.56 -			original_len=(int)((double)len/wave->cvt.len_ratio);
    2.57 -			if ( wave->cvt.len != original_len ) {
    2.58 +			original_len=(int)((double)len/theWave->cvt.len_ratio);
    2.59 +			if ( theWave->cvt.len != original_len ) {
    2.60  				int worksize;
    2.61 -				if ( wave->cvt.buf != NULL ) {
    2.62 -					free(wave->cvt.buf);
    2.63 +				if ( theWave->cvt.buf != NULL ) {
    2.64 +					free(theWave->cvt.buf);
    2.65  				}
    2.66 -				worksize = original_len*wave->cvt.len_mult;
    2.67 -				wave->cvt.buf=(Uint8 *)malloc(worksize);
    2.68 -				if ( wave->cvt.buf == NULL ) {
    2.69 +				worksize = original_len*theWave->cvt.len_mult;
    2.70 +				theWave->cvt.buf=(Uint8 *)malloc(worksize);
    2.71 +				if ( theWave->cvt.buf == NULL ) {
    2.72 +					SDL_mutexV(music_lock);
    2.73  					return;
    2.74  				}
    2.75 -				wave->cvt.len = original_len;
    2.76 +				theWave->cvt.len = original_len;
    2.77  			}
    2.78 -			if ( (wave->stop - pos) < original_len ) {
    2.79 -				original_len = (wave->stop - pos);
    2.80 +			if ( (theWave->stop - pos) < original_len ) {
    2.81 +				original_len = (theWave->stop - pos);
    2.82  			}
    2.83 -			wave->cvt.len = original_len;
    2.84 -			fread(wave->cvt.buf,original_len,1,wave->wavefp);
    2.85 -			SDL_ConvertAudio(&wave->cvt);
    2.86 -			memcpy(stream, wave->cvt.buf, wave->cvt.len_cvt);
    2.87 +			theWave->cvt.len = original_len;
    2.88 +			fread(theWave->cvt.buf,original_len,1,theWave->wavefp);
    2.89 +			SDL_ConvertAudio(&theWave->cvt);
    2.90 +			memcpy(stream, theWave->cvt.buf, theWave->cvt.len_cvt);
    2.91  		} else {
    2.92 -			if ( (wave->stop - pos) < len ) {
    2.93 -				len = (wave->stop - pos);
    2.94 +			if ( (theWave->stop - pos) < len ) {
    2.95 +				len = (theWave->stop - pos);
    2.96  			}
    2.97 -			fread(stream, len, 1, wave->wavefp);
    2.98 +			fread(stream, len, 1, theWave->wavefp);
    2.99  		}
   2.100  	}
   2.101 +	SDL_mutexV(music_lock);
   2.102  }
   2.103  
   2.104  /* Stop playback of a stream previously started with WAVStream_Start() */
   2.105  extern void WAVStream_Stop(void)
   2.106  {
   2.107 +	SDL_mutexP(music_lock);
   2.108 +	theWave = NULL;
   2.109 +	SDL_mutexV(music_lock);
   2.110  }
   2.111  
   2.112  /* Close the given WAV stream */
   2.113  extern void WAVStream_FreeSong(WAVStream *wave)
   2.114  {
   2.115  	if ( wave ) {
   2.116 +		/* Remove song from the currently playing list */
   2.117 +		SDL_mutexP(music_lock);
   2.118 +		if ( wave == theWave ) {
   2.119 +			theWave = NULL;
   2.120 +		}
   2.121 +		SDL_mutexV(music_lock);
   2.122 +
   2.123  		/* Clean up associated data */
   2.124  		if ( wave->wavefp ) {
   2.125  			fclose(wave->wavefp);
   2.126 @@ -157,16 +187,16 @@
   2.127  }
   2.128  
   2.129  /* Return non-zero if a stream is currently playing */
   2.130 -extern int WAVStream_Active(WAVStream *wave)
   2.131 +extern int WAVStream_Active(void)
   2.132  {
   2.133  	int active;
   2.134  
   2.135 -	SDL_LockAudio();
   2.136 +	SDL_mutexP(music_lock);
   2.137  	active = 0;
   2.138 -	if ( ftell(wave->wavefp) < wave->stop ) {
   2.139 +	if ( theWave && (ftell(theWave->wavefp) < theWave->stop) ) {
   2.140  		active = 1;
   2.141  	}
   2.142 -	SDL_UnlockAudio();
   2.143 +	SDL_mutexV(music_lock);
   2.144  
   2.145  	return(active);
   2.146  }
     3.1 --- a/wavestream.h	Mon Jul 03 00:25:20 2000 +0000
     3.2 +++ b/wavestream.h	Mon Jul 03 02:27:34 2000 +0000
     3.3 @@ -48,7 +48,7 @@
     3.4  extern void WAVStream_Start(WAVStream *wave);
     3.5  
     3.6  /* Play some of a stream previously started with WAVStream_Start() */
     3.7 -extern void WAVStream_PlaySome(WAVStream *wave, Uint8 *stream, int len);
     3.8 +extern void WAVStream_PlaySome(Uint8 *stream, int len);
     3.9  
    3.10  /* Stop playback of a stream previously started with WAVStream_Start() */
    3.11  extern void WAVStream_Stop(void);
    3.12 @@ -57,4 +57,4 @@
    3.13  extern void WAVStream_FreeSong(WAVStream *wave);
    3.14  
    3.15  /* Return non-zero if a stream is currently playing */
    3.16 -extern int WAVStream_Active(WAVStream *wave);
    3.17 +extern int WAVStream_Active(void);