Files
top-octane/src/audio/opl3_synth.hpp
T

117 lines
3.9 KiB
C++
Raw Normal View History

2026-11-30 22:31:51 +02:00
#pragma once
#include "assets/music_bank.hpp"
#include <array>
#include <cstdint>
namespace top_octane::audio {
// Abstract MIDI synthesizer interface used by XmidiPlayer
class IMidiSynth {
public:
virtual ~IMidiSynth() = default;
virtual void set_sample_rate(int sample_rate) = 0;
virtual void note_on(uint8_t ch, uint8_t key, uint8_t vel) = 0;
virtual void note_off(uint8_t ch, uint8_t key) = 0;
virtual void program_change(uint8_t ch, uint8_t program) = 0;
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;
2026-11-30 23:22:00 +02:00
virtual void reset() = 0;
2026-11-30 22:31:51 +02:00
virtual void render(float* stereo_out, int frames) = 0;
};
// Self-contained Yamaha OPL3 FM synthesizer driven by Miles SAMPLE.OPL timbres
class Opl3Synth final : public IMidiSynth {
public:
static constexpr int kNumVoices = 18;
static constexpr int kNumChannels = 16;
explicit Opl3Synth(const assets::OplTimbreBank* bank = nullptr, int sample_rate = 48000);
void set_timbre_bank(const assets::OplTimbreBank* bank) { bank_ = bank; }
void set_sample_rate(int sample_rate) override;
void note_on(uint8_t ch, uint8_t key, uint8_t vel) override;
void note_off(uint8_t ch, uint8_t key) override;
void program_change(uint8_t ch, uint8_t program) override;
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;
2026-11-30 23:22:00 +02:00
void reset() override;
2026-11-30 22:31:51 +02:00
void render(float* stereo_out, int frames) override;
private:
enum class EnvStage { Off = 0, Attack, Decay, Sustain, Release };
struct Operator {
double phase = 0.0;
double phase_inc = 0.0;
double mult = 1.0;
2026-11-30 23:22:00 +02:00
float env = 0.0f; // 0..1 linear amplitude
float env_db = 96.0f; // 0 dB = peak, 96 dB = silence
2026-11-30 22:31:51 +02:00
EnvStage stage = EnvStage::Off;
float attack_rate = 0.0f;
2026-11-30 23:22:00 +02:00
float decay_rate = 0.0f; // dB/s
float sustain_db = 12.0f; // dB attenuation
float release_rate = 0.0f; // dB/s
float attack_alpha = 0.0f;
float decay_mul = 1.0f;
float sustain_lin = 0.0f;
float release_mul = 1.0f;
2026-11-30 23:22:00 +02:00
float total_level = 1.0f; // linear scale from TL + KSL + vel
2026-11-30 22:31:51 +02:00
uint8_t wave = 0;
bool am = false;
bool vib = false;
bool sustain_hold = true;
};
struct Voice {
bool active = false;
2026-11-30 23:22:00 +02:00
bool sustained = false;
2026-11-30 22:31:51 +02:00
uint8_t channel = 0;
uint8_t key = 0;
uint8_t velocity = 100;
uint32_t age = 0;
float base_freq = 440.0f;
float feedback = 0.0f;
bool additive = false;
float prev_mod0 = 0.0f;
float prev_mod1 = 0.0f;
Operator op_mod{};
Operator op_car{};
};
struct ChannelState {
uint8_t program = 0;
uint8_t bank = 0;
2026-11-30 23:22:00 +02:00
float volume_target = 1.0f;
float vol_smooth = 1.0f;
float expr_target = 1.0f;
float expr_smooth = 1.0f;
2026-11-30 22:31:51 +02:00
float pan = 0.0f; // -1..+1
float pan_l = 0.70710678f;
float pan_r = 0.70710678f;
2026-11-30 22:31:51 +02:00
float pitch_bend_semitones = 0.0f;
float bend_mul = 1.0f;
2026-11-30 23:22:00 +02:00
bool sustain_pedal = false;
2026-11-30 22:31:51 +02:00
};
void setup_operator(Operator& op, uint8_t reg20, uint8_t reg40, uint8_t reg60,
2026-11-30 23:22:00 +02:00
uint8_t reg80, uint8_t regE0, float base_freq, float midi_note,
float vel_scale, bool is_carrier);
void update_operator_rates(Operator& op);
void step_envelope(Operator& op);
2026-11-30 22:31:51 +02:00
static float eval_waveform(uint8_t wave, double phase);
void update_voice_freq(Voice& v);
const assets::OplTimbreBank* bank_ = nullptr;
int sample_rate_ = 48000;
uint32_t voice_counter_ = 0;
double lfo_phase_ = 0.0;
std::array<Voice, kNumVoices> voices_{};
std::array<ChannelState, kNumChannels> channels_{};
};
} // namespace top_octane::audio