Presenting stimuli and randomising interstimulus interval for a duration of 40 seconds

Hello everyone,

First time using psychopy but looks reallly cool!

I would like to present a 0.2s stimulus every 6-10 seconds for a total duration of 40 seconds (the duration of a video I am showing on screen).

Now I created the 6-10 seconds stimulus presentation randomiser by creating a code object and including the following code

interstimulus_interval_randomiser_audio = randint(low = 6, high = 10) # choose a value
thisExp.addData('interstimulus_interval_randomiser_audio', interstimulus_interval_randomiser_audio) # record it in the data file

I then call $interstimulus_interval_randomiser_audio at the start box of my auditory stimulus object. My question is how do I loop this stimulus presentation->randomised ISI (6-10s)->stimulus presentation->… for the duration of my video object (40 seconds video)

Hi There,

You are on the right lines for sure! What makes this a bit tricker is that you want a component to be presented multiple times within the same routine as a the video. So, there are 2 solutions:

  1. add multiple sound components each with its own predetermined onset
  2. do this in code (which I think is the approach you might prefer here!)

For solution 2, you would use the code you have but with a few more considerations. On each frame within the routine we want to detect a) is the tone currently being presented and b) are we currently waiting for a tone to be presented c) if neiter a nor b let’s pick an interval to wait then present the tone (and do this continuously whilst our trial is going on in the background.

Since there are a couple of bits of code here, here is a demo to help you on your way. A tone is presented every 3-5 seconds on a lop for the duration of a 10 second long trial.

toneRandom.psyexp (9.2 KB)

Hope this helps,


Fantastic, thank you very much Becca for your help! I’ve now modified your code to the ISI and trial duration that is applicable to our experiment. Final question, what lines of code should I add for doing the exact same thing but with a picture stimulus?

For a picture, you would replace the initial creation of the sound component with an image (ImageStim — PsychoPy v2021.2) and replace instances of .play() with .draw() (you also wouldn’t need things like .stop() for an imagestim.

Hope this helps!


PS. As you mention you are new to psychopy, heres something that might be useful. If you want to learn more about the code underlying a component or python in general. Make it in builder, then click compile to code Screenshot 2021-01-21 174202 then search the name of your image component in the coder window that opens! More details here Extending Builder with code — Workshops for PsychoPy 2020 2020

1 Like

Thank you very much Becca for your help! Got everything to work as intended except for a new issue where the video I’m playing is always infront of the image (brief distractor) I’m trying to display. Any idea how to fix this?

Make sure your video is above your image in the components within the routine (right click > move up). If it isn’t this it’s probably a different issue, so please could you mark the solution for this thread then start a new topic :blush:


Hi Becca,

Thanks so much for this code!

I was able to use your code to present a text stimulus multiple times within the same routine as a video running for 30 seconds. However, I only want the text stimulus to appear twice randomly in the 30-second video sequence. Is there a way to change your code so the text-stim only appears randomly twice during the video sequence?

Hi There,

If you only want something to happen twice I would use a variable to count how many times something occurred and then present no more if that number is met.


Begin Routine tab:

count = 0

Each Frame Tab:

line 15 (last screenshot):

count += 1

indent lines 6 to 24 and add at the start:

if count < 2:
1 Like

Hi Becca,

Amazing thanks! It works! I just had to put:

count += 1

on line 25 (on the screenshots above). Otherwise, the Fixation wouldn’t draw.

Hi Becca,

your code has been hugely helpful to me when I needed use white noise to insert startle probes into my experiment. Thank you for this!

However, I need to include a second audiory stimulus (scream) that follows a different randomization and is supposed to play 1-3x during the routine while not overlapping with the startle sounds.
Right from the start when I try to modify the code to include the second sound in the ‘Begin Experiment’ tab, Psychopy creates the following error message:

Psychopy sound errror

Hence this code does not work:

I was wondering if there is a way to include two .wav files from different sources into the code while avoiding this error.

Any help would be much appreciated!

These look like two totally different problems.

The Javascript error I’m not sure about because I can’t see the JS code (what you are showing is just the python, please switch code type to “both” or “auto->js” to show the JS code), but my guess is that it’s the “import time” line because that just doesn’t exist in JS. However, w/r/t loading sound files into your program in JS, your best bet is actually to go to the experiment settings and go to the “online” tab, and add them as assets. Pavlovia loads all external files once at the beginning, and if these are files which are not referred to in sound components, it probably won’t know they exist.

The python error, which will not occur when this study is run online, has to do with the fact that different sound files in your experiment have different sampling rates. It’s a windows thing and it’s incredibly annoying. This says that something in your experiment is an audio file or tone with a sample rate of 11025Hz, and you’re trying to load an audio file with a sample rate of 44100Hz (which is much more standard). So you’ll need to look at the files themselves and figure out their sample rates (for Windows I recommend Audacity, which is a free audio editing app), and convert them so they all have the same sample rate.

1 Like

Thank you!
It appears that I was not fully understanding the content of the error message since I am rather new to psychopy and coding in python. I did not have “auto->js” selected because I am creating an experiment that will only be used offline so I was not too concerned with the javascript code.

That there could be a problem with the sound files on windows is a great hint! I will look into the mismatch in sample rates and correct it.