MCC
Loading...
Searching...
No Matches
ChromaticIndex.h
1#ifndef MCC_PITCH_CHROMATIC_INDEX_H
2#define MCC_PITCH_CHROMATIC_INDEX_H
3
4#include <MCC_BuildSettings.h>
5
6#include <stdint.h>
7
8#include <Foundation/Math/Arithmetic.h>
9
10#include <MCC/Pitch/PitchClass.h>
11
12namespace MCC {
13
27public:
29 static constexpr int16_t Minimum = -1528;
30
32 static constexpr int16_t Maximum = 1551;
33
38 static constexpr int16_t InvalidValue = 32767;
39
40private:
41 int16_t _value;
42
43public:
45 constexpr ChromaticIndex() noexcept : _value(InvalidValue) {}
46
51 constexpr explicit ChromaticIndex(int32_t value) noexcept
52 : _value((value >= Minimum && value <= Maximum)
53 ? static_cast<int16_t>(value)
54 : InvalidValue) {}
55
57 static constexpr ChromaticIndex Invalid() noexcept {
58 return ChromaticIndex();
59 }
60
62 constexpr bool IsValid() const noexcept { return _value != InvalidValue; }
63
68 constexpr int16_t Value() const noexcept { return _value; }
69
74 MCC_CONSTEXPR14 MCC::PitchClass PitchClass() const noexcept {
75 if (!IsValid()) {
77 }
78 return MCC::PitchClass(
79 Foundation::Math::FloorMod(_value, MCC::PitchClass::Count));
80 }
81
87 MCC_CONSTEXPR14 ChromaticIndex Transposed(int32_t semitones) const noexcept {
88 if (!IsValid() ||
89 semitones < Minimum - Maximum ||
90 semitones > Maximum - Minimum) {
91 return Invalid();
92 }
93 return ChromaticIndex(static_cast<int32_t>(_value) + semitones);
94 }
95
97 friend constexpr bool operator==(ChromaticIndex a, ChromaticIndex b) noexcept {
98 return a._value == b._value;
99 }
100
101 friend constexpr bool operator!=(ChromaticIndex a, ChromaticIndex b) noexcept {
102 return !(a == b);
103 }
104
106 friend constexpr bool operator<(ChromaticIndex a, ChromaticIndex b) noexcept {
107 return a._value < b._value;
108 }
109
110 friend constexpr bool operator>(ChromaticIndex a, ChromaticIndex b) noexcept {
111 return b < a;
112 }
113
114 friend constexpr bool operator<=(ChromaticIndex a, ChromaticIndex b) noexcept {
115 return !(b < a);
116 }
117
118 friend constexpr bool operator>=(ChromaticIndex a, ChromaticIndex b) noexcept {
119 return !(a < b);
120 }
121};
122
123} // namespace MCC
124
125#endif // MCC_PITCH_CHROMATIC_INDEX_H
Absolute semitone position of a pitch, with origin C-1 = 0.
Definition ChromaticIndex.h:26
static constexpr int16_t Minimum
Lowest valid index: Cbbbb in octave -128.
Definition ChromaticIndex.h:29
static constexpr int16_t Maximum
Highest valid index: B#### in octave 127.
Definition ChromaticIndex.h:32
friend constexpr bool operator<(ChromaticIndex a, ChromaticIndex b) noexcept
Orders by value; invalid sorts last.
Definition ChromaticIndex.h:106
constexpr int16_t Value() const noexcept
Returns the semitone position, or InvalidValue for the invalid index (SPEC-ERR-7).
Definition ChromaticIndex.h:68
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid index.
Definition ChromaticIndex.h:62
MCC_CONSTEXPR14 ChromaticIndex Transposed(int32_t semitones) const noexcept
Returns this index moved by semitones. The result is invalid when this index is invalid or the result...
Definition ChromaticIndex.h:87
friend constexpr bool operator==(ChromaticIndex a, ChromaticIndex b) noexcept
Equality; all invalid indices are equal (SPEC-ERR-6).
Definition ChromaticIndex.h:97
constexpr ChromaticIndex(int32_t value) noexcept
Creates the index value; values outside [Minimum, Maximum] produce the invalid index (SPEC-ERR-3).
Definition ChromaticIndex.h:51
MCC_CONSTEXPR14 MCC::PitchClass PitchClass() const noexcept
Returns the pitch class Value() mod 12 (SPEC-ORD-4); the invalid index yields the invalid class (SPEC...
Definition ChromaticIndex.h:74
static constexpr int16_t InvalidValue
Sentinel returned by Value() for the invalid index (SPEC-ERR-7). Do not rely on it; check IsValid().
Definition ChromaticIndex.h:38
static constexpr ChromaticIndex Invalid() noexcept
Returns the invalid index.
Definition ChromaticIndex.h:57
constexpr ChromaticIndex() noexcept
Creates the invalid index (SPEC-ERR-2).
Definition ChromaticIndex.h:45
Pitch class 0-11 with spelling discarded (C = 0).
Definition PitchClass.h:21
static constexpr uint8_t Count
Number of pitch classes in an octave.
Definition PitchClass.h:24
static constexpr PitchClass Invalid() noexcept
Returns the invalid pitch class.
Definition PitchClass.h:49
Music Composition Core public namespace.
Definition Version.h:4