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()
theSound.play(when=now+0.5) # play in EXACTLY 0.5s
kb.clock.reset() # start timing
keys = kb.waitKeys(maxWait = 30,
keyList = ["space"],
waitRelease=False)
theSound.stop()
for key in keys: # should only ever be one, but just in case
print(key.name, 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 3.0.18.2