From 9c2c08973d8948d9679fcf0c74f11624e75f4612 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sun, 30 Oct 2022 19:34:52 -0600 Subject: [PATCH] tempo map: fix incorrect computation of ::bbt_at() (#9049) This caused problems with TempoMap::get_grid() ("gone backwards"). --- libs/temporal/tempo.cc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index 48a81101be..6127e9f5ea 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -627,7 +627,21 @@ TempoMetric::bbt_at (timepos_t const & pos) const superclock_t sc = pos.superclocks(); - const Beats dq = _tempo->quarters_at_superclock (sc) - _meter->beats(); + /* Use the later of the tempo or meter as the reference point to + * compute the BBT distance. All map points are fully defined by all 3 + * time types, but we need the latest one to avoid incorrect + * computations of quarter duration. + */ + + const Point* reference_point; + + if (_tempo->beats() < _meter->beats()) { + reference_point = _meter; + } else { + reference_point = _tempo; + } + + const Beats dq = _tempo->quarters_at_superclock (sc) - reference_point->beats(); DEBUG_TRACE (DEBUG::TemporalMap, string_compose ("qn @ %1 = %2, meter @ %3 , delta %4\n", sc, _tempo->quarters_at_superclock (sc), _meter->beats(), dq)); @@ -641,8 +655,9 @@ TempoMetric::bbt_at (timepos_t const & pos) const const BBT_Offset bbt_offset (0, note_value_count, dq.get_ticks()); - DEBUG_TRACE (DEBUG::TemporalMap, string_compose ("BBT offset from meter @ %1: %2\n", _meter->bbt(), bbt_offset)); - return _meter->bbt_add (_meter->bbt(), bbt_offset); + DEBUG_TRACE (DEBUG::TemporalMap, string_compose ("BBT offset from %3 @ %1: %2\n", (_tempo->beats() < _meter->beats() ? _meter->bbt() : _tempo->bbt()), bbt_offset, + (_tempo->beats() < _meter->beats() ? "meter" : "tempo"))); + return _meter->bbt_add (reference_point->bbt(), bbt_offset); } superclock_t