diff --git a/Sources/LyrebirdShoeGaze/Cabinet/CabinetIRFactory.swift b/Sources/LyrebirdShoeGaze/Cabinet/CabinetIRFactory.swift index 3020311..b10e575 100644 --- a/Sources/LyrebirdShoeGaze/Cabinet/CabinetIRFactory.swift +++ b/Sources/LyrebirdShoeGaze/Cabinet/CabinetIRFactory.swift @@ -10,10 +10,8 @@ import Darwin /// Fraction of the sample rate above which a filter centre frequency is clamped, so the RBJ /// coefficients stay well-behaved below Nyquist even at low sample rates. private let cabinetBiquadNyquistFraction: Double = 0.49 -/// RBJ-cookbook divisor converting a peaking-filter gain in dB to its `A` amplitude term. -private let cabinetPeakingGainDivisor: Double = 40.0 -/// RBJ-cookbook divisor converting a shelf-filter gain in dB to its `A` amplitude term (same `/40`). -private let cabinetShelfGainDivisor: Double = 40.0 +/// RBJ-cookbook divisor converting a filter gain in dB to its `A` amplitude term (peaking + shelves). +private let cabinetDbToAmplitudeDivisor: Double = 40.0 /// Synthesizes a short cabinet impulse response for a ``CabinetVoice`` by running a unit /// impulse through a cascade of RBJ biquads (high-pass → two-stage low-pass → low-mid body @@ -178,7 +176,7 @@ private struct OfflineBiquad { static func peaking(freq: Double, q: Double, gainDb: Double, sampleRate: Double) -> OfflineBiquad { let f: Double = clampedFreq(freq, sampleRate: sampleRate) - let amplitude: Double = pow(10.0, gainDb / cabinetPeakingGainDivisor) + let amplitude: Double = pow(10.0, gainDb / cabinetDbToAmplitudeDivisor) let w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let alpha: Double = sin(w0) / (2.0 * q) @@ -194,7 +192,7 @@ private struct OfflineBiquad { /// RBJ low-shelf. `slope` is the cookbook `S` (1.0 = steepest without overshoot). static func lowShelf(freq: Double, gainDb: Double, slope: Double, sampleRate: Double) -> OfflineBiquad { let f: Double = clampedFreq(freq, sampleRate: sampleRate) - let amplitude: Double = pow(10.0, gainDb / cabinetShelfGainDivisor) + let amplitude: Double = pow(10.0, gainDb / cabinetDbToAmplitudeDivisor) let w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let sinW0: Double = sin(w0) @@ -214,7 +212,7 @@ private struct OfflineBiquad { /// RBJ high-shelf. `slope` is the cookbook `S` (1.0 = steepest without overshoot). static func highShelf(freq: Double, gainDb: Double, slope: Double, sampleRate: Double) -> OfflineBiquad { let f: Double = clampedFreq(freq, sampleRate: sampleRate) - let amplitude: Double = pow(10.0, gainDb / cabinetShelfGainDivisor) + let amplitude: Double = pow(10.0, gainDb / cabinetDbToAmplitudeDivisor) let w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let sinW0: Double = sin(w0) diff --git a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift index 1f19406..d4f9535 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift @@ -57,17 +57,15 @@ public final class CabinetPedal: Pedal { /// Convolution is taken fully wet here; the `Pedal` base class provides the user wet/dry mix. private let convolutionMix: Double = 1.0 - /// Output soft-limit drive. `1.0` gives unit small-signal gain (clean signals pass untouched) - /// with a `tanh` asymptote at ±1 — a speaker cab is analog and does not digital-clip, so peaks - /// (convolution can overshoot ±1) soft-compress rather than hard-clip. - private let softLimitDrive: Double = 1.0 - /// Symmetric output stage — a pure `tanh` limiter, no even-harmonic colour. - private let softLimitBias: Double = 0.0 - /// 2× is enough: the cab low-pass already band-limits the signal feeding this stage. - private let softLimitOversample: Int = 2 - /// Absolute digital ceiling for the hard safety clamp catching the sub-percent overshoot the - /// `tanh`'s DC-blocker / halfband filters can leave just above ±1. + /// Output peak ceiling. A true look-ahead limiter holds `|output| ≤` this, taming the peaks a + /// speaker convolution can push past ±1 (an analog cab can't digital-clip) while leaving signals + /// below the ceiling **untouched** — no always-on saturation of clean, below-ceiling material. private let outputCeiling: Double = 1.0 + /// Limiter look-ahead / output delay. Short (2 ms) so the added latency is negligible in-line + /// while still catching the brief convolution-overshoot transients. + private let limiterLookAheadSeconds: Double = 0.002 + /// Limiter gain-release time constant — slow enough not to modulate sustained low frequencies. + private let limiterReleaseSeconds: Double = 0.050 /// Selector stops, one per ``CabinetVoice`` in `allCases` order (value == index). private static let voiceStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases @@ -122,13 +120,17 @@ public final class CabinetPedal: Pedal { /// either selector triggers a board rebuild (both are `rebuildsOnChange`). Synthesizing the short /// IR happens off the RT thread in `build()`. /// - /// The closing `TriodeStage(bias: 0)` → `Clip(±1)` is a symmetric `tanh` soft-limit + hard - /// ceiling: a speaker cab is analog and can't digital-clip, and the convolution can push peaks - /// past ±1, so peaks soft-compress toward ±1 while clean signals (well below ±1) pass at ~unit - /// gain. This keeps a decoupled amp → cabinet chain from clipping the DAC. + /// The closing `LookAheadLimiter` is a transparent peak limiter on the **wet** convolution + /// output: a speaker cab is analog and can't digital-clip, and the convolution can push peaks + /// past ±1. Unlike a `tanh` saturator, a look-ahead limiter leaves signals **below** the ceiling + /// bit-for-bit untouched (no coloration of clean, below-ceiling material) and only pulls gain + /// down for the brief peaks that would exceed ±1. It adds a short (2 ms) output delay. At the + /// default fully-wet `mix` this bounds the pedal output to ±1 (e.g. a decoupled amp → cabinet + /// chain); a partial `mix` re-introduces the dry signal via the base class's equal-power + /// crossfade, so at intermediate settings final level is the board's output-stage concern. /// /// - Parameter dry: Dry mono input signal. - /// - Returns: The cabinet-convolved, mic-voiced, soft-limited mono output. + /// - Returns: The cabinet-convolved, mic-voiced, peak-limited mono output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { let sr: Double = sampleRate let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cabinet) @@ -138,12 +140,12 @@ public final class CabinetPedal: Pedal { ir: CabinetIRFactory.makeIR(voice: voice, mic: micVoice, sampleRate: sr), mix: convolutionMix ) - let softLimited: TriodeStage = TriodeStage( - input: convolved, - drive: softLimitDrive, - bias: softLimitBias, - oversample: softLimitOversample + return LookAheadLimiter( + in: convolved, + maxAmplitude: outputCeiling, + lookAheadTime: limiterLookAheadSeconds, + releaseTime: limiterReleaseSeconds, + sampleRate: sr ) - return Clip.Audio(softLimited, lo: -outputCeiling, hi: outputCeiling) } }