Random Sound Stimuli Help

OS : Mac OS 10.15.6
PsychoPy version : StandalonePsychoPy-2021.2.3-macOS
Standard Standalone? (y/n) y
What are you trying to achieve?:
I am trying to create a sound stimulus that plays at the start of my “reward” component. Ultimately, I have 4 sounds and I would like one of the four to randomly be selected and played.

What did you try to make it work?:

Very lost on how to do this!

Hi Adam,

You need a code component and in the begin routine write:

allSounds = ['sound1.wav', 'sound2.wav']# list must contain all possible sounds
shuffle(allSounds)
thisSound = allSounds[0]

Then in your sound component use $thisSound with the field set to update every repeat

Hope this helps,
Becca

Hey Becca,

I get this error:

File “/lastrun.py”, line 1886, in
soundreward.setSound(thisSound, hamming=True)
NameError: name ‘thisSound’ is not defined

I got it to work!

I put this in begin experiment:

allSounds = [‘sound1.wav’, ‘sound2.wav’, ‘sound3.wav’,‘sound4.wav’,‘sound5.wav’,‘sound6.wav’,‘sound7.wav’,‘sound8.wav’,‘sound9.wav’]# list must contain all possible sounds
shuffle(allSounds)
thisSound = allSounds[0]

I put this in begin routine:

shuffle(allSounds)
thisSound = allSounds[0]