// SpectralChainTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SpectralChain.Audio — basic shape") struct SpectralChainBasicTests { @Test func rateIsAudio() { let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 64, hopSize: 32) #expect(chain.rate == .audio) } @Test func defaultOutputIsMono() { let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 64, hopSize: 32) #expect(chain.outputChannelCount == 1) } @Test func fanOutControlsChannelCount() { let diffuser: SpectralStereoDiffuse = SpectralStereoDiffuse(amount: 0.5, seed: 1) let chain: SpectralChain.Audio = SpectralChain.Audio( input: Double(0.0), fftSize: 64, hopSize: 32, fanOut: diffuser ) #expect(chain.outputChannelCount == 2) } @Test func analyzeOnlyForcesMono() { let chain: SpectralChain.Audio = SpectralChain.Audio( input: Double(0.0), fftSize: 64, hopSize: 32, analyzeOnly: true ) #expect(chain.outputChannelCount == 1) } } @Suite("SpectralChain.Audio — analysis hop") struct SpectralChainAnalysisHopTests { @Test func dcBinDominatesForConstantInput() 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 chain: SpectralChain.Audio = SpectralChain.Audio( input: Double(1.0), fftSize: fftSize, hopSize: hopSize ) // Run 4 blocks to reach steady state (ring fully filled with DC). for _ in 0..<4 { let g: SynthGraph = makeSynthGraph(engine: engine) _ = chain.calculatedSamples(graph: g, count: blockSize) advanceBlock(engine) } let dcMag: Float = chain.spectralBuffer.magnitude(at: 0) #expect(dcMag > 0, "DC magnitude must be positive for constant input") let halfSize: Int = fftSize / 2 for bin: Int in 1.. binMag, "DC magnitude \(dcMag) should exceed bin \(bin) magnitude \(binMag)") } } @Test func analyzeOnlyOutputIsSilence() throws { let blockSize: Int = 32 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() defer { engine.stop() } let graph: SynthGraph = makeSynthGraph(engine: engine) let chain: SpectralChain.Audio = SpectralChain.Audio( input: Double(1.0), fftSize: 64, hopSize: 32, analyzeOnly: true ) let out: [[Double]] = chain.calculatedSamples(graph: graph, count: blockSize) for i: Int in 0..= warmUpBlocks { for i: Int in 0..= blockSize) #expect(out[1].count >= blockSize) } } @Suite("SpectralChain.Audio — synthOnly mode") struct SpectralChainSynthOnlyTests { /// Build a frame buffer with a constant non-zero DC magnitude on every /// frame so playback through synthOnly produces a non-silent signal. private func makeDCFrameBuffer(fftSize: Int, hopSize: Int, frames: Int) -> SpectralFrameBuffer { let fb: SpectralFrameBuffer = SpectralFrameBuffer(fftSize: fftSize, hopSize: hopSize, frameCount: frames, sampleRate: 48_000) let scratch: SpectralBuffer = SpectralBuffer(fftSize: fftSize) scratch.dcValue = 100.0 // strong DC tone for f: Int in 0.. 0, "Processor must be dispatched on the aliased source spectralBuffer") #expect(counter.lastSeenDC == 100.0, "Processor should see the DC the source wrote into the shared buffer") #expect(chain.spectralBuffer === player.spectralBuffer, "SynthOnly chain.spectralBuffer must alias spectralSource.spectralBuffer") } }