Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/CabinetIRFactoryTests.swift
T
josh 8a266a33dc fix(cab): keep modernStack breakup in-band; doc + test follow-ups
Review fixes:
- modernStack.breakupHz 4500 → 3800 so the cone-breakup peak sits below the
  4000 Hz low-pass (was the only voice with breakup > lowPass, which rolled off
  most of the intended boost).
- CabinetIRFactory class doc: list the full 7-stage chain (breakup + notch were
  missing).
- Tests: add loFiIsDarkerThanFullRange and breakupPeakLiftsUpperMid for
  per-voice differentiation; drop the hard-coded voice count in
  voiceNamesAreUnique.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-02 18:54:45 -07:00

114 lines
5.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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")
}
@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")
}
}