129 lines
4.3 KiB
Swift
129 lines
4.3 KiB
Swift
// SpectralNoiseFilterTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("SpectralNoiseFilter")
|
|
struct SpectralNoiseFilterTests {
|
|
|
|
@Test func phaseModeStableBinZeroed() {
|
|
let fftSize: Int = 64
|
|
let numFrames: Int = 4
|
|
let testBin: Int = 5
|
|
let stablePhaseStep: Float = 0.5
|
|
|
|
let filt: SpectralNoiseFilter = SpectralNoiseFilter(
|
|
fftSize: fftSize,
|
|
numFrames: numFrames,
|
|
mode: .phase,
|
|
hopSize: 16,
|
|
sampleRate: 44_100,
|
|
threshold: 0.05
|
|
)
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
var phase: Float = 0
|
|
for _ in 0...numFrames {
|
|
phase += stablePhaseStep
|
|
if phase > Float.pi { phase -= 2 * Float.pi }
|
|
buf[magnitude: testBin] = 1.0
|
|
buf[phase: testBin] = phase
|
|
filt.process(spectral: buf, engine: makeTestEngine())
|
|
}
|
|
|
|
#expect(buf[magnitude: testBin] == 0.0,
|
|
"Stable bin should be zeroed by NoiseFilter, got mag=\(buf[magnitude: testBin])")
|
|
}
|
|
|
|
@Test func phaseModeUnstableBinSurvives() {
|
|
let fftSize: Int = 64
|
|
let numFrames: Int = 4
|
|
let testBin: Int = 5
|
|
|
|
let filt: SpectralNoiseFilter = SpectralNoiseFilter(
|
|
fftSize: fftSize,
|
|
numFrames: numFrames,
|
|
mode: .phase,
|
|
hopSize: 16,
|
|
sampleRate: 44_100,
|
|
threshold: 0.05
|
|
)
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
let phases: [Float] = [0.0, 1.5, -2.5, 0.7, -1.2, 2.8]
|
|
for hop: Int in 0..<phases.count {
|
|
buf[magnitude: testBin] = 1.0
|
|
buf[phase: testBin] = phases[hop]
|
|
filt.process(spectral: buf, engine: makeTestEngine())
|
|
}
|
|
|
|
#expect(buf[magnitude: testBin] == 1.0,
|
|
"Unstable (noisy) bin should survive NoiseFilter, got mag=\(buf[magnitude: testBin])")
|
|
}
|
|
|
|
@Test func frequencyModeStableBinZeroed() {
|
|
let fftSize: Int = 64
|
|
let numFrames: Int = 4
|
|
let testBin: Int = 5
|
|
|
|
let filt: SpectralNoiseFilter = SpectralNoiseFilter(
|
|
fftSize: fftSize,
|
|
numFrames: numFrames,
|
|
mode: .frequency,
|
|
hopSize: 16,
|
|
sampleRate: 44_100,
|
|
threshold: 1.0 // 1 Hz tolerance
|
|
)
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
// Same expected-step setup as PartialFilter's frequency-mode test —
|
|
// produces a stable Hz estimate, which NoiseFilter should zero.
|
|
let twoPi: Float = 2 * Float.pi
|
|
let expectedStep: Float = twoPi * 16.0 / Float(fftSize) * Float(testBin)
|
|
|
|
var phase: Float = 0
|
|
for _ in 0...numFrames {
|
|
phase += expectedStep
|
|
phase = phase.remainder(dividingBy: twoPi)
|
|
buf[magnitude: testBin] = 1.0
|
|
buf[phase: testBin] = phase
|
|
filt.process(spectral: buf, engine: makeTestEngine())
|
|
}
|
|
|
|
#expect(buf[magnitude: testBin] == 0.0,
|
|
"Stable bin (.frequency) should be zeroed by NoiseFilter, got mag=\(buf[magnitude: testBin])")
|
|
}
|
|
|
|
@Test func warmupPreservesMagnitudes() {
|
|
let fftSize: Int = 64
|
|
let numFrames: Int = 4
|
|
let testBin: Int = 5
|
|
|
|
let filt: SpectralNoiseFilter = SpectralNoiseFilter(
|
|
fftSize: fftSize,
|
|
numFrames: numFrames,
|
|
mode: .phase,
|
|
hopSize: 16,
|
|
sampleRate: 44_100,
|
|
threshold: 100.0 // very loose — would zero everything if applied
|
|
)
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
// Run exactly numFrames hops with constant phase step (stable). Without
|
|
// warmup guard, NoiseFilter would zero this. With warmup, it survives.
|
|
for _ in 0..<numFrames {
|
|
buf[magnitude: testBin] = 1.0
|
|
buf[phase: testBin] = 0.5
|
|
filt.process(spectral: buf, engine: makeTestEngine())
|
|
}
|
|
|
|
#expect(buf[magnitude: testBin] == 1.0,
|
|
"Magnitude must survive every hop during warmup, got mag=\(buf[magnitude: testBin])")
|
|
}
|
|
}
|