1.0.0 release
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
// 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..<phases.count {
|
||||
buf[magnitude: testBin] = 1.0
|
||||
buf[phase: testBin] = phases[hop]
|
||||
filt.process(spectral: buf, engine: makeTestEngine())
|
||||
}
|
||||
|
||||
#expect(buf[magnitude: testBin] == 0.0,
|
||||
"Unstable bin should be zeroed, got mag=\(buf[magnitude: testBin])")
|
||||
}
|
||||
|
||||
// MARK: - Warmup
|
||||
|
||||
/// During warmup, magnitudes must not be modified even if metric is unstable.
|
||||
@Test func warmupPreservesMagnitudes() {
|
||||
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.001 // very strict — would zero everything if applied
|
||||
)
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
|
||||
// Run exactly numFrames hops (warmup window). Magnitude must survive even
|
||||
// with erratic phases.
|
||||
let phases: [Float] = [0.0, 1.0, -1.0, 2.0]
|
||||
for hop: Int in 0..<numFrames {
|
||||
buf[magnitude: testBin] = 1.0
|
||||
buf[phase: testBin] = phases[hop]
|
||||
filt.process(spectral: buf, engine: makeTestEngine())
|
||||
}
|
||||
|
||||
#expect(buf[magnitude: testBin] == 1.0,
|
||||
"Magnitude must survive every hop during warmup, got mag=\(buf[magnitude: testBin])")
|
||||
}
|
||||
|
||||
// MARK: - .frequency mode
|
||||
|
||||
@Test func frequencyModeStableBinSurvives() {
|
||||
let fftSize: Int = 64
|
||||
let numFrames: Int = 4
|
||||
let testBin: Int = 5
|
||||
|
||||
let filt: SpectralPartialFilter = SpectralPartialFilter(
|
||||
fftSize: fftSize,
|
||||
numFrames: numFrames,
|
||||
mode: .frequency,
|
||||
hopSize: 16,
|
||||
sampleRate: 44_100,
|
||||
threshold: 1.0 // 1 Hz tolerance
|
||||
)
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
|
||||
// For .frequency mode a stable phase-step around the bin's expected step
|
||||
// produces a stable Hz estimate. Use the centre-bin expected step exactly.
|
||||
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] == 1.0,
|
||||
"Stable bin (.frequency) should survive PartialFilter, got mag=\(buf[magnitude: testBin])")
|
||||
}
|
||||
|
||||
// MARK: - Threshold update
|
||||
|
||||
/// `threshold` is settable from any thread; the new value takes effect on the
|
||||
/// next process() call.
|
||||
@Test func thresholdUpdateLive() {
|
||||
let fftSize: Int = 64
|
||||
let filt: SpectralPartialFilter = SpectralPartialFilter(
|
||||
fftSize: fftSize,
|
||||
numFrames: 2,
|
||||
mode: .phase,
|
||||
hopSize: 16,
|
||||
sampleRate: 44_100,
|
||||
threshold: 100.0
|
||||
)
|
||||
#expect(filt.threshold == 100.0)
|
||||
filt.threshold = 5.0
|
||||
#expect(filt.threshold == 5.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user