FA2: bounded-schematic Gate envelope (15% sustain reserve, in-bounds release) + zero-fade-out grabbable via nearest-node hit-test; all nodes clamp in-canvas

This commit is contained in:
2026-07-27 18:25:03 -04:00
parent e2bd4f4351
commit d5d1902ea4
6 changed files with 392 additions and 121 deletions
+33 -8
View File
@@ -23,6 +23,15 @@ double secondsPerPixel(const Rect& area, double totalSeconds) {
return totalSeconds / static_cast<double>(w); return totalSeconds / static_cast<double>(w);
} }
// Seconds per pixel in the GATE timed region (FA2): the Gate schematic maps A/H/D/R onto
// gateTimedWidth(area) px, not the full canvas, so a Gate time-node drag must use this scale for
// the handle to track the cursor. Matches envelope_overlay::gatePolyline.
double gateSecondsPerPixel(const Rect& area, double totalSeconds) {
const int w = gateTimedWidth(area);
if (w <= 0 || totalSeconds <= 0.0) return 0.0;
return totalSeconds / static_cast<double>(w);
}
// Level (0..1) represented by one vertical pixel. levelToY spans (height-1) rows for [0,1], so one // Level (0..1) represented by one vertical pixel. levelToY spans (height-1) rows for [0,1], so one
// pixel is 1/(height-1). Zero when degenerate. Matches envelope_overlay::levelToY. // pixel is 1/(height-1). Zero when degenerate. Matches envelope_overlay::levelToY.
double levelPerPixel(const Rect& area) { double levelPerPixel(const Rect& area) {
@@ -46,14 +55,23 @@ bool isDraggable(EnvNode n) {
NodeHit nodeAtPoint(const AmpEnvelope& env, const Rect& area, double totalSeconds, int x, int y) { NodeHit nodeAtPoint(const AmpEnvelope& env, const Rect& area, double totalSeconds, int x, int y) {
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, area, totalSeconds); const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, area, totalSeconds);
// First-match in draw order (deterministic tie-break), skipping non-draggable anchors. // NEAREST draggable node within the pick radius wins (Chebyshev distance — the square grab
// box); ties break to the earlier draw-order node (FA2). Nearest-wins keeps every handle
// grabbable when nodes sit close (e.g. a short hold), while the draw-order tie-break makes
// exactly-coincident nodes deterministic: at zero fade-out, FadeOutStart overlays LengthEnd
// and WINS the tie, so the fade-out handle is grabbable at the right edge and can be dragged
// inward from zero.
NodeHit best;
int bestDist = kNodeGrabRadius + 1;
for (const EnvVertex& v : poly) { for (const EnvVertex& v : poly) {
if (!isDraggable(v.node)) continue; if (!isDraggable(v.node)) continue;
if (std::abs(x - v.x) <= kNodeGrabRadius && std::abs(y - v.y) <= kNodeGrabRadius) { const int dist = std::max(std::abs(x - v.x), std::abs(y - v.y));
return NodeHit{true, v.node}; if (dist < bestDist) { // strictly closer only: earlier draw order keeps ties
bestDist = dist;
best = NodeHit{true, v.node};
} }
} }
return NodeHit{false, EnvNode::Origin}; return best;
} }
AmpEnvelope resolveNodeDrag(const AmpEnvelope& grabEnv, EnvNode node, const Rect& area, AmpEnvelope resolveNodeDrag(const AmpEnvelope& grabEnv, EnvNode node, const Rect& area,
@@ -65,21 +83,28 @@ AmpEnvelope resolveNodeDrag(const AmpEnvelope& grabEnv, EnvNode node, const Rect
const double secPerPx = secondsPerPixel(area, totalSeconds); const double secPerPx = secondsPerPixel(area, totalSeconds);
if (secPerPx <= 0.0) return out; // degenerate area / duration — no motion if (secPerPx <= 0.0) return out; // degenerate area / duration — no motion
const double dSec = static_cast<double>(dxPixels) * secPerPx; const double dSec = static_cast<double>(dxPixels) * secPerPx;
// Gate time nodes live in the TIMED region of the Gate schematic (FA2), which is narrower
// than the canvas by the sustain-plateau reserve — their px->seconds scale differs so the
// dragged handle tracks the cursor 1:1. gateTimedWidth >= 1 whenever the area is non-empty,
// so gateDSec is well-defined past the degenerate guard above.
const double gateDSec =
static_cast<double>(dxPixels) * gateSecondsPerPixel(area, totalSeconds);
switch (node) { switch (node) {
// --- Gate: each cumulative-time node edits its OWN segment duration. Non-negative // --- Gate: each cumulative-time node edits its OWN segment duration. Non-negative
// durations ARE the monotonic-in-time guarantee (a node can never cross a neighbour // durations ARE the monotonic-in-time guarantee (a node can never cross a neighbour
// because every segment stays >= 0), so the [0, max] clamp is the whole constraint. // because every segment stays >= 0), so the [0, max] clamp is the whole constraint.
case EnvNode::AttackEnd: case EnvNode::AttackEnd:
out.attackSeconds = clamp(grabEnv.attackSeconds + dSec, 0.0, bounds.maxAttackSeconds); out.attackSeconds =
clamp(grabEnv.attackSeconds + gateDSec, 0.0, bounds.maxAttackSeconds);
break; break;
case EnvNode::HoldEnd: case EnvNode::HoldEnd:
out.holdSeconds = clamp(grabEnv.holdSeconds + dSec, 0.0, bounds.maxHoldSeconds); out.holdSeconds = clamp(grabEnv.holdSeconds + gateDSec, 0.0, bounds.maxHoldSeconds);
break; break;
case EnvNode::DecayEnd: { case EnvNode::DecayEnd: {
// Sustain node: X sets decay time, Y sets sustain level (drag DOWN = higher y = lower // Sustain node: X sets decay time, Y sets sustain level (drag DOWN = higher y = lower
// level, so subtract the level delta). // level, so subtract the level delta).
out.decaySeconds = clamp(grabEnv.decaySeconds + dSec, 0.0, bounds.maxDecaySeconds); out.decaySeconds = clamp(grabEnv.decaySeconds + gateDSec, 0.0, bounds.maxDecaySeconds);
const double lvlPerPx = levelPerPixel(area); const double lvlPerPx = levelPerPixel(area);
const double dLevel = -static_cast<double>(dyPixels) * lvlPerPx; const double dLevel = -static_cast<double>(dyPixels) * lvlPerPx;
out.sustainLevel = clamp(grabEnv.sustainLevel + dLevel, 0.0, 1.0); out.sustainLevel = clamp(grabEnv.sustainLevel + dLevel, 0.0, 1.0);
@@ -87,7 +112,7 @@ AmpEnvelope resolveNodeDrag(const AmpEnvelope& grabEnv, EnvNode node, const Rect
} }
case EnvNode::ReleaseEnd: case EnvNode::ReleaseEnd:
out.releaseSeconds = out.releaseSeconds =
clamp(grabEnv.releaseSeconds + dSec, 0.0, bounds.maxReleaseSeconds); clamp(grabEnv.releaseSeconds + gateDSec, 0.0, bounds.maxReleaseSeconds);
break; break;
// --- Trigger: fades + length are FRACTIONS. X pixels convert to a fraction of the PLAYED // --- Trigger: fades + length are FRACTIONS. X pixels convert to a fraction of the PLAYED
+12 -5
View File
@@ -20,11 +20,16 @@
// CALLER-SUPPLIED here (EnvClampBounds): the shell passes the same maxima it feeds the slider, // CALLER-SUPPLIED here (EnvClampBounds): the shell passes the same maxima it feeds the slider,
// so the two surfaces share one clamp by construction. // so the two surfaces share one clamp by construction.
// //
// WHICH AXES. Time-only nodes (attack-end, hold-end, release-end; fade-in-end, length-end, // WHICH AXES. Time-only nodes (AttackEnd, HoldEnd, ReleaseEnd; FadeInEnd, FadeOutStart,
// fade-out-end) drag on X only. The sustain node (DecayEnd) drags on BOTH axes — its X sets the // LengthEnd) drag on X only. The sustain node (DecayEnd) drags on BOTH axes — its X sets the
// decay time, its Y sets the sustain level (the standard ADSR-editor grammar). Origin and the // decay time, its Y sets the sustain level (the standard ADSR-editor grammar). Origin and the
// drawing-only ReleaseStart vertex are NOT draggable. // drawing-only ReleaseStart vertex are NOT draggable.
// //
// GATE DRAG SCALE (FA2). Gate time nodes live in the Gate schematic's TIMED region
// (gateTimedWidth(area) px — the canvas minus the sustain-plateau reserve), so their px->seconds
// conversion uses that width, not the full canvas; Trigger nodes keep the full-canvas scale.
// Both match the forward map in envelope_overlay, so a dragged handle tracks the cursor 1:1.
//
// Reuses editor_geometry's Rect + the EnvNode / AmpEnvelope / EnvMode types from // Reuses editor_geometry's Rect + the EnvNode / AmpEnvelope / EnvMode types from
// envelope_overlay (one shared node vocabulary across draw + edit), and the shared timeToX / // envelope_overlay (one shared node vocabulary across draw + edit), and the shared timeToX /
// levelToY maps so the handle the overlay drew and the grab region here agree pixel-for-pixel. // levelToY maps so the handle the overlay drew and the grab region here agree pixel-for-pixel.
@@ -64,9 +69,11 @@ struct EnvClampBounds {
// Which node a grab at (x, y) lands on, given the CURRENT envelope + overlay rect + sample // Which node a grab at (x, y) lands on, given the CURRENT envelope + overlay rect + sample
// duration (the same inputs buildEnvelopePolyline drew from, so the grab tests the drawn handles). // duration (the same inputs buildEnvelopePolyline drew from, so the grab tests the drawn handles).
// Returns EnvNode::Origin's NON-membership as a miss via the bool return: `hit` is false for a // Returns EnvNode::Origin's NON-membership as a miss via the bool return: `hit` is false for a
// point off every DRAGGABLE node. Origin and ReleaseStart are never returned (not draggable). On a // point off every DRAGGABLE node. Origin and ReleaseStart are never returned (not draggable).
// tie (two handles within the radius) the earlier draw-order node wins (deterministic, mirroring // The NEAREST node within the radius wins (Chebyshev distance); an exact tie goes to the earlier
// waveform_view::markerAtPoint's first-match). Pure. // draw-order node (FA2 — deterministic, and it makes coincident nodes grabbable: at zero
// fade-out, FadeOutStart overlays LengthEnd, wins the tie, and can be dragged inward from the
// right edge; at zero hold, AttackEnd wins over HoldEnd). Pure.
struct NodeHit { struct NodeHit {
bool hit = false; bool hit = false;
EnvNode node = EnvNode::Origin; // meaningful only when hit == true EnvNode node = EnvNode::Origin; // meaningful only when hit == true
+46 -17
View File
@@ -10,13 +10,22 @@ int timeToX(const Rect& area, double totalSeconds, double t) {
const int w = std::max(0, area.width()); const int w = std::max(0, area.width());
if (w <= 0 || totalSeconds <= 0.0) return area.left; if (w <= 0 || totalSeconds <= 0.0) return area.left;
if (t < 0.0) t = 0.0; if (t < 0.0) t = 0.0;
// Linear map, NOT clamped on the high side: t past totalSeconds maps past area.right (the Gate // Linear map, clamped on BOTH sides (FA2 bounds invariant): t past totalSeconds pins to the
// release tail, drawn after the sample end by design). Round to the nearest pixel. // last in-bounds column area.right-1. Round to the nearest pixel.
const double frac = t / totalSeconds; const double frac = t / totalSeconds;
const long xi = static_cast<long>(frac * static_cast<double>(w) + 0.5); long xi = static_cast<long>(frac * static_cast<double>(w) + 0.5);
if (xi > w - 1) xi = w - 1;
return area.left + static_cast<int>(xi); return area.left + static_cast<int>(xi);
} }
int gateTimedWidth(const Rect& area) {
const int w = std::max(0, area.width());
if (w <= 0) return 0;
const int sustainPx =
static_cast<int>(kGateSustainDisplayFraction * static_cast<double>(w) + 0.5);
return std::max(1, w - sustainPx);
}
int levelToY(const Rect& area, double level) { int levelToY(const Rect& area, double level) {
const int h = std::max(0, area.height()); const int h = std::max(0, area.height());
if (h <= 0) return area.top; if (h <= 0) return area.top;
@@ -46,6 +55,22 @@ EnvVertex vtx(EnvNode node, const Rect& area, double totalSeconds, double t, dou
return v; return v;
} }
// One Gate vertex from a pixel offset inside the area (the Gate schematic works in px space —
// timed px + the fixed sustain-plateau reserve — not through the plain timeToX map). Clamps x to
// the last in-bounds column (FA2 bounds invariant).
EnvVertex gateVtx(EnvNode node, const Rect& area, double px, double level) {
const int w = std::max(1, area.width());
long xi = static_cast<long>(px + 0.5);
if (xi < 0) xi = 0;
if (xi > w - 1) xi = w - 1;
EnvVertex v;
v.node = node;
v.x = area.left + static_cast<int>(xi);
v.y = levelToY(area, level);
v.level = level;
return v;
}
std::vector<EnvVertex> gatePolyline(const AmpEnvelope& env, const Rect& area, double totalSeconds) { std::vector<EnvVertex> gatePolyline(const AmpEnvelope& env, const Rect& area, double totalSeconds) {
// Non-negative segment durations (a stored negative would be an upstream bug; clamp defensively). // Non-negative segment durations (a stored negative would be an upstream bug; clamp defensively).
const double a = std::max(0.0, env.attackSeconds); const double a = std::max(0.0, env.attackSeconds);
@@ -54,23 +79,27 @@ std::vector<EnvVertex> gatePolyline(const AmpEnvelope& env, const Rect& area, do
const double r = std::max(0.0, env.releaseSeconds); const double r = std::max(0.0, env.releaseSeconds);
const double sus = clamp01(env.sustainLevel); const double sus = clamp01(env.sustainLevel);
// Cumulative wall-clock times of each breakpoint from t=0. // BOUNDED SCHEMATIC (FA2): A/H/D and R map onto the TIMED region (canvas minus the reserved
const double tAttack = a; // sustain-plateau width) at the sample's time scale; the sustain plateau is the fixed reserve
const double tHold = tAttack + h; // between DecayEnd and ReleaseStart. Cumulative px, clamped in gateVtx, stay monotonic.
const double tDecay = tHold + d; const int timedW = gateTimedWidth(area);
// The sustain plateau runs to the sample end; if the pre-sustain stages already overrun the const int sustainPx = std::max(0, area.width()) - timedW;
// sample, the plateau collapses to zero width (its end clamps up to tDecay). const double pxPerSec = static_cast<double>(timedW) / totalSeconds;
const double tSustainEnd = std::max(tDecay, totalSeconds);
const double tRelease = tSustainEnd + r; // release trails PAST the sample end, by design const double pxAttack = a * pxPerSec;
const double pxHold = (a + h) * pxPerSec;
const double pxDecay = (a + h + d) * pxPerSec;
const double pxPlateau = pxDecay + static_cast<double>(sustainPx); // schematic note-off
const double pxRelease = pxPlateau + r * pxPerSec;
std::vector<EnvVertex> pts; std::vector<EnvVertex> pts;
pts.reserve(6); pts.reserve(6);
pts.push_back(vtx(EnvNode::Origin, area, totalSeconds, 0.0, 0.0)); pts.push_back(gateVtx(EnvNode::Origin, area, 0.0, 0.0));
pts.push_back(vtx(EnvNode::AttackEnd, area, totalSeconds, tAttack, 1.0)); pts.push_back(gateVtx(EnvNode::AttackEnd, area, pxAttack, 1.0));
pts.push_back(vtx(EnvNode::HoldEnd, area, totalSeconds, tHold, 1.0)); pts.push_back(gateVtx(EnvNode::HoldEnd, area, pxHold, 1.0));
pts.push_back(vtx(EnvNode::DecayEnd, area, totalSeconds, tDecay, sus)); // sustain node pts.push_back(gateVtx(EnvNode::DecayEnd, area, pxDecay, sus)); // sustain node
pts.push_back(vtx(EnvNode::ReleaseStart, area, totalSeconds, tSustainEnd, sus)); // plateau end pts.push_back(gateVtx(EnvNode::ReleaseStart, area, pxPlateau, sus)); // plateau end
pts.push_back(vtx(EnvNode::ReleaseEnd, area, totalSeconds, tRelease, 0.0)); pts.push_back(gateVtx(EnvNode::ReleaseEnd, area, pxRelease, 0.0));
return pts; return pts;
} }
+66 -33
View File
@@ -4,16 +4,37 @@
// the DAW, while the editor shell (reasampler_editor.cpp) traces the polyline in an accent hue // the DAW, while the editor shell (reasampler_editor.cpp) traces the polyline in an accent hue
// and draws the node handles (via envelope_edit's hit-test). // and draws the node handles (via envelope_edit's hit-test).
// //
// WHAT IT DRAWS. The amp envelope over the Sample view's hero waveform at accurate wall-clock // WHAT IT DRAWS. The amp envelope over the Sample view's hero waveform (Simpler / Phase-Plant
// time (Simpler / Phase-Plant grammar): // grammar):
// * Gate -> the AHDSR shape: attack ramp 0->1, hold plateau at 1, decay 1->sustain, // * Gate -> the AHDSR shape: attack ramp 0->1, hold plateau at 1, decay 1->sustain,
// sustain plateau, release sustain->0. // sustain plateau, release sustain->0. Since there is no held note-off to draw
// against, Gate is a BOUNDED SCHEMATIC (FA2): a fixed fraction of the canvas
// width (kGateSustainDisplayFraction) is RESERVED for the sustain plateau, and
// the remaining "timed" width carries A/H/D AND the release at the sample's time
// scale — so A -> (H) -> D -> S-plateau -> R all render INSIDE the canvas and the
// release is a visible, draggable segment (it no longer trails past area.right).
// * Trigger -> the fade/%-length shape: fade-in 0->1, unity plateau, fade-out 1->0 anchored // * Trigger -> the fade/%-length shape: fade-in 0->1, unity plateau, fade-out 1->0 anchored
// to playEnd (= lengthFraction of the post-start span). // to playEnd (= lengthFraction of the post-start span). Trigger keeps the
// The horizontal axis is wall-clock TIME across the waveform rect; the vertical axis is LEVEL // waveform's exact time base so the shape lines up with the PCM under it.
// (0 at rect bottom, 1 at rect top). The overlay shares the waveform's time base so the drawn // The horizontal axis is TIME (Gate: schematic, see above; Trigger: wall-clock across the rect);
// shape lines up with the PCM under it: the same [0, frameCount] span waveform_view maps, so the // the vertical axis is LEVEL (0 at rect bottom, 1 at rect top).
// envelope's own duration is placed at the SAME frames the voice plays it over. //
// BOUNDS INVARIANT (FA2). EVERY vertex of EVERY polyline is clamped inside the canvas:
// x in [area.left, area.right-1], y in [area.top, area.bottom-1] (half-open rect convention).
// No node and no drawn segment ever exceeds the canvas — paint-time clipping of handles is no
// longer needed (and never fires) in the shell.
//
// FA2 CONTRACT CHANGE — WAVE B SHELL AUTHOR, READ THIS:
// * The EnvNode enum is UNCHANGED (same node set, same draggable set — Origin + ReleaseStart
// remain the only non-draggable anchors).
// * ALL vertices are now in-bounds (see above). The shell's previous "skip handle when
// v.x >= waveArea.right" clip is dead code: ReleaseEnd (Gate) and FadeOutStart/LengthEnd
// (Trigger, at full length / zero fade-out) now land at area.right-1 and MUST get handles.
// * Gate's x-axis is schematic, not PCM-aligned (the sustain reserve compresses the timed
// region); Trigger's x-axis is still PCM-aligned.
// * nodeAtPoint (envelope_edit) now resolves to the NEAREST node within the grab radius,
// draw-order tie-break — coincident nodes (zero fade-out at the right edge, zero hold) are
// deterministically grabbable.
// //
// DELIBERATELY ENGINE-FREE (house pattern — param_slider does the same). It does NOT depend on // DELIBERATELY ENGINE-FREE (house pattern — param_slider does the same). It does NOT depend on
// sample_map / sampler_core (which would drag bank_book / wav_trim in). The shell reads the // sample_map / sampler_core (which would drag bank_book / wav_trim in). The shell reads the
@@ -44,7 +65,8 @@ enum class EnvMode { Gate, Trigger };
// //
// Gate nodes: Origin -> AttackEnd -> HoldEnd -> DecayEnd(=sustain corner) -> ReleaseStart // Gate nodes: Origin -> AttackEnd -> HoldEnd -> DecayEnd(=sustain corner) -> ReleaseStart
// -> ReleaseEnd. The sustain node is DecayEnd (its Y is the sustain level); // -> ReleaseEnd. The sustain node is DecayEnd (its Y is the sustain level);
// ReleaseStart is a drawing-only plateau-end vertex. // ReleaseStart is a drawing-only plateau-end vertex (the schematic note-off);
// release is edited by dragging ReleaseEnd.
// Trigger nodes: Origin -> FadeInEnd -> FadeOutStart -> LengthEnd(playEnd, level 0). The fade-out // Trigger nodes: Origin -> FadeInEnd -> FadeOutStart -> LengthEnd(playEnd, level 0). The fade-out
// ramp is the FadeOutStart->LengthEnd segment; LengthEnd is the playEnd terminal. // ramp is the FadeOutStart->LengthEnd segment; LengthEnd is the playEnd terminal.
enum class EnvNode { enum class EnvNode {
@@ -55,7 +77,8 @@ enum class EnvNode {
// Y sets sustainLevel) // Y sets sustainLevel)
ReleaseStart, // Gate: end of the sustain plateau / start of the release (sustain level) — ReleaseStart, // Gate: end of the sustain plateau / start of the release (sustain level) —
// a DRAWING vertex only, not a draggable handle (release is edited at // a DRAWING vertex only, not a draggable handle (release is edited at
// ReleaseEnd; this vertex tracks its X = sample end, Y = sustain level) // ReleaseEnd; this vertex sits a fixed sustain-plateau width right of
// DecayEnd — the schematic note-off — Y = sustain level)
ReleaseEnd, // Gate: end of the release tail (level 0) — X sets releaseSeconds ReleaseEnd, // Gate: end of the release tail (level 0) — X sets releaseSeconds
FadeInEnd, // Trigger: top of the fade-in (level 1) — X sets fadeInFraction FadeInEnd, // Trigger: top of the fade-in (level 1) — X sets fadeInFraction
FadeOutStart, // Trigger: end of the unity plateau / start of the fade-out (level 1) — FadeOutStart, // Trigger: end of the unity plateau / start of the fade-out (level 1) —
@@ -121,36 +144,46 @@ struct EnvVertex {
} }
}; };
// The fraction of the canvas width RESERVED for the Gate sustain-plateau display (FA2). The
// plateau is a fixed-width schematic region between DecayEnd and ReleaseStart; the remaining
// width is the "timed" region A/H/D/R map onto at the sample's time scale. One constant shared
// by the forward map (here) and the inverse map (envelope_edit) so a drag tracks the cursor.
inline constexpr double kGateSustainDisplayFraction = 0.15;
// The pixel width of the Gate timed region: area.width() minus the sustain-plateau reserve,
// floored at 1 px so the px<->seconds scale never degenerates for a non-empty area. Returns 0
// for a zero/negative-width area. Shared by gatePolyline and envelope_edit's gate drag scale.
int gateTimedWidth(const Rect& area);
// Map an amp envelope to its polyline vertices inside `area`, over a sample of `totalSeconds` // Map an amp envelope to its polyline vertices inside `area`, over a sample of `totalSeconds`
// wall-clock duration. `area` is the waveform rect (left/top inclusive, right/bottom exclusive); // wall-clock duration. `area` is the waveform rect (left/top inclusive, right/bottom exclusive);
// x maps time 0..totalSeconds across [area.left, area.right], y maps level 0..1 across // y maps level 0..1 across [area.bottom-1 .. area.top] (level 1 at the TOP). The polyline reads
// [area.bottom-1 .. area.top] (level 1 at the TOP). The polyline reads left-to-right in draw // left-to-right in draw order, Origin first.
// order, Origin first.
// //
// TIME BASE. The envelope's own segment durations are placed on the SAME time axis the waveform // TIME BASE (FA2).
// occupies, so the curve lines up with the PCM: // * Gate: a bounded schematic. The canvas splits into a TIMED region of gateTimedWidth(area)
// * Gate: attack/hold/decay run from t=0; the sustain plateau runs to the note-off. Since the // px — where attack/hold/decay run from t=0 and the release ramp runs after the plateau, all
// overlay has no held note-off to draw against, the sustain plateau is drawn to the END of // at totalSeconds-over-timed-width scale — plus a FIXED sustain plateau of
// the sample (totalSeconds) and the release tail is drawn AFTER that boundary — i.e. the // (width - timedWidth) px between DecayEnd and ReleaseStart (the schematic note-off). Every
// release is appended past the sample end (the standard "release after key-up at end of // vertex x clamps to area.right-1, so when the stages overrun the visible span the trailing
// view" convention). When attack+hold+decay already exceed totalSeconds the plateau collapses // nodes pile up (still monotonic, still in-bounds, still draggable back left).
// to zero width (nodes clamp to the sample end) and release still trails past it. // * Trigger: the waveform's exact time base (PCM-aligned). The played span is
// * Trigger: the played span is lengthFraction * totalSeconds; fade-in/out are fractions OF // lengthFraction * totalSeconds; fade-in/out are fractions OF that played span. Nodes past
// that played span. Nodes past the played span never appear (LengthEnd/FadeOutEnd sit at the // the played span never appear (FadeOutStart/LengthEnd sit at the played span's right edge).
// played span's right edge).
// //
// A time beyond totalSeconds (the Gate release tail) maps past area.right — the shell clips at // BOUNDS: every vertex is inside the canvas — x in [area.left, area.right-1], y in
// paint time (the same way waveform_view lets a frame past the count pin the marker). A degenerate // [area.top, area.bottom-1]. Nothing maps past area.right (the pre-FA2 release tail is gone). A
// area (zero width/height) or totalSeconds <= 0 yields the two-point flat baseline [Origin, end at // degenerate area (zero width/height) or totalSeconds <= 0 yields the two-point flat baseline
// level 0] so the shell always has a drawable line. Pure — same inputs, same polyline. // [Origin, end at level 0] so the shell always has a drawable line. Pure — same inputs, same
// polyline.
std::vector<EnvVertex> buildEnvelopePolyline(const AmpEnvelope& env, const Rect& area, std::vector<EnvVertex> buildEnvelopePolyline(const AmpEnvelope& env, const Rect& area,
double totalSeconds); double totalSeconds);
// Map a time (seconds) to a pixel x inside `area`: t=0 -> area.left, t=totalSeconds -> area.right, // Map a time (seconds) to a pixel x inside `area`: t=0 -> area.left, t=totalSeconds ->
// linear. t is NOT clamped on the high side (a Gate release past the sample end maps past // area.right-1, linear, CLAMPED on both sides (t < 0 pins to area.left; t past totalSeconds pins
// area.right, by design — see buildEnvelopePolyline); t < 0 pins to area.left. A zero-width area // to area.right-1 — the in-bounds invariant, FA2). A zero-width area or totalSeconds <= 0 yields
// or totalSeconds <= 0 yields area.left. Pure — the shared time->x map both the polyline and the // area.left. Pure — the shared time->x map the Trigger polyline and the node hit-test
// node hit-test (envelope_edit) use, so the drawn handle and its grab region agree. // (envelope_edit) use, so the drawn handle and its grab region agree.
int timeToX(const Rect& area, double totalSeconds, double t); int timeToX(const Rect& area, double totalSeconds, double t);
// Map a level (0..1) to a pixel y inside `area`: level 1 -> area.top, level 0 -> area.bottom-1 // Map a level (0..1) to a pixel y inside `area`: level 1 -> area.top, level 0 -> area.bottom-1
+96 -25
View File
@@ -4,12 +4,14 @@
// boundaries (the load-bearing "a drag can never produce a param a slider couldn't" invariant). // boundaries (the load-bearing "a drag can never produce a param a slider couldn't" invariant).
// //
// Covers: nodeAtPoint (grabs a drawn handle within the pick radius; misses off every node; skips // Covers: nodeAtPoint (grabs a drawn handle within the pick radius; misses off every node; skips
// the non-draggable Origin/ReleaseStart anchors; first-match determinism); resolveNodeDrag Gate // the non-draggable Origin/ReleaseStart anchors; NEAREST-node-wins with draw-order tie-break —
// (each cumulative node edits its OWN segment; X->time, sustain node's Y->level; lower clamp at 0; // FA2); resolveNodeDrag Gate (each cumulative node edits its OWN segment at the TIMED-region px
// upper clamp at the caller's max; only the dragged param changes); resolveNodeDrag Trigger (fades // scale; X->time, sustain node's Y->level; lower clamp at 0; upper clamp at the caller's max;
// as fractions of the played span; fadeIn/fadeOut mutual clamp so they never cross; length clamp; // only the dragged param changes; ReleaseEnd grabbable + draggable — FA2); resolveNodeDrag
// FadeOutStart moves OPPOSITE the pixel delta); degenerate area/duration + non-draggable node -> // Trigger (fades as fractions of the played span; fadeIn/fadeOut mutual clamp so they never
// no motion. // cross; length clamp; FadeOutStart moves OPPOSITE the pixel delta; zero-fade-out node grabbable
// at the right edge and draggable inward — FA2); degenerate area/duration + non-draggable node
// -> no motion.
#include "../src/vst/envelope_edit.h" #include "../src/vst/envelope_edit.h"
@@ -24,9 +26,12 @@ static int g_fail = 0;
static bool near(double a, double b, double eps = 1e-9) { return std::fabs(a - b) <= eps; } static bool near(double a, double b, double eps = 1e-9) { return std::fabs(a - b) <= eps; }
// 1000px wide, 100px tall, offset origin. 2.0s total => 500 px/s => 0.002 s/px. // 1000px wide, 100px tall, offset origin. Trigger scale: 2.0s over 1000px => 0.002 s/px. Gate
// scale (FA2 schematic): 2.0s over the 850px TIMED region (150px sustain-plateau reserve) =>
// 2/850 s/px, and the gateEnv() nodes draw at A x@85, H x@128, D x@255, RS x@405, RE x@575.
static Rect wideArea() { return Rect{20, 10, 1020, 110}; } static Rect wideArea() { return Rect{20, 10, 1020, 110}; }
static constexpr double kTotal = 2.0; static constexpr double kTotal = 2.0;
static constexpr double kGateSecPerPx = kTotal / 850.0;
static AmpEnvelope gateEnv() { static AmpEnvelope gateEnv() {
AmpEnvelope e; AmpEnvelope e;
@@ -53,18 +58,18 @@ static AmpEnvelope triggerEnv() {
static void testHitGrabsDrawnHandle() { static void testHitGrabsDrawnHandle() {
const AmpEnvelope e = gateEnv(); const AmpEnvelope e = gateEnv();
const Rect a = wideArea(); const Rect a = wideArea();
// AttackEnd draws at x = left+100 (0.2s), y = top (level 1). A grab there hits it. // AttackEnd draws at x = left+85 (0.2s * 425 px/s in the timed region), y = top (level 1).
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 100, a.top); NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 85, a.top);
CHECK(h.hit && h.node == EnvNode::AttackEnd); CHECK(h.hit && h.node == EnvNode::AttackEnd);
// The sustain node (DecayEnd) at 0.6s -> left+300, level 0.5 -> ~top+50. // The sustain node (DecayEnd) at 0.6s -> left+255, level 0.5 -> ~top+50.
NodeHit s = nodeAtPoint(e, a, kTotal, a.left + 300, a.top + 50); NodeHit s = nodeAtPoint(e, a, kTotal, a.left + 255, a.top + 50);
CHECK(s.hit && s.node == EnvNode::DecayEnd); CHECK(s.hit && s.node == EnvNode::DecayEnd);
} }
static void testHitMissesOffEveryNode() { static void testHitMissesOffEveryNode() {
const AmpEnvelope e = gateEnv(); const AmpEnvelope e = gateEnv();
const Rect a = wideArea(); const Rect a = wideArea();
// A point far from any drawn handle (mid plateau, well away from a node). // A point far from any drawn handle (right of the release ramp, well away from a node).
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 700, a.top + 5); NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 700, a.top + 5);
CHECK(!h.hit); CHECK(!h.hit);
} }
@@ -75,22 +80,42 @@ static void testHitSkipsNonDraggableAnchors() {
// Origin draws at (left, bottom-1). Even a pixel-perfect grab there is NOT a draggable node. // Origin draws at (left, bottom-1). Even a pixel-perfect grab there is NOT a draggable node.
NodeHit o = nodeAtPoint(e, a, kTotal, a.left, a.bottom - 1); NodeHit o = nodeAtPoint(e, a, kTotal, a.left, a.bottom - 1);
CHECK(!o.hit); CHECK(!o.hit);
// ReleaseStart draws at (right, sustain level). It is drawing-only -> not grabbable. But // ReleaseStart draws at (left+405, sustain level ~top+50) — the fixed plateau end. It is
// ReleaseEnd is elsewhere, so a grab exactly at ReleaseStart's point must miss. // drawing-only -> not grabbable; no other node is within the radius, so this grab misses.
// ReleaseStart x == right (plateau to sample end), y == sustain (~top+50). NodeHit rs = nodeAtPoint(e, a, kTotal, a.left + 405, a.top + 50);
NodeHit rs = nodeAtPoint(e, a, kTotal, a.right, a.top + 50);
CHECK(!rs.hit); CHECK(!rs.hit);
} }
static void testHitNearestNodeWinsOverDrawOrder() {
// FA2 nearest-wins: with a SHORT hold, AttackEnd (x@85) and HoldEnd (x@89) both fall within
// the grab radius of a point at x@90 — the NEAREST (HoldEnd, 1px) must win, not the earlier
// draw-order AttackEnd (5px), so tightly packed handles stay individually grabbable.
AmpEnvelope e = gateEnv();
e.holdSeconds = 0.01; // hold end at 0.21s -> x@round(89.25)=89
const Rect a = wideArea();
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 90, a.top);
CHECK(h.hit && h.node == EnvNode::HoldEnd);
}
static void testHitCoincidentNodesTieToEarlierDrawOrder() {
// Zero hold: AttackEnd and HoldEnd draw at the SAME pixel. The tie goes to the earlier
// draw-order node (AttackEnd) — deterministic, mirroring the pre-FA2 first-match rule.
AmpEnvelope e = gateEnv();
e.holdSeconds = 0.0;
const Rect a = wideArea();
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 85, a.top);
CHECK(h.hit && h.node == EnvNode::AttackEnd);
}
// --- resolveNodeDrag Gate ----------------------------------------------------- // --- resolveNodeDrag Gate -----------------------------------------------------
static void testGateAttackDragMovesOnlyAttack() { static void testGateAttackDragMovesOnlyAttack() {
const AmpEnvelope e = gateEnv(); const AmpEnvelope e = gateEnv();
const Rect a = wideArea(); const Rect a = wideArea();
EnvClampBounds b; // default maxima 4.0s EnvClampBounds b; // default maxima 4.0s
// +50px at 0.002 s/px = +0.1s on attack (0.2 -> 0.3). Nothing else moves. // +50px at the GATE timed-region scale (2/850 s/px) on attack. Nothing else moves.
AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, 50, 0); AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, 50, 0);
CHECK(near(out.attackSeconds, 0.3)); CHECK(near(out.attackSeconds, 0.2 + 50.0 * kGateSecPerPx));
CHECK(near(out.holdSeconds, e.holdSeconds)); CHECK(near(out.holdSeconds, e.holdSeconds));
CHECK(near(out.decaySeconds, e.decaySeconds)); CHECK(near(out.decaySeconds, e.decaySeconds));
CHECK(near(out.sustainLevel, e.sustainLevel)); CHECK(near(out.sustainLevel, e.sustainLevel));
@@ -101,8 +126,8 @@ static void testGateTimeLowerClampAtZero() {
const AmpEnvelope e = gateEnv(); const AmpEnvelope e = gateEnv();
const Rect a = wideArea(); const Rect a = wideArea();
EnvClampBounds b; EnvClampBounds b;
// Drag attack far LEFT (-500px = -1.0s) from 0.2s: clamps to 0, never negative (monotonic: // Drag attack far LEFT (-500px ~= -1.18s at the gate scale) from 0.2s: clamps to 0, never
// the segment cannot go below zero). // negative (monotonic: the segment cannot go below zero).
AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, -500, 0); AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, -500, 0);
CHECK(near(out.attackSeconds, 0.0)); CHECK(near(out.attackSeconds, 0.0));
} }
@@ -112,8 +137,8 @@ static void testGateTimeUpperClampAtSliderMax() {
const Rect a = wideArea(); const Rect a = wideArea();
EnvClampBounds b; EnvClampBounds b;
b.maxDecaySeconds = 1.0; // the shell's decay slider tops out at 1.0s b.maxDecaySeconds = 1.0; // the shell's decay slider tops out at 1.0s
// Drag decay far RIGHT (+2000px = +4.0s) from 0.3s: clamps to the slider max 1.0, NOT beyond // Drag decay far RIGHT (+2000px ~= +4.7s at the gate scale) from 0.3s: clamps to the slider
// (the drag can't produce a param the slider couldn't). // max 1.0, NOT beyond (the drag can't produce a param the slider couldn't).
AmpEnvelope out = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 2000, 0); AmpEnvelope out = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 2000, 0);
CHECK(near(out.decaySeconds, 1.0)); CHECK(near(out.decaySeconds, 1.0));
} }
@@ -122,10 +147,10 @@ static void testGateSustainNodeBothAxes() {
const AmpEnvelope e = gateEnv(); const AmpEnvelope e = gateEnv();
const Rect a = wideArea(); const Rect a = wideArea();
EnvClampBounds b; EnvClampBounds b;
// DecayEnd: +100px X = +0.2s decay (0.3 -> 0.5); +bottom-ward Y LOWERS the level. Level span is // DecayEnd: +100px X at the gate timed scale on decay; +bottom-ward Y LOWERS the level. Level
// 99 px for [0,1]; drag DOWN by ~10px (positive dy) lowers sustain by ~10/99 ~= 0.101. // span is 99 px for [0,1]; drag DOWN by ~10px (positive dy) lowers sustain by ~10/99 ~= 0.101.
AmpEnvelope out = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 100, 10); AmpEnvelope out = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 100, 10);
CHECK(near(out.decaySeconds, 0.5)); CHECK(near(out.decaySeconds, 0.3 + 100.0 * kGateSecPerPx));
CHECK(out.sustainLevel < e.sustainLevel); // dragged DOWN -> lower sustain CHECK(out.sustainLevel < e.sustainLevel); // dragged DOWN -> lower sustain
CHECK(near(out.sustainLevel, 0.5 - 10.0 / 99.0, 1e-6)); CHECK(near(out.sustainLevel, 0.5 - 10.0 / 99.0, 1e-6));
} }
@@ -152,6 +177,26 @@ static void testGateTimeOnlyNodeIgnoresY() {
CHECK(near(out.sustainLevel, e.sustainLevel)); // Y ignored for a time-only node CHECK(near(out.sustainLevel, e.sustainLevel)); // Y ignored for a time-only node
} }
static void testGateReleaseEndGrabAndDrag() {
// The FA2 fix: ReleaseEnd is a drawn, IN-BOUNDS, grabbable handle (pre-FA2 it mapped past
// area.right and could never be grabbed). gateEnv() draws it at x@575, level 0 (bottom row).
const AmpEnvelope e = gateEnv();
const Rect a = wideArea();
EnvClampBounds b;
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 575, a.bottom - 1);
CHECK(h.hit && h.node == EnvNode::ReleaseEnd);
// Dragging it RIGHT lengthens the release at the gate timed scale; only release changes.
AmpEnvelope out = resolveNodeDrag(e, EnvNode::ReleaseEnd, a, kTotal, b, 85, 0);
CHECK(near(out.releaseSeconds, 0.4 + 85.0 * kGateSecPerPx));
CHECK(near(out.sustainLevel, e.sustainLevel));
CHECK(near(out.decaySeconds, e.decaySeconds));
// Far LEFT clamps to 0; far RIGHT clamps to the slider max.
AmpEnvelope lo = resolveNodeDrag(e, EnvNode::ReleaseEnd, a, kTotal, b, -2000, 0);
CHECK(near(lo.releaseSeconds, 0.0));
AmpEnvelope hi = resolveNodeDrag(e, EnvNode::ReleaseEnd, a, kTotal, b, 5000, 0);
CHECK(near(hi.releaseSeconds, b.maxReleaseSeconds));
}
// --- resolveNodeDrag Trigger -------------------------------------------------- // --- resolveNodeDrag Trigger --------------------------------------------------
static void testTriggerFadeInIsFractionOfPlaySpan() { static void testTriggerFadeInIsFractionOfPlaySpan() {
@@ -188,6 +233,28 @@ static void testTriggerFadeOutMovesOppositePixelDelta() {
CHECK(near(out.fadeInFraction, e.fadeInFraction)); CHECK(near(out.fadeInFraction, e.fadeInFraction));
} }
static void testTriggerZeroFadeOutGrabbableAtRightEdge() {
// The FA2 fix: at fade-out == 0 and full length, FadeOutStart draws AT the right edge
// (right-1, level 1). It must be grabbable there and draggable INWARD to grow the fade from
// zero (drag LEFT -> longer fade-out, opposite the pixel delta).
AmpEnvelope e;
e.mode = EnvMode::Trigger;
e.lengthFraction = 1.0; // played span = full 2.0s -> 1000px
e.fadeInFraction = 0.1;
e.fadeOutFraction = 0.0;
const Rect a = wideArea();
EnvClampBounds b;
NodeHit h = nodeAtPoint(e, a, kTotal, a.right - 1, a.top);
CHECK(h.hit && h.node == EnvNode::FadeOutStart);
// -100px = -0.2s on the 2.0s played span, applied OPPOSITE -> fadeOut 0.0 -> 0.1.
AmpEnvelope out = resolveNodeDrag(e, EnvNode::FadeOutStart, a, kTotal, b, -100, 0);
CHECK(near(out.fadeOutFraction, 0.1));
CHECK(near(out.lengthFraction, e.lengthFraction)); // length untouched
// LengthEnd sits at the same x but level 0 (bottom row) — grabbable at ITS drawn point.
NodeHit le = nodeAtPoint(e, a, kTotal, a.right - 1, a.bottom - 1);
CHECK(le.hit && le.node == EnvNode::LengthEnd);
}
static void testTriggerLengthClampsAtMax() { static void testTriggerLengthClampsAtMax() {
const AmpEnvelope e = triggerEnv(); // length 0.5 const AmpEnvelope e = triggerEnv(); // length 0.5
const Rect a = wideArea(); const Rect a = wideArea();
@@ -226,6 +293,8 @@ int main() {
testHitGrabsDrawnHandle(); testHitGrabsDrawnHandle();
testHitMissesOffEveryNode(); testHitMissesOffEveryNode();
testHitSkipsNonDraggableAnchors(); testHitSkipsNonDraggableAnchors();
testHitNearestNodeWinsOverDrawOrder();
testHitCoincidentNodesTieToEarlierDrawOrder();
testGateAttackDragMovesOnlyAttack(); testGateAttackDragMovesOnlyAttack();
testGateTimeLowerClampAtZero(); testGateTimeLowerClampAtZero();
@@ -233,10 +302,12 @@ int main() {
testGateSustainNodeBothAxes(); testGateSustainNodeBothAxes();
testGateSustainLevelClamps01(); testGateSustainLevelClamps01();
testGateTimeOnlyNodeIgnoresY(); testGateTimeOnlyNodeIgnoresY();
testGateReleaseEndGrabAndDrag();
testTriggerFadeInIsFractionOfPlaySpan(); testTriggerFadeInIsFractionOfPlaySpan();
testTriggerFadesCannotCross(); testTriggerFadesCannotCross();
testTriggerFadeOutMovesOppositePixelDelta(); testTriggerFadeOutMovesOppositePixelDelta();
testTriggerZeroFadeOutGrabbableAtRightEdge();
testTriggerLengthClampsAtMax(); testTriggerLengthClampsAtMax();
testNonDraggableNodeNoMotion(); testNonDraggableNodeNoMotion();
+138 -32
View File
@@ -1,14 +1,15 @@
// Standalone tests for reasampler::vst::envelope_overlay — no VST3, no REAPER, no framework. // Standalone tests for reasampler::vst::envelope_overlay — no VST3, no REAPER, no framework.
// Same fast assert loop as the sibling pure tests. Assert the S-VIEW-3 amp-envelope -> polyline // Same fast assert loop as the sibling pure tests. Assert the S-VIEW-3/FA2 amp-envelope ->
// FORWARD map: the Gate AHDSR shape (attack ramp / hold plateau / decay-to-sustain / plateau / // polyline FORWARD map: the Gate BOUNDED-SCHEMATIC AHDSR shape (attack ramp / hold plateau /
// release) and the Trigger fade/%-length shape, at the waveform time base (so the drawn curve // decay-to-sustain / fixed-width sustain plateau / in-bounds release) and the Trigger
// lines up with the PCM under it). // fade/%-length shape at the waveform time base.
// //
// Covers: timeToX / levelToY (linear maps, edge clamps, release-past-end NOT clamped, degenerate // Covers: timeToX / levelToY (linear maps, edge clamps, past-end CLAMPED to right-1 — the FA2
// area/duration); buildEnvelopePolyline Gate (node order, levels, cumulative time placement, // bounds invariant, degenerate area/duration); gateTimedWidth; buildEnvelopePolyline Gate (node
// sustain plateau to sample end, release past end, collapsed plateau when stages overrun); // order, levels, timed-region placement, fixed sustain-plateau reserve, release visible
// in-bounds, overrun pile-up clamped at the right edge, every vertex in-bounds);
// buildEnvelopePolyline Trigger (fade-in/unity/fade-out at fractions of the played span, overlap // buildEnvelopePolyline Trigger (fade-in/unity/fade-out at fractions of the played span, overlap
// clamp); degenerate flat baseline. // clamp, full-length/zero-fade-out nodes in-bounds at right-1); degenerate flat baseline.
#include "../src/vst/envelope_overlay.h" #include "../src/vst/envelope_overlay.h"
@@ -38,7 +39,7 @@ static bool findNode(const std::vector<EnvVertex>& poly, EnvNode node, EnvVertex
static void testTimeToXEndpoints() { static void testTimeToXEndpoints() {
const Rect a = wideArea(); const Rect a = wideArea();
CHECK(timeToX(a, 2.0, 0.0) == a.left); // t=0 -> left CHECK(timeToX(a, 2.0, 0.0) == a.left); // t=0 -> left
CHECK(timeToX(a, 2.0, 2.0) == a.right); // t=total -> right CHECK(timeToX(a, 2.0, 2.0) == a.right - 1); // t=total -> last in-bounds column
CHECK(timeToX(a, 2.0, 1.0) == a.left + 500); // midpoint CHECK(timeToX(a, 2.0, 1.0) == a.left + 500); // midpoint
} }
@@ -47,11 +48,19 @@ static void testTimeToXNegativePinsLeft() {
CHECK(timeToX(a, 2.0, -0.5) == a.left); // t<0 pins left CHECK(timeToX(a, 2.0, -0.5) == a.left); // t<0 pins left
} }
static void testTimeToXPastEndNotClamped() { static void testTimeToXPastEndClamps() {
// The Gate release tail is drawn past the sample end BY DESIGN: t past total maps past right. // FA2 bounds invariant: t past total pins to the last in-bounds column, never past right.
const Rect a = wideArea(); const Rect a = wideArea();
CHECK(timeToX(a, 2.0, 3.0) > a.right); // t=1.5x total -> past the right edge CHECK(timeToX(a, 2.0, 3.0) == a.right - 1);
CHECK(timeToX(a, 2.0, 3.0) == a.left + 1500); CHECK(timeToX(a, 2.0, 1000.0) == a.right - 1);
}
static void testGateTimedWidth() {
// 15% of the 1000px canvas is reserved for the sustain plateau -> 850px timed region.
CHECK(gateTimedWidth(wideArea()) == 850);
// Zero-width area -> 0; a tiny area still yields >= 1 so the px<->s scale never degenerates.
CHECK(gateTimedWidth(Rect{5, 5, 5, 45}) == 0);
CHECK(gateTimedWidth(Rect{0, 0, 1, 10}) == 1);
} }
static void testTimeToXDegenerate() { static void testTimeToXDegenerate() {
@@ -108,35 +117,81 @@ static void testGateNodeOrderAndLevels() {
CHECK(poly[5].level == 0.0); CHECK(poly[5].level == 0.0);
} }
static void testGateCumulativeTimePlacement() { static void testGateSchematicPlacement() {
// total 2.0s over 1000px => 500 px/s. attack .2 -> x@100, hold end .3 -> x@150, decay end .6 // FA2 bounded schematic: timed region = 850px (150px reserved plateau), total 2.0s =>
// -> x@300. Sustain plateau runs to the sample END (2.0 -> right). Release .4 trails past. // 425 px/s in the timed region. attack .2 -> x@85, hold end .3 -> x@round(127.5)=128, decay
// end .6 -> x@255. Plateau is the FIXED 150px reserve -> ReleaseStart x@405. Release .4 ->
// 170px ramp -> ReleaseEnd x@575, well inside the canvas.
AmpEnvelope env; AmpEnvelope env;
env.mode = EnvMode::Gate; env.mode = EnvMode::Gate;
env.attackSeconds = 0.2; env.attackSeconds = 0.2;
env.holdSeconds = 0.1; // hold end at 0.3s env.holdSeconds = 0.1; // hold end at 0.3s
env.decaySeconds = 0.3; // decay end at 0.6s env.decaySeconds = 0.3; // decay end at 0.6s
env.sustainLevel = 0.5; env.sustainLevel = 0.5;
env.releaseSeconds = 0.4; // release end at 2.4s (past the 2.0s end) env.releaseSeconds = 0.4;
const Rect a = wideArea(); const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0); const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex v; EnvVertex v;
CHECK(findNode(poly, EnvNode::AttackEnd, v) && v.x == a.left + 100); CHECK(findNode(poly, EnvNode::AttackEnd, v) && v.x == a.left + 85);
CHECK(findNode(poly, EnvNode::HoldEnd, v) && v.x == a.left + 150); CHECK(findNode(poly, EnvNode::HoldEnd, v) && v.x == a.left + 128);
CHECK(findNode(poly, EnvNode::DecayEnd, v) && v.x == a.left + 300); CHECK(findNode(poly, EnvNode::DecayEnd, v) && v.x == a.left + 255);
CHECK(findNode(poly, EnvNode::ReleaseStart, v) && v.x == a.right); // plateau to end CHECK(findNode(poly, EnvNode::ReleaseStart, v) && v.x == a.left + 405);
CHECK(findNode(poly, EnvNode::ReleaseEnd, v) && v.x == a.left + 1200); // 2.4s -> 1200px past CHECK(findNode(poly, EnvNode::ReleaseEnd, v) && v.x == a.left + 575);
} }
static void testGatePlateauCollapsesWhenStagesOverrun() { static void testGateSustainPlateauFixedWidth() {
// A/H/D sum to 3.0s > the 2.0s sample: the plateau collapses (ReleaseStart clamps to DecayEnd's // The sustain plateau is ALWAYS the reserved width (canvas - timed region), independent of
// time), and the release still trails past. // the AHDSR times — the bounded region that replaces the old plateau-to-sample-end.
AmpEnvelope env;
env.mode = EnvMode::Gate;
env.attackSeconds = 0.1;
env.holdSeconds = 0.0;
env.decaySeconds = 0.2;
env.sustainLevel = 0.6;
env.releaseSeconds = 0.3;
const Rect a = wideArea();
const int plateauPx = a.width() - gateTimedWidth(a); // 150
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex decay, plateauEnd;
CHECK(findNode(poly, EnvNode::DecayEnd, decay));
CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd));
CHECK(plateauEnd.x - decay.x == plateauPx);
CHECK(plateauEnd.level == 0.6); // plateau holds the sustain level
}
static void testGateReleaseVisibleInBounds() {
// The FA2 fix: Release is a VISIBLE, in-bounds segment — ReleaseEnd sits strictly right of
// the plateau end and strictly inside the canvas (pre-FA2 it mapped past area.right and the
// shell clipped its handle away).
AmpEnvelope env;
env.mode = EnvMode::Gate;
env.attackSeconds = 0.2;
env.holdSeconds = 0.1;
env.decaySeconds = 0.3;
env.sustainLevel = 0.5;
env.releaseSeconds = 0.4;
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex plateauEnd, rel;
CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd));
CHECK(findNode(poly, EnvNode::ReleaseEnd, rel));
CHECK(rel.x > plateauEnd.x); // a visible ramp, not a collapsed point
CHECK(rel.x < a.right); // strictly in-bounds
CHECK(rel.level == 0.0);
}
static void testGateOverrunClampsToCanvas() {
// A/H/D/R overrun the visible span (sum 3.5s > 2.0s total): the trailing nodes pile up at
// the last in-bounds column — monotonic, in-bounds, still individually draggable back left.
// NOTHING maps past area.right (the pre-FA2 release tail is gone).
AmpEnvelope env; AmpEnvelope env;
env.mode = EnvMode::Gate; env.mode = EnvMode::Gate;
env.attackSeconds = 1.0; env.attackSeconds = 1.0;
env.holdSeconds = 1.0; env.holdSeconds = 1.0;
env.decaySeconds = 1.0; // decay end at 3.0s env.decaySeconds = 1.0; // decay end at 3.0s -> already past the timed span
env.sustainLevel = 0.7; env.sustainLevel = 0.7;
env.releaseSeconds = 0.5; env.releaseSeconds = 0.5;
const Rect a = wideArea(); const Rect a = wideArea();
@@ -146,11 +201,37 @@ static void testGatePlateauCollapsesWhenStagesOverrun() {
CHECK(findNode(poly, EnvNode::DecayEnd, decay)); CHECK(findNode(poly, EnvNode::DecayEnd, decay));
CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd)); CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd));
CHECK(findNode(poly, EnvNode::ReleaseEnd, rel)); CHECK(findNode(poly, EnvNode::ReleaseEnd, rel));
CHECK(plateauEnd.x == decay.x); // collapsed: plateau has zero width CHECK(decay.x == a.right - 1); // pinned to the last in-bounds column
CHECK(rel.x > decay.x); // release trails past CHECK(plateauEnd.x == a.right - 1);
CHECK(rel.x == a.right - 1);
CHECK(plateauEnd.level == 0.7); // still at sustain CHECK(plateauEnd.level == 0.7); // still at sustain
} }
static void testGateAllVerticesInBounds() {
// The FA2 bounds invariant, swept over representative param sets (including extremes): every
// vertex of every polyline stays inside the canvas rect.
const Rect a = wideArea();
const AmpEnvelope base; // defaults
AmpEnvelope big = base;
big.mode = EnvMode::Gate;
big.attackSeconds = 4.0; big.holdSeconds = 4.0; big.decaySeconds = 4.0;
big.sustainLevel = 1.0; big.releaseSeconds = 4.0;
AmpEnvelope zero = base;
zero.mode = EnvMode::Gate;
zero.attackSeconds = 0.0; zero.holdSeconds = 0.0; zero.decaySeconds = 0.0;
zero.sustainLevel = 0.0; zero.releaseSeconds = 0.0;
AmpEnvelope trig = base;
trig.mode = EnvMode::Trigger;
trig.lengthFraction = 1.0; trig.fadeInFraction = 0.0; trig.fadeOutFraction = 0.0;
for (const AmpEnvelope& env : {base, big, zero, trig}) {
for (const EnvVertex& v : buildEnvelopePolyline(env, a, 2.0)) {
CHECK(v.x >= a.left && v.x < a.right);
CHECK(v.y >= a.top && v.y < a.bottom);
}
}
}
// --- Trigger polyline --------------------------------------------------------- // --- Trigger polyline ---------------------------------------------------------
static void testTriggerShape() { static void testTriggerShape() {
@@ -193,6 +274,26 @@ static void testTriggerFadeOverlapClamp() {
CHECK(fin.x == a.left + 800); CHECK(fin.x == a.left + 800);
} }
static void testTriggerFullLengthZeroFadeOutInBounds() {
// The FA2 fix: at full length + zero fade-out, FadeOutStart and LengthEnd land AT the last
// in-bounds column (right-1), NOT at the half-open right edge — so the shell draws their
// handles and the fade-out node is grabbable even when fade-out == 0.
AmpEnvelope env;
env.mode = EnvMode::Trigger;
env.lengthFraction = 1.0;
env.fadeInFraction = 0.1;
env.fadeOutFraction = 0.0;
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex fout, lend;
CHECK(findNode(poly, EnvNode::FadeOutStart, fout));
CHECK(findNode(poly, EnvNode::LengthEnd, lend));
CHECK(fout.x == a.right - 1); // present + in-bounds at zero fade-out
CHECK(lend.x == a.right - 1);
CHECK(fout.level == 1.0 && lend.level == 0.0);
}
// --- Degenerate --------------------------------------------------------------- // --- Degenerate ---------------------------------------------------------------
static void testDegenerateFlatBaseline() { static void testDegenerateFlatBaseline() {
@@ -206,23 +307,28 @@ static void testDegenerateFlatBaseline() {
const std::vector<EnvVertex> p2 = buildEnvelopePolyline(env, ok, 0.0); // no duration const std::vector<EnvVertex> p2 = buildEnvelopePolyline(env, ok, 0.0); // no duration
CHECK(p2.size() == 2); CHECK(p2.size() == 2);
CHECK(p2.front().level == 0.0 && p2.back().level == 0.0); CHECK(p2.front().level == 0.0 && p2.back().level == 0.0);
CHECK(p2.front().x == ok.left && p2.back().x == ok.right); // spans the whole area flat CHECK(p2.front().x == ok.left && p2.back().x == ok.right - 1); // spans the area, in-bounds
} }
int main() { int main() {
testTimeToXEndpoints(); testTimeToXEndpoints();
testTimeToXNegativePinsLeft(); testTimeToXNegativePinsLeft();
testTimeToXPastEndNotClamped(); testTimeToXPastEndClamps();
testTimeToXDegenerate(); testTimeToXDegenerate();
testGateTimedWidth();
testLevelToYEndpoints(); testLevelToYEndpoints();
testLevelToYClamps(); testLevelToYClamps();
testGateNodeOrderAndLevels(); testGateNodeOrderAndLevels();
testGateCumulativeTimePlacement(); testGateSchematicPlacement();
testGatePlateauCollapsesWhenStagesOverrun(); testGateSustainPlateauFixedWidth();
testGateReleaseVisibleInBounds();
testGateOverrunClampsToCanvas();
testGateAllVerticesInBounds();
testTriggerShape(); testTriggerShape();
testTriggerFadeOverlapClamp(); testTriggerFadeOverlapClamp();
testTriggerFullLengthZeroFadeOutInBounds();
testDegenerateFlatBaseline(); testDegenerateFlatBaseline();