Asynchronous sound success story

Fingers crossed I think I’ve worked out how to present sound files selected in code.

  1. I created a sound component (sound_1) in a routine prior to the one where I wanted the sound to be played. I called the sound $thisSound with “set every repeat” and (critically) set the start time to after I want the routine to end (e.g. 2 for a 1 second routine).

  2. I therefore had to end the routine in code

if t > 1:
    continueRoutine=False
    
  1. I start the sound file playing in End Routine
if thisSoundCondition != "Silence":
    sound_1.setVolume(1)
    sound_1.play()

setVolume probably isn’t needed – it’s still there from earlier attempts.

I then stop the sound in Begin Routine of the routine after where I want it played.

if thisSoundCondition != "Silence":
    sound_1.stop()

I don’t yet know how to stop PsychoPy having to download all 36 wav files (of which I need 13 – or possibly only 10) but when the experiment fails due to a memory issue at the start it usually works if I try again.

2 Likes

One solution is to do it in javascript:

var audio = new Audio('file.ext');
audio.play();

You can stop downloading all files if you put it into a folder other than resource. Then you just have to download the selected ones at the beginning of your routine. !!This is not tested!! You can do it in a for loop:

\\ full paths
files = ['./path-to-files/x.mp3', './path-to-files/y.mp3'];

\\ download resources
for (var i = 0; i <= files.length; i++) {
     psychoJS.downloadResources([{name: [i + '_sound']); path: files[i]}]);
}

edited 2020-06-11T23:00:00Z : I meant to say at the beginning of the experiment.