// 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") } /// A sine that steps from loud (0.3, gate open) down to a constant below-threshold level (0.03) /// over 50 ms and holds there — so the gate opens, then closes, while the source level stays /// constant. That isolates the gate's close fade (release-dependent) from any source decay. /// `Line` holds its `end` value after `dur`, giving the sustained low tail. private final class DecaySineSource: SynthesizerBase { private let bus: Int init(bus: Int) { self.bus = bus; super.init() } override func build() { output(bus: bus, source: OscSin.Audio(freq: 220.0) * Line.Control(start: 0.3, end: 0.03, dur: 0.05)) } } /// Energy in the gate's *close window* — the source steps down through the threshold ~50 ms in /// then holds a constant below-threshold level, so the output energy after that reflects only how /// long the gate lingers before it finishes fading shut. A longer `release` = more lingering /// energy; a snap close = little. (floor 0, so once fully closed the tail is silent.) private func closeWindowEnergy(release: Double) throws -> Double { let sampleRate: Double = 48000.0 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.6, 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: DecaySineSource = DecaySineSource(bus: srcBus.index) let gate: NoiseGatePedal = NoiseGatePedal(inputBus: srcBus.index, outputBus: dstBus.index) gate.sampleRate = sampleRate gate.threshold = 0.06 gate.floor = 0.0 gate.release = release let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) try engine.add(source) try engine.add(gate, position: NodePosition.after(source)) try engine.add(master, position: NodePosition.after(gate)) try engine.start() let inter: [Float] = readStereoCAF(url: url, sampleRate: sampleRate) let mono: [Double] = stride(from: 0, to: inter.count, by: 2).map { Double(inter[$0]) } let start: Int = Int(0.12 * sampleRate) // after the step-down; the gate is closing/closed var energy: Double = 0.0 for i: Int in start.. fast * 1.5, "long Release did not linger longer than short (fast=\(fast) slow=\(slow))") #expect(deflt > fast * 1.2, "default close is nearly as abrupt as the fast close (\(deflt) vs \(fast))") } @Test func exposesAttackReleaseInAdvancedPopover() throws { #expect(NoiseGatePedal().advancedKnobs.map { $0.label } == ["Attack", "Release"]) // Attack/Release are pop-over only — not on the face. let face: [String] = NoiseGatePedal().effectKnobs.map { $0.label } #expect(face == ["Threshold", "Floor"]) } @Test func attackReleaseRoundTripThroughState() throws { let pedal: NoiseGatePedal = NoiseGatePedal() pedal.attack = 0.02 pedal.release = 0.3 let restored: NoiseGatePedal = NoiseGatePedal() restored.applyState(pedal.captureState()) #expect(restored.attack == 0.02 && restored.release == 0.3, "Attack/Release did not round-trip") } @Test func legacyGateWithoutAdvancedValuesDefaultsTiming() throws { // A gate saved before Attack/Release existed has empty advancedValues → timing defaults, and // Threshold/Floor still restore correctly (append-only, no migration). let legacy: PedalState = PedalState( id: UUID(), type: "Noise Gate", knobValues: [0.03, 0.4, 1.0, 0.7], selectorValues: [], advancedValues: [], bypass: 0.0) let pedal: NoiseGatePedal = NoiseGatePedal() pedal.applyState(legacy) #expect(pedal.threshold == 0.03 && pedal.floor == 0.4, "legacy Threshold/Floor mis-mapped") #expect(pedal.attack == 0.005 && pedal.release == 0.08, "legacy gate should default Attack/Release") } }