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

+6 -6
View File
@@ -299,8 +299,8 @@ void AudioEngine::drain_commands() {
active_track_ = cmd.track;
active_loop_ = cmd.loop;
int slot = map_track_slot(cmd.track);
opl_synth_.all_notes_off();
sf_synth_.all_notes_off();
opl_synth_.reset();
sf_synth_.reset();
custom_music_playing_ = false;
custom_music_pos_ = 0.0;
if (active_backend_ == MusicBackend::Custom &&
@@ -316,8 +316,8 @@ void AudioEngine::drain_commands() {
}
case CmdType::StopMusic:
xmi_player_.stop();
opl_synth_.all_notes_off();
sf_synth_.all_notes_off();
opl_synth_.reset();
sf_synth_.reset();
custom_music_playing_ = false;
break;
@@ -325,8 +325,8 @@ void AudioEngine::drain_commands() {
if (cmd.backend != active_backend_) {
bool was_playing = xmi_player_.is_playing() || custom_music_playing_;
active_backend_ = cmd.backend;
opl_synth_.all_notes_off();
sf_synth_.all_notes_off();
opl_synth_.reset();
sf_synth_.reset();
custom_music_playing_ = false;
if (was_playing) {
int slot = map_track_slot(active_track_);
+144 -41
View File
@@ -14,22 +14,37 @@ constexpr double kMultTable[16] = {
};
float rate_to_sec(int rate_4bit, bool is_attack) {
if (rate_4bit <= 0) return 8.0f;
if (rate_4bit >= 15) return 0.002f;
// Exponential OPL rate curve mapping 1..14 cleanly to seconds
if (rate_4bit <= 0) return 0.0f;
if (rate_4bit >= 15) return is_attack ? 0.0008f : 0.010f;
float norm = static_cast<float>(15 - rate_4bit);
float base = is_attack ? 0.0025f : 0.008f;
return base * std::pow(1.62f, norm);
float base = is_attack ? 0.001f : 0.014f;
return base * std::pow(2.0f, norm);
}
inline float soft_limit_safety(float x, float thresh) {
float ax = std::abs(x);
if (ax <= thresh) return x;
float headroom = 1.0f - thresh;
float limited = thresh + headroom * std::tanh((ax - thresh) / headroom);
return (x >= 0.0f) ? limited : -limited;
}
} // namespace
Opl3Synth::Opl3Synth(const assets::OplTimbreBank* bank, int sample_rate)
: bank_(bank), sample_rate_(std::max(sample_rate, 8000)) {
reset();
}
void Opl3Synth::reset() {
for (auto& v : voices_) {
v = Voice{};
}
for (int i = 0; i < kNumChannels; ++i) {
channels_[i] = ChannelState{};
channels_[i].bank = (i == 9) ? 127 : 0;
}
lfo_phase_ = 0.0;
}
void Opl3Synth::set_sample_rate(int sample_rate) {
@@ -37,7 +52,7 @@ void Opl3Synth::set_sample_rate(int sample_rate) {
}
void Opl3Synth::setup_operator(Operator& op, uint8_t reg20, uint8_t reg40, uint8_t reg60,
uint8_t reg80, uint8_t regE0, float base_freq,
uint8_t reg80, uint8_t regE0, float base_freq, float midi_note,
float vel_scale, bool is_carrier) {
double mult = kMultTable[reg20 & 0x0Fu];
op.phase = 0.0;
@@ -47,9 +62,21 @@ void Opl3Synth::setup_operator(Operator& op, uint8_t reg20, uint8_t reg40, uint8
op.sustain_hold = (reg20 & 0x20u) != 0;
op.wave = regE0 & 7u;
// Total Level (6-bit, 0 = max, 63 = -47.25 dB)
// Total Level (6-bit, 0 = max, 63 = -47.25 dB) + Key Scale Level (KSL)
int ksl_bits = (reg40 >> 6) & 3;
float ksl_db_per_oct = 0.0f;
if (ksl_bits == 1) ksl_db_per_oct = 3.0f;
else if (ksl_bits == 2) ksl_db_per_oct = 1.5f;
else if (ksl_bits == 3) ksl_db_per_oct = 6.0f;
float ksl_db = 0.0f;
if (ksl_db_per_oct > 0.0f && midi_note > 52.0f) {
ksl_db = std::min(36.0f, ((midi_note - 52.0f) / 12.0f) * ksl_db_per_oct);
}
int tl = reg40 & 0x3Fu;
float tl_gain = std::pow(10.0f, (-0.75f * static_cast<float>(tl)) / 20.0f);
float tl_db = 0.75f * static_cast<float>(tl) + ksl_db;
float tl_gain = std::pow(10.0f, -tl_db / 20.0f);
op.total_level = tl_gain * (is_carrier ? vel_scale : 1.0f);
int ar = (reg60 >> 4) & 0x0Fu;
@@ -57,12 +84,17 @@ void Opl3Synth::setup_operator(Operator& op, uint8_t reg20, uint8_t reg40, uint8
int sl = (reg80 >> 4) & 0x0Fu;
int rr = reg80 & 0x0Fu;
op.attack_rate = 1.0f / rate_to_sec(ar, true);
op.decay_rate = 1.0f / rate_to_sec(dr, false);
op.sustain_level = (sl == 15) ? 0.0f : std::pow(10.0f, (-3.0f * static_cast<float>(sl)) / 20.0f);
op.release_rate = 1.0f / rate_to_sec(rr, false);
float a_sec = rate_to_sec(ar, true);
float d_sec = rate_to_sec(dr, false);
float r_sec = rate_to_sec(rr, false);
op.attack_rate = (a_sec > 0.0f) ? (3.0445f / a_sec) : 0.0f;
op.decay_rate = (d_sec > 0.0f) ? (96.0f / d_sec) : 0.0f;
op.sustain_db = (sl == 15) ? 96.0f : (3.0f * static_cast<float>(sl));
op.release_rate = (r_sec > 0.0f) ? (96.0f / r_sec) : 6.0f;
op.env = 0.0f;
op.stage = EnvStage::Attack;
op.env_db = 96.0f;
op.stage = (op.attack_rate > 0.0f) ? EnvStage::Attack : EnvStage::Off;
}
void Opl3Synth::update_voice_freq(Voice& v) {
@@ -120,34 +152,42 @@ void Opl3Synth::note_on(uint8_t ch, uint8_t key, uint8_t vel) {
midi_note += static_cast<float>(static_cast<int8_t>(patch->regs[2]));
}
v.base_freq = 440.0f * std::pow(2.0f, (midi_note - 69.0f) / 12.0f);
float vel_scale = std::clamp(static_cast<float>(vel) / 127.0f, 0.08f, 1.0f);
float vel_norm = std::clamp(static_cast<float>(vel) / 127.0f, 0.0f, 1.0f);
float vel_scale = std::max(vel_norm * vel_norm, 0.001f);
if (patch) {
uint8_t c0 = patch->regs[8];
int fb = (c0 >> 1) & 7;
v.feedback = (fb == 0) ? 0.0f : (static_cast<float>(1 << fb) / 64.0f);
v.feedback = (fb == 0) ? 0.0f : (0.5f / static_cast<float>(1 << (7 - fb)));
v.additive = (c0 & 1u) != 0;
setup_operator(v.op_mod, patch->regs[3], patch->regs[4], patch->regs[5],
patch->regs[6], patch->regs[7], v.base_freq,
patch->regs[6], patch->regs[7], v.base_freq, midi_note,
v.additive ? vel_scale : 1.0f, v.additive);
setup_operator(v.op_car, patch->regs[9], patch->regs[10], patch->regs[11],
patch->regs[12], patch->regs[13], v.base_freq,
patch->regs[12], patch->regs[13], v.base_freq, midi_note,
vel_scale, true);
} else {
v.feedback = 0.0f;
v.additive = false;
setup_operator(v.op_mod, 0x21, 0x18, 0xF2, 0x45, 0x00, v.base_freq, 1.0f, false);
setup_operator(v.op_car, 0x21, 0x08, 0xF4, 0x36, 0x00, v.base_freq, vel_scale, true);
setup_operator(v.op_mod, 0x21, 0x18, 0xF2, 0x45, 0x00, v.base_freq, midi_note, 1.0f, false);
setup_operator(v.op_car, 0x21, 0x08, 0xF4, 0x36, 0x00, v.base_freq, midi_note, vel_scale, true);
}
update_voice_freq(v);
}
void Opl3Synth::note_off(uint8_t ch, uint8_t key) {
if (ch >= kNumChannels) return;
bool hold = channels_[ch].sustain_pedal;
for (auto& v : voices_) {
if (v.active && v.channel == ch && v.key == key) {
if (v.op_car.stage != EnvStage::Off) v.op_car.stage = EnvStage::Release;
if (v.op_mod.stage != EnvStage::Off) v.op_mod.stage = EnvStage::Release;
if (hold) {
v.sustained = true;
} else {
v.sustained = false;
if (v.op_car.stage != EnvStage::Off) v.op_car.stage = EnvStage::Release;
if (v.op_mod.stage != EnvStage::Off) v.op_mod.stage = EnvStage::Release;
}
}
}
}
@@ -158,25 +198,63 @@ void Opl3Synth::program_change(uint8_t ch, uint8_t program) {
void Opl3Synth::control_change(uint8_t ch, uint8_t cc, uint8_t val) {
if (ch >= kNumChannels) return;
ChannelState& cs = channels_[ch];
switch (cc) {
case 0: // Bank Select MSB
channels_[ch].bank = (ch == 9) ? 127 : val;
cs.bank = (ch == 9) ? 127 : val;
break;
case 7: // Volume
channels_[ch].volume = static_cast<float>(val) / 127.0f;
cs.volume_target = std::clamp(static_cast<float>(val) / 127.0f, 0.0f, 1.0f);
break;
case 10: // Pan
channels_[ch].pan = (static_cast<float>(val) - 64.0f) / 64.0f;
cs.pan = (static_cast<float>(val) - 64.0f) / 64.0f;
break;
case 11: // Expression
channels_[ch].expression = static_cast<float>(val) / 127.0f;
cs.expr_target = std::clamp(static_cast<float>(val) / 127.0f, 0.0f, 1.0f);
break;
case 120:
case 123: // All Sound/Notes Off
case 64: // Sustain pedal
cs.sustain_pedal = (val >= 64);
if (!cs.sustain_pedal) {
for (auto& v : voices_) {
if (v.active && v.channel == ch && v.sustained) {
v.sustained = false;
if (v.op_car.stage != EnvStage::Off) v.op_car.stage = EnvStage::Release;
if (v.op_mod.stage != EnvStage::Off) v.op_mod.stage = EnvStage::Release;
}
}
}
break;
case 120: // All Sound Off
for (auto& v : voices_) {
if (v.channel == ch) v.active = false;
}
break;
case 121: // Reset All Controllers
cs.expr_target = 1.0f;
cs.expr_smooth = 1.0f;
cs.pitch_bend_semitones = 0.0f;
cs.sustain_pedal = false;
for (auto& v : voices_) {
if (v.active && v.channel == ch) {
update_voice_freq(v);
if (v.sustained) {
v.sustained = false;
if (v.op_car.stage != EnvStage::Off) v.op_car.stage = EnvStage::Release;
if (v.op_mod.stage != EnvStage::Off) v.op_mod.stage = EnvStage::Release;
}
}
}
break;
case 123: // All Notes Off
cs.sustain_pedal = false;
for (auto& v : voices_) {
if (v.active && v.channel == ch) {
v.sustained = false;
if (v.op_car.stage != EnvStage::Off) v.op_car.stage = EnvStage::Release;
if (v.op_mod.stage != EnvStage::Off) v.op_mod.stage = EnvStage::Release;
}
}
break;
default:
break;
}
@@ -197,32 +275,51 @@ void Opl3Synth::all_notes_off() {
}
void Opl3Synth::step_envelope(Operator& op, float dt) {
constexpr float kDbToLn = 0.11512925465f; // ln(10) / 20
switch (op.stage) {
case EnvStage::Off:
op.env = 0.0f;
op.env_db = 96.0f;
break;
case EnvStage::Attack:
op.env += op.attack_rate * dt;
if (op.env >= 1.0f) {
op.env = 1.0f;
op.stage = EnvStage::Decay;
if (op.attack_rate <= 0.0f) {
op.env = 0.0f;
op.env_db = 96.0f;
op.stage = EnvStage::Off;
} else {
op.env += (1.05f - op.env) * (1.0f - std::exp(-op.attack_rate * dt));
if (op.env >= 1.0f) {
op.env = 1.0f;
op.env_db = 0.0f;
op.stage = EnvStage::Decay;
}
}
break;
case EnvStage::Decay:
op.env -= op.decay_rate * dt;
if (op.env <= op.sustain_level) {
op.env = op.sustain_level;
if (op.decay_rate > 0.0f) {
op.env_db += op.decay_rate * dt;
if (op.env_db >= op.sustain_db) {
op.env_db = op.sustain_db;
op.stage = op.sustain_hold ? EnvStage::Sustain : EnvStage::Release;
}
} else if (op.sustain_db <= 0.01f) {
op.env_db = 0.0f;
op.stage = op.sustain_hold ? EnvStage::Sustain : EnvStage::Release;
}
op.env = (op.env_db >= 90.0f) ? 0.0f : std::exp(-op.env_db * kDbToLn);
break;
case EnvStage::Sustain:
op.env = op.sustain_level;
op.env_db = op.sustain_db;
op.env = (op.sustain_db >= 90.0f) ? 0.0f : std::exp(-op.env_db * kDbToLn);
break;
case EnvStage::Release:
op.env -= op.release_rate * dt;
if (op.env <= 0.0005f) {
op.env_db += op.release_rate * dt;
if (op.env_db >= 84.0f) {
op.env = 0.0f;
op.env_db = 96.0f;
op.stage = EnvStage::Off;
} else {
op.env = std::exp(-op.env_db * kDbToLn);
}
break;
}
@@ -255,6 +352,7 @@ float Opl3Synth::eval_waveform(uint8_t wave, double phase) {
void Opl3Synth::render(float* stereo_out, int frames) {
const float dt = 1.0f / static_cast<float>(sample_rate_);
const float cc_alpha = 1.0f - std::exp(-dt / 0.005f);
const double lfo_inc = 6.0 / static_cast<double>(sample_rate_);
for (int f = 0; f < frames; ++f) {
@@ -262,6 +360,11 @@ void Opl3Synth::render(float* stereo_out, int frames) {
if (lfo_phase_ >= 1.0) lfo_phase_ -= 1.0;
float lfo = static_cast<float>(std::sin(lfo_phase_ * kTwoPi));
for (int c = 0; c < kNumChannels; ++c) {
channels_[c].vol_smooth += (channels_[c].volume_target - channels_[c].vol_smooth) * cc_alpha;
channels_[c].expr_smooth += (channels_[c].expr_target - channels_[c].expr_smooth) * cc_alpha;
}
float left = 0.0f;
float right = 0.0f;
@@ -298,12 +401,12 @@ void Opl3Synth::render(float* stereo_out, int frames) {
sample = (mod_out + car_raw * v.op_car.env * v.op_car.total_level * am1) * 0.5f;
} else {
// FM phase modulation depth
float car_raw = eval_waveform(v.op_car.wave, v.op_car.phase + static_cast<double>(mod_out * 1.15f));
float car_raw = eval_waveform(v.op_car.wave, v.op_car.phase + static_cast<double>(mod_out * 1.35f));
sample = car_raw * v.op_car.env * v.op_car.total_level * am1;
}
const ChannelState& cs = channels_[v.channel];
float ch_gain = cs.volume * cs.expression * 0.55f;
float ch_gain = (cs.vol_smooth * cs.vol_smooth) * (cs.expr_smooth * cs.expr_smooth) * 0.28f;
float pan_norm = std::clamp(cs.pan, -1.0f, 1.0f);
float gl = std::sqrt(0.5f * (1.0f - pan_norm));
float gr = std::sqrt(0.5f * (1.0f + pan_norm));
@@ -311,8 +414,8 @@ void Opl3Synth::render(float* stereo_out, int frames) {
right += sample * ch_gain * gr;
}
stereo_out[f * 2 + 0] += std::tanh(left * 0.9f);
stereo_out[f * 2 + 1] += std::tanh(right * 0.9f);
stereo_out[f * 2 + 0] += soft_limit_safety(left, 0.85f);
stereo_out[f * 2 + 1] += soft_limit_safety(right, 0.85f);
}
}
+16 -8
View File
@@ -18,6 +18,7 @@ public:
virtual void control_change(uint8_t ch, uint8_t cc, uint8_t val) = 0;
virtual void pitch_bend(uint8_t ch, int16_t bend14) = 0; // -8192..+8191
virtual void all_notes_off() = 0;
virtual void reset() = 0;
virtual void render(float* stereo_out, int frames) = 0;
};
@@ -37,6 +38,7 @@ public:
void control_change(uint8_t ch, uint8_t cc, uint8_t val) override;
void pitch_bend(uint8_t ch, int16_t bend14) override;
void all_notes_off() override;
void reset() override;
void render(float* stereo_out, int frames) override;
private:
@@ -45,13 +47,14 @@ private:
struct Operator {
double phase = 0.0;
double phase_inc = 0.0;
float env = 0.0f; // 0..1 linear amplitude
float env = 0.0f; // 0..1 linear amplitude
float env_db = 96.0f; // 0 dB = peak, 96 dB = silence
EnvStage stage = EnvStage::Off;
float attack_rate = 0.0f;
float decay_rate = 0.0f;
float sustain_level = 0.5f;
float release_rate = 0.0f;
float total_level = 1.0f; // linear scale from TL + KSL + vel
float decay_rate = 0.0f; // dB/s
float sustain_db = 12.0f; // dB attenuation
float release_rate = 0.0f; // dB/s
float total_level = 1.0f; // linear scale from TL + KSL + vel
uint8_t wave = 0;
bool am = false;
bool vib = false;
@@ -60,6 +63,7 @@ private:
struct Voice {
bool active = false;
bool sustained = false;
uint8_t channel = 0;
uint8_t key = 0;
uint8_t velocity = 100;
@@ -76,14 +80,18 @@ private:
struct ChannelState {
uint8_t program = 0;
uint8_t bank = 0;
float volume = 1.0f;
float expression = 1.0f;
float volume_target = 1.0f;
float vol_smooth = 1.0f;
float expr_target = 1.0f;
float expr_smooth = 1.0f;
float pan = 0.0f; // -1..+1
float pitch_bend_semitones = 0.0f;
bool sustain_pedal = false;
};
void setup_operator(Operator& op, uint8_t reg20, uint8_t reg40, uint8_t reg60,
uint8_t reg80, uint8_t regE0, float base_freq, float vel_scale, bool is_carrier);
uint8_t reg80, uint8_t regE0, float base_freq, float midi_note,
float vel_scale, bool is_carrier);
void step_envelope(Operator& op, float dt);
static float eval_waveform(uint8_t wave, double phase);
void update_voice_freq(Voice& v);
+809 -140
View File
File diff suppressed because it is too large. Load diff
+86 -11
View File
@@ -5,21 +5,22 @@
#include <array>
#include <cstdint>
#include <string>
#include <vector>
namespace top_octane::audio {
// Creative SoundFont 1.0 (BULLFROG.SBK) & 2.0 (.sf2) wavetable synthesizer with OPL3 fallback
// Creative SoundFont 1.0 (BULLFROG.SBK) & 2.0 (.sf2) wavetable synthesizer with expressive GM ROM synthesis
class SfSynth final : public IMidiSynth {
public:
static constexpr int kMaxVoices = 32;
static constexpr int kMaxVoices = 48;
SfSynth(const assets::SoundFontBank* sf = nullptr,
const assets::OplTimbreBank* opl_fallback = nullptr,
int sample_rate = 48000);
void set_soundfont(const assets::SoundFontBank* sf) { sf_ = sf; }
void set_opl_fallback(const assets::OplTimbreBank* opl) { opl_fallback_.set_timbre_bank(opl); }
void set_opl_fallback(const assets::OplTimbreBank* opl) { (void)opl; }
void set_sample_rate(int sample_rate) override;
void note_on(uint8_t ch, uint8_t key, uint8_t vel) override;
@@ -28,45 +29,119 @@ public:
void control_change(uint8_t ch, uint8_t cc, uint8_t val) override;
void pitch_bend(uint8_t ch, int16_t bend14) override;
void all_notes_off() override;
void reset() override;
void render(float* stereo_out, int frames) override;
private:
enum class RomWaveKind { None, Saw, Square, Sine, SynthBass, Bell, Perc };
enum class RomWaveKind {
None,
Saw,
Square,
Sine,
SynthBass,
Bell,
Perc,
GmKey,
GmOrgan,
GmGuitar,
GmBass,
GmPad,
GmLead,
GmDrum
};
enum class EnvStage { Off = 0, Delay, Attack, Hold, Decay, Sustain, Release };
struct WtVoice {
bool active = false;
bool releasing = false;
bool sustained = false;
uint8_t channel = 0;
uint8_t key = 0;
uint32_t age = 0;
int sample_idx = -1;
RomWaveKind rom_kind = RomWaveKind::None;
bool loop = false;
uint32_t s_start = 0;
uint32_t s_end = 0;
uint32_t l_start = 0;
uint32_t l_end = 0;
double pos = 0.0;
double pos2 = 0.0;
double step = 1.0;
float gain = 0.0f;
float env = 0.0f;
float bend_mul = 1.0f;
float base_gain = 0.0f;
float pan = 0.0f;
float chorus_send = 0.0f;
float reverb_send = 0.0f;
double chorus_phase = 0.0;
// DAHDSR envelope
EnvStage env_stage = EnvStage::Off;
float env = 0.0f;
float env_db = 96.0f;
float stage_timer = 0.0f;
float delay_sec = 0.0f;
float attack_sec = 0.002f;
float hold_sec = 0.0f;
float decay_rate_db = 0.0f;
float sustain_db = 0.0f;
float release_rate_db = 600.0f;
// 2x-oversampled SVF resonant low-pass filter
bool has_filter = false;
float filter_fc_hz = 20000.0f;
float filter_q_damp = 1.414f;
float mod_env_to_filter_cents = 0.0f;
float svf_lp = 0.0f;
float svf_bp = 0.0f;
// Procedural GM/drum state
uint32_t noise_state = 0x12345678u;
float drum_pitch_hz = 120.0f;
float drum_end_hz = 50.0f;
float drum_sweep_rate = 30.0f;
float drum_tone_mix = 0.5f;
float gm_bright = 0.5f;
};
struct ChState {
uint8_t program = 0;
uint16_t bank = 0;
float volume = 1.0f;
float expression = 1.0f;
bool user_bank = false; // CC 119 > 0 selects BULLFROG.SBK user presets
float volume_target = 1.0f;
float vol_smooth = 1.0f;
float expr_target = 1.0f;
float expr_smooth = 1.0f;
float pan = 0.0f;
float pitch_bend_semitones = 0.0f;
float reverb_cc = 0.15f;
float chorus_cc = 0.0f;
bool sustain_pedal = false;
};
static RomWaveKind classify_rom_sample(const std::string& name);
float sample_voice(WtVoice& v);
int alloc_voice();
void spawn_sf_zone(uint8_t ch, uint8_t key, uint8_t vel, const assets::SoundFontZone& z);
void spawn_gm_voice(uint8_t ch, uint8_t key, uint8_t vel);
void step_env(WtVoice& v, float dt);
float sample_osc(WtVoice& v, float dt);
const assets::SoundFontBank* sf_ = nullptr;
Opl3Synth opl_fallback_;
int sample_rate_ = 48000;
uint32_t age_counter_ = 0;
std::array<WtVoice, kMaxVoices> voices_{};
std::array<ChState, 16> channels_{};
// Compact stereo comb-feedback reverb bus
std::vector<float> rev_buf_l_[2];
std::vector<float> rev_buf_r_[2];
size_t rev_idx_l_[2]{};
size_t rev_idx_r_[2]{};
float rev_lp_l_[2]{};
float rev_lp_r_[2]{};
};
} // namespace top_octane::audio
+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;
+9
View File
@@ -29,6 +29,13 @@ private:
uint8_t key = 0;
};
struct XmiLoopFrame {
size_t event_idx = 0;
uint32_t tick = 0;
uint8_t remaining = 0;
bool infinite = true;
};
void process_tick(IMidiSynth& synth);
const assets::XmiSequence* seq_ = nullptr;
@@ -42,6 +49,8 @@ private:
double sample_accum_ = 0.0;
std::array<PendingNoteOff, kMaxActiveNotes> note_offs_{};
std::array<XmiLoopFrame, 4> loop_stack_{};
int loop_depth_ = 0;
};
} // namespace top_octane::audio