Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/CabinetPedalTests.swift
T
josh d7eb0690ff feat(cabinet): dual-mic blend with comb (14c)
Phase 14c — the classic two-mic technique. When "Mic B" (a second mic-voice
selector, default No Mic) is engaged, a parallel convolver (same cab voice,
mic B's IR) is delayed by up to 1.5 ms (Mic Offset) and summed with the
aligned primary at the Mic Blend balance — the time offset between the two
coherent copies produces comb filtering (e.g. a 57 + 121 blend).

- Mic B: rebuildsOnChange selector; No Mic (default) builds NO second
  convolver, so the single-mic path is bit-identical to before and costs one
  convolution. Mic Blend / Mic Offset are live smooth knobs (advanced pop-over,
  appended after Position/Proximity — append-only for advancedValues).
- Coherent (same-source) mics sum linearly: A*(1-blend) + Bdelayed*blend.
- The look-ahead limiter still bounds the summed output to ±1.

Tests: second mic changes output; with Mic B Off the blend/offset are inert
(single-mic path untouched); blend + comb offset both change output when
engaged; dual-mic round-trips through state; dual-mic stays within ±1. 669
tests pass.

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

268 lines
14 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.
// CabinetPedalTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("CabinetPedal")
struct CabinetPedalTests {
private func render(configure: (CabinetPedal) -> Void = { _ in }) throws -> [Float] {
let sampleRate: Double = 48000.0
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.2, sampleRate: sampleRate, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(dstBus)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let pedal: CabinetPedal = CabinetPedal(inputBus: srcBus.index, outputBus: dstBus.index)
pedal.sampleRate = sampleRate
configure(pedal)
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
try engine.add(source)
try engine.add(pedal, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(pedal))
try engine.start()
return readStereoCAF(url: url, sampleRate: sampleRate)
}
@Test func buildsAndProducesNonSilentOutput() throws {
let samples: [Float] = try render()
#expect(!samples.isEmpty)
#expect(hasNonSilentOutput(samples), "CabinetPedal produced silence")
}
@Test func cabinetSelectionChangesOutput() throws {
// Index 0 = American 1×12, index 2 = Vintage Dark — different voicings, different output.
let american: [Float] = try render { $0.cabinet = 0.0 }
let dark: [Float] = try render { $0.cabinet = 2.0 }
#expect(hasNonSilentOutput(american) && hasNonSilentOutput(dark))
let skip: Int = 4096
let window: Int = 4096
guard american.count >= skip + window, dark.count >= skip + window else { return }
let ssd: Double = zip(american[skip..<(skip + window)], dark[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "cabinet selection had no effect on output")
}
@Test func exposesCabinetAndMicSelectors() throws {
let pedal: CabinetPedal = CabinetPedal()
let labels: [String] = pedal.effectSelectors.map { $0.label }
#expect(labels == ["Cab", "Mic", "Mic B"])
// One Cab stop per voice, and one stop per mic voice for both Mic and Mic B, in allCases order.
let cabStops: [String] = pedal.effectSelectors.first { $0.label == "Cab" }?.stops.map { $0.title } ?? []
#expect(cabStops == CabinetVoice.allCases.map { $0.displayName })
let micStops: [String] = pedal.effectSelectors.first { $0.label == "Mic" }?.stops.map { $0.title } ?? []
#expect(micStops == MicVoice.allCases.map { $0.displayName })
let micBStops: [String] = pedal.effectSelectors.first { $0.label == "Mic B" }?.stops.map { $0.title } ?? []
#expect(micBStops == MicVoice.allCases.map { $0.displayName })
}
@Test func micSelectionChangesOutput() throws {
// Mic 0 = No Mic, mic 1 = Dynamic — different baked IRs, different output.
let noMic: [Float] = try render { $0.mic = 0.0 }
let dynamic: [Float] = try render { $0.mic = 1.0 }
#expect(hasNonSilentOutput(noMic) && hasNonSilentOutput(dynamic))
let skip: Int = 4096, window: Int = 4096
guard noMic.count >= skip + window, dynamic.count >= skip + window else { return }
let ssd: Double = zip(noMic[skip..<(skip + window)], dynamic[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "mic selection had no effect on output")
}
@Test func micSelectionRoundTripsThroughState() throws {
let pedal: CabinetPedal = CabinetPedal()
pedal.cabinet = 2.0
pedal.mic = 3.0
let restored: CabinetPedal = CabinetPedal()
restored.applyState(pedal.captureState())
#expect(restored.cabinet == 2.0 && restored.mic == 3.0, "cab/mic selection did not round-trip")
}
@Test func positionChangesOutput() throws {
// Position sweeps a live high-shelf cut (on-axis → cone edge), so it must change the output.
let onAxis: [Float] = try render { $0.position = 0.0 }
let edge: [Float] = try render { $0.position = 1.0 }
let skip: Int = 4096, window: Int = 4096
#expect(onAxis.count >= skip + window && edge.count >= skip + window, "render too short")
guard onAxis.count >= skip + window, edge.count >= skip + window else { return }
let ssd: Double = zip(onAxis[skip..<(skip + window)], edge[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "mic Position had no effect on output")
}
@Test func proximityChangesOutput() throws {
// Proximity sweeps a live low-shelf boost (distant → close), so it must change the output.
let distant: [Float] = try render { $0.proximity = 0.0 }
let close: [Float] = try render { $0.proximity = 1.0 }
let skip: Int = 4096, window: Int = 4096
#expect(distant.count >= skip + window && close.count >= skip + window, "render too short")
guard distant.count >= skip + window, close.count >= skip + window else { return }
let ssd: Double = zip(distant[skip..<(skip + window)], close[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "mic Proximity had no effect on output")
}
@Test func explicitDefaultPlacementMatchesImplicit() throws {
// A default-consistency guard: the shipped defaults are position = 0 / proximity = 0, so
// setting them explicitly must reproduce the default render exactly. (That those defaults are
// a *no-op* on the tone follows from the RBJ shelves collapsing to unity at 0 dB — a Lyrebird
// property; this test pins that the defaults are those values and the render is deterministic.)
let byDefault: [Float] = try render()
let explicit: [Float] = try render { $0.position = 0.0; $0.proximity = 0.0 }
let skip: Int = 4096, window: Int = 4096
#expect(byDefault.count >= skip + window && explicit.count >= skip + window, "render too short")
guard byDefault.count >= skip + window, explicit.count >= skip + window else { return }
let ssd: Double = zip(byDefault[skip..<(skip + window)], explicit[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd == 0, "explicit default placement did not match the default render")
}
@Test func advancedKnobsExposePlacementAndDualMic() throws {
let pedal: CabinetPedal = CabinetPedal()
#expect(pedal.advancedKnobs.map { $0.label } == ["Position", "Proximity", "Mic Blend", "Mic Offset"])
}
// MARK: - Dual mic
@Test func secondMicChangesOutput() throws {
// Engaging Mic B (a second parallel convolver) must change the output vs the single mic.
let single: [Float] = try render { $0.micB = 0.0 }
let dual: [Float] = try render { $0.micB = 2.0 } // Ribbon as the second mic
let skip: Int = 4096, window: Int = 4096
#expect(single.count >= skip + window && dual.count >= skip + window, "render too short")
guard single.count >= skip + window, dual.count >= skip + window else { return }
let ssd: Double = zip(single[skip..<(skip + window)], dual[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "engaging Mic B had no effect")
}
@Test func micBOffMakesBlendAndOffsetInert() throws {
// With Mic B = No Mic, the second convolver is not built, so Mic Blend / Mic Offset must have
// no effect — proving the single-mic default path is untouched by the dual-mic controls.
let a: [Float] = try render { $0.micB = 0.0; $0.micBlend = 0.0; $0.micOffset = 0.0 }
let b: [Float] = try render { $0.micB = 0.0; $0.micBlend = 1.0; $0.micOffset = 1.0 }
let skip: Int = 4096, window: Int = 4096
#expect(a.count >= skip + window && b.count >= skip + window, "render too short")
guard a.count >= skip + window, b.count >= skip + window else { return }
let ssd: Double = zip(a[skip..<(skip + window)], b[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd == 0, "Mic Blend/Offset changed output while Mic B was Off")
}
@Test func micBlendAndOffsetChangeOutputWhenEngaged() throws {
// With Mic B engaged, both the blend balance and the comb offset must change the output.
let blendA: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.0 }
let blendB: [Float] = try render { $0.micB = 2.0; $0.micBlend = 1.0 }
let offset0: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.5; $0.micOffset = 0.0 }
let offset1: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.5; $0.micOffset = 1.0 }
let skip: Int = 4096, window: Int = 4096
func ssd(_ x: [Float], _ y: [Float]) -> Double {
guard x.count >= skip + window, y.count >= skip + window else { return 0.0 }
return zip(x[skip..<(skip + window)], y[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
}
#expect(ssd(blendA, blendB) > 0, "Mic Blend had no effect when engaged")
#expect(ssd(offset0, offset1) > 0, "Mic Offset (comb) had no effect when engaged")
}
@Test func dualMicRoundTripsThroughState() throws {
let pedal: CabinetPedal = CabinetPedal()
pedal.micB = 3.0
pedal.micBlend = 0.3
pedal.micOffset = 0.6
let restored: CabinetPedal = CabinetPedal()
restored.applyState(pedal.captureState())
#expect(restored.micB == 3.0 && restored.micBlend == 0.3 && restored.micOffset == 0.6,
"dual-mic controls did not round-trip")
}
@Test func dualMicStaysWithinCeiling() throws {
// Two convolvers summed can exceed ±1 pre-limiter; the look-ahead limiter must still bound it.
let ceilingEpsilon: Double = 1.0e-4
let out: [Float] = try render { $0.micB = 1.0; $0.micBlend = 0.5; $0.micOffset = 0.5 }
#expect(out.allSatisfy { $0.isFinite })
let peak: Double = out.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.0 + ceilingEpsilon, "dual-mic peak \(peak) exceeds ±1")
}
@Test func placementRoundTripsThroughState() throws {
let pedal: CabinetPedal = CabinetPedal()
pedal.position = 0.7
pedal.proximity = 0.4
let restored: CabinetPedal = CabinetPedal()
restored.applyState(pedal.captureState())
#expect(restored.position == 0.7 && restored.proximity == 0.4, "placement did not round-trip")
}
@Test func outputStaysWithinCeiling() throws {
// The tanh soft-limit + Clip(±1) bound the output so a decoupled amp → cabinet chain can't
// clip the DAC. No render may exceed ±1.
let ceilingEpsilon: Double = 1.0e-4
for cab: Double in [0.0, 2.0, 6.0] {
for micValue: Double in [0.0, 1.0, 2.0, 3.0] {
let out: [Float] = try render { $0.cabinet = cab; $0.mic = micValue }
#expect(out.allSatisfy { $0.isFinite })
let peak: Double = out.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.0 + ceilingEpsilon, "cab=\(cab) mic=\(micValue) peak \(peak) exceeds ±1")
}
}
// Placement extremes (max proximity bass lift) must still be caught by the limiter.
let placed: [Float] = try render { $0.position = 1.0; $0.proximity = 1.0 }
#expect(placed.allSatisfy { $0.isFinite })
let placedPeak: Double = placed.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(placedPeak <= 1.0 + ceilingEpsilon, "placement extremes peak \(placedPeak) exceeds ±1")
}
@Test func cabinetSelectorRebuildsOnChange() throws {
// Only the selected voice's convolver is built, so changing it must rebuild the board.
let pedal: CabinetPedal = CabinetPedal()
#expect(pedal.effectSelectors.first?.rebuildsOnChange == true)
}
@Test func voiceAtSelectorValueClampsAndRounds() throws {
let all: [CabinetVoice] = CabinetVoice.allCases
#expect(CabinetVoice.voice(atSelectorValue: 0.0) == all[0])
#expect(CabinetVoice.voice(atSelectorValue: 2.0) == all[2])
#expect(CabinetVoice.voice(atSelectorValue: 1.4) == all[1], "should round to nearest stop")
#expect(CabinetVoice.voice(atSelectorValue: -5.0) == all[0], "clamp below 0")
#expect(CabinetVoice.voice(atSelectorValue: 999.0) == all[all.count - 1], "clamp above max")
}
@Test func cabinetSelectionRoundTripsThroughState() throws {
// The selector value must survive capture/apply (preset round-trip).
let pedal: CabinetPedal = CabinetPedal()
pedal.cabinet = 2.0
let state: PedalState = pedal.captureState()
let restored: CabinetPedal = CabinetPedal()
restored.applyState(state)
#expect(restored.cabinet == 2.0, "cabinet selection did not round-trip through PedalState")
}
@Test func registeredInBuiltInRegistry() throws {
let pedal: Pedal? = PedalRegistry.makePedal(type: "Cabinet")
#expect(pedal is CabinetPedal)
}
}