Problem with the offset of a sound component

Hi,

I’m new to PsychoPy and Python in general and am working mostly in the Builder (v1.90.0, Win10). I created a loop that randomly selects one sound out of a set of 12 (each having a unique duration), waits for the participant’s response, and then repeats this process until all sounds have been played.

However, I’ve run into some problems with setting this up properly in the Builder. When I leave the ‘duration’ box of the sound component empty (as the tooltip in PsychoPy suggests), the experiment does not run and I get the following error message:

Traceback (most recent call last):
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\app\builder\builder.py”, line 2039, in runFile
script = self.generateScript(self.exp.expPath)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\app\builder\builder.py”, line 2247, in generateScript
target=target)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\experiment_experiment.py”, line 140, in writeScript
self.flow.writeBody(script)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\experiment\flow.py”, line 241, in writeBody
entry.writeInitCode(script)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\experiment\routine.py”, line 99, in writeInitCode
thisCompon.writeInitCode(buff)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\experiment\components\sound_init_.py”, line 65, in writeInitCode
if float(inits[‘stopVal’].val) > 2:
ValueError: could not convert string to float: None

If I do put in a random value (e.g. 10 seconds) in the duration box, it seems to work fine. At least in the sense that all of the files are being played. The problem is, however, that when I set the same fixed duration time for all of the files I’m unable to reliably measure response time, since the onset of the key response component is set to trigger after the sound file has been played (I want to measure how quickly the participants press a key after they’ve heard the sound).

The strange thing is that when I do same in the coder view, the experiment seems to run just fine. I did notice, however, that the response key component is not working as I intended: it seems to start with the onset of the sound file and not after the sound has played. But maybe this is because I did something wrong in the code? This is how I tried to set it up (key_resp_4 is the key response component and warmup_sentence is the sound component):

if t >= 1.0 and key_resp_4.status == NOT_STARTED:
key_resp_4.tStart = warmup_sentence.status == FINISHED
key_resp_4.frameNStart = frameN
key_resp_4.status = STARTED
key_resp_4.clock.reset()
if key_resp_4.status == STARTED:
theseKeys = event.getKeys(keyList=[‘left’, ‘right’])

I’d appreciate any advice on how to solve these issues. Thanks!

Hi @Dawid_Czech, there appears to be a bug in the Sound code that attempts to convert a ‘None’ string to float when no duration is provided. This is why you are seeing the ValueError. I will put this as an issue in the repository on Github, but you can make the following additions to the psychopy code to fix this error. In “psychopy\experiment\components\sound_init_.py” replace line 65 with:

if inits['stopVal'].val in ['', None, 'None']:
    inits['stopVal'].val = -1
elif float(inits['stopVal'].val) > 2:
    inits['stopVal'].val = -1

and line 85 and 86 (which were lines 83 and 84 prior to above change) with:

if not self.params['stopVal'].val in ['', None, -1, 'None']:
    if not float(self.params['stopVal'].val) < 2: # Reduce spectral splatter but not stopping short sounds

If you do this, you will no longer have the error. However your sound duration will be -1, and so you will not hear a sound unless you change the duration, but this can only be done when you instantiate the sound object.

The problem is, however, that when I set the same fixed duration time for all of the files I’m unable to reliably measure response time, since the onset of the key response component is set to trigger after the sound file has been played (I want to measure how quickly the participants press a key after they’ve heard the sound).

This should not be a problem. In Builder you can set your keyboard response to start at the onset of the sound, so your RTs will be relative to the sound onset. Would that be better?

2 Likes

Thanks for your response. The changes to psychopy code that you suggested made the error go away.

I want to compare the RTs to sounds (pre-recorded sentences) of different length, so I need absolute rather than relative values. That is why I was trying to get the keyboard response component to start at sound offset. I suppose I could simply get the values I need by subtracting the length of each file from the relative RTs. I was just wondering if there was a way to do it automatically to save time (I plan to eventually include around 60 pre-recorded sentences in the experiment).

This also fixed the same issue I had when playing audio file with stop duration not specified. I’m using PsychoPy v1.90.1 on macOS Sierra. Thank you.

For future reference to people finding this thread, David fixed it in the repository, so if it affects you, simply upgrade to the latest version: