// NoiseGatePedalTests.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("NoiseGatePedal") struct NoiseGatePedalTests { /// Renders a `NoiseGatePedal` through an offline engine and returns the interleaved output. private func render( threshold: Double, floor: Double ) throws -> [Float] { let renderDuration: Double = 0.1 let sampleRate: Double = 44100.0 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, sampleRate: sampleRate ) 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: NoiseGatePedal = NoiseGatePedal( inputBus: srcBus.index, outputBus: dstBus.index ) pedal.threshold = threshold pedal.floor = floor 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) } /// Sum of squared samples — a proxy for output power. private func power(_ samples: [Float]) -> Double { samples.reduce(0.0) { acc, s in let v: Double = Double(s) return acc + v * v } } @Test func openGatePassesSignal() throws { // A threshold of 0 keeps the gate open — the loop signal should pass through. let samples: [Float] = try render(threshold: 0.0, floor: 0.0) #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "Open gate produced silence") } @Test func closedGateWithZeroFloorStronglyAttenuates() throws { // An impossibly high threshold keeps the gate closed; with floor 0 the output // should be far quieter than the open-gate render. let openSamples: [Float] = try render(threshold: 0.0, floor: 0.0) let closedSamples: [Float] = try render(threshold: 0.95, floor: 0.0) let openPower: Double = power(openSamples) let closedPower: Double = power(closedSamples) #expect(openPower > 0, "Open gate render was silent") // Closed gate should suppress nearly all energy. let suppressionFactor: Double = 0.05 #expect( closedPower < openPower * suppressionFactor, "Closed gate did not attenuate: open=\(openPower) closed=\(closedPower)" ) } @Test func unityFloorLeavesSignalUntouched() throws { // floor = 1 means no gating regardless of threshold — output stays present. let samples: [Float] = try render(threshold: 0.95, floor: 1.0) #expect(hasNonSilentOutput(samples), "Unity-floor gate muted the signal") } }