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: live graphs no longer cost CPU when nobody looks at them (#600)
Egor Khindikaynend1dae9bc3ab4
diff --git a/src/native/nota.engine/src/devices/effects/Delay.cpp b/src/native/nota.engine/src/devices/effects/Delay.cppindex b164ef88..6a8d80a3 100644--- a/src/native/nota.engine/src/devices/effects/Delay.cpp+++ b/src/native/nota.engine/src/devices/effects/Delay.cpp@@ -176,7 +176,7 @@ void Delay::process(float* buf, int32_t frames) {// so the wake-up echo has no timing jump. Sleep engages only after the// loop's full flight time (an echo can arrive long after the input and the// output both read silent), tracked below via loopPk_/quietSamples_.- hist_.begin(); // a gap since the last block (track muted or asleep) scrolls the graph on+ const bool graph = hist_.begin(); // kept only while the card reads it; a gap scrolls it onconst bool inSilent = simd_audio::isSilent(buf, frames * 2);if (inSilent && !freeze && idle_) {wowPhase_ += wowInc * frames;@@ -255,8 +255,9 @@ void Delay::process(float* buf, int32_t frames) {outWetL = mid + side;outWetR = mid - side;}- hist_.push(std::max(std::fabs(l), std::fabs(r)), std::max(std::fabs(outWetL), std::fabs(outWetR)),- (outWetL + outWetR) * 0.5f);+ 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;diff --git a/src/native/nota.engine/src/devices/effects/Reverb.cpp b/src/native/nota.engine/src/devices/effects/Reverb.cppindex 91c208b5..88957116 100644--- a/src/native/nota.engine/src/devices/effects/Reverb.cpp+++ b/src/native/nota.engine/src/devices/effects/Reverb.cpp@@ -161,7 +161,7 @@ void Reverb::process(float* buf, int32_t frames) {// so the wake-up tail has no timing jump. Sleep engages only after the full// feed-through flight time (pre-delay + early taps + longest comb), tracked// below — output silence alone proves nothing while the lines are full.- hist_.begin(); // a gap since the last block (track muted or asleep) scrolls the graph on+ const bool graph = hist_.begin(); // kept only while the card reads it; a gap scrolls it onconst bool inSilent = simd_audio::isSilent(buf, frames * 2);if (inSilent && !freeze && idle_) {modPhase_ += modInc * frames;@@ -248,7 +248,8 @@ void Reverb::process(float* buf, int32_t frames) {}outL = mid + side;outR = mid - side;- hist_.push(std::max(std::fabs(l), std::fabs(r)), std::max(std::fabs(outL), std::fabs(outR)), mid);+ if (graph)+ hist_.push(std::max(std::fabs(l), std::fabs(r)), std::max(std::fabs(outL), std::fabs(outR)), mid);// Final guard: a runaway (but finite) loop build-up here would blast the// speakers. A hard clamp bounds it but the clip itself sounds metallic, sodiff --git a/src/native/nota.engine/src/devices/instruments/BassSynth.cpp b/src/native/nota.engine/src/devices/instruments/BassSynth.cppindex cf1e6231..f42ea07e 100644--- a/src/native/nota.engine/src/devices/instruments/BassSynth.cpp+++ b/src/native/nota.engine/src/devices/instruments/BassSynth.cpp@@ -272,12 +272,12 @@ void BassSynth::render(float* out, int32_t frames) {float mono = 0.0f;for (auto& v : voices_) {if (!v.active) continue;- advanceShapedEnv(v.aEnv, v.aStage, v.aShape, aA, aD, aS, aR, aBend);+ stepEnv(v.aEnv, v.aStage, v.aShape, aA, aD, aS, aR, aBend);if (v.aStage == Stage::Off) {v.active = false;continue;}- advanceShapedEnv(v.fEnv, v.fStage, v.fShape, fA, fD, fS, fR, fBend);+ stepEnv(v.fEnv, v.fStage, v.fShape, fA, fD, fS, fR, fBend);if (glideCoef < 1.0) v.freqCur += (v.freqTarget - v.freqCur) * glideCoef;else@@ -322,10 +322,10 @@ void BassSynth::render(float* out, int32_t frames) {out[i * 2 + 1] += o * panR;}{- const Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;+ Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;const bool on = lv && lv->active;- publishEnvTele(envTele_, on, on ? lv->aStage : Stage::Off, on ? lv->aShape : EnvShapeState{});- publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->fStage : Stage::Off, on ? lv->fShape : EnvShapeState{});+ publishEnvTele(envTele_, on, on ? lv->aStage : Stage::Off, on ? &lv->aShape : nullptr, on ? lv->aEnv : 0.0f, aS);+ publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->fStage : Stage::Off, on ? &lv->fShape : nullptr, on ? lv->fEnv : 0.0f, fS);}// Sleep: with no sounding voice the loop above accumulated exact zeros.idle_ = true;diff --git a/src/native/nota.engine/src/devices/instruments/EnvShape.h b/src/native/nota.engine/src/devices/instruments/EnvShape.hindex c8a71f51..49a0bb6d 100644--- a/src/native/nota.engine/src/devices/instruments/EnvShape.h+++ b/src/native/nota.engine/src/devices/instruments/EnvShape.h@@ -27,7 +27,9 @@ namespace nota {// Per-voice, per-envelope state beside the level and the stage the instrument already keeps.struct EnvShapeState {- float p = 0.0f; // progress through the current stage, 0..1 (telemetry reads it too)+ // Progress through the current stage, 0..1. A straight stage leaves it at −1 and it is+ // worked out from the level when asked (shapedEnvProgress), not on every sample.+ float p = 0.0f;float from = 0.0f; // the level the stage started fromint last = -1; // the stage seen on the previous step (-1: re-arm on the next step)};@@ -42,13 +44,16 @@ struct EnvShapeParams {// The bends k (−1..1) of attack, decay and release for a params set.struct EnvBends {float a = 0.0f, d = 0.0f, r = 0.0f;+ bool straight = true; // every k = 0: stepEnv takes the plain step (decided once per block)};inline EnvBends envBends(const EnvShapeParams& s) {constexpr float kExpBend = 0.6f; // how far Exp bends each stage toward the RC curveconst float e = s.exp >= 0.5f ? kExpBend : 0.0f;auto k = [e](float n) { return std::clamp((n - 0.5f) * 2.0f + e, -1.0f, 1.0f); };- return {e, k(s.decayCurve), k(s.releaseCurve)};+ EnvBends b{e, k(s.decayCurve), k(s.releaseCurve)};+ b.straight = b.a == 0.0f && b.d == 0.0f && b.r == 0.0f;+ return b;}inline float envCurve(float p, float k) {@@ -57,6 +62,49 @@ inline float envCurve(float p, float k) {return k > 0.0f ? 1.0f - std::pow(1.0f - p, e) : std::pow(p, e);}+// The progress through a stage the straight step left at −1, from the level `env`.+template <class Stage> inline float shapedEnvProgress(Stage st, const EnvShapeState& sh, float env, float sus) {+ if (sh.p >= 0.0f) return sh.p;+ auto frac = [](float done, float span) { return span > 1e-6f ? std::clamp(done / span, 0.0f, 1.0f) : 1.0f; };+ switch (st) {+ case Stage::Attack: return frac(env - sh.from, 1.0f - sh.from);+ case Stage::Decay: return frac(sh.from - env, sh.from - sus);+ case Stage::Release: return frac(sh.from - env, sh.from);+ default: return 0.0f;+ }+}++// One sample of a straight ADSR: the old `env ±= rate` step, and nothing else. It leaves the+// shape state alone — a few instructions per envelope per sample add up on a voice of four+// operators — so the graphs note the stage once per block instead (publishEnvTele).+template <class Stage>+inline void advanceStraightEnv(float& env, Stage& st, float atk, float dec, float sus, float rel) {+ switch (st) {+ case Stage::Attack:+ env += atk;+ if (env >= 1.0f) {+ env = 1.0f;+ st = Stage::Decay;+ }+ break;+ case Stage::Decay:+ env -= dec;+ if (env <= sus) {+ env = sus;+ st = Stage::Sustain;+ }+ break;+ case Stage::Release:+ env -= rel;+ if (env <= 0.0f) {+ env = 0.0f;+ st = Stage::Off;+ }+ break;+ default: break;+ }+}+// One sample of the ADSR. `Stage` is the instrument's own enum class with the members// Attack, Decay, Sustain, Release and Off.template <class Stage>@@ -68,20 +116,26 @@ inline void advanceShapedEnv(float& env, Stage& st, EnvShapeState& sh, float atksh.p = 0.0f;sh.from = env;}+ // A bent stage steps its progress; one that was straight until now carries on from its level.+ auto resume = [&] {+ if (sh.p < 0.0f) sh.p = shapedEnvProgress(st, sh, env, sus);+ };switch (st) {case Stage::Attack: {const float span = 1.0f - sh.from;if (k.a == 0.0f) {env += atk;+ sh.p = -1.0f;if (env >= 1.0f) {env = 1.0f;st = Stage::Decay;+ sh.p = 1.0f;}- sh.p = span > 1e-6f ? std::clamp((env - sh.from) / span, 0.0f, 1.0f) : 1.0f;} else if (span <= 1e-6f) {env = 1.0f;st = Stage::Decay;} else {+ resume();sh.p += atk / span;if (sh.p >= 1.0f) {env = 1.0f;@@ -96,15 +150,17 @@ inline void advanceShapedEnv(float& env, Stage& st, EnvShapeState& sh, float atkconst float span = sh.from - sus;if (k.d == 0.0f) {env -= dec;+ sh.p = -1.0f;if (env <= sus) {env = sus;st = Stage::Sustain;+ sh.p = 1.0f;}- sh.p = span > 1e-6f ? std::clamp((sh.from - env) / span, 0.0f, 1.0f) : 1.0f;} else if (span <= 1e-6f) {env = sus;st = Stage::Sustain;} else {+ resume();sh.p += dec / span;if (sh.p >= 1.0f) {env = sus;@@ -120,15 +176,17 @@ inline void advanceShapedEnv(float& env, Stage& st, EnvShapeState& sh, float atkconst float span = sh.from;if (k.r == 0.0f) {env -= rel;+ sh.p = -1.0f;if (env <= 0.0f) {env = 0.0f;st = Stage::Off;+ sh.p = 1.0f;}- sh.p = span > 1e-6f ? std::clamp((sh.from - env) / span, 0.0f, 1.0f) : 1.0f;} else if (span <= 1e-6f) {env = 0.0f;st = Stage::Off;} else {+ resume();sh.p += rel / span;if (sh.p >= 1.0f) {env = 0.0f;@@ -143,15 +201,37 @@ inline void advanceShapedEnv(float& env, Stage& st, EnvShapeState& sh, float atk}}+// One sample: the plain step while every stage is straight (the default), else the shaped+// one. A stage bent while a note plays starts its bend from the level it has reached.+template <class Stage>+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);+ else+ advanceShapedEnv(env, st, sh, atk, dec, sus, rel, k);+}+// Telemetry for the editor's envelope graphs: two floats per envelope — the stage// (0 A · 1 D · 2 S · 3 R · 4 none sounding) and the progress through it.constexpr int kEnvTeleFloats = 2;+// `sh` is the shown envelope's shape state (null: none sounding), `env` its level and `sus`+// its sustain. A straight envelope steps without its shape state, so a stage it entered is+// noted here, once per block: the graph's progress starts at most one block late.template <class Stage>-inline void publishEnvTele(std::atomic<float>* out, bool sounding, Stage st, const EnvShapeState& sh) {- const bool on = sounding && st != Stage::Off;+inline void publishEnvTele(std::atomic<float>* out, bool sounding, Stage st, EnvShapeState* sh, float env, float sus) {+ const bool on = sounding && sh && st != Stage::Off;+ float p = 0.0f;+ if (on) {+ const int s = static_cast<int>(st);+ if (s != sh->last) {+ sh->last = s;+ sh->p = -1.0f;+ sh->from = env;+ }+ p = shapedEnvProgress(st, *sh, env, sus);+ }out[0].store(on ? static_cast<float>(static_cast<int>(st)) : 4.0f, std::memory_order_relaxed);- out[1].store(on ? sh.p : 0.0f, std::memory_order_relaxed);+ out[1].store(p, std::memory_order_relaxed);}inline int32_t readEnvTele(const std::atomic<float>* in, int n, float* out, int32_t maxN) {@@ -228,7 +308,6 @@ inline void advanceRcEnv(float& env, int& st, EnvShapeState& sh, float sus, cons}return;}- if (sh.p < 0.0f) sh.p = rcEnvProgress(env, sh, sus); // carry on from where the generator wasS s = st == 1 ? S::Attack : st == 2 ? (sh.last == 2 ? S::Sustain : S::Decay) : st == 3 ? S::Release : S::Off;advanceShapedEnv(env, s, sh, c.atkRate, c.decRate, sus, c.relRate, envBends(p));if (s == S::Sustain) {diff --git a/src/native/nota.engine/src/devices/instruments/Monolith.cpp b/src/native/nota.engine/src/devices/instruments/Monolith.cppindex 84a6b625..9456f703 100644--- a/src/native/nota.engine/src/devices/instruments/Monolith.cpp+++ b/src/native/nota.engine/src/devices/instruments/Monolith.cpp@@ -252,8 +252,8 @@ void Monolith::render(float* out, int32_t frames) {int active = 0;for (int32_t i = 0; i < frames; ++i) {- advanceShapedEnv(aEnv_, aStage_, aShape_, aA, aD, aS, aRel, aBend);- advanceShapedEnv(fEnv_, fStage_, fShape_, fA, fD, fS, fRel, fBend);+ stepEnv(aEnv_, aStage_, aShape_, aA, aD, aS, aRel, aBend);+ stepEnv(fEnv_, fStage_, fShape_, fA, fD, fS, fRel, fBend);// A held note follows the Sustain knobs live (automation included), as Monolith's// own envelope always did; the shared step leaves the sustain level alone.if (aStage_ == Stage::Sustain) aEnv_ = aS;@@ -341,8 +341,8 @@ void Monolith::render(float* out, int32_t frames) {}if (aStage_ != Stage::Off) active = 1;active_.store(active, std::memory_order_relaxed);- publishEnvTele(envTele_, true, fStage_, fShape_);- publishEnvTele(envTele_ + kEnvTeleFloats, true, aStage_, aShape_);+ publishEnvTele(envTele_, true, fStage_, &fShape_, fEnv_, fS);+ publishEnvTele(envTele_ + kEnvTeleFloats, true, aStage_, &aShape_, aEnv_, aS);// Sleep: amp contour Off with nothing held means amp is exactly zero.idle_ = (aStage_ == Stage::Off && heldN_ == 0);}diff --git a/src/native/nota.engine/src/devices/instruments/Mosaic.cpp b/src/native/nota.engine/src/devices/instruments/Mosaic.cppindex 32748815..dc618141 100644--- a/src/native/nota.engine/src/devices/instruments/Mosaic.cpp+++ b/src/native/nota.engine/src/devices/instruments/Mosaic.cpp@@ -433,7 +433,7 @@ void Mosaic::render(float* out, int32_t frames) {if (v.relTrig) { // a release sample plays through on its own levelif (v.env < 1.0f) v.env = std::min(1.0f, v.env + atkRateRel_);} else {- advanceShapedEnv(v.env, v.stage, v.shape, atkRate, decRate, sustain, relRate, bend);+ stepEnv(v.env, v.stage, v.shape, atkRate, decRate, sustain, relRate, bend);if (v.stage == Stage::Off) v.active = false;}if (!v.active) break;@@ -772,7 +772,7 @@ void Mosaic::publishTelemetry(const float* out, int32_t frames, int nActive, doufloat peak = 0.0f;for (int32_t i = 0; i < frames * 2; ++i) peak = std::max(peak, std::abs(out[i]));int held = 0, sus = 0, rel = 0;- const Voice* best = nullptr;+ Voice* best = nullptr;int n = 0;uint32_t keys[8] = {};for (auto& v : voices_) {@@ -814,7 +814,7 @@ void Mosaic::publishTelemetry(const float* out, int32_t frames, int nActive, doutele_[11].store(best && fType > 0 ? static_cast<float>(best->cutoff) : -1.0f, std::memory_order_relaxed);tele_[12].store(best ? static_cast<float>(best->zone) : -1.0f, std::memory_order_relaxed);publishEnvTele(tele_ + kScEnv, best != nullptr && !best->relTrig, best ? best->stage : Stage::Off,- best ? best->shape : EnvShapeState{});+ best ? &best->shape : nullptr, best ? best->env : 0.0f, get(Sustain));tele_[13].store(static_cast<float>(lastNote_), std::memory_order_relaxed);tele_[14].store(static_cast<float>(std::lround(lastVel_ * 127.0f)), std::memory_order_relaxed);tele_[15].store(static_cast<float>(n), std::memory_order_relaxed);diff --git a/src/native/nota.engine/src/devices/instruments/OperatorSynth.cpp b/src/native/nota.engine/src/devices/instruments/OperatorSynth.cppindex 45fa2d3d..32179957 100644--- a/src/native/nota.engine/src/devices/instruments/OperatorSynth.cpp+++ b/src/native/nota.engine/src/devices/instruments/OperatorSynth.cpp@@ -225,6 +225,7 @@ void OperatorSynth::render(float* out, int32_t frames) {const int sb = ShapeBase + o * 3;bend[o] = envBends({get((Param)sb), get((Param)(sb + 1)), get((Param)(sb + 2))});}+ const bool allStraight = bend[0].straight && bend[1].straight && bend[2].straight && bend[3].straight;const int ftype = std::clamp((int)std::lround(get(FilterType) * 2.0f), 0, 2);const double fbase = expMap(get(FilterFreq), 60.0, 18000.0);const double fk = std::clamp(2.0 - 1.9 * get(FilterReso), 0.05, 2.0);@@ -284,7 +285,10 @@ void OperatorSynth::render(float* out, int32_t frames) {int nc = 0;bool anyOn = false;for (int o = 0; o < 4; ++o) {- advanceShapedEnv(v.env[o], v.stage[o], v.shape[o], (float)atk[o], (float)dec[o], (float)sus[o], (float)rel[o], bend[o]);+ // Loop-invariant (a block's shapes), so the compiler keeps the straight loop as lean as it was.+ if (allStraight) advanceStraightEnv(v.env[o], v.stage[o], (float)atk[o], (float)dec[o], (float)sus[o], (float)rel[o]);+ else+ advanceShapedEnv(v.env[o], v.stage[o], v.shape[o], (float)atk[o], (float)dec[o], (float)sus[o], (float)rel[o], bend[o]);if (v.stage[o] != Stage::Off) anyOn = true;double pmod = modIn[o];if (o == 0 && fb > 0.0) pmod += fb * v.fbLast;@@ -335,11 +339,11 @@ void OperatorSynth::render(float* out, int32_t frames) {for (auto& v : voices_)if (v.active) ++live;{- const Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;+ Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;const bool on = lv && lv->active;for (int o = 0; o < 4; ++o)publishEnvTele(envTele_ + o * kEnvTeleFloats, on, on ? lv->stage[o] : Stage::Off,- on ? lv->shape[o] : EnvShapeState{});+ on ? &lv->shape[o] : nullptr, on ? lv->env[o] : 0.0f, (float)sus[o]);}if (live == 0 && peak <= 1.0e-5f) {quietSamples_ += (uint64_t)frames;diff --git a/src/native/nota.engine/src/devices/instruments/PhysicalSynth.cpp b/src/native/nota.engine/src/devices/instruments/PhysicalSynth.cppindex af7d6e2d..8596819f 100644--- a/src/native/nota.engine/src/devices/instruments/PhysicalSynth.cpp+++ b/src/native/nota.engine/src/devices/instruments/PhysicalSynth.cpp@@ -265,7 +265,7 @@ void PhysicalSynth::render(float* out, int32_t frames) {if (v.mEnv < 1.0e-4f) v.mActive = false;}if (v.nStage != Stage::Off) {- advanceShapedEnv(v.nEnv, v.nStage, v.nShape, nA, nD, nS, nR, nBend);+ stepEnv(v.nEnv, v.nStage, v.nShape, nA, nD, nS, nR, nBend);// A burst with no sustain ends at the end of its decay, so a quiet voice// can be freed (noteOff never moves the noise stage on).if (v.nStage == Stage::Sustain && nS <= 1.0e-4f) {@@ -323,9 +323,9 @@ void PhysicalSynth::render(float* out, int32_t frames) {if (v.active && !v.choke) ++n;activeVoices_.store(n, std::memory_order_relaxed);{- const Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;+ Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;const bool on = lv && lv->active;- publishEnvTele(envTele_, on, on ? lv->nStage : Stage::Off, on ? lv->nShape : EnvShapeState{});+ publishEnvTele(envTele_, on, on ? lv->nStage : Stage::Off, on ? &lv->nShape : nullptr, on ? lv->nEnv : 0.0f, nS);}// Peak hold for the telemetry: ~300 ms fall.const float fall = static_cast<float>(std::exp(-static_cast<double>(frames) / (0.3 * sampleRate_)));diff --git a/src/native/nota.engine/src/devices/instruments/Sampler.cpp b/src/native/nota.engine/src/devices/instruments/Sampler.cppindex 60996127..f03cf32c 100644--- a/src/native/nota.engine/src/devices/instruments/Sampler.cpp+++ b/src/native/nota.engine/src/devices/instruments/Sampler.cpp@@ -242,7 +242,7 @@ void Sampler::render(float* out, int32_t frames) {}}// envelope- advanceShapedEnv(v.env, v.stage, v.shape, atkRate, decRate, sustain, relRate, bend);+ stepEnv(v.env, v.stage, v.shape, atkRate, decRate, sustain, relRate, bend);if (v.stage == Stage::Off) v.active = false;if (!v.active) break;@@ -293,7 +293,7 @@ void Sampler::render(float* out, int32_t frames) {}for (int32_t i = 0; i < frames * 2; ++i) peak = std::max(peak, std::abs(out[i]));// Publish the loudest active voice + the voice count for the UI.- const Voice* best = nullptr;+ Voice* best = nullptr;int still = 0;for (auto& v : voices_)if (v.active) {@@ -310,7 +310,7 @@ void Sampler::render(float* out, int32_t frames) {tele_[5].store(best && fType > 0 ? static_cast<float>(best->cutoff) : -1.0f, std::memory_order_relaxed);if (peak > tele_[6].load(std::memory_order_relaxed)) tele_[6].store(peak, std::memory_order_relaxed);tele_[7].store(static_cast<float>(heldN_), std::memory_order_relaxed);- publishEnvTele(tele_ + 8, best != nullptr, best ? best->stage : Stage::Off, best ? best->shape : EnvShapeState{});+ publishEnvTele(tele_ + 8, best != nullptr, best ? best->stage : Stage::Off, best ? &best->shape : nullptr, best ? best->env : 0.0f, sustain);// Auto-sleep: a looping voice stays active until note-off, so no active voice plus// our own silent block means every tail (incl. release) has fully decayed.diff --git a/src/native/nota.engine/src/devices/instruments/TwinVoice.h b/src/native/nota.engine/src/devices/instruments/TwinVoice.hindex 742c1e4e..f4b4e4c4 100644--- a/src/native/nota.engine/src/devices/instruments/TwinVoice.h+++ b/src/native/nota.engine/src/devices/instruments/TwinVoice.h@@ -186,7 +186,7 @@ struct Env {// 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.float progress() const {- if (!native) return sh.p;+ if (!native && sh.p >= 0.0f) return sh.p; // a bent stage keeps it; a straight one leaves −1switch (sh.last) {case 0: return sh.from < 1.0f ? std::clamp((lvl - sh.from) / (1.0f - sh.from), 0.0f, 1.0f) : 1.0f;case 1:diff --git a/src/native/nota.engine/src/devices/instruments/VoltSynth.cpp b/src/native/nota.engine/src/devices/instruments/VoltSynth.cppindex c11dcc37..15d358d4 100644--- a/src/native/nota.engine/src/devices/instruments/VoltSynth.cpp+++ b/src/native/nota.engine/src/devices/instruments/VoltSynth.cpp@@ -438,12 +438,12 @@ void VoltSynth::render(float* out, int32_t frames) {float outL = 0.0f, outR = 0.0f;for (auto& v : voices_) {if (!v.active) continue;- advanceShapedEnv(v.aEnv, v.aStage, v.aShape, aA, aD, aS, aR, aBend);+ stepEnv(v.aEnv, v.aStage, v.aShape, aA, aD, aS, aR, aBend);if (v.aStage == Stage::Off) {v.active = false;continue;}- advanceShapedEnv(v.fEnv, v.fStage, v.fShape, fA, fD, fS, fR, fBend);+ stepEnv(v.fEnv, v.fStage, v.fShape, fA, fD, fS, fR, fBend);if (v.fade1 < 1.0f) v.fade1 = std::min(1.0f, v.fade1 + fade1Inc);if (v.fade2 < 1.0f) v.fade2 = std::min(1.0f, v.fade2 + fade2Inc);@@ -582,10 +582,10 @@ void VoltSynth::render(float* out, int32_t frames) {}activeVoices_.store(countActive(), std::memory_order_relaxed);{- const Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;+ Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;const bool on = lv && lv->active;- publishEnvTele(envTele_, on, on ? lv->aStage : Stage::Off, on ? lv->aShape : EnvShapeState{});- publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->fStage : Stage::Off, on ? lv->fShape : EnvShapeState{});+ publishEnvTele(envTele_, on, on ? lv->aStage : Stage::Off, on ? &lv->aShape : nullptr, on ? lv->aEnv : 0.0f, aS);+ publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->fStage : Stage::Off, on ? &lv->fShape : nullptr, on ? lv->fEnv : 0.0f, fS);}// Sleep: with no sounding voice the loop above accumulated exact zeros.idle_ = (countActive() == 0);diff --git a/src/native/nota.engine/src/devices/instruments/WavetableSynth.cpp b/src/native/nota.engine/src/devices/instruments/WavetableSynth.cppindex 890299e3..5aa6c388 100644--- a/src/native/nota.engine/src/devices/instruments/WavetableSynth.cpp+++ b/src/native/nota.engine/src/devices/instruments/WavetableSynth.cpp@@ -710,12 +710,12 @@ void WavetableSynth::render(float* out, int32_t frames) {int live = 0;for (auto& v : voices_) {if (!v.active) continue;- advanceShapedEnv(v.env, v.stage, v.shape, atk, dec, sus, rel, bend1);+ stepEnv(v.env, v.stage, v.shape, atk, dec, sus, rel, bend1);if (v.stage == Stage::Off) {v.active = false;continue;}- advanceShapedEnv(v.env2, v.stage2, v.shape2, atk2, dec2, sus2, rel2, bend2);+ stepEnv(v.env2, v.stage2, v.shape2, atk2, dec2, sus2, rel2, bend2);++live;double dst[kDests] = {@@ -865,10 +865,10 @@ void WavetableSynth::render(float* out, int32_t frames) {scope_[1].store(follow[1], std::memory_order_relaxed);scope_[4].store((float)live, std::memory_order_relaxed);{- const Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;+ Voice* lv = lastVoice_ >= 0 ? &voices_[lastVoice_] : nullptr;const bool on = lv && lv->active;- publishEnvTele(envTele_, on, on ? lv->stage : Stage::Off, on ? lv->shape : EnvShapeState{});- publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->stage2 : Stage::Off, on ? lv->shape2 : EnvShapeState{});+ publishEnvTele(envTele_, on, on ? lv->stage : Stage::Off, on ? &lv->shape : nullptr, on ? lv->env : 0.0f, sus);+ publishEnvTele(envTele_ + kEnvTeleFloats, on, on ? lv->stage2 : Stage::Off, on ? &lv->shape2 : nullptr, on ? lv->env2 : 0.0f, sus2);}if (live == 0 && peak <= 1.0e-5f) {quietSamples_ += (uint64_t)frames;diff --git a/src/native/nota.engine/src/dsp/primitives/LevelHistory.h b/src/native/nota.engine/src/dsp/primitives/LevelHistory.hindex 052948b4..a0596917 100644--- a/src/native/nota.engine/src/dsp/primitives/LevelHistory.h+++ b/src/native/nota.engine/src/dsp/primitives/LevelHistory.h@@ -11,6 +11,10 @@// publish, filling with silence, so an old hit scrolls away instead of standing still. Stale// means no publish for three blocks (at least 60 ms), so a large audio buffer — 4096 frames is// 85 ms at 48 kHz — still reads as live between its callbacks.+// Only kept while something reads it: read() stamps the time, and once no read has come for+// kWatchNs (the card closed, the device scrolled out of view) begin() says so and the device+// skips the history — its per-sample work costs nothing on a track nobody is looking at. The+// next read opens it again; the gap reads as silence, as any other gap does.// Fixed size, no allocation, no lock.#pragma once@@ -33,6 +37,7 @@ class LevelHistory {static constexpr int kFloats = 2 * kPoints + kSpecN;static constexpr float kFloorDb = -120.0f;static constexpr int64_t kStaleNs = 60'000'000; // the least idle time that means no blocks run+ static constexpr int64_t kWatchNs = 1'000'000'000; // no read for this long: nobody is lookingvoid setSampleRate(double sr) {sr_ = sr > 0.0 ? sr : 48000.0;@@ -51,6 +56,7 @@ class LevelHistory {sA_.store(0, std::memory_order_relaxed);pubNs_.store(0, std::memory_order_relaxed);staleNs_.store(kStaleNs, std::memory_order_relaxed);+ watched_ = false;}// One sample: the input and wet magnitudes (any stereo fold the caller likes) and the mono wet.@@ -63,21 +69,28 @@ class LevelHistory {if (++cnt_ >= bucket_) close();}- // At the top of every process(): if the engine stopped calling the device for a while (a- // muted or settled track), first scroll the stored rings on by that gap in silence, so the+ // At the top of every process(). Returns whether the history is kept this block (a read+ // came within kWatchNs); when it isn't, the device skips push() and the rest do nothing.+ // If the engine stopped calling the device for a while (a muted or settled track, or a+ // card that was closed), first scroll the stored rings on by that gap in silence, so the// history read() showed moving on stays moved on instead of the old audio coming back.- void begin() {+ bool begin() {+ const int64_t now = nowNs();+ watched_ = now - readNs_.load(std::memory_order_relaxed) < kWatchNs;+ if (!watched_) return false;const int64_t pub = pubNs_.load(std::memory_order_relaxed);- if (pub <= 0) return;- const int64_t gap = nowNs() - pub;- if (gap <= staleNs_.load(std::memory_order_relaxed)) return;+ if (pub <= 0) return true;+ const int64_t gap = now - pub;+ if (gap <= staleNs_.load(std::memory_order_relaxed)) return true;const double frames = std::min(static_cast<double>(gap) * 1e-9 * sr_, static_cast<double>(kPoints + 1) * bucket_);fill(static_cast<int32_t>(frames));since_ = 0;+ return true;}// A block the device skipped (asleep on silence): the graph keeps scrolling through it.void silence(int32_t frames) {+ if (!watched_) return;fill(frames);since_ += frames;publish();@@ -100,6 +113,7 @@ class LevelHistory {public:void publish() {+ if (!watched_) return;// Three of the blocks the device sees, so the gap between two callbacks never reads as stale.const auto blockNs = static_cast<int64_t>(static_cast<double>(since_) / sr_ * 1e9);staleNs_.store(std::max(kStaleNs, 3 * blockNs), std::memory_order_relaxed);@@ -114,6 +128,7 @@ class LevelHistory {// written (0 when out is too small).int32_t read(float* out, int32_t room) const {if (!out || room < kFloats) return 0;+ readNs_.store(nowNs(), std::memory_order_relaxed); // someone is looking: keep the historyconst int64_t pub = pubNs_.load(std::memory_order_acquire);const int w = wA_.load(std::memory_order_relaxed), s = sA_.load(std::memory_order_relaxed);const int64_t idle = pub > 0 ? nowNs() - pub : 0;@@ -153,6 +168,8 @@ class LevelHistory {float pkIn_ = 0.0f, pkWet_ = 0.0f;std::atomic<int> wA_{0}, sA_{0};std::atomic<int64_t> pubNs_{0}, staleNs_{kStaleNs};+ mutable std::atomic<int64_t> readNs_{INT64_MIN / 2}; // the last read (UI thread); never read: not watched+ bool watched_ = false; // this block keeps the history (audio thread)static_assert(std::atomic<float>::is_always_lock_free, "the audio thread must not lock");};diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.csindex b71559dc..7d751a4d 100644--- a/tests/Nota.SmokeTest/Program.cs+++ b/tests/Nota.SmokeTest/Program.cs@@ -4869,11 +4869,17 @@ Console.WriteLine("-- Nota Delay --");// Telemetry + the MCP status line.var sc = new float[11];de.DeviceSetParam(dt, dd, Sync, 1f); de.DeviceSetParam(dt, dd, DivL, 2f / 7f);+ // The level history is kept only while a card reads it: everything rendered so far went+ // unread, so there is none; this read opens it, as an open card's refresh does.+ var dh = new float[11 + 2 * 1536 + 2048];+ de.DeviceScope(dt, dd, dh, dh.Length);+ float dUnread = -999;+ for (int i = 0; i < 2 * 1536; i++) dUnread = Math.Max(dUnread, dh[11 + i]);+ Check(dUnread <= -119, $"Delay keeps no level history while no card reads it ({dUnread:0} dB)");Render(20000);int scn = de.DeviceScope(dt, dd, sc, sc.Length);Check(scn == 11 && sc[4] > 1000 && sc[5] > 1, $"Delay scope reports sample rate + tempo ({sc[4]:0} Hz, {sc[5]:0.0} BPM)");// The live graph's level history follows the telemetry: input dB, echoes dB, echo samples.- var dh = new float[11 + 2 * 1536 + 2048];int dhn = de.DeviceScope(dt, dd, dh, dh.Length);float dIn = -999, dWet = -999;for (int i = 0; i < 1536; i++) { dIn = Math.Max(dIn, dh[11 + i]); dWet = Math.Max(dWet, dh[11 + 1536 + i]); }@@ -5176,6 +5182,8 @@ Console.WriteLine("-- Nota Reverb --");re.DeviceSetParam(rt, rd, DryWet, 1f);re.DeviceSetParam(rt, rd, PreDelay, 0.25f); // 50 ms, room to absorb the ~7 ms diffuserre.DeviceSetParam(rt, rd, LatencyComp, 1f);+ var rh = new float[11 + 2 * 1536 + 2048];+ re.DeviceScope(rt, rd, rh, rh.Length); // the card opens: the level history is kept from here onvar comped = Render(20000);re.DeviceSetParam(rt, rd, LatencyComp, 0f);var raw = Render(20000);@@ -5189,7 +5197,6 @@ Console.WriteLine("-- Nota Reverb --");Check(scn == 11 && sc[4] > 1000 && sc[8] > 100 && Math.Abs(sc[6] - 1.9) < 0.1,$"Reverb scope reports sample rate, diffuser delay and RT60 ({sc[4]:0} Hz, {sc[8]:0} smp, {sc[6]:0.00} s)");// The live graph's level history follows the telemetry: input dB, tail dB, tail samples.- var rh = new float[11 + 2 * 1536 + 2048];int rhn = re.DeviceScope(rt, rd, rh, rh.Length);float rIn = -999, rWet = -999;for (int i = 0; i < 1536; i++) { rIn = Math.Max(rIn, rh[11 + i]); rWet = Math.Max(rWet, rh[11 + 1536 + i]); }