instrument: one staged-envelope system — per-segment curves, the sustain-less AHD, and a shared overlay for all three envelopes
Trigger's fade pair folds into the AHD (and goes live); the release anchors right; Preserve rings its synthetic tail out instead of cutting it. Payload v10.
This commit is contained in:
+253
-306
@@ -1,21 +1,19 @@
|
||||
// Standalone tests for reasampler::instrument::ui::envelope_overlay — no VST3, no REAPER, no framework.
|
||||
// Same fast assert loop as the sibling pure tests. Assert the S-VIEW-3/FA2 amp-envelope ->
|
||||
// polyline FORWARD map: the Gate BOUNDED-SCHEMATIC AHDSR shape (attack ramp / hold plateau /
|
||||
// decay-to-sustain / fixed-width sustain plateau / in-bounds release) and the Trigger
|
||||
// fade/%-length shape at the waveform time base.
|
||||
// Standalone tests for reasampler::instrument::ui::envelope_overlay — no VST3, no REAPER, no
|
||||
// framework. Same fast assert loop as the sibling pure tests. Assert the staged-envelope ->
|
||||
// polyline FORWARD map for BOTH layout policies: the AHDSR bounded schematic with its
|
||||
// RIGHT-ANCHORED release, and the sustain-less AHD laid 1:1 over the waveform's time axis.
|
||||
//
|
||||
// Covers: timeToX / levelToY (linear maps, edge clamps, past-end CLAMPED to right-1 — the FA2
|
||||
// bounds invariant, no 32-bit overflow on huge times, degenerate area/duration); gateTimedWidth
|
||||
// + gatePxPerSecond; buildEnvelopePolyline Gate (node order, levels, PARAM-DOMAIN timed-region
|
||||
// placement independent of sample duration, per-segment kGateNodeSepPx separation — every node
|
||||
// distinct even at the tier-0 zero-hold/zero-decay defaults, fixed sustain-plateau reserve,
|
||||
// release visible in-bounds, overrun compressed from the right preserving the minimum gaps,
|
||||
// every vertex in-bounds); buildEnvelopePolyline Trigger (fade-in/unity/fade-out at fractions of
|
||||
// the played span, overlap clamp, full-length/zero-fade-out nodes in-bounds at right-1);
|
||||
// degenerate flat baseline.
|
||||
// Covers: timeToX / levelToY (linear maps, edge clamps, past-end clamped to right-1, no 32-bit
|
||||
// overflow on huge times, degenerate area/duration); gatePxPerSecond; the AHDSR polyline (node
|
||||
// order, levels, release anchored at the right edge, the sustain plateau reaching the edge at
|
||||
// zero release, per-segment separation at the tier-0 defaults, overrun compression, every
|
||||
// vertex in-bounds); splitAhdSeconds (A+H+D never exceeds the span, hold at 0% and 100%); the
|
||||
// AHD polyline (1:1 with the time axis, origin offset); curve knots (present only on sloped
|
||||
// non-zero segments, height following the exponent); the degenerate flat baseline.
|
||||
|
||||
#include "../src/core/instrument/ui/envelope_overlay.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
@@ -32,364 +30,313 @@ static OverlayArea overlayOf(const Rect& r) { return OverlayArea{r}; }
|
||||
// bugs). Under levelToY the level span is height-1 = 99 rows.
|
||||
static Rect wideArea() { return Rect::ltrb(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;
|
||||
}
|
||||
static bool hasNode(const std::vector<EnvVertex>& poly, EnvNode node) {
|
||||
EnvVertex v;
|
||||
return findNode(poly, node, v);
|
||||
}
|
||||
|
||||
static StageEnvelope ahdsr(double a, double h, double d, double sus, double r) {
|
||||
StageEnvelope e;
|
||||
e.kind = EnvKind::Ahdsr;
|
||||
e.attackSeconds = a;
|
||||
e.holdSeconds = h;
|
||||
e.decaySeconds = d;
|
||||
e.sustainLevel = sus;
|
||||
e.releaseSeconds = r;
|
||||
return e;
|
||||
}
|
||||
|
||||
static StageEnvelope ahd(double a, double d, double frac, double origin, double span) {
|
||||
StageEnvelope e;
|
||||
e.kind = EnvKind::Ahd;
|
||||
e.attackSeconds = a;
|
||||
e.decaySeconds = d;
|
||||
e.holdFraction = frac;
|
||||
e.originSeconds = origin;
|
||||
e.spanSeconds = span;
|
||||
return e;
|
||||
}
|
||||
|
||||
// --- timeToX / levelToY -------------------------------------------------------
|
||||
|
||||
static void testTimeToXEndpoints() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(timeToX(a, 2.0, 0.0) == a.x); // t=0 -> left
|
||||
CHECK(timeToX(a, 2.0, 2.0) == a.right() - 1); // t=total -> last in-bounds column
|
||||
CHECK(timeToX(a, 2.0, 1.0) == a.x + 500); // midpoint
|
||||
CHECK(timeToX(a, 2.0, 0.0) == a.x); // t=0 -> left
|
||||
CHECK(timeToX(a, 2.0, 2.0) == a.right() - 1); // t=total -> last in-bounds column
|
||||
CHECK(timeToX(a, 2.0, 1.0) == a.x + 500); // midpoint
|
||||
}
|
||||
|
||||
static void testTimeToXNegativePinsLeft() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(timeToX(a, 2.0, -0.5) == a.x); // t<0 pins left
|
||||
}
|
||||
|
||||
static void testTimeToXPastEndClamps() {
|
||||
// FA2 bounds invariant: t past total pins to the last in-bounds column, never past right.
|
||||
static void testTimeToXClampsBothEnds() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(timeToX(a, 2.0, -0.5) == a.x);
|
||||
CHECK(timeToX(a, 2.0, 3.0) == a.right() - 1);
|
||||
CHECK(timeToX(a, 2.0, 1000.0) == a.right() - 1);
|
||||
// A HUGE t must clamp in double space, not overflow the integer cast (32-bit long on
|
||||
// Windows would wrap to LONG_MIN and pin to the WRONG edge).
|
||||
CHECK(timeToX(a, 2.0, 1e15) == 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::ltrb(5, 5, 5, 45)) == 0);
|
||||
CHECK(gateTimedWidth(Rect::ltrb(0, 0, 1, 10)) == 1);
|
||||
}
|
||||
|
||||
static void testGatePxPerSecond() {
|
||||
// PARAM-DOMAIN scale: (timedW - 1 - 4*sep) px spread over 4 x kGateStageMaxSeconds. For the
|
||||
// 1000px canvas: (850 - 1 - 32) / 8.0s = 817/8 px/s. Independent of any sample duration.
|
||||
const double expected = 817.0 / (4.0 * kGateStageMaxSeconds);
|
||||
CHECK(gatePxPerSecond(wideArea()) == expected);
|
||||
CHECK(gatePxPerSecond(Rect::ltrb(5, 5, 5, 45)) == 0.0); // zero-width area -> 0
|
||||
CHECK(gatePxPerSecond(Rect::ltrb(0, 0, 10, 10)) > 0.0); // tiny area: usable floors at 1px, > 0
|
||||
}
|
||||
|
||||
static void testTimeToXDegenerate() {
|
||||
static void testLevelToY() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(timeToX(a, 0.0, 1.0) == a.x); // no duration -> left
|
||||
const Rect z = Rect::ltrb(5, 5, 5, 45); // zero width
|
||||
CHECK(timeToX(z, 2.0, 1.0) == z.x);
|
||||
CHECK(levelToY(a, 1.0) == a.y); // level 1 -> top row
|
||||
CHECK(levelToY(a, 0.0) == a.bottom() - 1); // level 0 -> bottom row
|
||||
CHECK(levelToY(a, 0.5) == a.y + 50); // 99-row span, rounded
|
||||
CHECK(levelToY(a, 5.0) == a.y); // clamps
|
||||
CHECK(levelToY(a, -5.0) == a.bottom() - 1);
|
||||
}
|
||||
|
||||
static void testLevelToYEndpoints() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(levelToY(a, 1.0) == a.y); // level 1 -> top row
|
||||
CHECK(levelToY(a, 0.0) == a.bottom() - 1); // level 0 -> bottom row
|
||||
CHECK(levelToY(a, 0.5) == a.y + 50); // mid: round((1-0.5)*99)=round(49.5)=50
|
||||
static void testDegenerateAreaAndDuration() {
|
||||
CHECK(timeToX(Rect{}, 2.0, 1.0) == 0);
|
||||
CHECK(timeToX(wideArea(), 0.0, 1.0) == wideArea().x);
|
||||
CHECK(levelToY(Rect{}, 0.5) == 0);
|
||||
CHECK(gatePxPerSecond(Rect{}) == 0.0);
|
||||
}
|
||||
|
||||
static void testLevelToYClamps() {
|
||||
// --- the AHDSR schematic ------------------------------------------------------
|
||||
|
||||
static void testAhdsrNodeOrderAndLevels() {
|
||||
const Rect a = wideArea();
|
||||
CHECK(levelToY(a, 2.0) == a.y); // >1 clamps to top
|
||||
CHECK(levelToY(a, -1.0) == a.bottom() - 1); // <0 clamps to bottom
|
||||
const Rect z = Rect::ltrb(5, 5, 45, 5); // zero height
|
||||
CHECK(levelToY(z, 0.5) == z.y);
|
||||
}
|
||||
|
||||
// --- 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, overlayOf(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 testGateSchematicPlacement() {
|
||||
// FA2 bounded schematic at the PARAM-DOMAIN scale: timed region = 850px (150px reserved
|
||||
// plateau), pps = (850-1-32)/8s = 102.125 px/s, each segment prefixed by the 8px separation
|
||||
// base. attack .2 -> x@round(8+20.425)=28; hold .1 -> x@round(28.425+8+10.2125)=47; decay
|
||||
// .3 -> x@round(46.6375+8+30.6375)=85; plateau is the FIXED 150px reserve -> ReleaseStart
|
||||
// x@235; release .4 -> x@round(235.275+8+40.85)=284, well inside the canvas.
|
||||
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, overlayOf(a), 2.0);
|
||||
|
||||
const std::vector<EnvVertex> poly =
|
||||
buildEnvelopePolyline(ahdsr(0.2, 0.1, 0.3, 0.5, 0.4), overlayOf(a), 4.0);
|
||||
EnvVertex v;
|
||||
CHECK(findNode(poly, EnvNode::AttackEnd, v) && v.x == a.x + 28);
|
||||
CHECK(findNode(poly, EnvNode::HoldEnd, v) && v.x == a.x + 47);
|
||||
CHECK(findNode(poly, EnvNode::DecayEnd, v) && v.x == a.x + 85);
|
||||
CHECK(findNode(poly, EnvNode::ReleaseStart, v) && v.x == a.x + 235);
|
||||
CHECK(findNode(poly, EnvNode::ReleaseEnd, v) && v.x == a.x + 284);
|
||||
}
|
||||
|
||||
static void testGateLayoutIndependentOfSampleDuration() {
|
||||
// The Gate schematic is scaled by the PARAM domain, NOT the capture length: the same params
|
||||
// produce the SAME polyline over a 0.3s and a 10s sample (pre-fix, a 60ms release on a 10s
|
||||
// capture collapsed to ~5px while 2s stages on a 0.3s capture pinned to the right edge).
|
||||
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.06;
|
||||
const Rect a = wideArea();
|
||||
CHECK(buildEnvelopePolyline(env, overlayOf(a), 0.3) == buildEnvelopePolyline(env, overlayOf(a), 10.0));
|
||||
}
|
||||
|
||||
static void testGateMinSeparationAtDefaults() {
|
||||
// THE FA2 headline: at the tier-0 Gate defaults (attack 3ms, hold 0, decay 0, sustain 1.0,
|
||||
// release 60ms) every consecutive node pair is at least kGateNodeSepPx apart — no node ever
|
||||
// renders on top of its neighbour, so each is individually grabbable.
|
||||
const AmpEnvelope env; // struct defaults ARE the tier-0 Gate defaults
|
||||
const Rect a = wideArea();
|
||||
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, overlayOf(a), 2.0);
|
||||
CHECK(poly.size() == 6);
|
||||
for (size_t i = 1; i < poly.size(); ++i) {
|
||||
CHECK(poly[i].x - poly[i - 1].x >= kGateNodeSepPx);
|
||||
CHECK(poly.size() >= 6);
|
||||
CHECK(poly[0].node == EnvNode::Origin && poly[0].level == 0.0);
|
||||
CHECK(poly[1].node == EnvNode::AttackEnd && poly[1].level == 1.0);
|
||||
CHECK(poly[2].node == EnvNode::HoldEnd && poly[2].level == 1.0);
|
||||
CHECK(poly[3].node == EnvNode::DecayEnd && poly[3].level == 0.5);
|
||||
CHECK(poly[4].node == EnvNode::ReleaseStart && poly[4].level == 0.5);
|
||||
CHECK(poly[5].node == EnvNode::ReleaseEnd && poly[5].level == 0.0);
|
||||
// Monotone in x across the traced line.
|
||||
for (std::size_t i = 1; i < 6; ++i) CHECK(poly[i].x >= poly[i - 1].x);
|
||||
// Every vertex in-bounds.
|
||||
for (const EnvVertex& p : poly) {
|
||||
CHECK(p.x >= a.x && p.x <= a.right() - 1);
|
||||
CHECK(p.y >= a.y && p.y <= a.bottom() - 1);
|
||||
}
|
||||
CHECK(findNode(poly, EnvNode::ReleaseEnd, v));
|
||||
CHECK(v.x == a.right() - 1); // ANCHORED, whatever the release is
|
||||
}
|
||||
|
||||
static void testGateSustainPlateauFixedWidth() {
|
||||
// The sustain plateau is ALWAYS the reserved width (canvas - timed region), independent of
|
||||
// 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;
|
||||
// The layout failure this policy exists to fix: at zero release the sustain plateau must run to
|
||||
// (near) the right edge instead of the figure bunching left.
|
||||
static void testZeroReleasePutsTheSustainPlateauAtTheRightEdge() {
|
||||
const Rect a = wideArea();
|
||||
const int plateauPx = a.width - gateTimedWidth(a); // 150
|
||||
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, overlayOf(a), 2.0);
|
||||
|
||||
EnvVertex decay, plateauEnd;
|
||||
const std::vector<EnvVertex> poly =
|
||||
buildEnvelopePolyline(ahdsr(0.05, 0.0, 0.05, 0.7, 0.0), overlayOf(a), 4.0);
|
||||
EnvVertex plateau, end;
|
||||
CHECK(findNode(poly, EnvNode::ReleaseStart, plateau));
|
||||
CHECK(findNode(poly, EnvNode::ReleaseEnd, end));
|
||||
CHECK(end.x == a.right() - 1);
|
||||
// One node separation short of the edge — the plateau spans essentially the whole canvas.
|
||||
CHECK(plateau.x == a.right() - 1 - kGateNodeSepPx);
|
||||
EnvVertex decay;
|
||||
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
|
||||
CHECK(plateau.x - decay.x > a.width / 2);
|
||||
}
|
||||
|
||||
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;
|
||||
// The release END never moves; the release START is what a longer release pushes left.
|
||||
static void testReleaseGrowsLeftwardFromTheAnchor() {
|
||||
const Rect a = wideArea();
|
||||
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, overlayOf(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);
|
||||
EnvVertex shortStart, longStart, shortEnd, longEnd;
|
||||
const std::vector<EnvVertex> shortR =
|
||||
buildEnvelopePolyline(ahdsr(0.1, 0.0, 0.1, 0.5, 0.1), overlayOf(a), 4.0);
|
||||
const std::vector<EnvVertex> longR =
|
||||
buildEnvelopePolyline(ahdsr(0.1, 0.0, 0.1, 0.5, 1.5), overlayOf(a), 4.0);
|
||||
CHECK(findNode(shortR, EnvNode::ReleaseStart, shortStart));
|
||||
CHECK(findNode(longR, EnvNode::ReleaseStart, longStart));
|
||||
CHECK(findNode(shortR, EnvNode::ReleaseEnd, shortEnd));
|
||||
CHECK(findNode(longR, EnvNode::ReleaseEnd, longEnd));
|
||||
CHECK(longStart.x < shortStart.x);
|
||||
CHECK(shortEnd.x == longEnd.x);
|
||||
}
|
||||
|
||||
static void testGateOverrunCompressesFromRight() {
|
||||
// Stages BEYOND the schematic domain (4.0s each > kGateStageMaxSeconds): the layout
|
||||
// compresses from the right preserving the minimum gaps — ReleaseEnd pins to the last
|
||||
// in-bounds column, but the trailing nodes stay strictly increasing and individually
|
||||
// separated (>= kGateNodeSepPx), NOT piled on one pixel. NOTHING maps past area.right().
|
||||
AmpEnvelope env;
|
||||
env.mode = EnvMode::Gate;
|
||||
env.attackSeconds = 4.0;
|
||||
env.holdSeconds = 4.0;
|
||||
env.decaySeconds = 4.0;
|
||||
env.sustainLevel = 0.7;
|
||||
env.releaseSeconds = 4.0;
|
||||
// Tier-0 defaults are zero hold and zero decay; every node still has to be independently
|
||||
// grabbable, which is what the per-segment separation base buys.
|
||||
static void testTierZeroDefaultsKeepEveryNodeDistinct() {
|
||||
const Rect a = wideArea();
|
||||
const std::vector<EnvVertex> poly = buildEnvelopePolyline(env, overlayOf(a), 2.0);
|
||||
CHECK(poly.size() == 6);
|
||||
|
||||
EnvVertex plateauEnd, rel;
|
||||
CHECK(findNode(poly, EnvNode::ReleaseStart, plateauEnd));
|
||||
CHECK(findNode(poly, EnvNode::ReleaseEnd, rel));
|
||||
CHECK(rel.x == a.right() - 1); // pinned to the last in-bounds column
|
||||
CHECK(plateauEnd.level == 0.7); // still at sustain
|
||||
for (size_t i = 1; i < poly.size(); ++i) {
|
||||
CHECK(poly[i].x > poly[i - 1].x); // strictly monotonic
|
||||
CHECK(poly[i].x - poly[i - 1].x >= kGateNodeSepPx - 1); // min gaps survive compression
|
||||
CHECK(poly[i].x >= a.x && poly[i].x < a.right()); // in-bounds
|
||||
const std::vector<EnvVertex> poly =
|
||||
buildEnvelopePolyline(ahdsr(0.003, 0.0, 0.0, 1.0, 0.060), overlayOf(a), 4.0);
|
||||
for (std::size_t i = 1; i < 6; ++i) {
|
||||
CHECK(poly[i].x - poly[i - 1].x >= kGateNodeSepPx - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void testGateAllVerticesInBounds() {
|
||||
// The FA2 bounds invariant, swept over representative param sets (including extremes): every
|
||||
// vertex of every polyline stays inside the canvas rect.
|
||||
// Every stage maxed: the schematic exactly fills the canvas, the plateau collapses to its
|
||||
// minimum gap, and nothing escapes the rect.
|
||||
static void testMaxedStagesCompressWithoutOverrunning() {
|
||||
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;
|
||||
// ABSURD stage values must clamp in double space, not overflow the integer cast (32-bit
|
||||
// long on Windows would wrap negative and land on the WRONG edge).
|
||||
AmpEnvelope huge = base;
|
||||
huge.mode = EnvMode::Gate;
|
||||
huge.releaseSeconds = 1e12;
|
||||
const double m = kGateStageMaxSeconds;
|
||||
const std::vector<EnvVertex> poly =
|
||||
buildEnvelopePolyline(ahdsr(m, m, m, 0.5, m), overlayOf(a), 4.0);
|
||||
for (std::size_t i = 1; i < 6; ++i) {
|
||||
CHECK(poly[i].x >= poly[i - 1].x);
|
||||
CHECK(poly[i].x <= a.right() - 1);
|
||||
}
|
||||
EnvVertex end;
|
||||
CHECK(findNode(poly, EnvNode::ReleaseEnd, end));
|
||||
CHECK(end.x == a.right() - 1);
|
||||
}
|
||||
|
||||
for (const AmpEnvelope& env : {base, big, zero, trig, huge}) {
|
||||
for (const EnvVertex& v : buildEnvelopePolyline(env, overlayOf(a), 2.0)) {
|
||||
CHECK(v.x >= a.x && v.x < a.right());
|
||||
CHECK(v.y >= a.y && v.y < a.bottom());
|
||||
// --- the AHD split ------------------------------------------------------------
|
||||
|
||||
// The combined-time bound, asserted structurally across the full domains: no (attack, decay,
|
||||
// fraction) triple can exceed the span, and no clamp on the SUM exists to be exercised.
|
||||
static void testAhdSplitNeverExceedsTheSpan() {
|
||||
const double span = 3.0;
|
||||
for (int ai = 0; ai <= 20; ++ai) {
|
||||
for (int di = 0; di <= 20; ++di) {
|
||||
for (int fi = 0; fi <= 10; ++fi) {
|
||||
const StageEnvelope e =
|
||||
ahd(ai * 0.25, di * 0.25, fi * 0.1, 0.0, span);
|
||||
const AhdSplit s = splitAhdSeconds(e);
|
||||
CHECK(s.attack >= 0.0 && s.hold >= 0.0 && s.decay >= 0.0);
|
||||
CHECK(s.total <= span + 1e-9);
|
||||
CHECK(std::fabs(s.total - (s.attack + s.hold + s.decay)) < 1e-12);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Trigger polyline ---------------------------------------------------------
|
||||
static void testHoldFractionEndpoints() {
|
||||
const StageEnvelope none = ahd(0.5, 0.5, 0.0, 0.0, 4.0);
|
||||
const AhdSplit s0 = splitAhdSeconds(none);
|
||||
CHECK(s0.hold == 0.0);
|
||||
CHECK(std::fabs(s0.total - 1.0) < 1e-12);
|
||||
|
||||
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, overlayOf(a), 2.0);
|
||||
const StageEnvelope full = ahd(0.5, 0.5, 1.0, 0.0, 4.0);
|
||||
const AhdSplit s1 = splitAhdSeconds(full);
|
||||
// 100% of what attack and decay left: 4 - 0.5 - 0.5 = 3.
|
||||
CHECK(std::fabs(s1.hold - 3.0) < 1e-12);
|
||||
CHECK(std::fabs(s1.total - 4.0) < 1e-12);
|
||||
|
||||
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.x + 100 && v.level == 1.0);
|
||||
CHECK(findNode(poly, EnvNode::FadeOutStart, v) && v.x == a.x + 350 && v.level == 1.0);
|
||||
CHECK(findNode(poly, EnvNode::LengthEnd, v) && v.x == a.x + 500 && v.level == 0.0);
|
||||
// Attack + decay alone longer than the span: they fit by their own per-stage bounds and the
|
||||
// remainder — and therefore hold — is zero. Still no clamp on the sum.
|
||||
const AhdSplit s2 = splitAhdSeconds(ahd(3.0, 3.0, 1.0, 0.0, 4.0));
|
||||
CHECK(std::fabs(s2.attack - 3.0) < 1e-12);
|
||||
CHECK(std::fabs(s2.decay - 1.0) < 1e-12);
|
||||
CHECK(s2.hold == 0.0);
|
||||
CHECK(std::fabs(s2.total - 4.0) < 1e-12);
|
||||
}
|
||||
|
||||
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, overlayOf(a), 2.0);
|
||||
// --- the AHD polyline ---------------------------------------------------------
|
||||
|
||||
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.x + 800);
|
||||
// The 1:1 property: a stage boundary at N seconds sits over the waveform at N seconds.
|
||||
static void testAhdIsOneToOneWithTheTimeAxis() {
|
||||
const Rect a = wideArea();
|
||||
const double total = 8.0;
|
||||
const StageEnvelope e = ahd(1.0, 2.0, 0.5, 1.0, 6.0);
|
||||
const std::vector<EnvVertex> poly = buildEnvelopePolyline(e, overlayOf(a), total);
|
||||
const AhdSplit s = splitAhdSeconds(e);
|
||||
EnvVertex origin, attack, hold, decay;
|
||||
CHECK(findNode(poly, EnvNode::Origin, origin));
|
||||
CHECK(findNode(poly, EnvNode::AttackEnd, attack));
|
||||
CHECK(findNode(poly, EnvNode::HoldEnd, hold));
|
||||
CHECK(findNode(poly, EnvNode::DecayEnd, decay));
|
||||
CHECK(origin.x == timeToX(a, total, 1.0));
|
||||
CHECK(attack.x == timeToX(a, total, 1.0 + s.attack));
|
||||
CHECK(hold.x == timeToX(a, total, 1.0 + s.attack + s.hold));
|
||||
CHECK(decay.x == timeToX(a, total, 1.0 + s.total));
|
||||
// Levels: rises to unity, holds, falls to zero. No sustain-only nodes exist.
|
||||
CHECK(origin.level == 0.0 && attack.level == 1.0 && hold.level == 1.0 && decay.level == 0.0);
|
||||
CHECK(!hasNode(poly, EnvNode::ReleaseStart));
|
||||
CHECK(!hasNode(poly, EnvNode::ReleaseEnd));
|
||||
CHECK(!hasNode(poly, EnvNode::ReleaseCurve));
|
||||
}
|
||||
|
||||
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, overlayOf(a), 2.0);
|
||||
// --- curve knots --------------------------------------------------------------
|
||||
|
||||
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);
|
||||
// A knot rides every sloped stage that has a duration, and none that does not — a zero-length
|
||||
// stage has no interior to put a handle in.
|
||||
static void testKnotsRideOnlySlopedNonZeroSegments() {
|
||||
const Rect a = wideArea();
|
||||
const std::vector<EnvVertex> full =
|
||||
buildEnvelopePolyline(ahdsr(0.2, 0.2, 0.2, 0.5, 0.2), overlayOf(a), 4.0);
|
||||
CHECK(hasNode(full, EnvNode::AttackCurve));
|
||||
CHECK(hasNode(full, EnvNode::DecayCurve));
|
||||
CHECK(hasNode(full, EnvNode::ReleaseCurve));
|
||||
|
||||
const std::vector<EnvVertex> flat =
|
||||
buildEnvelopePolyline(ahdsr(0.0, 0.2, 0.0, 0.5, 0.0), overlayOf(a), 4.0);
|
||||
CHECK(!hasNode(flat, EnvNode::AttackCurve));
|
||||
CHECK(!hasNode(flat, EnvNode::DecayCurve));
|
||||
CHECK(!hasNode(flat, EnvNode::ReleaseCurve));
|
||||
|
||||
const std::vector<EnvVertex> ahdPoly =
|
||||
buildEnvelopePolyline(ahd(0.5, 0.5, 0.5, 0.0, 4.0), overlayOf(a), 4.0);
|
||||
CHECK(hasNode(ahdPoly, EnvNode::AttackCurve));
|
||||
CHECK(hasNode(ahdPoly, EnvNode::DecayCurve));
|
||||
// Every knot is flagged as one and every stage node is not.
|
||||
for (const EnvVertex& v : ahdPoly) {
|
||||
const bool isKnot = v.node == EnvNode::AttackCurve || v.node == EnvNode::DecayCurve;
|
||||
CHECK(v.knot == isKnot);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Degenerate ---------------------------------------------------------------
|
||||
// The knot's HEIGHT is the exponent, read through the shared law: neutral sits at the segment
|
||||
// midpoint level, a larger exponent pulls the attack knot toward the floor, a smaller one
|
||||
// toward the ceiling. This is the visible half of the one-model rule.
|
||||
static void testKnotHeightTracksTheExponent() {
|
||||
const Rect a = wideArea();
|
||||
StageEnvelope e = ahdsr(0.4, 0.0, 0.0, 1.0, 0.0);
|
||||
EnvVertex neutral, steep, shallow;
|
||||
|
||||
static void testDegenerateFlatBaseline() {
|
||||
AmpEnvelope env; // any params
|
||||
const Rect zeroW = Rect::ltrb(0, 0, 0, 100);
|
||||
const std::vector<EnvVertex> p1 = buildEnvelopePolyline(env, overlayOf(zeroW), 2.0);
|
||||
CHECK(p1.size() == 2); // always a drawable line
|
||||
CHECK(p1.front().level == 0.0 && p1.back().level == 0.0);
|
||||
e.attackCurve = 1.0;
|
||||
CHECK(findNode(buildEnvelopePolyline(e, overlayOf(a), 4.0), EnvNode::AttackCurve, neutral));
|
||||
CHECK(std::fabs(neutral.level - 0.5) < 1e-12); // linear: half way up at half way across
|
||||
CHECK(neutral.y == levelToY(a, 0.5));
|
||||
|
||||
const Rect ok = wideArea();
|
||||
const std::vector<EnvVertex> p2 = buildEnvelopePolyline(env, overlayOf(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.x && p2.back().x == ok.right() - 1); // spans the area, in-bounds
|
||||
e.attackCurve = 4.0;
|
||||
CHECK(findNode(buildEnvelopePolyline(e, overlayOf(a), 4.0), EnvNode::AttackCurve, steep));
|
||||
CHECK(steep.level < neutral.level);
|
||||
CHECK(steep.y > neutral.y); // lower on screen
|
||||
|
||||
e.attackCurve = 0.25;
|
||||
CHECK(findNode(buildEnvelopePolyline(e, overlayOf(a), 4.0), EnvNode::AttackCurve, shallow));
|
||||
CHECK(shallow.level > neutral.level);
|
||||
CHECK(shallow.y < neutral.y);
|
||||
|
||||
// The knot sits between its segment's endpoints in x, and inside the canvas in y.
|
||||
CHECK(steep.x > a.x && steep.x < a.right() - 1);
|
||||
CHECK(steep.y >= a.y && steep.y <= a.bottom() - 1);
|
||||
}
|
||||
|
||||
// --- degenerate ---------------------------------------------------------------
|
||||
|
||||
static void testDegenerateSurfaceYieldsFlatBaseline() {
|
||||
const std::vector<EnvVertex> zeroArea =
|
||||
buildEnvelopePolyline(ahdsr(0.1, 0.1, 0.1, 0.5, 0.1), overlayOf(Rect{}), 4.0);
|
||||
CHECK(zeroArea.size() == 2);
|
||||
CHECK(zeroArea[0].level == 0.0 && zeroArea[1].level == 0.0);
|
||||
const std::vector<EnvVertex> zeroDur =
|
||||
buildEnvelopePolyline(ahd(0.1, 0.1, 0.5, 0.0, 1.0), overlayOf(wideArea()), 0.0);
|
||||
CHECK(zeroDur.size() == 2);
|
||||
}
|
||||
|
||||
int main() {
|
||||
testTimeToXEndpoints();
|
||||
testTimeToXNegativePinsLeft();
|
||||
testTimeToXPastEndClamps();
|
||||
testTimeToXDegenerate();
|
||||
testGateTimedWidth();
|
||||
testGatePxPerSecond();
|
||||
testLevelToYEndpoints();
|
||||
testLevelToYClamps();
|
||||
testTimeToXClampsBothEnds();
|
||||
testLevelToY();
|
||||
testDegenerateAreaAndDuration();
|
||||
|
||||
testGateNodeOrderAndLevels();
|
||||
testGateSchematicPlacement();
|
||||
testGateLayoutIndependentOfSampleDuration();
|
||||
testGateMinSeparationAtDefaults();
|
||||
testGateSustainPlateauFixedWidth();
|
||||
testGateReleaseVisibleInBounds();
|
||||
testGateOverrunCompressesFromRight();
|
||||
testGateAllVerticesInBounds();
|
||||
testAhdsrNodeOrderAndLevels();
|
||||
testZeroReleasePutsTheSustainPlateauAtTheRightEdge();
|
||||
testReleaseGrowsLeftwardFromTheAnchor();
|
||||
testTierZeroDefaultsKeepEveryNodeDistinct();
|
||||
testMaxedStagesCompressWithoutOverrunning();
|
||||
|
||||
testTriggerShape();
|
||||
testTriggerFadeOverlapClamp();
|
||||
testTriggerFullLengthZeroFadeOutInBounds();
|
||||
testAhdSplitNeverExceedsTheSpan();
|
||||
testHoldFractionEndpoints();
|
||||
testAhdIsOneToOneWithTheTimeAxis();
|
||||
|
||||
testDegenerateFlatBaseline();
|
||||
testKnotsRideOnlySlopedNonZeroSegments();
|
||||
testKnotHeightTracksTheExponent();
|
||||
|
||||
testDegenerateSurfaceYieldsFlatBaseline();
|
||||
|
||||
if (g_fail == 0) std::printf("envelope_overlay: all tests passed\n");
|
||||
else std::printf("envelope_overlay: %d FAILED\n", g_fail);
|
||||
|
||||
Reference in New Issue
Block a user