MCC
Loading...
Searching...
No Matches
PitchClass.h
1#ifndef MCC_PITCH_PITCH_CLASS_H
2#define MCC_PITCH_PITCH_CLASS_H
3
4#include <MCC_BuildSettings.h>
5
6#include <stdint.h>
7
8#include <Foundation/Math/Arithmetic.h>
9
10namespace MCC {
11
22public:
24 static constexpr uint8_t Count = 12;
25
30 static constexpr uint8_t InvalidValue = 0xFF;
31
32private:
33 uint8_t _value;
34
35public:
37 constexpr PitchClass() noexcept : _value(InvalidValue) {}
38
43 constexpr explicit PitchClass(int32_t value) noexcept
44 : _value((value >= 0 && value < Count)
45 ? static_cast<uint8_t>(value)
46 : InvalidValue) {}
47
49 static constexpr PitchClass Invalid() noexcept {
50 return PitchClass();
51 }
52
54 constexpr bool IsValid() const noexcept { return _value != InvalidValue; }
55
60 constexpr uint8_t Value() const noexcept { return _value; }
61
66 MCC_CONSTEXPR14 PitchClass Transposed(int32_t semitones) const noexcept {
67 if (!IsValid()) {
68 return Invalid();
69 }
70 return PitchClass(Foundation::Math::FloorMod(
71 _value + Foundation::Math::FloorMod(semitones, Count), Count));
72 }
73
75 friend constexpr bool operator==(PitchClass a, PitchClass b) noexcept {
76 return a._value == b._value;
77 }
78
79 friend constexpr bool operator!=(PitchClass a, PitchClass b) noexcept {
80 return !(a == b);
81 }
82
84 friend constexpr bool operator<(PitchClass a, PitchClass b) noexcept {
85 return a._value < b._value;
86 }
87
88 friend constexpr bool operator>(PitchClass a, PitchClass b) noexcept {
89 return b < a;
90 }
91
92 friend constexpr bool operator<=(PitchClass a, PitchClass b) noexcept {
93 return !(b < a);
94 }
95
96 friend constexpr bool operator>=(PitchClass a, PitchClass b) noexcept {
97 return !(a < b);
98 }
99};
100
101} // namespace MCC
102
103#endif // MCC_PITCH_PITCH_CLASS_H
Pitch class 0-11 with spelling discarded (C = 0).
Definition PitchClass.h:21
constexpr uint8_t Value() const noexcept
Returns the class in [0, 11], or InvalidValue for the invalid class (SPEC-ERR-7).
Definition PitchClass.h:60
constexpr PitchClass(int32_t value) noexcept
Creates the pitch class value; values outside [0, 11] produce the invalid value (SPEC-ERR-3).
Definition PitchClass.h:43
static constexpr uint8_t InvalidValue
Sentinel returned by Value() for the invalid class (SPEC-ERR-7). Do not rely on it; check IsValid().
Definition PitchClass.h:30
friend constexpr bool operator==(PitchClass a, PitchClass b) noexcept
Equality; all invalid classes are equal (SPEC-ERR-6).
Definition PitchClass.h:75
static constexpr uint8_t Count
Number of pitch classes in an octave.
Definition PitchClass.h:24
MCC_CONSTEXPR14 PitchClass Transposed(int32_t semitones) const noexcept
Returns this class moved by semitones, reduced modulo 12. An invalid class stays invalid (SPEC-ERR-4)...
Definition PitchClass.h:66
friend constexpr bool operator<(PitchClass a, PitchClass b) noexcept
Orders by value; invalid sorts last.
Definition PitchClass.h:84
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid class.
Definition PitchClass.h:54
constexpr PitchClass() noexcept
Creates the invalid pitch class (SPEC-ERR-2).
Definition PitchClass.h:37
static constexpr PitchClass Invalid() noexcept
Returns the invalid pitch class.
Definition PitchClass.h:49
Music Composition Core public namespace.
Definition Version.h:4