| Reference | Downloads | Github

Randomly playing audio files from an excel sheet

Hey everyone!

I’m currently working on programming an experiment with PsychoPy and for one part of the experiment, I want to present six different sounds randomly selected in a loop. I already programmed the visual loop where the stimuli the words in the excel sheet are converted into a list and shuffled and subsequently only the first six elements are presented. I was working on a similar solution for presenting the sounds:
In the excel file, I added the pathways of the sound files (which are located in a folder called “audios”). Then the program reads the excel file row by row and puts the pathways into an array which is then shuffled and accessed later to be presented. However, this method does not really work (I’m not sure if it is just a bug or if the general logic does not work).
Is there a better way to randomly play a subset of sound files that are stored in a folder?

I really appreciate any help, tips or question!

Thank you very much in advance. (78.0 KB) AStimuli.xlsx (7.4 KB)

Hi There!

If you choose to use coder view for this, I think you might want something like the following:

#import conditions file
conditions = data.importConditions('conditions.csv')
trials = data.TrialHandler(trialList=conditions, nReps=12, method='fullRandom')

for thisTrial in trials:

    this_Sound = thisTrial['audFile']
    core.wait(2)#or however long you want to wait

Where ‘audFile’ is the header of the column containing the paths to your auditory stimuli in your conditions file.

However, I see that you are working from a script exported from builder and already have a fair few trialHandler components. Please could I ask (in case I misunderstood your question) is there a specific reason why you would prefer to use coder over builder view for this? If not, this is something that might be easier to implement in builder view using a single conditions file.

The file would have one column with the header “VisStim” and one with the header “AudStim” (similar to what you already have but with a header in the first row). Then you would add a sound component and a text component to your routine. You would set the “text” in your test component to be $VisStim and set the “Sound” of your sound component to be $AudStim (ensure to use the “set every repeat” option)

Best of luck!


Hey Becca,
thank you very much for your help!
To clarify my issue a bit: The code was created by another person and my task is to fix bugs and add some additional requirements (varying blank spaces, randomized target stimuli). I just started using PsychoPy a few days ago, so I am not sure how to handle this the best way. The idea of the experiment is to have six different conditions (three of them are different visual stimuli, three are different auditory stimuli). In each condition/block six stimuli from a list of 36 stimuli should be presented right after another and after each block there is a delay of varying lenght. Each condition is supposed to be repeated 12 times. However, blocks and stimuli should be presented randomly and identical stimuli and blocks should not appear in more than two subsequent blocks. Additionally targets are randomly substituting a stimulus in position 2-6, on average every third block.
Currently the experiment has a main loop and six sub loops (for each condition). I wonder if it is possible and maybe even easier to just create one loop that reads in different columns from an excel file (each column being a different condition). Each column would then consist of 36 stimuli that would have to be randomized on every loop repeat.
Before I go into more detail, I wanted to ask if you think it is possible to build this experimental design with the builder or if the coder is more useful for this purpose.

Thank you, again, so much!

Hey there,

Ok, so to clarify if I have understood the trial structure you are aiming for:

6 blocks (3 visual 3 auditory)
6 stimuli per block (that are randomly selected from 36 possible stimuli - this would mean that not every stimulus is necessarily presented though - is that correct?)
Each of the 6 blocks repeated 12 times (?) this would mean 72 blocks, 432 trials, in total.

Please could you clarify the above?



Yes, you understood correctly.
There are six blocks each with different kinds of stimuli. Three of them are visual (words, pseudo-words, noise) and three are auditory (words, pseudo-words, noise). In every block, six stimuli from one of these conditions are presented randomly (drawn from a number of 36 stimuli of the respective condition). Every now and then (on average every third block), there should be a target item on which the subjects have to react by pressing a key.
Overall each block should be repeated 12 times, but never more than two times in a row (blocks should be repeated in a random order, but not totally random), which adds to a total number of 72 blocks and a presentation of 432 stimuli (of which approx. 30 should be target items). Each stimuli should be presented for 1 sec right after the previous one and after each block, there should be a pause of varying length.
The current code does not work that way. Currently, it shows lists instead of individual words and the sound presentation does not work at all. Also, the loops are designed in a way that there is a varying blank space between each stimulus of a block, which is also not desired.
I’ve started to build the experiment with the builder, but I’m sceptical if I can implement all the requirements with it.