fix(amps): clamp Modern High-Gain sag gain (no phase inversion)

Review blocker: sag gain 1 - sag*envelope went negative because the envelope
follows the post-Presence/Depth-shelf signal, which exceeds 1 (shelves add up to
+8/+6 dB) — at high sag+presence+depth it phase-inverted loud content instead of
drooping (verified ~-1.0 correlation). Clamp the sag gain to [sagGainFloor(0.15),
1] so it always attenuates, never inverts. Doc/const nits fixed (per-const docs,
"switches" not "sweeps" for the binary Tight). Regression test asserts positive
correlation (no inversion) at presence/depth/sag maxed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-03 16:25:26 -07:00
parent f7787c4ba3
commit b4a04f6dc8
2 changed files with 32 additions and 5 deletions
@@ -26,10 +26,16 @@ public final class ModernHighGainAmpPedal: BoutiqueAmpPedal {
private let sagAttackSeconds: Double = 0.02
/// Sag envelope-follower release — slow, so the sag lingers and blooms back (supply recovery).
private let sagReleaseSeconds: Double = 0.30
/// Tight low-cut: high-pass corner sweeps from ~20 Hz (loose, no cut) to `tightFreqHz`.
/// Tight low-cut: high-pass corner is ~20 Hz (loose, no cut) or `tightFreqHz` (tight).
private let tightLooseFreqHz: Double = 20.0
/// Tight-switch-on high-pass corner in Hz.
private let tightFreqHz: Double = 120.0
/// Tight high-pass Q (Butterworth).
private let tightQ: Double = 0.707
/// Lowest the sag gain can droop to. Bounds sag depth and — crucially — keeps the gain strictly
/// positive: the envelope tracks a post-shelf signal that can exceed 1, so an unclamped
/// `1 − sag·env` would go negative and phase-invert loud content instead of drooping.
private let sagGainFloor: Double = 0.15
public override var toneStackModel: ToneStackModel { .mesa }
public override var summaryText: String {
@@ -65,8 +71,8 @@ public final class ModernHighGainAmpPedal: BoutiqueAmpPedal {
cab = 1.0 // British 4×12 — tight, aggressive
}
/// Tight switch: a pre-preamp high-pass whose corner sweeps from ~20 Hz (loose) to
/// `tightFreqHz` (tight), firming the low end without a rebuild.
/// Tight switch: a pre-preamp high-pass whose corner switches between ~20 Hz (loose, negligible
/// cut) and `tightFreqHz` (tight), firming the low end — live, no rebuild.
public override func inputShaping(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput {
let cutoff: LinLin.Audio = LinLin.Audio(
$tight, inLo: 0.0, inHi: 1.0, outLo: tightLooseFreqHz, outHi: tightFreqHz)
@@ -79,8 +85,11 @@ public final class ModernHighGainAmpPedal: BoutiqueAmpPedal {
let shaped: any ValidGeneratorInput = super.shapeVoice(signal)
let envelope: AmplitudeFollower.Audio = AmplitudeFollower.Audio(
in: shaped, attackTime: sagAttackSeconds, releaseTime: sagReleaseSeconds)
// Gain droops toward (1 − sag) as the envelope rises; |signal| ≤ ~1 so gain stays in [0, 1].
let sagGain: any ValidGeneratorInput = 1.0 - ($sag * envelope)
// Gain droops toward `sagGainFloor` as the envelope rises. The Presence/Depth shelves can
// push the envelope above 1, so clamp to `[sagGainFloor, 1]` — an unclamped `1 − sag·env`
// would go negative and phase-invert loud content instead of attenuating it.
let sagGain: any ValidGeneratorInput = Clip.Audio(
1.0 - ($sag * envelope), lo: sagGainFloor, hi: 1.0)
return shaped * sagGain
}
}
@@ -63,6 +63,24 @@ struct ModernHighGainAmpPedalTests {
#expect(windowedSSD(stiff, saggy) > 0, "Sag had no effect")
}
@Test func sagAttenuatesWithoutPhaseInversion() throws {
// With Presence/Depth maxed the shelf boosts push the sag envelope above 1; the sag gain
// must stay positive (attenuation), never go negative (phase inversion). Assert the sag'd
// output correlates positively with the stiff one (not an inverted copy).
let cfg: (ModernHighGainAmpPedal) -> Void = { (p: ModernHighGainAmpPedal) in
p.gain = 20.0; p.channel = 1.0; p.presence = 1.0; p.depth = 1.0
}
let stiff: [Float] = try render { cfg($0); $0.sag = 0.0 }
let saggy: [Float] = try render { cfg($0); $0.sag = 1.0 }
let skip: Int = 4096, window: Int = 4096
guard stiff.count >= skip + window, saggy.count >= skip + window else {
Issue.record("render too short"); return
}
let corr: Double = zip(stiff[skip..<(skip + window)], saggy[skip..<(skip + window)])
.reduce(0.0) { acc, pair in acc + Double(pair.0) * Double(pair.1) }
#expect(corr > 0, "sag phase-inverted the signal (correlation \(corr) < 0)")
}
@Test func tightSwitchChangesOutput() throws {
let loose: [Float] = try render { $0.tight = 0.0 }
let tight: [Float] = try render { $0.tight = 1.0 }