From fe6beee68e86b9f1f329f72b9d8a5d6639404739 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sun, 26 Mar 2023 22:30:19 -0600 Subject: [PATCH] further steps towards correct (or more usable) MClk output --- libs/ardour/ardour/ticker.h | 2 +- libs/ardour/ticker.cc | 54 +++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/libs/ardour/ardour/ticker.h b/libs/ardour/ardour/ticker.h index 82c850b12e..570b1feee3 100644 --- a/libs/ardour/ardour/ticker.h +++ b/libs/ardour/ardour/ticker.h @@ -59,7 +59,7 @@ private: void reset (); void resync_latency (bool); double one_ppqn_in_samples (samplepos_t transport_position) const; - void sub_tick (samplepos_t start, samplepos_t end, pframes_t n_samples, samplecnt_t& pre_roll, double speed); + void sub_tick (samplepos_t start, samplepos_t end, pframes_t n_samples, samplecnt_t& pre_roll, double speed, pframes_t offset); void send_midi_clock_event (pframes_t offset, pframes_t nframes); void send_start_event (pframes_t offset, pframes_t nframes); diff --git a/libs/ardour/ticker.cc b/libs/ardour/ticker.cc index a684698d48..ccb8c96943 100644 --- a/libs/ardour/ticker.cc +++ b/libs/ardour/ticker.cc @@ -83,7 +83,6 @@ MidiClockTicker::resync_latency (bool playback) void MidiClockTicker::tick (ProcessedRanges const & pr, pframes_t n_samples, samplecnt_t pre_roll) { - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("tick for %1 ranges (%2) w/preroll %3\n", pr.cnt, n_samples, pre_roll)); const pframes_t full_nsamples = n_samples; samplecnt_t total = pr.end[0] - pr.start[0]; @@ -93,6 +92,9 @@ MidiClockTicker::tick (ProcessedRanges const & pr, pframes_t n_samples, samplecn } const double speed = total / n_samples; + pframes_t offset = 0; + + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("tick for %1 ranges (%2) w/preroll %3 @ %4, currently rolling %5\n", pr.cnt, n_samples, pre_roll, speed, _rolling)); _midi_port->cycle_start (n_samples); @@ -100,7 +102,7 @@ MidiClockTicker::tick (ProcessedRanges const & pr, pframes_t n_samples, samplecn * ensure we have sent a stop message, reset and be done. */ - if (!Config->get_send_midi_clock () || pr.start[0] == pr.end[0] || pr.end[0] < pr.start[0]) { /*|| !TransportMasterManager::instance().current()*/ + if (!Config->get_send_midi_clock () || (pr.start[0] == pr.end[0]) || (pr.end[0] < pr.start[0])) { /*|| !TransportMasterManager::instance().current()*/ if (_rolling) { send_stop_event (0, n_samples); DEBUG_TRACE (DEBUG::MidiClock, "stopped sending!\n"); @@ -172,7 +174,8 @@ MidiClockTicker::tick (ProcessedRanges const & pr, pframes_t n_samples, samplecn goto out; } - sub_tick (pr.start[0], pr.end[0], n_samples, pre_roll, speed); + sub_tick (pr.start[0], pr.end[0], n_samples, pre_roll, speed, offset); + offset = pr.end[0] - pr.start[0]; if (pr.cnt > 1) { @@ -193,11 +196,11 @@ MidiClockTicker::tick (ProcessedRanges const & pr, pframes_t n_samples, samplecn _beat_pos = beat_pos; _next_tick = clk_pos - _mclk_out_latency.max; - send_position_event (_beat_pos, 0, n_samples); // consider sending this early - send_continue_event (_next_tick - pr.start[1], n_samples); + send_position_event (_beat_pos, offset, n_samples); // consider sending this early + send_continue_event (offset + (_next_tick - pr.start[1]), n_samples); } - sub_tick (pr.start[1], pr.end[1], n_samples, pre_roll, speed); + sub_tick (pr.start[1], pr.end[1], n_samples, pre_roll, speed, offset); } _transport_pos = pr.end[pr.cnt - 1]; @@ -215,9 +218,9 @@ out: */ void -MidiClockTicker::sub_tick (samplepos_t start_sample, samplepos_t end_sample, pframes_t n_samples, samplecnt_t& pre_roll, double speed) +MidiClockTicker::sub_tick (samplepos_t start_sample, samplepos_t end_sample, pframes_t n_samples, samplecnt_t& pre_roll, double speed, pframes_t offset) { - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("sub-tick for %1 .. %2 (%3) w/preroll %4\n", start_sample, end_sample, n_samples, pre_roll)); + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("sub-tick for %1 .. %2 (%3) w/preroll %4 offset = %5\n", start_sample, end_sample, n_samples, pre_roll, offset)); /* silence buffer */ @@ -231,27 +234,26 @@ MidiClockTicker::sub_tick (samplepos_t start_sample, samplepos_t end_sample, pfr _beat_pos = beat_pos; _next_tick = clk_pos - _mclk_out_latency.max; + } - if (_next_tick >= start_sample && _next_tick < end_sample) { - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Start rolling at %1 beat-pos: %2\n", _next_tick, _beat_pos)); + if (_next_tick >= start_sample && _next_tick < end_sample) { + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Start rolling at %1 beat-pos: %2\n", _next_tick, _beat_pos)); - _rolling = true; - _clock_cnt = 0; - - if (_beat_pos == 0 && _next_tick == 0 && start_sample == 0) { - send_start_event (0, n_samples); - } else { - send_position_event (_beat_pos, 0, n_samples); // consider sending this early - send_continue_event (_next_tick - start_sample, n_samples); - } - } else { - return; - } - - _rolling = true; + _rolling = true; _clock_cnt = 0; - DEBUG_TRACE (DEBUG::MidiClock, string_compose ("next tick reset to %1 from %2 + %3 & %4\n", _next_tick, start_sample, _mclk_out_latency.max, clk_pos)); + if (_beat_pos == 0 && _next_tick == 0 && start_sample == 0) { + send_start_event (offset, n_samples); + } else { + send_position_event (_beat_pos, offset, n_samples); // consider sending this early + send_continue_event (offset + (_next_tick - start_sample), n_samples); + } + + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("next tick reset to %1 from %2 + %3\n", _next_tick, start_sample, _mclk_out_latency.max)); + + } else { + DEBUG_TRACE (DEBUG::MidiClock, string_compose ("not time for next tick at %1 from %2 + %3\n", _next_tick, start_sample, _mclk_out_latency.max)); + return; } } @@ -259,7 +261,7 @@ MidiClockTicker::sub_tick (samplepos_t start_sample, samplepos_t end_sample, pfr DEBUG_TRACE (DEBUG::MidiClock, string_compose ("Tick @ %1 cycle: %2 .. %3 nsamples: %4, ticker-pos: %5\n", _next_tick, start_sample, end_sample, n_samples, _transport_pos)); - send_midi_clock_event (_next_tick - start_sample, n_samples); + send_midi_clock_event (offset + (_next_tick - start_sample), n_samples); if (++_clock_cnt == 6) { _clock_cnt = 0;