psychopy.org | Reference | Downloads | Github

Reading list index in sound component

I have been trying to figure out if it is possible to use list indices as the input to a sound component where each index is a string containing the name of a sound file. I can make this work in PsychoPy but it doesn’t seem to translate to PsychoJS

I’ve been attempting to find a method for selectively randomizing pieces using two lists and have posted a few times in the forums over the last few months about this but have never found a solution.

If anyone has dealt with this before, I’d be grateful to hear how you managed to make it work.

How do you make this work in PsychoPy, for starters? I’m having a little trouble envisioning what you’re attempting to do.

1 Like

Hi Jonathan,

Thank you for your response.

One approach that works in PsychoPy is randomizing two lists (say, list A and list B), selecting some stimuli from each list and assigning those stimuli from A and B to a new list called list C. In principle you should be able to assign each list index in list C to a variable which can then be used in a sound component.
Something like the code below does work in PsychoPy but not when uploaded to Pavlovia.

#Make list of major pieces

listA = ("testMusic/A1.wav",
        "testMusic/A2.wav",
        "testMusic/A3.wav",
        "testMusic/A4.wav")

#Make list of minor pieces

listB = ("testMusic/B1.wav",
        "testMusic/B2.wav",
        "testMusic/B3.wav",
        "testMusic/B4.wav")

#Randomly sample two pieces from each list

shufA = np.random.choice(listA, 2, replace = False)
shufB = np.random.choice(listB, 2, replace = False)

#concatenate dataframes 

listC = shufA + shufB

shufC = np.random.shuffle(listC)

#assign pieces to variables

pracPiece1 = shufC[0]
pracPiece2 = shufC[1]
pracPiece3 = shufC[2]
pracPiece4 = shufC[3]

Translating the code to Javascript using something like this code doesn’t seem to work:

I see. So the first problem is constructing the list, the second is loading the relevant sound files into the sound component.

Obviously np.random.choice doesn’t work in JS either as numpy does not exist in JS. There are various ways to solve this, but here’s my rough shot at it in JS:

listA = ["testMusic/A1.wav",
        "testMusic/A2.wav",
        "testMusic/A3.wav",
        "testMusic/A4.wav"];


listB = ["testMusic/B1.wav",
        "testMusic/B2.wav",
        "testMusic/B3.wav",
        "testMusic/B4.wav"];

listC=[];
// Pick two random elements out of listA, without replacement, using pop
// Add them to listC using push
for (a = 0; a < 2; a++) {
    util.shuffle(listA); // PsychoJS has a built-in shuffle as part of its util library
    listC.push(listA.pop());
}

// Same again for listB
for (b=0; b<2; b++){
    util.shuffle(listB);
    listC.push(listB.pop());
}

util.shuffle(listC); // Now we have a listC with two items each from A and B in random order.

pracPiece1 = listC[0];
pracPiece2 = listC[1];
pracPiece3 = listC[2];
pracPiece4 = listC[3];

That code should at least get to the point where the four pracPiece variables are defined. I would put all that in a “begin experiment” pane of your code window, and then for the sound components, have them set every repeat, and in the filename just put pracPiece (no $, trust me on that).

Does that change the behavior at all?

2 Likes

Hi Jonathan,

Thank you for the code suggestion as I didn’t realize the psychoJS library had its own shuffle function.

I tried the fix you suggested, however I am still running into the same error that the resource can’t be found when I attempt to run it online. I am wondering if the error has to do with the implementation of the sound component in psychoJS. I’ve tried a few other approaches which seem to work in my browser’s Java terminal but not when attempting the run the experiment online, usually producing the same error message.

Yes, I was afraid of that. I think that it may not be possible to set the contents of a sound component from a code component online. This has come up in a few places, but I haven’t done any specific testing.

@jon how exactly do sound components work online? Are they only loaded at the beginning of the experiment or is there a way to update them later?

1 Like

I think the issue is jus that we need to tell the client what fiels to download, even though they can then be selected dynamically during the experiment.

There are two ways the list of resources can be generated:

  • A. if you use the html folder inside the project (as default) then there will be a resources folder inside that and you can drag a copy of the files into there manually. Builder will do it’s best to populate that folder based on what it finds in conditions files and parameter settings of components, but it can’t detect the contents of code components
  • B. the newer option is that we don’t use the html folder, just the root of the project for the javascript/html files. In theory this is better because we don’t need to duplicate files but we still need to provide a list of resources to the client that it should fetch at the beginning of the study. The way to make manual additions to that list isn’t yet exposed in the Builder interface and needs coding in the JS manually. But hopefully you’re using option A! :slight_smile:
2 Likes

Thank you for the clarification! I will try this method early in the week and report my findings.

Enjoy the holiday!

Cam