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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
64e21e0bcc
commit
2bc5cb1cbb
@@ -0,0 +1,159 @@
|
||||
// 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 FrasilDefs.kt (engineFrom, the 36 drone presets) and FrasilPatch.from.
|
||||
|
||||
#include <cstring>
|
||||
#include "frasil/FrasilVoice.hpp"
|
||||
#include "sizzle/Params.hpp"
|
||||
#include "KotlinMath.hpp"
|
||||
|
||||
namespace sizzle::frasil {
|
||||
namespace {
|
||||
|
||||
using namespace defs;
|
||||
|
||||
const char *const INTERVALS[] = {"Unison", "-12", "+7", "+12"};
|
||||
constexpr float INTERVAL_RATIO[] = {1.f, 0.5f, 1.4983071f, 2.f};
|
||||
const char *const FILTER_TYPES[] = {"Low", "Band", "High"};
|
||||
const char *const BREATH_PITCHES[] = {"-12", "Unison", "+12", "+19", "+24"};
|
||||
constexpr float BREATH_RATIO[] = {0.5f, 1.f, 2.f, 2.9966142f, 4.f};
|
||||
|
||||
template <int K> int indexOf(const char *const (&list)[K], const char *s) {
|
||||
for (int i = 0; i < K; ++i) if (!std::strcmp(list[i], s)) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
float r(int v) { return (float)v / 127.f; }
|
||||
|
||||
/** engineFrom: [num](key, def) reads a slider, [choice](key, def) a choice. */
|
||||
template <class Num, class Choice> int engineFrom(float *e, Num num, Choice choice) {
|
||||
auto u = [&](const char *key, float def) { return kt::coerceIn(num(key, def), 0.f, 1.f); };
|
||||
e[CHOIR] = u("choir", r(76)); e[PARTIALS] = u("partials", r(89)); e[BREATH] = u("breath", r(32));
|
||||
e[WARMTH] = u("warmth", r(19)); e[DETUNE] = u("detune", r(51)); e[CUTOFF] = u("cfilter", r(44));
|
||||
e[TILT] = u("tilt", r(57)); e[ODD] = u("odd", r(25)); e[STRETCH] = u("stretch", 0.f);
|
||||
e[RESONANCE] = u("bres", r(70)); e[COLOR] = u("color", 0.f);
|
||||
e[ATTACK] = u("attack", r(89)); e[RELEASE] = u("release", r(83));
|
||||
e[MOTION] = u("motion", r(44)); e[MOTION_RATE] = u("mrate", r(51));
|
||||
e[TIDE] = u("tide", r(25)); e[TIDE_RATE] = u("tperiod", r(38)); e[TIMBRE] = u("timbre", r(64));
|
||||
e[SHAPE] = u("shape", 0.f); e[CH_RESO] = u("creso", 0.f);
|
||||
e[EVOLVE] = u("evolve", 0.f); e[BEAT] = u("beat", 0.f);
|
||||
e[FORMANT] = 3.f * u("formant", r(85)); e[PEAK] = u("peak", 0.f);
|
||||
e[GRAIN] = u("grain", 0.f);
|
||||
float tn = u("btone", r(64)) * 127.f;
|
||||
e[TONE] = tn >= 126.5f ? 1.f : (tn - 64.f) / 63.f;
|
||||
e[AIR] = u("air", 0.f); e[SOFT] = u("soft", 0.f);
|
||||
e[VELOCITY] = u("velsens", r(38)); e[BODY] = u("body", 1.f);
|
||||
e[INTERVAL] = INTERVAL_RATIO[kt::coerceAtLeast(indexOf(INTERVALS, choice("interval", INTERVALS[0])), 0)];
|
||||
int bp = indexOf(BREATH_PITCHES, choice("bpitch", BREATH_PITCHES[1]));
|
||||
e[BR_PITCH] = BREATH_RATIO[bp < 0 ? 1 : bp];
|
||||
return kt::coerceAtLeast(indexOf(FILTER_TYPES, choice("ctype", FILTER_TYPES[0])), 0);
|
||||
}
|
||||
|
||||
// CHO PAR SOU WAR DET FIL TIL ODD STR BRES COL SPR ATT REL MOT RAT TID PER TIM SPA SHP INT CRES TYP EVO BEAT
|
||||
// FORM PEAK GRN PIT TONE AIR SOFT. SPREAD and SPACE (null) are FRASIL's stereo field and reverb.
|
||||
constexpr int NCOL = 33;
|
||||
const char *const COLS[NCOL] = {
|
||||
"choir", "partials", "breath", "warmth", "detune", "cfilter", "tilt", "odd", "stretch", "bres", "color",
|
||||
nullptr, "attack", "release", "motion", "mrate", "tide", "tperiod", "timbre", nullptr, "shape", "interval",
|
||||
"creso", "ctype", "evolve", "beat", "formant", "peak", "grain", "bpitch", "btone", "air", "soft",
|
||||
};
|
||||
|
||||
struct Raw { const char *name; int row[NCOL]; };
|
||||
|
||||
const Raw RAW[] = {
|
||||
{"Mist", {76, 89, 32, 19, 51, 44, 57, 25, 0, 70, 0, 89, 89, 83, 44, 51, 25, 38, 64, 57, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Cathedral", {110, 50, 20, 25, 85, 55, 60, 30, 0, 60, 0, 110, 100, 100, 60, 40, 20, 50, 60, 95, 0, 1, 20, 0, 10, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Glacier", {20, 100, 70, 5, 30, 60, 35, 50, 60, 115, 127, 100, 80, 110, 40, 30, 30, 40, 80, 90, 0, 0, 0, 0, 40, 15, 100, 30, 20, 2, 80, 0, 0}},
|
||||
{"Abyss", {90, 70, 25, 50, 70, 20, 90, 40, 0, 50, 0, 70, 100, 105, 50, 25, 50, 25, 40, 70, 30, 1, 40, 0, 20, 0, 60, 0, 0, 0, 30, 0, 0}},
|
||||
{"Aurora", {60, 105, 40, 15, 45, 65, 40, 15, 10, 85, 60, 115, 90, 105, 55, 45, 45, 50, 75, 95, 0, 0, 0, 0, 70, 25, 90, 35, 0, 2, 75, 10, 0}},
|
||||
{"Ember", {100, 55, 20, 80, 40, 40, 70, 60, 0, 50, 30, 80, 70, 80, 35, 40, 20, 40, 55, 50, 64, 0, 50, 0, 10, 0, 70, 0, 0, 1, 50, 0, 0}},
|
||||
{"Tide", {80, 80, 45, 25, 60, 45, 55, 30, 10, 75, 40, 100, 95, 100, 70, 30, 110, 60, 60, 70, 0, 0, 30, 0, 30, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Dust", {25, 40, 85, 20, 40, 50, 60, 30, 20, 100, 60, 120, 60, 90, 50, 50, 30, 40, 60, 75, 0, 0, 0, 0, 30, 0, 85, 0, 90, 2, 85, 40, 0}},
|
||||
{"Wind", {0, 20, 85, 0, 40, 50, 60, 30, 0, 20, 30, 127, 80, 95, 90, 80, 50, 70, 64, 80, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 75, 50, 0}},
|
||||
{"Organ", {40, 115, 10, 30, 10, 50, 45, 0, 0, 60, 0, 60, 40, 60, 15, 40, 10, 40, 64, 50, 0, 2, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Hollow", {105, 80, 25, 20, 35, 50, 50, 110, 0, 70, 64, 90, 80, 90, 40, 45, 25, 40, 60, 65, 64, 0, 40, 1, 15, 0, 85, 0, 0, 1, 60, 0, 0}},
|
||||
{"Vowel", {65, 125, 15, 10, 30, 55, 30, 20, 0, 70, 0, 85, 90, 95, 45, 35, 60, 45, 64, 70, 0, 0, 60, 1, 20, 0, 95, 105, 0, 1, 64, 0, 0}},
|
||||
{"Shimmer", {45, 100, 50, 10, 55, 75, 25, 20, 15, 100, 90, 120, 85, 110, 50, 55, 30, 40, 85, 100, 0, 3, 0, 0, 40, 50, 100, 20, 10, 3, 90, 20, 0}},
|
||||
{"Chanter", {105, 45, 15, 40, 30, 55, 60, 70, 0, 60, 30, 70, 50, 70, 30, 45, 15, 40, 60, 45, 110, 0, 35, 1, 0, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Ensemble", {115, 40, 20, 30, 75, 60, 55, 25, 0, 60, 0, 115, 95, 90, 55, 55, 20, 40, 65, 75, 90, 0, 15, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Bells", {15, 115, 45, 5, 25, 50, 30, 20, 95, 110, 127, 95, 40, 110, 30, 40, 20, 40, 75, 85, 0, 0, 0, 0, 30, 30, 85, 0, 0, 2, 80, 0, 0}},
|
||||
{"Nocturne", {90, 60, 30, 35, 55, 25, 85, 30, 0, 55, 0, 90, 110, 115, 45, 20, 40, 20, 45, 85, 20, 1, 70, 0, 25, 0, 70, 0, 0, 0, 35, 0, 0}},
|
||||
{"Haze", {85, 50, 60, 10, 60, 70, 50, 25, 0, 40, 30, 120, 100, 105, 60, 50, 30, 50, 70, 90, 0, 3, 20, 2, 20, 0, 85, 0, 0, 2, 80, 30, 0}},
|
||||
{"Pulse", {50, 95, 25, 20, 30, 50, 45, 30, 0, 70, 20, 100, 60, 80, 30, 50, 20, 40, 64, 65, 0, 0, 0, 0, 15, 100, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Rime", {30, 110, 70, 0, 20, 80, 20, 40, 30, 120, 100, 110, 70, 100, 35, 60, 20, 40, 90, 90, 0, 3, 0, 2, 30, 20, 110, 40, 40, 4, 100, 15, 0}},
|
||||
{"Deep", {100, 100, 0, 40, 25, 25, 100, 20, 0, 50, 0, 60, 90, 95, 25, 30, 20, 30, 40, 45, 0, 1, 10, 0, 0, 0, 40, 0, 0, 0, 30, 0, 0}},
|
||||
{"Spectral", {20, 120, 15, 5, 20, 50, 20, 10, 0, 70, 0, 90, 90, 100, 50, 40, 80, 50, 64, 80, 0, 0, 0, 0, 50, 10, 85, 127, 0, 1, 64, 0, 0}},
|
||||
{"Rust", {80, 50, 40, 100, 50, 45, 65, 60, 20, 80, 40, 80, 60, 80, 40, 55, 25, 40, 55, 55, 70, 0, 80, 1, 20, 15, 75, 0, 60, 1, 45, 20, 0}},
|
||||
{"Still", {0, 125, 0, 0, 0, 50, 127, 0, 0, 50, 0, 60, 100, 100, 5, 20, 0, 40, 50, 70, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0}},
|
||||
{"Velvet", {95, 45, 10, 15, 35, 40, 85, 20, 0, 50, 0, 100, 105, 110, 35, 30, 20, 45, 52, 70, 0, 0, 10, 0, 0, 0, 85, 0, 0, 1, 50, 0, 120}},
|
||||
{"Chrome", {35, 115, 15, 35, 15, 75, 15, 120, 45, 90, 80, 85, 60, 90, 35, 55, 15, 40, 85, 60, 64, 3, 85, 2, 20, 35, 110, 70, 0, 3, 95, 0, 0}},
|
||||
{"Swarm", {120, 75, 30, 30, 120, 60, 40, 30, 0, 60, 20, 127, 70, 85, 110, 100, 40, 15, 70, 60, 120, 0, 40, 1, 40, 70, 85, 0, 30, 2, 70, 20, 0}},
|
||||
{"Foghorn", {127, 60, 15, 70, 20, 22, 95, 90, 0, 40, 0, 50, 120, 120, 20, 20, 60, 70, 35, 65, 64, 1, 60, 0, 0, 0, 40, 30, 0, 0, 30, 0, 30}},
|
||||
{"Solar", {70, 105, 45, 20, 55, 90, 15, 15, 5, 80, 30, 120, 100, 110, 60, 50, 70, 35, 100, 90, 10, 3, 15, 2, 60, 25, 120, 40, 0, 3, 110, 30, 0}},
|
||||
{"Cave", {55, 40, 95, 30, 30, 18, 110, 30, 0, 100, 60, 70, 110, 120, 30, 20, 45, 60, 30, 100, 0, 1, 50, 0, 10, 0, 40, 0, 50, 0, 18, 5, 60}},
|
||||
{"Glass Harp", {10, 120, 20, 0, 12, 60, 30, 10, 25, 100, 90, 110, 50, 120, 25, 30, 15, 55, 90, 95, 0, 3, 0, 0, 45, 40, 120, 110, 0, 4, 100, 0, 127}},
|
||||
{"Monk", {110, 85, 10, 25, 30, 38, 70, 40, 0, 60, 0, 70, 100, 100, 30, 25, 30, 65, 50, 80, 0, 1, 55, 1, 15, 0, 62, 90, 0, 1, 50, 0, 90}},
|
||||
{"Static", {35, 40, 127, 15, 40, 70, 40, 50, 0, 110, 90, 127, 40, 70, 80, 110, 20, 20, 80, 55, 30, 0, 30, 2, 0, 0, 85, 0, 85, 3, 100, 40, 0}},
|
||||
{"Lullaby", {85, 70, 8, 10, 22, 45, 100, 10, 0, 60, 0, 80, 110, 120, 20, 15, 35, 80, 55, 85, 0, 2, 0, 0, 10, 8, 85, 20, 0, 1, 64, 0, 110}},
|
||||
{"Storm", {80, 40, 110, 90, 70, 28, 90, 40, 0, 70, 40, 127, 90, 110, 100, 85, 120, 25, 40, 75, 30, 1, 70, 0, 50, 20, 60, 0, 70, 0, 35, 60, 20}},
|
||||
{"Prism", {50, 120, 25, 10, 40, 65, 25, 60, 15, 90, 50, 120, 85, 105, 60, 45, 50, 40, 80, 90, 0, 3, 20, 1, 100, 30, 105, 80, 0, 2, 85, 10, 40}},
|
||||
};
|
||||
constexpr int NRAW = (int)(sizeof(RAW) / sizeof(RAW[0]));
|
||||
|
||||
int colOf(const char *key) {
|
||||
for (int c = 0; c < NCOL; ++c) if (COLS[c] && !std::strcmp(COLS[c], key)) return c;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** factoryEngine(name): a preset's values as FACTORY stores them (row / 127f as text,
|
||||
* which round-trips exactly, the choices by name), through engineFrom. */
|
||||
bool factoryEngine(const char *name, float *e, int &type) {
|
||||
for (const Raw &p : RAW) {
|
||||
if (std::strcmp(p.name, name)) continue;
|
||||
auto num = [&](const char *k, float d) -> float {
|
||||
if (!std::strcmp(k, "velsens")) return r(38);
|
||||
if (!std::strcmp(k, "body")) return 1.f;
|
||||
int c = colOf(k);
|
||||
return c < 0 ? d : (float)p.row[c] / 127.f;
|
||||
};
|
||||
auto choice = [&](const char *k, const char *d) -> const char * {
|
||||
int c = colOf(k);
|
||||
if (c < 0) return d;
|
||||
if (!std::strcmp(k, "interval")) return INTERVALS[p.row[c] % 4];
|
||||
if (!std::strcmp(k, "ctype")) return FILTER_TYPES[p.row[c] % 3];
|
||||
if (!std::strcmp(k, "bpitch")) return BREATH_PITCHES[p.row[c] % 5];
|
||||
return d;
|
||||
};
|
||||
type = engineFrom(e, num, choice);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int defs::factoryCount() { return NRAW; }
|
||||
const char *defs::factoryName(int i) { return i >= 0 && i < NRAW ? RAW[i].name : ""; }
|
||||
|
||||
FrasilPatch FrasilPatch::from(const Params *slot) {
|
||||
FrasilPatch p;
|
||||
p.aType = engineFrom(
|
||||
p.a,
|
||||
[&](const char *k, float d) { return slot ? slot->f(k, d) : d; },
|
||||
[&](const char *k, const char *d) { return slot ? slot->s(k, d) : d; });
|
||||
const char *target = slot ? slot->s(MORPH_TO, "Off") : "Off";
|
||||
int bt = 0;
|
||||
p.hasB = std::strcmp(target, "Off") != 0 && factoryEngine(target, p.b, bt);
|
||||
p.bType = p.hasB ? bt : p.aType;
|
||||
p.morph = kt::coerceIn(slot ? slot->f(MORPH, 0.f) : 0.f, 0.f, 1.f);
|
||||
p.morphTime = kt::coerceIn(slot ? slot->f(MORPH_TIME, 0.f) : 0.f, 0.f, 120.f);
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace sizzle::frasil
|
||||
@@ -0,0 +1,54 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// The reference tests' FRASIL maker (see TestVoice.hpp, IosGoldenInstruments.kt).
|
||||
|
||||
#include <cmath>
|
||||
#include "frasil/FrasilVoice.hpp"
|
||||
#include "sizzle/TestVoice.hpp"
|
||||
|
||||
namespace sizzle {
|
||||
namespace {
|
||||
|
||||
using frasil::FrasilClock;
|
||||
using frasil::FrasilPatch;
|
||||
using frasil::FrasilSeeds;
|
||||
using frasil::FrasilVoice;
|
||||
|
||||
/**
|
||||
* The seed the Kotlin export's FrasilVoice got: the export records Kotlin's global
|
||||
* seed counter right before the scenario built its voices ("seeds" line). The
|
||||
* SynthVoice built first takes the next seed for its own Morph Pad voice; the
|
||||
* FrasilVoice takes the one after.
|
||||
*/
|
||||
int32_t exportSeed(const TestVoiceSetup &s) {
|
||||
FrasilSeeds seeds;
|
||||
seeds.seeds = s.frasilSeeds;
|
||||
seeds.next(); // the SynthVoice's Morph Pad voice
|
||||
return seeds.next();
|
||||
}
|
||||
|
||||
class FrasilTestVoice final : public TestVoice {
|
||||
FrasilClock clock;
|
||||
FrasilPatch patch;
|
||||
FrasilVoice voice;
|
||||
int sampleRate;
|
||||
|
||||
public:
|
||||
explicit FrasilTestVoice(const TestVoiceSetup &s)
|
||||
: clock{s.clock0}, patch(FrasilPatch::from(s.params)), voice(s.sampleRate, clock, exportSeed(s)),
|
||||
sampleRate(s.sampleRate) {}
|
||||
|
||||
void blockStart(int frames) override { clock.advance(frames, sampleRate); }
|
||||
void noteOn(int midi, int velocity) override { voice.noteOn(midi, (float)velocity / 127.f, patch); }
|
||||
void noteOff(int) override { voice.noteOff(); }
|
||||
float render() override { return voice.render(); }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<TestVoice> makeFrasilTestVoice(const TestVoiceSetup &setup) {
|
||||
return std::make_unique<FrasilTestVoice>(setup);
|
||||
}
|
||||
|
||||
} // namespace sizzle
|
||||
@@ -0,0 +1,521 @@
|
||||
// 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
|
||||
@@ -0,0 +1,186 @@
|
||||
// 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 frasil/FrasilVoice.kt, FrasilPatch.kt and FrasilDefs.kt (the Morph Pad).
|
||||
// Kotlin's globals become explicit state: FrasilClock (TIDE's shared time) and
|
||||
// FrasilSeeds (the companion's seed counter) are owned by the engine and handed in.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace sizzle {
|
||||
class Params;
|
||||
}
|
||||
|
||||
namespace sizzle::frasil {
|
||||
|
||||
/** FRASIL's TIDE is one slow sine shared by every voice: the engine advances this
|
||||
* clock once a block, each voice reads it. Audio thread only. */
|
||||
struct FrasilClock {
|
||||
double seconds = 0.0;
|
||||
void advance(int frames, int sampleRate) { seconds += (double)frames / sampleRate; }
|
||||
};
|
||||
|
||||
/** The Kotlin companion's seed counter (FrasilVoice.nextSeed): each new voice takes
|
||||
* the next seed. One per engine, so voices get the seeds the Kotlin app gives them. */
|
||||
struct FrasilSeeds {
|
||||
uint32_t seeds = 0x2545F491u;
|
||||
int32_t next() {
|
||||
seeds += 0x6D2B79F5u;
|
||||
return seeds == 0 ? (int32_t)0x2545F491 : (int32_t)seeds;
|
||||
}
|
||||
};
|
||||
|
||||
/** Engine parameter indices (FrasilDefs). */
|
||||
namespace defs {
|
||||
constexpr int CHOIR = 0, PARTIALS = 1, BREATH = 2, WARMTH = 3, DETUNE = 4, CUTOFF = 5, TILT = 6,
|
||||
ODD = 7, STRETCH = 8, RESONANCE = 9, COLOR = 10, ATTACK = 11, RELEASE = 12,
|
||||
MOTION = 13, MOTION_RATE = 14, TIDE = 15, TIDE_RATE = 16, TIMBRE = 17, SHAPE = 18,
|
||||
CH_RESO = 19, EVOLVE = 20, BEAT = 21, FORMANT = 22, PEAK = 23, GRAIN = 24, TONE = 25,
|
||||
AIR = 26, SOFT = 27, VELOCITY = 28, BODY = 29, INTERVAL = 30, BR_PITCH = 31, N = 32;
|
||||
constexpr const char *MORPH_TO = "morphTo";
|
||||
constexpr const char *MORPH = "morph";
|
||||
constexpr const char *MORPH_TIME = "mtime";
|
||||
/** FRASIL's 36 drone presets (MORPH TO's targets). */
|
||||
int factoryCount();
|
||||
const char *factoryName(int i);
|
||||
} // namespace defs
|
||||
|
||||
/** A Morph Pad slot's snapshot (FrasilPatch): build it off the audio thread. */
|
||||
struct FrasilPatch {
|
||||
float a[defs::N] = {};
|
||||
int aType = 0;
|
||||
bool hasB = false; // Kotlin's b != null
|
||||
float b[defs::N] = {};
|
||||
int bType = 0;
|
||||
/** Where a note starts between a (0) and b (1). */
|
||||
float morph = 0.f;
|
||||
/** Seconds a held note takes from morph to b; 0 stays at morph. */
|
||||
float morphTime = 0.f;
|
||||
|
||||
/** FrasilPatch.from(slot); nullptr is Kotlin's from(null) (all defaults). */
|
||||
static FrasilPatch from(const Params *slot);
|
||||
static FrasilPatch from(const Params &slot) { return from(&slot); }
|
||||
/** FrasilPatch.DEFAULT. */
|
||||
static FrasilPatch defaults() { return from(nullptr); }
|
||||
};
|
||||
|
||||
/** One note of the Morph Pad (FrasilVoice). Allocation-free; the patch is copied in. */
|
||||
class FrasilVoice {
|
||||
public:
|
||||
static constexpr int NP = 8;
|
||||
static constexpr int NR = 3;
|
||||
static constexpr int CH = 64;
|
||||
static constexpr int BLOCK = 256;
|
||||
|
||||
/** [clock] must outlive the voice; [seed] from the engine's FrasilSeeds. */
|
||||
FrasilVoice(int fs, const FrasilClock &clock, int32_t seed);
|
||||
|
||||
int note() const { return note_; }
|
||||
bool isActive() const { return note_ >= 0; }
|
||||
bool isGated() const { return note_ >= 0 && gate; }
|
||||
|
||||
/** Start [midi] at [velocity] (0..1); a sounding voice goes on from where it is. */
|
||||
void noteOn(int midi, float velocity, const FrasilPatch &p);
|
||||
void noteOff() { gate = false; }
|
||||
/** Silence now, no tail. */
|
||||
void kill();
|
||||
/** Follow edits while sounding: the values glide there over ~25 ms. */
|
||||
void setPatch(const FrasilPatch &p) { patch = p; }
|
||||
/** A gliding engine value now (a defs index), for tests. */
|
||||
float valueForTest(int i) const { return cur[i]; }
|
||||
float render();
|
||||
|
||||
private:
|
||||
void start();
|
||||
void block();
|
||||
float morphPosition(const FrasilPatch &p) const;
|
||||
float ou(float x, float th, float s);
|
||||
void drift();
|
||||
void voiceBlock(bool fresh);
|
||||
void renderChunk();
|
||||
void choirChunk(float g0);
|
||||
void svfRun(float *b, const float *x, int s, float o0i, float o1i, float o2i, float e0, float e1, float e2);
|
||||
void partialsChunk(float g);
|
||||
void breathNoise();
|
||||
void souffleChunk(float g);
|
||||
float brK(float k) const;
|
||||
int32_t nextRand();
|
||||
float randf();
|
||||
float urand();
|
||||
|
||||
const FrasilClock *clock;
|
||||
const float invFs;
|
||||
const float r44;
|
||||
const float nyq;
|
||||
|
||||
int note_ = -1;
|
||||
bool gate = false;
|
||||
float vel = 0.f;
|
||||
float env = 0.f;
|
||||
float lvl = 0.f;
|
||||
FrasilPatch patch;
|
||||
bool prime = true;
|
||||
float cur[defs::N] = {};
|
||||
int type = 0;
|
||||
int64_t ageFrames = 0;
|
||||
float tideV = 0.f;
|
||||
uint32_t rng;
|
||||
|
||||
// drift
|
||||
float ouC[3] = {};
|
||||
float ouCut = 0.f;
|
||||
float ouPa[NP] = {};
|
||||
float ouPf[NP] = {};
|
||||
float ouR[NR] = {};
|
||||
|
||||
// CHOIR
|
||||
float cph[3] = {}, cdt[3] = {}, cid[3] = {}, cw[3] = {}, cpw[3] = {};
|
||||
float csh = 0.f;
|
||||
float sc0 = 0.f, sc1 = 0.f, sc2 = 1.f;
|
||||
float sp0 = 0.f, sp1 = 0.f, sp2 = 1.f;
|
||||
float clp[4] = {};
|
||||
float ca = 0.f;
|
||||
float sa1 = 0.f, sa2 = 0.f, sa3 = 0.f;
|
||||
float cgain = 1.f;
|
||||
float cint[6] = {};
|
||||
bool csvf = false;
|
||||
|
||||
// PARTIALS
|
||||
float px[NP] = {}, py[NP] = {}, pa[NP] = {}, ps[NP] = {}, pc[NP] = {};
|
||||
float pac[NP] = {}, pda[NP] = {}, pbt[NP] = {};
|
||||
int np = 0;
|
||||
|
||||
// BREATH
|
||||
float r1[NR] = {}, r2[NR] = {}, rb1[NR] = {}, rb2[NR] = {}, rg[NR] = {};
|
||||
float nlp = 0.f;
|
||||
|
||||
// output stages
|
||||
float dcState = 0.f;
|
||||
float thinState = 0.f;
|
||||
float ka = 0.f, kr = 0.f;
|
||||
float dk = 1.f, dmake = 1.f;
|
||||
bool driveOn = false;
|
||||
float thinA = 0.f;
|
||||
bool thin = false;
|
||||
const float kdc;
|
||||
const float glideK;
|
||||
const float tideK;
|
||||
|
||||
// chunk buffers
|
||||
float bc[CH] = {}, ba[CH] = {}, bb[CH] = {};
|
||||
float x0[CH] = {}, x1[CH] = {}, xc[CH] = {};
|
||||
float nz[CH] = {};
|
||||
float out[CH] = {};
|
||||
int pos = CH;
|
||||
int chunk = 0;
|
||||
bool ending = false;
|
||||
};
|
||||
|
||||
} // namespace sizzle::frasil
|
||||
Reference in New Issue
Block a user