From 99ba22a4a4e1d6ccd99bdc7845ced2419cda4b8b Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 10 Jan 2025 09:25:03 -0700 Subject: [PATCH] clean up/improve API used in 5ebf8152e --- libs/temporal/tempo.cc | 17 +++++++++++++---- libs/temporal/temporal/tempo.h | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index f2443b4b91..1cc9a4928d 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -346,6 +346,12 @@ Meter::bbt_subtract (Temporal::BBT_Time const & bbt, Temporal::BBT_Offset const return Temporal::BBT_Time (r.bars, r.beats, r.ticks); } +Temporal::Beats +Meter::round_to_beat (Temporal::Beats const & b) const +{ + return b.round_to_multiple (Beats::ticks (ticks_per_grid())); +} + Temporal::BBT_Time Meter::round_to_bar (Temporal::BBT_Time const & bbt) const { @@ -1353,12 +1359,15 @@ TempoMap::set_tempo (Tempo const & t, timepos_t const & time) /* tempo changes are required to be on-beat */ - TempoMetric metric (metric_at (time.beats(), false)); - Beats on_beat = time.beats().round_to_multiple (Beats::ticks (metric.meter().ticks_per_grid())); + Beats on_beat = metric_at (time.beats(), false).round_to_beat (time.beats()); superclock_t sc; BBT_Time bbt; - metric = metric_at (on_beat, false); + /* the metric for the on-beat position may be different than + * the one for the raw time, so look it up again. + */ + + TempoMetric metric (metric_at (on_beat, false)); bbt = metric.bbt_at (on_beat); sc = metric.superclock_at (on_beat); @@ -1376,7 +1385,7 @@ TempoMap::set_tempo (Tempo const & t, timepos_t const & time) /* tempo changes must be on beat */ - beats = tm.quarters_at_superclock (sc).round_to_multiple (Beats::ticks (tm.meter().ticks_per_grid())); + beats = tm.round_to_beat (tm.quarters_at_superclock (sc)); bbt = tm.bbt_at (beats); /* recompute superclock position of rounded beat */ diff --git a/libs/temporal/temporal/tempo.h b/libs/temporal/temporal/tempo.h index 549749fc82..4cb626feaf 100644 --- a/libs/temporal/temporal/tempo.h +++ b/libs/temporal/temporal/tempo.h @@ -310,6 +310,8 @@ class LIBTEMPORAL_API Meter { BBT_Time round_to_beat (BBT_Time const &) const; Beats to_quarters (BBT_Offset const &) const; + Beats round_to_beat (Beats const &) const; + XMLNode& get_state () const; int set_state (XMLNode const&, int version); @@ -472,6 +474,7 @@ class LIBTEMPORAL_API TempoMetric BBT_Argument bbt_subtract (BBT_Time const & bbt, BBT_Offset const & sub) const { return BBT_Argument (reftime(), _meter->bbt_subtract (bbt, sub)); } BBT_Argument round_to_bar (BBT_Time const & bbt) const { return BBT_Argument (reftime(), _meter->round_to_bar (bbt)); } Beats to_quarters (BBT_Offset const & bbo) const { return _meter->to_quarters (bbo); } + Beats round_to_beat (Beats const & b) const { return _meter->round_to_beat (b); } /* combination methods that require both tempo and meter information */