- Fixed memory leaks and used more the audio mutex
authorStephane Peter
Wed, 27 Oct 1999 00:18:07 +0000
changeset 8d2bf7cf9446c
parent 7 e73da9dc4bdc
child 9 7744cfb49357
- Fixed memory leaks and used more the audio mutex
- Keep the time at which a sample was started, and new functions
to get the oldest and most recent samples, to ease kicking out
older samples.
mixer.c
mixer.h
     1.1 --- a/mixer.c	Tue Oct 26 02:24:17 1999 +0000
     1.2 +++ b/mixer.c	Wed Oct 27 00:18:07 1999 +0000
     1.3 @@ -46,6 +46,7 @@
     1.4  	int looping;
     1.5  	int tag;
     1.6  	int expire;
     1.7 +	Uint32 start_time;
     1.8  	Mix_Fading fading;
     1.9  	int fade_volume;
    1.10  	int fade_length;
    1.11 @@ -74,44 +75,46 @@
    1.12  	mixed_channels = 0;
    1.13  	sdl_ticks = SDL_GetTicks();
    1.14  	for ( i=0; i<num_channels; ++i ) {
    1.15 -		if ( channel[i].expire > 0 && channel[i].expire < sdl_ticks ) {
    1.16 -			/* Expiration delay for that channel is reached */
    1.17 -			channel[i].playing = 0;
    1.18 -			channel[i].fading = MIX_NO_FADING;
    1.19 -			channel[i].expire = -1;
    1.20 -		} else if ( channel[i].fading != MIX_NO_FADING ) {
    1.21 -			Uint32 ticks = sdl_ticks - channel[i].ticks_fade;
    1.22 -			if( ticks > channel[i].fade_length ) {
    1.23 -				if( channel[i].fading == MIX_FADING_OUT ) {
    1.24 -					channel[i].playing = 0;
    1.25 -					channel[i].expire = -1;
    1.26 -					Mix_Volume(i, channel[i].fading); /* Restore the volume */
    1.27 -				}
    1.28 +		if( ! channel[i].paused ) {
    1.29 +			if ( channel[i].expire > 0 && channel[i].expire < sdl_ticks ) {
    1.30 +				/* Expiration delay for that channel is reached */
    1.31 +				channel[i].playing = 0;
    1.32  				channel[i].fading = MIX_NO_FADING;
    1.33 -			} else {
    1.34 -				if( channel[i].fading == MIX_FADING_OUT ) {
    1.35 -					Mix_Volume(i, (channel[i].fade_volume * (channel[i].fade_length-ticks))
    1.36 -							   / channel[i].fade_length );
    1.37 +				channel[i].expire = -1;
    1.38 +			} else if ( channel[i].fading != MIX_NO_FADING ) {
    1.39 +				Uint32 ticks = sdl_ticks - channel[i].ticks_fade;
    1.40 +				if( ticks > channel[i].fade_length ) {
    1.41 +					if( channel[i].fading == MIX_FADING_OUT ) {
    1.42 +						channel[i].playing = 0;
    1.43 +						channel[i].expire = -1;
    1.44 +						Mix_Volume(i, channel[i].fading); /* Restore the volume */
    1.45 +					}
    1.46 +					channel[i].fading = MIX_NO_FADING;
    1.47  				} else {
    1.48 -					Mix_Volume(i, (channel[i].fade_volume * ticks) / channel[i].fade_length );
    1.49 +					if( channel[i].fading == MIX_FADING_OUT ) {
    1.50 +						Mix_Volume(i, (channel[i].fade_volume * (channel[i].fade_length-ticks))
    1.51 +								   / channel[i].fade_length );
    1.52 +					} else {
    1.53 +						Mix_Volume(i, (channel[i].fade_volume * ticks) / channel[i].fade_length );
    1.54 +					}
    1.55  				}
    1.56  			}
    1.57 -		}
    1.58 -		if ( channel[i].playing && !channel[i].paused ) {
    1.59 -		    ++ mixed_channels;
    1.60 -			mixable = channel[i].playing;
    1.61 -			if ( mixable > len ) {
    1.62 -				mixable = len;
    1.63 -			}
    1.64 -			volume = (channel[i].volume*channel[i].chunk->volume) /
    1.65 -								MIX_MAX_VOLUME;
    1.66 -			SDL_MixAudio(stream,channel[i].samples,mixable,volume);
    1.67 -			channel[i].samples += mixable;
    1.68 -			channel[i].playing -= mixable;
    1.69 -			if ( ! channel[i].playing && channel[i].looping ) {
    1.70 -				if ( --channel[i].looping ) {
    1.71 -				    channel[i].samples = channel[i].chunk->abuf;
    1.72 -				    channel[i].playing = channel[i].chunk->alen;
    1.73 +			if ( channel[i].playing > 0 ) {
    1.74 +				++ mixed_channels;
    1.75 +				mixable = channel[i].playing;
    1.76 +				if ( mixable > len ) {
    1.77 +					mixable = len;
    1.78 +				}
    1.79 +				volume = (channel[i].volume*channel[i].chunk->volume) /
    1.80 +					MIX_MAX_VOLUME;
    1.81 +				SDL_MixAudio(stream,channel[i].samples,mixable,volume);
    1.82 +				channel[i].samples += mixable;
    1.83 +				channel[i].playing -= mixable;
    1.84 +				if ( ! channel[i].playing && channel[i].looping ) {
    1.85 +					if ( --channel[i].looping ) {
    1.86 +						channel[i].samples = channel[i].chunk->abuf;
    1.87 +						channel[i].playing = channel[i].chunk->alen;
    1.88 +					}
    1.89  				}
    1.90  			}
    1.91  		}
    1.92 @@ -439,13 +442,15 @@
    1.93  
    1.94  		/* Queue up the audio data for this channel */
    1.95  		if ( which >= 0 ) {
    1.96 +			Uint32 sdl_ticks = SDL_GetTicks();
    1.97  			channel[which].samples = chunk->abuf;
    1.98  			channel[which].playing = chunk->alen;
    1.99  			channel[which].looping = loops;
   1.100  			channel[which].chunk = chunk;
   1.101  			channel[which].paused = 0;
   1.102  			channel[which].fading = MIX_NO_FADING;
   1.103 -			channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : -1;
   1.104 +			channel[which].start_time = sdl_ticks;
   1.105 +			channel[which].expire = (ticks>0) ? (sdl_ticks + ticks) : -1;
   1.106  		}
   1.107  	}
   1.108  	SDL_mutexV(mixer_lock);
   1.109 @@ -501,6 +506,7 @@
   1.110  
   1.111  		/* Queue up the audio data for this channel */
   1.112  		if ( which >= 0 ) {
   1.113 +			Uint32 sdl_ticks = SDL_GetTicks();
   1.114  			channel[which].samples = chunk->abuf;
   1.115  			channel[which].playing = chunk->alen;
   1.116  			channel[which].looping = loops;
   1.117 @@ -510,8 +516,8 @@
   1.118  			channel[which].fade_volume = channel[which].volume;
   1.119  			channel[which].volume = 0;
   1.120  			channel[which].fade_length = ms;
   1.121 -			channel[which].ticks_fade = SDL_GetTicks();
   1.122 -			channel[which].expire = (ticks > 0) ? (SDL_GetTicks()+ticks) : -1;
   1.123 +			channel[which].start_time = channel[which].ticks_fade = sdl_ticks;
   1.124 +			channel[which].expire = (ticks > 0) ? (sdl_ticks+ticks) : -1;
   1.125  		}
   1.126  	}
   1.127  	SDL_mutexV(mixer_lock);
   1.128 @@ -672,6 +678,7 @@
   1.129  			Mix_HaltChannel(-1);
   1.130  			SDL_CloseAudio();
   1.131  			SDL_DestroyMutex(mixer_lock);
   1.132 +			free(channel);
   1.133  			channel = NULL;
   1.134  		}
   1.135  		--audio_opened;
   1.136 @@ -681,18 +688,18 @@
   1.137  /* Pause a particular channel (or all) */
   1.138  void Mix_Pause(int which)
   1.139  {
   1.140 -	/* TODO: Handle properly expiring samples */
   1.141 +	Uint32 sdl_ticks = SDL_GetTicks();
   1.142  	if ( which == -1 ) {
   1.143  		int i;
   1.144  
   1.145  		for ( i=0; i<num_channels; ++i ) {
   1.146  			if ( channel[i].playing > 0 ) {
   1.147 -				channel[i].paused = 1;
   1.148 +				channel[i].paused = sdl_ticks;
   1.149  			}
   1.150  		}
   1.151  	} else {
   1.152  		if ( channel[which].playing > 0 ) {
   1.153 -			channel[which].paused = 1;
   1.154 +			channel[which].paused = sdl_ticks;
   1.155  		}
   1.156  	}
   1.157  }
   1.158 @@ -700,18 +707,27 @@
   1.159  /* Resume a paused channel */
   1.160  void Mix_Resume(int which)
   1.161  {
   1.162 +	Uint32 sdl_ticks = SDL_GetTicks();
   1.163  	if ( which == -1 ) {
   1.164  		int i;
   1.165  
   1.166 +		SDL_mutexP(mixer_lock);
   1.167  		for ( i=0; i<num_channels; ++i ) {
   1.168  			if ( channel[i].playing > 0 ) {
   1.169 +				if(channel[i].expire > 0)
   1.170 +					channel[i].expire += sdl_ticks - channel[i].paused;
   1.171  				channel[i].paused = 0;
   1.172  			}
   1.173  		}
   1.174 +		SDL_mutexV(mixer_lock);
   1.175  	} else {
   1.176 +		SDL_mutexP(mixer_lock);
   1.177  		if ( channel[which].playing > 0 ) {
   1.178 +			if(channel[which].expire > 0)
   1.179 +				channel[which].expire += sdl_ticks - channel[which].paused;
   1.180  			channel[which].paused = 0;
   1.181  		}
   1.182 +		SDL_mutexV(mixer_lock);
   1.183  	}
   1.184  }
   1.185  
   1.186 @@ -758,3 +774,35 @@
   1.187  	}
   1.188  	return(count);
   1.189  }
   1.190 +
   1.191 +/* Finds the "oldest" sample playing in a group of channels */
   1.192 +int Mix_GroupOldest(int tag)
   1.193 +{
   1.194 +	int chan = -1;
   1.195 +	Uint32 mintime = SDL_GetTicks();
   1.196 +	int i;
   1.197 +	for( i=0; i < num_channels; i ++ ) {
   1.198 +		if ( (channel[i].tag==tag || tag==-1) && channel[i].playing > 0 
   1.199 +			 && channel[i].start_time <= mintime ) {
   1.200 +			mintime = channel[i].start_time;
   1.201 +			chan = i;
   1.202 +		}
   1.203 +	}
   1.204 +	return(chan);
   1.205 +}
   1.206 +
   1.207 +/* Finds the "most recent" (i.e. last) sample playing in a group of channels */
   1.208 +int Mix_GroupNewer(int tag)
   1.209 +{
   1.210 +	int chan = -1;
   1.211 +	Uint32 maxtime = 0;
   1.212 +	int i;
   1.213 +	for( i=0; i < num_channels; i ++ ) {
   1.214 +		if ( (channel[i].tag==tag || tag==-1) && channel[i].playing > 0 
   1.215 +			 && channel[i].start_time >= maxtime ) {
   1.216 +			maxtime = channel[i].start_time;
   1.217 +			chan = i;
   1.218 +		}
   1.219 +	}
   1.220 +	return(chan);
   1.221 +}
     2.1 --- a/mixer.h	Tue Oct 26 02:24:17 1999 +0000
     2.2 +++ b/mixer.h	Wed Oct 27 00:18:07 1999 +0000
     2.3 @@ -120,6 +120,10 @@
     2.4     way to get the total number of channels when 'tag' is -1
     2.5   */
     2.6  extern int Mix_GroupCount(int tag);
     2.7 +/* Finds the "oldest" sample playing in a group of channels */
     2.8 +extern int Mix_GroupOldest(int tag);
     2.9 +/* Finds the "most recent" (i.e. last) sample playing in a group of channels */
    2.10 +extern int Mix_GroupNewer(int tag);
    2.11  
    2.12  /* Play an audio chunk on a specific channel.
    2.13     If the specified channel is -1, play on the first free channel.