Code to play sounds in psychopy 3

Hello world,

I’m having trouble getting started with auditory stimuli in psychopy. I have so far managed to play pre-recorded wav files from a conditions file in excel, but I would like to also be able to play them using code. (I found some tutorials on this for the previous version of psychopy but it looks like lots has changed in the meantime.)

Let’s assume I want to play two sounds repeatedly with a fixed ISI between them, say sound1 onset, 500ms ISI, sound2 onset, 500ms ISI, sound1 onset, 500ms, etc. What would a code component that does this look like?

I have managed to get one sound to play, but I can’t seem to get it to play again. What am I doing wrong?

from psychopy import sound, core

snd = sound.backend_sounddevice.SoundDeviceSound(value='C', 
           secs=0.1, octave=4, stereo=-1, volume=1.0, 
           loops=0, sampleRate=44100, blockSize=128, 
           preBuffer=- 1, hamming=True, startTime=0, 
           stopTime=- 1, name='', autoLog=True) 

snd.play()
core.wait(1.5)
snd.play()
core.wait(1.5)

I think you might need to call the sound to stop in between? snd.stop() ?

Thanks! That’s it!

Do you by any chance know what I need to do to tell python to wait 0.5s between two tones?

In coder? A rough and ready approach would be to use core.wait(0.5) a more accurate approach would be to fetch the frameRate and wait nFrames - there is some info on presenting stimuli using frames here(but that is easier to make mistakes with - which is why I use builder for things now!)

If you are using a code component in builder you could use something like this:

#Begin Routine tab
waiting = False

#Each frame tab
if not waiting and t >= toneOnsetTime:#where tineOnsetTime is a time you want to play the tone
    startTime = t
    firstTone.play()
    waiting = True

if waiting and t >= startTime + 0.5: #where 0.5 if your deisred pause
    waiting = False
    nextTone.play()

Hope this helps!
Becca