1#ifndef MCC_RHYTHM_NOTE_VALUE_H
2#define MCC_RHYTHM_NOTE_VALUE_H
6#include <MCC_BuildSettings.h>
22 static constexpr uint8_t InvalidBaseExponent = 0xFFu;
23 static constexpr uint8_t MaximumBaseExponent = 8u;
25 uint8_t _baseExponent;
28 static constexpr bool IsDotCountValid(int32_t dots)
noexcept {
29 return dots >= 0 && dots <= 4;
32 constexpr NoteValue(uint8_t baseExponent, int32_t dots) noexcept
33 : _baseExponent(baseExponent <= MaximumBaseExponent && IsDotCountValid(dots)
35 : InvalidBaseExponent),
36 _dots(baseExponent <= MaximumBaseExponent && IsDotCountValid(dots)
37 ?
static_cast<uint8_t
>(dots)
48 constexpr NoteValue() noexcept : _baseExponent(InvalidBaseExponent), _dots(0u) {}
54 static constexpr NoteValue
Whole(int32_t dots = 0) noexcept {
59 static constexpr NoteValue
Half(int32_t dots = 0) noexcept {
64 static constexpr NoteValue
Quarter(int32_t dots = 0) noexcept {
69 static constexpr NoteValue
Eighth(int32_t dots = 0) noexcept {
74 static constexpr NoteValue
Sixteenth(int32_t dots = 0) noexcept {
79 static constexpr NoteValue
ThirtySecond(int32_t dots = 0) noexcept {
84 static constexpr NoteValue
SixtyFourth(int32_t dots = 0) noexcept {
105 int32_t baseDenominator, int32_t dots = 0) noexcept {
106 switch (baseDenominator) {
107 case 1:
return Whole(dots);
108 case 2:
return Half(dots);
110 case 8:
return Eighth(dots);
122 return _baseExponent != InvalidBaseExponent;
131 ?
static_cast<uint16_t
>(uint16_t(1u) << _baseExponent)
143 ?
static_cast<uint16_t
>((uint16_t(1u) << (_dots + 1u)) - 1u)
150 ?
static_cast<uint16_t
>(uint16_t(1u) << (_baseExponent + _dots))
155 constexpr NoteValue
WithDots(int32_t dots)
const noexcept {
160 friend constexpr bool operator==(NoteValue a, NoteValue b)
noexcept {
161 return a._baseExponent == b._baseExponent && a._dots == b._dots;
173 friend constexpr bool operator<(NoteValue a, NoteValue b)
noexcept {
174 return a.IsValid() != b.IsValid()
178 :
static_cast<uint32_t
>(a.Numerator()) * b.Denominator()
179 <
static_cast<uint32_t
>(b.Numerator()) * a.Denominator());
190 friend constexpr bool operator>=(NoteValue a, NoteValue b)
noexcept {
Exact written note length: a power-of-two base and zero to four dots.
Definition NoteValue.h:21
static constexpr NoteValue Invalid() noexcept
Returns the invalid note value.
Definition NoteValue.h:51
constexpr uint8_t DotCount() const noexcept
Returns the number of dots, or InvalidDotCount when invalid.
Definition NoteValue.h:136
static constexpr NoteValue TwoHundredFiftySixth(int32_t dots=0) noexcept
Creates a 256th note, optionally augmented by dots.
Definition NoteValue.h:94
static constexpr uint8_t MaximumDots
Largest supported number of augmentation dots.
Definition NoteValue.h:42
static constexpr NoteValue Eighth(int32_t dots=0) noexcept
Creates an eighth note, optionally augmented by dots.
Definition NoteValue.h:69
constexpr bool IsDotted() const noexcept
Returns true when this valid value has augmentation dots.
Definition NoteValue.h:126
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid note value.
Definition NoteValue.h:121
constexpr uint16_t Numerator() const noexcept
Returns the exact reduced numerator relative to a whole note.
Definition NoteValue.h:141
static MCC_CONSTEXPR14 NoteValue FromDenominator(int32_t baseDenominator, int32_t dots=0) noexcept
Creates a value from the undotted base denominator.
Definition NoteValue.h:104
static constexpr NoteValue Sixteenth(int32_t dots=0) noexcept
Creates a sixteenth note, optionally augmented by dots.
Definition NoteValue.h:74
static constexpr NoteValue OneHundredTwentyEighth(int32_t dots=0) noexcept
Creates a 128th note, optionally augmented by dots.
Definition NoteValue.h:89
static constexpr NoteValue Half(int32_t dots=0) noexcept
Creates a half note, optionally augmented by dots.
Definition NoteValue.h:59
static constexpr NoteValue SixtyFourth(int32_t dots=0) noexcept
Creates a sixty-fourth note, optionally augmented by dots.
Definition NoteValue.h:84
static constexpr uint8_t InvalidDotCount
Sentinel returned by DotCount() for an invalid value.
Definition NoteValue.h:45
static constexpr NoteValue Whole(int32_t dots=0) noexcept
Creates a whole note, optionally augmented by dots.
Definition NoteValue.h:54
constexpr uint16_t Denominator() const noexcept
Returns the exact reduced denominator relative to a whole note.
Definition NoteValue.h:148
constexpr uint16_t BaseDenominator() const noexcept
Returns the undotted denominator (1 through 256), or 0.
Definition NoteValue.h:129
static constexpr NoteValue ThirtySecond(int32_t dots=0) noexcept
Creates a thirty-second note, optionally augmented by dots.
Definition NoteValue.h:79
constexpr NoteValue WithDots(int32_t dots) const noexcept
Returns the same base with dots, or invalid for bad input.
Definition NoteValue.h:155
friend constexpr bool operator<(NoteValue a, NoteValue b) noexcept
Orders by exact duration, with invalid values after valid values. Written values with equal duration ...
Definition NoteValue.h:173
static constexpr NoteValue Quarter(int32_t dots=0) noexcept
Creates a quarter note, optionally augmented by dots.
Definition NoteValue.h:64
friend constexpr bool operator==(NoteValue a, NoteValue b) noexcept
Written equality: the base and dot count must both match.
Definition NoteValue.h:160
constexpr NoteValue() noexcept
Creates the invalid note value.
Definition NoteValue.h:48
Music Composition Core public namespace.
Definition Version.h:4