// 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.. 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") } @Test func fullRangeExtendsHigherThanCombo() { // Full-Range (FRFR) has a ~9 kHz low-pass vs the American combo's ~5.5 kHz, so it keeps // markedly more energy at 8 kHz. let frfr: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, sampleRate: sampleRate) let combo: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .americanCombo, sampleRate: sampleRate) let frfrHigh: Double = responseMagnitude(frfr, hz: 8000.0) let comboHigh: Double = responseMagnitude(combo, hz: 8000.0) #expect(frfrHigh > comboHigh * 2.0, "FRFR should keep more 8 kHz than the combo") } @Test func notchCreatesMidDip() { // Modern 4×12 has a deep interference notch at 1.7 kHz. Its response there should dip below // the surrounding midrange (points chosen clear of the body / presence peaks). let ir: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .modernStack, sampleRate: sampleRate) let atNotch: Double = responseMagnitude(ir, hz: 1700.0) let below: Double = responseMagnitude(ir, hz: 1100.0) let above: Double = responseMagnitude(ir, hz: 2400.0) #expect(atNotch < below, "notch did not dip below the lower midrange") #expect(atNotch < above, "notch did not dip below the upper midrange") } @Test func loFiIsDarkerThanFullRange() { // Lo-Fi 1×8 rolls off at ~3 kHz vs Full-Range's ~9 kHz, so it is markedly darker at 4 kHz. let loFi: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .loFiBox, sampleRate: sampleRate) let frfr: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, sampleRate: sampleRate) #expect(responseMagnitude(frfr, hz: 4000.0) > responseMagnitude(loFi, hz: 4000.0), "Lo-Fi should be darker than Full-Range at 4 kHz") } @Test func breakupPeakLiftsUpperMid() { // Bright 1×12 (breakup +3 dB @ 5.2 kHz, LP 6.5 kHz) keeps more ~5.1 kHz energy than // Boutique 2×12 (breakup +1.5 dB @ 5.0 kHz, LP 6.0 kHz) — both the stronger breakup boost // and the higher low-pass push the same direction. let bright: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .brightCombo, sampleRate: sampleRate) let boutique: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .boutiqueOpen, sampleRate: sampleRate) #expect(responseMagnitude(bright, hz: 5100.0) > responseMagnitude(boutique, hz: 5100.0), "brightCombo should have more upper-mid bite than boutiqueOpen at 5.1 kHz") } @Test func voiceNamesAreUnique() { let names: [String] = CabinetVoice.allCases.map { $0.displayName } #expect(Set(names).count == names.count, "duplicate cabinet display names") } // MARK: - Mic voicing /// Ratio of the IR's magnitude at `hz` to a mid-band reference (800 Hz). Peak-normalization /// scales the whole IR, so it cancels in this ratio — letting a mic's *relative* boost/cut at a /// frequency be compared across IRs regardless of their independent normalization. private func tiltRatio(_ ir: LyrebirdBuffer, hz: Double) -> Double { let reference: Double = responseMagnitude(ir, hz: 800.0) guard reference > 0.0 else { return 0.0 } return responseMagnitude(ir, hz: hz) / reference } @Test func noMicMatchesRawCabIR() { // MicVoice.off must append no stages, so the IR is byte-identical to the mic-less default — // the backward-compatible path for cabs saved before mic voicing existed. for voice: CabinetVoice in CabinetVoice.allCases { let raw: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: voice, sampleRate: sampleRate) let offMic: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: voice, mic: .off, sampleRate: sampleRate) #expect(raw.count == offMic.count) var identical: Bool = true for i: Int in 0.. tiltRatio(off, hz: 5500.0), "dynamic mic did not lift the presence region") } @Test func ribbonMicDarkensTop() { // The ribbon mic rolls the top off (−7 dB shelf @ 6.5 kHz) → a lower 8 kHz-to-mid tilt. let off: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, mic: .off, sampleRate: sampleRate) let ribbon: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, mic: .ribbon, sampleRate: sampleRate) #expect(tiltRatio(ribbon, hz: 8000.0) < tiltRatio(off, hz: 8000.0), "ribbon mic did not darken the top end") } @Test func condenserMicAddsAir() { // The condenser mic lifts the extreme top (+3 dB shelf @ 11 kHz) → a higher 10 kHz-to-mid tilt. let off: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, mic: .off, sampleRate: sampleRate) let condenser: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .fullRange, mic: .condenser, sampleRate: sampleRate) #expect(tiltRatio(condenser, hz: 10000.0) > tiltRatio(off, hz: 10000.0), "condenser mic did not add top-end air") } @Test func micNamesAreUnique() { let names: [String] = MicVoice.allCases.map { $0.displayName } #expect(Set(names).count == names.count, "duplicate mic display names") } }