Skip to content

Commit

Permalink
Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
Browse files Browse the repository at this point in the history
 * Added Mix_ChannelFinished() and Mix_GetChunk()
  • Loading branch information
Sam Lantinga committed Jun 10, 2001
1 parent f762ee5 commit 3309045
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,5 +1,7 @@

1.2.1:
Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
* Added Mix_ChannelFinished() and Mix_GetChunk()
Ryan C. Gordon - Tue, 5 Jun 2001 11:01:51
* Added VOC sound file support
Guillaume Cottenceau - Thu May 10 11:17:55 PDT 2001
Expand Down
10 changes: 10 additions & 0 deletions SDL_mixer.h
Expand Up @@ -123,6 +123,11 @@ extern DECLSPEC void Mix_HookMusicFinished(void (*music_finished)(void));
/* Get a pointer to the user data for the current music hook */
extern DECLSPEC void *Mix_GetMusicHookData(void);

/* Add your own callback when a channel has finished playing. NULL
* to disable callback.
*/
extern DECLSPEC void Mix_ChannelFinished(void (*channel_finished)(int channel));

/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
them dynamically to the next sample if requested with a -1 value below.
Returns the number of reserved channels.
Expand Down Expand Up @@ -219,6 +224,11 @@ extern DECLSPEC int Mix_PlayingMusic(void);
/* Stop music and set external music playback command */
extern DECLSPEC int Mix_SetMusicCMD(const char *command);

/* Get the Mix_Chunk currently associated with a mixer channel
Returns NULL if it's an invalid channel, or there's no chunk associated.
*/
extern DECLSPEC Mix_Chunk *Mix_GetChunk(int channel);

/* Close the mixer, halting all playing audio */
extern DECLSPEC void Mix_CloseAudio(void);

Expand Down
28 changes: 28 additions & 0 deletions mixer.c
Expand Up @@ -61,6 +61,9 @@ static int reserved_channels = 0;
static void (*mix_postmix)(void *udata, Uint8 *stream, int len) = NULL;
static void *mix_postmix_data = NULL;

/* rcg07062001 callback to alert when channels are done playing. */
static void (*channel_done_callback)(int channel) = NULL;

/* Music function declarations */
extern int open_music(SDL_AudioSpec *mixer);
extern void close_music(void);
Expand Down Expand Up @@ -139,6 +142,11 @@ static void mix_channels(void *udata, Uint8 *stream, int len)
mix_channel[i].playing = mix_channel[i].chunk->alen;
}
}

/* rcg06072001 Alert app if channel is done playing. */
if ( (!mix_channel[i].playing) && (channel_done_callback) ) {
channel_done_callback(i);
}
}
}
}
Expand Down Expand Up @@ -464,6 +472,14 @@ void *Mix_GetMusicHookData(void)
return(music_data);
}

void Mix_ChannelFinished(void (*channel_finished)(int channel))
{
SDL_LockAudio();
channel_done_callback = channel_finished;
SDL_UnlockAudio();
}


/* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
them dynamically to the next sample if requested with a -1 value below.
Returns the number of reserved channels.
Expand Down Expand Up @@ -739,6 +755,18 @@ int Mix_Playing(int which)
return(status);
}

/* rcg06072001 Get the chunk associated with a channel. */
Mix_Chunk *Mix_GetChunk(int channel)
{
Mix_Chunk *retval = NULL;

if ((channel >= 0) && (channel < num_channels)) {
retval = mix_channel[channel].chunk;
}

return(retval);
}

/* Close the mixer, halting all playing audio */
void Mix_CloseAudio(void)
{
Expand Down
30 changes: 29 additions & 1 deletion playwave.c
Expand Up @@ -54,7 +54,22 @@ void Usage(char *argv0)
{
fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
}



/*#define TEST_MIX_CHANNELFINISHED*/
#ifdef TEST_MIX_CHANNELFINISHED /* rcg06072001 */
static volatile int channel_is_done = 0;
static void channel_complete_callback(int chan)
{
Mix_Chunk *done_chunk = Mix_GetChunk(chan);
printf("We were just alerted that Mixer channel #%d is done.\n", chan);
printf("Channel's chunk pointer is (%p).\n", done_chunk);
printf(" Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT");
channel_is_done = 1;
}
#endif


main(int argc, char *argv[])
{
int audio_rate;
Expand Down Expand Up @@ -126,10 +141,23 @@ main(int argc, char *argv[])
return(2);
}

#ifdef TEST_MIX_CHANNELFINISHED /* rcg06072001 */
setbuf(stdout, NULL);
Mix_ChannelFinished(channel_complete_callback);
#endif

/* Play and then exit */
Mix_PlayChannel(0, wave, loops);

#ifdef TEST_MIX_CHANNELFINISHED /* rcg06072001 */
while (!channel_is_done) {
SDL_Delay(100);
}
#else
while ( Mix_Playing(0) ) {
SDL_Delay(100);
}
#endif

return(0);
}

0 comments on commit 3309045

Please sign in to comment.