How to get reaction time to an audio stimulus

Hello Wisdom Council

I need to collect reaction time (e.g. space bar) to audio files, with millisecond accuracy. I am confused about scheduling play vs when to start waiting for the keyboard event.

I understand that with PTB, I can schedule a sound to be played at a specific time in the future, and thereby minimize variable latency before the audio starts. My code is:

from psychopy import prefs
prefs.hardware['audioLib'] = ['PTB']
from psychopy import sound
import psychtoolbox as ptb
from psychopy.hardware import keyboard

theSound = sound.backend_ptb.SoundPTB('audioFile.wav')
now = ptb.getSecs() # play in EXACTLY 0.5s
kb.clock.reset()  # start timing
  keys = kb.waitKeys(maxWait = 30,
                                 keyList = ["space"], 
for key in keys: # should only ever be one, but just in case
  print(, key.rt)

So I schedule the audio to play in now+.5 secs, but then I immediately start the timer waiting for the keypress. So I will be timing for a variable time up to .5 secs before the audio even starts. How can I wait exactly until the audio will start playing before I start the timer for the keypress?

I’m using macOS Big Sur 11.4 on a 2017 MacBook Pro, running Python 3.8, PsychoPy 2021.2.3, and psychtoolbox

Hi There,

If there a reason that you are using pure code for this? It would almost certainly be easier and more accurate using the builder (with a sound component and a keyboard component).


Hi Becca, thx for the fast response. I chose coding because I read that it us more flexible than the builder, I assume the Builder is a wrapper around the Coder, and calls the same APIs. Nevertheless it’s possible that the coding solution to my problem might be correctly generated by the Builder. So I’ll give it a try.

I ran the above code on 30 audio files, pressing the space bar as soon as I heard any audio. I also made an external audio recording of the whole session, so that using an audio editor (Audacity) I could measure the real reaction times manually, and compare them with those measured by PsychoPy/PTB. My RTs were between 150 and 600 ms. On average, PsychoPy was 12 ms slower than reality, and it ws much more variable and inconsistent. The standard deviation of the difference between real and measured reaction time across the 30 instances was 16 ms.

If I can get comparable figures from a Builder version, I’ll post them here.

I assume that scheduling the playback in the future (“now+0.5”) would reduce that 16 ms variability. If only I can figure out when to start timing.