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>
This commit is contained in:
2026-08-02 21:09:30 -07:00
parent e1ed4e348c
commit fb50538e84
2 changed files with 43 additions and 3 deletions
@@ -17,9 +17,15 @@ 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) (output soft-limit — analog-style, ±1 ceiling)
/// ```
/// 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
/// brickwall, and its analog output can't digital-clip. With unit small-signal gain it leaves clean
/// playing untouched while soft-limiting peaks to the ±1 ceiling — so, combined with the
/// attenuate-only master `Level`, the amp can never overshoot into a hard digital clip.
///
/// Subclasses override ``toneStackModel``, ``summaryText``, ``signatureKnobs``, and
/// ``shapeVoice(_:)``, and may set a voice-appropriate default `gain` in their `init`.
open class AmpVoicePedal: Pedal {
@@ -64,6 +70,14 @@ 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
let gainRange: ClosedRange<Double> = 1.0 ... 30.0
let toneRange: ClosedRange<Double> = 0.0 ... 1.0
@@ -127,15 +141,20 @@ 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 asymptote to
/// ±1.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: The amped, cab-convolved mono output.
/// - Returns: The amped, cab-convolved, soft-limited 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 +164,16 @@ 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
)
return TriodeStage(
input: cabinet,
drive: outputStageDrive,
bias: outputStageBias,
oversample: outputStageOversample
)
}
}
@@ -97,6 +97,21 @@ struct AmpVoicePedalTests {
}
}
@Test func outputStaysWithinDigitalCeiling() throws {
// The tanh output stage soft-limits to ±1 (a small margin allows for oversampling-filter
// ripple on transients). Drive every voice hard and check nothing hard-clips the DAC.
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.05, "\(voice.name) peak \(peak) exceeds the digital ceiling")
#expect(peak > 0.3, "\(voice.name) unexpectedly quiet at max gain: \(peak)")
}
}
// MARK: - Signature controls
@Test func californiaBrightChangesOutput() throws {