precise control of audiovisual SOA

Dear all,

I need precise control of audiovisual SOA, with sound onset leading visual onset.  I figured out a way of doing that (see code below) but I’m not sure it’s the best, so I need advice.

VBLref = Screen('Flip', window); % get a reference VBL! Everything else will depend on it!
Screen('FillRect', window, 255, rect); % draw visual stimulus!
Screen('DrawingFinished', window); % useful for better timing if visual stimulus is complex!
PsychPortAudio('RescheduleStart', pahandle, VBLref + audDelay, 0); % delay audio by audDelay Re: VBLref!!
[VBL, trueVisualOnset] = Screen('Flip', window, VBLref + (3 - 0.5)*ifi); % delay visual by 3 frames Re: VBLref!!
Screen('Flip', window, VBL + (nFrames - 0.5)*ifi); % remove visual stimulus, so that stimulus duration is nFrames

As a result, the AV asynchrony would be precisely = 3*ifi - audDelay

If you use pause() or WaitSecs(), please ignore this…

Thanks!
Best,

Yinan