Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Random picking of sounds as variables

Hi there,

I am setting an experiment about sound comprehension. Here is the general loop of the experiment :

As you can see, there are 96 elements executed. The plan of the experiment is 2 * 6 * 8.

The elements are regrouped as follow : 2 main conditions 'a' and 'p', 6 conditions of silence by main conditions and 8 sentences by silent's condition.

I already have 12 files (silent conditions), each filled with 96 sentences. Now, what I want is to prepare 96 sentences in a inline script, so when I start the experiment with a subject, the program will pick randomly 96 sentences with this rule :

8 sentences * 12 silent conditions.

The general idea is to counterbalance the sentences between subjects. Unfortunately, I am not very familiar with python functions. So if people can help me to design the script it will be wonderful.

I hope this is clear,




  • Hi Aurélien,

    I don't fully understand what you're trying to do. It seems like the loop table is correct the way it is, right? Is it a matter of selecting the right sound file based on the experimental variables that you've specified in the table? I.e. do you want to map these variables onto a file name? Details would be helpful 😊



  • Hi Sebastiaan,

    Thank you very much for your answer. I advanced a little. I had one problem with this loop : I need to play sequentially [silent] with [condition] (for example, I need to play sequentially the [sentences] of [silent]=0 and [condition]=a, so 8 sentences).

    I'll try to be as clear as I could. So, in the most simple way, what I need is to define a variable (a category of sound) and fill it with random sounds in the pool. Because all my files, in this example, are named like '20p(number).wav', here is what I thought I should do :

    import random
    I = random.sample(range(96),8) #I have 96 possible sounds and I want 8 of them (randomly)
    var.set('20p',I) #assign to the condition 20p all the possible values

    So, theoretically, I now have one condition '20p' with 8 values, each corresponds to a real .wav file (for example '20p1.wav').

    What I need now, is two things :

    1) implement all values of 20p in a loop, so the loop will run randomly the 8 values (20p1 ... 20p6... 20p24...)

    2) play each 8 values in a sampler. I thought to do this in the script of the sampler :

    set sample "20p[I].wav"

    but if I do this, it will try to find something like '20p[0 1 2 3 4 5].wav' and not '20p0.wav'.

    Maybe, this is to much for one post !

    Thank you again,



  • Hi Aurélien,

    Ok—it's still not entirely clear to me, but I do see at least one basic confusion: The variable 20p is a list of multiple values. But then you're trying to use it as though it's a single value when you specify the sampler. And that's where things go wrong. Does that make sense?

    What I would do is first define a random list at the start of the experiment (or the start of the block if appropriate):

    import random
    sound_indices = random.sample(range(96), 8)

    And then, at the start of the trial, you randomly 'pop' one element from that list and use it in the sampler.

    var.current_sound_index = sound_indices.pop()

    Something along those lines should get you what you want, I believe.



  • Hi Sebastiaan,

    Thank you for your advices, I solved the problem! I generated random value and assign them to my var :

    import random
    import string
    I = random.sample(range(1,97),8)

    I put the script in the bloc sequence so it generate new random numbers every iteration (at last, I think...) and i1 to i8 are the value of sentences. Than, i1 to i8 where assigned to the variable [value] in a loop.

    So if the sample play [value] for i1 = 12, the sample will find and play the file 12.wav.

    Next step will be to access sound files not in the pool but in the experiment folder because I have 96*12 files. I will post the final step (hope it could help someone even if it is a bit messy :) )



  • Good to hear! 💪

  • Here I am with more info !

    So, if you want to play sounds as variable and randomly picking them from a file, here is one way to do it (I picked some lines of the script in this forum so thanks to everybody ! )

    import os

    import random

    #set path and list for conditions

    folder = r'C:\Users\condition'

    list = []

     for file in os.listdir(folder): # 'file' can be replaced by any 'name'

     if file.endswith('.wav'): #for other type of files

          path = os.path.join(folder, file)


    So now, you have a list with path of .wav files.

    Let say, you want to pick 8 random '.wav' from it to set a variable name sound:

    sound = random.sample(list,8)

    Now, you want to play them without repetition, place this in the beginning of the sequence :

    play = sound.pop() #every time play is executed, the file executed will be removed from the list = play #set the variable 'play' for the sampler

    Here you are.

    I think it works !

Sign In or Register to comment.