Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.
C# audioaudio-unitavaloniacpp20cross-platformdawdigital-audio-workstationdspjucelinux-audiomcp-servermidimusicmusic-productionpiano-rollplugin-hostsequencersynthesizervstvst3
(fix) Engine: Pentad, Consort, Twin, Volt and Delay back to 0.59's cost (#600)
Egor Khindikaynen677f5475f0b9
diff --git a/src/native/nota.engine/src/devices/effects/Delay.cpp b/src/native/nota.engine/src/devices/effects/Delay.cppindex 6a8d80a3..9441e84f 100644--- a/src/native/nota.engine/src/devices/effects/Delay.cpp+++ b/src/native/nota.engine/src/devices/effects/Delay.cpp@@ -8,6 +8,8 @@#include "Delay.h"+#include <type_traits>+#include "dsp/math/FastMath.h"#include "dsp/math/SimdAudio.h"@@ -187,86 +189,93 @@ void Delay::process(float* buf, int32_t frames) {float wetPk = 0.0f, outPkL = 0.0f, outPkR = 0.0f, pushPk = 0.0f;- for (int32_t i = 0; i < frames; ++i) {- if (!fade) {- curL_ += std::clamp(tgtL - curL_, -glide, glide);- curR_ += std::clamp(tgtR - curR_, -glide, glide);- } else if (fadePos_ < fadeLen_) {- ++fadePos_;- }- const float lfo = fast_math::sin((float)wowPhase_);- wowPhase_ += wowInc;- if (wowPhase_ >= 1.0) wowPhase_ -= 1.0;- const double mod = static_cast<double>(lfo * wowDepth);-- float rdL, rdR;- if (fadePos_ < fadeLen_) {- const float t = static_cast<float>(fadePos_) / static_cast<float>(fadeLen_);- const float a = 1.0f - t;- // NOTE: DelayLine.readLinear(x) == orig read(x + 1) — the original- // interpolates toward the newer sample, so all taps are offset by −1.- rdL = bufL_.readLinear((float)(oldL_ + mod) - 1.0f) * a + bufL_.readLinear((float)(curL_ + mod) - 1.0f) * t;- rdR = bufR_.readLinear((float)(oldR_ - mod) - 1.0f) * a + bufR_.readLinear((float)(curR_ - mod) - 1.0f) * t;- } else {- rdL = bufL_.readLinear((float)(curL_ + mod) - 1.0f);- rdR = bufR_.readLinear((float)(curR_ - mod) - 1.0f);- }-- const float l = buf[i * 2], r = buf[i * 2 + 1];- float wl = ping ? rdR : rdL, wr = ping ? rdL : rdR;- float inpL = l * inGain + wl * fb;- float inpR = r * inGain + wr * fb;- if (!freeze) {- // The loop's tone: cuts, tape head loss + saturation. Frozen, the loop is- // left untouched so it holds instead of slowly dulling.- if (hpOn) {- hpL_ += hpC * (inpL - hpL_);- inpL -= hpL_;- hpR_ += hpC * (inpR - hpR_);- inpR -= hpR_;+ // The sample loop twice over, with and without the card's graph, so the plain one (the card+ // closed) carries no trace of it.+ auto run = [&](auto withGraph) {+ for (int32_t i = 0; i < frames; ++i) {+ if (!fade) {+ curL_ += std::clamp(tgtL - curL_, -glide, glide);+ curR_ += std::clamp(tgtR - curR_, -glide, glide);+ } else if (fadePos_ < fadeLen_) {+ ++fadePos_;}- if (lpOn) {- lpL_ += lpC * (inpL - lpL_);- inpL = lpL_;- lpR_ += lpC * (inpR - lpR_);- inpR = lpR_;+ const float lfo = fast_math::sin((float)wowPhase_);+ wowPhase_ += wowInc;+ if (wowPhase_ >= 1.0) wowPhase_ -= 1.0;+ const double mod = static_cast<double>(lfo * wowDepth);++ float rdL, rdR;+ if (fadePos_ < fadeLen_) {+ const float t = static_cast<float>(fadePos_) / static_cast<float>(fadeLen_);+ const float a = 1.0f - t;+ // NOTE: DelayLine.readLinear(x) == orig read(x + 1) — the original+ // interpolates toward the newer sample, so all taps are offset by −1.+ rdL = bufL_.readLinear((float)(oldL_ + mod) - 1.0f) * a + bufL_.readLinear((float)(curL_ + mod) - 1.0f) * t;+ rdR = bufR_.readLinear((float)(oldR_ - mod) - 1.0f) * a + bufR_.readLinear((float)(curR_ - mod) - 1.0f) * t;+ } else {+ rdL = bufL_.readLinear((float)(curL_ + mod) - 1.0f);+ rdR = bufR_.readLinear((float)(curR_ - mod) - 1.0f);}- if (tape) {- tapeL_ += tapeC * (inpL - tapeL_);- inpL = fast_math::tanhPrecise(tapeL_ * drive) * invTanhDrive;- tapeR_ += tapeC * (inpR - tapeR_);- inpR = fast_math::tanhPrecise(tapeR_ * drive) * invTanhDrive;++ const float l = buf[i * 2], r = buf[i * 2 + 1];+ float wl = ping ? rdR : rdL, wr = ping ? rdL : rdR;+ float inpL = l * inGain + wl * fb;+ float inpR = r * inGain + wr * fb;+ if (!freeze) {+ // The loop's tone: cuts, tape head loss + saturation. Frozen, the loop is+ // left untouched so it holds instead of slowly dulling.+ if (hpOn) {+ hpL_ += hpC * (inpL - hpL_);+ inpL -= hpL_;+ hpR_ += hpC * (inpR - hpR_);+ inpR -= hpR_;+ }+ if (lpOn) {+ lpL_ += lpC * (inpL - lpL_);+ inpL = lpL_;+ lpR_ += lpC * (inpR - lpR_);+ inpR = lpR_;+ }+ if (tape) {+ tapeL_ += tapeC * (inpL - tapeL_);+ inpL = fast_math::tanhPrecise(tapeL_ * drive) * invTanhDrive;+ tapeR_ += tapeC * (inpR - tapeR_);+ inpR = fast_math::tanhPrecise(tapeR_ * drive) * invTanhDrive;+ }}- }- inpL = ap_[1].tick(ap_[0].tick(inpL, apG), apG);- inpR = ap_[3].tick(ap_[2].tick(inpR, apG), apG);- pushPk = std::max(pushPk, std::max(std::fabs(inpL), std::fabs(inpR)));- bufL_.push(inpL);- bufR_.push(inpR);-- // ---- output stage: width, bass mono, mix, trim ----- float outWetL = rdL, outWetR = rdR;- if (width != 1.0f || monoOn) {- float mid = (outWetL + outWetR) * 0.5f, side = (outWetL - outWetR) * 0.5f * width;- if (monoOn) {- monoL_ += monoC * (side - monoL_);- side -= monoL_;+ inpL = ap_[1].tick(ap_[0].tick(inpL, apG), apG);+ inpR = ap_[3].tick(ap_[2].tick(inpR, apG), apG);+ pushPk = std::max(pushPk, std::max(std::fabs(inpL), std::fabs(inpR)));+ bufL_.push(inpL);+ bufR_.push(inpR);++ // ---- output stage: width, bass mono, mix, trim ----+ float outWetL = rdL, outWetR = rdR;+ if (width != 1.0f || monoOn) {+ float mid = (outWetL + outWetR) * 0.5f, side = (outWetL - outWetR) * 0.5f * width;+ if (monoOn) {+ monoL_ += monoC * (side - monoL_);+ side -= monoL_;+ }+ outWetL = mid + side;+ outWetR = mid - side;}- outWetL = mid + side;- outWetR = mid - side;+ if constexpr (decltype(withGraph)::value)+ hist_.push(std::max(std::fabs(l), std::fabs(r)), std::max(std::fabs(outWetL), std::fabs(outWetR)),+ (outWetL + outWetR) * 0.5f);+ const float oL = (dryG * l + wetG * outWetL) * outG;+ const float oR = (dryG * r + wetG * outWetR) * outG;+ buf[i * 2] = oL;+ buf[i * 2 + 1] = oR;++ wetPk = std::max(wetPk, std::fabs(outWetL * wetG));+ outPkL = std::max(outPkL, std::fabs(oL));+ outPkR = std::max(outPkR, std::fabs(oR));}- if (graph)- hist_.push(std::max(std::fabs(l), std::fabs(r)), std::max(std::fabs(outWetL), std::fabs(outWetR)),- (outWetL + outWetR) * 0.5f);- const float oL = (dryG * l + wetG * outWetL) * outG;- const float oR = (dryG * r + wetG * outWetR) * outG;- buf[i * 2] = oL;- buf[i * 2 + 1] = oR;-- wetPk = std::max(wetPk, std::fabs(outWetL * wetG));- outPkL = std::max(outPkL, std::fabs(oL));- outPkR = std::max(outPkR, std::fabs(oR));- }+ };+ if (graph) run(std::true_type{});+ else+ run(std::false_type{});// Loop-feed peak (decays slowly so a ringing tail blocks accrual); sleep// additionally waits out the loop's flight time so an in-flight echo isdiff --git a/src/native/nota.engine/src/devices/instruments/Consort.cpp b/src/native/nota.engine/src/devices/instruments/Consort.cppindex 4598887e..2b8e920e 100644--- a/src/native/nota.engine/src/devices/instruments/Consort.cpp+++ b/src/native/nota.engine/src/devices/instruments/Consort.cpp@@ -1142,8 +1142,19 @@ void Consort::renderVoice(Voice& V, int n, const VoiceCtl& c) {V.g2s = g;}// -- envelopes (RC curves; the attack aims past 1 like the analog charge — or shaped, #531) --- advanceRcEnv(V.fenv, V.fst, V.fsh, c.fS, fRc, c.fShape, fNative);- advanceRcEnv(V.aenv, V.ast, V.ash, c.aS, aRc, c.aShape, aNative);+ // The plain generator inline while the shape is native (the default); the shaped step otherwise.+ if (fNative) {+ advanceRcStep(V.fenv, V.fst, c.fS, fRc);+ V.fsh.p = -1.0f;+ } else {+ advanceRcEnv(V.fenv, V.fst, V.fsh, c.fS, fRc, c.fShape, false);+ }+ if (aNative) {+ advanceRcStep(V.aenv, V.ast, c.aS, aRc);+ V.ash.p = -1.0f;+ } else {+ advanceRcEnv(V.aenv, V.ast, V.ash, c.aS, aRc, c.aShape, false);+ }if (V.ast == 0 && !V.gate && V.fade == 0 && !V.retrig) { // asleep: stop computing this voiceV.active = false;V.fst = 0;@@ -1413,11 +1424,11 @@ void Consort::publishTelemetry() {tele_[ScBpm].store(tSpb_ > 1.0 ? static_cast<float>(60.0 * sampleRate_ / tSpb_) : 120.0f, std::memory_order_relaxed);tele_[ScLfo].store(lfoVal_, std::memory_order_relaxed);- const Voice& F = voices_[focus_];+ Voice& F = voices_[focus_];tele_[ScEnv1].store(F.active ? F.fenv : 0.0f, std::memory_order_relaxed);tele_[ScEnv2].store(F.active ? F.aenv : 0.0f, std::memory_order_relaxed);- publishRcEnvTele(tele_ + ScEnvTele, F.active, F.fst, F.fsh, F.fenv, pn_[FSustain].load(std::memory_order_relaxed));- publishRcEnvTele(tele_ + ScEnvTele + kEnvTeleFloats, F.active, F.ast, F.ash, F.aenv,+ publishRcEnvTele(tele_ + ScEnvTele, F.active, F.fst, &F.fsh, F.fenv, pn_[FSustain].load(std::memory_order_relaxed));+ publishRcEnvTele(tele_ + ScEnvTele + kEnvTeleFloats, F.active, F.ast, &F.ash, F.aenv,pn_[ASustain].load(std::memory_order_relaxed));if (st == 3) { // the four most recent sounding voicesint idx[kOsc] = {-1, -1, -1, -1};diff --git a/src/native/nota.engine/src/devices/instruments/EnvShape.h b/src/native/nota.engine/src/devices/instruments/EnvShape.hindex 663c30ce..0bb27d86 100644--- a/src/native/nota.engine/src/devices/instruments/EnvShape.h+++ b/src/native/nota.engine/src/devices/instruments/EnvShape.h@@ -18,6 +18,8 @@#pragma once+#include "dsp/core/RtAttributes.h"+#include <algorithm>#include <atomic>#include <cmath>@@ -80,7 +82,7 @@ template <class Stage> inline float shapedEnvProgress(Stage st, const EnvShapeSt// set the state's progress to −1 after it (as stepEnv does), so a bend that comes back later// in the stage starts from the level instead of an old progress.template <class Stage>-inline void advanceStraightEnv(float& env, Stage& st, float atk, float dec, float sus, float rel) {+NOTA_ALWAYS_INLINE void advanceStraightEnv(float& env, Stage& st, float atk, float dec, float sus, float rel) {switch (st) {case Stage::Attack:env += atk;@@ -210,7 +212,7 @@ inline void advanceShapedEnv(float& env, Stage& st, EnvShapeState& sh, float atk// one. A stage bent while a note plays starts its bend from the level it has reached (the// straight step marks the progress unknown, so the shaped one never reuses an old one).template <class Stage>-inline void stepEnv(float& env, Stage& st, EnvShapeState& sh, float atk, float dec, float sus, float rel, const EnvBends& k) {+NOTA_ALWAYS_INLINE void stepEnv(float& env, Stage& st, EnvShapeState& sh, float atk, float dec, float sus, float rel, const EnvBends& k) {if (k.straight) {advanceStraightEnv(env, st, atk, dec, sus, rel);sh.p = -1.0f; // its progress is the level's now: a bend that comes back starts from here@@ -278,6 +280,32 @@ inline float rcEnvProgress(float env, const EnvShapeState& sh, float sus) {}}+// One sample of the RC generator alone (st: 0 idle · 1 attack · 2 decay / sustain · 3 release),+// small enough to inline into a voice loop. Callers that take it while the shape is native+// (rcEnvNative, decided once per block) set the state's progress to −1 after it: the graphs+// note the stage once per block (publishRcEnvTele), and a shape that comes back starts from+// the level.+NOTA_ALWAYS_INLINE void advanceRcStep(float& env, int& st, float sus, const RcEnvCoefs& c) {+ switch (st) {+ case 1:+ env += (c.aTgt - env) * c.aC;+ if (env >= 1.0f) {+ env = 1.0f;+ st = 2;+ }+ break;+ case 2: env += (sus - env) * c.dC; break;+ case 3:+ env -= env * c.rC;+ if (env < c.offFloor) {+ env = 0.0f;+ st = 0;+ }+ break;+ default: break;+ }+}+// `native` is rcEnvNative(p), which callers can work out once per block.inline void advanceRcEnv(float& env, int& st, EnvShapeState& sh, float sus, const RcEnvCoefs& c, const EnvShapeParams& p,bool native) {@@ -331,11 +359,21 @@ inline void advanceRcEnv(float& env, int& st, EnvShapeState& sh, float sus, cons}// The telemetry stage of an RC envelope (EnvShape.h's code: 0 A · 1 D · 2 S · 3 R · 4 none)-// and its progress; `env` and `sus` are the envelope's level and sustain.-inline void publishRcEnvTele(std::atomic<float>* out, bool sounding, int st, const EnvShapeState& sh, float env, float sus) {- const int code = !sounding || st == 0 ? 4 : st == 1 ? 0 : st == 2 ? (sh.last == 2 ? 2 : 1) : 3;+// and its progress; `sh` is the shown envelope's shape state (null: none sounding), `env` and+// `sus` its level and sustain. advanceRcStep doesn't track its stage, so a stage it entered is+// noted here, once per block: the progress starts at most one block late.+inline void publishRcEnvTele(std::atomic<float>* out, bool sounding, int st, EnvShapeState* sh, float env, float sus) {+ int code = 4;+ if (sounding && sh && st != 0) {+ code = st == 1 ? 0 : st == 2 ? (sh->last == 2 || std::fabs(env - sus) < 1e-3f ? 2 : 1) : 3;+ if (code != sh->last) {+ if (!(code == 2 && sh->last == 1)) sh->from = env; // decay → sustain keeps the decay's start+ sh->last = code;+ sh->p = -1.0f;+ }+ }out[0].store(static_cast<float>(code), std::memory_order_relaxed);- out[1].store(code == 4 ? 0.0f : rcEnvProgress(env, sh, sus), std::memory_order_relaxed);+ out[1].store(code == 4 ? 0.0f : rcEnvProgress(env, *sh, sus), std::memory_order_relaxed);}} // namespace notadiff --git a/src/native/nota.engine/src/devices/instruments/Pentad.cpp b/src/native/nota.engine/src/devices/instruments/Pentad.cppindex b4b01fc5..e8261a3d 100644--- a/src/native/nota.engine/src/devices/instruments/Pentad.cpp+++ b/src/native/nota.engine/src/devices/instruments/Pentad.cpp@@ -746,8 +746,19 @@ void Pentad::renderVoice(Voice& V, int n, const VoiceCtl& c) {// -- glide (exponential, in semitones) --V.pitch += (V.pitchTgt - V.pitch) * c.glideC;// -- envelopes (RC curves; attack aims past 1 like the analog charge — or shaped, #531) --- advanceRcEnv(V.fenv, V.fst, V.fsh, c.fS, fRc, c.fShape, fNative);- advanceRcEnv(V.aenv, V.ast, V.ash, c.aS, aRc, c.aShape, aNative); // the amp's floor ≈ −90 dB+ // The plain generator inline while the shape is native (the default); the shaped step otherwise.+ if (fNative) {+ advanceRcStep(V.fenv, V.fst, c.fS, fRc);+ V.fsh.p = -1.0f;+ } else {+ advanceRcEnv(V.fenv, V.fst, V.fsh, c.fS, fRc, c.fShape, false);+ }+ if (aNative) {+ advanceRcStep(V.aenv, V.ast, c.aS, aRc); // the amp's floor ≈ −90 dB+ V.ash.p = -1.0f;+ } else {+ advanceRcEnv(V.aenv, V.ast, V.ash, c.aS, aRc, c.aShape, false);+ }if (V.ast == 0 && V.fade == 0) { // asleep: stop computing this voiceV.active = false;V.gate = false;@@ -933,12 +944,12 @@ void Pentad::publishTelemetry(int32_t /*frames*/) {tele_[ScRate].store(static_cast<float>(sampleRate_), std::memory_order_relaxed);for (int v = 0; v < kMaxVoices; ++v)tele_[ScVoices + v].store(voices_[v].active ? voices_[v].level : 0.0f, std::memory_order_relaxed);- const Voice* nv = nullptr;- for (const Voice& V : voices_)+ Voice* nv = nullptr;+ for (Voice& V : voices_)if (V.active && (!nv || V.order > nv->order)) nv = &V;- publishRcEnvTele(tele_ + ScEnvTele, nv != nullptr, nv ? nv->fst : 0, nv ? nv->fsh : EnvShapeState{}, nv ? nv->fenv : 0.0f,+ publishRcEnvTele(tele_ + ScEnvTele, nv != nullptr, nv ? nv->fst : 0, nv ? &nv->fsh : nullptr, nv ? nv->fenv : 0.0f,pn_[FSustain].load(std::memory_order_relaxed));- publishRcEnvTele(tele_ + ScEnvTele + kEnvTeleFloats, nv != nullptr, nv ? nv->ast : 0, nv ? nv->ash : EnvShapeState{},+ publishRcEnvTele(tele_ + ScEnvTele + kEnvTeleFloats, nv != nullptr, nv ? nv->ast : 0, nv ? &nv->ash : nullptr,nv ? nv->aenv : 0.0f, pn_[ASustain].load(std::memory_order_relaxed));}diff --git a/src/native/nota.engine/src/devices/instruments/TwinVoice.h b/src/native/nota.engine/src/devices/instruments/TwinVoice.hindex f4b4e4c4..d45973a6 100644--- a/src/native/nota.engine/src/devices/instruments/TwinVoice.h+++ b/src/native/nota.engine/src/devices/instruments/TwinVoice.h@@ -181,7 +181,8 @@ struct Env {}// The stage for the graphs (EnvShape.h's code) and its progress.int teleStage() const {- return st == Attack ? 0 : st == Decay ? (sh.last == 2 ? 2 : 1) : st == Sustain ? 2 : st == Release ? 3 : 4;+ if (st == Decay) return sh.last == 2 || std::fabs(lvl - sus) < 1e-3f ? 2 : 1;+ return st == Attack ? 0 : st == Sustain ? 2 : st == Release ? 3 : 4;}// The progress through the stage. The RC generator works it out here, when the graphs// or a change of shape ask, rather than on every sample.@@ -247,7 +248,9 @@ struct Env {case Decay:if (!native) return tickShaped();lvl += (sus - lvl) * dC;- if (std::fabs(lvl - sus) < 1e-3f) {+ // Only the loop needs to know the decay has arrived; the graphs tell it themselves+ // (teleStage), so the plain decay doesn't check it on every sample.+ if (loop && std::fabs(lvl - sus) < 1e-3f) {sh.last = 2; // the graph's sustainst = Sustain;loopAt();