init commit

This commit is contained in:
Luxferre committed 2026-11-30 22:31:51 +02:00
commit 15ab48343a
187 files changed
+12644

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
#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{};
}
void XmidiPlayer::stop() {
playing_ = false;
seq_ = nullptr;
for (auto& n : note_offs_) n = PendingNoteOff{};
}
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;
}
}
// 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:
synth.note_off(ch, ev.data1);
break;
case 0x90u:
if (ev.data2 == 0) {
synth.note_off(ch, ev.data1);
} else {
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:
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<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;
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