Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
authorSam Lantinga
Sun, 10 Jun 2001 04:48:15 +0000
changeset 922a30d721bd79
parent 91 12164e8e7faf
child 93 a25e6a225de8
Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
* Added Mix_ChannelFinished() and Mix_GetChunk()
CHANGES
SDL_mixer.h
mixer.c
playwave.c
     1.1 --- a/CHANGES	Sun Jun 10 04:44:45 2001 +0000
     1.2 +++ b/CHANGES	Sun Jun 10 04:48:15 2001 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  
     1.5  1.2.1:
     1.6 +Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
     1.7 + * Added Mix_ChannelFinished() and Mix_GetChunk()
     1.8  Ryan C. Gordon - Tue, 5 Jun 2001 11:01:51
     1.9   * Added VOC sound file support
    1.10  Guillaume Cottenceau - Thu May 10 11:17:55 PDT 2001
     2.1 --- a/SDL_mixer.h	Sun Jun 10 04:44:45 2001 +0000
     2.2 +++ b/SDL_mixer.h	Sun Jun 10 04:48:15 2001 +0000
     2.3 @@ -123,6 +123,11 @@
     2.4  /* Get a pointer to the user data for the current music hook */
     2.5  extern DECLSPEC void *Mix_GetMusicHookData(void);
     2.6  
     2.7 +/* Add your own callback when a channel has finished playing. NULL
     2.8 + * to disable callback.
     2.9 + */
    2.10 +extern DECLSPEC void Mix_ChannelFinished(void (*channel_finished)(int channel));
    2.11 +
    2.12  /* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
    2.13     them dynamically to the next sample if requested with a -1 value below.
    2.14     Returns the number of reserved channels.
    2.15 @@ -219,6 +224,11 @@
    2.16  /* Stop music and set external music playback command */
    2.17  extern DECLSPEC int Mix_SetMusicCMD(const char *command);
    2.18  
    2.19 +/* Get the Mix_Chunk currently associated with a mixer channel
    2.20 +    Returns NULL if it's an invalid channel, or there's no chunk associated.
    2.21 +*/
    2.22 +extern DECLSPEC Mix_Chunk *Mix_GetChunk(int channel);
    2.23 +
    2.24  /* Close the mixer, halting all playing audio */
    2.25  extern DECLSPEC void Mix_CloseAudio(void);
    2.26  
     3.1 --- a/mixer.c	Sun Jun 10 04:44:45 2001 +0000
     3.2 +++ b/mixer.c	Sun Jun 10 04:48:15 2001 +0000
     3.3 @@ -61,6 +61,9 @@
     3.4  static void (*mix_postmix)(void *udata, Uint8 *stream, int len) = NULL;
     3.5  static void *mix_postmix_data = NULL;
     3.6  
     3.7 +/* rcg07062001 callback to alert when channels are done playing. */
     3.8 +static void (*channel_done_callback)(int channel) = NULL;
     3.9 +
    3.10  /* Music function declarations */
    3.11  extern int open_music(SDL_AudioSpec *mixer);
    3.12  extern void close_music(void);
    3.13 @@ -139,6 +142,11 @@
    3.14  						mix_channel[i].playing = mix_channel[i].chunk->alen;
    3.15  					}
    3.16  				}
    3.17 +
    3.18 +				/* rcg06072001 Alert app if channel is done playing. */
    3.19 +				if ( (!mix_channel[i].playing) && (channel_done_callback) ) {
    3.20 +					channel_done_callback(i);
    3.21 +				}
    3.22  			}
    3.23  		}
    3.24  	}
    3.25 @@ -464,6 +472,14 @@
    3.26  	return(music_data);
    3.27  }
    3.28  
    3.29 +void Mix_ChannelFinished(void (*channel_finished)(int channel))
    3.30 +{
    3.31 +    SDL_LockAudio();
    3.32 +    channel_done_callback = channel_finished;
    3.33 +    SDL_UnlockAudio();
    3.34 +}
    3.35 +
    3.36 +
    3.37  /* Reserve the first channels (0 -> n-1) for the application, i.e. don't allocate
    3.38     them dynamically to the next sample if requested with a -1 value below.
    3.39     Returns the number of reserved channels.
    3.40 @@ -739,6 +755,18 @@
    3.41  	return(status);
    3.42  }
    3.43  
    3.44 +/* rcg06072001 Get the chunk associated with a channel. */
    3.45 +Mix_Chunk *Mix_GetChunk(int channel)
    3.46 +{
    3.47 +	Mix_Chunk *retval = NULL;
    3.48 +
    3.49 +	if ((channel >= 0) && (channel < num_channels)) {
    3.50 +		retval = mix_channel[channel].chunk;
    3.51 +	}
    3.52 +
    3.53 +	return(retval);
    3.54 +}
    3.55 +
    3.56  /* Close the mixer, halting all playing audio */
    3.57  void Mix_CloseAudio(void)
    3.58  {
     4.1 --- a/playwave.c	Sun Jun 10 04:44:45 2001 +0000
     4.2 +++ b/playwave.c	Sun Jun 10 04:48:15 2001 +0000
     4.3 @@ -54,7 +54,22 @@
     4.4  {
     4.5  	fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
     4.6  }
     4.7 -	
     4.8 +
     4.9 +
    4.10 +/*#define TEST_MIX_CHANNELFINISHED*/
    4.11 +#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
    4.12 +static volatile int channel_is_done = 0;
    4.13 +static void channel_complete_callback(int chan)
    4.14 +{
    4.15 +	Mix_Chunk *done_chunk = Mix_GetChunk(chan);
    4.16 +	printf("We were just alerted that Mixer channel #%d is done.\n", chan);
    4.17 +	printf("Channel's chunk pointer is (%p).\n", done_chunk);
    4.18 +	printf(" Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT");
    4.19 +	channel_is_done = 1;
    4.20 +}
    4.21 +#endif
    4.22 +
    4.23 +
    4.24  main(int argc, char *argv[])
    4.25  {
    4.26  	int audio_rate;
    4.27 @@ -126,10 +141,23 @@
    4.28  		return(2);
    4.29  	}
    4.30  
    4.31 +#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
    4.32 +	setbuf(stdout, NULL);
    4.33 +	Mix_ChannelFinished(channel_complete_callback);
    4.34 +#endif
    4.35 +
    4.36  	/* Play and then exit */
    4.37  	Mix_PlayChannel(0, wave, loops);
    4.38 +
    4.39 +#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
    4.40 +	while (!channel_is_done) {
    4.41 +		SDL_Delay(100);
    4.42 +	}
    4.43 +#else
    4.44  	while ( Mix_Playing(0) ) {
    4.45  		SDL_Delay(100);
    4.46  	}
    4.47 +#endif
    4.48 +
    4.49  	return(0);
    4.50  }