ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
midi_parser.cpp
Go to the documentation of this file.
1#include "midi_parser.h"
2
3#include <cstring>
4
5namespace esphome::ble_midi {
6
7static MessageType type_for_status(uint8_t status) {
8 if (status >= 0xF8)
10 if (status >= 0xF1 && status <= 0xF6)
12 switch (status & 0xF0) {
13 case 0x80:
15 case 0x90:
17 case 0xA0:
19 case 0xB0:
21 case 0xC0:
23 case 0xD0:
25 case 0xE0:
27 default:
29 }
30}
31
32// Data bytes that follow a channel-voice or system-common status byte
33uint8_t MidiParser::expected_data_bytes(uint8_t status) {
34 if (status >= 0xF0) {
35 switch (status) {
36 case 0xF1: // MIDI Time Code Quarter Frame
37 case 0xF3: // Song Select
38 return 1;
39 case 0xF2: // Song Position Pointer
40 return 2;
41 default: // 0xF4, 0xF5 undefined, 0xF6 Tune Request
42 return 0;
43 }
44 }
45 switch (status & 0xF0) {
46 case 0xC0: // Program Change
47 case 0xD0: // Channel Aftertouch
48 return 1;
49 default:
50 return 2;
51 }
52}
53
55 this->running_status_ = 0;
56 this->sysex_active_ = false;
57 this->sysex_length_ = 0;
58 this->current_length_ = 0;
59}
60
61void MidiParser::sysex_push_(uint8_t byte) {
63 this->sysex_buffer_[this->sysex_length_++] = byte;
64 } else {
65 this->sysex_overflow_ = true;
66 }
67}
68
69// Ends the SysEx being assembled; it is complete when 0xF7 closed it and every byte fit the buffer
70void MidiParser::end_sysex_(bool complete, MidiMessageHandler handler, void *ctx) {
71 this->sysex_active_ = false;
72 this->running_status_ = 0;
74 this->sysex_buffer_, this->sysex_length_, handler, ctx);
75}
76
78 const uint8_t status = this->current_message_[0];
79 MessageType type = type_for_status(status);
80 // A Note On with velocity zero is canonically a Note Off.
81 if (type == MessageType::NOTE_ON && this->current_message_[2] == 0)
83 // System Common clears running status; channel-voice messages set it.
84 this->running_status_ = status >= 0xF0 ? 0 : status;
85 const uint16_t length = this->current_length_;
86 this->current_length_ = 0;
87 this->emit_(type, this->current_message_, length, handler, ctx);
88}
89
90// Takes a data byte for the message being assembled, or starts one from the running status
91void MidiParser::take_data_(const uint8_t *byte, MidiMessageHandler handler, void *ctx) {
92 if (this->current_length_ == 0) {
93 if (this->running_status_ == 0) {
94 // Data byte without any status context.
95 this->emit_(MessageType::UNKNOWN, byte, 1, handler, ctx);
96 return;
97 }
98 this->current_message_[0] = this->running_status_;
99 this->current_length_ = 1;
101 }
102 this->current_message_[this->current_length_++] = *byte;
103 if (this->current_length_ > this->current_expected_)
104 this->emit_current_(handler, ctx);
105}
106
107void MidiParser::feed(const uint8_t *data, size_t length, MidiMessageHandler handler, void *ctx) {
108 // A BLE-MIDI packet is at least a header byte plus one timestamp or SysEx
109 // continuation byte, and the header byte always has bit 7 set. Anything else
110 // is malformed.
111 if (length < 2 || (data[0] & 0x80) == 0) {
112 for (size_t i = 0; i < length; i++)
113 this->emit_(MessageType::UNKNOWN, data + i, 1, handler, ctx);
114 return;
115 }
116
117 // A timestamp byte precedes every status byte, even a real-time byte inside another message, so a byte with
118 // bit 7 set is a status byte only right after a timestamp and a timestamp everywhere else
119 bool expect_timestamp = true;
120 for (size_t i = 1; i < length; i++) {
121 const uint8_t byte = data[i];
122 if ((byte & 0x80) == 0) {
123 // A data byte continues the SysEx, the message being assembled, or the running status
124 if (this->sysex_active_) {
125 this->sysex_push_(byte);
126 } else {
127 this->take_data_(data + i, handler, ctx);
128 }
129 expect_timestamp = true;
130 continue;
131 }
132 if (expect_timestamp) {
133 expect_timestamp = false;
134 continue;
135 }
136 expect_timestamp = true;
137 if (byte >= 0xF8) {
138 // Real-time messages are single bytes that interrupt anything, a SysEx included, without affecting it
139 this->emit_(MessageType::SYSTEM_REAL_TIME, data + i, 1, handler, ctx);
140 continue;
141 }
142 // Any other status byte cuts a message still being assembled short
143 if (this->current_length_ > 0) {
144 const uint16_t length = this->current_length_;
145 this->current_length_ = 0;
146 this->emit_(MessageType::INCOMPLETE, this->current_message_, length, handler, ctx);
147 }
148 if (byte == STATUS_SYSEX_END) {
149 if (this->sysex_active_) {
150 this->sysex_push_(STATUS_SYSEX_END);
151 this->end_sysex_(true, handler, ctx);
152 } else {
153 this->emit_(MessageType::UNKNOWN, data + i, 1, handler, ctx);
154 }
155 continue;
156 }
157 // Any status byte but 0xF7 ends a SysEx still open
158 if (this->sysex_active_)
159 this->end_sysex_(false, handler, ctx);
160 if (byte == STATUS_SYSEX_START) {
161 this->sysex_active_ = true;
162 this->sysex_length_ = 0;
163 this->sysex_overflow_ = false;
164 this->sysex_push_(STATUS_SYSEX_START);
165 continue;
166 }
167 this->current_message_[0] = byte;
168 this->current_length_ = 1;
170 // A status byte without data bytes, e.g. Tune Request, is a message on its own
171 if (this->current_expected_ == 0)
172 this->emit_current_(handler, ctx);
173 }
174}
175
176size_t frame_sysex(const uint8_t *payload, size_t length, uint8_t *out, size_t out_size) {
177 if (length == 0)
178 return 0;
179 const size_t skip_start = payload[0] == STATUS_SYSEX_START ? 1 : 0;
180 const size_t skip_end = payload[length - 1] == STATUS_SYSEX_END ? 1 : 0;
181 const size_t body = length - skip_start - skip_end;
182 // 0xF0, the body, then the timestamp before 0xF7
183 const size_t framed = body + 3;
184 if (framed > out_size)
185 return 0;
186 // A body byte with bit 7 set would be read as a timestamp or status byte
187 for (size_t i = 0; i < body; i++) {
188 if (payload[skip_start + i] & 0x80)
189 return 0;
190 }
191 out[0] = STATUS_SYSEX_START;
192 std::memcpy(out + 1, payload + skip_start, body);
193 out[body + 1] = PACKET_TIMESTAMP;
194 out[body + 2] = STATUS_SYSEX_END;
195 return framed;
196}
197
198} // namespace esphome::ble_midi
uint8_t status
Definition bl0942.h:8
void take_data_(const uint8_t *byte, MidiMessageHandler handler, void *ctx)
void feed(const uint8_t *data, size_t length, MidiMessageHandler handler, void *ctx)
Feed one BLE notification payload.
void emit_current_(MidiMessageHandler handler, void *ctx)
static uint8_t expected_data_bytes(uint8_t status)
Number of data bytes that follow a status byte other than SysEx.
void emit_(MessageType type, const uint8_t *data, uint16_t length, MidiMessageHandler handler, void *ctx)
Definition midi_parser.h:94
void sysex_push_(uint8_t byte)
void reset()
Drop all parser state.
void end_sysex_(bool complete, MidiMessageHandler handler, void *ctx)
uint8_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
size_t frame_sysex(const uint8_t *payload, size_t length, uint8_t *out, size_t out_size)
Frame a SysEx payload for one BLE-MIDI packet: 0xF0 and 0xF7 are added when the payload lacks them,...
void(*)(void *ctx, const MidiMessage &message) MidiMessageHandler
Called once per decoded message with the context passed to feed().
Definition midi_parser.h:52
uint16_t length
Definition tt21100.cpp:0