13
0

temporal: add BBT_Time::round_up_to_beat_div()

This commit is contained in:
Paul Davis 2023-09-04 10:46:52 -06:00
parent 7c5e7ddf59
commit f1f5df7c9a
2 changed files with 20 additions and 0 deletions

View File

@ -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,

View File

@ -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