# How to present a sound randomly in between trials?

**URL:** <https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869>\
**Category:** Builder\
**Created:** [December 9, 2023, 12:53pm UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869 "2023-12-09T12:53:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mori](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mori](https://discourse.psychopy.org/u/Mori)\
**Post date:** [December 9, 2023, 12:53pm UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/1 "2023-12-09T12:53:36Z")

</div>

Hello,  
I am trying to build an emotional dot motion task in builder. I have created a simple loop and a condition file in which my stimuli are presented and the participants answer each trial with left or right keys and it works fine. But i also have a sound file (a siren sound) and i want it to be played only four times randomly in between trials. Is there any way to do this?  
Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [December 14, 2023, 12:21am UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/2 "2023-12-14T00:21:58Z")

</div>

Please could you describe the trial sequence should it be:

trial (left/right judgement) \> 4 sirens with set intervals (presented on 50% of trials for example) \>trial left/right) \> 4 sirens with set intervals (presented on 50% of trials for example) and so on?

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [December 14, 2023, 10:41am UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/3 "2023-12-14T10:41:03Z")

</div>

You could set up a routine that plays the sound after every trial. Then use a code component to skip that routine except on your key values of trials.thisN.

For example, you could set up

Begin Experiment

```auto
sirenTrial = randint(4)+3

```

Begin Routine (siren routine)

```auto
if trials.thisN == sirenTrial:
     sirenTrial += randint(4)+3
else:
     continueRoutine = False

```

---

<div class="post-metadata">

**Author:** ![Mori](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mori](https://discourse.psychopy.org/u/Mori)\
**Post date:** [December 14, 2023, 12:34pm UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/4 "2023-12-14T12:34:23Z")

</div>

Hello,  
thank you for replying…what i wanted to do was to present the subjects with 60 trials of dots and i also had a siren sound that played only four times and each time for 5 second randomly after the dots. for example:

dots (20 trials) \> siren (1st time) \> siren (2nd time) \> dots (15 trials) \> dots (10 trials) \> siren (3rd time) \> dots (15 trials) \> siren (4th time)

---

<div class="post-metadata">

**Author:** ![Mori](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mori](https://discourse.psychopy.org/u/Mori)\
**Post date:** [December 14, 2023, 12:52pm UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/5 "2023-12-14T12:52:02Z")

</div>

Hello,  
What i did 4 days ago was to create 12 inner loops (8 dot loops and 4 siren loops) and a randomizer outer loop:

dot loops (dots\_1 = 6 trials, dots\_2 = 10 trials, dots\_3 = 4 trials, dots\_4 = 6 trials ,dots\_5 = 8 trials, dots\_6 = 6 trials, dots\_7 = 6 trials, dots\_8 = 14 trials) = 60 trials

siren loops ( siren\_1 to siren\_4 = 1 trial each) = 4 trials

randomizer outer loop = present a random combination of inner loops for each participant.

then i randomized my inner loops using the method described in the link below:  
[https://discourse.psychopy.org/t/randomizing-blocks/23590](https://discourse.psychopy.org/t/randomizing-blocks/23590)

and it’s now working i expect.

 ![Screenshot 2023-12-14 153145](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/d/0d2013e67a726b630aeaff4990db2779369fbe51.png)

---

<div class="post-metadata">

**Author:** ![Mori](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mori](https://discourse.psychopy.org/u/Mori)\
**Post date:** [December 15, 2023, 8:06am UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/6 "2023-12-15T08:06:46Z")

</div>

I also tried this code and it plays the sound routine every 4 to 7 trials randomly which is more than the 4 time limit for the sound routine, but what i wanted to do was to present the sound routine only 4 times during the whole loop.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [December 15, 2023, 8:31am UTC](https://discourse.psychopy.org/t/how-to-present-a-sound-randomly-in-between-trials/37869/7 "2023-12-15T08:31:45Z")

</div>

My proposal involves a single loop. I assumed you wanted the positions to be random but if you want them after trials 20, 35, 45 and 60 then try this.

Begin Experiment

```auto
sirenTrials = [19,34,44,59]

```

Begin Routine

```auto
if trials.thisN not in sirenTrials:
     continueRoutine = False

```
