Use code to pick a random sound from my files in sound component

OS: Win10
PsychoPy Standard version: 2021.1.2

Hello! I am trying to create a oral digit span task where sound files say a letter followed by random numbers. I am trying to use the sound component in the builder to make the program play a random sound from my folder. I am using the code component to create a list of the sounds in my folder and make the program chose the sound using “random.choice”.

Here is my code:

Before experiment:
import random

Begin experiment & each frame:
digit1 = random.choice([“1.wav”,“2.wav”,“3.wav”,“4.wav”,“5.wav”,“6.wav”,“7.wav”,“8.wav”,“9.wav”])

In the sound component, I input $digit1 to specify the file.
digit_1

This method works with the text component when I have values (1,2,3,…) in my list but here the program seems to not want to accept the strings in my list as a file name.

If I set the sound to “constant” I get this error:
digit_1 = sound.Sound(digit1, secs=-1, stereo=True, hamming=True,
NameError: name ‘digit1’ is not defined

If I set the sound to “set every repeat” I get this error:
Traceback (most recent call last):
File “D:…\trial\Trial 4 in 10s_lastrun.py”, line 1755, in
digit_1.setSound(digit1, hamming=True)
File “D:\Program Files\PsychoPy3\lib\site-packages\psychopy\sound\backend_ptb.py”, line 428, in setSound
_SoundBase.setSound(self, value, secs, octave, hamming, log)
File “D:\Program Files\PsychoPy3\lib\site-packages\psychopy\sound_base.py”, line 174, in setSound
raise ValueError(msg % value)
ValueError: Sound: bad requested frequency 5

I have very limited python knowledge, so I tried changing the code in some ways, using the print() function, changing how I make my list, changing when the code happens, changing the audio library (program says to use ptb), sadly I am not good enough at coding to find a solution. The problem doesn’t come from my .wav files as they do work in my other program where I don’t randomize the numbers. I would be grateful for any help!

Hello,

see here and here if you selected the proper frequency for your audio files.

Best wishes Jens

Hello, thank you for your answer!
I have tried the different audio libraries and latencies and I did check my .wav files, which work when I specify the file directly instead of using the code. I think the problem comes from the interaction of my code and the sound component, where the sound component might not accept the strings I listed using the random.choice function.

I also tried listing my files like this:
my_list = [“1.wav”,“2.wav”]
digit1 = random.choice(my_list)

I still get the same error:
File “D:\Users…\trial\Trial 4 in 10s_lastrun.py”, line 1758, in
digit_1.setSound(digit1, hamming=True)
File “D:\Program Files\PsychoPy3\lib\site-packages\psychopy\sound\backend_ptb.py”, line 428, in setSound
_SoundBase.setSound(self, value, secs, octave, hamming, log)
File “D:\Program Files\PsychoPy3\lib\site-packages\psychopy\sound_base.py”, line 174, in setSound
raise ValueError(msg % value)
ValueError: Sound: bad requested frequency 8

Any other idea?

Hello,

if there are no further restrictions, you could use an Excel-file to load the wav-files and a random loop over trials instead of coding it by hand.

Do you really want to set the wav-file in every frame? Don’t you mean every repeat?

Do you want to run the experiment online? If yes, do not import libraries. Builder imports all libraries that have matching PsychoJS-libraries.

I do not have any audio-files which I could use for testing here. If you upload two, I’ll test it here.

Best wishes Jens

Hello, thanks again for your answer!

I do use excel files to decide which letters appear, as they are not random. I could add the .wav files in excel but I don’t know yet how I would randomize them, as I need to keep my loop sequential so the letters appear in a certain order. I will not run it online.

My code works to display random numbers as text:
digit2 = random.choice([1,2,3,4,5,6,7,8,9]) → With $digit2 as the text, it displays a random number between 1 and 9 every time.
But if I use the code:
digit1 = random.choice([“1.wav”,“2.wav”,“3.wav”,“4.wav”,“5.wav”,“6.wav”,“7.wav”,“8.wav”,“9.wav”]) → with $digit1 in the sound component, the program refuses it, maybe because it’s string instead of a value so it doesn’t work?

If all else fails, I will have to forget about the randomization and will write all my .wav files in the excel file.

Here are 2 .wav files if you want to try!


Thanks again!

Hello,

does this toy-version do what you want?

Snd.psyexp (9.3 KB)

Best wishes Jens

Hello,

Your version works just fine!
It was not working at first in my program, but moving the code from the bottom to the top (for convenience) like in your program fixed it. I retried with my original code and it works too if the code is at the top… I did not know about this and checked Code Component — PsychoPy v2022.1.1 which explains it.
I’m sorry for the trouble but thank you so much, I might never have found it without your help!
All the best to you.