// SVFTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - SVFTests @Suite("SVF") struct SVFTests { let engine: SynthesisEngine let blockSize: Int = 64 let sampleRate: Double = 44100 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } var graph: SynthGraph { makeSynthGraph(engine: engine) } // MARK: 1. Rate @Test func audioRate() { let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ) #expect(svf.rate == .audio) } @Test func controlRate() { let svf: SVF.Control = SVF.Control(input: 1.0, freq: 1000.0, q: svfButterworthQ) #expect(svf.rate == .control) } // MARK: 2. Output channel count @Test func audioOutputChannelCount() { let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ) let g: SynthGraph = graph let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize) #expect(result.count == svfChannelCount) } @Test func controlOutputChannelCount() { let svf: SVF.Control = SVF.Control(input: 1.0, freq: 1000.0, q: svfButterworthQ) let g: SynthGraph = graph let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize) #expect(result.count == svfChannelCount) } @Test func multiChannelOutputCount() { // Required spec test: _computeSamples returns svfChannelCount rows. let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ) let g: SynthGraph = graph let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize) #expect(result.count == svfChannelCount) for ch: Int in 0.. 0.05, "Bandpass RMS should exceed 0.05 after impulse with Q=10; got \(rms)") } // MARK: 10. Denormal guard — no subnormal accumulation @Test func denormalGuardSettles() { // Drive a tiny impulse then run many blocks of silence. // denormalsToZero() prevents IEEE subnormal accumulation (values below ~2.2e-308). // After sufficient decay the state should either reach exactly 0 or have no // IEEE subnormal values (i.e. every state update went through the guard). // The impulse period is 1/8192 Hz → period of 8192 samples at 44100 Hz, // which is >> the 256-block settle window (16384 samples), so only one impulse fires. let impulseGen: Impulse.Audio = Impulse.Audio(freq: 1.0 / 8192.0) let svf: SVF.Audio = SVF.Audio(input: impulseGen, freq: 500.0, q: svfButterworthQ) // One impulse block let g0: SynthGraph = makeSynthGraph(engine: engine) _ = svf.calculatedSamples(graph: g0, count: blockSize) advanceBlock(engine) // 256 blocks of silence (16384 samples — plenty of decay time) for _ in 0..<256 { let g: SynthGraph = makeSynthGraph(engine: engine) _ = svf.calculatedSamples(graph: g, count: blockSize) advanceBlock(engine) } // Neither state variable should be an IEEE subnormal — denormalsToZero guards that. #expect(!svf.low.isSubnormal, "low state must not be subnormal; got \(svf.low)") #expect(!svf.band.isSubnormal, "band state must not be subnormal; got \(svf.band)") } // MARK: 11. Frequency change responds to generator input @Test func frequencyChangeRespondsToGeneratorInput() { // Compare lowpass RMS when cutoff is very low vs. very high for a broadband input. // Low cutoff → passes less energy than high cutoff. let noise: WhiteNoise.Audio = WhiteNoise.Audio() let svfLow: SVF.Audio = SVF.Audio(input: noise, freq: 100.0, q: svfButterworthQ) let svfHigh: SVF.Audio = SVF.Audio(input: noise, freq: 10000.0, q: svfButterworthQ) var rmsLow: Double = 0.0 var rmsHigh: Double = 0.0 let settleBlocks: Int = 32 let measureBlocks: Int = 32 // Settle for _ in 0.. rmsLow * 2.0, "High-cutoff lowpass RMS (\(rmsHigh)) should exceed low-cutoff RMS (\(rmsLow)) by at least 2×") } // MARK: 12. Control and Audio match at block boundary @Test func controlAndAudioMatchAtBlockBoundary() { // Both variants with identical constant input, freq, q — control output at // last sample of block should match audio output at last sample. let dc: any ValidGeneratorInput = ConstantGeneratorInput(0.5) let freq: Double = 800.0 let q: Double = 1.0 let audioSVF: SVF.Audio = SVF.Audio(input: dc, freq: freq, q: q) let controlSVF: SVF.Control = SVF.Control(input: dc, freq: freq, q: q) // Settle both for the same number of samples to align IIR state. let settleBlocks: Int = 16 for _ in 0..