From d6e331fe6cf0396ee5c1e356015a5f4c5bc2bea6 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 22 Aug 2025 10:44:50 -0600 Subject: [PATCH] temporal: new non-iterative implementation of Meter::bbt_delta() --- libs/temporal/tempo.cc | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index bd88a01764..f070f46658 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -360,50 +360,32 @@ Meter::bbt_delta (BBT_Time const & later, BBT_Time const & earlier) const assert (later > earlier); -#if 0 // more efficient but broken +#if 1 // more efficient than iterative method below BBT_Offset d; BBT_Time a (earlier); BBT_Time b (later); - if (a.ticks == ticks_per_grid()) { - - /* Next tick is the next beat */ - - a.beats++; - d.beats++; - + if (a.ticks > b.ticks) { + d.ticks = b.ticks + (ticks_per_grid() - a.ticks); if (a.beats == _divisions_per_bar) { a.beats = 1; a.bars++; - d.bars++; + } else { + a.beats++; } } else { - - /* tick delta is just the remainder to the next beat */ - - d.ticks = ticks_per_grid() - earlier.ticks; + d.ticks = b.ticks - a.ticks; } - if (a.beats == _divisions_per_bar) { - - /* next beat is the next bar */ - - - a.beats = 1; + if (a.beats > b.beats) { + d.beats = b.beats + (_divisions_per_bar - a.beats); a.bars++; - d.bars++; - } else { - - /* beat delta is just the remainder to the next bar */ - - d.beats = _divisions_per_bar - earlier.beats; + d.beats = b.beats - a.beats; } - /* count bars */ - - d.bars = b.bars - a.bars; + d.bars = b.bars - a.bars; return d;