MCC
Loading...
Searching...
No Matches
Note.h
1#ifndef MCC_RHYTHM_NOTE_H
2#define MCC_RHYTHM_NOTE_H
3
4#include <stdint.h>
5
6#include <MCC_BuildSettings.h>
7#include <MCC/Pitch/Pitch.h>
8#include <MCC/Rhythm/NoteValue.h>
9
10namespace MCC {
11
25class Note {
26 MCC::Pitch _pitch;
27 MCC::NoteValue _value;
28
29public:
31 constexpr Note() noexcept : _pitch(), _value() {}
32
36 constexpr explicit Note(
37 MCC::Pitch pitch,
38 MCC::NoteValue value = MCC::NoteValue::Quarter()) noexcept
39 : _pitch(pitch.IsValid() && value.IsValid()
40 ? pitch
41 : MCC::Pitch::Invalid()),
42 _value(pitch.IsValid() && value.IsValid()
43 ? value
44 : MCC::NoteValue::Invalid()) {}
45
47 constexpr Note(
48 MCC::NoteName noteName,
49 int32_t octave,
50 MCC::NoteValue value = MCC::NoteValue::Quarter()) noexcept
51 : Note(MCC::Pitch(noteName, octave), value) {}
52
54 constexpr Note(
55 MCC::Letter letter,
56 int32_t octave,
57 MCC::NoteValue value = MCC::NoteValue::Quarter()) noexcept
58 : Note(MCC::Pitch(letter, octave), value) {}
59
64 constexpr Note(
65 MCC::Letter letter,
66 MCC::Accidental accidental,
67 int32_t octave,
68 MCC::NoteValue value = MCC::NoteValue::Quarter()) noexcept
69 : Note(MCC::Pitch(letter, accidental, octave), value) {}
70
72 static constexpr Note Invalid() noexcept { return Note(); }
73
75 constexpr bool IsValid() const noexcept {
76 return _pitch.IsValid() && _value.IsValid();
77 }
78
80 constexpr MCC::Pitch Pitch() const noexcept { return _pitch; }
81
83 constexpr MCC::NoteValue Value() const noexcept { return _value; }
84
86 constexpr MCC::NoteName NoteName() const noexcept {
87 return _pitch.NoteName();
88 }
89
91 constexpr MCC::Letter Letter() const noexcept { return _pitch.Letter(); }
92
94 constexpr MCC::Accidental Accidental() const noexcept {
95 return _pitch.Accidental();
96 }
97
99 constexpr int8_t Octave() const noexcept { return _pitch.Octave(); }
100
102 MCC_CONSTEXPR14 MCC::PitchClass PitchClass() const noexcept {
103 return _pitch.PitchClass();
104 }
105
107 MCC_CONSTEXPR14 MCC::ChromaticIndex ChromaticIndex() const noexcept {
108 return _pitch.ChromaticIndex();
109 }
110
112 constexpr Note WithPitch(MCC::Pitch pitch) const noexcept {
113 return Note(pitch, _value);
114 }
115
117 constexpr Note WithValue(MCC::NoteValue value) const noexcept {
118 return Note(_pitch, value);
119 }
120
122 constexpr Note WithDots(int32_t dots) const noexcept {
123 return Note(_pitch, _value.WithDots(dots));
124 }
125
127 MCC_CONSTEXPR14 Note MovedDiatonically(int32_t steps) const noexcept {
128 return Note(_pitch.MovedDiatonically(steps), _value);
129 }
130
132 MCC_CONSTEXPR14 Note Altered(int32_t semitones) const noexcept {
133 return Note(_pitch.Altered(semitones), _value);
134 }
135
137 MCC_CONSTEXPR14 Note MovedByOctaves(int32_t octaves) const noexcept {
138 return Note(_pitch.MovedByOctaves(octaves), _value);
139 }
140
142 friend constexpr bool operator==(Note a, Note b) noexcept {
143 return a._pitch == b._pitch && a._value == b._value;
144 }
145
146 friend constexpr bool operator!=(Note a, Note b) noexcept {
147 return !(a == b);
148 }
149
154 friend MCC_CONSTEXPR14 bool operator<(Note a, Note b) noexcept {
155 if (!a.IsValid() || !b.IsValid()) {
156 return a.IsValid() && !b.IsValid();
157 }
158 return a._pitch != b._pitch ? a._pitch < b._pitch : a._value < b._value;
159 }
160
161 friend MCC_CONSTEXPR14 bool operator>(Note a, Note b) noexcept {
162 return b < a;
163 }
164
165 friend MCC_CONSTEXPR14 bool operator<=(Note a, Note b) noexcept {
166 return !(b < a);
167 }
168
169 friend MCC_CONSTEXPR14 bool operator>=(Note a, Note b) noexcept {
170 return !(a < b);
171 }
172};
173
178MCC_CONSTEXPR14 bool IsNoteLowerThan(Note a, Note b) noexcept {
179 if (!a.IsValid() || !b.IsValid()) {
180 return a.IsValid() && !b.IsValid();
181 }
182 if (a.ChromaticIndex() != b.ChromaticIndex()) {
183 return a.ChromaticIndex() < b.ChromaticIndex();
184 }
185 return a.Pitch() != b.Pitch() ? a.Pitch() < b.Pitch() : a.Value() < b.Value();
186}
187
188} // namespace MCC
189
190#endif // MCC_RHYTHM_NOTE_H
Chromatic alteration of a letter, from -4 (quadruple flat) to +4 (quadruple sharp).
Definition Accidental.h:20
constexpr Accidental() noexcept
Creates the invalid accidental (SPEC-ERR-2).
Definition Accidental.h:39
Absolute semitone position of a pitch, with origin C-1 = 0.
Definition ChromaticIndex.h:26
constexpr ChromaticIndex() noexcept
Creates the invalid index (SPEC-ERR-2).
Definition ChromaticIndex.h:45
Written note name: a letter plus an accidental, without octave.
Definition NoteName.h:29
constexpr NoteName() noexcept
Creates the invalid note name (SPEC-ERR-2).
Definition NoteName.h:37
Exact written note length: a power-of-two base and zero to four dots.
Definition NoteValue.h:21
static constexpr NoteValue Quarter(int32_t dots=0) noexcept
Creates a quarter note, optionally augmented by dots.
Definition NoteValue.h:64
A written pitch combined with an exact written note value.
Definition Note.h:25
constexpr Note(MCC::NoteName noteName, int32_t octave, MCC::NoteValue value=MCC::NoteValue::Quarter()) noexcept
Creates noteName in octave, with a quarter by default.
Definition Note.h:47
MCC_CONSTEXPR14 MCC::ChromaticIndex ChromaticIndex() const noexcept
Returns the chromatic index, or the invalid chromatic index.
Definition Note.h:107
constexpr Note WithPitch(MCC::Pitch pitch) const noexcept
Returns the same note value with a new written pitch.
Definition Note.h:112
constexpr MCC::NoteValue Value() const noexcept
Returns the written note value, or the invalid value.
Definition Note.h:83
static constexpr Note Invalid() noexcept
Returns the invalid note.
Definition Note.h:72
constexpr MCC::Pitch Pitch() const noexcept
Returns the written pitch, or the invalid pitch.
Definition Note.h:80
constexpr Note() noexcept
Creates the invalid note (SPEC-ERR-2).
Definition Note.h:31
constexpr MCC::Accidental Accidental() const noexcept
Returns the written accidental, or the invalid accidental.
Definition Note.h:94
friend MCC_CONSTEXPR14 bool operator<(Note a, Note b) noexcept
Written order by pitch, then exact note-value duration; invalid notes sort last.
Definition Note.h:154
MCC_CONSTEXPR14 Note Altered(int32_t semitones) const noexcept
Alters the pitch and preserves the note value.
Definition Note.h:132
constexpr MCC::NoteName NoteName() const noexcept
Returns the written note name, or the invalid note name.
Definition Note.h:86
friend constexpr bool operator==(Note a, Note b) noexcept
Written equality of both pitch and note value.
Definition Note.h:142
constexpr Note WithValue(MCC::NoteValue value) const noexcept
Returns the same written pitch with a new note value.
Definition Note.h:117
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid note.
Definition Note.h:75
MCC_CONSTEXPR14 MCC::PitchClass PitchClass() const noexcept
Returns the pitch class, or the invalid pitch class.
Definition Note.h:102
constexpr MCC::Letter Letter() const noexcept
Returns the written letter.
Definition Note.h:91
constexpr int8_t Octave() const noexcept
Returns the written octave, or 0 for the invalid note.
Definition Note.h:99
constexpr Note(MCC::Letter letter, MCC::Accidental accidental, int32_t octave, MCC::NoteValue value=MCC::NoteValue::Quarter()) noexcept
Creates letter + accidental in octave, with a quarter by default.
Definition Note.h:64
constexpr Note(MCC::Pitch pitch, MCC::NoteValue value=MCC::NoteValue::Quarter()) noexcept
Creates pitch with value; the default value is a quarter.
Definition Note.h:36
constexpr Note(MCC::Letter letter, int32_t octave, MCC::NoteValue value=MCC::NoteValue::Quarter()) noexcept
Creates natural letter in octave, with a quarter by default.
Definition Note.h:54
MCC_CONSTEXPR14 Note MovedDiatonically(int32_t steps) const noexcept
Moves the pitch diatonically and preserves the note value.
Definition Note.h:127
constexpr Note WithDots(int32_t dots) const noexcept
Returns the same written pitch and base with a new dot count.
Definition Note.h:122
MCC_CONSTEXPR14 Note MovedByOctaves(int32_t octaves) const noexcept
Moves the pitch by octaves and preserves the note value.
Definition Note.h:137
Pitch class 0-11 with spelling discarded (C = 0).
Definition PitchClass.h:21
constexpr PitchClass() noexcept
Creates the invalid pitch class (SPEC-ERR-2).
Definition PitchClass.h:37
Written absolute pitch: a note name plus an octave.
Definition Pitch.h:35
Letter
Diatonic letter name.
Definition Letter.h:22
MCC_CONSTEXPR14 bool IsNoteLowerThan(Note a, Note b) noexcept
Orders notes by sounding pitch, then by written pitch and value.
Definition Note.h:178
Music Composition Core public namespace.
Definition Version.h:4