This commit is contained in:
Luxferre committed 2026-11-30 23:22:00 +02:00
1 parent 15ab48343a
commit c017d27b2a
11 files changed
+1554 -263

No files matched your search

+56 -2
View File
@@ -18,12 +18,14 @@ void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
sample_accum_ = 0.0;
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
for (auto& n : note_offs_) n = PendingNoteOff{};
loop_depth_ = 0;
}
void XmidiPlayer::stop() {
playing_ = false;
seq_ = nullptr;
for (auto& n : note_offs_) n = PendingNoteOff{};
loop_depth_ = 0;
}
void XmidiPlayer::process_tick(IMidiSynth& synth) {
@@ -37,24 +39,41 @@ void XmidiPlayer::process_tick(IMidiSynth& synth) {
}
}
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_
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) {
// Miles XMIDI stays anchored at 120 ticks/sec; tempo meta adjusts subtly if needed
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_) {
@@ -70,7 +89,41 @@ void XmidiPlayer::process_tick(IMidiSynth& synth) {
}
break;
case 0xB0u:
synth.control_change(ch, ev.data1, ev.data2);
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);
}
break;
case 0xC0u:
synth.program_change(ch, ev.data1);
@@ -91,6 +144,7 @@ void XmidiPlayer::process_tick(IMidiSynth& synth) {
synth.all_notes_off();
event_cursor_ = 0;
current_tick_ = 0;
loop_depth_ = 0;
for (auto& n : note_offs_) n = PendingNoteOff{};
} else {
playing_ = false;