Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/CabinetIRFactoryTests.swift
T
josh 4a5d4009f6 feat(pedals): Cabinet IR pedal (S1) with synthesized cab voices
First amp-modeling increment (ShoeGaze 2.0, plan S1). No Lyrebird change —
reuses ConvolutionReverb + LyrebirdBuffer.

- CabinetPedal: builds one ConvolutionReverb per built-in voice at build() time
  and picks the active one with a `Select` driven by the persisted `cabinet`
  selector — live cab switching that round-trips through the rig save format.
- CabinetVoice + CabinetIRFactory: three built-in voicings synthesized in code
  (impulse through an RBJ biquad cascade — HP, true 4th-order Butterworth LP,
  body + presence peaks), so the bundled cab set is our own MIT content (no
  third-party IR licensing). Filter centre frequencies are Nyquist-clamped.
- Registered in PedalRegistry / PedalType / PedalCategory (.filter) / palette;
  README pedal count + catalog updated.

User-supplied captured .wav cab IRs are a planned follow-up (needs a persisted
file-path field PedalState doesn't yet carry).

Tests: IR is finite, peak-normalized, mono; rolls off lows and highs; voices
differ; selector exposed; selection changes output and round-trips through
PedalState; pedal builds, non-silent, resolves from the registry. Suite 486.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-01 17:13:08 -07:00

70 lines
2.9 KiB
Swift

// CabinetIRFactoryTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
@testable import LyrebirdShoeGaze
@MainActor
@Suite("CabinetIRFactory")
struct CabinetIRFactoryTests {
private let sampleRate: Double = 48000.0
/// Goertzel magnitude of the IR's frequency response at `hz`.
private func responseMagnitude(_ ir: LyrebirdBuffer, hz: Double) -> Double {
let omega: Double = 2.0 * Double.pi * hz / sampleRate
let cosine: Double = cos(omega)
let sine: Double = sin(omega)
let coeff: Double = 2.0 * cosine
var s1: Double = 0.0
var s2: Double = 0.0
for i: Int in 0..<ir.count {
let s0: Double = ir[i, 0] + coeff * s1 - s2
s2 = s1
s1 = s0
}
let real: Double = s1 - s2 * cosine
let imag: Double = s2 * sine
return (real * real + imag * imag).squareRoot()
}
@Test func producesFiniteNormalizedMonoIR() {
for voice: CabinetVoice in CabinetVoice.allCases {
let ir: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: voice, sampleRate: sampleRate)
#expect(ir.channelCount == 1)
#expect(ir.count == CabinetIRFactory.defaultFrames)
var peak: Double = 0.0
for i: Int in 0..<ir.count {
let v: Double = ir[i, 0]
#expect(v.isFinite, "\(voice) IR had non-finite sample")
peak = Swift.max(peak, abs(v))
}
#expect(peak > 0.9 && peak <= 1.0001, "\(voice) IR not peak-normalized: \(peak)")
}
}
@Test func rollsOffLowAndHighFrequencies() {
// A speaker cab attenuates sub-bass and high treble relative to the midrange.
let ir: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .americanCombo, sampleRate: sampleRate)
let low: Double = responseMagnitude(ir, hz: 40.0) // below the ~80 Hz high-pass
let mid: Double = responseMagnitude(ir, hz: 800.0) // passband
let high: Double = responseMagnitude(ir, hz: 12000.0) // above the ~5.5 kHz low-pass
#expect(mid > low * 2.0, "low end not attenuated (mid=\(mid) low=\(low))")
#expect(mid > high * 2.0, "high end not attenuated (mid=\(mid) high=\(high))")
}
@Test func voicesDifferInResponse() {
let american: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .americanCombo, sampleRate: sampleRate)
let dark: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .vintageDark, sampleRate: sampleRate)
// The darker voice rolls off earlier, so it has less 5 kHz energy than the American voice.
let americanHigh: Double = responseMagnitude(american, hz: 5000.0)
let darkHigh: Double = responseMagnitude(dark, hz: 5000.0)
#expect(americanHigh > darkHigh, "vintageDark should be darker at 5 kHz")
}
}