- Fixed Mix_PlayingMusic()
authorStephane Peter <megastep@lokigames.com>
Sat, 30 Oct 1999 00:21:34 +0000
changeset 102fbd0414a1fc
parent 9 7744cfb49357
child 11 3939a99bf422
- Fixed Mix_PlayingMusic()
- Added real generic music looping, just like samples
- Added Mix_FadeOutInMusic() to fade out the current music and fade in
a new one in the background
mixer.h
music.c
playmus.c
     1.1 --- a/mixer.h	Wed Oct 27 22:49:38 1999 +0000
     1.2 +++ b/mixer.h	Sat Oct 30 00:21:34 1999 +0000
     1.3 @@ -146,6 +146,11 @@
     1.4  #define Mix_FadeInChannel(channel,chunk,loops,ms) Mix_FadeInChannelTimed(channel,chunk,loops,ms,-1)
     1.5  extern int Mix_FadeInChannelTimed(int channel, Mix_Chunk *chunk, int loops, int ms, int ticks);
     1.6  
     1.7 +/* Fades out the currently playing music, and progressively fades in a new music,
     1.8 +   all in the background. The 'ms' period is half for fading out the music and
     1.9 +   fading in the new one */
    1.10 +extern int Mix_FadeOutInMusic(Mix_Music *music, int loops, int ms);
    1.11 +
    1.12  /* Set the volume in the range of 0-128 of a specific channel or chunk.
    1.13     If the specified channel is -1, set volume for all channels.
    1.14     Returns the original volume.
     2.1 --- a/music.c	Wed Oct 27 22:49:38 1999 +0000
     2.2 +++ b/music.c	Sat Oct 30 00:21:34 1999 +0000
     2.3 @@ -62,6 +62,10 @@
     2.4  static int music_swap8;
     2.5  static int music_swap16;
     2.6  
     2.7 +/* For fading/chaining musics */
     2.8 +static Mix_Music *next_music = 0;
     2.9 +static int next_period = 0, next_loops = 0;
    2.10 +
    2.11  struct _Mix_Music {
    2.12  	enum {
    2.13  		MUS_CMD,
    2.14 @@ -95,7 +99,10 @@
    2.15  };
    2.16  static int timidity_ok;
    2.17  
    2.18 +/* Local low-level functions prototypes */
    2.19  static void lowlevel_halt(void);
    2.20 +static int  lowlevel_play(Mix_Music *music);
    2.21 +static int  lowlevel_playing(void);
    2.22  
    2.23  /* Mixing function */
    2.24  void music_mixer(void *udata, Uint8 *stream, int len)
    2.25 @@ -109,13 +116,26 @@
    2.26  			lowlevel_halt(); /* This function sets music_playing to NULL */
    2.27  			return;
    2.28  		}	
    2.29 -		if ( music_playing->fading != MIX_NO_FADING ) {
    2.30 +		if ( music_playing && music_loops && !lowlevel_playing() ) {
    2.31 +			if ( -- music_loops ) {
    2.32 +				Mix_RewindMusic();
    2.33 +				if ( lowlevel_play(music_playing) < 0 )
    2.34 +					music_stopped = 1; /* Something went wrong */
    2.35 +			}
    2.36 +		}
    2.37 +		if ( !music_stopped && music_playing->fading != MIX_NO_FADING ) {
    2.38  			Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
    2.39  			if( ticks > music_playing->fade_length ) {
    2.40  				if ( music_playing->fading == MIX_FADING_OUT ) {
    2.41  					music_volume = music_playing->fade_volume;
    2.42  					music_playing->fading = MIX_NO_FADING;
    2.43 -					lowlevel_halt();
    2.44 +					if (next_music) {
    2.45 +						Mix_FadeInMusic(next_music,next_loops,next_period);
    2.46 +						next_music = 0;
    2.47 +						next_period = next_loops = 0;
    2.48 +					} else {
    2.49 +						lowlevel_halt();
    2.50 +					}
    2.51  					return;
    2.52  				}
    2.53  				music_playing->fading = MIX_NO_FADING;
    2.54 @@ -418,18 +438,11 @@
    2.55  	}
    2.56  }
    2.57  
    2.58 -/* Play a music chunk.  Returns 0, or -1 if there was an error.
    2.59 -*/
    2.60 -int Mix_PlayMusic(Mix_Music *music, int loops)
    2.61 +static int lowlevel_play(Mix_Music *music)
    2.62  {
    2.63 -	/* Don't play null pointers :-) */
    2.64 -	if ( music == NULL ) {
    2.65 +	if(!music)
    2.66  		return(-1);
    2.67 -	}
    2.68 -	/* If the current music is fading out, wait for the fade to complete */
    2.69 -	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
    2.70 -		SDL_Delay(100);
    2.71 -	}
    2.72 +
    2.73  	switch (music->type) {
    2.74  #ifdef CMD_MUSIC
    2.75  		case MUS_CMD:
    2.76 @@ -461,7 +474,6 @@
    2.77  			SMPEG_enableaudio(music->data.mp3,1);
    2.78  			SMPEG_enablevideo(music->data.mp3,0);
    2.79  			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
    2.80 -			SMPEG_loop(music->data.mp3, loops);
    2.81  			SMPEG_play(music->data.mp3);
    2.82  			break;
    2.83  #endif
    2.84 @@ -469,8 +481,28 @@
    2.85  			/* Unknown music type?? */
    2.86  			return(-1);
    2.87  	}
    2.88 +	return(0);
    2.89 +}
    2.90 +
    2.91 +/* Play a music chunk.  Returns 0, or -1 if there was an error.
    2.92 +*/
    2.93 +int Mix_PlayMusic(Mix_Music *music, int loops)
    2.94 +{
    2.95 +	/* Don't play null pointers :-) */
    2.96 +	if ( music == NULL ) {
    2.97 +		return(-1);
    2.98 +	}
    2.99 +	/* If the current music is fading out, wait for the fade to complete */
   2.100 +	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
   2.101 +		SDL_Delay(100);
   2.102 +	}
   2.103 +
   2.104 +	if(lowlevel_play(music)<0)
   2.105 +		return(-1);
   2.106 +
   2.107  	music_active = 1;
   2.108  	music_stopped = 0;
   2.109 +	music_loops = loops;
   2.110  	music_playing = music;
   2.111  	music_playing->fading = MIX_NO_FADING;
   2.112  	return(0);
   2.113 @@ -491,6 +523,23 @@
   2.114  	return(0);
   2.115  }
   2.116  
   2.117 +/* Fades out the currently playing music, and progressively fades in a new music,
   2.118 +   all in the background. The 'ms' period is half for fading out the music and
   2.119 +   fading in the new one */
   2.120 +int Mix_FadeOutInMusic(Mix_Music *music, int loops, int ms)
   2.121 +{
   2.122 +	if( music_stopped || !music_playing ) {
   2.123 +		return Mix_FadeInMusic(music,loops,ms/2);
   2.124 +	} else {
   2.125 +		/* Set up the data for chaining the next music */
   2.126 +		next_music = music;
   2.127 +		next_period = ms/2;
   2.128 +		next_loops = loops;
   2.129 +		/* Fade out the current music */
   2.130 +		Mix_FadeOutMusic(ms/2);
   2.131 +	}
   2.132 +}
   2.133 +
   2.134  /* Set the music volume */
   2.135  int Mix_VolumeMusic(int volume)
   2.136  {
   2.137 @@ -668,47 +717,58 @@
   2.138  }
   2.139  
   2.140  /* Check the status of the music */
   2.141 -int Mix_PlayingMusic(void)
   2.142 +static int lowlevel_playing(void)
   2.143  {
   2.144  	if ( music_playing && !music_stopped ) {
   2.145  		switch (music_playing->type) {
   2.146  #ifdef CMD_MUSIC
   2.147  			case MUS_CMD:
   2.148  				if (!MusicCMD_Active(music_playing->data.cmd)) {
   2.149 -					music_playing = 0;
   2.150 +					return(0);
   2.151  				}
   2.152  				break;
   2.153  #endif
   2.154  #ifdef WAV_MUSIC
   2.155  			case MUS_WAV:
   2.156  				if ( ! WAVStream_Active() ) {
   2.157 -					music_playing = 0;
   2.158 +					return(0);
   2.159  				}
   2.160  				break;
   2.161  #endif
   2.162  #ifdef MOD_MUSIC
   2.163  			case MUS_MOD:
   2.164  				if ( ! Player_Active() ) {
   2.165 -					music_playing = 0;
   2.166 +					return(0);
   2.167  				}
   2.168  				break;
   2.169  #endif
   2.170  #ifdef MID_MUSIC
   2.171  			case MUS_MID:
   2.172  				if ( ! Timidity_Active() ) {
   2.173 -					music_playing = 0;
   2.174 +					return(0);
   2.175  				}
   2.176  				break;
   2.177  #endif
   2.178  #ifdef MP3_MUSIC
   2.179  			case MUS_MP3:
   2.180  				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
   2.181 -					music_playing = 0;
   2.182 +					return(0);
   2.183  				break;
   2.184  #endif
   2.185  		}
   2.186  	}
   2.187 -	return(music_playing ? 1 : 0);
   2.188 +	return(1);
   2.189 +}
   2.190 +
   2.191 +int Mix_PlayingMusic(void)
   2.192 +{
   2.193 +	int active;
   2.194 +	active = lowlevel_playing();
   2.195 +	if ( !active && music_loops ) {
   2.196 +		return(1);
   2.197 +	} else {
   2.198 +		return(active);
   2.199 +	}
   2.200  }
   2.201  
   2.202  /* Set the external music playback command */
     3.1 --- a/playmus.c	Wed Oct 27 22:49:38 1999 +0000
     3.2 +++ b/playmus.c	Sat Oct 30 00:21:34 1999 +0000
     3.3 @@ -43,20 +43,20 @@
     3.4  		Mix_FadeOutMusic(1500);
     3.5  		SDL_Delay(1500);
     3.6  	}
     3.7 +	if ( music ) {
     3.8 +		Mix_FreeMusic(music);
     3.9 +		music = NULL;
    3.10 +	}
    3.11  	if ( audio_open ) {
    3.12  		Mix_CloseAudio();
    3.13  		audio_open = 0;
    3.14  	}
    3.15 -	if ( music ) {
    3.16 -		Mix_FreeMusic(music);
    3.17 -		music = NULL;
    3.18 -	}
    3.19  	SDL_Quit();
    3.20  }
    3.21  
    3.22  void Usage(char *argv0)
    3.23  {
    3.24 -	fprintf(stderr, "Usage: %s [-8] [-r rate] [-s] <musicfile>\n", argv0);
    3.25 +	fprintf(stderr, "Usage: %s |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
    3.26  }
    3.27  	
    3.28  main(int argc, char *argv[])
    3.29 @@ -64,6 +64,7 @@
    3.30  	Uint32 audio_rate;
    3.31  	Uint16 audio_format;
    3.32  	int audio_channels;
    3.33 +	int looping = 0;
    3.34  	int i;
    3.35  
    3.36  	/* Initialize variables */
    3.37 @@ -80,6 +81,9 @@
    3.38  		if ( strcmp(argv[i], "-m") == 0 ) {
    3.39  			audio_channels = 1;
    3.40  		} else
    3.41 +		if ( strcmp(argv[i], "-l") == 0 ) {
    3.42 +			looping = -1;
    3.43 +		} else
    3.44  		if ( strcmp(argv[i], "-8") == 0 ) {
    3.45  			audio_format = AUDIO_U8;
    3.46  		} else {
    3.47 @@ -125,7 +129,7 @@
    3.48  	}
    3.49  
    3.50  	/* Play and then exit */
    3.51 -	Mix_FadeInMusic(music,0,2000);
    3.52 +	Mix_FadeInMusic(music,looping,2000);
    3.53  	while ( Mix_PlayingMusic() ) {
    3.54  		SDL_Delay(100);
    3.55  	}