SDL_Mixer - Patch to allow Mix_FadeOutMusic to fade out the music curerntly being faded in

Using SDL_Mixer, the Mix_FadeOutMusic command (well as of v1.2.6) ignores the request to fade out the music if there is already music in the process of being faded in.

I made the following code change to my copy of Mix_FadeOutMusic that adds the ability to fade out music even though it is not done fading in. It has a nice and natural fade out by reading where the current fade in is in progress and adjusting the new fade out accordingly.

(I just included the part between the audio lock and unlock…)

if ( music_playing && (music_playing->fading != MIX_FADING_OUT) ) {
if ( music_playing->fading == MIX_FADING_IN ) {
float percentFadedIn = (float)music_playing->fade_step / (float)music_playing->fade_steps;

   // set the new values based on where it is now during the current fade in
   music_playing->fade_steps = ms/ms_per_step;

   //"pro-rate" the fade out by the percentage complete of the previous fade in so the transition is smooth
   music_playing->fade_step = music_playing->fade_steps - (int)( (float)music_playing->fade_steps * percentFadedIn );
} else {
   music_playing->fade_steps = ms/ms_per_step;
   music_playing->fade_step = 0;
}
// note: code could be added to allow fade outs to occur even though a fade out is currently in progress using similar techniques

// insure that some sort of fade occurs - not sure if this is needed
if ( music_playing->fade_step >= music_playing->fade_steps ) {
   music_playing->fade_step = music_playing->fade_steps - 1;
}

music_playing->fading = MIX_FADING_OUT;  
retval = 1;

}

Hope you can make it part of the next library release.

Thanks,
–Warren Schwader