Removed FadeInOut because it leads to deadlock problems
authorSam Lantinga <slouken@lokigames.com>
Thu, 11 Nov 1999 17:13:44 +0000
changeset 17d5638c74656f
parent 16 ada544ec16c6
child 18 461dd4cb63ba
Removed FadeInOut because it leads to deadlock problems
mixer.c
mixer.h
music.c
     1.1 --- a/mixer.c	Tue Nov 09 20:45:52 1999 +0000
     1.2 +++ b/mixer.c	Thu Nov 11 17:13:44 1999 +0000
     1.3 @@ -53,15 +53,13 @@
     1.4  	Uint32 ticks_fade;
     1.5  } *channel = NULL;
     1.6  static int num_channels;
     1.7 +static int reserved_channels = 0;
     1.8  
     1.9 -#define MUSIC_VOL	64		/* Music volume 0-64 */
    1.10  
    1.11  /* Support for user defined music functions, plus the default one */
    1.12  extern int music_active;
    1.13  extern void music_mixer(void *udata, Uint8 *stream, int len);
    1.14  static void (*mix_music)(void *udata, Uint8 *stream, int len) = music_mixer;
    1.15 -static int mixed_channels = 0; /* Holds the number of channels actually mixed, for debugging */
    1.16 -static int reserved_channels = 0;
    1.17  void *music_data = NULL;
    1.18  
    1.19  /* Mixing function */
    1.20 @@ -77,7 +75,6 @@
    1.21  
    1.22  	/* Grab the channels we need to mix */
    1.23  	SDL_mutexP(mixer_lock);
    1.24 -	mixed_channels = 0;
    1.25  	sdl_ticks = SDL_GetTicks();
    1.26  	for ( i=0; i<num_channels; ++i ) {
    1.27  		if( ! channel[i].paused ) {
    1.28 @@ -105,7 +102,6 @@
    1.29  				}
    1.30  			}
    1.31  			if ( channel[i].playing > 0 ) {
    1.32 -				++ mixed_channels;
    1.33  				mixable = channel[i].playing;
    1.34  				if ( mixable > len ) {
    1.35  					mixable = len;
     2.1 --- a/mixer.h	Tue Nov 09 20:45:52 1999 +0000
     2.2 +++ b/mixer.h	Thu Nov 11 17:13:44 1999 +0000
     2.3 @@ -146,11 +146,6 @@
     2.4  #define Mix_FadeInChannel(channel,chunk,loops,ms) Mix_FadeInChannelTimed(channel,chunk,loops,ms,-1)
     2.5  extern int Mix_FadeInChannelTimed(int channel, Mix_Chunk *chunk, int loops, int ms, int ticks);
     2.6  
     2.7 -/* Fades out the currently playing music, and progressively fades in a new music,
     2.8 -   all in the background. The 'ms' period is half for fading out the music and
     2.9 -   fading in the new one */
    2.10 -extern int Mix_FadeOutInMusic(Mix_Music *music, int loops, int ms);
    2.11 -
    2.12  /* Set the volume in the range of 0-128 of a specific channel or chunk.
    2.13     If the specified channel is -1, set volume for all channels.
    2.14     Returns the original volume.
     3.1 --- a/music.c	Tue Nov 09 20:45:52 1999 +0000
     3.2 +++ b/music.c	Thu Nov 11 17:13:44 1999 +0000
     3.3 @@ -54,19 +54,14 @@
     3.4  
     3.5  int music_active = 1;
     3.6  static int music_stopped = 0;
     3.7 -static int music_paused = 0;
     3.8  static int music_loops = 0;
     3.9  static char *music_cmd = NULL;
    3.10  static int samplesize;
    3.11  static Mix_Music *music_playing = 0;
    3.12 -static int music_volume;
    3.13 +static int music_volume = MIX_MAX_VOLUME;
    3.14  static int music_swap8;
    3.15  static int music_swap16;
    3.16  
    3.17 -/* For fading/chaining musics */
    3.18 -static Mix_Music *next_music = 0;
    3.19 -static int next_period = 0, next_loops = 0;
    3.20 -
    3.21  struct _Mix_Music {
    3.22  	enum {
    3.23  		MUS_CMD,
    3.24 @@ -94,12 +89,15 @@
    3.25  	} data;
    3.26  	Mix_Fading fading;
    3.27  	int fade_volume;
    3.28 -	int fade_length;
    3.29 -	Uint32 ticks_fade;
    3.30 +	int fade_step;
    3.31 +	int fade_steps;
    3.32  	int error;
    3.33  };
    3.34  static int timidity_ok;
    3.35  
    3.36 +/* Used to calculate fading steps */
    3.37 +static int ms_per_step;
    3.38 +
    3.39  /* Local low-level functions prototypes */
    3.40  static void lowlevel_halt(void);
    3.41  static int  lowlevel_play(Mix_Music *music);
    3.42 @@ -116,34 +114,25 @@
    3.43  			lowlevel_halt(); /* This function sets music_playing to NULL */
    3.44  			return;
    3.45  		}	
    3.46 -		if ( music_paused ) {
    3.47 -			return;
    3.48 -		}
    3.49  		/* Handle fading */
    3.50 -		if ( !music_stopped && music_playing->fading != MIX_NO_FADING ) {
    3.51 -			Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
    3.52 -			if( ticks > music_playing->fade_length ) {
    3.53 +		if ( music_playing->fading != MIX_NO_FADING ) {
    3.54 +			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
    3.55 +                int fade_volume = music_playing->fade_volume;
    3.56 +                int fade_step = music_playing->fade_step;
    3.57 +                int fade_steps = music_playing->fade_steps;
    3.58 +
    3.59  				if ( music_playing->fading == MIX_FADING_OUT ) {
    3.60 -					music_volume = music_playing->fade_volume;
    3.61 -					music_playing->fading = MIX_NO_FADING;
    3.62 -					if (next_music) {
    3.63 -						Mix_FadeInMusic(next_music,next_loops,next_period);
    3.64 -						next_music = 0;
    3.65 -						next_period = next_loops = 0;
    3.66 -					} else {
    3.67 -						lowlevel_halt();
    3.68 -					}
    3.69 +					Mix_VolumeMusic((fade_volume * (fade_steps-fade_step)) 
    3.70 +									/ fade_steps);
    3.71 +				} else { /* Fading in */
    3.72 +					Mix_VolumeMusic((fade_volume * fade_step) / fade_steps);
    3.73 +				}
    3.74 +			} else {
    3.75 +				if ( music_playing->fading == MIX_FADING_OUT ) {
    3.76 +					lowlevel_halt();
    3.77  					return;
    3.78  				}
    3.79  				music_playing->fading = MIX_NO_FADING;
    3.80 -			} else {
    3.81 -				if ( music_playing->fading == MIX_FADING_OUT ) {
    3.82 -					Mix_VolumeMusic((music_playing->fade_volume * (music_playing->fade_length-ticks)) 
    3.83 -									/ music_playing->fade_length);
    3.84 -				} else { /* Fading in */
    3.85 -					Mix_VolumeMusic((music_playing->fade_volume * ticks) 
    3.86 -									/ music_playing->fade_length);
    3.87 -				}
    3.88  			}
    3.89  		}
    3.90  		/* Restart music if it has to loop */
    3.91 @@ -293,12 +282,15 @@
    3.92  #endif
    3.93  	music_playing = 0;
    3.94  	music_stopped = 0;
    3.95 -
    3.96  	if ( music_error ) {
    3.97  		return(-1);
    3.98  	}
    3.99  	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
   3.100  
   3.101 +    /* Calculate the number of ms for each callback */
   3.102 +    ms_per_step = ((float)mixer->samples * 1000.0) / mixer->freq;
   3.103 +printf("MS per step = %d\n", ms_per_step);
   3.104 +
   3.105  	return(0);
   3.106  }
   3.107  
   3.108 @@ -409,7 +401,7 @@
   3.109  		if ( music == music_playing && !music_stopped ) {
   3.110  			if ( music->fading == MIX_FADING_OUT ) {
   3.111  				/* Wait for the fade out to finish */
   3.112 -				while(music_playing && !music_stopped && music_playing->fading == MIX_FADING_OUT)
   3.113 +				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
   3.114  					SDL_Delay(100);
   3.115  			} else {
   3.116  				Mix_HaltMusic(); /* Stop it immediately */
   3.117 @@ -508,9 +500,9 @@
   3.118  		SDL_Delay(100);
   3.119  	}
   3.120  
   3.121 -	if(lowlevel_play(music)<0)
   3.122 +	if ( lowlevel_play(music) < 0 ) {
   3.123  		return(-1);
   3.124 -
   3.125 +    }
   3.126  	music_active = 1;
   3.127  	music_stopped = 0;
   3.128  	music_loops = loops;
   3.129 @@ -525,32 +517,16 @@
   3.130  	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
   3.131  		music->fade_volume = music_volume;
   3.132  		music_volume = 0;
   3.133 -		if(Mix_PlayMusic(music,loops)<0)
   3.134 +		if ( Mix_PlayMusic(music, loops) < 0 ) {
   3.135  			return(-1);
   3.136 +        }
   3.137 +		music_playing->fade_step = 0;
   3.138 +		music_playing->fade_steps = ms/ms_per_step;
   3.139  		music_playing->fading = MIX_FADING_IN;
   3.140 -		music_playing->fade_length = ms;
   3.141 -		music_playing->ticks_fade = SDL_GetTicks();
   3.142  	}
   3.143  	return(0);
   3.144  }
   3.145  
   3.146 -/* Fades out the currently playing music, and progressively fades in a new music,
   3.147 -   all in the background. The 'ms' period is half for fading out the music and
   3.148 -   fading in the new one */
   3.149 -int Mix_FadeOutInMusic(Mix_Music *music, int loops, int ms)
   3.150 -{
   3.151 -	if( music_stopped || !music_playing ) {
   3.152 -		return Mix_FadeInMusic(music,loops,ms/2);
   3.153 -	} else {
   3.154 -		/* Set up the data for chaining the next music */
   3.155 -		next_music = music;
   3.156 -		next_period = ms/2;
   3.157 -		next_loops = loops;
   3.158 -		/* Fade out the current music */
   3.159 -		Mix_FadeOutMusic(ms/2);
   3.160 -	}
   3.161 -}
   3.162 -
   3.163  /* Set the music volume */
   3.164  int Mix_VolumeMusic(int volume)
   3.165  {
   3.166 @@ -631,13 +607,12 @@
   3.167  		/* Unknown music type?? */
   3.168  		return;
   3.169  	}
   3.170 -	if(music_playing->fading != MIX_NO_FADING) /* Restore volume */
   3.171 +	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
   3.172  		music_volume = music_playing->fade_volume;
   3.173  	music_playing->fading = MIX_NO_FADING;
   3.174 -	music_playing = 0;
   3.175 +	music_playing = NULL;
   3.176  	music_active = 0;
   3.177  	music_loops = 0;
   3.178 -	music_paused = 0;
   3.179  	music_stopped = 0;
   3.180  }
   3.181  
   3.182 @@ -657,12 +632,12 @@
   3.183  /* Progressively stop the music */
   3.184  int Mix_FadeOutMusic(int ms)
   3.185  {
   3.186 -	if ( music_playing && !music_stopped && music_playing->fading==MIX_NO_FADING ) {
   3.187 -		if ( music_volume>0 ) {
   3.188 +	if ( music_playing && !music_stopped && (music_playing->fading == MIX_NO_FADING) ) {
   3.189 +		if ( music_volume > 0 ) {
   3.190  			music_playing->fading = MIX_FADING_OUT;
   3.191  			music_playing->fade_volume = music_volume;
   3.192 -			music_playing->fade_length = ms;
   3.193 -			music_playing->ticks_fade = SDL_GetTicks();
   3.194 +		    music_playing->fade_step = 0;
   3.195 +		    music_playing->fade_steps = ms/ms_per_step;
   3.196  			return(1);
   3.197  		}
   3.198  	}
   3.199 @@ -679,17 +654,15 @@
   3.200  /* Pause/Resume the music stream */
   3.201  void Mix_PauseMusic(void)
   3.202  {
   3.203 -	if ( music_playing && music_active && !music_stopped ) {
   3.204 +	if ( music_playing && !music_stopped ) {
   3.205  		music_active = 0;
   3.206 -		music_paused = 1;
   3.207  	}
   3.208  }
   3.209  
   3.210  void Mix_ResumeMusic(void)
   3.211  {
   3.212 -	if ( music_playing && !music_active && !music_stopped ) {
   3.213 +	if ( music_playing && !music_stopped ) {
   3.214  		music_active = 1;
   3.215 -		music_paused = 0;
   3.216  	}
   3.217  }
   3.218  
   3.219 @@ -709,7 +682,7 @@
   3.220  
   3.221  int Mix_PausedMusic(void)
   3.222  {
   3.223 -	return music_paused;
   3.224 +	return (music_active == 0);
   3.225  }
   3.226  
   3.227  /* Check the status of the music */