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