psychopy.org | Reference | Downloads | Github

Play tones from list

If I have a list of stimulus names for tones, either written in code or pulled up from an excel file using xlrd, how do I play them using code in psychopy?

# For example, if my tones are
lo = sound.backend_sounddevice.SoundDeviceSound(value='A', 
           secs=0.05, octave=4, stereo=-1, volume=1.0, 
           loops=0, sampleRate=44100, blockSize=128, 
           preBuffer=- 1, hamming=True, startTime=0, 
           stopTime=- 1, name='', autoLog=True) 
       
hi = sound.backend_sounddevice.SoundDeviceSound(value='B', 
           secs=0.05, octave=4, stereo=-1, volume=1.0, 
           loops=0, sampleRate=44100, blockSize=128, 
           preBuffer=- 1, hamming=True, startTime=0, 
           stopTime=- 1, name='', autoLog=True) 

# and the stimulus list is
tone_stimuli = ['hi', 'hi', 'lo', 'hi', 'lo']

# and I want to play them in order with a command like
hi.play()

What is the code I need in order for this to play the high tone, high tone, low tone, high tone, low tone, in sequence, as in the tone_stimuli list?

I tried this but it didn’t work:

current_tone = str(tone_stimuli[0])
current_tone.play()

(At the end of the day I need to make an audio-visual experiment where tones play in the background of the visual task, and independently of the visual trials. To do so, the tones need to be played using code, and need to pre-exist in a list for timing perfection, so workarounds where the tones are called in Builder unfortunately won’t work in this case.)

Hi There,

You are close! Try this!

tone_order = ['hi', 'hi', 'lo', 'hi', 'lo']
tone_stimuli = []

for tone in tone_order:
    if tone == 'hi':
        thisTone = sound.backend_sounddevice.SoundDeviceSound(value='B', 
           secs=0.05, octave=4, stereo=-1, volume=1.0, 
           loops=0, sampleRate=44100, blockSize=128, 
           preBuffer=- 1, hamming=True, startTime=0, 
           stopTime=- 1, name='', autoLog=True) 
    elif tone == 'lo':
        thisTone = sound.backend_sounddevice.SoundDeviceSound(value='A', 
           secs=0.05, octave=4, stereo=-1, volume=1.0, 
           loops=0, sampleRate=44100, blockSize=128, 
           preBuffer=- 1, hamming=True, startTime=0, 
           stopTime=- 1, name='', autoLog=True) 
    tone_stimuli.append(thisTone)

current_tone = tone_stimuli[0]#select the first tone from the list
current_tone.play()

I think the issue was that you were calling the .play() function on a string rather than the tone object itself, so by making a list of tone objects rather than a list of strings you can call .play using the approach that you were implementing.

Hope this helps!
Becca

It works! Thank you!

Wonderful! :raised_hands: