491 lines
22 KiB
Swift
491 lines
22 KiB
Swift
// 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..<svfChannelCount {
|
||
#expect(Array(result[ch].prefix(blockSize)).count == blockSize)
|
||
}
|
||
}
|
||
|
||
// MARK: 3. Fan-out
|
||
|
||
@Test func audioFanOutCacheHit() {
|
||
let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
let g: SynthGraph = graph
|
||
let first: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let second: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
#expect(Array(first[0].prefix(blockSize)) == Array(second[0].prefix(blockSize)))
|
||
}
|
||
|
||
@Test func controlFanOutCacheHit() {
|
||
let svf: SVF.Control = SVF.Control(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
let g: SynthGraph = graph
|
||
let first: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let second: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
#expect(Array(first[0].prefix(blockSize)) == Array(second[0].prefix(blockSize)))
|
||
}
|
||
|
||
// MARK: 4. State advances once per block (cache hit does not double-advance)
|
||
|
||
@Test func audioStateAdvancesOncePerBlock() {
|
||
let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
let g: SynthGraph = graph
|
||
// Two calls in same segment return the same result (cache hit).
|
||
let a: [Double] = Array(svf.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
|
||
let b: [Double] = Array(svf.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
|
||
#expect(a == b)
|
||
}
|
||
|
||
// MARK: 5. Integration — runs inside a Synthesizer without crash
|
||
|
||
@Test func audioIntegrationNoCrash() {
|
||
// Drive the generator directly for several blocks; must not crash or produce NaN/Inf.
|
||
let svf: SVF.Audio = SVF.Audio(input: WhiteNoise.Audio(), freq: 800.0, q: 1.5)
|
||
let samples: [Double] = collectBlocks(svf, engine: engine, blocks: 4)
|
||
for s: Double in samples {
|
||
#expect(s.isFinite, "SVF integration output must be finite; got \(s)")
|
||
}
|
||
}
|
||
|
||
// MARK: 6. Lowpass passes DC
|
||
|
||
@Test func lowpassPassesDC() {
|
||
let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
// Settle for 4096 samples (64 blocks)
|
||
for _ in 0..<64 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = svf.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let lpSamples: [Double] = Array(result[0].prefix(blockSize))
|
||
let mean: Double = lpSamples.reduce(0.0, +) / Double(blockSize)
|
||
#expect(abs(mean - 1.0) < 0.05, "Lowpass output should be ≈ 1.0 in steady state; got \(mean)")
|
||
}
|
||
|
||
// MARK: 7. Highpass rejects DC
|
||
|
||
@Test func highpassRejectsDC() {
|
||
let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
// Settle for 4096 samples (64 blocks)
|
||
for _ in 0..<64 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = svf.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let hpSamples: [Double] = Array(result[1].prefix(blockSize))
|
||
let mean: Double = hpSamples.map { abs($0) }.reduce(0.0, +) / Double(blockSize)
|
||
#expect(mean < 0.05, "Highpass should reject DC; mean abs value was \(mean)")
|
||
}
|
||
|
||
// MARK: 8. Notch = highpass + lowpass
|
||
|
||
@Test func notchEqualsHighPlusLow() {
|
||
let svf: SVF.Audio = SVF.Audio(input: OscSin.Audio(freq: 330.0), freq: 800.0, q: 1.0)
|
||
// Settle
|
||
for _ in 0..<8 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = svf.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let lp: [Double] = Array(result[0].prefix(blockSize))
|
||
let hp: [Double] = Array(result[1].prefix(blockSize))
|
||
let notch: [Double] = Array(result[3].prefix(blockSize))
|
||
for i: Int in 0..<blockSize {
|
||
let expected: Double = lp[i] + hp[i]
|
||
#expect(abs(notch[i] - expected) < 1e-9,
|
||
"notch[\(i)] = \(notch[i]) ≠ lp[\(i)] + hp[\(i)] = \(expected)")
|
||
}
|
||
}
|
||
|
||
// MARK: 9. Bandpass resonant peak
|
||
|
||
@Test func bandpassResonantPeak() {
|
||
// Drive with an impulse — high Q bandpass should ring with significant energy.
|
||
let impulseGen: Impulse.Audio = Impulse.Audio(freq: 1.0)
|
||
let svf: SVF.Audio = SVF.Audio(input: impulseGen, freq: 1000.0, q: 10.0)
|
||
let g: SynthGraph = graph
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let bp: [Double] = Array(result[2].prefix(blockSize))
|
||
// RMS energy over the block should be well above noise floor for a ringing high-Q filter.
|
||
let rms: Double = (bp.map { $0 * $0 }.reduce(0.0, +) / Double(blockSize)).squareRoot()
|
||
#expect(rms > 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..<settleBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = svfLow.calculatedSamples(graph: g, count: blockSize)
|
||
_ = svfHigh.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
// Measure
|
||
for _ in 0..<measureBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let lowResult: [[Double]] = svfLow.calculatedSamples(graph: g, count: blockSize)
|
||
let highResult: [[Double]] = svfHigh.calculatedSamples(graph: g, count: blockSize)
|
||
let lpLow: [Double] = Array(lowResult[0].prefix(blockSize))
|
||
let lpHigh: [Double] = Array(highResult[0].prefix(blockSize))
|
||
rmsLow += lpLow.map { $0 * $0 }.reduce(0.0, +)
|
||
rmsHigh += lpHigh.map { $0 * $0 }.reduce(0.0, +)
|
||
advanceBlock(engine)
|
||
}
|
||
rmsLow = (rmsLow / Double(measureBlocks * blockSize)).squareRoot()
|
||
rmsHigh = (rmsHigh / Double(measureBlocks * blockSize)).squareRoot()
|
||
|
||
// High-cutoff lowpass passes significantly more energy than low-cutoff lowpass.
|
||
#expect(rmsHigh > 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..<settleBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = audioSVF.calculatedSamples(graph: g, count: blockSize)
|
||
_ = controlSVF.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let audioResult: [[Double]] = audioSVF.calculatedSamples(graph: g, count: blockSize)
|
||
let controlResult: [[Double]] = controlSVF.calculatedSamples(graph: g, count: blockSize)
|
||
|
||
// Last sample of block: lowpass channel (channel 0).
|
||
let audioLastLP: Double = audioResult[0][blockSize - 1]
|
||
let controlLastLP: Double = controlResult[0][blockSize - 1]
|
||
|
||
// Both run the same Chamberlin SVF recurrence; results should be very close.
|
||
let tolerance: Double = 1e-9
|
||
#expect(abs(audioLastLP - controlLastLP) < tolerance,
|
||
"Audio/Control lowpass mismatch at block boundary: audio=\(audioLastLP), control=\(controlLastLP)")
|
||
}
|
||
|
||
// MARK: 13. mulAdd applied to all channels
|
||
|
||
@Test func mulAddAppliedToAllChannels() {
|
||
let mulFactor: Double = 2.0
|
||
let addOffset: Double = 1.0
|
||
|
||
let inputDC: any ValidGeneratorInput = ConstantGeneratorInput(0.5)
|
||
let svfPlain: SVF.Audio = SVF.Audio(input: inputDC, freq: 800.0, q: 1.0)
|
||
let svfMulAdd: SVF.Audio = SVF.Audio(input: inputDC, freq: 800.0, q: 1.0,
|
||
mul: mulFactor, add: addOffset)
|
||
|
||
// Settle
|
||
let settleBlocks: Int = 32
|
||
for _ in 0..<settleBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = svfPlain.calculatedSamples(graph: g, count: blockSize)
|
||
_ = svfMulAdd.calculatedSamples(graph: g, count: blockSize)
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let plainResult: [[Double]] = svfPlain.calculatedSamples(graph: g, count: blockSize)
|
||
let mulAddResult: [[Double]] = svfMulAdd.calculatedSamples(graph: g, count: blockSize)
|
||
|
||
// Check all svfChannelCount channels: mulAdd variant should be (plain × 2 + 1).
|
||
// Use mid-block sample index to avoid ramp transients.
|
||
let sampleIdx: Int = blockSize / 2
|
||
for ch: Int in 0..<svfChannelCount {
|
||
let expected: Double = plainResult[ch][sampleIdx] * mulFactor + addOffset
|
||
let actual: Double = mulAddResult[ch][sampleIdx]
|
||
#expect(abs(actual - expected) < 1e-9,
|
||
"Channel \(ch) mulAdd mismatch: expected \(expected), got \(actual)")
|
||
}
|
||
}
|
||
|
||
// MARK: 14. Nyquist-safe on high frequency
|
||
|
||
@Test func nyquistSafeOnHighFreq() {
|
||
// freq = 20000 at sampleRate = 44100 — above the π/sr stability limit.
|
||
// f coefficient is clamped to 1.0 so output must stay finite.
|
||
let highFreqSvf: SVF.Audio = SVF.Audio(input: OscSin.Audio(freq: 440.0), freq: 20000.0, q: 1.0)
|
||
for _ in 0..<16 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = highFreqSvf.calculatedSamples(graph: g, count: blockSize)
|
||
for ch: Int in 0..<svfChannelCount {
|
||
let chSamples: [Double] = Array(result[ch].prefix(blockSize))
|
||
for s: Double in chSamples {
|
||
#expect(s.isFinite, "SVF output should be finite at freq=20000; got \(s) on channel \(ch)")
|
||
}
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
// MARK: 15. Named aliases compile and return correct channel
|
||
|
||
@Test func namedAliasesAudio() {
|
||
let svf: SVF.Audio = SVF.Audio(input: 1.0, freq: 500.0, q: svfButterworthQ)
|
||
// Compile-time check that aliases exist and return ChannelSelect.Audio.
|
||
let lp: ChannelSelect.Audio = svf.lowpass
|
||
let hp: ChannelSelect.Audio = svf.highpass
|
||
let bp: ChannelSelect.Audio = svf.bandpass
|
||
let nt: ChannelSelect.Audio = svf.notch
|
||
|
||
// Evaluate each alias and verify channel consistency.
|
||
let g: SynthGraph = graph
|
||
let fullResult: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let lpFromAlias: [[Double]] = lp.calculatedSamples(graph: g, count: blockSize)
|
||
let hpFromAlias: [[Double]] = hp.calculatedSamples(graph: g, count: blockSize)
|
||
let bpFromAlias: [[Double]] = bp.calculatedSamples(graph: g, count: blockSize)
|
||
let ntFromAlias: [[Double]] = nt.calculatedSamples(graph: g, count: blockSize)
|
||
|
||
#expect(Array(fullResult[0].prefix(blockSize)) == Array(lpFromAlias[0].prefix(blockSize)))
|
||
#expect(Array(fullResult[1].prefix(blockSize)) == Array(hpFromAlias[0].prefix(blockSize)))
|
||
#expect(Array(fullResult[2].prefix(blockSize)) == Array(bpFromAlias[0].prefix(blockSize)))
|
||
#expect(Array(fullResult[3].prefix(blockSize)) == Array(ntFromAlias[0].prefix(blockSize)))
|
||
}
|
||
|
||
@Test func namedAliasesControl() {
|
||
let svf: SVF.Control = SVF.Control(input: 1.0, freq: 500.0, q: svfButterworthQ)
|
||
let lp: ChannelSelect.Control = svf.lowpass
|
||
let hp: ChannelSelect.Control = svf.highpass
|
||
let bp: ChannelSelect.Control = svf.bandpass
|
||
let nt: ChannelSelect.Control = svf.notch
|
||
|
||
let g: SynthGraph = graph
|
||
_ = lp.calculatedSamples(graph: g, count: blockSize)
|
||
_ = hp.calculatedSamples(graph: g, count: blockSize)
|
||
_ = bp.calculatedSamples(graph: g, count: blockSize)
|
||
_ = nt.calculatedSamples(graph: g, count: blockSize)
|
||
// If we get here without crash, aliases work for Control too.
|
||
}
|
||
|
||
// MARK: 16. GeneratorEnum type aliases resolve
|
||
|
||
@Test func generatorEnumTypeAliases() {
|
||
// SVF must conform to GeneratorEnum; AudioType and ControlType must be correct.
|
||
let audio: SVF.AudioType = SVF.Audio(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
let ctrl: SVF.ControlType = SVF.Control(input: 1.0, freq: 1000.0, q: svfButterworthQ)
|
||
#expect(audio.rate == .audio)
|
||
#expect(ctrl.rate == .control)
|
||
}
|
||
|
||
// MARK: 17. Control ramp — last sample reaches target
|
||
|
||
@Test func controlRampLastSampleReachesTarget() {
|
||
// Constant DC input at 1.0 with a low cutoff; lowpass should ramp toward 1.0.
|
||
let svf: SVF.Control = SVF.Control(input: 1.0, freq: 200.0, q: svfButterworthQ)
|
||
// After many blocks the lowpass should settle near 1.0;
|
||
// the ramp convention means result[0][count-1] == previousOutputValue after the call.
|
||
let settleBlocks: Int = 256
|
||
for _ in 0..<settleBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let lastSample: Double = result[0][blockSize - 1]
|
||
// After settle, previousOutputValue should equal the last sample computed.
|
||
#expect(abs(lastSample - svf.previousOutputValue) < 1e-9,
|
||
"previousOutputValue should equal last sample; last=\(lastSample), prev=\(svf.previousOutputValue)")
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
// MARK: 18. Freq driven by audio generator (per-sample freq path)
|
||
|
||
@Test func freqDrivenByAudioGenerator() {
|
||
// LFO sweeping cutoff between 1 kHz and 9 kHz (center=5000, amp=4000).
|
||
let lfo: OscSin.Audio = OscSin.Audio(freq: 0.5, mul: 4000.0, add: 5000.0)
|
||
let input: OscSin.Audio = OscSin.Audio(freq: 100.0)
|
||
let svf: SVF.Audio = SVF.Audio(input: input, freq: lfo, q: svfButterworthQ)
|
||
|
||
var allFinite: Bool = true
|
||
var allSame: Bool = true
|
||
var firstSample: Double? = nil
|
||
|
||
for _ in 0..<8 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let lp: [Double] = Array(result[0].prefix(blockSize))
|
||
for s: Double in lp {
|
||
if !s.isFinite { allFinite = false }
|
||
if let prev: Double = firstSample, s != prev { allSame = false }
|
||
if firstSample == nil { firstSample = s }
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
#expect(allFinite, "SVF.Audio with LFO-driven freq must produce finite output")
|
||
// Lowpass output must vary across samples — confirms per-sample freq path executed.
|
||
#expect(!allSame, "SVF.Audio with LFO-driven freq must produce non-constant output")
|
||
}
|
||
|
||
// MARK: 19. Q driven by audio generator (per-sample q path)
|
||
|
||
@Test func qDrivenByAudioGenerator() {
|
||
// LFO sweeping Q between 0.5 and 5.0.
|
||
let qLFO: OscSin.Audio = OscSin.Audio(freq: 0.3, mul: 2.25, add: 2.75)
|
||
let svf: SVF.Audio = SVF.Audio(input: OscSin.Audio(freq: 440.0), freq: 1000.0, q: qLFO)
|
||
|
||
var allFinite: Bool = true
|
||
var allSame: Bool = true
|
||
var firstSample: Double? = nil
|
||
|
||
for _ in 0..<8 {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = svf.calculatedSamples(graph: g, count: blockSize)
|
||
let bp: [Double] = Array(result[2].prefix(blockSize))
|
||
for s: Double in bp {
|
||
if !s.isFinite { allFinite = false }
|
||
if let prev: Double = firstSample, s != prev { allSame = false }
|
||
if firstSample == nil { firstSample = s }
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
#expect(allFinite, "SVF.Audio with LFO-driven Q must produce finite output")
|
||
#expect(!allSame, "SVF.Audio with LFO-driven Q must produce non-constant bandpass output")
|
||
}
|
||
|
||
// MARK: 20. Control matches Audio with audio-rate input (sub-block bypass verification)
|
||
|
||
@Test func controlMatchesAudioWithAudioRateInput() {
|
||
// Use two OscSin generators at different freqs summed via Add — produces
|
||
// a non-trivially-changing per-sample input that is deterministic.
|
||
// Both SVF.Audio and SVF.Control receive the same signal source (fan-out safe).
|
||
let sig: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.5, add: 0.0)
|
||
let sig2: OscSin.Audio = OscSin.Audio(freq: 333.0, mul: 0.5, add: 0.0)
|
||
// Use operator + (BinaryOpGenerator) to sum two signals sample-by-sample.
|
||
let mixed: BinaryOpGenerator = sig + sig2
|
||
|
||
let audioSVF: SVF.Audio = SVF.Audio(input: mixed, freq: 800.0, q: 1.2)
|
||
let controlSVF: SVF.Control = SVF.Control(input: mixed, freq: 800.0, q: 1.2)
|
||
|
||
// Drive several full audio blocks.
|
||
let tolerance: Double = 1e-9
|
||
let testBlocks: Int = 8
|
||
for _ in 0..<testBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let audioResult: [[Double]] = audioSVF.calculatedSamples(graph: g, count: blockSize)
|
||
let controlResult: [[Double]] = controlSVF.calculatedSamples(graph: g, count: blockSize)
|
||
|
||
// Last sample of block (lowpass channel) must match between Audio and Control.
|
||
let audioLast: Double = audioResult[0][blockSize - 1]
|
||
let controlLast: Double = controlResult[0][blockSize - 1]
|
||
let diff: Double = abs(audioLast - controlLast)
|
||
#expect(diff < tolerance,
|
||
"Audio/Control lowpass mismatch with audio-rate input: audio=\(audioLast), control=\(controlLast)")
|
||
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
}
|