#include "audio/xmidi_player.hpp" #include namespace top_octane::audio { void XmidiPlayer::set_sample_rate(int sample_rate) { sample_rate_ = std::max(sample_rate, 8000); samples_per_tick_ = static_cast(sample_rate_) / 120.0; } void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop, uint32_t start_tick) { seq_ = seq; loop_ = loop; playing_ = (seq != nullptr && !seq->events.empty()); event_cursor_ = 0; current_tick_ = 0; pending_start_tick_ = (seq && start_tick <= seq->total_ticks) ? start_tick : 0; sample_accum_ = 0.0; samples_per_tick_ = static_cast(sample_rate_) / 120.0; for (auto& n : note_offs_) n = PendingNoteOff{}; loop_depth_ = 0; } void XmidiPlayer::stop() { playing_ = false; seq_ = nullptr; pending_start_tick_ = 0; for (auto& n : note_offs_) n = PendingNoteOff{}; loop_depth_ = 0; } void XmidiPlayer::process_tick(IMidiSynth& synth) { if (!seq_ || !playing_) return; const auto& evs = seq_->events; // Fast-forward controller/program/pitch-bend setup before requested start_tick if (pending_start_tick_ > 0) { while (event_cursor_ < evs.size() && evs[event_cursor_].tick < pending_start_tick_) { const auto& ev = evs[event_cursor_++]; if (ev.status == 0xFFu) continue; uint8_t hi = ev.status & 0xF0u; uint8_t ch = ev.status & 0x0Fu; if (hi == 0xB0u) { if (ev.data1 != 116u && ev.data1 != 117u && ev.data1 != 120u && ev.data1 != 123u) { synth.control_change(ch, ev.data1, ev.data2); } } else if (hi == 0xC0u) { synth.program_change(ch, ev.data1); } else if (hi == 0xE0u) { int16_t bend14 = static_cast(((ev.data2 & 0x7Fu) << 7) | (ev.data1 & 0x7Fu)) - 8192; synth.pitch_bend(ch, bend14); } } current_tick_ = pending_start_tick_; pending_start_tick_ = 0; } // Fire due note-offs for (auto& n : note_offs_) { if (n.active && current_tick_ >= n.off_tick) { synth.note_off(n.channel, n.key); n.active = false; } } auto clear_pending_note = [&](uint8_t ch, uint8_t key) { for (auto& n : note_offs_) { if (n.active && n.channel == ch && n.key == key) { n.active = false; } } }; // Fire events at or before current_tick_ while (event_cursor_ < evs.size() && evs[event_cursor_].tick <= current_tick_) { const auto& ev = evs[event_cursor_++]; if (ev.status == 0xFFu) { continue; } uint8_t hi = ev.status & 0xF0u; uint8_t ch = ev.status & 0x0Fu; switch (hi) { case 0x80u: clear_pending_note(ch, ev.data1); synth.note_off(ch, ev.data1); break; case 0x90u: if (ev.data2 == 0) { clear_pending_note(ch, ev.data1); synth.note_off(ch, ev.data1); } else { // Release/replace any active pending note-off for (ch, key) so stale timers // never cut off a re-triggered note early for (auto& n : note_offs_) { if (n.active && n.channel == ch && n.key == ev.data1) { synth.note_off(ch, ev.data1); n.active = false; } } synth.note_on(ch, ev.data1, ev.data2); if (ev.duration_ticks > 0) { for (auto& n : note_offs_) { if (!n.active) { n.active = true; n.off_tick = current_tick_ + ev.duration_ticks; n.channel = ch; n.key = ev.data1; break; } } } } break; case 0xB0u: if (ev.data1 == 116u) { // XMIDI FOR Loop Start if (loop_depth_ < static_cast(loop_stack_.size())) { XmiLoopFrame f; f.event_idx = event_cursor_; f.tick = ev.tick; f.infinite = (ev.data2 == 0); f.remaining = ev.data2; loop_stack_[loop_depth_++] = f; } } else if (ev.data1 == 117u) { // XMIDI Next/Break Loop if (loop_depth_ > 0) { XmiLoopFrame& f = loop_stack_[loop_depth_ - 1]; bool repeat = (f.infinite && loop_) || (f.remaining > 1); if (repeat) { if (!f.infinite && f.remaining > 1) { --f.remaining; } for (auto& n : note_offs_) { if (n.active) { synth.note_off(n.channel, n.key); n.active = false; } } event_cursor_ = f.event_idx; current_tick_ = f.tick; return; } else { --loop_depth_; } } } else { synth.control_change(ch, ev.data1, ev.data2); } break; case 0xC0u: synth.program_change(ch, ev.data1); break; case 0xE0u: { int16_t bend14 = static_cast(((ev.data2 & 0x7Fu) << 7) | (ev.data1 & 0x7Fu)) - 8192; synth.pitch_bend(ch, bend14); break; } default: break; } } ++current_tick_; if (event_cursor_ >= evs.size() && current_tick_ > seq_->total_ticks) { if (loop_) { synth.all_notes_off(); event_cursor_ = 0; current_tick_ = 0; loop_depth_ = 0; for (auto& n : note_offs_) n = PendingNoteOff{}; } else { playing_ = false; } } } void XmidiPlayer::render(float* stereo_out, int frames, IMidiSynth& synth) { if (!playing_ || !seq_) return; int offset = 0; while (offset < frames && playing_) { if (sample_accum_ <= 0.0) { process_tick(synth); sample_accum_ += samples_per_tick_; } int step = std::min(frames - offset, std::max(1, static_cast(sample_accum_))); synth.render(stereo_out + offset * 2, step); sample_accum_ -= static_cast(step); offset += step; } } } // namespace top_octane::audio