From f1f5df7c9a715f15ecdc80bd856839150d43d41c Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Mon, 4 Sep 2023 10:46:52 -0600 Subject: [PATCH] temporal: add BBT_Time::round_up_to_beat_div() --- libs/temporal/bbt_time.cc | 19 +++++++++++++++++++ libs/temporal/temporal/bbt_time.h | 1 + 2 files changed, 20 insertions(+) diff --git a/libs/temporal/bbt_time.cc b/libs/temporal/bbt_time.cc index 03b7bf3726..9bc8797619 100644 --- a/libs/temporal/bbt_time.cc +++ b/libs/temporal/bbt_time.cc @@ -56,6 +56,25 @@ BBT_Time::round_up_to_bar() const return b; } +BBT_Time +BBT_Time::round_up_to_beat_div (int beat_div) const +{ + /* XXX this doesn't work where "beats" are not quarters, because + we could have B|b|0 and this is not on a beat_div, even though it is + an integer beat position (think triplets. + */ + + int32_t div_ticks = ticks_per_beat / beat_div; + int32_t ticks_remainder = ticks % div_ticks; + int32_t rounded_up = ticks + div_ticks - ticks_remainder; + + if (rounded_up == ticks_per_beat) { + return BBT_Time (bars, beats+1, 0); + } + + return BBT_Time (bars, beats, rounded_up); +} + BBT_Offset::BBT_Offset (double dbeats) { /* NOTE: this does not construct a BBT time in a canonical form, diff --git a/libs/temporal/temporal/bbt_time.h b/libs/temporal/temporal/bbt_time.h index a622f9e1a6..b7206c4348 100644 --- a/libs/temporal/temporal/bbt_time.h +++ b/libs/temporal/temporal/bbt_time.h @@ -115,6 +115,7 @@ struct LIBTEMPORAL_API BBT_Time BBT_Time round_to_beat () const { return ticks >= (ticks_per_beat/2) ? BBT_Time (bars, beats+1, 0) : BBT_Time (bars, beats, 0); } BBT_Time round_down_to_beat () const { return BBT_Time (bars, beats, 0); } BBT_Time round_up_to_beat () const { return ticks ? BBT_Time (bars, beats+1, 0) : *this; } + BBT_Time round_up_to_beat_div (int beat_div) const; /* cannot implement round_to_bar() without knowing meter (time * signature) information, since it requires knowing how many beats