[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Confusing values from mixer.Channel.get_sound()



I actually was aware that such reuse could take place, so I did check if they point to the same object. But in my case, those reprs don't give the same output as in your case.

Here's my updated code and output:

def channel_test_2():
    print('Play the sound on channel1')
    channel1 = dog.play()
    print('channel1=', channel1)
    sleep(1)
    print('After 1 second: channel1 - ', channel1.get_sound() == dog)
    sleep(6)
    print('After 7 seconds: channel1 - ', channel1.get_sound() == dog)
    print('Play the sound on channel2')
    channel2 = dog.play()
    print('channel2=', channel2)
    sleep(1)
    print('After 1 seconds: channel1 - ', channel1.get_sound() == dog, ', channel2 - ', channel2.get_sound() == dog)
    sleep(3)
    print('After 4 seconds: channel1 - ', channel1.get_sound() == dog, ', channel2 - ', channel2.get_sound() == dog)
    sleep(3)
    print('After 7 seconds: channel1 - ', channel1.get_sound() == dog, ', channel2 - ', channel2.get_sound() == dog)
    print('channel1=', channel1)
    print('channel2=', channel2)
    print('Are they the same?', channel1 == channel2)

Play the sound on channel1
channel1= <Channel object at 0xb642bfb0>
After 1 second: channel1 -  True
After 7 seconds: channel1 -  False
Play the sound on channel2
channel2= <Channel object at 0xb5563450>
After 1 seconds: channel1 -  True , channel2 -  True
After 4 seconds: channel1 -  True , channel2 -  True
After 7 seconds: channel1 -  False , channel2 -  False
channel1= <Channel object at 0xb642bfb0>
channel2= <Channel object at 0xb5563450>
Are they the same? False

As you can see, in my case the channels point to different addresses, but still behave as if they are linked. Maybe it is important on what platform I am running this code?
My platform:
- Raspberry Pi Zero W
- Raspbian GNU/Linux 10 (buster)
- Python 3.7.3
- pygame 2.0.0 (SDL 2.0.9)

I am aware of the channel end_event but since the number of custom event IDs is very limited, I had to implement my own solution (a wrapper class for the Channel object) for executing a function when the sound has finished playing, using a timer object, set to sound.get_length(). I have now implemented extra property in my wrapper class that tracks if the sound is currently playing. This works fine, but it still remains a mystery, why those channels behave as they do, even though they point to different addresses (in my case).

On Sat, Dec 26, 2020 at 2:49 AM Greg Ewing <greg.ewing@xxxxxxxxxxxxxxxx> wrote:
On 26/12/20 3:38 am, Pavils Jurjans wrote:
> I first play a sound (get the channel object in return), and let it play
> until the end. channel.get_sound() returns None, as it should. Then I
> play this sound a second time (storing the returned channel object in
> another variable). While the sound is still playing, if I query the
> first channel.get_sound(), I get the reference to the sound object.

Because you started playing the second sound after the first one
finished, it re-used the same channel.

I added a couple of lines to print out the repr of the sound
object returned by each play() call. This is what I got:

Play the sound on channel1
channel1 = <Channel object at 0x10ddf1030>
After 1 second: channel1 -  True
After 7 seconds: channel1 -  False
Play the sound on channel2
channel2 = <Channel object at 0x10ddf1030>
After 1 seconds: channel1 -  True , channel2 -  True
After 4 seconds: channel1 -  True , channel2 -  True
After 7 seconds: channel1 -  False , channel2 -  False

You can see that channel1 and channel2 are actually the same
channel object.

I don't think there's anything you can do about that; you'll
have to re-think your method of detecting whether a sound is
still playing.

Note that you can use Channel.set_endevent() to arrange for an
event to be posted when a sound finishes playing.

--
Greg