// ExpanderParameterRampingTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("Expander parameter ramping") struct ExpanderParameterRampingTests { @Test func thresholdGeneratorInputIsRead() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 64) try engine.start() defer { engine.stop() } let threshNode: ParameterNode = ParameterNode(-40.0) let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.1) let expander: Expander.Audio = Expander.Audio(in: signal, threshold: threshNode) let g: SynthGraph = makeSynthGraph(engine: engine) let out: [[Double]] = expander.calculatedSamples(graph: g, count: 64) let samples: [Double] = Array((out.first ?? []).prefix(64)) #expect(samples.allSatisfy { $0.isFinite }, "Expander.Audio output must be finite with ParameterNode threshold") } @Test func thresholdParameterRampChangesGain() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 64) try engine.start() defer { engine.stop() } // Start with threshold well above signal (-10 dB, signal ~0.1 ≈ -20 dB) → expansion active. let threshNode: ParameterNode = ParameterNode(-10.0) let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.1) let expander: Expander.Audio = Expander.Audio(in: signal, threshold: threshNode, ratio: 0.0) // gate let g1: SynthGraph = makeSynthGraph(engine: engine) let out1: [[Double]] = expander.calculatedSamples(graph: g1, count: 64) advanceBlock(engine) // Now set threshold very low (gate opens) → signal passes through. threshNode.value = -80.0 let g2: SynthGraph = makeSynthGraph(engine: engine) let out2: [[Double]] = expander.calculatedSamples(graph: g2, count: 64) let rms1: Double = rmsValue(Array((out1.first ?? []).prefix(64))) let rms2: Double = rmsValue(Array((out2.first ?? []).prefix(64))) // With gate open (threshold=-80dB), signal should pass more freely. #expect(rms2 > rms1, "After gate threshold ramp to -80dB, signal should pass more freely: \(rms2) vs \(rms1)") } @Test func controlThresholdGeneratorInputIsRead() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 64) try engine.start() defer { engine.stop() } let threshNode: ParameterNode = ParameterNode(-40.0) let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.1) let expander: Expander.Control = Expander.Control(in: signal, threshold: threshNode) let g: SynthGraph = makeSynthGraph(engine: engine) let out: [[Double]] = expander.calculatedSamples(graph: g, count: 64) let samples: [Double] = Array((out.first ?? []).prefix(64)) #expect(samples.allSatisfy { $0.isFinite }, "Expander.Control output must be finite with ParameterNode threshold") } }