From 6cde95848046d516e20295d3ef2fb4e2cc0d6f61 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Mon, 13 Feb 2023 16:46:54 -0700 Subject: [PATCH] BBT_Time: provide conversion to/from integer format This is not guaranteed to be lossless, but with 44 bits for bars, 256 beats per bar and up to 4095 ticks per beat, it should be fine. --- libs/temporal/bbt_time.cc | 18 ++++++++++++++++++ libs/temporal/temporal/bbt_time.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/libs/temporal/bbt_time.cc b/libs/temporal/bbt_time.cc index 5c876b0a38..c03b9301a1 100644 --- a/libs/temporal/bbt_time.cc +++ b/libs/temporal/bbt_time.cc @@ -23,6 +23,24 @@ using namespace Temporal; +int64_t +BBT_Time::as_integer () const +{ + /* up to 256 beats in a bar, 4095 ticks in a beat, + and whatever is left for bars (a lot!) + */ + return (((int64_t) bars)<<20)|(beats<<12)|ticks; +} + +BBT_Time +BBT_Time::from_integer (int64_t v) +{ + int32_t B = v>>20; + int32_t b = (v>>12) & 0xff; + int32_t t= v & 0xfff; + return BBT_Time (B, b, t); +} + BBT_Time BBT_Time::round_up_to_bar() const { diff --git a/libs/temporal/temporal/bbt_time.h b/libs/temporal/temporal/bbt_time.h index 010d28ec31..a622f9e1a6 100644 --- a/libs/temporal/temporal/bbt_time.h +++ b/libs/temporal/temporal/bbt_time.h @@ -54,6 +54,9 @@ struct LIBTEMPORAL_API BBT_Time int32_t beats; int32_t ticks; + int64_t as_integer() const; + static BBT_Time from_integer (int64_t); + bool is_bar() const { return beats == 1 && ticks == 0; } bool is_beat() const { return ticks == 0; }