// RBJFilterTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - Helpers /// Compute DFT magnitude at a single bin frequency. O(N) — fine for tests. private func dftMag(samples: [Double], freq: Double, sampleRate: Double) -> Double { let n: Int = samples.count guard n > 0 else { return 0 } var re: Double = 0.0, im = 0.0 for i: Int in 0.. 0.2) } // MARK: RBJHighPass @Test func hpfAttenuatesDC() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let hpf: RBJHighPass.Audio = RBJHighPass.Audio(input: dc, freq: 1000.0, q: 0.707) let samples: [Double] = collectBlocks(hpf, engine: engine, blocks: 16) let last: [Double] = Array(samples.suffix(64)) let mean: Double = last.map { abs(Double($0)) }.reduce(0, +) / Double(last.count) #expect(mean < 0.05) } @Test func hpfPassesHighFreq() { let sineHigh: OscSin.Audio = OscSin.Audio(freq: 8000.0) let hpf: RBJHighPass.Audio = RBJHighPass.Audio(input: sineHigh, freq: 1000.0, q: 0.707) let samples: [Double] = collectBlocks(hpf, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: 8000, sampleRate: sr) #expect(mag > 0.2) } // MARK: RBJBandPassSkirt @Test func bpfSkirtPassesCentreFreq() { let centreHz: Double = 1000.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let bpf: RBJBandPassSkirt.Audio = RBJBandPassSkirt.Audio(input: sine, freq: Double(centreHz), q: 2.0) let samples: [Double] = collectBlocks(bpf, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: centreHz, sampleRate: sr) #expect(mag > 0.05) } @Test func bpfSkirtAttenuatesDC() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let bpf: RBJBandPassSkirt.Audio = RBJBandPassSkirt.Audio(input: dc, freq: 1000.0, q: 2.0) let samples: [Double] = collectBlocks(bpf, engine: engine, blocks: 16) let last: [Double] = Array(samples.suffix(64)) let mean: Double = last.map { abs(Double($0)) }.reduce(0, +) / Double(last.count) #expect(mean < 0.05) } // MARK: RBJNotch @Test func notchAttenuatesAtCentreFreq() { let centreHz: Double = 1000.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let notch: RBJNotch.Audio = RBJNotch.Audio(input: sine, freq: Double(centreHz), q: 5.0) let samples: [Double] = collectBlocks(notch, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: centreHz, sampleRate: sr) #expect(mag < 0.1) } @Test func notchPassesDC() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let notch: RBJNotch.Audio = RBJNotch.Audio(input: dc, freq: 1000.0, q: 5.0) let samples: [Double] = collectBlocks(notch, engine: engine, blocks: 8) let last: [Double] = Array(samples.suffix(64)) let mean: Double = last.reduce(0.0 as Double, +) / Double(last.count) #expect(abs(Double(mean) - 1.0) < 0.1) } // MARK: RBJAllPass @Test func allpassOutputIsFiniteAfterManyBlocks() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let ap: RBJAllPass.Audio = RBJAllPass.Audio(input: sine, freq: 1000.0, q: 0.707) let samples: [Double] = collectBlocks(ap, engine: engine, blocks: 32) #expect(samples.allSatisfy { $0.isFinite }) } @Test func allpassPreservesRMS() { // Allpass has flat magnitude response: RMS of sustained sine should be preserved. let centreHz: Double = 1000.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let ap: RBJAllPass.Audio = RBJAllPass.Audio(input: sine, freq: Double(centreHz), q: 0.707) // Discard the first 4 blocks (transient) then compare RMS _ = collectBlocks(ap, engine: engine, blocks: 4) let samples: [Double] = collectBlocks(ap, engine: engine, blocks: 8) let rmsOut: Double = sqrt(samples.map { Double($0*$0) }.reduce(0,+) / Double(samples.count)) // Unit sine has RMS = 1/sqrt(2) ≈ 0.707; allpass should preserve it #expect(rmsOut > 0.3 && rmsOut < 0.9) } // MARK: RBJPeakingEQ @Test func peakingEQBoostsAtCentreFreq() { let centreHz: Double = 1000.0 let gainDB: Double = 12.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let eq: RBJPeakingEQ.Audio = RBJPeakingEQ.Audio(input: sine, freq: Double(centreHz), gain: Double(gainDB), q: 1.0) let samplesEQ: [Double] = collectBlocks(eq, engine: engine, blocks: 16) let magEQ: Double = dftMag(samples: samplesEQ, freq: centreHz, sampleRate: sr) // +12 dB = 4× amplitude; mag should be clearly > unfiltered baseline (~0.5) #expect(magEQ > 0.3) } @Test func peakingEQCutsAtCentreFreq() { let centreHz: Double = 1000.0 let gainDB: Double = -12.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let eq: RBJPeakingEQ.Audio = RBJPeakingEQ.Audio(input: sine, freq: Double(centreHz), gain: Double(gainDB), q: 1.0) let samplesEQ: [Double] = collectBlocks(eq, engine: engine, blocks: 16) let magEQ: Double = dftMag(samples: samplesEQ, freq: centreHz, sampleRate: sr) // -12 dB = 0.25× amplitude; well below unfiltered baseline #expect(magEQ < 0.2) } @Test func peakingEQUnityGainAtZeroDB() { let centreHz: Double = 1000.0 let sine: OscSin.Audio = OscSin.Audio(freq: centreHz) let eq: RBJPeakingEQ.Audio = RBJPeakingEQ.Audio(input: sine, freq: Double(centreHz), gain: 0.0, q: 1.0) let samples: [Double] = collectBlocks(eq, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: centreHz, sampleRate: sr) #expect(mag > 0.2 && mag < 0.8) } // MARK: RBJLowShelf @Test func lowShelfBoostsDC() { let gainDB: Double = 6.0 let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let shelf: RBJLowShelf.Audio = RBJLowShelf.Audio(input: dc, freq: 500.0, gain: Double(gainDB), slope: 1.0) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 16) let last: [Double] = Array(samples.suffix(64)) let mean: Double = last.reduce(0.0 as Double, +) / Double(last.count) // +6 dB ≈ 2× amplitude #expect(Double(mean) > 1.5) } @Test func lowShelfPassesHighFreqAtUnity() { // High-frequency sine well above the shelf freq should pass through near unity let sineHigh: OscSin.Audio = OscSin.Audio(freq: 8000.0) let shelf: RBJLowShelf.Audio = RBJLowShelf.Audio(input: sineHigh, freq: 200.0, gain: 12.0, slope: 1.0) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: 8000, sampleRate: sr) #expect(mag > 0.15 && mag < 1.0) } // MARK: RBJHighShelf @Test func highShelfBoostsHighFreq() { let gainDB: Double = 6.0 let sineHigh: OscSin.Audio = OscSin.Audio(freq: 8000.0) let shelf: RBJHighShelf.Audio = RBJHighShelf.Audio(input: sineHigh, freq: 2000.0, gain: Double(gainDB), slope: 1.0) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 16) let mag: Double = dftMag(samples: samples, freq: 8000, sampleRate: sr) // +6 dB → ~2× amplitude; unfiltered 8kHz mag ≈ 0.35–0.5; boosted should be > 0.4 #expect(mag > 0.3) } @Test func highShelfPassesDCAtUnity() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let shelf: RBJHighShelf.Audio = RBJHighShelf.Audio(input: dc, freq: 4000.0, gain: 12.0, slope: 1.0) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 16) let last: [Double] = Array(samples.suffix(64)) let mean: Double = last.reduce(0.0 as Double, +) / Double(last.count) #expect(abs(Double(mean) - 1.0) < 0.1) } // MARK: RBJBandPass (regression — uses migrated BiquadState) @Test func bandPassScalarPathStillWorks() { let sine: OscSin.Audio = OscSin.Audio(freq: 1000.0) let bpf: RBJBandPass.Audio = RBJBandPass.Audio(input: sine, freq: 1000.0, bandwidth: 1.0) let samples: [Double] = collectBlocks(bpf, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) let mag: Double = dftMag(samples: samples, freq: 1000, sampleRate: sr) #expect(mag > 0.05) } // MARK: Audio-rate modulation @Test func lpfAudioRateFreqSweepNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let sweep: OscSin.Audio = OscSin.Audio(freq: 0.5) let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: sine, freq: sweep, q: 1.0) let samples: [Double] = collectBlocks(lpf, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func hpfAudioRateFreqSweepNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let sweep: OscSin.Audio = OscSin.Audio(freq: 0.5) let hpf: RBJHighPass.Audio = RBJHighPass.Audio(input: sine, freq: sweep, q: 1.0) let samples: [Double] = collectBlocks(hpf, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func peakingEQAudioRateGainNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 1000.0) let gainMod: OscSin.Audio = OscSin.Audio(freq: 1.0) let eq: RBJPeakingEQ.Audio = RBJPeakingEQ.Audio(input: sine, freq: 1000.0, gain: gainMod, q: 1.0) let samples: [Double] = collectBlocks(eq, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func lowShelfAudioRateSlopeNoCrash() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let slopeMod: OscSin.Audio = OscSin.Audio(freq: 0.1) let shelf: RBJLowShelf.Audio = RBJLowShelf.Audio(input: dc, freq: 500.0, gain: 6.0, slope: slopeMod) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func highShelfAudioRateSlopeNoCrash() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let slopeMod: OscSin.Audio = OscSin.Audio(freq: 0.1) let shelf: RBJHighShelf.Audio = RBJHighShelf.Audio(input: dc, freq: 8000.0, gain: 3.0, slope: slopeMod) let samples: [Double] = collectBlocks(shelf, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func bandPassSkirtAudioRateFreqSweepNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let sweep: OscSin.Audio = OscSin.Audio(freq: 0.5) let bps: RBJBandPassSkirt.Audio = RBJBandPassSkirt.Audio(input: sine, freq: sweep, q: 2.0) let samples: [Double] = collectBlocks(bps, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func notchAudioRateFreqSweepNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let sweep: OscSin.Audio = OscSin.Audio(freq: 0.5) let notch: RBJNotch.Audio = RBJNotch.Audio(input: sine, freq: sweep, q: 4.0) let samples: [Double] = collectBlocks(notch, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } @Test func allPassAudioRateFreqSweepNoCrash() { let sine: OscSin.Audio = OscSin.Audio(freq: 440.0) let sweep: OscSin.Audio = OscSin.Audio(freq: 0.5) let ap: RBJAllPass.Audio = RBJAllPass.Audio(input: sine, freq: sweep, q: 0.707) let samples: [Double] = collectBlocks(ap, engine: engine, blocks: 8) #expect(samples.allSatisfy { $0.isFinite }) } // MARK: Nyquist guard @Test func filtersReturnSilenceAboveNyquist() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: dc, freq: 25000.0, q: 1.0) let block: [Double] = lpf.calculatedSamples(graph: graph, count: 64)[0] #expect(block.prefix(64).allSatisfy { $0 == 0 }) } @Test func filtersReturnSilenceForNegativeFreq() { let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let hpf: RBJHighPass.Audio = RBJHighPass.Audio(input: dc, freq: -100.0, q: 1.0) let block: [Double] = hpf.calculatedSamples(graph: graph, count: 64)[0] #expect(block.prefix(64).allSatisfy { $0 == 0 }) } // MARK: Denormal handling (with FTZ, matching RT context) @Test func lpfNoSubnormalsAfterLongSilence() throws { // Matches RT context: CoreAudioDriver.render() calls enableFlushToZero() before each block. enableFlushToZero() let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr)) let impulseGen: Impulse.Audio = Impulse.Audio(freq: impulseFreqHz) let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: impulseGen, freq: 200.0, q: 1.0) let bs: Int = engine.blockSize for _ in 0..<200 { let g: SynthGraph = makeSynthGraph(engine: engine) let block: [Double] = lpf.calculatedSamples(graph: g, count: bs)[0] for sample: Double in block.prefix(bs) { #expect(!sample.isSubnormal) } advanceBlock(engine) } } @Test func notchNoSubnormalsAfterLongSilence() throws { enableFlushToZero() let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr)) let impulseGen: Impulse.Audio = Impulse.Audio(freq: impulseFreqHz) let notch: RBJNotch.Audio = RBJNotch.Audio(input: impulseGen, freq: 1000.0, q: 2.0) let bs: Int = engine.blockSize for _ in 0..<200 { let g: SynthGraph = makeSynthGraph(engine: engine) let block: [Double] = notch.calculatedSamples(graph: g, count: bs)[0] for sample: Double in block.prefix(bs) { #expect(!sample.isSubnormal) } advanceBlock(engine) } } private func noSubnormalsCheck(filter: any Generator, blocks: Int = 200) { enableFlushToZero() let bs: Int = engine.blockSize for _ in 0..