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.

1 Like

Thanks for this solution! Being guided by your answer, I wanted to share my solution for playing different audio files from an excel sheet, with audio files located in a folder called “audio”, for anyone who had similar problems. This is only for the online experiment. The solution is also very messy and with a bit of randomness/luck, it is working for me. The code may seem redundant but I ran into a similar problem where it wouldn’t work when I tried to simplify it.

I have my code in the Begin Routine tab because a new audio file plays simultaneously with another routine each time, rather than one continuous audio file for the duration of the experiment.
In the Begin Routine tab, click on JS from the dropdown menu

var audio_sent;

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

audio_sent.setVolume(1);

audio_sent = new sound.Sound({
    win: psychoJS.window,
    value: (('audio/' + audio) + '.ogg'),
    secs: -1,
    });

audio_sent.setVolume(1);

if ((trials.thisN === 0)) {
    audio_sent.play();
}

audio_sent is what I named by audio component

In the line: value: (('audio/' + audio) + '.ogg'),
Change 'audio/' to the name of the folder in which the audio files are stores. As pointed out, make sure this folder with its audio files are in the html/resources folder of the project
Change audio to the name of the column in the excel file
Change '.ogg' to the correct extension

Use "trials.thisN" even if you have a different loop name. Javascript has trouble referencing the current loop by name for reasons that are unclear

2 Likes

Hello! I am trying to add backgroynd sound during different routines as a part of a digit span task.
I would like to ask where your excel file is placed in the experiment flow and also where the code component is placed in the flow. I am sorry for the oversimplified questions, I am quite new to psycho py and still trying to figure things out. Thank you in advance!

Hi There,

What is the continuous sound that you are trying to play? If it is a sound changing trial by trial you:

  1. Make an excel file with a column header “thisSound” and the path to each sound file in each row underneath.
  2. add the excel file in the “conditions” field of the loop around your routine.
  3. add a sound component and in the “sound” field enter $thisSound and “set every repeat” in the dropdown.

No need to apologies for basic questions - welcome to PsychoPy!
Becca

Hi Michael, I have a similar issue and this fix worked to get my sound to play throughout the loop but I would ideally like it to begin 15 seconds prior to the start of the loop (which shows a series of 15 words on the screen, paced 3 seconds each) and then end 15 seconds after

I’m new to python, but is there a simple way to add additional time to the sound before/after the loop using this code?

To start the sound 15 seconds before the loop starts, you would just shift the code component that starts the sound to be on a routine with a fixed 15 second duration that starts immediately before the loop begins.

Similarly you would need a routine after the loop to end the sound. That code could go in the “End routine” tab of a code component of a 15 s duration routine, for example.

Thank you for the quick reply - this worked! Now I’m just kicking myself for not taking a basic Python course in college as I’m realizing there are so many useful (and easy) things I could use it for here! Thanks again.