Generating parallel port triggers upon detection of a vocal response - need help!

[I previously posted this on stack overflow but I was advised to bring it here.] I’ve created an experiment in psychopy builder in which participants must vocally name pictures presented onscreen (for example, if a picture of a chair appears, the participant has to respond by saying “chair”). I’ve set up a code component to detect each vocal response, which ends the trial and initiates the next one. This part of the experiment works well, however I’m having trouble integrating EEG recording.

Some important information: My trial loop reads images and triggerVal’s out of a .csv file. I have an image component (called english_naming) that displays images for participants to name out-loud. The component’s STOP field is defined as $vpvk.event_onset - this forces the trial to end and the next one to begin upon detection of a vocal response.

So, here is my (working) code component at present:

Begin Experiment:

from psychopy import parallel
port = parallel.port(address=61432)

Begin Routine

vpvk = vk.onsetVoiceKey(
sec=10) # creates the voice key

vpvk.start() #starts recording.

port.setData(triggerVal) # tells psychopy to read trigger values from the .csv file

End Routine

vpvk.stop() # ends the recording

port.setData(0) # resets the trigger value to 0 for the start of the next trial

My problem is this

At present, parallel port events are time-locked to the start of each trial, but I need them to be time-locked to participant’s vocal responses. I tried inserting if vpvk.event_onset(): above port.setData(triggerVal), but this fails to generate any trigger codes at all. I’ve also tried if english_naming==FINISHED but the same problem occurred. I’ve tried a bunch of variants on these two lines of code, but nothing I can think of seems to work.

I would really really appreciate any advice on this problem. Thanks in advance!

I’m not familiar with using this class (hopefully someone else can chime in with useful advice). But as a first step, shift your checking of the event onset to the Each frame tab of the code component. Currently you are checking in the Begin routine tab. That code will only be executed once per trial, at the beginning of the trial. By definition, there shouldn’t be a response at that stage, as the recording is only just about to start when you do the check, and so a trigger will never get sent.

Code in the Each frame tab, however, is executed once per screen refresh, so you will get continuous checks throughout the trial (typically at 60 Hz).

Lastly, you should probably still send a trigger at the start of the trial, regardless. Even onsets will likely lag the true onset as it takes some time to calculate them in real-time. You will want a reliable marker of the trial start time, as you might find that it is more accurate to calculate voice onsets off-line, and the trial start markers could provide useful reference points to apply the onsets to.