How to present an auditory stimuli in some trials

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): 11
PsychoPy version (e.g. 1.84.x): 22.5
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
I am trying to add an audio file in some trials using a random uniform distribution, but I am not sure about the code I have to use for it.

What did you try to make it work?:
I have already tried with np.random.uniform. It works for the random uniform distribution, but I do not know how to add the audio file.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.


you need to make the routine for presenting of your audio-file conditional on the result of your random distribution.

if your_random_value < 0.5:
    continueRoutine = False
    continueRoutine = True

I assumed that you use an interval of 0 - 1.

1 Like

Thank you¡
I have tried this code. However, the sound is played in all the trials and not just in the 25% of them. Also, I need that the audio should be played with a delay of 0.75 ms. That’s why there is that delay condition. I can’t figure out how to make that my audio will play only in the 25% of the trials.

This is the code written in ‘Begin Routine’. And in ‘Begin Experiment’ is just import random.

r = random.random()
tone = ‘ondasin.wav’

if r < 0.25:
value = 1
vol = 1
value = 0
vol = 0
delay = random.random()/2 + 0.25

Thanks so much

Hello Adriana

Ok, you shouldn’t import the random library when using the Builder. It will import all the libraries it needs. With the approach you have chosen, you determine the probability of each trial being played or not. But you write that you want the sound to be played in 25% or all trials. This is different.

Suppose you have 12 trials and you want a sound to play in 25% of the trials, you write this in the Begin Experiment tab of a code component:

PlayOrNotList = [0]*9 + [1]*3

This creates a list of 9 times ‘0’ and 3 times ‘1’. This list is then shuffled.

In the Begin routine tab, write:
PlayOrNot = PlayOrNotList.pop()

if PlayOrNot:
    continueRoutine = True
    continueRoutine = False

Now, the routine is continued in 25% of your trials.

Do you really mean a delay of 0.75 milliseconds? And a delay relative to what? To the start of the routine? Do you need a random delay, as you have specified, or a fixed delay, as you have written?

Best wishes Jens

Hello Jens

Thank you so much. I will try this code. The delay is constant. There are two sound objects that will play at the same time. The first one contains 108 audios and, the second one is the sound I want to be played randomly in the 25% of trials of the first object of sounds (the 108 audios). So, after one audio, from the 108, there must be a delay of 0.75ms before the second one is presented.

Also, I want these audios to sound regardless of whether the participant answers or not. Thank you so much

Hello Adriana

do you really mean 0.75 milliseconds? Less than 1 millisecond? Notice that you are working at the edge of what PsychoPy is capable of with this mini-delay. See here

or here

On a Win 10-computer an auditory stimulus has a mean lag of 0.56 ms! The reported accuracies are valid for the reported system. Your system might vary from that.

Best wishes Jens