From 4b9e46f11de1a4f6a9c4d17968098ed34f25edeb Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Fri, 13 Nov 2020 02:27:09 +0900 Subject: [PATCH] Fix normalization in Temporal::Beats for times between -1 and 0 beats Also fix normalization of times with negative beats and positive ticks --- libs/temporal/temporal/beats.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libs/temporal/temporal/beats.h b/libs/temporal/temporal/beats.h index a7c8696db3..757f0c8771 100644 --- a/libs/temporal/temporal/beats.h +++ b/libs/temporal/temporal/beats.h @@ -19,6 +19,7 @@ #ifndef TEMPORAL_BEATS_HPP #define TEMPORAL_BEATS_HPP +#include #include #include #include @@ -42,15 +43,21 @@ public: /** Normalize so ticks is within PPQN. */ void normalize() { // First, fix negative ticks with positive beats - if (_beats >= 0) { - while (_ticks < 0) { - --_beats; - _ticks += PPQN; - } + while (_beats > 0 && _ticks < 0) { + --_beats; + _ticks += PPQN; } + // Now fix positive ticks with negative beats + while (_beats < 0 && _ticks > 0) { + ++_beats; + _ticks -= PPQN; + } + + assert ((_beats < 0 && _ticks <= 0) || (_beats > 0 && _ticks >= 0) || _beats == 0); + // Work with positive beats and ticks to normalize - const int32_t sign = _beats < 0 ? -1 : 1; + const int32_t sign = _beats < 0 ? -1 : _ticks < 0 ? -1 : 1; int32_t beats = abs(_beats); int32_t ticks = abs(_ticks);