[linux-audio-dev] Synchronizing sound to MIDI events?

Denis de Leeuw Duarte den_ddld at hotmail.com
Wed Jul 16 18:56:00 UTC 2003


Hello, (I'm new to this list, so hi everyone!)

I'm rather stuck on the following: I'm writing an app that uses JACK for its 
audio output.  I now want to control this app using midi but I have trouble 
figuring out how to synchronize the rendered sound to the incoming events. 
The events, midi notes for example, come in with timestamps in one thread. 
Another thread (the one entered by process()) renders the audio. In order to 
render properly, it would need to calculate the exact sample at which the 
incoming note should begin to take effect in the rendered output stream.

If you have an evenly spaced font, here's a graphical representation of the 
problem:

|...e.....e|e....e....|...ee...e.|.....e.e.e|....e...e.| midi events
|..........|...rrr....|.rr.......|......rrr.|....rrrr..| rendering
|..........|..........|ssssssssss|ssssssssss|ssssssssss| sound

Here, the e's represent midi events (but could be gui events just as well). 
The r's in the second bar represent the calls to the process function of my 
app. During this time, the audio that will be played back during the next 
cycle will be rendered. The s'es in the third bar represent the actual sound 
as it was rendered during the previous block. The vertical bars represent 
blocks of time equivalent to the buffer size.

The best I can think of now is that I have to record midi events during the 
first block, process these into audio during the second block (because I 
want to take into account all events that occured during the first block) so 
it can be played back during the third. Now, all is fine, but time in the 
event-bar is measured in seconds and fractions thereof, but time in the 
third bar is measured in samples. How can I translate the time recorden in 
the events (seconds) to time in samples? How can I know at which exact time 
relative to the current playback time my process() method was called?

If I just measure time at the start of my application I'm afraid things will 
drift. Is that correct? How have other people solved this problem? Hope 
somebody can help!

Regards,

Denis

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail




More information about the Linux-audio-dev mailing list