// 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 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) } }