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

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



I am getting strange behavior when reading channel.get_sound() value from a supposedly silent channel.

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.

Is this a bug or it's by design? This behavior messes up my application logic, because I store the returned channel objects to query later if the sound is still playing on them.

I tested the problem on both Pygame 1.9.6 and 2.0.0.

Here's my test code:

import pygame
from time import sleep

mixer = pygame.mixer
mixer.pre_init(44100, -16, 1, 8192)
mixer.init()
mixer.set_num_channels(10)
pygame.init()

dog = mixer.Sound('dog.ogg')
dog = mixer.Sound('apps/sounds/media/animal: dog.ogg')

# Test #1: use channel.get_sound() to see what sound the channel is playing. It should return None, when no sound is playing. This test passes.
def channel_test_1():
    channel1 = dog.play()
    sleep(1)
    print('After 1 second:', channel1.get_sound() == dog)
    sleep(6)
    print('After 7 seconds:', channel1.get_sound() == dog)

# Test #2: play sound on one channel. Let it play to the end. The channel1.get_sound() returns None. Then play the sound on channel2. The channel1.get_sound() should still return None, however once the sound starts playing on channel2, the channel1.get_sound() is no more None. This test fails.
def channel_test_2():
    print('Play the sound on channel1')
    channel1 = dog.play()
    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()
    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)
 
channel_test_2()

Here are my results for the channel_test_2():

>>> channel_test_2()
Play the sound on channel1
After 1 second: channel1 -  True
After 7 seconds: channel1 -  False
Play the sound on channel2
After 1 seconds: channel1 -  True , channel2 -  True
After 4 seconds: channel1 -  True , channel2 -  True
After 7 seconds: channel1 -  False , channel2 -  False


The dog.ogg sound is 5.5 seconds long. For the sake of complete representation, I attach the audio file of this good boy to this post.

Attachment: dog.ogg
Description: audio/ogg