2 Commits

Author SHA1 Message Date
josh 544a07d569 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>
2026-08-02 21:15:16 -07:00
josh fb50538e84 feat(amps): analog-style tanh output stage (no digital clipping)
The cab convolution and signature shelves can push amp output past ±1, which
would hard-clip the DAC. A real amp has no brickwall — the output transformer /
speaker soft-compress. Add a symmetric tanh output stage (TriodeStage bias 0,
2× oversampled) as the final node of AmpVoicePedal.effect(), after the cab.

Unit small-signal gain leaves clean playing untouched; peaks asymptote to ±1.
Combined with the attenuate-only master Level and fully-wet mix, the amp can no
longer overshoot into a hard digital clip. Test: every voice driven at max gain
+ tone stays within the digital ceiling.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-02 21:09:30 -07:00
2 changed files with 55 additions and 3 deletions
@@ -17,9 +17,18 @@ import LyrebirdUI
/// → TriodeStage(power-amp drive) (power-amp warmth)
/// → shapeVoice(_:) (subclass signature filter)
/// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, rebuild on change)
/// → 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.
///
/// The `tanh` output stage models output-transformer / speaker compression: a real amp has no
/// brickwall, and its analog output can't digital-clip. Small signals pass at ~unit gain (clean
/// playing largely intact); peaks soft-limit toward ±1, adding gentle output compression. Because
/// 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
/// ``shapeVoice(_:)``, and may set a voice-appropriate default `gain` in their `init`.
open class AmpVoicePedal: Pedal {
@@ -64,6 +73,18 @@ open class AmpVoicePedal: Pedal {
private let powerAmpDrive: Double = 1.5
private let powerAmpBias: Double = 0.0
private let cabinetMix: Double = 1.0
/// Output soft-limit drive. `1.0` gives unit small-signal gain (clean playing untouched) with a
/// hard `tanh` asymptote at ±1 — models output-transformer / speaker compression and keeps the
/// amp from ever digital-clipping the DAC.
private let outputStageDrive: Double = 1.0
/// Symmetric output stage (no even-harmonic bias) — a pure `tanh` limiter, not a tube colour.
private let outputStageBias: Double = 0.0
/// 2× is enough: the cab low-pass already band-limits the signal feeding this stage.
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 toneRange: ClosedRange<Double> = 0.0 ... 1.0
@@ -127,15 +148,21 @@ open class AmpVoicePedal: Pedal {
// MARK: - Effect
/// Preamp → fixed tone stack → power amp → signature shaping → cabinet (voice-selected).
/// Preamp → fixed tone stack → power amp → signature shaping → cabinet → output soft-limit.
///
/// > Note: only the voice named by the live `cab` selector is instantiated, so exactly one
/// > `ConvolutionReverb` runs. The `cab` selector is `rebuildsOnChange`, so switching cabs
/// > rebuilds the board (re-entering `effect(_:)` with the new voice) rather than running every
/// > voice at once.
///
/// 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
/// rather than hard-clip. Unit small-signal gain keeps clean tones intact; peaks soft-limit
/// 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.
/// - Returns: The amped, cab-convolved mono output.
/// - Returns: The amped, cab-convolved, soft-limited, ceiling-clamped mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let sr: Double = sampleRate
let preamp: TriodeStage = TriodeStage(input: dry, drive: $gain, bias: preampBias, oversample: 4)
@@ -145,10 +172,19 @@ open class AmpVoicePedal: Pedal {
input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2)
let shaped: any ValidGeneratorInput = shapeVoice(powerAmp)
let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cab)
return ConvolutionReverb.Audio(
let cabinet: ConvolutionReverb.Audio = ConvolutionReverb.Audio(
input: shaped,
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
mix: cabinetMix
)
let softLimited: TriodeStage = TriodeStage(
input: cabinet,
drive: outputStageDrive,
bias: outputStageBias,
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)
}
}
@@ -97,6 +97,22 @@ struct AmpVoicePedalTests {
}
}
@Test func outputStaysWithinDigitalCeiling() throws {
// The tanh soft-limits and the trailing Clip(±1) hard-clamps, so no sample may exceed ±1
// (a tiny epsilon covers float round-trip through the CAF). Drive every voice hard.
let ceilingEpsilon: Double = 1.0e-4
for voice in voices {
let hot: [Float] = try render(voice.make) { (pedal: AmpVoicePedal) in
pedal.gain = 30.0
pedal.bass = 1.0
pedal.treble = 1.0
}
let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#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)")
}
}
// MARK: - Signature controls
@Test func californiaBrightChangesOutput() throws {