Aligning triggers with audio replay

Hi, I am trying to send a serial port pulse at the onset of the replay of a sound file in a trial.

I set up a task in builder and modified it to improve the delay between the pulse and the onset of the audio to 1-5ms. 80% of trials have a delay of 1ms, but some come in at 5ms.

Here the relevant parts of the script (at least what I thought is relevant :)):

prefs.hardware['audioLatencyMode'] = '3'
...
for thisAudio_loop in audio_loop:
    ...
    audio_sample.play(when=win)  # sync with win flip
    win.callOnFlip(send_ttl, ttl_code)
    ...
    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

Does anyone have any advice to further improve that timing, I would be okay with 1ms delays, the few trials that have 5ms would be great to improve on.

Best,
Max