Files
sizzletracker-android/core/src/fx/MixTightener.cpp
T
ReactorcoremeltdownandClaude Opus 5.5 2bc5cb1cbb 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>
2026-11-16 14:49:46 +01:00

191 lines
8.4 KiB
C++

// 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