diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index f36a3eac3b..9a5dd01969 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -4408,6 +4408,7 @@ TempoMap::midi_clock_beat_at_or_after (samplepos_t const pos, samplepos_t& clk_p TempoPoint const & tp (tempo_at (sc)); Temporal::Beats b = (tp.quarters_at_sample (pos)).round_up_to_beat (); + again: /* We cannot use * clk_pos = sample_at (b); * because in this case we have to round up to the start @@ -4423,19 +4424,19 @@ TempoMap::midi_clock_beat_at_or_after (samplepos_t const pos, samplepos_t& clk_p */ clk_beat = b.get_beats () * 4 ; // 4 = 24 / 6; - /* It can happen (mostly due to odd plugin latency) that the @p pos - * argument is a few samples past the audio time of the current - * beat. For example, beat 20 @ 120 bpm @ 44100Hz would be at sample - * 441000 but the process() call that gets us here might start at - * 441002. Check if the error is more than 1 tick at the relevant - * tempo, and if so, round up the start (@p pos) + /* It can happen that the computed beat is actually slightly before + * pos. For example, if @p pos is 441002, this is less than 1 tick + * beyond beat 20 (at 120bpm, 44100Hz, 4/4). The call to + * quarters_at_sample(pos) will return 20:0, which does not (and + * cannot) be rounded up). But when we convert that back to a sample + * time, we get 441000, which is before the time (@p pos) that we are + * meant to be computing for. So .. advance to the next beat, and do it + * again. */ if (clk_pos < pos) { - samplecnt_t one_tick_in_superclocks (tp.superclocks_per_note_type_at (timepos_t (sc))/ ((tp.note_type() * 1920) / 4)); - if (abs (clk_pos - pos) < superclock_to_samples (one_tick_in_superclocks, TEMPORAL_SAMPLE_RATE)) { - clk_pos = pos; - } + b += Beats (1, 0); + goto again; } }