Synchronizing midi input data with audio callback

I’ve been playing around with a small audio application for a while now and while everything else is coming around quite nicely, this particular issue is giving me a headache.
David Olofson already wrote a few very useful pointers in another thread (http://forums.libsdl.org/viewtopic.php?t=7193&highlight=audio+callback) but I still haven’t figured it out.

Ok, if I have audio callback function like this…

Code:

void audioCallback(void *userdata, Uint8 *stream, int len)
{

while(framesLeft) {

processMidiEvents();

    processPlugins(buffer, frames);
    ...
}
....

}

…where processMidiEvents pops a MIDI event from a queue and sends the message (note on/off etc.) to synth plugins. Then processPlugins does all the magic and mixes the sounds to the output buffer.

Now, everything works pretty much as it should, but there is slight jitter in timing. I didn’t even notice this before, as I was only playing live with a MIDI keyboard and it’s hard to get lower than 50ms latencies with SDL audio (nor DirectSound or WSAPI for that matter, ASIO is another story of course). But, for example, if I forward the midi output from an external sequencer to my application you can hear it instantly.

I haven’t had the time to write my own sequencer to the application, so naturally I’d like to be able to use and test with something else. I did try it with a dummy seuquencer (pretty much the same way that David has done it in DrumToy, driven directly from the audio callback) and let it feed a few simple test midi events to the queue. With this approach everything is in sync and it sounds just like it shoud sound.

So, is it possible to get the midi events “in sync” with the audio callback? I can get the timestamps from the winmm midi input callback function, but that doesn’t help me much, or does it?

The problem isn’t in the midi-rerouting, since it works just fine with Reason for example.

Regards,
Pekka Nissinen[/url]