// Standalone tests for reasampler::vst::envelope_edit — no VST3, no REAPER, no framework. // Same fast assert loop as the sibling pure tests. Assert the S-VIEW-3 draggable-node INVERSE // map: node hit-test + pixel-delta -> clamped/monotonic param set, HARD at the clamp + monotonic // 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 // the non-draggable Origin/ReleaseStart anchors; NEAREST-node-wins with draw-order tie-break — // FA2); resolveNodeDrag Gate (each cumulative node edits its OWN segment at the TIMED-region px // scale; X->time, sustain node's Y->level; lower clamp at 0; upper clamp at the caller's max; // only the dragged param changes; ReleaseEnd grabbable + draggable — FA2); resolveNodeDrag // Trigger (fades as fractions of the played span; fadeIn/fadeOut mutual clamp so they never // 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 #include using namespace reasampler::vst; static int g_fail = 0; #define CHECK(cond) do { if(!(cond)) { \ std::printf("FAIL line %d: %s\n", __LINE__, #cond); ++g_fail; } } while(0) static bool near(double a, double b, double eps = 1e-9) { return std::fabs(a - b) <= eps; } // 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 constexpr double kTotal = 2.0; static constexpr double kGateSecPerPx = kTotal / 850.0; static AmpEnvelope gateEnv() { AmpEnvelope e; e.mode = EnvMode::Gate; e.attackSeconds = 0.2; e.holdSeconds = 0.1; e.decaySeconds = 0.3; e.sustainLevel = 0.5; e.releaseSeconds = 0.4; return e; } static AmpEnvelope triggerEnv() { AmpEnvelope e; e.mode = EnvMode::Trigger; e.lengthFraction = 0.5; // played span 1.0s -> 500px e.fadeInFraction = 0.2; e.fadeOutFraction = 0.2; return e; } // --- nodeAtPoint -------------------------------------------------------------- static void testHitGrabsDrawnHandle() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); // 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 + 85, a.top); CHECK(h.hit && h.node == EnvNode::AttackEnd); // The sustain node (DecayEnd) at 0.6s -> left+255, level 0.5 -> ~top+50. NodeHit s = nodeAtPoint(e, a, kTotal, a.left + 255, a.top + 50); CHECK(s.hit && s.node == EnvNode::DecayEnd); } static void testHitMissesOffEveryNode() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); // 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); CHECK(!h.hit); } static void testHitSkipsNonDraggableAnchors() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); // 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); CHECK(!o.hit); // ReleaseStart draws at (left+405, sustain level ~top+50) — the fixed plateau end. It is // drawing-only -> not grabbable; no other node is within the radius, so this grab misses. NodeHit rs = nodeAtPoint(e, a, kTotal, a.left + 405, a.top + 50); 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 ----------------------------------------------------- static void testGateAttackDragMovesOnlyAttack() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; // default maxima 4.0s // +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); CHECK(near(out.attackSeconds, 0.2 + 50.0 * kGateSecPerPx)); CHECK(near(out.holdSeconds, e.holdSeconds)); CHECK(near(out.decaySeconds, e.decaySeconds)); CHECK(near(out.sustainLevel, e.sustainLevel)); CHECK(near(out.releaseSeconds, e.releaseSeconds)); } static void testGateTimeLowerClampAtZero() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; // Drag attack far LEFT (-500px ~= -1.18s at the gate scale) from 0.2s: clamps to 0, never // negative (monotonic: the segment cannot go below zero). AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, -500, 0); CHECK(near(out.attackSeconds, 0.0)); } static void testGateTimeUpperClampAtSliderMax() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; b.maxDecaySeconds = 1.0; // the shell's decay slider tops out at 1.0s // Drag decay far RIGHT (+2000px ~= +4.7s at the gate scale) from 0.3s: clamps to the slider // 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); CHECK(near(out.decaySeconds, 1.0)); } static void testGateSustainNodeBothAxes() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; // DecayEnd: +100px X at the gate timed scale on decay; +bottom-ward Y LOWERS the level. Level // 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); CHECK(near(out.decaySeconds, 0.3 + 100.0 * kGateSecPerPx)); CHECK(out.sustainLevel < e.sustainLevel); // dragged DOWN -> lower sustain CHECK(near(out.sustainLevel, 0.5 - 10.0 / 99.0, 1e-6)); } static void testGateSustainLevelClamps01() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; // Drag sustain UP hard (dy very negative): clamps to 1.0. AmpEnvelope up = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 0, -10000); CHECK(near(up.sustainLevel, 1.0)); // Drag sustain DOWN hard (dy very positive): clamps to 0.0. AmpEnvelope dn = resolveNodeDrag(e, EnvNode::DecayEnd, a, kTotal, b, 0, 10000); CHECK(near(dn.sustainLevel, 0.0)); } static void testGateTimeOnlyNodeIgnoresY() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; // HoldEnd is time-only: a big Y delta must NOT change any level (there is no level to change). AmpEnvelope out = resolveNodeDrag(e, EnvNode::HoldEnd, a, kTotal, b, 0, 500); CHECK(near(out.holdSeconds, e.holdSeconds)); // dx 0 -> no time change either 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 -------------------------------------------------- static void testTriggerFadeInIsFractionOfPlaySpan() { const AmpEnvelope e = triggerEnv(); // played span 1.0s -> 500px const Rect a = wideArea(); EnvClampBounds b; // +50px = +0.1s on the play timeline = +0.1/1.0 = +0.1 fraction. fadeIn 0.2 -> 0.3. AmpEnvelope out = resolveNodeDrag(e, EnvNode::FadeInEnd, a, kTotal, b, 50, 0); CHECK(near(out.fadeInFraction, 0.3)); CHECK(near(out.fadeOutFraction, e.fadeOutFraction)); // unchanged } static void testTriggerFadesCannotCross() { AmpEnvelope e = triggerEnv(); e.fadeInFraction = 0.5; e.fadeOutFraction = 0.3; // sum 0.8, room 0.2 before they'd cross const Rect a = wideArea(); EnvClampBounds b; // Drag fade-in far RIGHT (+2000px): would push fadeIn well past 1-fadeOut=0.7, but the mutual // clamp caps it at 0.7 so the fade nodes never cross (monotonic on the play timeline). AmpEnvelope out = resolveNodeDrag(e, EnvNode::FadeInEnd, a, kTotal, b, 2000, 0); CHECK(near(out.fadeInFraction, 0.7)); CHECK(near(out.fadeOutFraction, 0.3)); } static void testTriggerFadeOutMovesOppositePixelDelta() { const AmpEnvelope e = triggerEnv(); // fadeOut 0.2, play span 1.0s -> 500px const Rect a = wideArea(); EnvClampBounds b; // FadeOutStart sits at (1-fadeOut) of the span; dragging it LEFT (-50px) LENGTHENS the fade-out. // -50px = -0.1s = -0.1 fraction on the span, applied OPPOSITE -> fadeOut 0.2 -> 0.3. AmpEnvelope out = resolveNodeDrag(e, EnvNode::FadeOutStart, a, kTotal, b, -50, 0); CHECK(near(out.fadeOutFraction, 0.3)); 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() { const AmpEnvelope e = triggerEnv(); // length 0.5 const Rect a = wideArea(); EnvClampBounds b; // maxLengthFraction 1.0 // LengthEnd maps to a fraction of the WHOLE sample: +2000px = +4.0s = +2.0 fraction, clamps 1.0. AmpEnvelope out = resolveNodeDrag(e, EnvNode::LengthEnd, a, kTotal, b, 2000, 0); CHECK(near(out.lengthFraction, 1.0)); // Drag far LEFT clamps to 0. AmpEnvelope lo = resolveNodeDrag(e, EnvNode::LengthEnd, a, kTotal, b, -2000, 0); CHECK(near(lo.lengthFraction, 0.0)); } // --- No-motion guards --------------------------------------------------------- static void testNonDraggableNodeNoMotion() { const AmpEnvelope e = gateEnv(); const Rect a = wideArea(); EnvClampBounds b; AmpEnvelope o = resolveNodeDrag(e, EnvNode::Origin, a, kTotal, b, 500, 500); CHECK(near(o.attackSeconds, e.attackSeconds) && near(o.sustainLevel, e.sustainLevel)); AmpEnvelope rs = resolveNodeDrag(e, EnvNode::ReleaseStart, a, kTotal, b, 500, 500); CHECK(near(rs.releaseSeconds, e.releaseSeconds)); } static void testDegenerateAreaNoMotion() { const AmpEnvelope e = gateEnv(); EnvClampBounds b; const Rect zeroW = Rect{0, 0, 0, 100}; AmpEnvelope o1 = resolveNodeDrag(e, EnvNode::AttackEnd, zeroW, kTotal, b, 500, 0); CHECK(near(o1.attackSeconds, e.attackSeconds)); AmpEnvelope o2 = resolveNodeDrag(e, EnvNode::AttackEnd, wideArea(), 0.0, b, 500, 0); // no time CHECK(near(o2.attackSeconds, e.attackSeconds)); } int main() { testHitGrabsDrawnHandle(); testHitMissesOffEveryNode(); testHitSkipsNonDraggableAnchors(); testHitNearestNodeWinsOverDrawOrder(); testHitCoincidentNodesTieToEarlierDrawOrder(); testGateAttackDragMovesOnlyAttack(); testGateTimeLowerClampAtZero(); testGateTimeUpperClampAtSliderMax(); testGateSustainNodeBothAxes(); testGateSustainLevelClamps01(); testGateTimeOnlyNodeIgnoresY(); testGateReleaseEndGrabAndDrag(); testTriggerFadeInIsFractionOfPlaySpan(); testTriggerFadesCannotCross(); testTriggerFadeOutMovesOppositePixelDelta(); testTriggerZeroFadeOutGrabbableAtRightEdge(); testTriggerLengthClampsAtMax(); testNonDraggableNodeNoMotion(); testDegenerateAreaNoMotion(); if (g_fail == 0) std::printf("envelope_edit: all tests passed\n"); else std::printf("envelope_edit: %d FAILED\n", g_fail); return g_fail == 0 ? 0 : 1; }