[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Re: Check Sound status in Pygame
It seems you need to do your own channel management. It is easiest show
with a code sample. Note this is untested.
# A queue of channels from oldest to newest
channel_queue = []
def play_sound(sound):
"""Play a sound
If all channels are busy then reuse the channel that was playing the
longest.
"""
for i, c in enumerate(channel_queue):
if not c.get_busy():
channel = channel_queue.pop(i)
break
else:
if len(channel_queue) < pygame.mixer.get_num_channels():
channel = Channel(len(channel_queue))
else:
channel = channel_queue.pop(0)
channel.play(sound)
channel_queue.append(channel)
Lenard
Wyatt Olson wrote:
OK, I found the method get_num_channels which seems to do this.
However, I now need to be able to stop playing oldest sounds if I
wish, to clear out space for other ones. For instance, assume that I
have 16 channels defined. I am trying to play 30 notes on the ride
cymbal in a 5 second interval. The ride cymbal sample is 10 seconds
long. Without stopping sounds, once I play the sample 16 times, I
cannot play any more until one of the samples finished playing. What
I would like to do is check how many samples are playing, and if the
count is greater than some threshold, stop the oldest instance of the
playing sound. I can't seem to find any way to stop a single
*instance* of a sound - if you call stop() or fadeout(), all instances
of the sound will be stopped. I realize that this requirement is a
bit more unique for my application, but I imagine that manipulating
individual instances of existing sounds can be useful to anyone.
Please let me know if I am missing something else which should be
obvious to me.
Cheers
Wyatt Olson wrote:
Hello all,
I am developing a drum sequencer program using Pygame, and have a
(hopefully easy) architectural question for you. How can you tell if
a Sound object is currently playing? (On any channel - I don't care
where it is playing, I just want to know if it is). There is no
'is_playing()' or similar method which I could find. I imagine that
this is a pretty common request, so I must be just missing something.
Thanks!