S-VIEW-3: add envelope_overlay + envelope_edit pure modules

AHDSR/Trigger params -> polyline forward map (overlay) and node hit-test +
clamped/monotonic pixel->param inverse map (edit) for the draggable Sample-view
envelope. Engine-free, unit-tested, wired into ctest. No shell changes.
This commit is contained in:
2026-07-27 13:24:48 -04:00
parent 3b9b78b82c
commit 12a7d072ac
7 changed files with 988 additions and 0 deletions
+248
View File
@@ -0,0 +1,248 @@
// 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; first-match determinism); resolveNodeDrag Gate
// (each cumulative node edits its OWN segment; X->time, sustain node's Y->level; lower clamp at 0;
// upper clamp at the caller's max; only the dragged param changes); 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); degenerate area/duration + non-draggable node ->
// no motion.
#include "../src/vst/envelope_edit.h"
#include <cmath>
#include <cstdio>
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. 2.0s total => 500 px/s => 0.002 s/px.
static Rect wideArea() { return Rect{20, 10, 1020, 110}; }
static constexpr double kTotal = 2.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+100 (0.2s), y = top (level 1). A grab there hits it.
NodeHit h = nodeAtPoint(e, a, kTotal, a.left + 100, a.top);
CHECK(h.hit && h.node == EnvNode::AttackEnd);
// The sustain node (DecayEnd) at 0.6s -> left+300, level 0.5 -> ~top+50.
NodeHit s = nodeAtPoint(e, a, kTotal, a.left + 300, 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 (mid plateau, 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 (right, sustain level). It is drawing-only -> not grabbable. But
// ReleaseEnd is elsewhere, so a grab exactly at ReleaseStart's point must miss.
// ReleaseStart x == right (plateau to sample end), y == sustain (~top+50).
NodeHit rs = nodeAtPoint(e, a, kTotal, a.right, a.top + 50);
CHECK(!rs.hit);
}
// --- resolveNodeDrag Gate -----------------------------------------------------
static void testGateAttackDragMovesOnlyAttack() {
const AmpEnvelope e = gateEnv();
const Rect a = wideArea();
EnvClampBounds b; // default maxima 4.0s
// +50px at 0.002 s/px = +0.1s on attack (0.2 -> 0.3). Nothing else moves.
AmpEnvelope out = resolveNodeDrag(e, EnvNode::AttackEnd, a, kTotal, b, 50, 0);
CHECK(near(out.attackSeconds, 0.3));
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.0s) 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.0s) 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 = +0.2s decay (0.3 -> 0.5); +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.5));
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
}
// --- 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 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();
testGateAttackDragMovesOnlyAttack();
testGateTimeLowerClampAtZero();
testGateTimeUpperClampAtSliderMax();
testGateSustainNodeBothAxes();
testGateSustainLevelClamps01();
testGateTimeOnlyNodeIgnoresY();
testTriggerFadeInIsFractionOfPlaySpan();
testTriggerFadesCannotCross();
testTriggerFadeOutMovesOppositePixelDelta();
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;
}
+232
View File
@@ -0,0 +1,232 @@
// 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
// FORWARD map: the Gate AHDSR shape (attack ramp / hold plateau / decay-to-sustain / plateau /
// release) and the Trigger fade/%-length shape, at the waveform time base (so the drawn curve
// lines up with the PCM under it).
//
// Covers: timeToX / levelToY (linear maps, edge clamps, release-past-end NOT clamped, degenerate
// area/duration); buildEnvelopePolyline Gate (node order, levels, cumulative time placement,
// sustain plateau to sample end, release past end, collapsed plateau when stages overrun);
// buildEnvelopePolyline Trigger (fade-in/unity/fade-out at fractions of the played span, overlap
// clamp); degenerate flat baseline.
#include "../src/vst/envelope_overlay.h"
#include <cstdio>
#include <vector>
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)
// A comfortable overlay area: 1000px wide, 100px tall, offset so left/top != 0 (catches origin
// bugs). Under levelToY the level span is height-1 = 99 rows.
static Rect wideArea() { return Rect{20, 10, 1020, 110}; } // width 1000, height 100
// Find the first vertex with a given node in a polyline; asserts presence via the returned bool.
static bool findNode(const std::vector<EnvVertex>& poly, EnvNode node, EnvVertex& out) {
for (const EnvVertex& v : poly) {
if (v.node == node) { out = v; return true; }
}
return false;
}
// --- timeToX / levelToY -------------------------------------------------------
static void testTimeToXEndpoints() {
const Rect a = wideArea();
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, 1.0) == a.left + 500); // midpoint
}
static void testTimeToXNegativePinsLeft() {
const Rect a = wideArea();
CHECK(timeToX(a, 2.0, -0.5) == a.left); // t<0 pins left
}
static void testTimeToXPastEndNotClamped() {
// The Gate release tail is drawn past the sample end BY DESIGN: t past total maps past right.
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.left + 1500);
}
static void testTimeToXDegenerate() {
const Rect a = wideArea();
CHECK(timeToX(a, 0.0, 1.0) == a.left); // no duration -> left
const Rect z = Rect{5, 5, 5, 45}; // zero width
CHECK(timeToX(z, 2.0, 1.0) == z.left);
}
static void testLevelToYEndpoints() {
const Rect a = wideArea();
CHECK(levelToY(a, 1.0) == a.top); // level 1 -> top row
CHECK(levelToY(a, 0.0) == a.bottom - 1); // level 0 -> bottom row
CHECK(levelToY(a, 0.5) == a.top + 50); // mid: round((1-0.5)*99)=round(49.5)=50
}
static void testLevelToYClamps() {
const Rect a = wideArea();
CHECK(levelToY(a, 2.0) == a.top); // >1 clamps to top
CHECK(levelToY(a, -1.0) == a.bottom - 1); // <0 clamps to bottom
const Rect z = Rect{5, 5, 45, 5}; // zero height
CHECK(levelToY(z, 0.5) == z.top);
}
// --- Gate polyline ------------------------------------------------------------
static void testGateNodeOrderAndLevels() {
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);
// Six vertices, in draw order.
CHECK(poly.size() == 6);
CHECK(poly[0].node == EnvNode::Origin);
CHECK(poly[1].node == EnvNode::AttackEnd);
CHECK(poly[2].node == EnvNode::HoldEnd);
CHECK(poly[3].node == EnvNode::DecayEnd);
CHECK(poly[4].node == EnvNode::ReleaseStart);
CHECK(poly[5].node == EnvNode::ReleaseEnd);
// Levels: origin 0, attack/hold peak 1, decay settles to sustain, plateau holds sustain,
// release ends at 0.
CHECK(poly[0].level == 0.0);
CHECK(poly[1].level == 1.0);
CHECK(poly[2].level == 1.0);
CHECK(poly[3].level == 0.5); // sustain
CHECK(poly[4].level == 0.5); // plateau end holds sustain
CHECK(poly[5].level == 0.0);
}
static void testGateCumulativeTimePlacement() {
// total 2.0s over 1000px => 500 px/s. attack .2 -> x@100, hold end .3 -> x@150, decay end .6
// -> x@300. Sustain plateau runs to the sample END (2.0 -> right). Release .4 trails past.
AmpEnvelope env;
env.mode = EnvMode::Gate;
env.attackSeconds = 0.2;
env.holdSeconds = 0.1; // hold end at 0.3s
env.decaySeconds = 0.3; // decay end at 0.6s
env.sustainLevel = 0.5;
env.releaseSeconds = 0.4; // release end at 2.4s (past the 2.0s end)
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex v;
CHECK(findNode(poly, EnvNode::AttackEnd, v) && v.x == a.left + 100);
CHECK(findNode(poly, EnvNode::HoldEnd, v) && v.x == a.left + 150);
CHECK(findNode(poly, EnvNode::DecayEnd, v) && v.x == a.left + 300);
CHECK(findNode(poly, EnvNode::ReleaseStart, v) && v.x == a.right); // plateau to end
CHECK(findNode(poly, EnvNode::ReleaseEnd, v) && v.x == a.left + 1200); // 2.4s -> 1200px past
}
static void testGatePlateauCollapsesWhenStagesOverrun() {
// A/H/D sum to 3.0s > the 2.0s sample: the plateau collapses (ReleaseStart clamps to DecayEnd's
// time), and the release still trails past.
AmpEnvelope env;
env.mode = EnvMode::Gate;
env.attackSeconds = 1.0;
env.holdSeconds = 1.0;
env.decaySeconds = 1.0; // decay end at 3.0s
env.sustainLevel = 0.7;
env.releaseSeconds = 0.5;
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex decay, plateauEnd, rel;
CHECK(findNode(poly, EnvNode::DecayEnd, decay));
CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd));
CHECK(findNode(poly, EnvNode::ReleaseEnd, rel));
CHECK(plateauEnd.x == decay.x); // collapsed: plateau has zero width
CHECK(rel.x > decay.x); // release trails past
CHECK(plateauEnd.level == 0.7); // still at sustain
}
// --- Trigger polyline ---------------------------------------------------------
static void testTriggerShape() {
// played span = length * total = 0.5 * 2.0 = 1.0s -> 500px wide. fadeIn .2 of play -> 0.2s
// (x@100), fade-out .3 of play -> begins at 0.7s (x@350), playEnd at 1.0s (x@500).
AmpEnvelope env;
env.mode = EnvMode::Trigger;
env.lengthFraction = 0.5;
env.fadeInFraction = 0.2;
env.fadeOutFraction = 0.3;
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
CHECK(poly.size() == 4);
CHECK(poly[0].node == EnvNode::Origin);
CHECK(poly[1].node == EnvNode::FadeInEnd);
CHECK(poly[2].node == EnvNode::FadeOutStart);
CHECK(poly[3].node == EnvNode::LengthEnd);
EnvVertex v;
CHECK(findNode(poly, EnvNode::FadeInEnd, v) && v.x == a.left + 100 && v.level == 1.0);
CHECK(findNode(poly, EnvNode::FadeOutStart, v) && v.x == a.left + 350 && v.level == 1.0);
CHECK(findNode(poly, EnvNode::LengthEnd, v) && v.x == a.left + 500 && v.level == 0.0);
}
static void testTriggerFadeOverlapClamp() {
// fadeIn + fadeOut > 1: the fade-out is trimmed so they meet exactly (no crossed nodes).
AmpEnvelope env;
env.mode = EnvMode::Trigger;
env.lengthFraction = 1.0; // played span = full 2.0s -> 1000px
env.fadeInFraction = 0.8; // fade-in end at 0.8*2.0 = 1.6s -> x@800
env.fadeOutFraction = 0.6; // would be 1.4s -> clamped to 1-0.8=0.2 -> begins at 0.8*2.0 too
const Rect a = wideArea();
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, a, 2.0);
EnvVertex fin, fout;
CHECK(findNode(poly, EnvNode::FadeInEnd, fin));
CHECK(findNode(poly, EnvNode::FadeOutStart, fout));
CHECK(fin.x == fout.x); // fades meet exactly, never cross
CHECK(fin.x == a.left + 800);
}
// --- Degenerate ---------------------------------------------------------------
static void testDegenerateFlatBaseline() {
AmpEnvelope env; // any params
const Rect zeroW = Rect{0, 0, 0, 100};
const std::vector<EnvVertex> p1 = buildEnvelopePolyline(env, zeroW, 2.0);
CHECK(p1.size() == 2); // always a drawable line
CHECK(p1.front().level == 0.0 && p1.back().level == 0.0);
const Rect ok = wideArea();
const std::vector<EnvVertex> p2 = buildEnvelopePolyline(env, ok, 0.0); // no duration
CHECK(p2.size() == 2);
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
}
int main() {
testTimeToXEndpoints();
testTimeToXNegativePinsLeft();
testTimeToXPastEndNotClamped();
testTimeToXDegenerate();
testLevelToYEndpoints();
testLevelToYClamps();
testGateNodeOrderAndLevels();
testGateCumulativeTimePlacement();
testGatePlateauCollapsesWhenStagesOverrun();
testTriggerShape();
testTriggerFadeOverlapClamp();
testDegenerateFlatBaseline();
if (g_fail == 0) std::printf("envelope_overlay: all tests passed\n");
else std::printf("envelope_overlay: %d FAILED\n", g_fail);
return g_fail == 0 ? 0 : 1;
}