Why does Sound.play() work once and other instances of it do not work?

import psychopy
from psychopy import visual, core, event, monitors, sound, clock

# Window
window = visual.Window(
    size=(1024, 768), fullscr=True, screen=0, 
    winType='pyglet', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
    blendMode='avg', useFBO=True, 
    units='cm')

beep = sound.Sound(
    value = 'A', secs = 0.2,
    volume = 1.0)

beep.play() #Beep 1
core.wait(1.0)
beep.play() #Beep 2

window.close()
core.quit()

the first beep i.e BEEP 1 works perfectly fine but for some reason the second beep i.e BEEP 2 doesn’t work and I can’t seem to figure out why. Is there some process related to sound.play() that I’m missing out?

Which audio library (backend) are you using?

I removed the unnecessary parts from my code.
Also I don’t understand what you’re asking me, I clearly used the sound module from psychopy but the second audio cue just doesn’t work.

There are multiple sound libraries that PsychoPy can use to play sounds as explained here:
https://www.psychopy.org/api/sound.html

In the application preferences you’ll see them, but you can also control them within the script if you prefer.
https://www.psychopy.org/api/preferences.html

Also, what version of PsychoPy are you using?

The latest one I suppose, just installed it a week ago. I also solved the issue but don’t know why it’s responding that way, can you try to explain it to me?

After each play(), I added core.wait(2.0) followed by beep.stop() and this worked. But if I put beep.stop() before core.wait(2.0) l it didn’t work.
Any idea why this is happening?

Oh I see, yes. That’s just because you stopped the sound too fast, or because in the original code you posted above your script ended (which also stops the sound) before you had time to hear anything.

If you aren’t very confident with programming I’d really strongly recomend you use the Builder interface. Actually I’d recommend that for most people even if they are competent programmers! I almost never hand-write the code for my experiments. Using Builder would have avoided simple errors like this.

Thanks for the help and I’ll take your suggestion to account.
But I found something that contradicts your reasoning as shown in the description below:
When I did the below it worked fine - any wait time(core.wait()) works and I tried up to 100 ms.

beep1.play()
core.wait(0.5)
beep1.stop()
beep1.play()
core.wait(0.5)

But when I removed the stop, It didn’t work.

beep1.play()
core.wait(0.5)
beep1.play()
core.wait(0.5)

From the above, I don’t think this issue has anything to do about the sound being too fast or the program ending. To confirm this I also reduced the duration of the beep to as low as 0.03s and the same thing happend as stated for the above two scripts.

From this I can say that this is probably happening because the play() doesn’t get reset once the sound is over hence stop() needs to be explicitly defined for the sound to reset. If the stop() isn’t defined then once the sound is over it doesn’t get reset hence when beep.play() is done the second time I hear nothing. Do you think my reasoning makes sense?

That’s conceivable and would probably depend on the sound lib being used, as mentioned above, but I’m surprised this isn’t causing lots of problems for others if true. Anyway, glad you got a fix.

Which audio library (backend) are you using?

Is there a default backend that is defined if I do not choose one?

Open the application and go to Preferences

Hello, I am actually getting exactly this.

OS: POP_OS 22.04 LTS
Python: 3.10.6
PsychoPy: 2023.1.3
Psychtoolbox: 3.0.19.0

MWE:

from psychopy.clock import wait
from psychopy.sound.backend_ptb import SoundPTB as Sound


sound = Sound(value=1000, volume=1, secs=0.2, hamming=False)
wait(0.1)
sound.play()
wait(1, hogCPUperiod=0.3)
sound.play()

This code snippet outputs a single sound, with 2 log entries in the console:

Failure: No such entity
Failure: No such entity

Adding a stop after 200 ms does solve the issue and now both sounds are played. The 2 log entry Failure: No such entity still remain.

from psychopy.clock import wait
from psychopy.sound.backend_ptb import SoundPTB as Sound


sound = Sound(value=1000, volume=1, secs=0.2, hamming=False)
wait(0.1)
sound.play()
wait(0.2, hogCPUperiod=0.1)
sound.stop()
wait(0.8, hogCPUperiod=0.3)
sound.play()

Seems like a bug?

Mathieu