psychopy.org | Reference | Downloads | Github

Sound not playing full after "if" condition

OS : Windows 10 v.1909 (compolation18363.836)
PsychoPy version: 2020.1.3
Standard Standalone?: yes
What are you trying to achieve?:
I am trying to play a sound based on a “if” statement placed on code.

What did you try to make it work?:
It works, but it does not play the entire sound, but only 10 seconds.

This is a long experiment with six parts:

  • DASS-21 Scale
  • PF&C Task
  • Playing the sound
  • PF&C Task again
  • OASIS images

The stranger thing is that I can play all the sound (it is a 10 min music) if I point directly to the wave file in the sound component. It works perfectly. But I want to play a sound based on information placed in the beginning of the experiment, it stops playing within 10 seconds. I have also tried to use continueRoutine = True inside the if statement, but it made no difference.

Any thoughts?!
Thanks a lot!

PS: Experiment will crash without a “Result.png” file after the PF&C Task, but it works if the file already exists in the folder of the experiment file. It probably will crash also without the wave files, that I cannot upload here (too large also).

Experiment and conditions files:
Trial.psyexp (124.1 KB)
dass.xlsx (6.0 KB)
ordem.xlsx (6.1 KB)
oasis.xlsx (9.0 KB)

Please show us your relevant custom code.

Hi Michael,

Here is the code:

if expInfo['Grupo (Con ou C ou D)'] == 'C':
    musica = 'conso.wav'
    continueRoutine = True
elif expInfo['Grupo (Con ou C ou D)'] == 'D':
    musica = 'disso.wav'
    continueRoutine = True
else :
    musica = 'mudo.wav'
    continueRoutine = True 

“musica” is in the sound component as “$musica”

Thanks

Hello, just note that continueRoutine = True does nothing. It is set to True by default on every screen refresh, so only setting it to False will have any effect. So you can just delete all three of those lines.

It’s not clear to me why the sound won’t play to completion. Does this happen when running locally in Python, or when online, or both?

Hello, Michael. Thanks for the answer.

I realize the continueRoutine does nothing. It was just a try, I guess.

I am trying to run the experiment local, from the builder. I also think it makes no sense. Especially if it works with the same wave file, as long there is no code and the wave file is pointed directly at the sound component.

It coulb be the code or if statement has different buffer or memory properties?

Your own code is working correctly if it actually sets the sound file name properly.

If so, the problem probably lies with PsychoPy itself. Can you try setting the sound library to something other than 'ptb'? Normally our advice is the opposite, as the Psychtoolbox library has the best performance. But try going to the “Audio” tab of your experiment settings and selecting another option. Does it change the behaviour?

Please could you note where your code is in your flow and a screenshot of the sound component?

I think it should be in Begin Experiment of a routine that appears earlier than the sound component that uses it

I don’t think that is the issue, as the correct sound file does get played, but only for 10 s rather than for its full duration. This does make it very mysterious…

Offline I’d recommend you use ogg format instead of wav

It might be that by the time it gets to 10 seconds it’s trying to play sound it hasn’t loaded yet

Hello Michael and wakecarter, thanks for all the input.

Michael, at some point I changed the sound library to PTB because the log after the experiment suggested to do so.

WARNING We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use [‘pyo’] (in that order).

Interestingly, changing it to ‘pyo’ made it work (even not working when set to “user preference”). And with strange behaviours: I cannot use the Esc key to exit the experiment, but that is not a problem.

wakecarter, the code component is, indeed, above the sound component, and the if statements in the Begin Routine tab. Although I see your point about using a ogg file, it should just work with the wave one.

I think we can close this one on the better behaviour of the pyo library, but I think it could be worth a deeper check sometime.

Thanks for all the help so far!

@felipeviegas I am having this same exact issue, and I cannot use the escape key when using ‘pyo’ though it does allow the sound to play for specified duration.

I agree this is worth a deeper check (@Michael) as for my experiments low latency is crucial and I can’t get as low of latencies/jitter with pyo as I can with PTB.