// 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..