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