// SpectralAutoPullTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Verifies that the analysis-form constructors on spectral consumer /// generators (`SpectralCentroid.Control`, etc.) drive the paired /// `SpectralChain.Audio` evaluation themselves — callers do not need to add /// the chain via `addOutputGenerator(...)` or to manually call /// `chain.calculatedSamples(...)` each block. @Suite("Spectral consumer auto-pull") struct SpectralAutoPullTests { @Test func spectralChainStandaloneProducesAudio() throws { let fftSize: Int = 128 let hopSize: Int = 64 let blockSize: Int = hopSize let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() defer { engine.stop() } // Constant DC input → chain populates the DC bin → IFFT bank // resynthesises a constant scaled by the WOLA normalisation factor. let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0), fftSize: fftSize, hopSize: hopSize) var anyNonZero: Bool = false for _ in 0..<8 { let g: SynthGraph = makeSynthGraph(engine: engine) let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize) for s: Double in out[0].prefix(blockSize) where s != 0 { anyNonZero = true break } advanceBlock(engine) } #expect(anyNonZero, "SpectralChain.Audio should produce non-zero output for non-zero DC input") } @Test func spectralCentroidAutoPullsAnalysis() throws { let fftSize: Int = 128 let hopSize: Int = 64 let blockSize: Int = hopSize let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() defer { engine.stop() } let analysis: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true) let centroid: SpectralCentroid.Control = SpectralCentroid.Control(analysis: analysis, sampleRate: 44_100) // Run several blocks; centroid should track the DC bin (returns ≈ 0 Hz // since DC is the dominant bin). The exact value isn't important — what // matters is that the analysis got pulled (otherwise centroid would // never see any spectral data). We assert centroid finishes finite. for _ in 0..<8 { let g: SynthGraph = makeSynthGraph(engine: engine) _ = centroid.calculatedSamples(graph: g, count: blockSize) advanceBlock(engine) } #expect(centroid.previousOutputValue.isFinite, "Centroid should produce a finite value via auto-pull, got \(centroid.previousOutputValue)") } @Test func spectralFluxAutoPullsAnalysis() throws { let fftSize: Int = 128 let hopSize: Int = 64 let blockSize: Int = hopSize let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() defer { engine.stop() } let analysis: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true) let flux: SpectralFlux.Control = SpectralFlux.Control(analysis: analysis) for _ in 0..<8 { let g: SynthGraph = makeSynthGraph(engine: engine) _ = flux.calculatedSamples(graph: g, count: blockSize) advanceBlock(engine) } // Flux on a steady DC signal should be ≈ 0 after the first block (no // spectral change). It should NOT be NaN/inf. #expect(flux.previousOutputValue.isFinite, "Flux should produce a finite value via auto-pull, got \(flux.previousOutputValue)") } }