0

I'm trying to use Pygame library in Python to play some piano sounds. I have the notes in .wav format, and I want to play some notes consistently, using a while loop, but the result isn't as I except.

I really tried some other possible solution, but I didn't found any that solve my problem. I want these three notes (sounds) to be played simultaneously and to be repeated every 0.4 seconds.

c4.wav : https://gofile.io/?c=F3U1LG

e4.wav : https://gofile.io/?c=4KflZ9

a4.wav : https://gofile.io/?c=mtNXWd

import pygame
import time
pygame.init()

c4 = pygame.mixer.Sound("c4.wav")
e4 = pygame.mixer.Sound("e4.wav")
a4 = pygame.mixer.Sound("a4.wav")

while True:
    c4.play()
    e4.play()
    a4.play()
    time.sleep(0.4)
CurlyError
  • 305
  • 2
  • 15
  • Can you please edit your question to include information on *exactly* what happened when the program was executed. You've only told us that it wasn't what you expected. – Kingsley Sep 12 '19 at 01:19
  • Depending on what is wrong, these answers may also help - https://stackoverflow.com/questions/2936914/pygame-sounds-dont-play – Kingsley Sep 12 '19 at 01:39

1 Answers1

0

From the pygame.mixer.Sound.play() documentation:

Begin playback of the Sound (i.e., on the computer's speakers) on an available Channel. This will forcibly select a Channel, so playback may cut off a currently playing sound if necessary.

So you need to wait for the sound to finish playing (or use multiple channels for simultaneous sounds)

Maybe something like:

def playAndWait( sound ):
    """ Play a sound, wait for it to finish """
    sound.play()                      # play the sound
    time.sleep( sound.get_length() )  # wait for sound to end

    # Quickly confirm sound has stopped, should not loop (much).
    # (This is probably unnecessary)
    while ( pygame.mixer.get_busy() ):
        time.sleep( 0.1 )

while True:
    playAndWait( c4 )
    playAndWait( e4 )
    playAndWait( a4 )
    time.sleep( 0.4 )

It's not ideal to use time.sleep() in pygame, because it slows/breaks the event handling. So in practice, some other method of wait-delay timing will need to be used.

Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • I don't want to wait for sounds to finish. Suppose these sounds are 2 secons long each. After I play each of them, I want to play the exact sounds, without interrupting the first time I played them, after 0.4 secons it has been playing. So when I play the first sound, before it ends (2 seconds), I want to play it 5 more times. So I want to play ot when the first sound is ate 0.4 seconds, 0.8 seconds, 1.2 seconds, 1.6 seconds and when it ends at 2 seconds, but none of these 5 times shouldn't stop. Is it possible? – CurlyError Sep 12 '19 at 10:11
  • @MateoKurti - Yes it's possible, but you have to manage the delays yourself. The code in the question plays all 3 sounds, probably starting the next one before the previous has even been output, so a user will only hear 0.4 seconds of the last-queued sound (a4), before the cycle repeats. – Kingsley Sep 12 '19 at 22:37