libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
midinote.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <promeki/core/string.h>
12
14
26class MidiNote {
27 public:
36 enum Value : uint8_t {
37 // Octave -1 (MIDI 0–11)
38 C_1 = 0, Cs_1 = 1, D_1 = 2, Ds_1 = 3,
39 E_1 = 4, F_1 = 5, Fs_1 = 6, G_1 = 7,
40 Gs_1 = 8, A_1 = 9, As_1 = 10, B_1 = 11,
41 Db_1 = 1, Eb_1 = 3, Gb_1 = 6, Ab_1 = 8, Bb_1 = 10,
42
43 // Octave 0 (MIDI 12–23)
44 C0 = 12, Cs0 = 13, D0 = 14, Ds0 = 15,
45 E0 = 16, F0 = 17, Fs0 = 18, G0 = 19,
46 Gs0 = 20, A0 = 21, As0 = 22, B0 = 23,
47 Db0 = 13, Eb0 = 15, Gb0 = 18, Ab0 = 20, Bb0 = 22,
48
49 // Octave 1 (MIDI 24–35)
50 C1 = 24, Cs1 = 25, D1 = 26, Ds1 = 27,
51 E1 = 28, F1 = 29, Fs1 = 30, G1 = 31,
52 Gs1 = 32, A1 = 33, As1 = 34, B1 = 35,
53 Db1 = 25, Eb1 = 27, Gb1 = 30, Ab1 = 32, Bb1 = 34,
54
55 // Octave 2 (MIDI 36–47)
56 C2 = 36, Cs2 = 37, D2 = 38, Ds2 = 39,
57 E2 = 40, F2 = 41, Fs2 = 42, G2 = 43,
58 Gs2 = 44, A2 = 45, As2 = 46, B2 = 47,
59 Db2 = 37, Eb2 = 39, Gb2 = 42, Ab2 = 44, Bb2 = 46,
60
61 // Octave 3 (MIDI 48–59)
62 C3 = 48, Cs3 = 49, D3 = 50, Ds3 = 51,
63 E3 = 52, F3 = 53, Fs3 = 54, G3 = 55,
64 Gs3 = 56, A3 = 57, As3 = 58, B3 = 59,
65 Db3 = 49, Eb3 = 51, Gb3 = 54, Ab3 = 56, Bb3 = 58,
66
67 // Octave 4 (MIDI 60–71) — Middle C = C4
68 C4 = 60, Cs4 = 61, D4 = 62, Ds4 = 63,
69 E4 = 64, F4 = 65, Fs4 = 66, G4 = 67,
70 Gs4 = 68, A4 = 69, As4 = 70, B4 = 71,
71 Db4 = 61, Eb4 = 63, Gb4 = 66, Ab4 = 68, Bb4 = 70,
72
73 // Octave 5 (MIDI 72–83)
74 C5 = 72, Cs5 = 73, D5 = 74, Ds5 = 75,
75 E5 = 76, F5 = 77, Fs5 = 78, G5 = 79,
76 Gs5 = 80, A5 = 81, As5 = 82, B5 = 83,
77 Db5 = 73, Eb5 = 75, Gb5 = 78, Ab5 = 80, Bb5 = 82,
78
79 // Octave 6 (MIDI 84–95)
80 C6 = 84, Cs6 = 85, D6 = 86, Ds6 = 87,
81 E6 = 88, F6 = 89, Fs6 = 90, G6 = 91,
82 Gs6 = 92, A6 = 93, As6 = 94, B6 = 95,
83 Db6 = 85, Eb6 = 87, Gb6 = 90, Ab6 = 92, Bb6 = 94,
84
85 // Octave 7 (MIDI 96–107)
86 C7 = 96, Cs7 = 97, D7 = 98, Ds7 = 99,
87 E7 = 100, F7 = 101, Fs7 = 102, G7 = 103,
88 Gs7 = 104, A7 = 105, As7 = 106, B7 = 107,
89 Db7 = 97, Eb7 = 99, Gb7 = 102, Ab7 = 104, Bb7 = 106,
90
91 // Octave 8 (MIDI 108–119)
92 C8 = 108, Cs8 = 109, D8 = 110, Ds8 = 111,
93 E8 = 112, F8 = 113, Fs8 = 114, G8 = 115,
94 Gs8 = 116, A8 = 117, As8 = 118, B8 = 119,
95 Db8 = 109, Eb8 = 111, Gb8 = 114, Ab8 = 116, Bb8 = 118,
96
97 // Octave 9 (MIDI 120–127, partial)
98 C9 = 120, Cs9 = 121, D9 = 122, Ds9 = 123,
99 E9 = 124, F9 = 125, Fs9 = 126, G9 = 127,
100 Db9 = 121, Eb9 = 123, Gb9 = 126,
101
102 // General MIDI Percussion (channel 10, notes 27–87)
103 GMP_HighQ = 27,
104 GMP_Slap = 28,
105 GMP_ScratchPush = 29,
106 GMP_ScratchPull = 30,
107 GMP_Sticks = 31,
108 GMP_SquareClick = 32,
109 GMP_MetronomeClick = 33,
110 GMP_MetronomeBell = 34,
111 GMP_AcousticBassDrum = 35,
112 GMP_BassDrum1 = 36,
113 GMP_SideStick = 37,
114 GMP_AcousticSnare = 38,
115 GMP_HandClap = 39,
116 GMP_ElectricSnare = 40,
117 GMP_LowFloorTom = 41,
118 GMP_ClosedHiHat = 42,
119 GMP_HighFloorTom = 43,
120 GMP_PedalHiHat = 44,
121 GMP_LowTom = 45,
122 GMP_OpenHiHat = 46,
123 GMP_LowMidTom = 47,
124 GMP_HiMidTom = 48,
125 GMP_CrashCymbal1 = 49,
126 GMP_HighTom = 50,
127 GMP_RideCymbal1 = 51,
128 GMP_ChineseCymbal = 52,
129 GMP_RideBell = 53,
130 GMP_Tambourine = 54,
131 GMP_SplashCymbal = 55,
132 GMP_Cowbell = 56,
133 GMP_CrashCymbal2 = 57,
134 GMP_Vibraslap = 58,
135 GMP_RideCymbal2 = 59,
136 GMP_HiBongo = 60,
137 GMP_LowBongo = 61,
138 GMP_MuteHiConga = 62,
139 GMP_OpenHiConga = 63,
140 GMP_LowConga = 64,
141 GMP_HighTimbale = 65,
142 GMP_LowTimbale = 66,
143 GMP_HighAgogo = 67,
144 GMP_LowAgogo = 68,
145 GMP_Cabasa = 69,
146 GMP_Maracas = 70,
147 GMP_ShortWhistle = 71,
148 GMP_LongWhistle = 72,
149 GMP_ShortGuiro = 73,
150 GMP_LongGuiro = 74,
151 GMP_Claves = 75,
152 GMP_HiWoodBlock = 76,
153 GMP_LowWoodBlock = 77,
154 GMP_MuteCuica = 78,
155 GMP_OpenCuica = 79,
156 GMP_MuteTriangle = 80,
157 GMP_OpenTriangle = 81,
158 GMP_Shaker = 82,
159 GMP_JingleBell = 83,
160 GMP_Belltree = 84,
161 GMP_Castanets = 85,
162 GMP_MuteSurdo = 86,
163 GMP_OpenSurdo = 87,
164
165 Invalid = 0xFF
166 };
167
169 MidiNote() : _value(Invalid) {}
170
172 MidiNote(Value v) : _value(v) {}
173
178 MidiNote(uint8_t v) : _value(v > 127 ? Invalid : static_cast<Value>(v)) {}
179
181 bool isValid() const { return _value != Invalid; }
182
184 Value value() const { return _value; }
185
187 uint8_t rawValue() const { return static_cast<uint8_t>(_value); }
188
190 operator uint8_t() const { return static_cast<uint8_t>(_value); }
191
192 // --- Instance methods (return values for this note) ---
193
195 const char *pitchClassName() const;
196
198 const char *pitchClassNameFlat() const;
199
201 int octave() const;
202
204 int pitchClass() const;
205
207 String name() const;
208
213 double frequency(double a4Hz = 440.0) const;
214
215 // --- Static methods (operate on raw values) ---
216
218 static const char *pitchClassName(int midiNote);
219
221 static const char *pitchClassNameFlat(int midiNote);
222
224 static int octave(int midiNote);
225
227 static int pitchClass(int midiNote);
228
230 static String nameFromMidiNote(int midiNote);
231
237 static MidiNote fromName(const String &name);
238
244 static double frequencyFromMidiNote(double midiNote, double a4Hz = 440.0);
245
251 static double midiNoteFromFrequency(double frequencyHz, double a4Hz = 440.0);
252
253 // --- Comparison operators ---
254
255 bool operator==(const MidiNote &other) const { return _value == other._value; }
256 bool operator!=(const MidiNote &other) const { return _value != other._value; }
257 bool operator<(const MidiNote &other) const { return _value < other._value; }
258 bool operator<=(const MidiNote &other) const { return _value <= other._value; }
259 bool operator>(const MidiNote &other) const { return _value > other._value; }
260 bool operator>=(const MidiNote &other) const { return _value >= other._value; }
261
262 private:
263 Value _value;
264};
265
Dynamic array container wrapping std::vector.
Definition list.h:40
Represents a MIDI note number (0–127) with utility methods.
Definition midinote.h:26
static const char * pitchClassName(int midiNote)
Returns the pitch class name for a MIDI note number.
static int pitchClass(int midiNote)
Returns the pitch class (0–11) for a MIDI note number.
int octave() const
Returns the octave number.
int pitchClass() const
Returns the pitch class (0–11).
MidiNote(uint8_t v)
Constructs a MidiNote from a raw byte value.
Definition midinote.h:178
bool isValid() const
Returns true if this note is valid (0–127).
Definition midinote.h:181
MidiNote(Value v)
Constructs a MidiNote from a Value enum.
Definition midinote.h:172
static MidiNote fromName(const String &name)
Parses a note name string to a MidiNote.
static const char * pitchClassNameFlat(int midiNote)
Returns the pitch class name using flats.
static double frequencyFromMidiNote(double midiNote, double a4Hz=440.0)
Computes the frequency in Hz for a MIDI note number.
const char * pitchClassNameFlat() const
Returns the pitch class name using flats (e.g. "Db").
Value value() const
Returns the Value enum.
Definition midinote.h:184
MidiNote()
Default-constructs an invalid MidiNote.
Definition midinote.h:169
static double midiNoteFromFrequency(double frequencyHz, double a4Hz=440.0)
Computes the MIDI note number for a frequency in Hz.
uint8_t rawValue() const
Returns the raw MIDI note number as uint8_t, or 0xFF if invalid.
Definition midinote.h:187
const char * pitchClassName() const
Returns the pitch class name (e.g. "C", "C#").
String name() const
Returns the human-readable name (e.g. "C4").
Value
Enumeration of all 128 MIDI note values with sharp and flat aliases.
Definition midinote.h:36
static int octave(int midiNote)
Returns the octave number for a MIDI note number.
static String nameFromMidiNote(int midiNote)
Converts a MIDI note number to a human-readable name.
double frequency(double a4Hz=440.0) const
Returns the frequency in Hz.
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19