PsychoPy can't find .wav files when the sound component is set via code

Hi all, I can’t seem to solve an issue with PsychoPy being unable to locate .wav files despite my best effort.

I have a complex design and I need to set up my audio component using code. I used this:

if speaker_cond == 'cond1':
    sounds.setSound('s/' + str(custom_variable1) + '_' + str(custom_variable2) + '.wav')
elif speaker_cond == 'cond2':
    sounds.setSound('s/' + str(custom_variable3) + '_' + str(custom_variable4) + '.wav')
else:
    sounds.setSound('s/' + str(custom_variable5) + '_' + str(custom_variable6) + '.wav')

I use the same coding to set up custom pictures and it works perfect. The sound, however, does not. It give me this error:

Traceback (most recent call last):
Hello from the pygame community. https://www.pygame.org/contribute.html
2.2848     WARNING     C:\Users\User\AppData\Roaming\psychopy3\fonts\Helvetica.truetype doesn't have valid font family name
  File "C:\Users\User\OneDrive\Desktop\TEST\WK_test_lastrun.py", line 3073, in <module>
    sounds.setSound('', secs=3, hamming=True)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\backend_ptb.py", line 426, in setSound
    _SoundBase.setSound(self, value, secs, octave, hamming, log)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\_base.py", line 198, in setSound
    raise ValueError(msg + value)
ValueError: setSound: could not find a sound file named 
################ Experiment ended with exit code 1 [pid:10684] #################

I know the code is complex and I tried doing this to check if it works:

if speaker_cond == 'cond1':
    sounds.setSound('s/native1_1.wav')
elif speaker_cond == 'cond2':
    sounds.setSound('s/native2_1.wav')
else:
    sounds.setSound('s/foreign1_1.wav')

Same error.

I tried even this:

sounds.setSound('s/native2_1.wav')

Same error again. All my files have the same sample rate (48000) as does my audio equipment. The only way it works is when I put the same file name in the sound component like this

or through a column is my datafile with file pathes. I tried leaving the component like this in hope that my code will override it but nope. It would be the same sound playing over and over in that case.

I am really at my wit’s ends and I can’t, unfortunately, do it just through GUI – I have to use code.

Does anyone have any ideas why it happens?

Forgot to mention that I am using the PTB library if that matters.

Hello,

when you want to use the sound-property with a different sound-file each trial, you need to set sound to the column-name of your Excel-file, e.g. $soundStim and change constant to set every repeat.

Best wishes Jens

Hi Jens! I am well aware of that, and this is not what I am asking here. I need to set up the sound component using code, exactly as in the examples above, and this is when it doesn’t work.

Hello

sorry, I misinterpreted your problem. But, isn’t it

sounds = sound.Sound('camerashutter.wav')

instead of

sounds.setSound('camerashutter.wav')

See also here

https://psychopy.org/api/sound/playback.html

Best wishes Jens

Yeah, I checked that, it also says you can set the audio component using setSound(). I tried your way and it produces the exact same error, so something different must be going on.

Hello,

do you mind posting a toy-example of your experiment that produces the error?

Best wishes Jens

Hello,

I experimented a little bit myself. The following code worked for me

if cond == "cond1":
    stimSounds = 's/KbdSpacebar.wav'
elif cond == "cond2":
    stimSounds = 's/KbdSwipeGesture.wav'

grafik

The wav-files I used are Win10-system-files.

The property Sound of the sound component expects a string (filename) or a frequency or a letter that gives a note as as written in the manual. sound.Sound('soundfilename) is an Sound PTB object at a certain address. So neither of the above. Try

print(sound.Sound('s/KbdSwipeGesture.wav'))

I don’t know if your are using the Builder or the Coder. In the Coder, the Builder generated code was

if cond == "cond1":
    stimSounds = 's/KbdSpacebar.wav'
elif cond == "cond2":
   stimSounds = 's/KbdSwipeGesture.wav'
condSounds.setSound(stimSounds, secs=1, hamming=False)
condSounds.setVolume(1.0, log=False)

condSounds is the name of sound-component.

Best wishes Jens

Huh, it does work! I’ll admit I am not sure why you have to do it in such a roundabout way with the sound component when the text one and the image one work perfectly the way I set them up, but oh well.

Thank you so so much Jens, you help was really priceless. I was so lost.