339 lines
14 KiB
C++
339 lines
14 KiB
C++
// 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/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.
|
|
//
|
|
// Covers: timeToX / levelToY (linear maps, edge clamps, past-end CLAMPED to right-1 — the FA2
|
|
// bounds invariant, degenerate area/duration); gateTimedWidth; buildEnvelopePolyline Gate (node
|
|
// 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
|
|
// clamp, full-length/zero-fade-out nodes in-bounds at right-1); 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 - 1); // t=total -> last in-bounds column
|
|
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 testTimeToXPastEndClamps() {
|
|
// FA2 bounds invariant: t past total pins to the last in-bounds column, never past right.
|
|
const Rect a = wideArea();
|
|
CHECK(timeToX(a, 2.0, 3.0) == a.right - 1);
|
|
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() {
|
|
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 testGateSchematicPlacement() {
|
|
// FA2 bounded schematic: timed region = 850px (150px reserved plateau), total 2.0s =>
|
|
// 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;
|
|
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;
|
|
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 + 85);
|
|
CHECK(findNode(poly, EnvNode::HoldEnd, v) && v.x == a.left + 128);
|
|
CHECK(findNode(poly, EnvNode::DecayEnd, v) && v.x == a.left + 255);
|
|
CHECK(findNode(poly, EnvNode::ReleaseStart, v) && v.x == a.left + 405);
|
|
CHECK(findNode(poly, EnvNode::ReleaseEnd, v) && v.x == a.left + 575);
|
|
}
|
|
|
|
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;
|
|
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;
|
|
env.mode = EnvMode::Gate;
|
|
env.attackSeconds = 1.0;
|
|
env.holdSeconds = 1.0;
|
|
env.decaySeconds = 1.0; // decay end at 3.0s -> already past the timed span
|
|
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(decay.x == a.right - 1); // pinned to the last in-bounds column
|
|
CHECK(plateauEnd.x == a.right - 1);
|
|
CHECK(rel.x == a.right - 1);
|
|
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 ---------------------------------------------------------
|
|
|
|
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);
|
|
}
|
|
|
|
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 ---------------------------------------------------------------
|
|
|
|
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 - 1); // spans the area, in-bounds
|
|
}
|
|
|
|
int main() {
|
|
testTimeToXEndpoints();
|
|
testTimeToXNegativePinsLeft();
|
|
testTimeToXPastEndClamps();
|
|
testTimeToXDegenerate();
|
|
testGateTimedWidth();
|
|
testLevelToYEndpoints();
|
|
testLevelToYClamps();
|
|
|
|
testGateNodeOrderAndLevels();
|
|
testGateSchematicPlacement();
|
|
testGateSustainPlateauFixedWidth();
|
|
testGateReleaseVisibleInBounds();
|
|
testGateOverrunClampsToCanvas();
|
|
testGateAllVerticesInBounds();
|
|
|
|
testTriggerShape();
|
|
testTriggerFadeOverlapClamp();
|
|
testTriggerFullLengthZeroFadeOutInBounds();
|
|
|
|
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;
|
|
}
|