13
0

a better solution for the problem being addressed in b40513ff7

This commit is contained in:
Paul Davis 2023-10-22 17:31:29 -06:00
parent a5a1caf9fa
commit ea02c49856

View File

@ -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;
}
}