How to get participants to see a message if they don't respond fast enough (using microphone recordings)

Hello!

I am trying to create an experiment in which participants see a word on the screen (ICE) and must respond with the appropriate answer (CREAM) by speaking into a microphone. I would like a buzzer sound and message to appear if participants do not respond fast enough.

I’ve inserted a pared version of my experiment here. I’ve inserted code trying to get the microphone to detect when voice onset occurs, and that if a sound occurs by a certain point (time_out = 2 seconds), the participant will proceed through all of the stimuli. If the microphone does not detect a voice onset by the time_out time, the buzzer routine should play, consisting of a message (“Respond faster”) and a tone. The participant should then be able to proceed through the rest of the words.

I know psychopy has a voicekey.Onset function, but I’ve been unable to even import it (import psychopy.voicekey as vk) without running into issues regarding missing libraries. Therefore I’ve tried doing this manually and I’m running into issues. (Although if anyone has any idea about how to import voicekey on a MacOS, that can work as well.)

I will insert my code here. I set a threshold of 500 (I found this number on another resource page) and essentially I am trying to see that if the mic reports below threshold by a certain time, the buzzer routine should play. If not, this means participants have said something and should proceed to the next trial. I’m not the best coder, and right now I’ve been getting error messages saying mic isn’t iterable. I chose mic as an object here because I thought that’s what PsychoPy saves audio recordings under.

In the Begin Experiment tab:

THRESHOLD = 500

Begin Routine:

skip_buzzer = False
buzzer_time = .3;
time_out = 2;

def is_silent(mic):
    "Returns 'True' if below the 'silent' threshold"
    return max(mic) < THRESHOLD

Each frame:

if max(mic) < THRESHOLD is True:
    skip_buzzer = True

Any help would be appreciated! And I can clarify anything that was unclear.
voicekeytest.psyexp (17.1 KB)