iOS 0.1.0: the eight audio effects as AUv3 plugins on a C++ core

C++ ports of the Kotlin effects, checked against audio the Kotlin code renders
(25 scenarios, IosGoldenExport); an Objective-C++ AUv3 wrapper checked on the
Mac against the core (parameters, presets, saved state); a container app that
checks each plugin on the device. Built as a TrollStore .ipa.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-14 20:47:06 +01:00
co-authored by Claude Opus 5.5
parent c9ad186de1
commit dcf85ba915
44 changed files with 3542 additions and 127 deletions
+65
View File
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#include "sizzle/Params.hpp"
#include <cctype>
#include <cstdio>
namespace sizzle {
void Params::fill(const DeviceSpec &spec) {
n_ = 0;
for (int i = 0; i < spec.paramCount; ++i) {
const ParamSpec &p = spec.params[i];
if (p.isEnum()) {
int idx = (int)p.def;
int n = p.choiceCount();
if (idx < 0) idx = 0;
if (idx > n - 1) idx = n - 1;
set(p.key, p.choices[idx]);
} else {
set(p.key, p.def);
}
}
}
int Params::find(const char *key) const {
for (int i = 0; i < n_; ++i) if (std::strcmp(keys_[i], key) == 0) return i;
return -1;
}
void Params::set(const char *key, const char *value) {
int i = find(key);
if (i < 0) {
if (n_ >= kMax) return;
i = n_++;
std::snprintf(keys_[i], kKeyLen, "%s", key);
}
std::snprintf(vals_[i], kValLen, "%s", value);
}
void Params::set(const char *key, float value) {
// Shortest text that reads back as the same float (Kotlin stores toString()).
char buf[kValLen];
std::snprintf(buf, sizeof buf, "%.9g", (double)value);
set(key, buf);
}
float Params::f(const char *key, float fallback) const {
int i = find(key);
if (i < 0) return fallback;
const char *s = vals_[i];
while (std::isspace((unsigned char)*s)) ++s;
if (!*s) return fallback;
char *end = nullptr;
float v = std::strtof(s, &end);
while (end && std::isspace((unsigned char)*end)) ++end;
return (end && *end == '\0') ? v : fallback;
}
const char *Params::s(const char *key, const char *fallback) const {
int i = find(key);
return i < 0 ? fallback : vals_[i];
}
} // namespace sizzle
+14
View File
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#include <cstring>
#include "sizzle/DeviceSpecs.gen.hpp"
namespace sizzle {
const DeviceSpec *findDevice(const char *type) {
for (const auto &d : gen::devices) if (std::strcmp(d.type, type) == 0) return &d;
return nullptr;
}
} // namespace sizzle
+401
View File
@@ -0,0 +1,401 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of AmbienceReverb.kt: mono 8-line FDN reverb (OTODESK "Ambience" style) with
// predelay, early reflections, input diffusers, nested allpasses, xorshift-noise
// LFOs, ducking and wet saturation. Line-for-line; Float stays float.
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <vector>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
// The JVM never fuses a*b+c; keep the compiler from contracting into FMA.
#if defined(__clang__)
#pragma clang fp contract(off)
#endif
namespace sizzle {
namespace {
constexpr int N = 8;
const float FWHT_NORM = 1.f / kt::sqrtf_((float)N);
constexpr float LATE_SCALE = 2.f / N;
constexpr int MAX_ER = 12;
constexpr int MAX_ER_ACTIVE = 6;
constexpr float PHI = 1.6180339887f;
constexpr float TWO_PI = 6.28318530718f;
const float ALGO_HF[] = {0.00f, 0.26f, 0.26f, 0.22f, 0.36f, 0.14f, 0.30f};
const float ALGO_OFFSET_DB[] = {0.8f, 0.9f, 0.5f, 0.5f, 1.5f, 0.6f, 0.6f};
const float ALGO_SAT_MUL[] = {0.90f, 0.90f, 0.93f, 0.93f, 1.00f, 1.05f, 1.02f};
const float SIGN_FLIP[N] = {1.f, -1.f, 1.f, -1.f, -1.f, 1.f, -1.f, 1.f};
const char *const ALGORITHMS[] = {"Room 1", "Room 2", "Hall 1", "Hall 2", "Plate", "Spring", "Goldfoil"};
const float ER_ROOM1[] = {
5.2f, 0.65f, 8.7f, 0.58f, 12.4f, 0.52f, 15.8f, 0.46f, 19.3f, 0.42f, 24.1f, 0.36f,
28.6f, 0.32f, 33.5f, 0.28f, 38.9f, 0.24f, 45.2f, 0.20f, 52.8f, 0.16f, 62.4f, 0.13f,
};
const float ER_ROOM2[] = {
7.5f, 0.62f, 12.3f, 0.55f, 17.1f, 0.49f, 22.8f, 0.43f, 28.5f, 0.38f, 34.2f, 0.33f,
41.6f, 0.28f, 49.3f, 0.24f, 57.8f, 0.20f, 67.5f, 0.17f, 78.2f, 0.14f, 90.6f, 0.11f,
};
const float ER_HALL1[] = {
12.0f, 0.58f, 18.5f, 0.52f, 25.7f, 0.47f, 33.4f, 0.42f, 42.1f, 0.38f, 51.6f, 0.33f,
62.3f, 0.29f, 73.9f, 0.25f, 86.5f, 0.21f, 99.8f, 0.18f, 113.4f, 0.15f, 128.7f, 0.12f,
};
const float ER_HALL2[] = {
16.5f, 0.55f, 24.8f, 0.50f, 33.7f, 0.45f, 43.5f, 0.40f, 54.2f, 0.36f, 65.8f, 0.32f,
78.4f, 0.28f, 92.1f, 0.24f, 107.3f, 0.21f, 123.6f, 0.18f, 141.2f, 0.15f, 160.5f, 0.12f,
};
const float *const ER_PATTERNS[] = {ER_ROOM1, ER_ROOM2, ER_HALL1, ER_HALL2, nullptr, nullptr, nullptr};
const int ER_PATTERN_SIZE[] = {24, 24, 24, 24, 0, 0, 0};
inline float lnf_(float x) { return (float)std::log((double)x); }
inline float floorf_(float x) { return (float)std::floor((double)x); }
inline float pow10(float x) { return kt::expf_(x * 2.302585093f); }
inline float db2lin(float db) { return pow10(db / 20.f); }
/** In-place size-N Fast Walsh-Hadamard transform, normalised (÷√N) → orthonormal. */
void fwht(float *v) {
int h = 1;
while (h < N) {
int i = 0;
while (i < N) {
int j = i;
while (j < i + h) {
float a = v[j]; float b = v[j + h];
v[j] = a + b; v[j + h] = a - b;
j++;
}
i += h * 2;
}
h *= 2;
}
for (int k = 0; k < N; ++k) v[k] *= FWHT_NORM;
}
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
int i = 3;
while (i * i <= n) { if (n % i == 0) return false; i += 2; }
return true;
}
bool usedNone(const int *used, int count, int v) {
for (int k = 0; k < count; ++k) if (used[k] == v) return false;
return true;
}
int nearestUniquePrime(int target, const int *used, int count) {
int t = std::max(target, 2);
int offset = 0;
while (offset < 100000) {
int hi = t + offset;
if (isPrime(hi) && usedNone(used, count, hi)) return hi;
int lo = t - offset;
if (offset > 0 && lo >= 2 && isPrime(lo) && usedNone(used, count, lo)) return lo;
offset++;
}
return t;
}
float softClip(float x) {
if (x > 1.f) return 1.f - 1.f / (x + 1.f);
if (x < -1.f) return -1.f + 1.f / (-x + 1.f);
return x;
}
/** A power-of-two circular buffer with fractional (linear-interpolated) reads. */
class DelayLine {
std::vector<float> buf;
int mask;
int w = 0;
public:
explicit DelayLine(int size) {
int p = 1;
while (p < size) p *= 2;
buf.assign((size_t)p, 0.f);
mask = p - 1;
}
float maxDelay() const { return (float)(mask - 2); }
void write(float x) { buf[(size_t)w] = x; w = (w + 1) & mask; }
float read(float delay) const {
float d = kt::coerceIn(delay, 1.f, (float)mask - 1.f);
float rp = (float)w - d;
float i0f = floorf_(rp);
float frac = rp - i0f;
int i0 = kt::toInt(i0f) & mask;
int i1 = (i0 + 1) & mask;
return buf[(size_t)i0] * (1.f - frac) + buf[(size_t)i1] * frac;
}
};
class AmbienceReverb final : public Effect {
float fs;
DelayLine preDelay;
DelayLine erLine;
std::vector<DelayLine> diffusers;
std::vector<DelayLine> fdn;
std::vector<DelayLine> apf;
float fbVec[N] = {};
float fbScratch[N] = {};
float nextFb[N] = {};
float baseDelay[N] = {};
float decayGain[N] = {};
float hfState[N] = {};
float lfState[N] = {};
float freqMod[N];
float apfBase[N];
float diffuserSmp[4];
int32_t lfoState[N];
float lfoSmooth[N] = {};
float lfoCoeff[N] = {};
float lfoRateMul[N];
float hfCoeff = 1.f;
float lfCoeff = 0.f;
float lfAmt = 0.f;
float apfGain = 0.5f;
float diffusionSens = 1.f;
bool bypassER = false;
float makeup = 1.f;
float satMul = 1.f;
float modDepthScale = 1.f;
int erCount = 0;
float erDelaySmp[MAX_ER] = {};
float erGain[MAX_ER] = {};
float wetLin = 0.63f;
float dryLin = 1.f;
float preDelaySmp = 480.f;
float modDepth = 0.f;
float diffGain = 0.63f;
float effApfGain = 0.4f;
float satAmt = 0.f;
float erLevel = 0.6f;
float duckAmtDb = 0.f;
float duckThreshDb = -20.f;
float duckThreshLin = 0.1f;
float duckAtt = 0.01f;
float duckRel = 0.001f;
float duckEnv = 0.f;
float dc1 = 0.f;
float dcY = 0.f;
int lastAlgo = -1;
float lastSize = NAN;
float lastDecay = NAN;
float lastHf = NAN;
float lastLf = NAN;
float lastDiffusion = NAN;
void recompute(int algo, float size, float decay, float hf, float lf, float diffusion) {
(void)diffusion;
switch (algo) {
case 0: case 1: apfGain = 0.3f; diffusionSens = 1.f; bypassER = false; break;
case 2: case 3: apfGain = 0.618f; diffusionSens = 1.f; bypassER = false; break;
case 4: apfGain = 0.7f; diffusionSens = 0.7f; bypassER = true; break;
case 5: apfGain = 0.5f; diffusionSens = 0.5f; bypassER = true; break;
default: apfGain = 0.75f; diffusionSens = 0.8f; bypassER = true; break;
}
float sizeCoeff = kt::coerceIn(size + 0.5f, 0.5f, 2.f);
float minMs = 15.f + sizeCoeff * 7.5f;
float maxMs = 50.f + sizeCoeff * 75.f;
int minS = std::max(11, kt::toInt(minMs * 0.001f * fs));
int maxS = kt::coerceAtLeast(kt::toInt(maxMs * 0.001f * fs), minS + 1);
float logMin = lnf_((float)minS);
float logMax = lnf_((float)maxS);
int used[N] = {};
for (int i = 0; i < N; ++i) {
float t = (float)i / ((float)N - 1.f);
int target = kt::toInt(kt::expf_(logMin + t * (logMax - logMin)));
int p = nearestUniquePrime(target, used, i);
used[i] = p;
baseDelay[i] = kt::coerceAtMost((float)p, fdn[(size_t)i].maxDelay());
}
for (int i = 0; i < N; ++i) {
float tSec = baseDelay[i] / fs;
decayGain[i] = kt::coerceIn(pow10(-3.f * tSec / decay), 0.f, 0.9995f);
}
float totalHf = kt::coerceIn(hf + ALGO_HF[algo], 0.f, 0.95f);
float hfCut = 16000.f * (1.f - totalHf) + 700.f * totalHf;
hfCoeff = kt::coerceIn(1.f - kt::expf_(-TWO_PI * hfCut / fs), 0.02f, 1.f);
lfCoeff = kt::coerceIn(1.f - kt::expf_(-TWO_PI * 200.f / fs), 0.f, 1.f);
lfAmt = lf * 0.9f;
modDepthScale = 1.f + kt::coerceIn((decay - 1.f) * 0.5f, 0.f, 2.f);
float decayCompDb = 7.f * kt::log10f_(kt::coerceAtLeast(decay, 0.1f));
makeup = db2lin(16.f + decayCompDb + ALGO_OFFSET_DB[algo]);
satMul = ALGO_SAT_MUL[algo];
const float *pattern = ER_PATTERNS[algo];
erCount = bypassER ? 0 : std::min(ER_PATTERN_SIZE[algo] / 2, MAX_ER_ACTIVE);
float erSizeScale = 0.5f + size;
for (int t = 0; t < erCount; ++t) {
erDelaySmp[t] = kt::coerceIn(pattern[t * 2] * 0.001f * fs * erSizeScale, 1.f, erLine.maxDelay());
erGain[t] = pattern[t * 2 + 1];
}
}
float nextLfo(int i) {
uint32_t s = (uint32_t)lfoState[i];
s = s ^ (s << 13); s = s ^ (s >> 17); s = s ^ (s << 5);
lfoState[i] = (int32_t)s;
float n = (float)s * 2.3283064e-10f * 2.f - 1.f;
lfoSmooth[i] += (n - lfoSmooth[i]) * lfoCoeff[i];
return lfoSmooth[i];
}
public:
explicit AmbienceReverb(int sampleRate)
: fs((float)sampleRate),
preDelay(kt::toInt(fs * 0.55f)),
erLine(kt::toInt(fs * 0.6f)) {
diffusers.reserve(4);
for (int i = 0; i < 4; ++i) diffusers.emplace_back(kt::toInt(fs * 0.03f));
fdn.reserve(N);
apf.reserve(N);
for (int i = 0; i < N; ++i) fdn.emplace_back(kt::toInt(fs * 0.3f));
for (int i = 0; i < N; ++i) apf.emplace_back(kt::toInt(fs * 0.03f));
for (int it = 0; it < N; ++it) {
freqMod[it] = 0.5f + (1.f - (float)it / ((float)N - 1.f));
apfBase[it] = (2.3f + (float)it * 0.37f) * 0.001f * fs;
lfoState[it] = 12345 + it * 9876;
float a = (float)it * PHI;
lfoRateMul[it] = 0.8f + (a - floorf_(a)) * 0.4f;
}
for (int it = 0; it < 4; ++it) diffuserSmp[it] = (3.f + (float)it * 2.f) * 0.001f * fs;
}
void update(const Params &p, float) override {
const char *algoName = p.s("algo", "Room 1");
int algo = -1;
for (int k = 0; k < 7; ++k) if (std::strcmp(ALGORITHMS[k], algoName) == 0) { algo = k; break; }
algo = kt::coerceIn(algo, 0, 6);
float size = kt::coerceIn(p.f("roomsize", 1.f), 0.3f, 2.f);
float decay = kt::coerceIn(p.f("decay", 1.5f), 0.1f, 20.f);
float hf = kt::coerceIn(p.f("hfdamp", 0.f), 0.f, 1.f);
float lf = kt::coerceIn(p.f("lfabsorb", 0.f), 0.f, 1.f);
float diffusion = kt::coerceIn(p.f("diffusion", 0.7f), 0.f, 1.f);
wetLin = db2lin(p.f("wet", -4.f));
dryLin = db2lin(p.f("dry", 0.f));
preDelaySmp = kt::coerceIn(p.f("predelay", 10.f), 0.f, 500.f) * 0.001f * fs;
erLevel = kt::coerceIn(p.f("erlevel", 0.6f), 0.f, 1.f);
satAmt = kt::coerceIn(p.f("saturation", 0.f), 0.f, 1.f);
float modAmt = kt::coerceIn(p.f("modamt", 0.25f), 0.f, 1.f);
float modRate = kt::coerceIn(p.f("modrate", 0.5f), 0.05f, 2.f);
// Uses the modDepthScale from the previous recompute (as the Kotlin does).
modDepth = modAmt * modAmt * 0.001f * fs * modDepthScale;
for (int i = 0; i < N; ++i) {
lfoCoeff[i] = kt::coerceIn(1.f - kt::expf_(-TWO_PI * modRate * lfoRateMul[i] / fs), 0.0001f, 0.9999f);
}
duckAmtDb = kt::coerceIn(p.f("duckamt", 0.f), 0.f, 20.f);
duckThreshDb = p.f("duckthresh", -20.f);
duckThreshLin = db2lin(duckThreshDb);
duckAtt = 1.f - kt::expf_(-1.f / (fs * kt::coerceAtLeast(p.f("duckattack", 10.f), 0.1f) * 0.001f));
duckRel = 1.f - kt::expf_(-1.f / (fs * kt::coerceAtLeast(p.f("duckrelease", 200.f), 0.1f) * 0.001f));
float effDiff = diffusion * diffusionSens;
diffGain = 0.25f + effDiff * 0.55f;
effApfGain = apfGain * (0.6f + effDiff * 0.4f) * 0.78f;
if (algo != lastAlgo || size != lastSize || decay != lastDecay ||
hf != lastHf || lf != lastLf || diffusion != lastDiffusion) {
lastAlgo = algo; lastSize = size; lastDecay = decay;
lastHf = hf; lastLf = lf; lastDiffusion = diffusion;
recompute(algo, size, decay, hf, lf, diffusion);
float d2 = diffusion * diffusionSens;
diffGain = 0.25f + d2 * 0.55f;
effApfGain = apfGain * (0.6f + d2 * 0.4f) * 0.78f;
}
}
float process(float x) override {
preDelay.write(x);
float din = preDelaySmp > 0.5f ? preDelay.read(preDelaySmp) : x;
float peak = std::fabs(x);
duckEnv += (peak - duckEnv) * (peak > duckEnv ? duckAtt : duckRel);
float duckGain = 1.f;
if (duckAmtDb > 0.001f && duckEnv > duckThreshLin) {
float overDb = 20.f * kt::log10f_(std::max(duckEnv, 1e-6f)) - duckThreshDb;
duckGain = db2lin(-std::min(overDb, duckAmtDb));
}
float fdnIn = din;
for (int i = 0; i < 4; ++i) {
float d = diffusers[(size_t)i].read(diffuserSmp[i]);
float w = fdnIn + diffGain * d;
diffusers[(size_t)i].write(w);
fdnIn = d - diffGain * w;
}
float er = 0.f;
if (!bypassER) {
erLine.write(din);
for (int t = 0; t < erCount; ++t) er += erLine.read(erDelaySmp[t]) * erGain[t] * 0.5f;
fdnIn += er * 0.15f;
}
std::memcpy(fbScratch, fbVec, sizeof(fbScratch));
fwht(fbScratch);
for (int i = 0; i < N; ++i) fbScratch[i] *= SIGN_FLIP[i];
float inPerCh = fdnIn * 0.25f;
float out = 0.f;
for (int i = 0; i < N; ++i) {
float lfo = nextLfo(i);
DelayLine &line = fdn[(size_t)i];
float d = line.read(baseDelay[i] + lfo * modDepth * freqMod[i]);
d *= decayGain[i];
hfState[i] += hfCoeff * (d - hfState[i]); d = hfState[i];
lfState[i] += lfCoeff * (d - lfState[i]); d -= lfAmt * lfState[i];
DelayLine &ap = apf[(size_t)i];
float ad = ap.read(apfBase[i] + lfo * modDepth * 0.1f * freqMod[i]);
float aw = d + effApfGain * ad;
ap.write(aw);
float apfOut = ad - effApfGain * aw;
nextFb[i] = apfOut;
line.write(inPerCh + fbScratch[i]);
out += apfOut;
}
std::memcpy(fbVec, nextFb, sizeof(fbVec));
float late = out * LATE_SCALE * makeup;
float amt = kt::coerceIn(satAmt * satMul, 0.f, 1.f);
if (amt > 0.0001f) late += amt * (kt::tanhf_(late * 2.f) * 0.5f - late);
float wetRaw = (bypassER ? 0.f : er * erLevel) + late;
float dcOut = wetRaw - dc1 + 0.999f * dcY;
dc1 = wetRaw; dcY = dcOut;
float wet = softClip(dcOut * wetLin * duckGain);
return x * dryLin + wet;
}
};
} // namespace
std::unique_ptr<Effect> makeAmbienceReverb(int sampleRate) {
return std::make_unique<AmbienceReverb>(sampleRate);
}
} // namespace sizzle
+42
View File
@@ -0,0 +1,42 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of Bitcrusher (Effects.kt): bit-depth reduction + sample-rate reduction + drive.
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
class Bitcrusher final : public Effect {
float levels = 256.f;
int downsample = 1;
float drive = 1.f;
int counter = 0;
float held = 0.f;
public:
void update(const Params &p, float) override {
float bits = kt::coerceIn(p.f("bits", 8.f), 1.f, 16.f);
levels = kt::powf_(2.f, bits);
downsample = kt::coerceAtLeast(kt::toInt(p.f("downsample", 1.f)), 1);
drive = 1.f + p.f("drive", 0.2f) * 4.f;
}
float process(float x) override {
if (counter <= 0) {
counter = downsample;
float driven = kt::tanhf_(x * drive);
held = (float)kt::roundToInt(driven * levels) / levels;
}
counter--;
return held;
}
};
} // namespace
std::unique_ptr<Effect> makeBitcrusher(int) { return std::make_unique<Bitcrusher>(); }
} // namespace sizzle
+111
View File
@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of BiquadFilterEffect (Effects.kt): RBJ LP/HP/BP biquad whose cutoff an
// input-level-gated ADSR opens, plus soft-saturation "warmth".
#include <algorithm>
#include <cmath>
#include <cstring>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
constexpr int CTRL_INTERVAL = 32;
constexpr float GATE_HI = 0.010f;
constexpr float GATE_LO = 0.004f;
class Filter final : public Effect {
double b0 = 1, b1 = 0, b2 = 0, a1 = 0, a2 = 0, z1 = 0, z2 = 0;
int sampleRate;
float sr;
enum Type { LPF, HPF, BPF } type = LPF;
float baseCutoff = 0.7f;
double q = 1.0;
float warmth = 0.f, envAmount = 0.f;
enum Phase { IDLE, ATTACK, DECAY, SUSTAIN, RELEASE } phase = IDLE;
float env = 0.f, attackInc = 1.f, decayInc = 1.f, sustainLevel = 1.f;
float releaseSeconds = 0.2f, releaseInc = 1.f, levelEnv = 0.f, levelCoeff = 0.f;
bool gateOpen = false;
int ctrl = 0;
void computeCoefficients(Type t, double freq, double qq) {
double w0 = 2.0 * M_PI * freq / sampleRate;
double cosW = std::cos(w0);
double alpha = std::sin(w0) / (2.0 * qq);
double a0;
switch (t) {
case HPF:
b0 = (1 + cosW) / 2; b1 = -(1 + cosW); b2 = (1 + cosW) / 2;
a0 = 1 + alpha; a1 = -2 * cosW; a2 = 1 - alpha; break;
case BPF:
b0 = alpha; b1 = 0.0; b2 = -alpha;
a0 = 1 + alpha; a1 = -2 * cosW; a2 = 1 - alpha; break;
default:
b0 = (1 - cosW) / 2; b1 = 1 - cosW; b2 = (1 - cosW) / 2;
a0 = 1 + alpha; a1 = -2 * cosW; a2 = 1 - alpha; break;
}
b0 /= a0; b1 /= a0; b2 /= a0; a1 /= a0; a2 /= a0;
}
public:
explicit Filter(int rate) : sampleRate(rate), sr((float)rate) {}
void update(const Params &p, float) override {
const char *t = p.s("type", "LPF");
type = std::strcmp(t, "HPF") == 0 ? HPF : std::strcmp(t, "BPF") == 0 ? BPF : LPF;
baseCutoff = kt::coerceIn(p.f("cutoff", 0.7f), 0.f, 1.f);
q = (double)(0.5 + p.f("resonance", 0.2f) * 8.f);
warmth = kt::coerceIn(p.f("warmth", 0.f), 0.f, 1.f);
envAmount = kt::coerceIn(p.f("envamt", 0.f), 0.f, 1.f);
sustainLevel = kt::coerceIn(p.f("sustain", 1.f), 0.f, 1.f);
attackInc = 1.f / (std::max(p.f("attack", 0.005f), 0.0005f) * sr);
float decay = p.f("decay", 0.10f);
decayInc = decay <= 0.f ? 1.f : (1.f - sustainLevel) / (decay * sr);
releaseSeconds = p.f("release", 0.20f);
levelCoeff = 1.f - kt::expf_(-1.f / (sr * 0.005f));
ctrl = 0;
}
float process(float x) override {
levelEnv += (std::fabs(x) - levelEnv) * levelCoeff;
if (!gateOpen && levelEnv > GATE_HI) {
gateOpen = true; phase = ATTACK;
} else if (gateOpen && levelEnv < GATE_LO) {
gateOpen = false;
releaseInc = releaseSeconds <= 0.f ? 1.f : env / (releaseSeconds * sr);
if (releaseInc <= 0.f) releaseInc = 1.f;
phase = RELEASE;
}
switch (phase) {
case ATTACK: env += attackInc; if (env >= 1.f) { env = 1.f; phase = DECAY; } break;
case DECAY: env -= decayInc; if (env <= sustainLevel) { env = sustainLevel; phase = SUSTAIN; } break;
case RELEASE: env -= releaseInc; if (env <= 0.f) { env = 0.f; phase = IDLE; } break;
default: break;
}
if (ctrl <= 0) {
ctrl = CTRL_INTERVAL;
float eff = kt::coerceIn(baseCutoff + envAmount * env * (1.f - baseCutoff), 0.f, 1.f);
double freq = 20.0 * std::pow(900.0, (double)eff);
computeCoefficients(type, kt::coerceIn(freq, 20.0, sampleRate * 0.45), q);
}
ctrl--;
double input = (double)x;
double out = b0 * input + z1;
z1 = b1 * input - a1 * out + z2;
z2 = b2 * input - a2 * out;
float y = (float)out;
if (warmth > 0.0001f) y += warmth * (kt::tanhf_(y * 1.8f) / 1.8f - y);
return y;
}
};
} // namespace
std::unique_ptr<Effect> makeFilter(int sr) { return std::make_unique<Filter>(sr); }
} // namespace sizzle
+91
View File
@@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of GranularGlitch (Effects.kt), audio path only: a rolling 1 s buffer whose
// read head randomly jumps back to replay short (sometimes octave-shifted) grains,
// dry in between. The VizEffect scope/playhead is not ported (output-independent).
#include <cstdint>
#include <vector>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
constexpr float MAX_TRIGGER = 0.0009f;
constexpr float MIN_JUMP_S = 0.03f;
constexpr float MAX_JUMP_S = 0.5f;
constexpr float GRAIN_MIN_S = 0.02f;
constexpr float GRAIN_MAX_S = 0.15f;
class GranularGlitch final : public Effect {
int sampleRate;
std::vector<float> buf;
int size;
int writePos = 0;
double readPos = 0.0;
int grainLeft = 0;
double grainRate = 1.0;
float intensity = 0.f;
uint32_t rng = 0x2F6E1DB7u;
float nextUnit() {
uint32_t s = rng; s ^= s << 13; s ^= s >> 17; s ^= s << 5; rng = s;
return (float)s * 2.3283064e-10f; // 0 … 1
}
bool maybeStartGrain(int wp) {
if (nextUnit() > intensity * intensity * MAX_TRIGGER) return false;
float minJ = MIN_JUMP_S * (float)sampleRate;
float maxJ = (MIN_JUMP_S + intensity * (MAX_JUMP_S - MIN_JUMP_S)) * (float)sampleRate;
float jump = minJ + nextUnit() * (maxJ - minJ);
double rp = (double)wp - (double)jump;
while (rp < 0) rp += size;
readPos = rp;
float lenSec = GRAIN_MAX_S - intensity * (GRAIN_MAX_S - GRAIN_MIN_S);
grainLeft = kt::coerceAtLeast(kt::toInt(lenSec * (float)sampleRate * (0.5f + nextUnit())), 64);
if (nextUnit() < intensity * 0.3f) grainRate = 2.0;
else if (nextUnit() < intensity * 0.2f) grainRate = 0.5;
else grainRate = 1.0;
return true;
}
public:
explicit GranularGlitch(int rate) : sampleRate(rate), buf((size_t)rate, 0.f), size(rate) {}
void update(const Params &p, float) override {
intensity = kt::coerceIn(p.f("intensity", 0.3f), 0.f, 1.f);
}
float process(float x) override {
int wp = writePos;
buf[wp] = x;
writePos++; if (writePos >= size) writePos = 0;
float out;
if (grainLeft <= 0 && (intensity < 0.001f || !maybeStartGrain(wp))) {
readPos = (double)wp;
out = x;
} else {
int i0 = (int)readPos % size;
int i1 = (i0 + 1) % size;
float frac = (float)(readPos - (int)readPos);
out = buf[i0] + (buf[i1] - buf[i0]) * frac;
double rp = readPos + grainRate;
if (rp >= size) rp -= size;
if (rp < 0) rp += size;
double gap = wp - rp; if (gap < 0) gap += size;
if (gap < 2.0) { rp = wp - 2.0; if (rp < 0) rp += size; }
readPos = rp;
grainLeft--;
}
return out;
}
};
} // namespace
std::unique_ptr<Effect> makeGranularGlitch(int sr) { return std::make_unique<GranularGlitch>(sr); }
} // namespace sizzle
+67
View File
@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of GraphicEq (Effects.kt): ten peaking biquads at octave-spaced centres.
#include <cmath>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
struct PeakBiquad {
double b0 = 1, b1 = 0, b2 = 0, a1 = 0, a2 = 0, z1 = 0, z2 = 0;
void set(int sampleRate, float freq, float gainDb) {
if (freq >= sampleRate * 0.45f) return;
double a = std::pow(10.0, gainDb / 40.0);
double w0 = 2.0 * M_PI * freq / sampleRate;
double cosW = std::cos(w0);
double alpha = std::sin(w0) / (2.0 * 1.0);
double a0 = 1 + alpha / a;
b0 = (1 + alpha * a) / a0;
b1 = (-2 * cosW) / a0;
b2 = (1 - alpha * a) / a0;
a1 = (-2 * cosW) / a0;
a2 = (1 - alpha / a) / a0;
}
float process(float x) {
double input = (double)x;
double out = b0 * input + z1;
z1 = b1 * input - a1 * out + z2;
z2 = b2 * input - a2 * out;
return (float)out;
}
};
class GraphicEq final : public Effect {
static constexpr float centers[10] = {31.f, 63.f, 125.f, 250.f, 500.f, 1000.f, 2000.f, 4000.f, 8000.f, 16000.f};
static constexpr const char *keys[10] = {"band0", "band1", "band2", "band3", "band4",
"band5", "band6", "band7", "band8", "band9"};
PeakBiquad bands[10];
int sampleRate;
public:
explicit GraphicEq(int sr) : sampleRate(sr) {}
void update(const Params &p, float) override {
for (int i = 0; i < 10; ++i) {
float gainDb = kt::coerceIn(p.f(keys[i], 0.f), -1.f, 1.f) * 12.f;
bands[i].set(sampleRate, centers[i], gainDb);
}
}
float process(float x) override {
float y = x;
for (auto &b : bands) y = b.process(y);
return y;
}
};
} // namespace
std::unique_ptr<Effect> makeGraphicEq(int sr) { return std::make_unique<GraphicEq>(sr); }
} // namespace sizzle
+34
View File
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
// Kotlin/JVM math semantics, so ports read like the Kotlin and match its output.
// kotlin.math on Float goes through Java's double Math and rounds back to float.
#include <cmath>
namespace sizzle::kt {
inline float tanhf_(float x) { return (float)std::tanh((double)x); }
inline float expf_(float x) { return (float)std::exp((double)x); }
inline float sinf_(float x) { return (float)std::sin((double)x); }
inline float cosf_(float x) { return (float)std::cos((double)x); }
inline float powf_(float a, float b) { return (float)std::pow((double)a, (double)b); }
inline float sqrtf_(float x) { return (float)std::sqrt((double)x); }
inline float log10f_(float x) { return (float)std::log10((double)x); }
/** Float.roundToInt(): Math.round, i.e. floor(x + 0.5). */
inline int roundToInt(float x) { return (int)std::floor((double)x + 0.5); }
inline int roundToInt(double x) { return (int)std::floor(x + 0.5); }
/** Float.toInt(): truncation toward zero, NaN → 0, saturating. */
inline int toInt(float x) {
if (std::isnan(x)) return 0;
if (x >= 2147483647.f) return 2147483647;
if (x <= -2147483648.f) return (-2147483647 - 1);
return (int)x;
}
template <class T> inline T coerceIn(T v, T lo, T hi) { return v < lo ? lo : (v > hi ? hi : v); }
template <class T> inline T coerceAtLeast(T v, T lo) { return v < lo ? lo : v; }
template <class T> inline T coerceAtMost(T v, T hi) { return v > hi ? hi : v; }
} // namespace sizzle::kt
+190
View File
@@ -0,0 +1,190 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of MixTightener (Effects.kt): parametric EQ -> 3-band multiband compressor ->
// peak limiter, morphed by one Strength control; per-band values are hidden params
// set by presets. Includes the private Bq biquad (TDF-II, double state).
#include <cmath>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
class Bq {
double b0 = 1.0, b1 = 0.0, b2 = 0.0;
double a1 = 0.0, a2 = 0.0;
double z1 = 0.0, z2 = 0.0;
void setRaw(double nb0, double nb1, double nb2, double a0, double na1, double na2) {
b0 = nb0 / a0; b1 = nb1 / a0; b2 = nb2 / a0; a1 = na1 / a0; a2 = na2 / a0;
}
public:
void identity() { b0 = 1.0; b1 = 0.0; b2 = 0.0; a1 = 0.0; a2 = 0.0; }
void peak(int sr, float freq, float gainDb, float q) {
float f = kt::coerceIn(freq, 20.f, sr * 0.45f);
double a = std::pow(10.0, gainDb / 40.0);
double w0 = 2.0 * M_PI * f / sr; double cw = std::cos(w0);
double alpha = std::sin(w0) / (2.0 * kt::coerceAtLeast(q, 0.1f));
setRaw(1 + alpha * a, -2 * cw, 1 - alpha * a, 1 + alpha / a, -2 * cw, 1 - alpha / a);
}
void lowShelf(int sr, float freq, float gainDb) {
float f = kt::coerceIn(freq, 20.f, sr * 0.45f);
double a = std::pow(10.0, gainDb / 40.0); double w0 = 2.0 * M_PI * f / sr; double cw = std::cos(w0);
double alpha = std::sin(w0) / 2.0 * std::sqrt(2.0); double ta = 2.0 * std::sqrt(a) * alpha;
setRaw(
a * ((a + 1) - (a - 1) * cw + ta), 2 * a * ((a - 1) - (a + 1) * cw),
a * ((a + 1) - (a - 1) * cw - ta), (a + 1) + (a - 1) * cw + ta,
-2 * ((a - 1) + (a + 1) * cw), (a + 1) + (a - 1) * cw - ta);
}
void highShelf(int sr, float freq, float gainDb) {
float f = kt::coerceIn(freq, 20.f, sr * 0.45f);
double a = std::pow(10.0, gainDb / 40.0); double w0 = 2.0 * M_PI * f / sr; double cw = std::cos(w0);
double alpha = std::sin(w0) / 2.0 * std::sqrt(2.0); double ta = 2.0 * std::sqrt(a) * alpha;
setRaw(
a * ((a + 1) + (a - 1) * cw + ta), -2 * a * ((a - 1) + (a + 1) * cw),
a * ((a + 1) + (a - 1) * cw - ta), (a + 1) - (a - 1) * cw + ta,
2 * ((a - 1) - (a + 1) * cw), (a + 1) - (a - 1) * cw - ta);
}
void lowPass(int sr, float freq, float q) {
float f = kt::coerceIn(freq, 20.f, sr * 0.45f);
double w0 = 2.0 * M_PI * f / sr; double cw = std::cos(w0); double alpha = std::sin(w0) / (2.0 * q);
setRaw((1 - cw) / 2, 1 - cw, (1 - cw) / 2, 1 + alpha, -2 * cw, 1 - alpha);
}
void highPass(int sr, float freq, float q) {
float f = kt::coerceIn(freq, 20.f, sr * 0.45f);
double w0 = 2.0 * M_PI * f / sr; double cw = std::cos(w0); double alpha = std::sin(w0) / (2.0 * q);
setRaw((1 + cw) / 2, -(1 + cw), (1 + cw) / 2, 1 + alpha, -2 * cw, 1 - alpha);
}
void reset() { z1 = 0.0; z2 = 0.0; }
float process(float x) {
double inp = (double)x;
double out = b0 * inp + z1;
z1 = b1 * inp - a1 * out + z2;
z2 = b2 * inp - a2 * out;
if (!std::isfinite(out)) { z1 = 0.0; z2 = 0.0; out = 0.0; }
return (float)out;
}
};
constexpr int CTRL = 16; // samples between (expensive) gain recomputes
constexpr float GAIN_SMOOTH = 0.05f; // per-sample gain ramp toward the control-rate target
class MixTightener final : public Effect {
int sampleRate;
Bq eqLo, eqMid, eqHi;
bool eqActive = false;
Bq xLoLP, xLoHP, xHiLP, xHiHP;
bool compActive = false;
float env[3] = {0.f, 0.f, 0.f};
float gain[3] = {1.f, 1.f, 1.f};
float target[3] = {1.f, 1.f, 1.f};
float atkCoef[3] = {}, relCoef[3] = {};
float thrDb[3] = {}, ratio[3] = {}, makeDb[3] = {};
int ctrl = 0;
float limCeil = 1.f, limRelCoef = 0.f, limEnv = 0.f;
float outGain = 1.f;
float strength = 1.f;
float compScale = 1.f;
static float dbToLin(float db) { return (float)std::pow(10.0, db / 20.0); }
float coefFromMs(float ms) const {
float t = kt::coerceAtLeast(ms, 0.1f) * 0.001f * sampleRate;
return (float)(1.0 - std::exp(-1.0 / t));
}
float compBand(int i, float band) {
float r = std::fabs(band);
float e = env[i];
env[i] = r > e ? e + atkCoef[i] * (r - e) : e + relCoef[i] * (r - e);
gain[i] += GAIN_SMOOTH * (target[i] - gain[i]);
return band * gain[i];
}
public:
explicit MixTightener(int rate) : sampleRate(rate) {}
void update(const Params &p, float) override {
strength = kt::coerceIn(p.f("strength", 1.f), 0.f, 1.f);
float eqS = strength * kt::coerceIn(p.f("eqAmt", 1.f), 0.f, 1.f);
float compAmt = kt::coerceIn(p.f("compAmt", 1.f), 0.f, 1.f);
compScale = strength * compAmt;
float limS = strength * kt::coerceIn(p.f("limAmt", 1.f), 0.f, 1.f);
float loG = kt::coerceIn(p.f("eqLoGain", 0.f), -18.f, 18.f) * eqS;
float midG = kt::coerceIn(p.f("eqMidGain", 0.f), -18.f, 18.f) * eqS;
float hiG = kt::coerceIn(p.f("eqHiGain", 0.f), -18.f, 18.f) * eqS;
if (std::fabs(loG) > 0.1f) eqLo.lowShelf(sampleRate, p.f("eqLoFreq", 120.f), loG); else eqLo.identity();
if (std::fabs(midG) > 0.1f) eqMid.peak(sampleRate, p.f("eqMidFreq", 1000.f), midG,
kt::coerceIn(p.f("eqMidQ", 1.0f), 0.2f, 8.f)); else eqMid.identity();
if (std::fabs(hiG) > 0.1f) eqHi.highShelf(sampleRate, p.f("eqHiFreq", 8000.f), hiG); else eqHi.identity();
eqActive = std::fabs(loG) > 0.1f || std::fabs(midG) > 0.1f || std::fabs(hiG) > 0.1f;
float xl = kt::coerceIn(p.f("xLo", 200.f), 40.f, sampleRate * 0.4f);
float xh = kt::coerceIn(p.f("xHi", 2500.f), xl + 50.f, sampleRate * 0.45f);
xLoLP.lowPass(sampleRate, xl, 0.5f); xLoHP.highPass(sampleRate, xl, 0.5f);
xHiLP.lowPass(sampleRate, xh, 0.5f); xHiHP.highPass(sampleRate, xh, 0.5f);
thrDb[0] = p.f("cLoThr", -24.f); ratio[0] = kt::coerceIn(p.f("cLoRatio", 2.f), 1.f, 20.f);
makeDb[0] = p.f("cLoMk", 0.f); atkCoef[0] = coefFromMs(p.f("cLoAtk", 10.f)); relCoef[0] = coefFromMs(p.f("cLoRel", 120.f));
thrDb[1] = p.f("cMidThr", -22.f); ratio[1] = kt::coerceIn(p.f("cMidRatio", 2.f), 1.f, 20.f);
makeDb[1] = p.f("cMidMk", 0.f); atkCoef[1] = coefFromMs(p.f("cMidAtk", 15.f)); relCoef[1] = coefFromMs(p.f("cMidRel", 140.f));
thrDb[2] = p.f("cHiThr", -24.f); ratio[2] = kt::coerceIn(p.f("cHiRatio", 2.f), 1.f, 20.f);
makeDb[2] = p.f("cHiMk", 0.f); atkCoef[2] = coefFromMs(p.f("cHiAtk", 5.f)); relCoef[2] = coefFromMs(p.f("cHiRel", 90.f));
bool anyRatio = false, anyMake = false;
for (int i = 0; i < 3; i++) {
if (ratio[i] > 1.01f) anyRatio = true;
if (std::fabs(makeDb[i]) > 0.1f) anyMake = true;
}
compActive = compAmt > 0.001f && (anyRatio || anyMake);
limCeil = 1.f + (dbToLin(kt::coerceIn(p.f("limCeil", -0.5f), -24.f, 0.f)) - 1.f) * limS;
limRelCoef = coefFromMs(p.f("limRel", 100.f));
outGain = 1.f + (dbToLin(kt::coerceIn(p.f("outGain", 0.f), -12.f, 18.f)) - 1.f) * limS;
}
float process(float x) override {
if (strength < 0.001f) return x;
float s = x;
if (eqActive) { s = eqLo.process(s); s = eqMid.process(s); s = eqHi.process(s); }
if (compActive) {
float low = xLoLP.process(s);
float hp = xLoHP.process(s);
float mid = xHiLP.process(hp);
float high = xHiHP.process(hp);
if (ctrl <= 0) {
ctrl = CTRL;
for (int i = 0; i < 3; i++) {
float envDb = 20.f * kt::log10f_(env[i] + 1e-9f);
float over = envDb - thrDb[i];
float grDb = over > 0.f ? -over * (1.f - 1.f / ratio[i]) : 0.f;
target[i] = dbToLin(kt::coerceIn((grDb + makeDb[i]) * compScale, -40.f, 24.f));
}
}
ctrl--;
s = compBand(0, low) + compBand(1, mid) + compBand(2, high);
}
float r = std::fabs(s);
limEnv = r > limEnv ? r : limEnv + limRelCoef * (r - limEnv);
if (limEnv > limCeil && limEnv > 1e-6f) s *= limCeil / limEnv;
s *= outGain;
return s > 1.f ? 1.f : s < -1.f ? -1.f : !std::isfinite(s) ? 0.f : s;
}
};
} // namespace
std::unique_ptr<Effect> makeMixTightener(int sampleRate) { return std::make_unique<MixTightener>(sampleRate); }
} // namespace sizzle
+39
View File
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Effect::create. Each effect lives in src/fx/<Name>.cpp and defines its maker
// below. The defaults here are weak definitions returning nullptr; a port's own
// (strong) definition replaces its default at link time, so a build without some
// of those files still links (lets ports land one by one).
#include <cstring>
#include "sizzle/Effect.hpp"
#define SIZZLE_MAKER(fn) \
__attribute__((weak)) std::unique_ptr<Effect> fn(int) { return nullptr; }
namespace sizzle {
SIZZLE_MAKER(makeBitcrusher);
SIZZLE_MAKER(makeFilter);
SIZZLE_MAKER(makeGraphicEq);
SIZZLE_MAKER(makeTapeDelay);
SIZZLE_MAKER(makeMixTightener);
SIZZLE_MAKER(makeTapeEngine);
SIZZLE_MAKER(makeGranularGlitch);
SIZZLE_MAKER(makeAmbienceReverb);
std::unique_ptr<Effect> Effect::create(const char *type, int sr) {
struct Entry { const char *type; std::unique_ptr<Effect> (*make)(int); };
const Entry table[] = {
{"BITCRUSHER", makeBitcrusher}, {"FILTER", makeFilter},
{"EQ10", makeGraphicEq}, {"DELAY", makeTapeDelay},
{"MIX_TIGHTENER", makeMixTightener}, {"TAPE_ENGINE", makeTapeEngine},
{"GRANULAR", makeGranularGlitch}, {"AMBIENCE", makeAmbienceReverb},
};
for (const auto &e : table)
if (std::strcmp(type, e.type) == 0) return e.make(sr);
return nullptr;
}
} // namespace sizzle
+122
View File
@@ -0,0 +1,122 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of TapeDelay (Effects.kt): one shared delay line read by up to four
// tempo-synced tape heads (averaged), with saturation, a darkening one-pole LP
// in the feedback path and sine wow/flutter, all scaled by "tape".
#include <cmath>
#include <vector>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
// The JVM never fuses a*b+c; keep clang from contracting into FMAs.
#if defined(__clang__)
#pragma STDC FP_CONTRACT OFF
#endif
namespace sizzle {
namespace {
// DelayDivisions (model/Toolbox.kt).
constexpr int HEADS = 4;
constexpr float MAX_FEEDBACK = 0.9f;
constexpr double BEAT_FRACTIONS[] = {
4.0, // 1/1
3.0, // 1/2.
2.0, // 1/2
1.5, // 1/4.
4.0 / 3.0, // 1/2T
1.0, // 1/4
0.75, // 1/8.
2.0 / 3.0, // 1/4T
0.5, // 1/8
0.375, // 1/16.
1.0 / 3.0, // 1/8T
0.25, // 1/16
1.0 / 6.0, // 1/16T
};
constexpr int DIV_COUNT = (int)(sizeof(BEAT_FRACTIONS) / sizeof(BEAT_FRACTIONS[0]));
constexpr int DIV_DEFAULT = 5; // indexOf("1/4")
inline double beatFraction(int i) { return BEAT_FRACTIONS[kt::coerceIn(i, 0, DIV_COUNT - 1)]; }
constexpr double FLUTTER_HZ = 5.0;
const char *const HEAD_ON_KEYS[HEADS] = {"head0On", "head1On", "head2On", "head3On"};
const char *const HEAD_DIV_KEYS[HEADS] = {"head0Div", "head1Div", "head2Div", "head3Div"};
class TapeDelay final : public Effect {
int sampleRate;
std::vector<float> buffer; // up to 4 s of delay line
int bufferSize;
int writeIndex = 0;
int delaySamples[HEADS];
bool headOn[HEADS] = {};
float feedback = 0.4f;
float dryWet = 0.35f;
float satBlend = 0.f;
float lpAlpha = 1.f;
float lpState = 0.f;
double flutterInc = 0.0;
float flutterDepth = 0.f;
double flutterPhase = 0.0;
public:
explicit TapeDelay(int rate)
: sampleRate(rate), buffer((size_t)rate * 4, 0.f), bufferSize(rate * 4) {
for (int h = 0; h < HEADS; h++) delaySamples[h] = rate / 2;
}
void update(const Params &p, float tempoBpm) override {
double samplesPerBeat = (60.0 / (double)tempoBpm) * sampleRate;
for (int h = 0; h < HEADS; h++) {
headOn[h] = p.f(HEAD_ON_KEYS[h], h == 0 ? 1.f : 0.f) >= 0.5f;
int idx = kt::toInt(p.f(HEAD_DIV_KEYS[h], (float)DIV_DEFAULT));
// Double.toInt(): truncation (values here are small and positive).
double d = beatFraction(idx) * samplesPerBeat;
int ds = std::isnan(d) ? 0 : d >= 2147483647.0 ? 2147483647 : (int)d;
delaySamples[h] = kt::coerceIn(ds, 1, bufferSize - 2);
}
feedback = kt::coerceIn(p.f("feedback", 0.4f), 0.f, MAX_FEEDBACK);
dryWet = kt::coerceIn(p.f("drywet", 0.35f), 0.f, 1.f);
float tape = kt::coerceIn(p.f("tape", 0.3f), 0.f, 1.f);
satBlend = tape;
lpAlpha = 1.f - tape * 0.85f;
flutterInc = 2.0 * M_PI * FLUTTER_HZ / sampleRate;
flutterDepth = tape * (float)sampleRate * 0.0015f;
}
float process(float x) override {
flutterPhase += flutterInc;
if (flutterPhase > 2 * M_PI) flutterPhase -= 2 * M_PI;
float flutter = (float)std::sin(flutterPhase) * flutterDepth;
float wet = 0.f;
int active = 0;
for (int h = 0; h < HEADS; h++) {
if (!headOn[h]) continue;
active++;
float pos = (float)(writeIndex - delaySamples[h]) - flutter;
while (pos < 0.f) pos += (float)bufferSize;
int i0 = kt::toInt(pos) % bufferSize;
int i1 = (i0 + 1) % bufferSize;
float frac = pos - (float)kt::toInt(pos);
wet += buffer[i0] + (buffer[i1] - buffer[i0]) * frac;
}
if (active > 0) wet /= (float)active;
float fb = wet * feedback;
fb += satBlend * (kt::tanhf_(fb) - fb);
lpState += lpAlpha * (fb - lpState);
fb = lpState;
buffer[writeIndex] = x + fb;
writeIndex = (writeIndex + 1) % bufferSize;
return x * (1.f - dryWet) + wet * dryWet;
}
};
} // namespace
std::unique_ptr<Effect> makeTapeDelay(int sampleRate) { return std::make_unique<TapeDelay>(sampleRate); }
} // namespace sizzle
+102
View File
@@ -0,0 +1,102 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Port of TapeEngine (Effects.kt): a varispeed reel-to-reel. Input is written into
// a ring at rate 1 and read back at a gliding `speed`, with wow/flutter wobble and
// xorshift hiss.
#include <cmath>
#include <cstdint>
#include <vector>
#include "sizzle/Effect.hpp"
#include "KotlinMath.hpp"
namespace sizzle {
namespace {
constexpr int NOMINAL_DELAY = 128;
constexpr float MIN_SPEED = 0.25f;
constexpr float MAX_WOW = 0.03f;
constexpr float MAX_FLUT = 0.008f;
constexpr float MAX_HISS = 0.02f;
constexpr double FLUTTER_HZ = 11.0;
constexpr float SPEED_GLIDE = 0.00012f;
constexpr double MIN_GAP = 2.0;
/** TimeDivision.beatFraction by index, with fromIndex's clamping. */
double beatFraction(int i) {
static constexpr double kFrac[] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125, 0.75};
return kFrac[kt::coerceIn(i, 0, 6)];
}
class TapeEngine final : public Effect {
int sampleRate;
std::vector<float> buf;
int size;
int writePos = NOMINAL_DELAY;
double readPos = 0.0;
double targetSpeed = 1.0;
double curSpeed = 1.0;
double wowInc = 0.0, wowPhase = 0.0; float wowDepth = 0.f;
double flutInc, flutPhase = 0.0; float flutDepth = 0.f;
float hiss = 0.f;
uint32_t rng = 0x51ED2A19u;
float 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
}
public:
explicit TapeEngine(int rate)
: sampleRate(rate), buf((size_t)rate * 2, 0.f), size(rate * 2),
flutInc(FLUTTER_HZ / rate) {}
void update(const Params &p, float tempoBpm) override {
targetSpeed = (double)kt::coerceIn(p.f("speed", 1.f), MIN_SPEED, 1.f);
wowDepth = kt::coerceIn(p.f("wow", 0.15f), 0.f, 1.f) * MAX_WOW;
flutDepth = kt::coerceIn(p.f("flutter", 0.15f), 0.f, 1.f) * MAX_FLUT;
hiss = kt::coerceIn(p.f("hiss", 0.10f), 0.f, 1.f) * MAX_HISS;
double secPerBeat = 60.0 / (double)tempoBpm;
double frac = beatFraction(kt::toInt(p.f("division", 2.f)));
double wowHz = 1.0 / kt::coerceAtLeast(frac * secPerBeat, 1e-4);
wowInc = wowHz / sampleRate;
flutInc = FLUTTER_HZ / sampleRate;
}
float process(float x) override {
buf[writePos] = x;
writePos++; if (writePos >= size) writePos = 0;
curSpeed += (targetSpeed - curSpeed) * (double)SPEED_GLIDE; // tape inertia
wowPhase += wowInc; if (wowPhase >= 1.0) wowPhase -= 1.0;
flutPhase += flutInc; if (flutPhase >= 1.0) flutPhase -= 1.0;
double wow = std::sin(2.0 * M_PI * wowPhase) * (double)wowDepth;
double flut = std::sin(2.0 * M_PI * flutPhase) * (double)flutDepth;
double rate = curSpeed * (1.0 + wow + flut);
if (rate < 0.01) rate = 0.01;
int i0 = (int)readPos % size;
int i1 = (i0 + 1) % size;
float frac = (float)(readPos - (int)readPos);
float out = buf[i0] + (buf[i1] - buf[i0]) * frac;
double rp = readPos + rate;
if (rp >= size) rp -= size;
double gap = writePos - rp; if (gap < 0) gap += size;
double maxGap = size - 2.0;
if (gap < MIN_GAP) { rp = writePos - MIN_GAP; if (rp < 0) rp += size; }
else if (gap > maxGap) { rp = writePos - maxGap; if (rp < 0) rp += size; }
readPos = rp;
if (hiss > 0.f) out += nextRand() * hiss;
return out;
}
};
} // namespace
std::unique_ptr<Effect> makeTapeEngine(int sr) { return std::make_unique<TapeEngine>(sr); }
} // namespace sizzle