From c5ecd14622adab53f8fd8b6da14f1623c9776c25 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Thu, 25 Mar 2021 11:29:45 -0600 Subject: [PATCH] temporal: fix logic errors in setting up TempoMetric objects will likely revisit this to avoid code-duplication and "optimize" traversal of _points list --- libs/temporal/tempo.cc | 51 +++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index 7e656a4285..3b74bdc159 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -1665,20 +1665,26 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t start, superclock_t end, u * grid, depending on whether or not it is a multiple of bar_mod. */ - for (tp = &_tempos.front(), mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->sclock() < start; ++p) { + for (tp = &_tempos.front(), p = _points.begin(); p != _points.end() && p->sclock() < start; ++p) { TempoPoint* tpp; - MeterPoint* mpp; - DEBUG_TRACE (DEBUG::Grid, string_compose ("Looking at a point %1\n", *p)); + DEBUG_TRACE (DEBUG::Grid, string_compose ("Looking at a point tempo %1\n", *p)); if ((tpp = dynamic_cast (&(*p))) != 0) { - DEBUG_TRACE (DEBUG::Grid, "set tempo with that\n"); + DEBUG_TRACE (DEBUG::Grid, string_compose ("set tempo with that (check: %1 < %2)\n", p->sclock(), start)); tp = tpp; } + } + + for (mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->sclock() < start; ++p) { + + MeterPoint* mpp; + + DEBUG_TRACE (DEBUG::Grid, string_compose ("Looking at a point for meter %1\n", *p)); if ((mpp = dynamic_cast (&(*p))) != 0) { - DEBUG_TRACE (DEBUG::Grid, "set meter with that\n"); + DEBUG_TRACE (DEBUG::Grid, string_compose ("set meter with that (check: %1 < %2)\n", p->sclock(), start)); mp = mpp; } @@ -1717,14 +1723,17 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t start, superclock_t end, u DEBUG_TRACE (DEBUG::Grid, string_compose ("new bbt for start (rounded up) = %1\n", bbt)); - for (tp = &_tempos.front(), mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->sclock() < start; ++p) { + for (tp = &_tempos.front(), p = _points.begin(); p != _points.end() && p->bbt() < bbt; ++p) { TempoPoint* tpp; - MeterPoint* mpp; if ((tpp = dynamic_cast (&(*p))) != 0) { tp = tpp; } + } + + for (mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->bbt() < bbt; ++p) { + MeterPoint* mpp; if ((mpp = dynamic_cast (&(*p))) != 0) { mp = mpp; @@ -1735,6 +1744,7 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t start, superclock_t end, u /* reset metric */ metric = TempoMetric (*tp, *mp); + DEBUG_TRACE (DEBUG::Grid, string_compose ("metric in effect(2) at %1 = %2\n", start, metric)); /* recompute superclock position */ @@ -1776,7 +1786,7 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t start, superclock_t end, u bbt = bar; - for (tp = &_tempos.front(), mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->sclock() < start; ++p) { + for (tp = &_tempos.front(), p = _points.begin(); p != _points.end() && p->bbt() < bbt; ++p) { TempoPoint* tpp; MeterPoint* mpp; @@ -1784,17 +1794,20 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t start, superclock_t end, u if ((tpp = dynamic_cast (&(*p))) != 0) { tp = tpp; } + } + + for (mp = &_meters.front(), p = _points.begin(); p != _points.end() && p->bbt() < bbt; ++p) { + MeterPoint* mpp; if ((mpp = dynamic_cast (&(*p))) != 0) { mp = mpp; } - } /* reset metric */ metric = TempoMetric (*tp, *mp); - + DEBUG_TRACE (DEBUG::Grid, string_compose ("metric in effect(3) at %1 = %2\n", start, metric)); start = metric.superclock_at (bbt); } else { @@ -2765,7 +2778,6 @@ TempoMap::metric_at (superclock_t sc, bool can_match) const MeterPoint const * mpp = 0; TempoPoint const * prev_t = &_tempos.front(); - MeterPoint const * prev_m = &_meters.front(); /* Yes, linear search because the typical size of _points * is 2, and extreme sizes are on the order of 10-100 @@ -2777,6 +2789,11 @@ TempoMap::metric_at (superclock_t sc, bool can_match) const if ((tpp = dynamic_cast (&(*p)))) { prev_t = tpp; } + } + + MeterPoint const * prev_m = &_meters.front(); + + for (p = _points.begin(); p != _points.end() && p->sclock() < sc; ++p) { if ((mpp = dynamic_cast (&(*p)))) { prev_m = mpp; } @@ -2818,7 +2835,6 @@ TempoMap::metric_at (Beats const & b, bool can_match) const MeterPoint const * mpp = 0; TempoPoint const * prev_t = &_tempos.front(); - MeterPoint const * prev_m = &_meters.front(); /* Yes, linear search because the typical size of _points * is 2, and extreme sizes are on the order of 10-100 @@ -2830,6 +2846,11 @@ TempoMap::metric_at (Beats const & b, bool can_match) const if ((tpp = dynamic_cast (&(*p)))) { prev_t = tpp; } + } + + MeterPoint const * prev_m = &_meters.front(); + + for (p = _points.begin(); p != _points.end() && p->beats() < b; ++p) { if ((mpp = dynamic_cast (&(*p)))) { prev_m = mpp; } @@ -2871,7 +2892,6 @@ TempoMap::metric_at (BBT_Time const & bbt, bool can_match) const MeterPoint const * mpp = 0; TempoPoint const * prev_t = &_tempos.front(); - MeterPoint const * prev_m = &_meters.front(); /* Yes, linear search because the typical size of _points * is 2, and extreme sizes are on the order of 10-100 @@ -2883,6 +2903,11 @@ TempoMap::metric_at (BBT_Time const & bbt, bool can_match) const if ((tpp = dynamic_cast (&(*p)))) { prev_t = tpp; } + } + + MeterPoint const * prev_m = &_meters.front(); + + for (p = _points.begin(); p != _points.end() && p->bbt() < bbt; ++p) { if ((mpp = dynamic_cast (&(*p)))) { prev_m = mpp; }