// CabinetIRFactory.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth 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 /// 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 /// peak → presence peak → cone-breakup peak → mid notch) and capturing the result into a mono /// ``LyrebirdBuffer``. /// /// This runs **off the RT thread** (at pedal build time), so it uses plain `Double` math with /// no allocation constraints. The produced buffer is a normal IR consumed by `ConvolutionReverb`. public enum CabinetIRFactory { /// Default IR length in frames. 1024 taps (~21 ms at 48 kHz) is ample for a speaker-cab /// magnitude curve, which has no long tail. public static let defaultFrames: Int = 1024 /// Butterworth Q for the high-pass (low rolloff) stage. private static let highPassQ: Double = 0.707 /// Per-stage Q values for a true 4th-order Butterworth low-pass built as two cascaded /// biquads (maximally flat passband). Q ≈ 0.5412 and 1.3066. private static let lowPassCascadeQ: [Double] = [0.541_196_100_146_197, 1.306_562_964_876_377] /// Q for the resonant body / presence peaks. private static let peakQ: Double = 1.2 /// Q for the narrower cone-breakup peak (higher than the body/presence peaks). private static let breakupQ: Double = 2.0 /// Q for the mid interference notch (narrow, so it dips without gutting the midrange). private static let notchQ: Double = 2.5 /// Builds the impulse response for `voice` at `sampleRate`. /// /// - Parameters: /// - voice: The cabinet voicing to synthesize. /// - sampleRate: Engine sample rate in Hz. /// - frames: IR length in frames. Defaults to ``defaultFrames``. /// - Returns: A mono `LyrebirdBuffer` holding the normalized IR, tagged with `sampleRate`. public static func makeIR( voice: CabinetVoice, sampleRate: Double, frames: Int = CabinetIRFactory.defaultFrames ) -> LyrebirdBuffer { var chain: [OfflineBiquad] = [ OfflineBiquad.highPass(freq: voice.highPassHz, q: highPassQ, sampleRate: sampleRate), OfflineBiquad.lowPass(freq: voice.lowPassHz, q: lowPassCascadeQ[0], sampleRate: sampleRate), OfflineBiquad.lowPass(freq: voice.lowPassHz, q: lowPassCascadeQ[1], sampleRate: sampleRate), OfflineBiquad.peaking(freq: voice.bodyHz, q: peakQ, gainDb: voice.bodyGainDb, sampleRate: sampleRate), OfflineBiquad.peaking(freq: voice.presenceHz, q: peakQ, gainDb: voice.presenceGainDb, sampleRate: sampleRate), OfflineBiquad.peaking(freq: voice.breakupHz, q: breakupQ, gainDb: voice.breakupGainDb, sampleRate: sampleRate), OfflineBiquad.peaking(freq: voice.notchHz, q: notchQ, gainDb: voice.notchGainDb, sampleRate: sampleRate), ] var ir: [Double] = [Double](repeating: 0.0, count: frames) for n: Int in 0.. 0.0 { let inv: Double = 1.0 / peak for i: Int in ir.indices { ir[i] *= inv } } let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frames, channelCount: 1) buffer.fill(using: { (frame: Int, _: Int) -> Double in ir[frame] }) buffer.setSampleRate(sampleRate) return buffer } } /// A minimal off-RT RBJ biquad (transposed Direct Form II) used only to render cabinet IRs. private struct OfflineBiquad { private let b0: Double private let b1: Double private let b2: Double private let a1: Double private let a2: Double private var z1: Double = 0.0 private var z2: Double = 0.0 private init( b0: Double, b1: Double, b2: Double, a1: Double, a2: Double ) { self.b0 = b0 self.b1 = b1 self.b2 = b2 self.a1 = a1 self.a2 = a2 } /// Centre frequency clamped below Nyquist for the given sample rate. private static func clampedFreq(_ freq: Double, sampleRate: Double) -> Double { Swift.min(freq, sampleRate * cabinetBiquadNyquistFraction) } mutating func process(_ x: Double) -> Double { let y: Double = b0 * x + z1 z1 = b1 * x - a1 * y + z2 z2 = b2 * x - a2 * y return y } static func highPass(freq: Double, q: Double, sampleRate: Double) -> OfflineBiquad { let f: Double = clampedFreq(freq, sampleRate: sampleRate) let w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let alpha: Double = sin(w0) / (2.0 * q) let a0: Double = 1.0 + alpha let b0: Double = (1.0 + cosW0) / 2.0 let b1: Double = -(1.0 + cosW0) let b2: Double = (1.0 + cosW0) / 2.0 let a1: Double = -2.0 * cosW0 let a2: Double = 1.0 - alpha return OfflineBiquad(b0: b0 / a0, b1: b1 / a0, b2: b2 / a0, a1: a1 / a0, a2: a2 / a0) } static func lowPass(freq: Double, q: Double, sampleRate: Double) -> OfflineBiquad { let f: Double = clampedFreq(freq, sampleRate: sampleRate) let w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let alpha: Double = sin(w0) / (2.0 * q) let a0: Double = 1.0 + alpha let b0: Double = (1.0 - cosW0) / 2.0 let b1: Double = 1.0 - cosW0 let b2: Double = (1.0 - cosW0) / 2.0 let a1: Double = -2.0 * cosW0 let a2: Double = 1.0 - alpha return OfflineBiquad(b0: b0 / a0, b1: b1 / a0, b2: b2 / a0, a1: a1 / a0, a2: a2 / a0) } 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 w0: Double = 2.0 * Double.pi * f / sampleRate let cosW0: Double = cos(w0) let alpha: Double = sin(w0) / (2.0 * q) let a0: Double = 1.0 + alpha / amplitude let b0: Double = 1.0 + alpha * amplitude let b1: Double = -2.0 * cosW0 let b2: Double = 1.0 - alpha * amplitude let a1: Double = -2.0 * cosW0 let a2: Double = 1.0 - alpha / amplitude return OfflineBiquad(b0: b0 / a0, b1: b1 / a0, b2: b2 / a0, a1: a1 / a0, a2: a2 / a0) } }