0.37.0 (71): one engine, one version for Android and iOS
- Three source trees: core/ (shared C++ engine, moved from ios/core), app/ (Android UI), ios/app/ (iOS UI) - Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe callback renders natively; the Kotlin model is mirrored as project text and applied as a diff, so playback continues through edits - The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference) - Shared MasterRecorder in core/src/host, used by both apps; host_test - versions.properties: one versionName/versionCode for both platforms, continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
64e21e0bcc
commit
2bc5cb1cbb
@@ -0,0 +1,48 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
// Port of model/SynthDefs.kt: the option lists the subtractive synth's slot
|
||||
// values are written with, and its shape constants.
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace sizzle::synth::defs {
|
||||
|
||||
/** Oscillator waveforms. */
|
||||
constexpr const char *WAVES[] = {"Tri", "Saw", "Square", "Pulse25", "Pulse12"};
|
||||
constexpr int WAVES_COUNT = 5;
|
||||
/** LFO waveforms. */
|
||||
constexpr const char *LFO_WAVES[] = {"Tri", "Saw", "Square", "Sine", "S&H"};
|
||||
constexpr int LFO_WAVES_COUNT = 5;
|
||||
/** LFO start-phase offsets, eight 45-degree steps. */
|
||||
constexpr const char *LFO_PHASES[] = {"0°", "45°", "90°", "135°", "180°", "225°", "270°", "315°"};
|
||||
constexpr int LFO_PHASES_COUNT = 8;
|
||||
/** Modulation-matrix sources; index 0 ("Off") disables the slot. */
|
||||
constexpr const char *MOD_SRC[] = {"Off", "LFO", "FiltEnv", "AmpEnv", "Vel", "KeyTrk"};
|
||||
constexpr int MOD_SRC_COUNT = 6;
|
||||
/** Modulation-matrix destinations. */
|
||||
constexpr const char *MOD_DST[] = {"Pitch", "Osc2", "Osc3", "Cutoff", "PWM", "Amp"};
|
||||
constexpr int MOD_DST_COUNT = 6;
|
||||
|
||||
constexpr int OSC_COUNT = 3;
|
||||
constexpr int MOD_SLOTS = 4;
|
||||
|
||||
/** Init-patch oscillator waveforms (per oscillator). */
|
||||
constexpr const char *DEF_OSC_WAVE[OSC_COUNT] = {"Saw", "Square", "Tri"};
|
||||
/** Init-patch oscillator mixer levels (only oscillator 1 audible by default). */
|
||||
constexpr float DEF_OSC_LEVEL[OSC_COUNT] = {0.9f, 0.f, 0.f};
|
||||
|
||||
/** List.indexOf: -1 when absent. */
|
||||
inline int indexOf(const char *const *list, int n, const char *v) {
|
||||
for (int i = 0; i < n; ++i) if (std::strcmp(list[i], v) == 0) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** A LFO_PHASES label as a fraction of one LFO cycle (0..1). */
|
||||
inline float lfoPhaseFraction(const char *label) {
|
||||
int i = indexOf(LFO_PHASES, LFO_PHASES_COUNT, label);
|
||||
return i < 0 ? 0.f : (float)i / (float)LFO_PHASES_COUNT;
|
||||
}
|
||||
|
||||
} // namespace sizzle::synth::defs
|
||||
@@ -0,0 +1,100 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Port of SynthPatch.kt, line for line.
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include "synth/SynthPatch.hpp"
|
||||
#include "KotlinMath.hpp"
|
||||
#include "model/TimeDivision.hpp"
|
||||
#include "sizzle/Params.hpp"
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang fp contract(off) // Java never fuses multiply-adds
|
||||
#endif
|
||||
|
||||
namespace sizzle::synth {
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<uint32_t> nextId{1};
|
||||
|
||||
int idx(int i) { return i < 0 ? 0 : i; }
|
||||
int waveIndex(const char *s) { return idx(defs::indexOf(defs::WAVES, defs::WAVES_COUNT, s)); }
|
||||
int lfoWaveIndex(const char *s) { return idx(defs::indexOf(defs::LFO_WAVES, defs::LFO_WAVES_COUNT, s)); }
|
||||
int srcIndex(const char *s) { return idx(defs::indexOf(defs::MOD_SRC, defs::MOD_SRC_COUNT, s)); }
|
||||
int dstIndex(const char *s) { return idx(defs::indexOf(defs::MOD_DST, defs::MOD_DST_COUNT, s)); }
|
||||
|
||||
} // namespace
|
||||
|
||||
SynthPatch SynthPatch::from(const Params *slot, float tempoBpm) {
|
||||
auto f = [&](const char *k, float d) { return slot ? slot->f(k, d) : d; };
|
||||
auto s = [&](const char *k, const char *d) { return slot ? slot->s(k, d) : d; };
|
||||
char key[16];
|
||||
SynthPatch p;
|
||||
for (int i = 0; i < OSC_COUNT; ++i) {
|
||||
std::snprintf(key, sizeof key, "osc%dwave", i);
|
||||
p.oscWave[i] = waveIndex(s(key, defs::DEF_OSC_WAVE[i]));
|
||||
std::snprintf(key, sizeof key, "osc%doct", i);
|
||||
p.oscOct[i] = kt::coerceIn(kt::toInt(f(key, 0.f)), -2, 2);
|
||||
std::snprintf(key, sizeof key, "osc%dsemi", i);
|
||||
p.oscSemi[i] = kt::coerceIn(kt::toInt(f(key, 0.f)), -12, 12);
|
||||
std::snprintf(key, sizeof key, "osc%dfine", i);
|
||||
p.oscFine[i] = kt::coerceIn(f(key, 0.f), -50.f, 50.f);
|
||||
std::snprintf(key, sizeof key, "osc%dlevel", i);
|
||||
p.oscLevel[i] = kt::coerceIn(f(key, defs::DEF_OSC_LEVEL[i]), 0.f, 1.f);
|
||||
}
|
||||
|
||||
int divIdx = kt::toInt(f("lfodiv", 3.f));
|
||||
float beatFraction = (float)model::beatFraction(model::fromIndex(divIdx));
|
||||
p.lfoHz = beatFraction > 0.f ? (tempoBpm / 60.f) / beatFraction : 2.f;
|
||||
|
||||
for (int n = 0; n < MOD_SLOTS; ++n) {
|
||||
std::snprintf(key, sizeof key, "m%dsrc", n);
|
||||
p.modSrc[n] = srcIndex(s(key, "Off"));
|
||||
std::snprintf(key, sizeof key, "m%ddst", n);
|
||||
p.modDst[n] = dstIndex(s(key, "Cutoff"));
|
||||
std::snprintf(key, sizeof key, "m%damt", n);
|
||||
p.modAmt[n] = kt::coerceIn(f(key, 0.f), -1.f, 1.f);
|
||||
}
|
||||
|
||||
p.noiseLevel = kt::coerceIn(f("noise", 0.f), 0.f, 1.f);
|
||||
p.cutoff = kt::coerceIn(f("cutoff", 0.55f), 0.f, 1.f);
|
||||
p.resonance = kt::coerceIn(f("reso", 0.15f), 0.f, 1.f);
|
||||
p.keytrack = kt::coerceIn(f("keytrack", 0.3f), 0.f, 1.f);
|
||||
p.filterEnvAmt = kt::coerceIn(f("fenvamt", 0.4f), -1.f, 1.f);
|
||||
p.fA = f("fatk", 0.005f); p.fD = f("fdec", 0.25f);
|
||||
p.fS = f("fsus", 0.4f); p.fR = f("frel", 0.2f);
|
||||
p.aA = f("attack", 0.005f); p.aD = f("decay", 0.2f);
|
||||
p.aS = f("sustain", 0.7f); p.aR = f("release", 0.2f);
|
||||
p.lfoWave = lfoWaveIndex(s("lfowave", "Tri"));
|
||||
// Absent in presets saved before the phase control existed: the default label, 0.
|
||||
p.lfoPhase = defs::lfoPhaseFraction(s("lfophase", defs::LFO_PHASES[0]));
|
||||
p.kind = Kind::SYNTH;
|
||||
p.id = nextId.fetch_add(1, std::memory_order_relaxed);
|
||||
return p;
|
||||
}
|
||||
|
||||
const SynthPatch &SynthPatch::defaults() {
|
||||
static const SynthPatch d = from(nullptr, 120.f);
|
||||
return d;
|
||||
}
|
||||
|
||||
SynthPatch SynthPatch::forPad(const frasil::FrasilPatch &pad) {
|
||||
SynthPatch p = defaults();
|
||||
p.kind = Kind::PAD;
|
||||
p.pad = pad;
|
||||
p.id = nextId.fetch_add(1, std::memory_order_relaxed);
|
||||
return p;
|
||||
}
|
||||
|
||||
SynthPatch SynthPatch::forWave(const wavetable::WavePatch &wave) {
|
||||
SynthPatch p = defaults();
|
||||
p.kind = Kind::WAVE;
|
||||
p.wave = wave;
|
||||
p.id = nextId.fetch_add(1, std::memory_order_relaxed);
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace sizzle::synth
|
||||
@@ -0,0 +1,68 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
// Port of audio/SynthPatch.kt: every parameter of the subtractive synth voice,
|
||||
// parsed once from a slot. A fixed-size copyable value (no heap). Kotlin's nullable
|
||||
// `pad` / `wave` fields become [kind] plus the patch value.
|
||||
|
||||
#include <cstdint>
|
||||
#include "frasil/FrasilVoice.hpp"
|
||||
#include "synth/SynthDefs.hpp"
|
||||
#include "wavetable/WaveVoice.hpp"
|
||||
|
||||
namespace sizzle {
|
||||
class Params;
|
||||
}
|
||||
|
||||
namespace sizzle::synth {
|
||||
|
||||
struct SynthPatch {
|
||||
static constexpr int OSC_COUNT = defs::OSC_COUNT;
|
||||
static constexpr int MOD_SLOTS = defs::MOD_SLOTS;
|
||||
|
||||
/** Which engine plays the note: Kotlin's pad != null / wave != null. */
|
||||
enum class Kind : uint8_t { SYNTH, PAD, WAVE };
|
||||
|
||||
// ---- oscillators (index 0..2) ----
|
||||
int oscWave[OSC_COUNT] = {}; // index into WAVES
|
||||
int oscOct[OSC_COUNT] = {}; // octave transpose, -2..+2
|
||||
int oscSemi[OSC_COUNT] = {}; // semitone detune, -12..+12
|
||||
float oscFine[OSC_COUNT] = {}; // fine detune in cents, -50..+50
|
||||
float oscLevel[OSC_COUNT] = {}; // mixer level, 0..1
|
||||
float noiseLevel = 0.f;
|
||||
// ---- filter ----
|
||||
float cutoff = 0.f, resonance = 0.f, keytrack = 0.f, filterEnvAmt = 0.f;
|
||||
float fA = 0.f, fD = 0.f, fS = 0.f, fR = 0.f;
|
||||
// ---- amplifier ----
|
||||
float aA = 0.f, aD = 0.f, aS = 0.f, aR = 0.f;
|
||||
// ---- LFO ----
|
||||
int lfoWave = 0;
|
||||
float lfoHz = 0.f; // tempo-synced rate
|
||||
float lfoPhase = 0.f; // start-phase offset, 0..1
|
||||
// ---- modulation matrix ----
|
||||
int modSrc[MOD_SLOTS] = {}; // index into MOD_SRC (0 == Off)
|
||||
int modDst[MOD_SLOTS] = {}; // index into MOD_DST
|
||||
float modAmt[MOD_SLOTS] = {}; // -1..1
|
||||
|
||||
Kind kind = Kind::SYNTH;
|
||||
/** Valid when kind == PAD (the subtractive fields above are then unused). */
|
||||
frasil::FrasilPatch pad{};
|
||||
/** Valid when kind == WAVE. */
|
||||
wavetable::WavePatch wave{};
|
||||
|
||||
/** Stands in for Kotlin's object identity (SynthVoice.setPatch's `p === patch`):
|
||||
* stamped by each factory below, kept by copies. */
|
||||
uint32_t id = 0;
|
||||
|
||||
/** SynthPatch.from(slot, tempoBpm); [slot] may be null (init patch). */
|
||||
static SynthPatch from(const Params *slot, float tempoBpm);
|
||||
/** SynthPatch.DEFAULT: from(null, 120). */
|
||||
static const SynthPatch &defaults();
|
||||
/** A Morph Pad patch: the default synth fields, carrying [pad]. */
|
||||
static SynthPatch forPad(const frasil::FrasilPatch &pad);
|
||||
/** A Wavetable patch: the default synth fields, carrying [wave]. */
|
||||
static SynthPatch forWave(const wavetable::WavePatch &wave);
|
||||
};
|
||||
|
||||
} // namespace sizzle::synth
|
||||
@@ -0,0 +1,60 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// The reference tests' SYNTH maker (see TestVoice.hpp, IosGoldenInstruments.kt):
|
||||
// a SynthVoice with the patch the engine builds for the slot's type.
|
||||
|
||||
#include <cstring>
|
||||
#include "frasil/FrasilVoice.hpp"
|
||||
#include "sizzle/TestVoice.hpp"
|
||||
#include "synth/SynthVoice.hpp"
|
||||
#include "wavetable/WaveTables.hpp"
|
||||
|
||||
namespace sizzle {
|
||||
namespace {
|
||||
|
||||
using frasil::FrasilClock;
|
||||
using frasil::FrasilSeeds;
|
||||
using synth::SynthPatch;
|
||||
using synth::SynthVoice;
|
||||
|
||||
SynthPatch enginePatch(const TestVoiceSetup &s) {
|
||||
if (std::strcmp(s.type, "NES_SYNTH") == 0) return SynthPatch::from(s.params, s.tempoBpm);
|
||||
if (std::strcmp(s.type, "MORPH_PAD") == 0) return SynthPatch::forPad(frasil::FrasilPatch::from(s.params));
|
||||
if (std::strcmp(s.type, "WAVETABLE") == 0)
|
||||
return SynthPatch::forWave(wavetable::WavePatch::from(s.params, s.tempoBpm));
|
||||
return SynthPatch::defaults();
|
||||
}
|
||||
|
||||
FrasilSeeds seedsAt(uint32_t counter) {
|
||||
FrasilSeeds seeds;
|
||||
seeds.seeds = counter;
|
||||
return seeds;
|
||||
}
|
||||
|
||||
class SynthTestVoice final : public TestVoice {
|
||||
FrasilClock clock;
|
||||
FrasilSeeds seeds;
|
||||
SynthPatch patch;
|
||||
SynthVoice voice;
|
||||
int sampleRate;
|
||||
|
||||
public:
|
||||
explicit SynthTestVoice(const TestVoiceSetup &s)
|
||||
: clock{s.clock0}, seeds(seedsAt(s.frasilSeeds)), patch(enginePatch(s)),
|
||||
voice(s.sampleRate, clock, seeds.next()), sampleRate(s.sampleRate) {}
|
||||
|
||||
void blockStart(int frames) override { clock.advance(frames, sampleRate); }
|
||||
void noteOn(int midi, int velocity) override { voice.noteOn(midi, velocity, patch); }
|
||||
void noteOff(int) override { voice.noteOff(); }
|
||||
float render() override { return voice.render(); }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<TestVoice> makeSynthTestVoice(const TestVoiceSetup &setup) {
|
||||
wavetable::WaveTables::prewarmBlocking();
|
||||
return std::make_unique<SynthTestVoice>(setup);
|
||||
}
|
||||
|
||||
} // namespace sizzle
|
||||
@@ -0,0 +1,286 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Port of SynthVoice.kt, line for line. See the Kotlin for the commentary on the
|
||||
// sound; comments here are about the port.
|
||||
|
||||
#include <cmath>
|
||||
#include "synth/SynthVoice.hpp"
|
||||
#include "KotlinMath.hpp"
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang fp contract(off) // Java never fuses multiply-adds
|
||||
#endif
|
||||
|
||||
namespace sizzle::synth {
|
||||
|
||||
namespace {
|
||||
constexpr double PI = 3.141592653589793; // Math.PI
|
||||
constexpr float OUT_GAIN = 0.5f;
|
||||
constexpr int CTRL_INTERVAL = 16;
|
||||
constexpr double AMP_MOD_SMOOTH_SEC = 0.002;
|
||||
|
||||
double fastPow2(double x) { return std::pow(2.0, x); }
|
||||
double fastPow(double base, double x) { return std::pow(base, x); }
|
||||
} // namespace
|
||||
|
||||
// ------------------------------------------------------------------- Adsr ---
|
||||
|
||||
void SynthVoice::Adsr::gateOn(float aa, float dd, float ss, float rr) {
|
||||
a = kt::coerceAtLeast(aa, 0.0005f); d = dd; s = kt::coerceIn(ss, 0.f, 1.f); r = rr;
|
||||
stage = 1;
|
||||
}
|
||||
|
||||
float SynthVoice::Adsr::advance(float dt) {
|
||||
switch (stage) {
|
||||
case 1: level += dt / a; if (level >= 1.f) { level = 1.f; stage = 2; } break;
|
||||
case 2: level -= dt / kt::coerceAtLeast(d, 0.001f) * (1.f - s); if (level <= s) { level = s; stage = 3; } break;
|
||||
case 3: break;
|
||||
case 4: level -= dt / kt::coerceAtLeast(r, 0.001f); if (level <= 0.f) { level = 0.f; stage = 0; } break;
|
||||
default: break;
|
||||
}
|
||||
return level;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- SynthVoice ---
|
||||
|
||||
SynthVoice::SynthVoice(int sampleRate, const frasil::FrasilClock &clock, int32_t padSeed)
|
||||
: sampleRate(sampleRate), patch(SynthPatch::defaults()),
|
||||
ampModCoeff((float)(1.0 - std::exp(-1.0 / (sampleRate * AMP_MOD_SMOOTH_SEC)))),
|
||||
pad(sampleRate, clock, padSeed), wave(sampleRate) {}
|
||||
|
||||
double SynthVoice::midiToHz(int midi) { return 440.0 * std::pow(2.0, (midi - 69) / 12.0); }
|
||||
|
||||
bool SynthVoice::isActive() const {
|
||||
switch (mode) {
|
||||
case MODE_PAD: return pad.isActive();
|
||||
case MODE_WAVE: return wave.isActive();
|
||||
default: return ampEnv.active();
|
||||
}
|
||||
}
|
||||
|
||||
bool SynthVoice::isGated() const {
|
||||
switch (mode) {
|
||||
case MODE_PAD: return pad.isGated();
|
||||
case MODE_WAVE: return wave.isGated();
|
||||
default: return ampEnv.gateOpen();
|
||||
}
|
||||
}
|
||||
|
||||
void SynthVoice::noteOn(int midi, int velocity, const SynthPatch &p) {
|
||||
const bool hasPad = p.kind == SynthPatch::Kind::PAD;
|
||||
const bool hasWave = p.kind == SynthPatch::Kind::WAVE;
|
||||
const int want = hasPad ? MODE_PAD : hasWave ? MODE_WAVE : MODE_SYNTH;
|
||||
if (want != mode) { // a slot switched kinds: the old engine stops
|
||||
ampEnv.kill(); filtEnv.kill(); pad.kill(); wave.kill();
|
||||
mode = want;
|
||||
}
|
||||
if (hasPad) {
|
||||
patch = p;
|
||||
pitch_ = midi;
|
||||
pad.noteOn(midi, (float)velocity / 127.f, p.pad);
|
||||
return;
|
||||
}
|
||||
if (hasWave) {
|
||||
patch = p;
|
||||
pitch_ = midi;
|
||||
wave.noteOn(midi, (float)velocity / 127.f, p.wave);
|
||||
return;
|
||||
}
|
||||
patch = p;
|
||||
pitch_ = midi;
|
||||
this->velocity = kt::coerceIn((float)velocity / 127.f, 0.f, 1.f);
|
||||
baseFreq = midiToHz(midi);
|
||||
keyNorm = kt::coerceIn((float)(midi - 60) / 60.f, -1.f, 1.f);
|
||||
ampEnv.gateOn(patch.aA, patch.aD, patch.aS, patch.aR);
|
||||
filtEnv.gateOn(patch.fA, patch.fD, patch.fS, patch.fR);
|
||||
lfoInc = (double)patch.lfoHz / sampleRate;
|
||||
lfoPhase = 0.0;
|
||||
ctrl = 0;
|
||||
ampModPrimed = false;
|
||||
computeTuning();
|
||||
}
|
||||
|
||||
void SynthVoice::noteOff() {
|
||||
switch (mode) {
|
||||
case MODE_PAD: pad.noteOff(); break;
|
||||
case MODE_WAVE: wave.noteOff(); break;
|
||||
default: ampEnv.gateOff(); filtEnv.gateOff(); break;
|
||||
}
|
||||
}
|
||||
|
||||
void SynthVoice::kill() {
|
||||
ampEnv.kill(); filtEnv.kill();
|
||||
pad.kill();
|
||||
wave.kill();
|
||||
pitch_ = -1;
|
||||
ownerTrack = -1;
|
||||
icB = 0.f; icL = 0.f;
|
||||
}
|
||||
|
||||
void SynthVoice::setPatch(const SynthPatch &p) {
|
||||
if (p.id == patch.id) return;
|
||||
const bool hasPad = p.kind == SynthPatch::Kind::PAD;
|
||||
const bool hasWave = p.kind == SynthPatch::Kind::WAVE;
|
||||
if (mode != MODE_SYNTH || hasPad || hasWave) {
|
||||
// Only within the same kind; a slot switched kinds takes effect on the next note.
|
||||
if (mode == MODE_PAD && hasPad) { patch = p; pad.setPatch(p.pad); }
|
||||
if (mode == MODE_WAVE && hasWave) { patch = p; wave.setPatch(p.wave); }
|
||||
return;
|
||||
}
|
||||
patch = p;
|
||||
lfoInc = (double)p.lfoHz / sampleRate;
|
||||
computeTuning();
|
||||
}
|
||||
|
||||
void SynthVoice::computeTuning() {
|
||||
for (int i = 0; i < OSC_COUNT; ++i) {
|
||||
float semis = (float)patch.oscOct[i] * 12.f + (float)patch.oscSemi[i] + patch.oscFine[i] / 100.f;
|
||||
staticSemis[i] = semis;
|
||||
staticInc[i] = kt::coerceIn(baseFreq * fastPow2(semis / 12.0) / sampleRate, 0.0, 0.49);
|
||||
modInc[i] = staticInc[i];
|
||||
}
|
||||
pitchModActive = false;
|
||||
for (int n = 0; n < MOD_SLOTS; ++n)
|
||||
if (patch.modSrc[n] != 0 && patch.modDst[n] >= 0 && patch.modDst[n] <= 2) { pitchModActive = true; break; }
|
||||
}
|
||||
|
||||
float SynthVoice::render() {
|
||||
if (mode == MODE_PAD) return pad.render();
|
||||
if (mode == MODE_WAVE) return wave.render();
|
||||
if (!ampEnv.active()) return 0.f;
|
||||
const float dt = 1.f / (float)sampleRate;
|
||||
const float ampE = ampEnv.advance(dt);
|
||||
const float filtE = filtEnv.advance(dt);
|
||||
|
||||
// ---- LFO ----
|
||||
lfoPhase += lfoInc;
|
||||
if (lfoPhase >= 1.0) { lfoPhase -= 1.0; shValue = nextRand(); }
|
||||
const float lfo = lfoValue();
|
||||
|
||||
// ---- modulation matrix ----
|
||||
float mPitch = 0.f, mOsc2 = 0.f, mOsc3 = 0.f;
|
||||
float mCut = 0.f, mPwm = 0.f, mAmp = 0.f;
|
||||
for (int n = 0; n < MOD_SLOTS; ++n) {
|
||||
float s;
|
||||
switch (patch.modSrc[n]) {
|
||||
case 1: s = lfo; break;
|
||||
case 2: s = filtE; break;
|
||||
case 3: s = ampE; break;
|
||||
case 4: s = velocity; break;
|
||||
case 5: s = keyNorm; break;
|
||||
default: continue;
|
||||
}
|
||||
const float v = patch.modAmt[n] * s;
|
||||
switch (patch.modDst[n]) {
|
||||
case 0: mPitch += v; break;
|
||||
case 1: mOsc2 += v; break;
|
||||
case 2: mOsc3 += v; break;
|
||||
case 3: mCut += v; break;
|
||||
case 4: mPwm += v; break;
|
||||
case 5: mAmp += v; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- control-rate update ----
|
||||
if (ctrl <= 0) {
|
||||
ctrl = CTRL_INTERVAL;
|
||||
const float e = kt::coerceIn(patch.cutoff + patch.filterEnvAmt * filtE + patch.keytrack * keyNorm + mCut,
|
||||
0.f, 1.3f);
|
||||
const double hz = kt::coerceIn(20.0 * fastPow(1000.0, (double)e), 20.0, sampleRate * 0.45);
|
||||
const float g = (float)std::tan(PI * hz / sampleRate);
|
||||
const float k = kt::coerceIn(2.f - 1.96f * patch.resonance, 0.05f, 2.f);
|
||||
ca1 = 1.f / (1.f + g * (g + k)); ca2 = g * ca1; ca3 = g * ca2;
|
||||
if (pitchModActive) {
|
||||
for (int i = 0; i < OSC_COUNT; ++i) {
|
||||
float semis = staticSemis[i] + mPitch * 12.f;
|
||||
if (i == 1) semis += mOsc2 * 12.f;
|
||||
if (i == 2) semis += mOsc3 * 12.f;
|
||||
modInc[i] = kt::coerceIn(baseFreq * fastPow2(semis / 12.0) / sampleRate, 0.0, 0.49);
|
||||
}
|
||||
}
|
||||
}
|
||||
ctrl--;
|
||||
|
||||
// ---- oscillators + noise ----
|
||||
float mix = 0.f;
|
||||
for (int i = 0; i < OSC_COUNT; ++i) {
|
||||
const float level = patch.oscLevel[i];
|
||||
if (level <= 0.0001f) continue;
|
||||
const double inc = pitchModActive ? modInc[i] : staticInc[i];
|
||||
double ph = phase[i] + inc;
|
||||
if (ph >= 1.0) ph -= 1.0;
|
||||
phase[i] = ph;
|
||||
mix += waveform(patch.oscWave[i], ph, inc, mPwm) * level;
|
||||
}
|
||||
if (patch.noiseLevel > 0.0001f) mix += nextRand() * patch.noiseLevel;
|
||||
|
||||
// ---- filter ----
|
||||
const float filtered = svf(mix);
|
||||
|
||||
// ---- amplifier ----
|
||||
if (ampModPrimed) ampMod += (mAmp - ampMod) * ampModCoeff;
|
||||
else { ampMod = mAmp; ampModPrimed = true; }
|
||||
float a = ampE * velocity * (1.f + ampMod);
|
||||
if (a < 0.f) a = 0.f;
|
||||
const float y = filtered * a * OUT_GAIN;
|
||||
return std::isfinite(y) ? y : 0.f;
|
||||
}
|
||||
|
||||
float SynthVoice::waveform(int w, double t, double dt, float pwm) const {
|
||||
switch (w) {
|
||||
case 1: return (float)(2.0 * t - 1.0 - polyBlep(t, dt)); // Saw
|
||||
case 2: return pulse(t, dt, kt::coerceIn(0.5f + pwm * 0.45f, 0.02f, 0.98f)); // Square
|
||||
case 3: return pulse(t, dt, kt::coerceIn(0.25f + pwm * 0.45f, 0.02f, 0.98f)); // Pulse 25%
|
||||
case 4: return pulse(t, dt, kt::coerceIn(0.125f + pwm * 0.45f, 0.02f, 0.98f)); // Pulse 12.5%
|
||||
default: return (float)(1.0 - 4.0 * std::fabs(t - 0.5)); // Triangle
|
||||
}
|
||||
}
|
||||
|
||||
float SynthVoice::pulse(double t, double dt, float w) {
|
||||
double y = t < (double)w ? 1.0 : -1.0;
|
||||
y += polyBlep(t, dt);
|
||||
double t2 = t + (1.0 - (double)w);
|
||||
if (t2 >= 1.0) t2 -= 1.0;
|
||||
y -= polyBlep(t2, dt);
|
||||
return (float)y;
|
||||
}
|
||||
|
||||
double SynthVoice::polyBlep(double t, double dt) {
|
||||
if (dt <= 0.0) return 0.0;
|
||||
if (t < dt) { double x = t / dt; return x + x - x * x - 1.0; }
|
||||
if (t > 1.0 - dt) { double x = (t - 1.0) / dt; return x * x + x + x + 1.0; }
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float SynthVoice::lfoValue() const {
|
||||
double p = lfoPhase + (double)patch.lfoPhase;
|
||||
if (p >= 1.0) p -= 1.0;
|
||||
switch (patch.lfoWave) {
|
||||
case 1: return (float)(2.0 * p - 1.0); // Saw
|
||||
case 2: return p < 0.5 ? 1.f : -1.f; // Square
|
||||
case 3: return (float)std::sin(2.0 * PI * p); // Sine
|
||||
case 4: return shValue; // Sample & Hold
|
||||
default: return (float)(1.0 - 4.0 * std::fabs(p - 0.5)); // Triangle
|
||||
}
|
||||
}
|
||||
|
||||
float SynthVoice::svf(float input) {
|
||||
const float v3 = input - icL;
|
||||
const float v1 = ca1 * icB + ca2 * v3;
|
||||
const float v2 = icL + ca2 * icB + ca3 * v3;
|
||||
icB = 2.f * v1 - icB;
|
||||
icL = 2.f * v2 - icL;
|
||||
if (!std::isfinite(v2)) { icB = 0.f; icL = 0.f; return 0.f; }
|
||||
return v2;
|
||||
}
|
||||
|
||||
float SynthVoice::nextRand() {
|
||||
uint32_t s = rng;
|
||||
s ^= s << 13; s ^= s >> 17; s ^= s << 5;
|
||||
rng = s;
|
||||
return (float)(int32_t)s * 4.656613e-10f; // -1..1
|
||||
}
|
||||
|
||||
} // namespace sizzle::synth
|
||||
@@ -0,0 +1,107 @@
|
||||
// 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
|
||||
Reference in New Issue
Block a user