// envelope_overlay.cpp — see envelope_overlay.h. Pure geometry; no host types. #include "envelope_overlay.h" #include namespace reasampler::vst { int timeToX(const Rect& area, double totalSeconds, double t) { const int w = std::max(0, area.width()); if (w <= 0 || totalSeconds <= 0.0) return area.left; if (t < 0.0) t = 0.0; // Linear map, clamped on BOTH sides (FA2 bounds invariant): t past totalSeconds pins to the // last in-bounds column area.right-1. Round to the nearest pixel. const double frac = t / totalSeconds; long xi = static_cast(frac * static_cast(w) + 0.5); if (xi > w - 1) xi = w - 1; return area.left + static_cast(xi); } int gateTimedWidth(const Rect& area) { const int w = std::max(0, area.width()); if (w <= 0) return 0; const int sustainPx = static_cast(kGateSustainDisplayFraction * static_cast(w) + 0.5); return std::max(1, w - sustainPx); } int levelToY(const Rect& area, double level) { const int h = std::max(0, area.height()); if (h <= 0) return area.top; if (level < 0.0) level = 0.0; if (level > 1.0) level = 1.0; // Level 1 -> top row, level 0 -> bottom row (bottom-1 under the half-open convention). The // range spans (h-1) pixels so both endpoints land ON a drawable row. const int span = h - 1; const long dy = static_cast((1.0 - level) * static_cast(span) + 0.5); return area.top + static_cast(dy); } namespace { double clamp01(double v) { if (v < 0.0) return 0.0; if (v > 1.0) return 1.0; return v; } EnvVertex vtx(EnvNode node, const Rect& area, double totalSeconds, double t, double level) { EnvVertex v; v.node = node; v.x = timeToX(area, totalSeconds, t); v.y = levelToY(area, level); v.level = level; 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(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(xi); v.y = levelToY(area, level); v.level = level; return v; } std::vector gatePolyline(const AmpEnvelope& env, const Rect& area, double totalSeconds) { // 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 h = std::max(0.0, env.holdSeconds); const double d = std::max(0.0, env.decaySeconds); const double r = std::max(0.0, env.releaseSeconds); const double sus = clamp01(env.sustainLevel); // BOUNDED SCHEMATIC (FA2): A/H/D and R map onto the TIMED region (canvas minus the reserved // sustain-plateau width) at the sample's time scale; the sustain plateau is the fixed reserve // between DecayEnd and ReleaseStart. Cumulative px, clamped in gateVtx, stay monotonic. const int timedW = gateTimedWidth(area); const int sustainPx = std::max(0, area.width()) - timedW; const double pxPerSec = static_cast(timedW) / totalSeconds; const double pxAttack = a * pxPerSec; const double pxHold = (a + h) * pxPerSec; const double pxDecay = (a + h + d) * pxPerSec; const double pxPlateau = pxDecay + static_cast(sustainPx); // schematic note-off const double pxRelease = pxPlateau + r * pxPerSec; std::vector pts; pts.reserve(6); pts.push_back(gateVtx(EnvNode::Origin, area, 0.0, 0.0)); pts.push_back(gateVtx(EnvNode::AttackEnd, area, pxAttack, 1.0)); pts.push_back(gateVtx(EnvNode::HoldEnd, area, pxHold, 1.0)); pts.push_back(gateVtx(EnvNode::DecayEnd, area, pxDecay, sus)); // sustain node pts.push_back(gateVtx(EnvNode::ReleaseStart, area, pxPlateau, sus)); // plateau end pts.push_back(gateVtx(EnvNode::ReleaseEnd, area, pxRelease, 0.0)); return pts; } std::vector triggerPolyline(const AmpEnvelope& env, const Rect& area, double totalSeconds) { // The played span is lengthFraction of the whole sample; fades are fractions OF that span. const double len = clamp01(env.lengthFraction); double fadeIn = clamp01(env.fadeInFraction); double fadeOut = clamp01(env.fadeOutFraction); // Fades cannot overlap: clamp so fadeIn + fadeOut <= 1 (of the played span), mirroring the // engine's TriggerParams clamp. Trim the LATER fade (fade-out) first, matching the engine. if (fadeIn + fadeOut > 1.0) fadeOut = std::max(0.0, 1.0 - fadeIn); const double playSeconds = len * totalSeconds; const double tFadeInEnd = fadeIn * playSeconds; const double tFadeOutStart = playSeconds - fadeOut * playSeconds; // where fade-out begins std::vector pts; pts.reserve(4); pts.push_back(vtx(EnvNode::Origin, area, totalSeconds, 0.0, 0.0)); pts.push_back(vtx(EnvNode::FadeInEnd, area, totalSeconds, tFadeInEnd, 1.0)); pts.push_back(vtx(EnvNode::FadeOutStart, area, totalSeconds, tFadeOutStart, 1.0)); // unity end pts.push_back(vtx(EnvNode::LengthEnd, area, totalSeconds, playSeconds, 0.0)); // playEnd return pts; } } // namespace std::vector buildEnvelopePolyline(const AmpEnvelope& env, const Rect& area, double totalSeconds) { if (area.width() <= 0 || area.height() <= 0 || totalSeconds <= 0.0) { // Degenerate surface: a two-point flat baseline at level 0 so the shell always has a line. return {vtx(EnvNode::Origin, area, 1.0, 0.0, 0.0), vtx(EnvNode::ReleaseEnd, area, 1.0, 1.0, 0.0)}; } return env.mode == EnvMode::Gate ? gatePolyline(env, area, totalSeconds) : triggerPolyline(env, area, totalSeconds); } } // namespace reasampler::vst