// 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 voiceNamesAreUnique() { let names: [String] = CabinetVoice.allCases.map { $0.displayName } #expect(Set(names).count == names.count, "duplicate cabinet display names") #expect(names.count == 8, "expected 8 cabinet voices") } }