Listing 10-2. Using the stereo_pan Function tank.explode() # Do explosion visual explosion_channel = explosion_sound.play() if explosion_channel is not None:
left, right = stereo_pan(tank.position.x, SCREEN_SIZE) explosion_channel.set_volume(left, right)
Figure 10-4. Setting the stereo panning of an explosion
Generally it is best to leave the task of selecting a channel to Pygame, but it is possible to force a Sound object to play through a particular channel by calling the play method of the Channel object, which takes the sound you want to play followed by the number of times you want it to repeat and the maximum time you want it to play. One reason to do this is to reserve one or more channels for high-priority sounds. For instance, you might not want background ambient noise to block the player's gunfire. To reserve a number of channels, call the pygame.mixer.set_reserved function, which prevents a number of channels from being considered by the Sound.play method. For instance, if you call pygame.mixer.set_serserved(2), Pygame will not select channel 0 or 1 when calling play from a Sound object. Listing 10-3 shows how to reserve the first two channels.
Listing 10-3. Reserving Channels pygame.mixer.set_reserved(2) reserved_channel_0 = pygame.mixer.Channel(o) reserved_channel_1 = pygame.mixer.Channel(l)
Here's how you would force a sound to be played through one of the reserved channels:
See Table 10-2 for a complete list of the methods of Channel objects.
Table 10-2. Methods of Channel Objects
Purpose fadeout Fades out (reduces volume) a sound over a period of time, given in milliseconds.
get_busy Returns True if a sound is currently playing on the channel.
get_endevent Returns the event that will be sent when a sound finishes playing, or NOEVENT if there is no end event set.
get_queue Returns any sound that is queued for playback, or None if there is no queued sound.
get_volume Retrieves the current volume of the channel as a single value between 0.0 and 1.0 (does not take into account the stereo volume set by set_volume).
pause Temporarily pauses playback of any sound on this channel.
play Plays a sound on a specific channel. Takes a Sound object and optional values for looping and max time, which have the same meaning as Sound. play.
queue Plays a given sound when the current sound has finished. Takes the Sound object to be queued.
set_endevent Requests an event when the current sound has finished playing. Takes the id of the event to be sent, which should be above USEREVENT (a constant in pygame.locals), to avoid conflict with existing events. If no parameter is given, Pygame will stop sending end events.
set_volume Sets the volume of this channel. If one value is given, it is used for both speakers. If two values are given, the left and right speaker volume is set independently. Both methods take the volume as a value between 0.0 and 1.0, where 0.0 is silent and 1.0 is the maximum volume.
Method Purpose stop Instantly stops playback of any sound on the channel.
unpause Resumes playback of a channel that has been paused.
Was this article helpful?