Files
josh 7f1856e8e5 feat(cabinet): mic voicing (14a) + cabinet output soft-limit
Phase 14a of cabinet mic modeling.

- MicVoice (No Mic / Dynamic / Ribbon / Condenser): broad-strokes synthesized
  mic curves (our own MIT content, not captures) as CabIRStage biquad specs
  (presence bump, low/high shelving) that CabinetIRFactory appends to the cab
  cascade before baking — folded into the SAME single convolution IR, so mic
  choice costs nothing at render time. MicVoice.off appends nothing → the IR
  is byte-identical to the raw cab voice (backward compatible).
- CabinetIRFactory.makeIR gains a `mic:` param (default .off) + RBJ low/high
  shelf offline biquads.
- CabinetPedal: Mic selector (rebuildsOnChange, append-only 2nd selector so
  old presets still load with mic defaulting to off) + a tanh soft-limit →
  Clip(±1) output stage, so a decoupled amp → cabinet chain can't clip the
  DAC (a speaker cab is analog; clean signals pass at ~unit gain).
- Help acknowledgment notes the mic voicings are synthesized, not captures.

Tests: No-Mic IR byte-identical to raw cab; dynamic lifts presence, ribbon
darkens top, condenser adds air (peak-normalization-invariant tilt ratios);
mic changes output, round-trips; output stays within ±1 across cab×mic. 659
tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-04 08:55:11 -07:00

167 lines
8.6 KiB
Swift
Raw Permalink 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")
}
// 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..<raw.count where raw[i, 0] != offMic[i, 0] { identical = false; break }
#expect(identical, "\(voice) No-Mic IR differs from the raw cab IR")
}
}
@Test func dynamicMicLiftsPresence() {
// The dynamic mic adds a presence bump at ~5.5 kHz → a higher 5.5 kHz-to-mid tilt than no mic.
let off: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .americanCombo, mic: .off, sampleRate: sampleRate)
let dynamic: LyrebirdBuffer = CabinetIRFactory.makeIR(voice: .americanCombo, mic: .dynamic, sampleRate: sampleRate)
#expect(tiltRatio(dynamic, hz: 5500.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")
}
}