psychopy.org | Reference | Downloads | Github

Is it possible to play continuous sound in Builder?

Hi, I would like to play a .wav file continuously throughout an experiment using Builder, PsychoPy3 2020.1.2 (MacOSx 10.13)

Unfortunately, the sound file is automatically stopped when a response key is pressed, which I would like to avoid.

If I deselect ‘Force end of routine’ or ‘Discard previous’ within the response component, the program does not move on to the next trial.

Is there a way to play a sound file independently of/uninterrupted by the trial routine?
Thank you!

Perfect, thank you! It works like a charm. I am new to PsychoPy and it took me a while to figure out that the code component can be found within the Custom Components in the Builder.

Hi, I am now running into a related problem.

When wanting to run this experiment online, I receive the error message ReferenceError: sound is not defined

Within the code component I selected ‘both’ and I thought this would take care of translating my code to JS for online use. Is there a steo that I am missing?

Thank you.

The code reads as follows:

//Begin experiment
JS: background_sound = new sound.Sound(“traffic.wav”);
Py: background_sound = sound.Sound(‘traffic.wav’)

//Begin routine
JS:
if ((practiceTrials.thisN === 0)) {
background_sound.play();
}

Py:
if practiceTrials.thisN == 0:
background_sound.play()

You are right, if there are both Python and Javascript equivalents of custom code snippets like this, Builder will automatically use the appropriate one, depending on whether you are running locally (Python) or online (Javascript).

I’m not a javascript person so can’t really advise too much here, but the background_sound = new sound.Sound("traffic.wav"); snippet looks exactly what Builder would propose as an auto-translation. That doesn’t necessarily mean that it is correct (at this stage it is just doing its best). What I would suggest you do is insert a regular Builder sound component. Then look through the generated Javascript code for how it would create a sound (use a sound file name that you can easily search for in the code). What does that line look like?

I’ve just done that myself, and the line looks like:

  background_sound = new sound.Sound({
    win: psychoJS.window,
    value: 'traffic.wav',
    secs: (- 1),
    });

Does that syntax make a difference? I’m asking, as I really don’t know. Maybe this is just a more verbose equivalent of what you already have, and it makes no difference at all.

Another possible issue: the code is correct but it fails to find your traffic.wav sound file. Is it in the correct place?

Thank you for your help. Unfortunately, I still cannot get it to work. I have looked at the .js code when playing short sound snippets within a Builder loop, but I fail to properly translate the begin and end routines from short to continuous sound. The sound should be in the right place. If I use it as a sound snipped within a Builder loop it also works online but restarts at every trial.

Wow, that seems quite complex. Thank you. I will give it a try.

Hi,
I think I managed to solve the issue, and have an online experiment with a continuous sound. The solution is a bit messy, and I got it playing a bit randomly, but it is working.
Basically you have to do the following steps.

1) Create a code snippet for the continuous sound
1a) First, in the Begin Experiment tab, I created manually this code, selecting JS from the dropdown menu.

background_sound = new sound.Sound({
    win: psychoJS.window,
    value: 'traffic.wav',
    secs: -1,
    });
 
 background_sound.setVolume(1);

1b) Then I created in the Begin Experiment this line of code selecting the Py code from the dropdown menu.

background_sound = sound.Sound('traffic.wav')

1c) Finally in the Begin routine tab play the sound, selecting Py code from dropdown menu.

background_sound.play()

NOTE: the step 1b could seem redundant, but for some reasons without this step, the solution won’t work (I assume it is related to the automatic conversion to JavaScript).

These 3 sub-steps so far should make the code work locally. But to work online you need another step

2) Copy manually your continuous file (i.e., 'traffic.wav' in the example) in the html/resources folder of the project
I noticed that the file was missing from this folder, so I manually copied it.

This solution worked fine for me. Locally it worked also if there were other auditory stimuli during the background noise. But online it gave me some java script errors and did not worked. Online it worked only with a background sound and visual stimuli in the experiment.

Hope this helps

NOTE: i edited and reposted the comment adding some missing info after deleting it.