2026-11-30 22:31:51 +02:00
|
|
|
#include "audio/xmidi_player.hpp"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace top_octane::audio {
|
|
|
|
|
|
|
|
|
|
void XmidiPlayer::set_sample_rate(int sample_rate) {
|
|
|
|
|
sample_rate_ = std::max(sample_rate, 8000);
|
|
|
|
|
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
|
|
|
|
|
seq_ = seq;
|
|
|
|
|
loop_ = loop;
|
|
|
|
|
playing_ = (seq != nullptr && !seq->events.empty());
|
|
|
|
|
event_cursor_ = 0;
|
|
|
|
|
current_tick_ = 0;
|
|
|
|
|
sample_accum_ = 0.0;
|
|
|
|
|
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
|
|
|
|
|
for (auto& n : note_offs_) n = PendingNoteOff{};
|
2026-11-30 23:22:00 +02:00
|
|
|
loop_depth_ = 0;
|
2026-11-30 22:31:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XmidiPlayer::stop() {
|
|
|
|
|
playing_ = false;
|
|
|
|
|
seq_ = nullptr;
|
|
|
|
|
for (auto& n : note_offs_) n = PendingNoteOff{};
|
2026-11-30 23:22:00 +02:00
|
|
|
loop_depth_ = 0;
|
2026-11-30 22:31:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XmidiPlayer::process_tick(IMidiSynth& synth) {
|
|
|
|
|
if (!seq_ || !playing_) return;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 23:22:00 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-30 22:31:51 +02:00
|
|
|
// Fire events at or before current_tick_
|
|
|
|
|
const auto& evs = seq_->events;
|
|
|
|
|
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:
|
2026-11-30 23:22:00 +02:00
|
|
|
clear_pending_note(ch, ev.data1);
|
2026-11-30 22:31:51 +02:00
|
|
|
synth.note_off(ch, ev.data1);
|
|
|
|
|
break;
|
|
|
|
|
case 0x90u:
|
|
|
|
|
if (ev.data2 == 0) {
|
2026-11-30 23:22:00 +02:00
|
|
|
clear_pending_note(ch, ev.data1);
|
2026-11-30 22:31:51 +02:00
|
|
|
synth.note_off(ch, ev.data1);
|
|
|
|
|
} else {
|
2026-11-30 23:22:00 +02:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-30 22:31:51 +02:00
|
|
|
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:
|
2026-11-30 23:22:00 +02:00
|
|
|
if (ev.data1 == 116u) {
|
|
|
|
|
// XMIDI FOR Loop Start
|
|
|
|
|
if (loop_depth_ < static_cast<int>(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);
|
|
|
|
|
}
|
2026-11-30 22:31:51 +02:00
|
|
|
break;
|
|
|
|
|
case 0xC0u:
|
|
|
|
|
synth.program_change(ch, ev.data1);
|
|
|
|
|
break;
|
|
|
|
|
case 0xE0u: {
|
|
|
|
|
int16_t bend14 = static_cast<int16_t>(((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;
|
2026-11-30 23:22:00 +02:00
|
|
|
loop_depth_ = 0;
|
2026-11-30 22:31:51 +02:00
|
|
|
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<int>(sample_accum_)));
|
|
|
|
|
synth.render(stereo_out + offset * 2, step);
|
|
|
|
|
sample_accum_ -= static_cast<double>(step);
|
|
|
|
|
offset += step;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace top_octane::audio
|