music.c
changeset 10 2fbd0414a1fc
parent 7 e73da9dc4bdc
child 11 3939a99bf422
     1.1 --- a/music.c	Wed Oct 27 22:49:38 1999 +0000
     1.2 +++ b/music.c	Sat Oct 30 00:21:34 1999 +0000
     1.3 @@ -62,6 +62,10 @@
     1.4  static int music_swap8;
     1.5  static int music_swap16;
     1.6  
     1.7 +/* For fading/chaining musics */
     1.8 +static Mix_Music *next_music = 0;
     1.9 +static int next_period = 0, next_loops = 0;
    1.10 +
    1.11  struct _Mix_Music {
    1.12  	enum {
    1.13  		MUS_CMD,
    1.14 @@ -95,7 +99,10 @@
    1.15  };
    1.16  static int timidity_ok;
    1.17  
    1.18 +/* Local low-level functions prototypes */
    1.19  static void lowlevel_halt(void);
    1.20 +static int  lowlevel_play(Mix_Music *music);
    1.21 +static int  lowlevel_playing(void);
    1.22  
    1.23  /* Mixing function */
    1.24  void music_mixer(void *udata, Uint8 *stream, int len)
    1.25 @@ -109,13 +116,26 @@
    1.26  			lowlevel_halt(); /* This function sets music_playing to NULL */
    1.27  			return;
    1.28  		}	
    1.29 -		if ( music_playing->fading != MIX_NO_FADING ) {
    1.30 +		if ( music_playing && music_loops && !lowlevel_playing() ) {
    1.31 +			if ( -- music_loops ) {
    1.32 +				Mix_RewindMusic();
    1.33 +				if ( lowlevel_play(music_playing) < 0 )
    1.34 +					music_stopped = 1; /* Something went wrong */
    1.35 +			}
    1.36 +		}
    1.37 +		if ( !music_stopped && music_playing->fading != MIX_NO_FADING ) {
    1.38  			Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
    1.39  			if( ticks > music_playing->fade_length ) {
    1.40  				if ( music_playing->fading == MIX_FADING_OUT ) {
    1.41  					music_volume = music_playing->fade_volume;
    1.42  					music_playing->fading = MIX_NO_FADING;
    1.43 -					lowlevel_halt();
    1.44 +					if (next_music) {
    1.45 +						Mix_FadeInMusic(next_music,next_loops,next_period);
    1.46 +						next_music = 0;
    1.47 +						next_period = next_loops = 0;
    1.48 +					} else {
    1.49 +						lowlevel_halt();
    1.50 +					}
    1.51  					return;
    1.52  				}
    1.53  				music_playing->fading = MIX_NO_FADING;
    1.54 @@ -418,18 +438,11 @@
    1.55  	}
    1.56  }
    1.57  
    1.58 -/* Play a music chunk.  Returns 0, or -1 if there was an error.
    1.59 -*/
    1.60 -int Mix_PlayMusic(Mix_Music *music, int loops)
    1.61 +static int lowlevel_play(Mix_Music *music)
    1.62  {
    1.63 -	/* Don't play null pointers :-) */
    1.64 -	if ( music == NULL ) {
    1.65 +	if(!music)
    1.66  		return(-1);
    1.67 -	}
    1.68 -	/* If the current music is fading out, wait for the fade to complete */
    1.69 -	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
    1.70 -		SDL_Delay(100);
    1.71 -	}
    1.72 +
    1.73  	switch (music->type) {
    1.74  #ifdef CMD_MUSIC
    1.75  		case MUS_CMD:
    1.76 @@ -461,7 +474,6 @@
    1.77  			SMPEG_enableaudio(music->data.mp3,1);
    1.78  			SMPEG_enablevideo(music->data.mp3,0);
    1.79  			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
    1.80 -			SMPEG_loop(music->data.mp3, loops);
    1.81  			SMPEG_play(music->data.mp3);
    1.82  			break;
    1.83  #endif
    1.84 @@ -469,8 +481,28 @@
    1.85  			/* Unknown music type?? */
    1.86  			return(-1);
    1.87  	}
    1.88 +	return(0);
    1.89 +}
    1.90 +
    1.91 +/* Play a music chunk.  Returns 0, or -1 if there was an error.
    1.92 +*/
    1.93 +int Mix_PlayMusic(Mix_Music *music, int loops)
    1.94 +{
    1.95 +	/* Don't play null pointers :-) */
    1.96 +	if ( music == NULL ) {
    1.97 +		return(-1);
    1.98 +	}
    1.99 +	/* If the current music is fading out, wait for the fade to complete */
   1.100 +	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
   1.101 +		SDL_Delay(100);
   1.102 +	}
   1.103 +
   1.104 +	if(lowlevel_play(music)<0)
   1.105 +		return(-1);
   1.106 +
   1.107  	music_active = 1;
   1.108  	music_stopped = 0;
   1.109 +	music_loops = loops;
   1.110  	music_playing = music;
   1.111  	music_playing->fading = MIX_NO_FADING;
   1.112  	return(0);
   1.113 @@ -491,6 +523,23 @@
   1.114  	return(0);
   1.115  }
   1.116  
   1.117 +/* Fades out the currently playing music, and progressively fades in a new music,
   1.118 +   all in the background. The 'ms' period is half for fading out the music and
   1.119 +   fading in the new one */
   1.120 +int Mix_FadeOutInMusic(Mix_Music *music, int loops, int ms)
   1.121 +{
   1.122 +	if( music_stopped || !music_playing ) {
   1.123 +		return Mix_FadeInMusic(music,loops,ms/2);
   1.124 +	} else {
   1.125 +		/* Set up the data for chaining the next music */
   1.126 +		next_music = music;
   1.127 +		next_period = ms/2;
   1.128 +		next_loops = loops;
   1.129 +		/* Fade out the current music */
   1.130 +		Mix_FadeOutMusic(ms/2);
   1.131 +	}
   1.132 +}
   1.133 +
   1.134  /* Set the music volume */
   1.135  int Mix_VolumeMusic(int volume)
   1.136  {
   1.137 @@ -668,47 +717,58 @@
   1.138  }
   1.139  
   1.140  /* Check the status of the music */
   1.141 -int Mix_PlayingMusic(void)
   1.142 +static int lowlevel_playing(void)
   1.143  {
   1.144  	if ( music_playing && !music_stopped ) {
   1.145  		switch (music_playing->type) {
   1.146  #ifdef CMD_MUSIC
   1.147  			case MUS_CMD:
   1.148  				if (!MusicCMD_Active(music_playing->data.cmd)) {
   1.149 -					music_playing = 0;
   1.150 +					return(0);
   1.151  				}
   1.152  				break;
   1.153  #endif
   1.154  #ifdef WAV_MUSIC
   1.155  			case MUS_WAV:
   1.156  				if ( ! WAVStream_Active() ) {
   1.157 -					music_playing = 0;
   1.158 +					return(0);
   1.159  				}
   1.160  				break;
   1.161  #endif
   1.162  #ifdef MOD_MUSIC
   1.163  			case MUS_MOD:
   1.164  				if ( ! Player_Active() ) {
   1.165 -					music_playing = 0;
   1.166 +					return(0);
   1.167  				}
   1.168  				break;
   1.169  #endif
   1.170  #ifdef MID_MUSIC
   1.171  			case MUS_MID:
   1.172  				if ( ! Timidity_Active() ) {
   1.173 -					music_playing = 0;
   1.174 +					return(0);
   1.175  				}
   1.176  				break;
   1.177  #endif
   1.178  #ifdef MP3_MUSIC
   1.179  			case MUS_MP3:
   1.180  				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
   1.181 -					music_playing = 0;
   1.182 +					return(0);
   1.183  				break;
   1.184  #endif
   1.185  		}
   1.186  	}
   1.187 -	return(music_playing ? 1 : 0);
   1.188 +	return(1);
   1.189 +}
   1.190 +
   1.191 +int Mix_PlayingMusic(void)
   1.192 +{
   1.193 +	int active;
   1.194 +	active = lowlevel_playing();
   1.195 +	if ( !active && music_loops ) {
   1.196 +		return(1);
   1.197 +	} else {
   1.198 +		return(active);
   1.199 +	}
   1.200  }
   1.201  
   1.202  /* Set the external music playback command */