- Massive bugfixes for pausing/resuming music (handled in a generic way)
authorStephane Peter <megastep@lokigames.com>
Mon, 01 Nov 1999 00:40:43 +0000
changeset 131e489d8b97d8
parent 12 f38e14b566c8
child 14 2500ac56c440
- Massive bugfixes for pausing/resuming music (handled in a generic way)
- New functions to query the paused state of either music or channels
- Added interactive mode (-i) to playmus, to aid debugging
mixer.c
mixer.h
music.c
playmus.c
     1.1 --- a/mixer.c	Sun Oct 31 14:16:07 1999 +0000
     1.2 +++ b/mixer.c	Mon Nov 01 00:40:43 1999 +0000
     1.3 @@ -731,6 +731,14 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +int Mix_Paused(int which)
     1.8 +{
     1.9 +	if ( which < 0 || which > num_channels )
    1.10 +		return(0);
    1.11 +
    1.12 +	return channel[which].paused != 0;
    1.13 +}
    1.14 +
    1.15  /* Change the group of a channel */
    1.16  int Mix_GroupChannel(int which, int tag)
    1.17  {
     2.1 --- a/mixer.h	Sun Oct 31 14:16:07 1999 +0000
     2.2 +++ b/mixer.h	Mon Nov 01 00:40:43 1999 +0000
     2.3 @@ -186,11 +186,13 @@
     2.4  /* Pause/Resume a particular channel */
     2.5  extern void Mix_Pause(int channel);
     2.6  extern void Mix_Resume(int channel);
     2.7 +extern int  Mix_Paused(int channel);
     2.8  
     2.9  /* Pause/Resume the music stream */
    2.10  extern void Mix_PauseMusic(void);
    2.11  extern void Mix_ResumeMusic(void);
    2.12  extern void Mix_RewindMusic(void);
    2.13 +extern int  Mix_PausedMusic(void);
    2.14  
    2.15  /* Check the status of a specific channel.
    2.16     If the specified channel is -1, check all channels.
     3.1 --- a/music.c	Sun Oct 31 14:16:07 1999 +0000
     3.2 +++ b/music.c	Mon Nov 01 00:40:43 1999 +0000
     3.3 @@ -54,6 +54,7 @@
     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 @@ -102,7 +103,6 @@
    3.12  /* Local low-level functions prototypes */
    3.13  static void lowlevel_halt(void);
    3.14  static int  lowlevel_play(Mix_Music *music);
    3.15 -static int  lowlevel_playing(void);
    3.16  
    3.17  /* Mixing function */
    3.18  void music_mixer(void *udata, Uint8 *stream, int len)
    3.19 @@ -116,6 +116,10 @@
    3.20  			lowlevel_halt(); /* This function sets music_playing to NULL */
    3.21  			return;
    3.22  		}	
    3.23 +		if ( music_paused ) {
    3.24 +			return;
    3.25 +		}
    3.26 +		/* Handle fading */
    3.27  		if ( !music_stopped && music_playing->fading != MIX_NO_FADING ) {
    3.28  			Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
    3.29  			if( ticks > music_playing->fade_length ) {
    3.30 @@ -142,7 +146,8 @@
    3.31  				}
    3.32  			}
    3.33  		}
    3.34 -		if ( music_loops && !lowlevel_playing() ) {
    3.35 +		/* Restart music if it has to loop */
    3.36 +		if ( music_loops && !Mix_PlayingMusic() ) {
    3.37  			if ( -- music_loops ) {
    3.38  				Mix_RewindMusic();
    3.39  				if ( lowlevel_play(music_playing) < 0 ) {
    3.40 @@ -629,6 +634,7 @@
    3.41  	music_playing = 0;
    3.42  	music_active = 0;
    3.43  	music_loops = 0;
    3.44 +	music_paused = 0;
    3.45  	music_stopped = 0;
    3.46  }
    3.47  
    3.48 @@ -671,39 +677,17 @@
    3.49  void Mix_PauseMusic(void)
    3.50  {
    3.51  	if ( music_playing && music_active && !music_stopped ) {
    3.52 -		switch ( music_playing->type ) {
    3.53 -#ifdef CMD_MUSIC
    3.54 -		case MUS_CMD:
    3.55 -			MusicCMD_Pause(music_playing->data.cmd);
    3.56 -			break;
    3.57 -#endif
    3.58 -#ifdef MP3_MUSIC
    3.59 -		case MUS_MP3:
    3.60 -			SMPEG_pause(music_playing->data.mp3);
    3.61 -			break;
    3.62 -#endif
    3.63 -		}
    3.64 +		music_active = 0;
    3.65 +		music_paused = 1;
    3.66  	}
    3.67 -	music_active = 0;
    3.68  }
    3.69  
    3.70  void Mix_ResumeMusic(void)
    3.71  {
    3.72  	if ( music_playing && !music_active && !music_stopped ) {
    3.73 -		switch ( music_playing->type ) {
    3.74 -#ifdef CMD_MUSIC
    3.75 -		case MUS_CMD:
    3.76 -			MusicCMD_Resume(music_playing->data.cmd);
    3.77 -			break;
    3.78 -#endif
    3.79 -#ifdef MP3_MUSIC
    3.80 -		case MUS_MP3:
    3.81 -			SMPEG_pause(music_playing->data.mp3);
    3.82 -			break;
    3.83 -#endif
    3.84 -		}
    3.85 +		music_active = 1;
    3.86 +		music_paused = 0;
    3.87  	}
    3.88 -	music_active = 1;
    3.89  }
    3.90  
    3.91  void Mix_RewindMusic(void)
    3.92 @@ -720,10 +704,15 @@
    3.93  	}
    3.94  }
    3.95  
    3.96 +int Mix_PausedMusic(void)
    3.97 +{
    3.98 +	return music_paused;
    3.99 +}
   3.100 +
   3.101  /* Check the status of the music */
   3.102 -static int lowlevel_playing(void)
   3.103 +int Mix_PlayingMusic(void)
   3.104  {
   3.105 -	if ( music_playing && !music_stopped ) {
   3.106 +	if ( music_playing && ! music_stopped ) {
   3.107  		switch (music_playing->type) {
   3.108  #ifdef CMD_MUSIC
   3.109  			case MUS_CMD:
   3.110 @@ -760,19 +749,9 @@
   3.111  				break;
   3.112  #endif
   3.113  		}
   3.114 +		return(1);
   3.115  	}
   3.116 -	return(1);
   3.117 -}
   3.118 -
   3.119 -int Mix_PlayingMusic(void)
   3.120 -{
   3.121 -	int active;
   3.122 -	active = lowlevel_playing();
   3.123 -	if ( !active && music_loops ) {
   3.124 -		return(1);
   3.125 -	} else {
   3.126 -		return(active);
   3.127 -	}
   3.128 +	return(0);
   3.129  }
   3.130  
   3.131  /* Set the external music playback command */
     4.1 --- a/playmus.c	Sun Oct 31 14:16:07 1999 +0000
     4.2 +++ b/playmus.c	Mon Nov 01 00:40:43 1999 +0000
     4.3 @@ -56,15 +56,38 @@
     4.4  
     4.5  void Usage(char *argv0)
     4.6  {
     4.7 -	fprintf(stderr, "Usage: %s |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
     4.8 +	fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
     4.9  }
    4.10 -	
    4.11 +
    4.12 +void Menu(void)
    4.13 +{
    4.14 +	char buf[10];
    4.15 +
    4.16 +	printf("Available commands: (p)ause (r)esume (h)alt > ");
    4.17 +	fflush(stdin);
    4.18 +	scanf("%s",buf);
    4.19 +	switch(buf[0]){
    4.20 +	case 'p': case 'P':
    4.21 +		Mix_PauseMusic();
    4.22 +		break;
    4.23 +	case 'r': case 'R':
    4.24 +		Mix_ResumeMusic();
    4.25 +		break;
    4.26 +	case 'h': case 'H':
    4.27 +		Mix_HaltMusic();
    4.28 +		break;
    4.29 +	}
    4.30 +	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    4.31 +		   Mix_PausedMusic() ? "yes" : "no");
    4.32 +}
    4.33 +
    4.34  main(int argc, char *argv[])
    4.35  {
    4.36  	Uint32 audio_rate;
    4.37  	Uint16 audio_format;
    4.38  	int audio_channels;
    4.39  	int looping = 0;
    4.40 +	int interactive = 0;
    4.41  	int i;
    4.42  
    4.43  	/* Initialize variables */
    4.44 @@ -84,6 +107,9 @@
    4.45  		if ( strcmp(argv[i], "-l") == 0 ) {
    4.46  			looping = -1;
    4.47  		} else
    4.48 +		if ( strcmp(argv[i], "-i") == 0 ) {
    4.49 +			interactive = 1;
    4.50 +		} else
    4.51  		if ( strcmp(argv[i], "-8") == 0 ) {
    4.52  			audio_format = AUDIO_U8;
    4.53  		} else {
    4.54 @@ -130,8 +156,11 @@
    4.55  
    4.56  	/* Play and then exit */
    4.57  	Mix_FadeInMusic(music,looping,2000);
    4.58 -	while ( Mix_PlayingMusic() ) {
    4.59 -		SDL_Delay(100);
    4.60 +	while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
    4.61 +		if(interactive)
    4.62 +			Menu();
    4.63 +		else
    4.64 +			SDL_Delay(100);
    4.65  	}
    4.66  	exit(0);
    4.67  }