From ab3a7dc721e61efd5ef1f31236c657d435286e9f Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 4 Mar 2022 19:40:30 +0100 Subject: [PATCH] Fix bbt_subtract math When r.ticks is already negative, add the negative value (not subtract), likewise handle negative beat offset correctly. --- libs/temporal/tempo.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index cb4d3e718b..3829ae8c86 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -327,18 +327,13 @@ Meter::bbt_subtract (Temporal::BBT_Time const & bbt, Temporal::BBT_Offset const const int32_t tpg = ticks_per_grid (); if (r.ticks < 0) { - r.beats -= (r.ticks / tpg); + r.beats += floor ((double) r.ticks / tpg); r.ticks = tpg + (r.ticks % Temporal::Beats::PPQN); } - if (r.beats < 0) { - - r.beats += 1; - - r.bars -= r.beats / _divisions_per_bar; - r.beats = r.beats % _divisions_per_bar; - - r.beats -= 1; + if (r.beats <= 0) { + r.bars += floor ((r.beats - 1.0) / _divisions_per_bar); + r.beats = _divisions_per_bar + (r.beats % _divisions_per_bar); } if (r.bars <= 0) {