// SpectralBinDelayTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SpectralBinDelay") struct SpectralBinDelayTests { /// `delayTime = 0` for every bin should make the processor a pass-through. @Test func zeroDelayPassesThrough() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let hopSize: Int = 16 let testBin: Int = 5 let delays: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) delays.fill(with: 0.0) let feedbacks: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) feedbacks.fill(with: 0.0) let bd: SpectralBinDelay = SpectralBinDelay( fftSize: fftSize, hopSize: hopSize, maxDelaySeconds: 0.05, sampleRate: 44_100, delayTimes: delays, feedbacks: feedbacks ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) let inputMag: Float = 0.7 buf[magnitude: testBin] = inputMag buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) #expect(abs(buf[magnitude: testBin] - inputMag) < 1e-4, "Zero delay should pass mag through, got \(buf[magnitude: testBin])") } /// With a fixed delay of N hops and zero feedback, the output should be the /// input from N hops earlier. @Test func fixedDelayProducesNHopLatency() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let hopSize: Int = 16 let sampleRate: Double = 44_100.0 let testBin: Int = 5 let delayHops: Int = 2 let delaySeconds: Double = Double(delayHops) * Double(hopSize) / sampleRate let delays: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) delays.fill(with: 0.0) delays.setValue(delaySeconds, at: testBin) let feedbacks: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) feedbacks.fill(with: 0.0) let bd: SpectralBinDelay = SpectralBinDelay( fftSize: fftSize, hopSize: hopSize, maxDelaySeconds: 0.05, sampleRate: sampleRate, delayTimes: delays, feedbacks: feedbacks ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) let inputMags: [Float] = [1.0, 2.0, 3.0, 4.0] var outputMags: [Float] = [] for mag: Float in inputMags { buf[magnitude: testBin] = mag buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) outputMags.append(buf[magnitude: testBin]) } // First two hops: ring slots not yet populated → zero output. #expect(abs(outputMags[0]) < 1e-4, "Hop 0 output should be zero, got \(outputMags[0])") #expect(abs(outputMags[1]) < 1e-4, "Hop 1 output should be zero, got \(outputMags[1])") // Hops 2 and 3: should equal inputs 0 and 1. #expect(abs(outputMags[2] - inputMags[0]) < 1e-3, "Hop 2 output should equal input 0, got \(outputMags[2]) vs \(inputMags[0])") #expect(abs(outputMags[3] - inputMags[1]) < 1e-3, "Hop 3 output should equal input 1, got \(outputMags[3]) vs \(inputMags[1])") } /// Bins with `delayTimes == 0` and `feedbacks == 0` are not affected by /// other bins' settings. @Test func untouchedBinsArePassThrough() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let testBin: Int = 7 let otherBin: Int = 11 let delays: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) delays.fill(with: 0.0) delays.setValue(0.001, at: otherBin) // some delay on a different bin let feedbacks: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) feedbacks.fill(with: 0.0) feedbacks.setValue(0.5, at: otherBin) let bd: SpectralBinDelay = SpectralBinDelay( fftSize: fftSize, hopSize: 16, maxDelaySeconds: 0.05, sampleRate: 44_100, delayTimes: delays, feedbacks: feedbacks ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) buf[magnitude: testBin] = 0.5 buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) #expect(abs(buf[magnitude: testBin] - 0.5) < 1e-4, "Untouched bin should pass through") } /// With a non-zero feedback gain and a fixed N-hop delay, repeatedly /// driving the same input magnitude should produce a delayed output that /// builds up over time as the head slot accumulates the attenuated /// delayed bin on each hop. @Test func feedbackAccumulatesEnergy() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let hopSize: Int = 16 let sampleRate: Double = 44_100.0 let testBin: Int = 5 let delayHops: Int = 1 let delaySeconds: Double = Double(delayHops) * Double(hopSize) / sampleRate let feedback: Double = 0.7 let delays: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) delays.fill(with: 0.0) delays.setValue(delaySeconds, at: testBin) let feedbacks: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) feedbacks.fill(with: 0.0) feedbacks.setValue(feedback, at: testBin) let bd: SpectralBinDelay = SpectralBinDelay( fftSize: fftSize, hopSize: hopSize, maxDelaySeconds: 0.05, sampleRate: sampleRate, delayTimes: delays, feedbacks: feedbacks ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Steady input magnitude, run several hops; inspect the magnitude at the // delayed output. With feedback > 0, head slot accumulates attenuated // history each hop, so the delayed read at hop t should grow as t grows // (until it asymptotes near `input / (1 - feedback)`). let inputMag: Float = 1.0 var outputs: [Float] = [] for _ in 0..<8 { buf[magnitude: testBin] = inputMag buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) outputs.append(buf[magnitude: testBin]) } // First hop reads zero ring slot — output is zero. #expect(outputs[0] < inputMag, "First-hop output should be below input (ring not yet populated), got \(outputs[0])") // Later hops should exceed `inputMag` as feedback accumulates the // delayed magnitude into the head slot before the next read. let lateMax: Float = outputs[3...].max() ?? 0 let msg: String = "Feedback should accumulate magnitude past inputMag (\(inputMag)) over multiple hops, got max \(lateMax) in outputs \(outputs)" #expect(lateMax > inputMag, "\(msg)") } /// Live `LyrebirdBuffer.setValue` calls between hops are picked up by the /// processor on the next hop. @Test func liveDelayUpdate() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let testBin: Int = 3 let delays: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) delays.fill(with: 0.0) let feedbacks: LyrebirdBuffer = LyrebirdBuffer(count: halfSize) feedbacks.fill(with: 0.0) let bd: SpectralBinDelay = SpectralBinDelay( fftSize: fftSize, hopSize: 16, maxDelaySeconds: 0.05, sampleRate: 44_100, delayTimes: delays, feedbacks: feedbacks ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // First two hops with delay 0 — pass-through. buf[magnitude: testBin] = 0.4 buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) let firstOutput: Float = buf[magnitude: testBin] #expect(abs(firstOutput - 0.4) < 1e-4) // Update the delay buffer between hops — must not crash and must take // effect on the next hop. We can't easily measure the delayed sample // exactly without orchestrating the ring; just assert that the process // call after a live update returns a finite value and does not trap. delays.setValue(0.001, at: testBin) buf[magnitude: testBin] = 0.6 buf[phase: testBin] = 0 bd.process(spectral: buf, engine: makeTestEngine()) #expect(buf[magnitude: testBin].isFinite, "Output after live update must be finite, got \(buf[magnitude: testBin])") } }