Sounds in background of visual task

Hello everyone,

I was wondering whether it is possible to play a sound loop in the background of a visual task, if the visual task is made in Builder but the sounds are created using code.

I have been looking for solutions and I came across two, but I haven’t managed to make either of them work in this playground example where text flashes briefly on the screen on each trial. One solution is to set the sound loop to run on the first trial, in the Begin Routine tab:

if trials.thisN == 0:
    my_tones()

…and the other is to use threading:

thread_running = threading.Event()
thread_running.set()
sound_thread = threading.Thread(target=my_tones(), args=[thread_running], daemon = True)

In practice, what I need is for sounds to play independently from events in the visual task, for a long as the visual task is running. The visual task is pretty complicated in the real experiment and I can’t get my head around the code produced by runner, which is why I’m hoping to get the tones to play while the visual task remains purely in builder.

In the example experiment that I made, a tone series is created when the experiment initialises, and is then called with the my_tones function when the visual trials are running.

The first solution runs the tone series once, and only then starts the visual trials. The second solution plays as many tone loops as there are trials, and messes up the timing of the visual events completely. I’m sure there is much more to threading than what I managed so far, but this looked like a good place to stop and ask for advice. How can I get the tones to play in the background of the visual task?

background_tones_example.psyexp (17.8 KB)