How to simultaneously play randomised audio files in the background of other routines?

OS: Win 11
PsychoPy version : 2022.2.4
Standard Standalone? (y/n) If not then what?: Y

I am new to PsychoPy. I am creating an experiment in which participants will have to complete three different routines while simultaneously listening to either music or silence in the form of audio files. Between each task, participants will have to read a set instructions before they are prompted to proceed.

There are three subtasks in each task, each 2.5min long with a 30s break afterwards. For each subtask, participants will listen to “Silence”, “Music Type 1” and “Music Type 2”, once for each of the three subtasks and in random order.

Within each “Music Type” are three different audio files to be randomised. I have also made a silent audio file for “Silence”. Hence, I have a total of six 2.5min long audio files and one silent file(repeated three times in total) for nine subtasks. Each audio file is to play only once in the entire experiment.

I have already created the tasks. Now, what I want to achieve is to have one audio file of each type to simultaneously play in the background of each 2.5min long subtask and this to be done for all three main tasks. Do I first make a separate routine which is looped to randomise the audio files within each type and then a loop surrounding it to randomise the three types? If so, what code can be used in this routine for it to simultaneously play with the tasks routines?

Any help is appreciated, thank you!

Hi @egg,

yes, three inner and one outer loop should do the trick. For the outer loop you need a conditions file of this format:

Task    T1rep     ...
T1       1        ...
T2       0        ...
T3       0        ...

Then, use T1rep, T2rep and T3rep in the “number of repetitions” field for the three inner loops. This randomizes the order of the tasks.

For the inner loops you need a conditions file of this format:


To your task routine(s) add a code component:

Begin experiment

import random
vocal_files = ["v1.mp3","v2.mp3","v3.mp3"]
instrumental_files = ["i1.mp3","i2.mp3","i3.mp3"]

Begin routine

if condition == "silence":
    audio_file = "silence.mp3"
elif condition == "vocal":
    audio_file = vocal_files.pop()
elif condition == "instrumental":
    audio_file = instrumental_files.pop()

thisExp.addData("audio_file", audio_file)

Then, insert an audio component to your trial(s) and set the file to $audio_file.
Let me know if that works for you!

1 Like

Thank you so much for the help, the audio is playing well. However, since each subtask includes multiple stimuli within a loop, when I insert the code into the routine within the loop, the audio will play for its entire duration before moving onto the next stimuli in this loop.

Is there a way where each file can play through the multiple iterations of each loop. Thanks!

Yes, to achieve that you have to create the sound component in code like this after having set the audio_file:

background_audio = sound.Sound(audio_file, secs=-1, stereo=True, hamming=True,
    name='background_audio ')

After that, you can use and background_audio.stop() at any point in your experiment to start and stop the audio. It will play across multiple routines or repeats of a loop without a problem.

Make sure to select the ptb audio backend in the experiment settings!