8000 fix(fx): synth-voice ADSR envelope — kill fit-to-region stretching by johannboehme · Pull Request #23 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 82 additions & 16 deletions frontend/src/editor/fx/envelope.test.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,40 @@ describe("envelopeAt — edge cases", () => {
expect(envelopeAt(env(0, 0, 1, 0), 1.0, 1.5)).toBe(0);
});

it("compresses A+D to fit (region - R), keeping the release tail intact", () => {
// After release: regionDur = held + R. If A+D > held, A+D compress
// to fit `held`; R stays exactly R so the user's release fade plays
// out unmodified.
it("releases from partial-attack amplitude on quick tap (synth-voice)", () => {
// After release: regionDur = held + R. With A > held, attack does NOT
// compress to fit; it freezes mid-ramp and release fades from there.
// E.g. held 50 ms with A=100 ms → attack reached 0.5 at let-go; release
// fades 0.5 → 0 over R, NOT 1.0 → 0.
const e = env(0.1, 0, 1, 0.2);
const regionDur = 0.05 + 0.2; // held 50 ms + R=200 ms = 250 ms
// Mid-attack at 25 ms — A is compressed to 50 ms, so 25 ms is half.
expect(envelopeAt(e, regionDur, 0.025)).toBeCloseTo(0.5, 4);
// At the snapped release moment (50 ms): peak, release just starting.
expect(envelopeAt(e, regionDur, 0.05)).toBeCloseTo(1, 4);
// Halfway through release: 50 ms + 100 ms = 150 ms → wetness 0.5.
expect(envelopeAt(e, regionDur, 0.15)).toBeCloseTo(0.5, 4);
// Mid-attack at 25 ms (still inside attack ramp): 0.025 / 0.1 = 0.25.
expect(envelopeAt(e, regionDur, 0.025)).toBeCloseTo(0.25, 4);
// At the release moment (50 ms): attack only reached 0.5 — release
// starts from there, not from peak.
expect(envelopeAt(e, regionDur, 0.05)).toBeCloseTo(0.5, 4);
// Halfway through release (150 ms): 0.5 * (1 - 0.5) = 0.25.
expect(envelopeAt(e, regionDur, 0.15)).toBeCloseTo(0.25, 4);
});

it("stays silent when R alone exceeds the region (sub-R sliver)", () => {
// 100 ms region but R=200 → releaseStart = -100 ms. Release samples
// the natural envelope at t=0 (the earliest visible point), which
// for A>0 is 0 → effect plays silent. Synth analogue: tapped too
// briefly to make any sound.
const e = env(0.1, 0.05, 0.5, 0.2);
expect(envelopeAt(e, 0.1, 0)).toBeCloseTo(0, 5);
expect(envelopeAt(e, 0.1, 0.05)).toBeCloseTo(0, 5);
expect(envelopeAt(e, 0.1, 0.099)).toBeCloseTo(0, 3);
});

it("clamps R alone when even R exceeds the region (sub-R sliver)", () => {
// 100 ms region but R=200 → R clamps to 100, A/D zeroed.
const e = env(0.1, 0.05, 0.5, 0.2);
expect(envelopeAt(e, 0.1, 0)).toBeCloseTo(0.5, 5);
expect(envelopeAt(e, 0.1, 0.05)).toBeCloseTo(0.25, 5);
expect(envelopeAt(e, 0.1, 0.099)).toBeCloseTo(0.005, 3);
it("stays silent for sub-R sliver even when A=0 (release-start at t=0)", () => {
// With A=D=0 the natural curve is at S from t=0, so a sub-R sliver
// does play — as a fade from S down to 0 over the available time.
// Documents the boundary: silent only kicks in when A>0.
const e = env(0, 0, 0.5, 0.2);
expect(envelopeAt(e, 0.1, 0)).toBeCloseTo(0.25, 4); // S * (1 - 0.1/0.2)
expect(envelopeAt(e, 0.1, 0.05)).toBeCloseTo(0.125, 4); // S * (1 - 0.15/0.2)
});

it("clamps sustain level into [0, 1]", () => {
Expand All @@ -110,6 +124,58 @@ describe("envelopeAt — edge cases", () => {
});
});

describe("envelopeAt — holding (live pad press)", () => {
// While the user holds a pad, `tickFxHold` keeps `outS = playhead +
// overshoot`, so `regionDurS ≈ localT + overshoot`. Old behavior
// rescaled A and D against this moving target → attack collapsed to
// ≈ elapsed-time and reached ~1 in the first frame regardless of slider.
// Fix: while holding, A and D stay raw — the phase logic does the rest.

it("plays the user-set attack at full length while held (Vignette repro)", () => {
// attackS = 2.0 (slider max). 50 ms into hold, overshoot = 50 ms:
// regionDurS = 0.1, localT = 0.05. Expected: 0.05 / 2.0 = 0.025.
// Buggy old behavior would have returned ≈ 0.5 (after rescaling A→0.1).
const e = env(2.0, 0, 1, 0.3);
expect(envelopeAt(e, 0.1, 0.05, true)).toBeCloseTo(0.025, 4);
// 250 ms in → 0.25 / 2.0 = 0.125
expect(envelopeAt(e, 0.3, 0.25, true)).toBeCloseTo(0.125, 4);
// 1 s in → 0.5
expect(envelopeAt(e, 1.05, 1.0, true)).toBeCloseTo(0.5, 4);
// 2 s in → 1.0 (peak reached)
expect(envelopeAt(e, 2.05, 2.0, true)).toBeCloseTo(1, 4);
});

it("plays decay correctly while held", () => {
// A = 0.1, D = 0.2, S = 0.5. Halfway through decay (localT = 0.2):
// 1 + (S - 1) * ((localT - A) / D) = 1 + (-0.5) * 0.5 = 0.75.
const e = env(0.1, 0.2, 0.5, 0.3);
expect(envelopeAt(e, 1.0, 0.2, true)).toBeCloseTo(0.75, 4);
});

it("returns sustain level once past A+D while held", () => {
const e = env(0.1, 0.1, 0.6, 0.3);
expect(envelopeAt(e, 0.55, 0.5, true)).toBeCloseTo(0.6, 6);
expect(envelopeAt(e, 5.0, 4.0, true)).toBeCloseTo(0.6, 6);
});

it("never engages release while held — even with huge R near outS", () => {
// Synth-voice semantics: release only fires after let-go. While held,
// localT close to regionDurS must stay at sustain, not fade out.
const e = env(0, 0, 0.7, 1.0);
expect(envelopeAt(e, 1.0, 0.5, true)).toBeCloseTo(0.7, 6);
expect(envelopeAt(e, 1.0, 0.99, true)).toBeCloseTo(0.7, 6);
});

it("does not return 0 at or past outS while held", () => {
// Active-resolver pushes outS just ahead of playhead; the envelope
// sampler must not zero-out within the overshoot tail or the FX
// would flicker off mid-hold.
const e = env(0.05, 0, 1, 0.2);
expect(envelopeAt(e, 1.0, 1.0, true)).toBe(1);
expect(envelopeAt(e, 1.0, 1.5, true)).toBe(1);
});
});

describe("envelopeAt — INSTANT_ENVELOPE default", () => {
it("returns 1 for any localT in [0, regionDur)", () => {
expect(envelopeAt(INSTANT_ENVELOPE, 1.0, 0)).toBe(1);
Expand Down
91 changes: 40 additions & 51 deletions frontend/src/editor/fx/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* dass die Hard-Edges an `inS`/`outS` weich werden, ohne dass jeder Effekt-
* Kind eigene "off"-Definition kennt.
*
* Zeit-Encoding ist absolut (Sekunden). Wenn A+D+R die Region-Dauer
* übersteigt, werden alle drei proportional auf die Region skaliert
* (Sustain-Länge wird dann 0).
* Synth-Voice-Semantik: A, D, R bleiben unverändert wie der User sie
* gesetzt hat — keine Stauchung auf die Region. Tippt der User kürzer
* als A, friert die Attack mid-ramp ein und Release fadet vom erreichten
* Pegel (nicht vom Peak) auf 0. Wenn R länger ist als die Region, beginnt
* das Release effektiv vor t=0 → Effekt bleibt stumm (echter Synth: zu
* kurz angespielt zum Hören).
*/

export interface ADSREnvelope {
Expand Down Expand Up @@ -36,19 +39,24 @@ const clamp01 = (v: number): number => (v < 0 ? 0 : v > 1 ? 1 : v);
* Sampled die Hüllkurve bei lokaler Zeit `localT` (relativ zur Region-
* Start `inS`). Returns ∈ [0, 1].
*
* Phasen (zeitlich aufeinanderfolgend):
* [0, A) Attack: linear 0 → 1
* [A, A+D) Decay: linear 1 → S
* [A+D, regionDur-R) Sustain: konstant S
* [regionDur-R, regionDur) Release: linear S → 0
* >= regionDur Out: 0
* Phasen (zeitlich aufeinanderfolgend, A/D/R = User-Werte, ungestaucht):
* [0, A) Attack: linear 0 → 1
* [A, A+D) Decay: linear 1 → S
* [A+D, regionDurS - R) Sustain: konstant S
* [regionDurS - R, regionDurS) Release: linear releaseAmp → 0
* >= regionDurS Out: 0
*
* `holding`: while the user is still holding the pad/key for this fx,
* the release phase MUST NOT engage — the effect should hold at sustain
* level until they let go, like a synth voice. While held, `outS` is
* the live-extended end-of-region (overshoots the playhead by a few ms)
* which would otherwise put us inside the release window the entire
* time and the effect would never settle into sustain.
* `releaseAmp` ist die natürliche Hüllkurven-Amplitude am Moment des
* Release-Beginns (`regionDurS - R`). Tippt der User kürzer als A,
* bricht die Attack mid-ramp ab und Release fadet von dem Teilpegel —
* exakt wie eine Synth-Voice. Sub-R-Sliver (regionDurS < R, üblicherweise
* nur durch manuelles Trimmen) lässt releaseStart < 0 werden und die
* Hüllkurve sampled am Punkt 0 → 0 → Effekt stumm.
*
* `holding`: solange der User den Pad/Key hält, läuft nur die natürliche
* Kurve (Attack→Decay→Sustain), Release engagiert nicht. Wichtig, weil
* während des Haltens `outS` ein paar ms vor den Playhead geschoben wird
* (siehe store.tickFxHold) — sonst säßen wir permanent im Release-Window.
*/
export function envelopeAt(
env: ADSREnvelope,
Expand All @@ -60,44 +68,25 @@ export function envelopeAt(
if (localT < 0) return 0;
if (!holding && localT >= regionDurS) return 0;

let A = env.attackS;
let D = env.decayS;
let R = env.releaseS;
const A = env.attackS;
const D = env.decayS;
const R = env.releaseS;
const S = clamp01(env.sustain);

// Fit-to-region. Release is the protected slot — the user has just
// let go and explicitly asked for a fade-out, so R stays R as much as
// possible. Attack + Decay get compressed to fit the remaining space.
// Only when even R alone exceeds the region (a sub-R sliver) do we
// clamp R itself and zero out A/D.
if (!holding) {
if (R >= regionDurS) {
R = regionDurS;
A = 0;
D = 0;
} else {
const adSpace = regionDurS - R;
const adSum = A + D;
if (adSum > adSpace && adSum > 0) {
const k = adSpace / adSum;
A *= k;
D *= k;
}
}
} else {
// Holding → only A+D matter (release isn't sampled).
const sum = A + D;
if (sum > regionDurS && sum > 0) {
const k = regionDurS / sum;
A *= k;
D *= k;
}
}
// Natürliche Hüllkurven-Amplitude bei lokaler Zeit `t` — ohne Release.
// Hierfür werden A und D NICHT auf die Region gestaucht; eine kurz
// angespielte Note bricht ihre Attack mid-ramp ab.
const ampAt = (t: number): number => {
if (t < A) return A > 0 ? t / A : 1;
if (t < A + D) return D > 0 ? 1 + (S - 1) * ((t - A) / D) : S;
return S;
};

if (holding) return ampAt(localT);

if (localT < A) return A > 0 ? localT / A : 1;
if (localT < A + D) return D > 0 ? 1 + (S - 1) * ((localT - A) / D) : S;
if (holding) return S;
const releaseStart = regionDurS - R;
if (localT < releaseStart) return S;
return R > 0 ? S * (1 - (localT - releaseStart) / R) : 0;
if (localT < releaseStart) return ampAt(localT);
if (R <= 0) return 0;
const releaseAmp = ampAt(Math.max(0, releaseStart));
return releaseAmp * (1 - (localT - releaseStart) / R);
}
Loading
0