Reading in audio files via code component pavlovia

URL of experiment:

Description of the problem:

I am building an online experiment where participants have to listen for a target sentence and make a response, additionally they press a button whenever they hear a tone beep. I have the tone beep (detection of the beep is the main goal of the experiment), being read in from a conditions file within a loop no problems there. As such I would like the target sentences to be read in via a code component. I have been following this thread on using code component to read in stimulus onset times.

I have applied this to my problem with the following python code.

 if Tones.thisN == 0: # only on the first trial
    targets = ['stimuli/Baron/070001.wav','stimuli/Baron/070006.wav']*54 # create the list
    shuffle(targets) # randomise its order

current_target = targets.pop() # extract one entry on each trial
thisExp.addData('target_phrase', current_target)

Which converts to the following JS code

if ((Tones.thisN === 0)) {
    targets = (["stimuli/Baron/070001.wav", "stimuli/Baron/070006.wav"] * 54);
current_target = targets.pop();
thisExp.addData("target_phrase", current_target);

Then in the sound component I have $current_target.

Any help would be appreciated.

I should note the error messages!

I am getting this one on firefox

And the following from chrome

Hi Liam! I did this by using the JS-only part of the code component and the “Begin Experiment, Begin Routine, End Routine” tabs. The conversion from .py to .js is sometimes wonky but I think this is very straightforward to write directly in .js (at least it worked for me and I know nothing about javascript).

What I did was the following…

Begin Experiment tab: define a .js function shuffle (literally could cop/paste what i have here), start an iterator at 0, make a list of your audio files:

function shuffle(a) {
  var j, x, i;
  for (i = a.length - 1; i > 0; i--) {
      j = Math.floor(Math.random() * (i + 1));
      x = a[i];
      a[i] = a[j];
      a[j] = x;
  return a;

t_2 = 0;  // start iterator at 0

music_list = ['007','010','014','028', 
'105','109','110']; // names of my audio files

Begin Routine tab: define current_target as targets[current_iterator]. As you have done I put $music_play in the sound component.

var stim = "Stimuli/"; //subfolder to find the audio file in
var mp3 = ".mp3"; //file type to appended to the end

var music_play = stim.concat(music_list[t_2],mp3); //concatenate folder, audio file name (by indexing the list), and .mp3 

psychoJS.experiment.addData('musicPlayed', music_play) // add to log

End Routine tab: +1 to your iterating variable

t_2 = t_2+1

One issue I’ve run into is that sometimes audio files later in the loop sometimes get cut short instead of playing to the end of the file. Apparently this issue can be avoided if you play the longest possible audio file first, but that doesn’t work for my experimental design. I noticed the issue a couple days ago and I am getting assistance with it here.


Actually IIRC I tried to use the “target.pop” style solution as well but I also ran into issues, which is why I use the iterating index now.

Oh also apparently .mp3 is better for cross-platform compatibility. Check the sound component notes here:

(And .mp3 loads way faster than .wav too!)

Thanks so much for your advice.

Unfortunatley I am still having issues. I am able to get the paradigm to run. But it doesn’t play the files.

Is there error gone?

The* error

Yes the error messages are gone. Just the files won’t play.

Which is odd because if the javascript was incorrect there would be a JS error, and if the javascript is working but passing bad file paths to the sound component then I’d get an error there :thinking:

That would be my expectation as well! I know it’s strange but sometimes when I run into totally unexplainable behavior like that, I try the steps: make the experiment inactive, clearing my browser cache, closing psychopy, opening psychopy, pushing the commit, turn experiment back to pilot mode, and then try again. Sometimes that works! :dizzy_face:

Also, just as a sanity check do other .wav files (the beep i guess?) work? Otherwise I might be concerned that it’s because certain browsers mgiht have trouble playing .wav files?

Yeah will try that!

The files do work when read in via spreadsheet, or writing the absolute path. So it’s quite odd…

Thanks again. Will keep you updated!

Ugh, very weird. Yes, please lmk! As a fellow music cog person I’m very invested in figuring out how to optimize audio presentation in pavlovia :slight_smile: