# How to present an auditory stimuli in some trials

**URL:** <https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593>\
**Category:** Builder\
**Created:** [April 27, 2023, 7:13pm UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593 "2023-04-27T19:13:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADRIANA\_MARIEL\_CHACH](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/adriana_mariel_chach/32/34029_2.png) [@ADRIANA\_MARIEL\_CHACH](https://discourse.psychopy.org/u/ADRIANA_MARIEL_CHACH)\
**Post date:** [April 27, 2023, 7:13pm UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/1 "2023-04-27T19:13:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 28, 2023, 11:31am UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/2 "2023-04-28T11:31:42Z")

</div>

Hello,

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

```auto
if your_random_value < 0.5:
    continueRoutine = False
else:
    continueRoutine = True

```

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

---

<div class="post-metadata">

**Author:** ![ADRIANA\_MARIEL\_CHACH](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/adriana_mariel_chach/32/34029_2.png) [@ADRIANA\_MARIEL\_CHACH](https://discourse.psychopy.org/u/ADRIANA_MARIEL_CHACH)\
**Post date:** [May 11, 2023, 3:41pm UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/3 "2023-05-11T15:41:49Z")

</div>

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  
else:  
value = 0  
vol = 0  
delay = random.random()/2 + 0.25

Thanks so much

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [May 12, 2023, 7:24am UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/4 "2023-05-12T07:24:18Z")

</div>

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:

```auto
PlayOrNotList = [0]*9 + [1]*3
shuffle(PlayOrNotList)

```

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()

```auto
if PlayOrNot:
    continueRoutine = True
else:
    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

---

<div class="post-metadata">

**Author:** ![ADRIANA\_MARIEL\_CHACH](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/adriana_mariel_chach/32/34029_2.png) [@ADRIANA\_MARIEL\_CHACH](https://discourse.psychopy.org/u/ADRIANA_MARIEL_CHACH)\
**Post date:** [May 12, 2023, 3:50pm UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/5 "2023-05-12T15:50:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [May 12, 2023, 4:21pm UTC](https://discourse.psychopy.org/t/how-to-present-an-auditory-stimuli-in-some-trials/34593/6 "2023-05-12T16:21:26Z")

</div>

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

> **[The timing mega-study: comparing a range of experiment generators, both...](https://peerj.com/articles/9414/)**
>
> Many researchers in the behavioral sciences depend on research software that presents stimuli, and records response times, with sub-millisecond precision. There are a large number of software packages with which to conduct these behavioral...

or here

[https://psychopy.org/timing/2020/table2.html](https://psychopy.org/timing/2020/table2.html)

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
