109 lines
3.6 KiB
C++
109 lines
3.6 KiB
C++
#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;
|
|
virtual void reset() = 0;
|
|
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;
|
|
void reset() override;
|
|
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;
|
|
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; // 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;
|
|
bool sustain_hold = true;
|
|
};
|
|
|
|
struct Voice {
|
|
bool active = false;
|
|
bool sustained = false;
|
|
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;
|
|
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 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);
|
|
|
|
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
|