// 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 = 11.0; p.bass = 1.0; p.treble = 1.0; p.master = 1.0; p.channel = 1.0 p.drive = 11.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 = 9.8; $0.channel = 1.0; $0.sag = 0.0 } let saggy: [Float] = try render { $0.gain = 9.8; $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 = 9.8; 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() // Sag now lives in the shared advanced pop-over (with Bias), not on the pedal face. #expect(pedal.effectKnobs.map { $0.label } == ["Gain", "Bass", "Mid", "Treble", "Presence", "Depth", "Master", "Drive"]) #expect(pedal.advancedKnobs.map { $0.label } == ["Sag", "Bias", "Bias X"]) #expect(pedal.effectSwitches.map { $0.label } == ["Channel", "Tight"]) #expect(pedal.widthUnits == 2) #expect(pedal.toneStackModel == .mesa) #expect(pedal.sag == 0.3, "Modern High-Gain keeps rectifier sag on by default") } @Test func controlsRoundTripThroughState() throws { let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal() pedal.sag = 0.8; pedal.tight = 1.0; pedal.channel = 1.0 pedal.drive = 9.8; 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 == 9.8 && restored.master == 0.7 && restored.cab == 2.0) } @Test func registeredInBuiltInRegistry() throws { #expect(PedalRegistry.makePedal(type: "Modern High-Gain") is ModernHighGainAmpPedal) } @Test func legacyPresetWithSagOnFaceMigratesWithoutShiftingMixLevel() throws { // A rig saved when Sag was the 9th face knob has 11 knobValues (Gain…Drive, Sag, Mix, Level) // and no advancedValues. Sag has since moved to the advanced pop-over, so the current shape // has 10 knobValues (Gain…Drive, Mix, Level) + 2 advancedValues (Sag, Bias). Loading the // legacy preset must lift Sag into advancedValues and keep Mix/Level in place — never shift // the old Sag onto Mix or the old Mix onto Level. let legacy: PedalState = PedalState( id: UUID(), type: "Modern High-Gain", knobValues: [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.7, 0.8, 0.9], bypass: 0.0 ) let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal() pedal.applyState(legacy) let restored: PedalState = pedal.captureState() // Gain…Drive, then Mix (0.8), Level (0.9) — the old Sag (0.7) is gone from knobValues. #expect(restored.knobValues == [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.8, 0.9], "legacy Sag shifted Mix/Level: \(restored.knobValues)") // Sag lifted into advanced slot 0; Bias (0.5) and Bias X (0.0) had no legacy value → defaults. #expect(restored.advancedValues == [0.7, 0.5, 0.0], "Sag/Bias/Bias X advanced values wrong: \(restored.advancedValues)") } }