Skip to content

Commit

Permalink
- Massive bugfixes for pausing/resuming music (handled in a generic way)
Browse files Browse the repository at this point in the history
- New functions to query the paused state of either music or channels
- Added interactive mode (-i) to playmus, to aid debugging
  • Loading branch information
Stephane Peter committed Nov 1, 1999
1 parent 54536d7 commit ec617e7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
8 changes: 8 additions & 0 deletions mixer.c
Expand Up @@ -731,6 +731,14 @@ void Mix_Resume(int which)
}
}

int Mix_Paused(int which)
{
if ( which < 0 || which > num_channels )
return(0);

return channel[which].paused != 0;
}

/* Change the group of a channel */
int Mix_GroupChannel(int which, int tag)
{
Expand Down
2 changes: 2 additions & 0 deletions mixer.h
Expand Up @@ -186,11 +186,13 @@ extern Mix_Fading Mix_FadingChannel(int which);
/* Pause/Resume a particular channel */
extern void Mix_Pause(int channel);
extern void Mix_Resume(int channel);
extern int Mix_Paused(int channel);

/* Pause/Resume the music stream */
extern void Mix_PauseMusic(void);
extern void Mix_ResumeMusic(void);
extern void Mix_RewindMusic(void);
extern int Mix_PausedMusic(void);

/* Check the status of a specific channel.
If the specified channel is -1, check all channels.
Expand Down
61 changes: 20 additions & 41 deletions music.c
Expand Up @@ -54,6 +54,7 @@ static SDL_AudioSpec used_mixer;

int music_active = 1;
static int music_stopped = 0;
static int music_paused = 0;
static int music_loops = 0;
static char *music_cmd = NULL;
static int samplesize;
Expand Down Expand Up @@ -102,7 +103,6 @@ static int timidity_ok;
/* Local low-level functions prototypes */
static void lowlevel_halt(void);
static int lowlevel_play(Mix_Music *music);
static int lowlevel_playing(void);

/* Mixing function */
void music_mixer(void *udata, Uint8 *stream, int len)
Expand All @@ -116,6 +116,10 @@ void music_mixer(void *udata, Uint8 *stream, int len)
lowlevel_halt(); /* This function sets music_playing to NULL */
return;
}
if ( music_paused ) {
return;
}
/* Handle fading */
if ( !music_stopped && music_playing->fading != MIX_NO_FADING ) {
Uint32 ticks = SDL_GetTicks() - music_playing->ticks_fade;
if( ticks > music_playing->fade_length ) {
Expand All @@ -142,7 +146,8 @@ void music_mixer(void *udata, Uint8 *stream, int len)
}
}
}
if ( music_loops && !lowlevel_playing() ) {
/* Restart music if it has to loop */
if ( music_loops && !Mix_PlayingMusic() ) {
if ( -- music_loops ) {
Mix_RewindMusic();
if ( lowlevel_play(music_playing) < 0 ) {
Expand Down Expand Up @@ -629,6 +634,7 @@ static void lowlevel_halt(void)
music_playing = 0;
music_active = 0;
music_loops = 0;
music_paused = 0;
music_stopped = 0;
}

Expand Down Expand Up @@ -671,39 +677,17 @@ Mix_Fading Mix_FadingMusic(void)
void Mix_PauseMusic(void)
{
if ( music_playing && music_active && !music_stopped ) {
switch ( music_playing->type ) {
#ifdef CMD_MUSIC
case MUS_CMD:
MusicCMD_Pause(music_playing->data.cmd);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_pause(music_playing->data.mp3);
break;
#endif
}
music_active = 0;
music_paused = 1;
}
music_active = 0;
}

void Mix_ResumeMusic(void)
{
if ( music_playing && !music_active && !music_stopped ) {
switch ( music_playing->type ) {
#ifdef CMD_MUSIC
case MUS_CMD:
MusicCMD_Resume(music_playing->data.cmd);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_pause(music_playing->data.mp3);
break;
#endif
}
music_active = 1;
music_paused = 0;
}
music_active = 1;
}

void Mix_RewindMusic(void)
Expand All @@ -720,10 +704,15 @@ void Mix_RewindMusic(void)
}
}

int Mix_PausedMusic(void)
{
return music_paused;
}

/* Check the status of the music */
static int lowlevel_playing(void)
int Mix_PlayingMusic(void)
{
if ( music_playing && !music_stopped ) {
if ( music_playing && ! music_stopped ) {
switch (music_playing->type) {
#ifdef CMD_MUSIC
case MUS_CMD:
Expand Down Expand Up @@ -760,19 +749,9 @@ static int lowlevel_playing(void)
break;
#endif
}
}
return(1);
}

int Mix_PlayingMusic(void)
{
int active;
active = lowlevel_playing();
if ( !active && music_loops ) {
return(1);
} else {
return(active);
}
return(0);
}

/* Set the external music playback command */
Expand Down
37 changes: 33 additions & 4 deletions playmus.c
Expand Up @@ -56,15 +56,38 @@ void CleanUp(void)

void Usage(char *argv0)
{
fprintf(stderr, "Usage: %s |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
}


void Menu(void)
{
char buf[10];

printf("Available commands: (p)ause (r)esume (h)alt > ");
fflush(stdin);
scanf("%s",buf);
switch(buf[0]){
case 'p': case 'P':
Mix_PauseMusic();
break;
case 'r': case 'R':
Mix_ResumeMusic();
break;
case 'h': case 'H':
Mix_HaltMusic();
break;
}
printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no",
Mix_PausedMusic() ? "yes" : "no");
}

main(int argc, char *argv[])
{
Uint32 audio_rate;
Uint16 audio_format;
int audio_channels;
int looping = 0;
int interactive = 0;
int i;

/* Initialize variables */
Expand All @@ -84,6 +107,9 @@ main(int argc, char *argv[])
if ( strcmp(argv[i], "-l") == 0 ) {
looping = -1;
} else
if ( strcmp(argv[i], "-i") == 0 ) {
interactive = 1;
} else
if ( strcmp(argv[i], "-8") == 0 ) {
audio_format = AUDIO_U8;
} else {
Expand Down Expand Up @@ -130,8 +156,11 @@ main(int argc, char *argv[])

/* Play and then exit */
Mix_FadeInMusic(music,looping,2000);
while ( Mix_PlayingMusic() ) {
SDL_Delay(100);
while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
if(interactive)
Menu();
else
SDL_Delay(100);
}
exit(0);
}

0 comments on commit ec617e7

Please sign in to comment.