// DelayLineAllpassModulationTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - DelayLineAllpassModulationTests /// Regression guard for the allpass fractional-delay interpolator's recursive-state /// handling. When the delay lands on an exact integer sample (`frac == 0`) the interpolator /// bypasses to the integer-delayed sample; its recursive state must record that sample as /// the previous output. Leaving it stale (freezing the pre-bypass value) clicks when the /// delay dwells on an integer and then moves off. The timeline dwells on an exact integer /// then ramps a fractional delay out, and the allpass step is compared against the same /// modulation through `.linear` interpolation, which has no recursive state and stays smooth. @MainActor @Suite("DelayLine allpass modulation") struct DelayLineAllpassModulationTests { /// Allpass-vs-linear sample-step ratio tolerated. Correct state handling keeps this /// ~1×; freezing the stale state drives it past 3× for DelayLine. let clickRatioLimit: Double = 2.0 @Test func modulatedAllpassDelayStaysClickFreeThroughIntegerDwell() throws { let timeline: [Double] = modulatedDelayDwellTimeline() let linear: [Double] = try renderModulatedDelay(delaySamples: timeline) { input, modulator, maxDelay in DelayLine(input: input, maxDelay: maxDelay, delay: modulator, sampleRate: delaySweepSampleRate, interpolation: .linear) } let allpass: [Double] = try renderModulatedDelay(delaySamples: timeline) { input, modulator, maxDelay in DelayLine(input: input, maxDelay: maxDelay, delay: modulator, sampleRate: delaySweepSampleRate, interpolation: .allpass) } #expect(allpass.allSatisfy { $0.isFinite }, "allpass output must be finite") let linearStep: Double = maxAbsDelta(linear) let allpassStep: Double = maxAbsDelta(allpass) #expect(linearStep > 0, "linear reference should have non-zero motion") #expect( allpassStep < clickRatioLimit * linearStep, "modulated allpass delay clicks at integer dwell/crossing: allpass maxΔ \(allpassStep) vs linear \(linearStep)" ) } }