// SustainerPedalTests.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("SustainerPedal") struct SustainerPedalTests { private func render(configure: (SustainerPedal) -> Void = { _ in }) throws -> [Float] { let renderDuration: Double = 0.1 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, 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: SustainerPedal = SustainerPedal(inputBus: srcBus.index, outputBus: dstBus.index) 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) } @Test func buildsAndProducesNonSilentOutput() throws { let samples: [Float] = try render() #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "SustainerPedal produced silence") } @Test func exposesSustainAndToneKnobs() throws { let pedal: SustainerPedal = SustainerPedal() #expect(pedal.effectKnobs.map { $0.label } == ["Sustain", "Tone"]) } @Test func sustainAmountChangesOutput() throws { // Min vs max sustain drive the compressor very differently → measurably different output. let gentle: [Float] = try render { $0.sustain = 0.0 } let squashed: [Float] = try render { $0.sustain = 10.0 } #expect(hasNonSilentOutput(gentle) && hasNonSilentOutput(squashed)) let skip: Int = 1024, window: Int = 2048 guard gentle.count >= skip + window, squashed.count >= skip + window else { return } let ssd: Double = zip(gentle[skip..<(skip + window)], squashed[skip..<(skip + window)]).reduce(0.0) { acc, pair in let d: Double = Double(pair.0 - pair.1); return acc + d * d } #expect(ssd > 0, "sustain knob had no effect on output") } @Test func exposesSafetySwitch() throws { let pedal: SustainerPedal = SustainerPedal() #expect(pedal.effectSwitches.map { $0.label } == ["Safety"]) #expect(pedal.effectSwitches.first?.options.map { $0.title } == ["Off", "On"]) } @Test func safetyLimiterCapsPeaks() throws { // Max sustain drives a low threshold, high ratio, and +18 dB makeup — the compressor's // non-zero attack lets transients overshoot. With Safety on, the output peak must stay // near full scale, and the two switch states must differ. let off: [Float] = try render { $0.sampleRate = 44100.0; $0.sustain = 10.0; $0.safetyLimiter = 0.0 } let on: [Float] = try render { $0.sampleRate = 44100.0; $0.sustain = 10.0; $0.safetyLimiter = 1.0 } #expect(hasNonSilentOutput(off) && hasNonSilentOutput(on)) let fill: Int = 2048 guard on.count > fill else { return } let onPeak: Float = on[fill...].reduce(0) { Swift.max($0, abs($1)) } #expect(onPeak <= 1.05, "safety limiter failed to cap peak: \(onPeak)") let window: Int = 2048 guard off.count >= fill + window, on.count >= fill + window else { return } let ssd: Double = zip(off[fill..<(fill + window)], on[fill..<(fill + window)]).reduce(0.0) { acc, pair in let d: Double = Double(pair.0 - pair.1); return acc + d * d } #expect(ssd > 0, "safety switch had no effect on output") } }