Files
Lyrebird/Tests/LyrebirdTests/Generators/Delays/DelayLineCubicTests.swift
T
2026-06-21 11:45:58 -07:00

94 lines
4.2 KiB
Swift

// DelayLineCubicTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - DelayLineCubicTests
@MainActor
@Suite("DelayLine cubic")
struct DelayLineCubicTests {
let engine: SynthesisEngine
let sr: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
/// A unit impulse through a fractional pure delay reproduces the interpolation
/// kernel's impulse response directly (no feedback to entangle the math).
/// Delay = 3.5 samples → integer part 3, frac 0.5. The impulse is written at
/// buffer index 0 (frame 0); reads happen before writes, so it surfaces across
/// the four cubic taps at frames 2…5.
@Test func cubicImpulseResponseMatchesCatmullRomAtHalfSample() {
// catmullRomCubic basis at t = 0.5: pM1 → -0.0625, p0 → 0.5625,
// p1 → 0.5625, p2 → -0.0625. As the lone impulse passes through taps
// (i0,i1,i2,i3) = (writePos-intD+1 … -2), output[2..5] trace that basis.
let dl: DelayLine = DelayLine(input: Impulse.Audio(freq: 1.0),
maxDelay: 16.0 / sr, delay: 3.5 / sr,
sampleRate: sr, interpolation: .cubic)
let g: SynthGraph = makeSynthGraph(engine: engine)
let s: [Double] = dl.calculatedSamples(graph: g, count: engine.blockSize)[0]
#expect(abs(s[0]) < 1e-12)
#expect(abs(s[1]) < 1e-12)
#expect(abs(s[2] - (-0.0625)) < 1e-12)
#expect(abs(s[3] - 0.5625) < 1e-12)
#expect(abs(s[4] - 0.5625) < 1e-12)
#expect(abs(s[5] - (-0.0625)) < 1e-12)
for i: Int in 6..<engine.blockSize {
#expect(abs(s[i]) < 1e-12, "sample \(i) should be silent")
}
}
@Test func linearImpulseResponseAtHalfSample() {
// Linear interp of the same impulse: 0.5 at the two integer taps bracketing
// the 3.5-sample read (frames 3 and 4).
let dl: DelayLine = DelayLine(input: Impulse.Audio(freq: 1.0),
maxDelay: 16.0 / sr, delay: 3.5 / sr,
sampleRate: sr, interpolation: .linear)
let g: SynthGraph = makeSynthGraph(engine: engine)
let s: [Double] = dl.calculatedSamples(graph: g, count: engine.blockSize)[0]
#expect(abs(s[3] - 0.5) < 1e-12)
#expect(abs(s[4] - 0.5) < 1e-12)
#expect(abs(s[2]) < 1e-12)
#expect(abs(s[5]) < 1e-12)
}
@Test func cubicReproducesLinearRampExactly() throws {
// Catmull-Rom is exact on collinear data, so a unit-per-sample ramp delayed
// by a fractional amount yields the same output under .cubic and .linear.
let rampCubic: Phasor.Audio = Phasor.Audio(rate: 1.0, start: 0.0, end: 1.0e9)
let dlCubic: DelayLine = DelayLine(input: rampCubic, maxDelay: 16.0 / sr,
delay: 4.25 / sr, sampleRate: sr, interpolation: .cubic)
let sCubic: [Double] = collectBlocks(dlCubic, engine: engine, blocks: 4)
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
try engine2.start()
let rampLinear: Phasor.Audio = Phasor.Audio(rate: 1.0, start: 0.0, end: 1.0e9)
let dlLinear: DelayLine = DelayLine(input: rampLinear, maxDelay: 16.0 / sr,
delay: 4.25 / sr, sampleRate: sr, interpolation: .linear)
let sLinear: [Double] = collectBlocks(dlLinear, engine: engine2, blocks: 4)
// Skip the warm-up edge (first ~6 samples) where the ramp history is incomplete.
for i: Int in 8..<sCubic.count {
#expect(abs(sCubic[i] - sLinear[i]) < 1e-9, "cubic vs linear differ at \(i)")
}
}
@Test func cubicFiniteUnderNoise() {
let dl: DelayLine = DelayLine(input: WhiteNoise.Audio(), maxDelay: 0.05,
delay: 0.013, sampleRate: sr, interpolation: .cubic)
let s: [Double] = collectBlocks(dl, engine: engine, blocks: 20)
#expect(s.allSatisfy { $0.isFinite })
}
}