From 8e52ea74dafcb2dc13cc8c49982d9f4a771c67f9 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 29 May 2020 22:48:16 +0200 Subject: [PATCH] Add API to lookup MIDI Clock Beat position --- libs/ardour/ardour/tempo.h | 2 ++ libs/ardour/tempo.cc | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/libs/ardour/ardour/tempo.h b/libs/ardour/ardour/tempo.h index c0132fc085..0615cce53c 100644 --- a/libs/ardour/ardour/tempo.h +++ b/libs/ardour/ardour/tempo.h @@ -374,6 +374,8 @@ class LIBARDOUR_API TempoMap : public PBD::StatefulDestructible void get_grid (std::vector&, samplepos_t start, samplepos_t end, uint32_t bar_mod = 0); + void midi_clock_beat_at_of_after (samplepos_t const pos, samplepos_t& clk_pos, uint32_t& clk_beat); + static const Tempo& default_tempo() { return _default_tempo; } static const Meter& default_meter() { return _default_meter; } diff --git a/libs/ardour/tempo.cc b/libs/ardour/tempo.cc index 68081d6707..a7a51bd7d4 100644 --- a/libs/ardour/tempo.cc +++ b/libs/ardour/tempo.cc @@ -4189,6 +4189,24 @@ TempoMap::get_grid (vector& points, } } +void +TempoMap::midi_clock_beat_at_of_after (samplepos_t const pos, samplepos_t& clk_pos, uint32_t& clk_beat) +{ + /* Sequences are always assumed to start on a MIDI Beat of 0 (ie, the downbeat). + * Each MIDI Beat spans 6 MIDI Clocks. In other words, each MIDI Beat is a 16th note + * (since there are 24 MIDI Clocks in a quarter note, therefore 4 MIDI Beats also fit in a quarter). + * So, a master can sync playback to a resolution of any particular 16th note + * + * from http://midi.teragonaudio.com/tech/midispec/seq.htm + */ + Glib::Threads::RWLock::ReaderLock lm (lock); + + /* pulse is a whole note */ + clk_beat = ceil (16.0 * (pulse_at_minute_locked (_metrics, minute_at_sample (pos)))); + clk_pos = sample_at_minute (minute_at_pulse_locked (_metrics, clk_beat / 16.0)); + assert (clk_pos >= pos); +} + const TempoSection& TempoMap::tempo_section_at_sample (samplepos_t sample) const {