510 lines
22 KiB
Swift
510 lines
22 KiB
Swift
// 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..<n {
|
||
let phase: Double = 2.0 * Double.pi * freq * Double(i) / sampleRate
|
||
re += Double(samples[i]) * cos(phase)
|
||
im -= Double(samples[i]) * sin(phase)
|
||
}
|
||
return sqrt(re*re + im*im) / Double(n)
|
||
}
|
||
|
||
// MARK: - RBJFilterTests
|
||
|
||
@Suite("RBJFilters")
|
||
struct RBJFilterTests {
|
||
|
||
let engine: SynthesisEngine
|
||
let sr: Double = 44100
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
// MARK: BiquadCoeffs / BiquadState (unit)
|
||
|
||
@Test func biquadZeroCoeffsProducesSilence() {
|
||
var state: BiquadState = BiquadState()
|
||
let input: [Double] = [Double](repeating: 0.5, count: 8)
|
||
var result: [Double] = [Double](repeating: 0, count: 8)
|
||
state.processBlock(input: input, coeffs: .zero, into: &result, count: 8)
|
||
#expect(result.allSatisfy { $0 == 0 })
|
||
}
|
||
|
||
@Test func biquadIdentityPassthrough() {
|
||
// nb0=1, nb1=nb2=na1=na2=0 → y[n]=x[n]
|
||
var state: BiquadState = BiquadState()
|
||
let coeffs: BiquadCoeffs = BiquadCoeffs(nb0: 1, nb1: 0, nb2: 0, na1: 0, na2: 0)
|
||
let input: [Double] = [1, 2, 3, 4, 5]
|
||
var result: [Double] = [Double](repeating: 0, count: 5)
|
||
state.processBlock(input: input, coeffs: coeffs, into: &result, count: 5)
|
||
for (a, b) in zip(result, input) {
|
||
#expect(abs(Double(a) - Double(b)) < 1e-12)
|
||
}
|
||
}
|
||
|
||
@Test func biquadInterpolatedPathRunsWithoutCrash() {
|
||
var state: BiquadState = BiquadState()
|
||
let start: BiquadCoeffs = BiquadCoeffs(nb0: 1, nb1: 0, nb2: 0, na1: 0, na2: 0)
|
||
let end: BiquadCoeffs = BiquadCoeffs(nb0: 0.5, nb1: 0, nb2: 0, na1: 0, na2: 0)
|
||
let input: [Double] = [Double](repeating: 0.1, count: 64)
|
||
var result: [Double] = [Double](repeating: 0, count: 64)
|
||
state.processBlock(input: input, startCoeffs: start, endCoeffs: end,
|
||
into: &result, count: 64)
|
||
#expect(result.allSatisfy { $0.isFinite })
|
||
}
|
||
|
||
// MARK: RBJLowPass
|
||
|
||
@Test func lpfPassesDC() {
|
||
// DC input through a 1 kHz LPF should settle near 1.0
|
||
let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||
let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: dc, freq: 1000.0, q: 0.707)
|
||
let samples: [Double] = collectBlocks(lpf, 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.05)
|
||
}
|
||
|
||
@Test func lpfAttenuatesAboveCutoff() {
|
||
let sineHigh: OscSin.Audio = OscSin.Audio(freq: 8000.0)
|
||
let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: sineHigh, freq: 1000.0, q: 0.707)
|
||
let samples: [Double] = collectBlocks(lpf, engine: engine, blocks: 16)
|
||
let mag: Double = dftMag(samples: samples, freq: 8000, sampleRate: sr)
|
||
#expect(mag < 0.1)
|
||
}
|
||
|
||
@Test func lpfPassesFreqBelowCutoff() {
|
||
let sineLow: OscSin.Audio = OscSin.Audio(freq: 100.0)
|
||
let lpf: RBJLowPass.Audio = RBJLowPass.Audio(input: sineLow, freq: 1000.0, q: 0.707)
|
||
let samples: [Double] = collectBlocks(lpf, engine: engine, blocks: 16)
|
||
let mag: Double = dftMag(samples: samples, freq: 100, sampleRate: sr)
|
||
#expect(mag > 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..<blocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let block: [Double] = filter.calculatedSamples(graph: g, count: bs)[0]
|
||
for sample: Double in block.prefix(bs) {
|
||
#expect(!sample.isSubnormal)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func hpfNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJHighPass.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 400.0, q: 1.0))
|
||
}
|
||
|
||
@Test func bandPassSkirtNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJBandPassSkirt.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 1000.0, q: 2.0))
|
||
}
|
||
|
||
@Test func allPassNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJAllPass.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 1000.0, q: 0.707))
|
||
}
|
||
|
||
@Test func peakingEQNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJPeakingEQ.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 1000.0, gain: 6.0, q: 1.0))
|
||
}
|
||
|
||
@Test func lowShelfNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJLowShelf.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 200.0, gain: 6.0, slope: 1.0))
|
||
}
|
||
|
||
@Test func highShelfNoSubnormalsAfterLongSilence() {
|
||
let impulseFreqHz: Double = 1.0 / (Double(engine.blockSize * 100) / Double(sr))
|
||
noSubnormalsCheck(filter: RBJHighShelf.Audio(input: Impulse.Audio(freq: impulseFreqHz), freq: 8000.0, gain: 3.0, slope: 1.0))
|
||
}
|
||
|
||
// MARK: Deterministic output
|
||
|
||
// Two fresh instances with identical parameters and identical constant input must produce
|
||
// bit-identical output on the first block — verifies no hidden non-determinism.
|
||
|
||
private func deterministicCheck<G: Generator>(_ a: G, _ b: G) {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let outA: Array<Double>.SubSequence = a.calculatedSamples(graph: g, count: engine.blockSize)[0].prefix(engine.blockSize)
|
||
let outB: Array<Double>.SubSequence = b.calculatedSamples(graph: g, count: engine.blockSize)[0].prefix(engine.blockSize)
|
||
#expect(Array(outA) == Array(outB))
|
||
}
|
||
|
||
@Test func deterministicOutputLowPass() {
|
||
deterministicCheck(
|
||
RBJLowPass.Audio(input: 0.5, freq: 800.0, q: 0.707),
|
||
RBJLowPass.Audio(input: 0.5, freq: 800.0, q: 0.707)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputHighPass() {
|
||
deterministicCheck(
|
||
RBJHighPass.Audio(input: 0.5, freq: 400.0, q: 0.707),
|
||
RBJHighPass.Audio(input: 0.5, freq: 400.0, q: 0.707)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputBandPassSkirt() {
|
||
deterministicCheck(
|
||
RBJBandPassSkirt.Audio(input: 0.5, freq: 1000.0, q: 2.0),
|
||
RBJBandPassSkirt.Audio(input: 0.5, freq: 1000.0, q: 2.0)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputNotch() {
|
||
deterministicCheck(
|
||
RBJNotch.Audio(input: 0.5, freq: 1000.0, q: 4.0),
|
||
RBJNotch.Audio(input: 0.5, freq: 1000.0, q: 4.0)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputAllPass() {
|
||
deterministicCheck(
|
||
RBJAllPass.Audio(input: 0.5, freq: 1000.0, q: 0.707),
|
||
RBJAllPass.Audio(input: 0.5, freq: 1000.0, q: 0.707)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputPeakingEQ() {
|
||
deterministicCheck(
|
||
RBJPeakingEQ.Audio(input: 0.5, freq: 1000.0, gain: 6.0, q: 1.0),
|
||
RBJPeakingEQ.Audio(input: 0.5, freq: 1000.0, gain: 6.0, q: 1.0)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputLowShelf() {
|
||
deterministicCheck(
|
||
RBJLowShelf.Audio(input: 0.5, freq: 200.0, gain: 6.0, slope: 1.0),
|
||
RBJLowShelf.Audio(input: 0.5, freq: 200.0, gain: 6.0, slope: 1.0)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputHighShelf() {
|
||
deterministicCheck(
|
||
RBJHighShelf.Audio(input: 0.5, freq: 8000.0, gain: 3.0, slope: 1.0),
|
||
RBJHighShelf.Audio(input: 0.5, freq: 8000.0, gain: 3.0, slope: 1.0)
|
||
)
|
||
}
|
||
|
||
@Test func deterministicOutputBandPass() {
|
||
deterministicCheck(
|
||
RBJBandPass.Audio(input: 0.5, freq: 1000.0, bandwidth: 1.0),
|
||
RBJBandPass.Audio(input: 0.5, freq: 1000.0, bandwidth: 1.0)
|
||
)
|
||
}
|
||
}
|