// SpectralPartialFilterTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SpectralPartialFilter") struct SpectralPartialFilterTests { // MARK: - .phase mode /// A bin with constant hop-to-hop phase difference is "stable" — its mag should /// survive after the warmup window. @Test func phaseModeStableBinSurvives() { let fftSize: Int = 64 let numFrames: Int = 4 let testBin: Int = 5 let stablePhaseStep: Float = 0.5 let filt: SpectralPartialFilter = SpectralPartialFilter( fftSize: fftSize, numFrames: numFrames, mode: .phase, hopSize: 16, sampleRate: 44_100, threshold: 0.05 ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Drive the bin with a constant phase increment for numFrames + 1 hops. var phase: Float = 0 for _ in 0...numFrames { phase += stablePhaseStep // Wrap to (-π, π]. if phase > Float.pi { phase -= 2 * Float.pi } buf[magnitude: testBin] = 1.0 buf[phase: testBin] = phase filt.process(spectral: buf, engine: makeTestEngine()) } // After warmup the magnitude should still be 1.0 (stable bin survives). #expect(buf[magnitude: testBin] == 1.0, "Stable bin should survive PartialFilter, got mag=\(buf[magnitude: testBin])") } /// A bin with random phase has noisy hop-to-hop phase difference — its mag /// should be zeroed after warmup. @Test func phaseModeUnstableBinZeroed() { let fftSize: Int = 64 let numFrames: Int = 4 let testBin: Int = 5 let filt: SpectralPartialFilter = SpectralPartialFilter( fftSize: fftSize, numFrames: numFrames, mode: .phase, hopSize: 16, sampleRate: 44_100, threshold: 0.05 ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Pseudo-random but deterministic phases — large hop-to-hop swings. let phases: [Float] = [0.0, 1.5, -2.5, 0.7, -1.2, 2.8] for hop: Int in 0..