All six instruments now run in C++ (61 reference scenarios, 16 sampler tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
|
|
// Port of audio/SynthVoice.kt: one note of the subtractive synth, or (for a Morph
|
|
// Pad / Wavetable patch) the Frasil / wavetable voice it holds. noteOn, noteOff,
|
|
// setPatch and render never allocate or lock.
|
|
|
|
#include <cstdint>
|
|
#include "frasil/FrasilVoice.hpp"
|
|
#include "synth/SynthPatch.hpp"
|
|
#include "wavetable/WaveVoice.hpp"
|
|
|
|
namespace sizzle::synth {
|
|
|
|
class SynthVoice {
|
|
public:
|
|
/** [clock] must outlive the voice; [padSeed] is the next seed of the engine's
|
|
* FrasilSeeds (Kotlin's FrasilVoice(sampleRate) inside the SynthVoice takes it). */
|
|
SynthVoice(int sampleRate, const frasil::FrasilClock &clock, int32_t padSeed);
|
|
|
|
int pitch() const { return pitch_; }
|
|
/** Which tracker (lane,track) started this voice, or -1 if unowned (live/arp). */
|
|
int ownerTrack = -1;
|
|
|
|
bool isActive() const;
|
|
/** Gate still open (attack/decay/sustain): the note is held. */
|
|
bool isGated() const;
|
|
|
|
/** Start (or steal-and-restart) this voice with [patch] (copied). */
|
|
void noteOn(int midi, int velocity, const SynthPatch &patch);
|
|
void noteOff();
|
|
/** Hard stop with no release tail (transport stop / voice steal). */
|
|
void kill();
|
|
/** Swap in updated params for a still-sounding voice; a patch with the same id
|
|
* as the current one is ignored (Kotlin's `p === patch`). */
|
|
void setPatch(const SynthPatch &p);
|
|
float render();
|
|
|
|
/** Equal-tempered MIDI note -> Hz (A4 = 69 = 440 Hz). */
|
|
static double midiToHz(int midi);
|
|
|
|
private:
|
|
/** Linear ADSR envelope (levels 0..1); level is not reset on gate-on. */
|
|
class Adsr {
|
|
public:
|
|
bool active() const { return stage != 0; }
|
|
bool gateOpen() const { return stage >= 1 && stage <= 3; }
|
|
void gateOn(float aa, float dd, float ss, float rr);
|
|
void gateOff() { if (stage != 0) stage = 4; }
|
|
void kill() { stage = 0; level = 0.f; }
|
|
float advance(float dt);
|
|
private:
|
|
int stage = 0; // 0 idle, 1 attack, 2 decay, 3 sustain, 4 release
|
|
float level = 0.f;
|
|
float a = 0.01f, d = 0.1f, s = 0.7f, r = 0.2f;
|
|
};
|
|
|
|
static constexpr int MODE_SYNTH = 0;
|
|
static constexpr int MODE_PAD = 1;
|
|
static constexpr int MODE_WAVE = 2;
|
|
static constexpr int OSC_COUNT = SynthPatch::OSC_COUNT;
|
|
static constexpr int MOD_SLOTS = SynthPatch::MOD_SLOTS;
|
|
|
|
void computeTuning();
|
|
float waveform(int wave, double t, double dt, float pwm) const;
|
|
static float pulse(double t, double dt, float w);
|
|
static double polyBlep(double t, double dt);
|
|
float lfoValue() const;
|
|
float svf(float input);
|
|
float nextRand();
|
|
|
|
const int sampleRate;
|
|
SynthPatch patch;
|
|
int pitch_ = -1;
|
|
float velocity = 0.f;
|
|
double baseFreq = 440.0;
|
|
float keyNorm = 0.f;
|
|
|
|
double phase[OSC_COUNT] = {};
|
|
float staticSemis[OSC_COUNT] = {};
|
|
double staticInc[OSC_COUNT] = {};
|
|
double modInc[OSC_COUNT] = {};
|
|
bool pitchModActive = false;
|
|
|
|
float icB = 0.f, icL = 0.f;
|
|
float ca1 = 0.f, ca2 = 0.f, ca3 = 0.f;
|
|
int ctrl = 0;
|
|
|
|
double lfoPhase = 0.0;
|
|
double lfoInc = 0.0;
|
|
float shValue = 0.f;
|
|
uint32_t rng = 0x2545F491u;
|
|
|
|
float ampMod = 0.f;
|
|
bool ampModPrimed = false;
|
|
const float ampModCoeff;
|
|
|
|
Adsr ampEnv;
|
|
Adsr filtEnv;
|
|
|
|
frasil::FrasilVoice pad;
|
|
wavetable::WaveVoice wave;
|
|
int mode = MODE_SYNTH;
|
|
};
|
|
|
|
} // namespace sizzle::synth
|