[LAD] Time & How to approach it

Olivier Guilyardi list at samalyse.com
Wed Jan 20 18:35:12 UTC 2010


On 01/20/2010 01:56 PM, Harry Van Haaren wrote:

> I'm trying to understand how programs deal with time passing.
> JACK has a transport mechanism, which also provides BeatBarTick &
> Position info.

Okay, then I'll explain the basic idea of timing with JACK midi, which is
special because sample synchronized. If you want to use ALSA midi directly then
it's different and not covered below.

So currently, in JACK you can either create audio or midi ports.

Every time your jack process() is called you have to deal with a buffer of N
audio frames on your audio ports, where N is the jack buffer size.

The jack_position_t::frame, as set by jack_transport_query(), indicates the
transport frame position of (the first frame of) this buffer.

Now what about a midi output ports? They are meant to contain midi events, which
you can write into the midi port buffer using either jack_midi_event_reserve()
or jack_midi_event_write().

Both of these functions accept a time argument which is meant to be the frame
offset of the event relative to the beginning of the buffer.

Now putting things together, assuming that you want a midi event to occur at
00:00:10 (H:M:S), here's an untested example of what your process() might contain:

double event_time             = 10; // seconds
jack_nframes_t event_framepos = event_time * jack_get_sample_rate(client);
jack_nframes_t buffer_size    = jack_get_buffer_size(client);
jack_position_t position;

jack_transport_query(client, &position);
jack_nframes_t event_offset = event_framepos - position.frame;

void* port_buffer = jack_port_get_buffer(midi_output_port, nframes);
unsigned char* buffer;
jack_midi_clear_buffer(port_buffer);

if (event_offset >= 0 && event_offset < buffer_size) {
    jack_midi_event_write(port_buffer, event_offset, ...);
}

> How can I interpret this data, so that I can have a midi note play at
> BeatBarTick 0,2,0 (ie: 2nd beat in a bar)?

For BBT I guess that you just need to compute the event_framepos of my above
example using the various values (beats_per_minute, beats_per_bar, etc...) of
jack_position_t.

I also recommend that you look at the (tested) JACK midi example clients.

Hope that helps

--
  Olivier




More information about the Linux-audio-dev mailing list