- 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>
522 lines
20 KiB
C++
522 lines
20 KiB
C++
// SPDX-FileCopyrightText: 2026 Styves Goupil (FRASIL)
|
|
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown (Kotlin port for Sizzle, C++ port)
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
// https://mozilla.org/MPL/2.0/ (also in LICENSES/MPL-2.0.txt of this repository).
|
|
//
|
|
// Port of FrasilVoice.kt, line for line (FRASIL 3.3's drone voice). See the Kotlin
|
|
// for the commentary on the sound; comments here are about the port.
|
|
|
|
#include <cmath>
|
|
#include "frasil/FrasilVoice.hpp"
|
|
#include "KotlinMath.hpp"
|
|
|
|
// Kotlin rounds every float operation; no fused multiply-adds.
|
|
#if defined(__clang__)
|
|
#pragma clang fp contract(off)
|
|
#endif
|
|
|
|
namespace sizzle::frasil {
|
|
namespace {
|
|
|
|
using namespace defs;
|
|
|
|
constexpr float VOICE_G = 0.2f;
|
|
constexpr float TAIL_END = 0.01f;
|
|
constexpr float SIDE_GAIN = 0.70710678f;
|
|
constexpr float OUT_GAIN = 2.2f;
|
|
constexpr float TIDE_SMOOTH_S = 0.5f;
|
|
constexpr float PI = 3.14159265f;
|
|
constexpr float TWO_PI = 6.2831853f;
|
|
constexpr float SIDE[3] = {0.f, -1.f, 1.f};
|
|
constexpr float PWR[3] = {0.21f, 0.29f, 0.37f};
|
|
constexpr float LOG2K[8] = {0.f, 1.f, 1.5849625f, 2.f, 2.3219281f, 2.5849625f, 2.8073549f, 3.f};
|
|
constexpr float BEAT_SPREAD[8] = {0.55f, 1.25f, 0.8f, 1.45f, 0.65f, 1.1f, 0.95f, 1.35f};
|
|
|
|
/** BREATH's ratios (log2): ln(r) / ln(2) in double, to float. */
|
|
struct RatioL2 {
|
|
float v[3][3];
|
|
RatioL2() {
|
|
const double r[3][3] = {{1, 2, 3}, {1, 3, 5}, {1, 2.756f, 5.404f}};
|
|
for (int i = 0; i < 3; ++i)
|
|
for (int k = 0; k < 3; ++k) v[i][k] = (float)(std::log(r[i][k]) / std::log(2.0));
|
|
}
|
|
};
|
|
const RatioL2 RATIO_L2;
|
|
|
|
inline float tanf_(float x) { return (float)std::tan((double)x); }
|
|
inline float floorf_(float x) { return (float)std::floor((double)x); }
|
|
|
|
/** A blep with the step's inverse. */
|
|
inline float blep(float t0, float dt, float idt) {
|
|
if (t0 < dt) { float t = t0 * idt; return t + t - t * t - 1.f; }
|
|
if (t0 > 1.f - dt) { float t = (t0 - 1.f) * idt; return t * t + t + t + 1.f; }
|
|
return 0.f;
|
|
}
|
|
|
|
inline float flush(float x) { return (x > -1e-20f && x < 1e-20f) ? 0.f : x; }
|
|
|
|
} // namespace
|
|
|
|
FrasilVoice::FrasilVoice(int fs_, const FrasilClock &clock_, int32_t seed)
|
|
: clock(&clock_), invFs(1.f / (float)fs_), r44(44100.f / (float)fs_), nyq(0.45f * (float)fs_),
|
|
patch(FrasilPatch::defaults()), rng((uint32_t)seed),
|
|
kdc(brK(0.0014f)), glideK(brK(0.2f)),
|
|
tideK(1.f - kt::expf_(-(float)BLOCK / ((float)fs_ * TIDE_SMOOTH_S))) {}
|
|
|
|
void FrasilVoice::noteOn(int midi, float velocity, const FrasilPatch &p) {
|
|
bool fresh = note_ < 0;
|
|
note_ = midi;
|
|
gate = true;
|
|
vel = kt::coerceIn(velocity, 0.f, 1.f);
|
|
patch = p;
|
|
ageFrames = 0;
|
|
ending = false;
|
|
chunk = 0;
|
|
if (fresh) start();
|
|
}
|
|
|
|
void FrasilVoice::kill() {
|
|
note_ = -1; gate = false; env = 0.f; lvl = 0.f; pos = CH; ending = false;
|
|
}
|
|
|
|
float FrasilVoice::render() {
|
|
if (pos >= CH) {
|
|
if (note_ < 0) return 0.f;
|
|
if (ending) { note_ = -1; ending = false; return 0.f; }
|
|
renderChunk();
|
|
pos = 0;
|
|
}
|
|
return out[pos++];
|
|
}
|
|
|
|
// ------------------------------------------------------------------- start ---
|
|
void FrasilVoice::start() {
|
|
env = 0.f; lvl = 0.f; pos = CH; prime = true;
|
|
dcState = 0.f; thinState = 0.f; nlp = 0.f;
|
|
for (int k = 0; k < 3; ++k) {
|
|
cph[k] = urand();
|
|
cpw[k] = (float)k * 0.33f;
|
|
ouC[k] = randf() * 0.5f;
|
|
}
|
|
for (float &v : clp) v = 0.f;
|
|
for (float &v : cint) v = 0.f;
|
|
csvf = false;
|
|
for (int k = 0; k < NP; ++k) {
|
|
float ph = urand() * TWO_PI;
|
|
px[k] = kt::cosf_(ph); py[k] = kt::sinf_(ph);
|
|
ps[k] = 0.f; pc[k] = 1.f;
|
|
ouPa[k] = randf() * 0.5f; ouPf[k] = randf() * 0.5f;
|
|
pac[k] = 0.f; pda[k] = 0.f;
|
|
pbt[k] = urand();
|
|
}
|
|
for (int k = 0; k < NR; ++k) { r1[k] = 0.f; r2[k] = 0.f; ouR[k] = randf() * 0.5f; }
|
|
ouCut = randf() * 0.5f;
|
|
}
|
|
|
|
// ------------------------------------------------------------------- block ---
|
|
void FrasilVoice::block() {
|
|
const FrasilPatch &p = patch;
|
|
const float *b = p.hasB ? p.b : nullptr;
|
|
float m = b == nullptr ? 0.f : morphPosition(p);
|
|
float gk = prime ? 1.f : glideK;
|
|
for (int i = 0; i < N; ++i) {
|
|
float t = b == nullptr ? p.a[i] : p.a[i] + (b[i] - p.a[i]) * m;
|
|
cur[i] += (t - cur[i]) * gk;
|
|
}
|
|
type = (b != nullptr && m >= 0.5f) ? p.bType : p.aType;
|
|
float period = 300.f * kt::powf_(1.f / 30.f, cur[TIDE_RATE]);
|
|
double ph = clock->seconds / (double)period;
|
|
float tideT = kt::sinf_(TWO_PI * (float)(ph - std::floor(ph)));
|
|
tideV = prime ? tideT : tideV + (tideT - tideV) * tideK;
|
|
bool wasPrime = prime;
|
|
prime = false;
|
|
ageFrames += BLOCK;
|
|
|
|
drift();
|
|
voiceBlock(wasPrime);
|
|
|
|
ka = 1.f - kt::expf_(-CH * invFs * 3.f / (0.005f * kt::powf_(6000.f, cur[ATTACK])));
|
|
kr = 1.f - kt::expf_(-CH * invFs * 3.f / (0.05f * kt::powf_(1200.f, cur[RELEASE])));
|
|
float full = cur[BODY];
|
|
dk = 1.f + 5.f * cur[WARMTH] * full;
|
|
dmake = 1.f / kt::tanhf_(dk * 0.35f) * 0.35f;
|
|
driveOn = cur[WARMTH] * full > 0.001f;
|
|
thin = full < 0.995f;
|
|
thinA = 1.f - kt::expf_(-TWO_PI * 20.f * kt::powf_(20.f, 1.f - full) * invFs);
|
|
}
|
|
|
|
float FrasilVoice::morphPosition(const FrasilPatch &p) const {
|
|
if (p.morphTime <= 0.f) return p.morph;
|
|
float x = kt::coerceIn(((float)ageFrames * invFs) / p.morphTime, 0.f, 1.f);
|
|
return p.morph + (1.f - p.morph) * x * x * (3.f - 2.f * x);
|
|
}
|
|
|
|
float FrasilVoice::ou(float x, float th, float s) {
|
|
// Kotlin evaluates the three randf() left to right.
|
|
float a = randf();
|
|
float b = randf();
|
|
float c = randf();
|
|
return kt::coerceIn(x - th * x + s * (a + b + c), -1.5f, 1.5f);
|
|
}
|
|
|
|
void FrasilVoice::drift() {
|
|
float tau = 20.f * kt::powf_(0.025f, cur[MOTION_RATE]);
|
|
float th = BLOCK * invFs / tau;
|
|
float s = 0.6f * kt::sqrtf_(2.f * th);
|
|
float te = th * (1.f + 3.f * cur[EVOLVE]);
|
|
float se = 0.6f * kt::sqrtf_(2.f * te);
|
|
for (int k = 0; k < 3; ++k) ouC[k] = ou(ouC[k], th, s);
|
|
for (int k = 0; k < NP; ++k) { ouPa[k] = ou(ouPa[k], te, se); ouPf[k] = ou(ouPf[k], th, s); }
|
|
for (int k = 0; k < NR; ++k) ouR[k] = ou(ouR[k], th, s);
|
|
ouCut = ou(ouCut, th, s);
|
|
}
|
|
|
|
void FrasilVoice::voiceBlock(bool fresh) {
|
|
const float *c = cur;
|
|
float f = 440.f * kt::powf_(2.f, (float)(note_ - 69) / 12.f);
|
|
float m = c[MOTION];
|
|
float mw = m * (1.f + 2.f * m);
|
|
float bright = (c[TIMBRE] - 0.5f) + tideV * c[TIDE] * 0.5f;
|
|
float full = c[BODY];
|
|
|
|
// CHOIR
|
|
if (c[CHOIR] > 0.001f) {
|
|
float dc = c[DETUNE] * c[DETUNE] * 40.f * (0.2f + 0.8f * full);
|
|
float sh = c[SHAPE];
|
|
float a = sh < 0.5f ? 2.f * sh : 1.f;
|
|
float w = sh < 0.5f ? 0.5f : 0.5f - 0.8f * (sh - 0.5f);
|
|
float sw = sh > 0.5f ? 0.03f + 0.18f * (sh - 0.5f) : 0.03f * a;
|
|
for (int j = 0; j < 3; ++j) {
|
|
float cents = SIDE[j] * dc + mw * 7.f * ouC[j];
|
|
cdt[j] = kt::coerceIn(f * (j == 0 ? c[INTERVAL] : 1.f) * kt::powf_(2.f, cents / 1200.f) * invFs, 1e-6f, 0.45f);
|
|
cid[j] = 1.f / cdt[j];
|
|
cpw[j] += PWR[j] * BLOCK * invFs;
|
|
cpw[j] -= floorf_(cpw[j]);
|
|
cw[j] = kt::coerceIn(w + sw * kt::sinf_(TWO_PI * cpw[j]) + 0.1f * mw * ouC[j] * a, 0.04f, 0.96f);
|
|
}
|
|
csh = a;
|
|
float ce = kt::coerceIn(c[CUTOFF] + bright * 0.6f + mw * 0.2f * ouCut, 0.f, 1.f);
|
|
float hc = 1.f + 39.f * ce * ce;
|
|
float fc = kt::coerceIn(f * hc, 20.f, std::fmin(16000.f, nyq));
|
|
ca = 1.f - kt::expf_(-TWO_PI * fc * invFs);
|
|
float pv = (1.f + a * a) / 3.f - 2.f * a * (1.f / 3.f - 2.f * w * (1.f - w));
|
|
cgain = kt::sqrtf_((1.f / 3.f) / (pv > 0.05f ? pv : 0.05f));
|
|
if (c[CH_RESO] > 0.002f || type != 0) {
|
|
float g = tanf_(PI * fc * invFs);
|
|
float q = 0.5f * kt::powf_(40.f, c[CH_RESO]);
|
|
float k = 1.f / q;
|
|
sa1 = 1.f / (1.f + g * (g + k)); sa2 = g * sa1; sa3 = g * sa2;
|
|
switch (type) {
|
|
case 1: sc0 = 0.f; sc1 = k * (1.4f + 0.12f * kt::sqrtf_(hc)) * kt::sqrtf_(q); sc2 = 0.f; break;
|
|
case 2: {
|
|
float mk = kt::coerceIn(kt::sqrtf_(0.75f * hc), 1.f, 4.f);
|
|
sc0 = mk; sc1 = -k * mk; sc2 = -mk;
|
|
break;
|
|
}
|
|
default: sc0 = 0.f; sc1 = 0.f; sc2 = 1.f; break;
|
|
}
|
|
cgain *= 1.f / kt::sqrtf_(1.f + 0.22f * (q - 0.5f));
|
|
}
|
|
if (fresh) { sp0 = sc0; sp1 = sc1; sp2 = sc2; }
|
|
}
|
|
|
|
// PARTIALS
|
|
if (c[PARTIALS] > 0.001f) {
|
|
float pe = kt::coerceIn(c[TILT] - bright * 0.7f, 0.f, 1.f);
|
|
float pw = 0.3f + 2.7f * pe;
|
|
float sx = 1.f + 0.12f * c[STRETCH];
|
|
float fnp = 2.f + 6.f * full;
|
|
int nmax = kt::toInt(fnp);
|
|
float top = fnp - (float)nmax;
|
|
if (top > 0.001f) nmax++; else top = 1.f;
|
|
if (nmax > NP) nmax = NP;
|
|
float sum2 = 0.f;
|
|
int n = 0;
|
|
for (int j = 0; j < nmax; ++j) {
|
|
float fk = f * kt::powf_(2.f, sx * LOG2K[j] + mw * 5.f * ouPf[j] / 1200.f);
|
|
if (fk > std::fmin(15000.f, nyq)) break;
|
|
float w = TWO_PI * fk * invFs;
|
|
float cw0 = kt::cosf_(w); float sn = kt::sinf_(w);
|
|
float q = kt::sqrtf_(2.f * (1.f + cw0));
|
|
float x = px[j];
|
|
float y = (py[j] - x * ps[j]) * pc[j];
|
|
float gn = 1.5f - 0.5f * (x * x + y * y);
|
|
float shh = sn / q;
|
|
float chh = (1.f + cw0) / q;
|
|
x *= gn; y *= gn;
|
|
ps[j] = shh;
|
|
pc[j] = q / (1.f + cw0);
|
|
px[j] = x;
|
|
py[j] = y * chh + x * shh;
|
|
float a = kt::powf_(2.f, -pw * LOG2K[j] + 1.6f * (m + 2.5f * c[EVOLVE]) * ouPa[j]);
|
|
if ((j & 1) == 1) a *= 1.f - c[ODD];
|
|
float pk = c[PEAK];
|
|
if (pk > 0.001f) {
|
|
float d = (sx * LOG2K[j] - c[FORMANT]) * (1.5f + 4.f * pk);
|
|
float fl = 1.f - 0.97f * pk;
|
|
a *= fl + (1.f + 7.f * pk - fl) / (1.f + d * d);
|
|
}
|
|
if (j + 1 == nmax) a *= top;
|
|
pa[j] = a;
|
|
sum2 += a * a;
|
|
n++;
|
|
}
|
|
np = n;
|
|
float norm = 0.6f / kt::sqrtf_(sum2 + 1e-9f);
|
|
for (int j = 0; j < n; ++j) pa[j] *= norm;
|
|
if (c[BEAT] > 0.001f) {
|
|
float bt = c[BEAT];
|
|
float dep = std::fmin(1.f, bt * 1.4f);
|
|
float hz = 0.15f + 5.f * bt * bt;
|
|
for (int j = 0; j < n; ++j) {
|
|
pbt[j] += hz * BEAT_SPREAD[j] * BLOCK * invFs;
|
|
pbt[j] -= floorf_(pbt[j]);
|
|
pa[j] *= (1.f - dep * (0.5f + 0.5f * kt::sinf_(TWO_PI * pbt[j]))) / (1.f - 0.5f * dep);
|
|
}
|
|
}
|
|
for (int j = 0; j < n; ++j) pda[j] = (pa[j] - pac[j]) / BLOCK;
|
|
for (int j = n; j < NP; ++j) { pac[j] = 0.f; pda[j] = 0.f; }
|
|
}
|
|
|
|
// BREATH
|
|
if (c[BREATH] > 0.001f) {
|
|
float col = kt::coerceIn(c[COLOR], 0.f, 1.f) * 2.f;
|
|
bool lo = col < 1.f;
|
|
const float *ra = RATIO_L2.v[lo ? 0 : 1];
|
|
const float *rb = RATIO_L2.v[lo ? 1 : 2];
|
|
if (!lo) col -= 1.f;
|
|
float q = 2.f * kt::powf_(300.f, kt::coerceIn(c[RESONANCE] + mw * 0.1f * ouR[0], 0.f, 1.f));
|
|
float sb = kt::coerceIn(0.55f + bright * 0.8f, 0.05f, 1.f);
|
|
for (int j = 0; j < NR; ++j) {
|
|
float l2 = ra[j] + (rb[j] - ra[j]) * col + mw * 12.f * ouR[j] / 1200.f;
|
|
float fr = kt::coerceIn(f * c[BR_PITCH] * kt::powf_(2.f, l2), 20.f, std::fmin(16000.f, nyq));
|
|
double r = (double)kt::expf_(-PI * fr / q * invFs);
|
|
double b1 = 2.0 * r * (double)kt::cosf_(TWO_PI * fr * invFs);
|
|
double b2 = r * r;
|
|
double ng = (1.0 + b2) / ((1.0 - b2) * ((1.0 + b2) * (1.0 + b2) - b1 * b1));
|
|
rb1[j] = (float)b1; rb2[j] = (float)b2;
|
|
rg[j] = (float)(0.4 * std::sqrt(3.0 / ng)) * (j == 0 ? 1.f : sb * (j == 2 ? sb : 1.f));
|
|
}
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------- chunk ---
|
|
void FrasilVoice::renderChunk() {
|
|
if (chunk == 0) block();
|
|
chunk = (chunk + 1) % (BLOCK / CH);
|
|
const float *c = cur;
|
|
env += ((gate ? 1.f : 0.f) - env) * (gate ? ka : kr);
|
|
float level = (1.f - c[VELOCITY] + c[VELOCITY] * vel) * VOICE_G;
|
|
float g1 = (!gate && env < TAIL_END) ? 0.f : level * env * env;
|
|
float g = lvl;
|
|
lvl = g1;
|
|
float dg = (g1 - g) / CH;
|
|
for (int i = 0; i < CH; ++i) { bc[i] = 0.f; ba[i] = 0.f; bb[i] = 0.f; }
|
|
float full = c[BODY];
|
|
if (c[CHOIR] > 0.001f) choirChunk(0.35f * c[CHOIR] * (0.3f + 0.7f * full));
|
|
if (c[PARTIALS] > 0.001f) partialsChunk(c[PARTIALS] * (0.7f + 0.3f * full));
|
|
if (c[BREATH] > 0.001f) { breathNoise(); souffleChunk(c[BREATH] * full * full); }
|
|
|
|
float z = bc[CH - 1] + ba[CH - 1] + bb[CH - 1] + bc[CH / 2] + ba[CH / 2] + bb[CH / 2];
|
|
if (!(z > -1e4f && z < 1e4f)) {
|
|
for (int i = 0; i < CH; ++i) { bc[i] = 0.f; ba[i] = 0.f; bb[i] = 0.f; }
|
|
start();
|
|
prime = false;
|
|
}
|
|
|
|
float dcs = dcState;
|
|
float th = thinState;
|
|
for (int i = 0; i < CH; ++i) {
|
|
g += dg;
|
|
float x = (bc[i] + SIDE_GAIN * (ba[i] + bb[i])) * g;
|
|
dcs += kdc * (x - dcs);
|
|
x -= dcs;
|
|
if (driveOn) x = kt::tanhf_(x * dk) * dmake;
|
|
if (thin) { th += thinA * (x - th); x -= th; }
|
|
out[i] = x * OUT_GAIN;
|
|
}
|
|
dcState = flush(dcs);
|
|
thinState = thin ? flush(th) : 0.f;
|
|
if (g1 == 0.f && !gate && env < TAIL_END) ending = true;
|
|
}
|
|
|
|
void FrasilVoice::choirChunk(float g0) {
|
|
bool svf = cur[CH_RESO] > 0.002f || type != 0;
|
|
bool sq = cur[SHAPE] > 0.002f;
|
|
bool soft = cur[SOFT] > 0.002f;
|
|
if (svf != csvf) {
|
|
clp[0] = svf ? 0.f : clp[1];
|
|
clp[2] = svf ? 0.f : clp[3];
|
|
csvf = svf;
|
|
}
|
|
float g = g0 * cgain;
|
|
float p0 = cph[0], p1 = cph[1], p2 = cph[2];
|
|
const float d0 = cdt[0], d1 = cdt[1], d2 = cdt[2];
|
|
const float i0 = cid[0], i1 = cid[1], i2 = cid[2];
|
|
const float sh = csh, w0 = cw[0], w1 = cw[1], w2 = cw[2];
|
|
for (int i = 0; i < CH; ++i) {
|
|
p0 += d0; if (p0 >= 1.f) p0 -= 1.f;
|
|
p1 += d1; if (p1 >= 1.f) p1 -= 1.f;
|
|
p2 += d2; if (p2 >= 1.f) p2 -= 1.f;
|
|
float s0 = 2.f * p0 - 1.f - blep(p0, d0, i0);
|
|
float s1 = 2.f * p1 - 1.f - blep(p1, d1, i1);
|
|
float s2 = 2.f * p2 - 1.f - blep(p2, d2, i2);
|
|
if (sq) {
|
|
float q0 = p0 + w0; if (q0 >= 1.f) q0 -= 1.f;
|
|
float q1 = p1 + w1; if (q1 >= 1.f) q1 -= 1.f;
|
|
float q2 = p2 + w2; if (q2 >= 1.f) q2 -= 1.f;
|
|
s0 -= sh * (2.f * q0 - 1.f - blep(q0, d0, i0));
|
|
s1 -= sh * (2.f * q1 - 1.f - blep(q1, d1, i1));
|
|
s2 -= sh * (2.f * q2 - 1.f - blep(q2, d2, i2));
|
|
}
|
|
if (soft) { xc[i] = s0; x0[i] = s1; x1[i] = s2; } else { x0[i] = s0 + s1; x1[i] = s0 + s2; }
|
|
}
|
|
cph[0] = p0; cph[1] = p1; cph[2] = p2;
|
|
if (soft) {
|
|
float kc = TWO_PI * d0; float ks = TWO_PI * d1;
|
|
float lc = 1.f - 0.15f * kc; float ls = 1.f - 0.15f * ks;
|
|
float u = cur[SOFT];
|
|
float a0, a1, a2;
|
|
if (u < 0.5f) { a0 = kt::cosf_(PI * u); a1 = kt::sinf_(PI * u); a2 = 0.f; }
|
|
else { a0 = 0.f; a1 = kt::cosf_(PI * (u - 0.5f)); a2 = kt::sinf_(PI * (u - 0.5f)); }
|
|
float c1 = a1 * kc, c2 = a2 * kc * kc, e1 = a1 * ks, e2 = a2 * ks * ks;
|
|
float cA = cint[0], cB = cint[1], l1 = cint[2], l2 = cint[3], q1 = cint[4], q2 = cint[5];
|
|
for (int j = 0; j < CH; ++j) {
|
|
float vc = xc[j], vl = x0[j], vr = x1[j];
|
|
cA = lc * cA + vc; cB = lc * cB + cA;
|
|
l1 = ls * l1 + vl; l2 = ls * l2 + l1;
|
|
q1 = ls * q1 + vr; q2 = ls * q2 + q1;
|
|
float cc = a0 * vc + c1 * cA + c2 * cB;
|
|
x0[j] = cc + a0 * vl + e1 * l1 + e2 * l2;
|
|
x1[j] = cc + a0 * vr + e1 * q1 + e2 * q2;
|
|
}
|
|
if (!(cB > -1e9f && cB < 1e9f && l2 > -1e9f && l2 < 1e9f && q2 > -1e9f && q2 < 1e9f)) {
|
|
cA = 0.f; cB = 0.f; l1 = 0.f; l2 = 0.f; q1 = 0.f; q2 = 0.f;
|
|
}
|
|
cint[0] = cA; cint[1] = cB; cint[2] = l1; cint[3] = l2; cint[4] = q1; cint[5] = q2;
|
|
} else {
|
|
for (float &v : cint) v = 0.f;
|
|
}
|
|
if (svf) {
|
|
float o0 = sp0 * g, o1 = sp1 * g, o2 = sp2 * g;
|
|
float e0 = (sc0 - sp0) * g / CH, e1 = (sc1 - sp1) * g / CH, e2 = (sc2 - sp2) * g / CH;
|
|
svfRun(ba, x0, 0, o0, o1, o2, e0, e1, e2);
|
|
svfRun(bb, x1, 2, o0, o1, o2, e0, e1, e2);
|
|
} else {
|
|
float a = ca;
|
|
float l1 = clp[0], l2 = clp[1], q1 = clp[2], q2 = clp[3];
|
|
for (int i = 0; i < CH; ++i) {
|
|
l1 += a * (x0[i] - l1); l2 += a * (l1 - l2);
|
|
q1 += a * (x1[i] - q1); q2 += a * (q1 - q2);
|
|
ba[i] += l2 * g; bb[i] += q2 * g;
|
|
}
|
|
clp[0] = l1; clp[1] = l2; clp[2] = q1; clp[3] = q2;
|
|
}
|
|
sp0 = sc0; sp1 = sc1; sp2 = sc2;
|
|
}
|
|
|
|
void FrasilVoice::svfRun(float *b, const float *x, int s, float o0i, float o1i, float o2i,
|
|
float e0, float e1, float e2) {
|
|
float s1 = clp[s], s2 = clp[s + 1];
|
|
float o0 = o0i, o1 = o1i, o2 = o2i;
|
|
const float a1 = sa1, a2 = sa2, a3 = sa3;
|
|
for (int i = 0; i < CH; ++i) {
|
|
float v = x[i] - s2;
|
|
float t1 = a1 * s1 + a2 * v;
|
|
float t2 = s2 + a2 * s1 + a3 * v;
|
|
s1 = 2.f * t1 - s1;
|
|
s2 = 2.f * t2 - s2;
|
|
b[i] += o0 * x[i] + o1 * t1 + o2 * t2;
|
|
o0 += e0; o1 += e1; o2 += e2;
|
|
}
|
|
clp[s] = s1; clp[s + 1] = s2;
|
|
}
|
|
|
|
void FrasilVoice::partialsChunk(float g) {
|
|
for (int j = 0; j < np; ++j) {
|
|
float *bus = j == 0 ? bc : ((j & 1) == 1 ? ba : bb); // 1, 3, 5, 7 -> ba; 2, 4, 6 -> bb
|
|
float x = px[j], y = py[j];
|
|
float e = 2.f * ps[j];
|
|
float a = pac[j] * g;
|
|
float d = pda[j] * g;
|
|
for (int i = 0; i < CH; ++i) {
|
|
x -= e * y;
|
|
y += e * x;
|
|
bus[i] += a * x;
|
|
a += d;
|
|
}
|
|
px[j] = x; py[j] = y;
|
|
pac[j] += pda[j] * CH;
|
|
}
|
|
}
|
|
|
|
void FrasilVoice::breathNoise() {
|
|
float grain = cur[GRAIN];
|
|
if (grain < 0.002f) {
|
|
for (int i = 0; i < CH; ++i) nz[i] = randf();
|
|
} else {
|
|
float p = kt::powf_(2.f, -13.f * grain) * r44;
|
|
float amp = std::fmin(16.f, kt::sqrtf_(1.f / (3.f * 0.5833f * p)));
|
|
int32_t th = kt::toInt(p * 16777216.f);
|
|
for (int i = 0; i < CH; ++i) {
|
|
int32_t r = nextRand();
|
|
float a = amp * (0.5f + (float)(r & 255) * (0.5f / 255.f));
|
|
nz[i] = (int32_t)((uint32_t)r >> 8) < th ? ((r & 256) != 0 ? a : -a) : 0.f;
|
|
}
|
|
}
|
|
float t = cur[TONE];
|
|
if (t < -0.01f) {
|
|
float fc = 16000.f * kt::powf_(300.f / 16000.f, -t);
|
|
float a = 1.f - kt::expf_(-TWO_PI * std::fmin(fc, nyq) * invFs);
|
|
float gn = kt::sqrtf_((2.f - a) / a);
|
|
float l = nlp;
|
|
for (int i = 0; i < CH; ++i) { l += a * (nz[i] - l); nz[i] = l * gn; }
|
|
nlp = flush(l);
|
|
} else if (t > 0.01f) {
|
|
float fc = 20.f * kt::powf_(150.f, t);
|
|
float a = 1.f - kt::expf_(-TWO_PI * fc * invFs);
|
|
float gn = 1.f / kt::sqrtf_(1.f + a / (2.f - a) - 2.f * a);
|
|
float l = nlp;
|
|
for (int i = 0; i < CH; ++i) { l += a * (nz[i] - l); nz[i] = (nz[i] - l) * gn; }
|
|
nlp = flush(l);
|
|
}
|
|
}
|
|
|
|
void FrasilVoice::souffleChunk(float g) {
|
|
float air = cur[AIR];
|
|
float ga = air * 0.7f * g;
|
|
for (int j = 0; j < NR; ++j) {
|
|
float *bus = j == 0 ? bc : (j == 1 ? ba : bb);
|
|
float y1 = r1[j], y2 = r2[j];
|
|
const float b1 = rb1[j], b2 = rb2[j], gg = rg[j] * g;
|
|
bool withAir = j == 0 && air > 0.001f;
|
|
for (int i = 0; i < CH; ++i) {
|
|
float y = gg * nz[i] + b1 * y1 - b2 * y2;
|
|
y2 = y1; y1 = y;
|
|
bus[i] += withAir ? y + ga * nz[i] : y;
|
|
}
|
|
if (y1 > -1e-20f && y1 < 1e-20f && y2 > -1e-20f && y2 < 1e-20f) { y1 = 0.f; y2 = 0.f; }
|
|
r1[j] = y1; r2[j] = y2;
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------- helpers ---
|
|
float FrasilVoice::brK(float k) const { return 1.f - kt::powf_(1.f - k, r44); }
|
|
|
|
int32_t FrasilVoice::nextRand() {
|
|
uint32_t x = rng;
|
|
x ^= x << 13;
|
|
x ^= x >> 17;
|
|
x ^= x << 5;
|
|
rng = x;
|
|
return (int32_t)x;
|
|
}
|
|
|
|
float FrasilVoice::randf() { return (float)nextRand() * (1.f / 2147483648.f); }
|
|
float FrasilVoice::urand() { return (float)((uint32_t)nextRand() >> 8) * (1.f / 16777216.f); }
|
|
|
|
} // namespace sizzle::frasil
|