84 static_assert(Outputs >= 1,
"a sequencer needs an output");
87 static constexpr uint8_t
OUTPUTS = Outputs;
97 using Sink = void (*)(
void* context, uint8_t output,
const MidiEvent& event);
107 Sequencer(uint32_t tick_rate,
Sink sink,
void* context) : sink_(sink), context_(context) {
108 const uint32_t rate = tick_rate > 0 ? tick_rate : 1;
109 const uint32_t g = std::gcd(rate,
static_cast<uint32_t
>(MICROS_PER_SECOND));
110 us_scale_ = rate / g;
111 tick_scale_ =
static_cast<uint32_t
>(MICROS_PER_SECOND) / g;
112 for (uint8_t o = 0; o < Outputs; ++o) {
133 track_output_[t] = (t >= 1 && t - 1 < Outputs) ?
static_cast<uint8_t
>(t - 1) :
NO_OUTPUT;
135 for (uint8_t c = 0; c <
CHANNELS; ++c) {
136 channel_output_[c] = c < Outputs ? c :
NO_OUTPUT;
153 track_output_[track] = output < Outputs ? output :
NO_OUTPUT;
168 channel_output_[channel] = output < Outputs ? output :
NO_OUTPUT;
180 if (output >= Outputs) {
195 if (output >= Outputs || muted_[output] == on) {
199 release_notes(output);
200 send_all_notes_off(output);
205 bool output_muted(uint8_t output)
const {
return output < Outputs && muted_[output]; }
220 for (uint8_t i = 0; i < snapshot_count_; ++i) {
221 if (muted_[snapshot_[i].output]) {
224 emit_cc(snapshot_[i].output, snapshot_[i].channel, snapshot_[i].controller, snapshot_[i].value);
227 pass_start_scaled_ = now_scaled_;
244 if (loaded_ && !finished_) {
268 const uint64_t step =
static_cast<uint64_t
>(elapsed_ticks) * tick_scale_;
269 if (!loaded_ || !playing_) {
270 pass_start_scaled_ += step;
277 uint8_t best = NO_TRACK;
278 uint32_t best_tick = 0;
279 for (uint8_t t = 0; t < track_count_; ++t) {
281 if (ev !=
nullptr && (best == NO_TRACK || ev->
tick < best_tick)) {
283 best_tick = ev->
tick;
287 if (best == NO_TRACK) {
288 if (!loop_ || song_us_ == 0) {
295 pass_start_scaled_ += song_us_ * us_scale_;
301 const uint64_t numerator =
static_cast<uint64_t
>(best_tick - last_tick_) * tempo_us_ + tick_remainder_;
302 const uint64_t event_us = song_us_ + numerator / ticks_per_quarter_;
303 if (pass_start_scaled_ + event_us * us_scale_ > now_scaled_) {
308 tick_remainder_ = numerator % ticks_per_quarter_;
309 last_tick_ = best_tick;
313 if (ev.
kind == SmfEvent::Kind::Tempo && ev.
tempo_us > 0) {
315 }
else if (ev.
kind == SmfEvent::Kind::Channel) {
316 route(best, ev.
midi);
322 static constexpr uint64_t MICROS_PER_SECOND = 1000000;
323 static constexpr uint8_t NO_TRACK = 0xFF;
324 static constexpr uint8_t NOTE_MASK = 0x7F;
325 static constexpr uint8_t NOTE_BITS = 32;
326 static constexpr uint8_t NOTE_WORDS = 128 / NOTE_BITS;
336 for (uint8_t t = 0; t < track_count_; ++t) {
346 bool destination(uint8_t track,
const MidiEvent& e, uint8_t& output, uint8_t& channel)
const {
347 output = format_ == 0 ? channel_output_[e.channel & (
CHANNELS - 1)] : track_output_[track];
348 if (output >= Outputs) {
351 channel = output_channel_[output] ==
KEEP_CHANNEL ? e.channel : output_channel_[output];
355 void route(uint8_t track, MidiEvent e) {
356 if (e.type == MessageType::ProgramChange && pass_ > 0) {
361 if (!destination(track, e, output, channel)) {
364 if (muted_[output]) {
368 if (e.type == MessageType::NoteOn || e.type == MessageType::NoteOff) {
369 mark(output, channel, e.data1, e.type == MessageType::NoteOn && e.data2 > 0);
371 if (sink_ !=
nullptr) {
372 sink_(context_, output, e);
377 void mark(uint8_t output, uint8_t channel, uint8_t note,
bool on) {
378 const uint8_t n = note & NOTE_MASK;
379 uint32_t& word = sounding_[output][channel & (
CHANNELS - 1)][n / NOTE_BITS];
380 const uint32_t bit = 1U << (n % NOTE_BITS);
381 word = on ? (word | bit) : (word & ~bit);
384 void emit_note_off(uint8_t output, uint8_t channel, uint8_t note) {
386 e.type = MessageType::NoteOff;
390 if (sink_ !=
nullptr) {
391 sink_(context_, output, e);
395 void emit_cc(uint8_t output, uint8_t channel, uint8_t controller, uint8_t value) {
397 e.type = MessageType::ControlChange;
399 e.data1 = controller;
401 if (sink_ !=
nullptr) {
402 sink_(context_, output, e);
407 void release_notes(uint8_t o) {
408 for (uint8_t c = 0; c <
CHANNELS; ++c) {
409 for (uint8_t w = 0; w < NOTE_WORDS; ++w) {
410 for (uint8_t b = 0; b < NOTE_BITS && sounding_[o][c][w] != 0; ++b) {
411 if (sounding_[o][c][w] & (1U << b)) {
412 sounding_[o][c][w] &= ~(1U << b);
413 emit_note_off(o, c,
static_cast<uint8_t
>(w * NOTE_BITS + b));
421 void send_all_notes_off(uint8_t o) {
422 for (uint8_t c = 0; c <
CHANNELS; ++c) {
423 if (used_channels_[o] & (1u << c)) {
430 void all_notes_off() {
431 for (uint8_t o = 0; o < Outputs; ++o) {
434 for (uint8_t o = 0; o < Outputs; ++o) {
436 send_all_notes_off(o);
443 for (uint8_t o = 0; o < Outputs; ++o) {
444 used_channels_[o] = 0;
447 for (uint8_t t = 0; t < track_count_; ++t) {
448 SmfTrack cursor = tracks_[t];
450 while (
const SmfEvent* ev = cursor.peek()) {
453 if (ev->kind == SmfEvent::Kind::Channel && destination(t, ev->midi, output, channel)) {
454 used_channels_[output] |=
static_cast<uint16_t
>(1u << channel);
455 if (ev->midi.type == MessageType::ControlChange) {
456 remember(output, channel, ev->midi);
464 void remember(uint8_t output, uint8_t channel,
const MidiEvent& e) {
465 for (uint8_t i = 0; i < snapshot_count_; ++i) {
466 const Controller& c = snapshot_[i];
467 if (c.output == output && c.channel == channel && c.controller == e.data1) {
472 snapshot_[snapshot_count_++] = Controller{output, channel, e.data1, e.data2};
481 uint8_t channel_output_[
CHANNELS] = {};
482 uint8_t output_channel_[Outputs] = {};
483 uint16_t used_channels_[Outputs] = {};
484 bool muted_[Outputs] = {};
485 uint32_t sounding_[Outputs][
CHANNELS][NOTE_WORDS] = {};
487 uint8_t snapshot_count_ = 0;
489 uint16_t format_ = 0;
490 uint8_t track_count_ = 0;
491 uint16_t ticks_per_quarter_ = 1;
492 uint32_t us_scale_ = 1;
493 uint32_t tick_scale_ = 1;
495 bool loaded_ =
false;
496 bool playing_ =
false;
497 bool finished_ =
false;
501 uint64_t song_us_ = 0;
502 uint32_t last_tick_ = 0;
503 uint64_t tick_remainder_ = 0;
504 uint64_t now_scaled_ = 0;
505 uint64_t pass_start_scaled_ = 0;