Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1/**
2 * @file types.hpp
3 * @brief MIDI message types
4 *
5 * Core types for MIDI protocol handling. All values follow the MIDI 1.0 spec.
6 */
7
8#ifndef SBL_PROTOCOL_MIDI_TYPES_HPP_
9#define SBL_PROTOCOL_MIDI_TYPES_HPP_
10
11#include <cstdint>
12
13namespace sbl::midi {
14
15enum class MessageType : uint8_t {
16 NoteOff = 0x80,
17 NoteOn = 0x90,
18 PolyPressure = 0xA0,
19 ControlChange = 0xB0,
20 ProgramChange = 0xC0,
21 ChannelPressure = 0xD0,
22 PitchBend = 0xE0,
23
24 // System Real-Time (no channel, no data bytes)
25 Clock = 0xF8,
26 Start = 0xFA,
27 Continue = 0xFB,
28 Stop = 0xFC,
29 ActiveSensing = 0xFE,
30 SystemReset = 0xFF,
31
32 // System Common
33 SysEx = 0xF0,
34 SysExEnd = 0xF7,
35 TimeCode = 0xF1,
36 SongPosition = 0xF2,
37 SongSelect = 0xF3,
38 TuneRequest = 0xF6,
39
40 None = 0x00,
41};
42
43/** Controller numbers with a meaning fixed by the MIDI 1.0 specification. */
44namespace cc {
45inline constexpr uint8_t RESET_ALL_CONTROLLERS = 121;
46inline constexpr uint8_t ALL_NOTES_OFF = 123;
47} // namespace cc
48
49/** A controller used as a switch is on from this value up. */
50inline constexpr uint8_t CC_SWITCH_ON = 64;
51
52/** The largest 7-bit data value. */
53inline constexpr uint8_t DATA_MAX = 127;
54
55/** A value in [0, 1] as a 7-bit data byte, rounded to nearest; values outside are held to the ends. */
56inline constexpr uint8_t to_7bit(float value) {
57 const float c = value * static_cast<float>(DATA_MAX) + 0.5f;
58 return c <= 0.0f ? uint8_t{0} : (c >= static_cast<float>(DATA_MAX) ? DATA_MAX : static_cast<uint8_t>(c));
59}
60
61struct MidiEvent {
62 MessageType type = MessageType::None;
63 uint8_t channel = 0; /**< 0-15 for channel messages, 0 for system */
64 uint8_t data1 = 0; /**< Note number, CC number, program, etc. */
65 uint8_t data2 = 0; /**< Velocity, CC value, etc. */
66
67 /** @brief A Control Change event: channel 0-15, controller number and value 0-127 */
68 static constexpr MidiEvent control_change(uint8_t channel, uint8_t number, uint8_t value) {
69 MidiEvent event;
70 event.type = MessageType::ControlChange;
71 event.channel = channel;
72 event.data1 = number;
73 event.data2 = value;
74 return event;
75 }
76
77 /** @brief Note number (alias for data1) */
78 uint8_t note() const { return data1; }
79
80 /** @brief Velocity (alias for data2) */
81 uint8_t velocity() const { return data2; }
82
83 /** @brief CC number (alias for data1) */
84 uint8_t cc_number() const { return data1; }
85
86 /** @brief CC value (alias for data2) */
87 uint8_t cc_value() const { return data2; }
88
89 /**
90 * @brief 14-bit pitch bend value, centered at 8192
91 *
92 * Range: 0 (max down) to 16383 (max up), 8192 = center.
93 */
94 int16_t pitch_bend_value() const { return static_cast<int16_t>(((static_cast<int>(data2) << 7) | data1) - 8192); }
95
96 /** @brief Raw 14-bit pitch bend (0-16383) */
97 uint16_t pitch_bend_raw() const { return (static_cast<uint16_t>(data2) << 7) | data1; }
98
99 /**
100 * @brief Encode this event as raw MIDI bytes
101 * @param out Buffer for MIDI bytes (must be at least 3 bytes)
102 * @return Number of bytes written (0 if unsupported message type)
103 */
104 uint8_t to_bytes(uint8_t* out) const {
105 switch (type) {
106 // 3-byte channel messages
107 case MessageType::NoteOn:
108 out[0] = 0x90 | (channel & 0x0F);
109 out[1] = data1 & 0x7F;
110 out[2] = data2 & 0x7F;
111 return 3;
112 case MessageType::NoteOff:
113 out[0] = 0x80 | (channel & 0x0F);
114 out[1] = data1 & 0x7F;
115 out[2] = data2 & 0x7F;
116 return 3;
117 case MessageType::ControlChange:
118 out[0] = 0xB0 | (channel & 0x0F);
119 out[1] = data1 & 0x7F;
120 out[2] = data2 & 0x7F;
121 return 3;
122 case MessageType::PitchBend:
123 out[0] = 0xE0 | (channel & 0x0F);
124 out[1] = data1 & 0x7F; // LSB
125 out[2] = data2 & 0x7F; // MSB
126 return 3;
127 case MessageType::PolyPressure:
128 out[0] = 0xA0 | (channel & 0x0F);
129 out[1] = data1 & 0x7F;
130 out[2] = data2 & 0x7F;
131 return 3;
132
133 // 2-byte channel messages
134 case MessageType::ProgramChange:
135 out[0] = 0xC0 | (channel & 0x0F);
136 out[1] = data1 & 0x7F;
137 return 2;
138 case MessageType::ChannelPressure:
139 out[0] = 0xD0 | (channel & 0x0F);
140 out[1] = data1 & 0x7F;
141 return 2;
142
143 // System Real-Time (1 byte, no data)
144 case MessageType::Clock: out[0] = 0xF8; return 1;
145 case MessageType::Start: out[0] = 0xFA; return 1;
146 case MessageType::Continue: out[0] = 0xFB; return 1;
147 case MessageType::Stop: out[0] = 0xFC; return 1;
148 case MessageType::ActiveSensing: out[0] = 0xFE; return 1;
149 case MessageType::SystemReset:
150 out[0] = 0xFF;
151 return 1;
152
153 // System Common
154 case MessageType::SongPosition:
155 out[0] = 0xF2;
156 out[1] = data1 & 0x7F;
157 out[2] = data2 & 0x7F;
158 return 3;
159 case MessageType::SongSelect:
160 out[0] = 0xF3;
161 out[1] = data1 & 0x7F;
162 return 2;
163 case MessageType::TuneRequest: out[0] = 0xF6; return 1;
164
165 default: return 0; // Unsupported (SysEx, None, etc.)
166 }
167 }
168};
169
170/** Callback type for parsed MIDI events */
171using MidiCallback = void (*)(const MidiEvent& event);
172
173/** Number of data bytes expected for a channel message status byte */
174inline uint8_t expected_data_bytes(uint8_t status) {
175 switch (status & 0xF0) {
176 case 0xC0: // Program Change
177 case 0xD0: return 1; // Channel Pressure
178 default: return 2; // Note On/Off, CC, Poly AT, Pitch Bend
179 }
180}
181
182} // namespace sbl::midi
183
184#endif // SBL_PROTOCOL_MIDI_TYPES_HPP_
MIDI parser, encoder, SMF and sequencer.
Definition drain.hpp:31
uint8_t expected_data_bytes(uint8_t status)
Definition types.hpp:174
constexpr uint8_t CC_SWITCH_ON
Definition types.hpp:50
constexpr uint8_t DATA_MAX
Definition types.hpp:53
constexpr uint8_t to_7bit(float value)
Definition types.hpp:56
void(*)(const MidiEvent &event) MidiCallback
Definition types.hpp:171
uint8_t cc_value() const
CC value (alias for data2)
Definition types.hpp:87
int16_t pitch_bend_value() const
14-bit pitch bend value, centered at 8192
Definition types.hpp:94
uint8_t cc_number() const
CC number (alias for data1)
Definition types.hpp:84
static constexpr MidiEvent control_change(uint8_t channel, uint8_t number, uint8_t value)
A Control Change event: channel 0-15, controller number and value 0-127.
Definition types.hpp:68
uint16_t pitch_bend_raw() const
Raw 14-bit pitch bend (0-16383)
Definition types.hpp:97
uint8_t note() const
Note number (alias for data1)
Definition types.hpp:78
uint8_t to_bytes(uint8_t *out) const
Encode this event as raw MIDI bytes.
Definition types.hpp:104
uint8_t velocity() const
Velocity (alias for data2)
Definition types.hpp:81