Files
sizzletracker-android/core/src/fx/TapeEngine.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

103 lines
3.5 KiB
C++

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