fix(amps): true ±1 ceiling — hard safety clamp after the tanh stage

Review follow-up: the tanh output stage soft-limits but does not strictly bound
output — its DC-blocker (Nyquist gain ~1.0025) and the halfband oversampling
filters can leave a sub-percent transient overshoot above ±1, which the DAC
would still clip. The prior "can never overshoot" doc claim was overstated.

Add a final Clip(±1) after the tanh: the tanh does the musical analog-style
soft-limiting, the clamp only catches the inaudible ripple residue, so the amp
stage provably never emits a sample past ±1. Docs corrected (soft-limit is a
digital safety ceiling placed after the cab, not literal circuit order; master
Level ≤ 1 carries the ceiling through). Ceiling test tightened 1.05 → 1.0+1e-4.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 21:15:16 -07:00
parent fb50538e84
commit 544a07d569
2 changed files with 24 additions and 12 deletions
@@ -17,14 +17,17 @@ import LyrebirdUI
/// → TriodeStage(power-amp drive) (power-amp warmth) /// → TriodeStage(power-amp drive) (power-amp warmth)
/// → shapeVoice(_:) (subclass signature filter) /// → shapeVoice(_:) (subclass signature filter)
/// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, rebuild on change) /// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, rebuild on change)
/// → TriodeStage(bias: 0) (output soft-limit — analog-style, ±1 ceiling) /// → TriodeStage(bias: 0) → Clip(±1) (output soft-limit + hard safety ceiling)
/// ``` /// ```
/// Fully wet (the amp replaces the dry signal); `Level` is the master. Double-width amp-head card. /// Fully wet (the amp replaces the dry signal); `Level` is the master. Double-width amp-head card.
/// ///
/// The final `tanh` output stage models output-transformer / speaker compression: a real amp has no /// The `tanh` output stage models output-transformer / speaker compression: a real amp has no
/// brickwall, and its analog output can't digital-clip. With unit small-signal gain it leaves clean /// brickwall, and its analog output can't digital-clip. Small signals pass at ~unit gain (clean
/// playing untouched while soft-limiting peaks to the ±1 ceiling — so, combined with the /// playing largely intact); peaks soft-limit toward ±1, adding gentle output compression. Because
/// attenuate-only master `Level`, the amp can never overshoot into a hard digital clip. /// that soft-limit sits *after* the cab (the overshoot it guards is a digital convolution /
/// oversampling artifact, not literal circuit order) and the `tanh`'s own DC-blocker can leave a
/// sub-percent overshoot, a final `Clip(±1)` hard-clamps the residue so the amp stage never emits a
/// sample past ±1. At the normal master `Level` (≤ 1) that ceiling carries through to the output.
/// ///
/// Subclasses override ``toneStackModel``, ``summaryText``, ``signatureKnobs``, and /// Subclasses override ``toneStackModel``, ``summaryText``, ``signatureKnobs``, and
/// ``shapeVoice(_:)``, and may set a voice-appropriate default `gain` in their `init`. /// ``shapeVoice(_:)``, and may set a voice-appropriate default `gain` in their `init`.
@@ -78,6 +81,10 @@ open class AmpVoicePedal: Pedal {
private let outputStageBias: Double = 0.0 private let outputStageBias: Double = 0.0
/// 2× is enough: the cab low-pass already band-limits the signal feeding this stage. /// 2× is enough: the cab low-pass already band-limits the signal feeding this stage.
private let outputStageOversample: Int = 2 private let outputStageOversample: Int = 2
/// Absolute digital ceiling. The `tanh` stage does the musical soft-limiting; this hard clamp
/// only ever catches the sub-percent overshoot the oversampling filters and DC-blocker can leave
/// just above ±1, guaranteeing the amp stage never emits a sample the DAC would clip.
private let outputCeiling: Double = 1.0
let gainRange: ClosedRange<Double> = 1.0 ... 30.0 let gainRange: ClosedRange<Double> = 1.0 ... 30.0
let toneRange: ClosedRange<Double> = 0.0 ... 1.0 let toneRange: ClosedRange<Double> = 0.0 ... 1.0
@@ -150,11 +157,12 @@ open class AmpVoicePedal: Pedal {
/// ///
/// The closing `TriodeStage(bias: 0)` is a symmetric `tanh` output limiter: the cab convolution /// The closing `TriodeStage(bias: 0)` is a symmetric `tanh` output limiter: the cab convolution
/// and signature shelves can push peaks past ±1, which a real analog output would soft-compress /// and signature shelves can push peaks past ±1, which a real analog output would soft-compress
/// rather than hard-clip. Unit small-signal gain keeps clean tones intact; peaks asymptote to /// rather than hard-clip. Unit small-signal gain keeps clean tones intact; peaks soft-limit
/// ±1. /// toward ±1. The trailing `Clip(±1)` is a hard safety clamp for the sub-percent overshoot the
/// `tanh`'s DC-blocker and the halfband filters can leave — so no sample can clip the DAC.
/// ///
/// - Parameter dry: Dry mono input signal. /// - Parameter dry: Dry mono input signal.
/// - Returns: The amped, cab-convolved, soft-limited mono output. /// - Returns: The amped, cab-convolved, soft-limited, ceiling-clamped mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let sr: Double = sampleRate let sr: Double = sampleRate
let preamp: TriodeStage = TriodeStage(input: dry, drive: $gain, bias: preampBias, oversample: 4) let preamp: TriodeStage = TriodeStage(input: dry, drive: $gain, bias: preampBias, oversample: 4)
@@ -169,11 +177,14 @@ open class AmpVoicePedal: Pedal {
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
mix: cabinetMix mix: cabinetMix
) )
return TriodeStage( let softLimited: TriodeStage = TriodeStage(
input: cabinet, input: cabinet,
drive: outputStageDrive, drive: outputStageDrive,
bias: outputStageBias, bias: outputStageBias,
oversample: outputStageOversample oversample: outputStageOversample
) )
// Hard safety clamp: the tanh keeps the signal near ±1, but its DC-blocker and the halfband
// filters can leave a sub-percent transient overshoot — clamp it so no sample can clip the DAC.
return Clip.Audio(softLimited, lo: -outputCeiling, hi: outputCeiling)
} }
} }
@@ -98,8 +98,9 @@ struct AmpVoicePedalTests {
} }
@Test func outputStaysWithinDigitalCeiling() throws { @Test func outputStaysWithinDigitalCeiling() throws {
// The tanh output stage soft-limits to ±1 (a small margin allows for oversampling-filter // The tanh soft-limits and the trailing Clip(±1) hard-clamps, so no sample may exceed ±1
// ripple on transients). Drive every voice hard and check nothing hard-clips the DAC. // (a tiny epsilon covers float round-trip through the CAF). Drive every voice hard.
let ceilingEpsilon: Double = 1.0e-4
for voice in voices { for voice in voices {
let hot: [Float] = try render(voice.make) { (pedal: AmpVoicePedal) in let hot: [Float] = try render(voice.make) { (pedal: AmpVoicePedal) in
pedal.gain = 30.0 pedal.gain = 30.0
@@ -107,7 +108,7 @@ struct AmpVoicePedalTests {
pedal.treble = 1.0 pedal.treble = 1.0
} }
let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) } let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.05, "\(voice.name) peak \(peak) exceeds the digital ceiling") #expect(peak <= 1.0 + ceilingEpsilon, "\(voice.name) peak \(peak) exceeds the ±1 ceiling")
#expect(peak > 0.3, "\(voice.name) unexpectedly quiet at max gain: \(peak)") #expect(peak > 0.3, "\(voice.name) unexpectedly quiet at max gain: \(peak)")
} }
} }