Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift
T
josh 3cdb8d9ddb docs+test(amps): Modern High-Gain re-review nits
- Note the sagGainFloor 0.15 rationale (~-16 dB, deepest droop before gating).
- Rename the sag test's metric to dotProduct (it's an unnormalized sign check).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-03 16:28:05 -07:00

122 lines
5.7 KiB
Swift

// ModernHighGainAmpPedalTests.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("ModernHighGainAmpPedal")
struct ModernHighGainAmpPedalTests {
private let sampleRate: Double = 48000.0
private func render(configure: (ModernHighGainAmpPedal) -> Void = { _ in }) throws -> [Float] {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.3, 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: ModernHighGainAmpPedal = ModernHighGainAmpPedal(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)
}
private func windowedSSD(_ a: [Float], _ b: [Float], skip: Int = 4096, window: Int = 4096) -> Double {
guard a.count >= skip + window, b.count >= skip + window else { return 0.0 }
return 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
}
}
@Test func buildsNonSilentFiniteWithinCeiling() throws {
let hot: [Float] = try render { (p: ModernHighGainAmpPedal) in
p.gain = 30.0; p.bass = 1.0; p.treble = 1.0; p.master = 1.0; p.channel = 1.0
p.drive = 30.0; p.sag = 1.0; p.tight = 1.0
}
#expect(hasNonSilentOutput(hot) && hot.allSatisfy { $0.isFinite })
let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.0 + 1.0e-4, "Modern High-Gain exceeds the ±1 ceiling: \(peak)")
}
@Test func sagChangesOutput() throws {
let stiff: [Float] = try render { $0.gain = 20.0; $0.channel = 1.0; $0.sag = 0.0 }
let saggy: [Float] = try render { $0.gain = 20.0; $0.channel = 1.0; $0.sag = 1.0 }
#expect(windowedSSD(stiff, saggy) > 0, "Sag had no effect")
}
@Test func sagAttenuatesWithoutPhaseInversion() throws {
// With Presence/Depth maxed the shelf boosts push the sag envelope above 1; the sag gain
// must stay positive (attenuation), never go negative (phase inversion). Assert the sag'd
// output correlates positively with the stiff one (not an inverted copy).
let cfg: (ModernHighGainAmpPedal) -> Void = { (p: ModernHighGainAmpPedal) in
p.gain = 20.0; p.channel = 1.0; p.presence = 1.0; p.depth = 1.0
}
let stiff: [Float] = try render { cfg($0); $0.sag = 0.0 }
let saggy: [Float] = try render { cfg($0); $0.sag = 1.0 }
let skip: Int = 4096, window: Int = 4096
guard stiff.count >= skip + window, saggy.count >= skip + window else {
Issue.record("render too short"); return
}
// Unnormalized dot product — its sign is what matters: negative ⇒ phase-inverted copy.
let dotProduct: Double = zip(stiff[skip..<(skip + window)], saggy[skip..<(skip + window)])
.reduce(0.0) { acc, pair in acc + Double(pair.0) * Double(pair.1) }
#expect(dotProduct > 0, "sag phase-inverted the signal (dot product \(dotProduct) < 0)")
}
@Test func tightSwitchChangesOutput() throws {
let loose: [Float] = try render { $0.tight = 0.0 }
let tight: [Float] = try render { $0.tight = 1.0 }
#expect(windowedSSD(loose, tight) > 0, "Tight switch had no effect")
}
@Test func channelSwitchInherited() throws {
let clean: [Float] = try render { $0.channel = 0.0 }
let lead: [Float] = try render { $0.channel = 1.0 }
#expect(windowedSSD(clean, lead) > 0, "channel switch had no effect")
let channel: PedalSwitch? = ModernHighGainAmpPedal().effectSwitches.first { $0.label == "Channel" }
#expect(channel?.rebuildsOnChange == true)
}
@Test func exposesControlSurface() throws {
let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
#expect(pedal.effectKnobs.map { $0.label }
== ["Gain", "Bass", "Mid", "Treble", "Presence", "Depth", "Master", "Drive", "Sag"])
#expect(pedal.effectSwitches.map { $0.label } == ["Channel", "Tight"])
#expect(pedal.widthUnits == 4)
#expect(pedal.toneStackModel == .mesa)
}
@Test func controlsRoundTripThroughState() throws {
let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
pedal.sag = 0.8; pedal.tight = 1.0; pedal.channel = 1.0
pedal.drive = 20.0; pedal.master = 0.7; pedal.cab = 2.0
let restored: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
restored.applyState(pedal.captureState())
#expect(restored.sag == 0.8 && restored.tight == 1.0 && restored.channel == 1.0)
#expect(restored.drive == 20.0 && restored.master == 0.7 && restored.cab == 2.0)
}
@Test func registeredInBuiltInRegistry() throws {
#expect(PedalRegistry.makePedal(type: "Modern High-Gain") is ModernHighGainAmpPedal)
}
}