Loop allowing to display a music background during every routines of that loop

I work on Win 10, PsychoPy v2020.2.5 standard standalone version

I’m trying to build an experiment in which participants would have to reply to different surveys about a given music while this music is playing. The 3 surveys are in 3 separated routines (as their formatting are quite different). So I get my routines working properly. I surrounded them with a loop that call the *.csv file containing a column that indicates the music to be read at each iteration of the loop. However, i cannot figure out which kind of code I should include to indicate where to find that list and when to end it.
I saw related topics but most of them asked for the music to be read during n iterations of the loop and had a very limited list of background sounds so they manually included this list at the begining of the experiment.

This question is probably a newbie one, sorry. I have accurate programming knowledge with R but I’m a beginner with Python, and especially in experiments building with PsychoPy. I don’t mind moving to the coder but I don’t think that’s necessary to solve that problem.

So, more precisely, I don’t think I have to insert a sound component as it will end with the end of the first routine/survey. So what kind of code to insert in a code component to inform PsychoPy that I would like to play the sounds included in my *.csv file, each lasting during the whole loop and changing at each iteration ?

I hope I’m clear enough. If not, please tell me.
Many thanks!


As a component is contained within a routine, making one single component run across multiple routines is difficult, but not impossible!

The trick is to store the time taken at the end of each routine, then start the sound playing again from that same point at the start of every routine. You can do this using a Variable component: Set the value at experiment start to be 0, then the variable at routine end to be its own name + t. This means that the time each routine took will be added to the variable, so you can then use a Code component to tell the Sound component to start at this point like so:


(where mySoundComponent is the name of your sound component and myVariable is the name of your Variable component)

This would go in the Begin Routine tab of the Code component.

The way I would do this is to put a sound component in the first routine within the loop which sets up the sound for that loop. However, put a start time of, say, 10 seconds and then end the routine after a fraction of a second. In the start routine tab in the next routine put sound_1.play() if your sound component in sound_1. Then put sound_1.stop() in the End Routine of the last routine of the loop.

This is from memory so my syntax may be slightly off.

Many thanks for your replies !!
I managed to do something a bit betweeen both.

If anyone else need it --> :

I inserted a routine at the beginning of my loop in which I added the sound component (let’s call him “sound_1”) with no stop time and a code component in which I put in the Each Frame tab

if t >=0:
    continueRoutine = False

I had to this because between the 1st and the 2nd routine, the sound was starting from the beginning again :woman_shrugging: , which was not the case for the other routines.

Then, for each of the other routines, i added a code component containing in the Begin Routine tab


And in the End Routine tab


It works perfectly ! :blush:
Many thanks !!!

1 Like

Thank you so much for posting this!! This was what I was missing to finish the experiment for my thesis and had troubled me for very long. Honesly I can’t thank you enough! This works perfectly!
Sending my best wishes!

1 Like

Good afternoon! Could I ask for some more advice? After I applied this solution to my experiment it worked perfectly, however a few days ago it started playing the track from the very beggining each time in each routine. I double-checked for typos but I didn’t have any. Do you have any idea why this stopped working?
I duplicated the experiment so that I have 2 slightly different copies of it. Could that be a problem?
Thank you so much for your time.

Have you got two copies in the same folder? New experiments need to be in new folders.

I have put the experiments in different folders and renamed the excel condition file for the second copy and the folder of the sound files and put them together with it. So every experiment has its own folder and takes data from its own files .