Files
top-octane/src/audio/xmidi_player.cpp
T

207 lines
7.6 KiB
C++
Raw Normal View History

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, uint32_t start_tick) {
2026-11-30 22:31:51 +02:00
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;
2026-11-30 22:31:51 +02:00
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;
pending_start_tick_ = 0;
2026-11-30 22:31:51 +02:00
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;
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() &&
static_cast<uint64_t>(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<int16_t>(((ev.data2 & 0x7Fu) << 7) | (ev.data1 & 0x7Fu)) - 8192;
synth.pitch_bend(ch, bend14);
}
}
current_tick_ = pending_start_tick_;
pending_start_tick_ = 0;
}
2026-11-30 22:31:51 +02:00
// 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_
while (event_cursor_ < evs.size() &&
static_cast<uint64_t>(evs[event_cursor_].tick) <= current_tick_) {
2026-11-30 22:31:51 +02:00
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) {
PendingNoteOff* slot = nullptr;
PendingNoteOff* oldest = &note_offs_[0];
2026-11-30 22:31:51 +02:00
for (auto& n : note_offs_) {
if (!n.active) {
slot = &n;
2026-11-30 22:31:51 +02:00
break;
}
if (n.off_tick < oldest->off_tick) {
oldest = &n;
}
2026-11-30 22:31:51 +02:00
}
if (!slot) {
synth.note_off(oldest->channel, oldest->key);
slot = oldest;
}
slot->active = true;
slot->off_tick = current_tick_ + static_cast<uint64_t>(ev.duration_ticks);
slot->channel = ch;
slot->key = ev.data1;
2026-11-30 22:31:51 +02:00
}
}
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_ > static_cast<uint64_t>(seq_->total_ticks)) {
2026-11-30 22:31:51 +02:00
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 (!stereo_out || frames <= 0 || !playing_ || !seq_) return;
2026-11-30 22:31:51 +02:00
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