// 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 #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 makeGraphicEq(int sr) { return std::make_unique(sr); } } // namespace sizzle