59 SmfTrack(
const uint8_t* data, uint32_t length) : data_(data), length_(length) {
rewind(); }
68 done_ = (data_ ==
nullptr);
73 if (!has_pending_ && !done_) {
74 has_pending_ = decode(pending_);
76 return has_pending_ ? &pending_ :
nullptr;
80 void pop() { has_pending_ =
false; }
83 bool done()
const {
return done_ && !has_pending_; }
85 bool error()
const {
return error_; }
88 static constexpr uint8_t STATUS_BIT = 0x80;
89 static constexpr uint8_t META = 0xFF;
90 static constexpr uint8_t SYSEX = 0xF0;
91 static constexpr uint8_t SYSEX_ESCAPE = 0xF7;
92 static constexpr uint8_t SYSTEM_FIRST = 0xF0;
93 static constexpr uint8_t META_TRACK_NAME = 0x03;
94 static constexpr uint8_t META_END_OF_TRACK = 0x2F;
95 static constexpr uint8_t META_TEMPO = 0x51;
96 static constexpr uint8_t META_TIME_SIGNATURE = 0x58;
97 static constexpr uint32_t TEMPO_LENGTH = 3;
98 static constexpr uint32_t TIME_SIGNATURE_LENGTH = 2;
99 static constexpr uint8_t VLQ_MAX_BYTES = 4;
100 static constexpr uint8_t VLQ_MORE = 0x80;
101 static constexpr uint8_t VLQ_BITS = 0x7F;
102 static constexpr uint8_t VLQ_SHIFT = 7;
103 static constexpr uint8_t STATUS_KIND_MASK = 0xF0;
104 static constexpr uint8_t CHANNEL_MASK = 0x0F;
105 static constexpr uint8_t PROGRAM_CHANGE = 0xC0;
106 static constexpr uint8_t CHANNEL_PRESSURE = 0xD0;
114 bool read_vlq(uint32_t& out) {
116 for (uint8_t i = 0; i < VLQ_MAX_BYTES; ++i) {
117 if (pos_ >= length_) {
120 const uint8_t b = data_[pos_++];
121 out = (out << VLQ_SHIFT) | (b & VLQ_BITS);
122 if ((b & VLQ_MORE) == 0) {
130 bool decode(SmfEvent& ev) {
131 while (pos_ < length_) {
133 if (!read_vlq(delta)) {
137 if (pos_ >= length_) {
140 const uint8_t lead = data_[pos_];
143 if (pos_ + 2 > length_) {
146 const uint8_t type = data_[pos_ + 1];
149 if (!read_vlq(len) || pos_ + len > length_) {
152 const uint32_t body = pos_;
159 case META_END_OF_TRACK:
160 ev.kind = SmfEvent::Kind::EndOfTrack;
164 if (len < TEMPO_LENGTH) {
167 ev.kind = SmfEvent::Kind::Tempo;
168 ev.tempo_us = (
static_cast<uint32_t
>(data_[body]) << 16) |
169 (
static_cast<uint32_t
>(data_[body + 1]) << 8) | data_[body + 2];
171 case META_TIME_SIGNATURE:
172 if (len < TIME_SIGNATURE_LENGTH) {
175 ev.kind = SmfEvent::Kind::TimeSignature;
176 ev.numerator = data_[body];
177 ev.denominator_power = data_[body + 1];
179 case META_TRACK_NAME:
180 ev.kind = SmfEvent::Kind::TrackName;
181 ev.text = data_ + body;
182 ev.text_length = len;
188 if (lead == SYSEX || lead == SYSEX_ESCAPE) {
191 if (!read_vlq(len) || pos_ + len > length_) {
199 uint8_t status = running_status_;
200 if (lead & STATUS_BIT) {
201 if (lead >= SYSTEM_FIRST) {
205 running_status_ = lead;
207 }
else if (status == 0) {
211 const uint8_t kind = status & STATUS_KIND_MASK;
212 const uint8_t data_bytes = (kind == PROGRAM_CHANGE || kind == CHANNEL_PRESSURE) ? 1 : 2;
213 if (pos_ + data_bytes > length_) {
218 ev.kind = SmfEvent::Kind::Channel;
220 ev.midi.type =
static_cast<MessageType
>(kind);
221 ev.midi.channel = status & CHANNEL_MASK;
222 ev.midi.data1 = data_[pos_];
223 ev.midi.data2 = data_bytes == 2 ? data_[pos_ + 1] : 0;
231 const uint8_t* data_ =
nullptr;
232 uint32_t length_ = 0;
235 uint8_t running_status_ = 0;
236 bool has_pending_ =
false;