Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

(fix) RC envelopes: a held note follows a raised Sustain after a switch to Lin (#600)

Egor Khindikaynen45e7281b8598
diff --git a/src/native/nota.engine/src/devices/instruments/EnvShape.h b/src/native/nota.engine/src/devices/instruments/EnvShape.h
index 0bb27d86..9c2ad92e 100644
--- a/src/native/nota.engine/src/devices/instruments/EnvShape.h
+++ b/src/native/nota.engine/src/devices/instruments/EnvShape.h
@@ -345,6 +345,11 @@ inline void advanceRcEnv(float& env, int& st, EnvShapeState& sh, float sus, cons
}
return;
}
+ // The plain generator (advanceRcStep, Twin's inline step) doesn't note when a voice reaches
+ // its sustain, and a Sustain raised above a held level isn't a decay at all: a voice in the
+ // decay / sustain stage whose level is not above the sustain follows it as the sustain does,
+ // instead of being taken for a decay that lands on the new level at once.
+ if (st == 2 && sh.last != 2 && env <= sus + 1e-3f) sh.last = 2;
S 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/tests/Nota.SmokeTest/RcEnvSustainTests.cs b/tests/Nota.SmokeTest/RcEnvSustainTests.cs
index 3d94a417..0a00b282 100644
--- a/tests/Nota.SmokeTest/RcEnvSustainTests.cs
+++ b/tests/Nota.SmokeTest/RcEnvSustainTests.cs
@@ -24,6 +24,63 @@ internal static class RcEnvSustainTests
yield return (before > 1e-3 && db < -6,
$"Consort, {name}: a held note follows Sustain 0.9 → 0.15 ({before:0.0000} → {after:0.0000}, {db:0.0} dB)");
}
+ // The generator doesn't note that a voice reached its sustain; switching to the shaped
+ // step (Lin) right after Sustain went up must not take the held note for a decay that
+ // lands on the new level at once (review of #600).
+ var (held, first) = RaisedThenLin();
+ double jump = 20 * Math.Log10(Math.Max(first, 1e-9) / Math.Max(held, 1e-9));
+ yield return (held > 1e-3 && jump < 3,
+ $"Pentad: Sustain 0.2 → 0.8 then Lin on held notes rises smoothly, no jump ({held:0.0000} → {first:0.0000}, {jump:+0.0;-0.0} dB over the next 0.1 s)");
+ }
+
+ private static NotaEngine Instrument(int kind, out int t, out Action<string, float> set)
+ {
+ var e = new NotaEngine();
+ e.SetMetronome(false);
+ var buf = new float[Block * 2];
+ e.RenderOffline(buf, Block, Sr);
+ int tr = e.AddInstrumentTrack();
+ e.SetTrackBuiltinInstrument(tr, kind);
+ t = tr;
+ set = (id, v) =>
+ {
+ for (int i = 0; i < e.PluginParamCount(tr, -1); i++)
+ if (e.PluginParamId(tr, -1, i) == id) { e.PluginParamSet(tr, -1, i, v); return; }
+ throw new InvalidOperationException("no param " + id);
+ };
+ return e;
+ }
+
+ private static double Rms(NotaEngine e, int blocks)
+ {
+ var buf = new float[Block * 2];
+ double s = 0; int n = 0;
+ for (int b = 0; b < blocks; b++)
+ {
+ e.RenderOffline(buf, Block, Sr);
+ foreach (var x in buf) { s += x * x; n++; }
+ }
+ return Math.Sqrt(s / n);
+ }
+
+ // RMS of two notes held at Sustain 0.2, then over the 0.1 s after Sustain goes to 0.8 (with
+ // a slow decay) and the envelope switches to Lin.
+ private static (double held, double first) RaisedThenLin()
+ {
+ using var e = Instrument(14, out int t, out var P); // Pentad: the graphs track only its newest voice
+ P("aattack", 0f); P("adecay", 0.05f); P("asustain", 0.2f); P("arelease", 0.3f);
+ P("adcurve", 0.5f); P("aenvexp", 1f);
+ e.TrackNoteOn(t, 57, 0.9f);
+ e.TrackNoteOn(t, 64, 0.9f); // the graphs show this one; 57 is a voice they never track
+ Rms(e, 90); // both notes sit at Sustain 0.2, on the generator
+ double held = Rms(e, 1);
+ P("adecay", 0.9f); // a slow decay: the level climbs to a raised Sustain slowly
+ P("asustain", 0.8f);
+ P("aenvexp", 0f); // Lin: the shaped step from here on
+ double first = Rms(e, 10); // ~0.1 s: the slow decay has barely moved
+ e.TrackNoteOff(t, 57);
+ e.TrackNoteOff(t, 64);
+ return (held, first);
}
// RMS of a held note settled at Sustain 0.9, then after Sustain drops to 0.15.