313 lines
12 KiB
Swift
313 lines
12 KiB
Swift
// SpectralPolarClosureTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("SpectralPolarClosure")
|
||
struct SpectralPolarClosureTests {
|
||
|
||
// MARK: - Helpers
|
||
|
||
private static func fillKnownPolar(_ buf: SpectralBuffer) {
|
||
buf.dcValue = 0.5
|
||
buf.nyquistValue = -0.25
|
||
let halfSize: Int = buf.fftSize / 2
|
||
for k: Int in 1..<halfSize {
|
||
buf[magnitude: k] = Float(k) * 0.01
|
||
buf[phase: k] = 0.3
|
||
}
|
||
}
|
||
|
||
// MARK: - Basic structural tests
|
||
|
||
@Test func identityClosureDoesNotMutate() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
Self.fillKnownPolar(buf)
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { _ in }
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
|
||
let halfSize: Int = fftSize / 2
|
||
for k: Int in 1..<halfSize {
|
||
#expect(abs(buf[magnitude: k] - Float(k) * 0.01) < 1e-6,
|
||
"magnitude of bin \(k) mutated by identity closure")
|
||
#expect(abs(buf[phase: k] - 0.3) < 1e-6,
|
||
"phase of bin \(k) mutated by identity closure")
|
||
}
|
||
#expect(abs(buf.dcValue - 0.5) < 1e-6)
|
||
#expect(abs(buf.nyquistValue - (-0.25)) < 1e-6)
|
||
}
|
||
|
||
@Test func closureReceivesCorrectBinCount() {
|
||
let fftSize: Int = 128
|
||
var observedBinCount: Int = 0
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 64, sampleRate: 44100
|
||
) { ctx in
|
||
observedBinCount = ctx.binCount
|
||
}
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
// binCount = fftSize/2 - 1
|
||
#expect(observedBinCount == fftSize / 2 - 1)
|
||
}
|
||
|
||
@Test func closureReceivesCorrectMetadata() {
|
||
let fftSize: Int = 256
|
||
let hopSize: Int = 64
|
||
let sampleRate: Double = 48000.0
|
||
var seenFFTSize: Int = 0
|
||
var seenHopSize: Int = 0
|
||
var seenSampleRate: Double = 0.0
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: hopSize, sampleRate: sampleRate
|
||
) { ctx in
|
||
seenFFTSize = ctx.fftSize
|
||
seenHopSize = ctx.hopSize
|
||
seenSampleRate = ctx.sampleRate
|
||
}
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
#expect(seenFFTSize == fftSize)
|
||
#expect(seenHopSize == hopSize)
|
||
#expect(seenSampleRate == sampleRate)
|
||
}
|
||
|
||
// MARK: - Magnitude mutation
|
||
|
||
@Test func magnitudeHalvingDropsAllBins() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
Self.fillKnownPolar(buf)
|
||
let originalMags: [Float] = (1..<(fftSize / 2)).map { k in buf[magnitude: k] }
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
for i: Int in 0..<ctx.binCount {
|
||
ctx.magnitudes[i] *= 0.5
|
||
}
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
|
||
for (i, k) in (1..<(fftSize / 2)).enumerated() {
|
||
#expect(abs(buf[magnitude: k] - originalMags[i] * 0.5) < 1e-6,
|
||
"bin \(k) magnitude not halved")
|
||
}
|
||
}
|
||
|
||
@Test func closureCanMutateDCValue() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
buf.dcValue = 2.0
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
ctx.dcValue.pointee *= 2
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
#expect(abs(buf.dcValue - 4.0) < 1e-6)
|
||
}
|
||
|
||
@Test func closureCanMutateNyquistValue() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
buf.nyquistValue = -1.0
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
ctx.nyquistValue.pointee = 3.0
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
#expect(abs(buf.nyquistValue - 3.0) < 1e-6)
|
||
}
|
||
|
||
// MARK: - Phase mutation
|
||
|
||
@Test func phaseMutationPersists() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
let halfSize: Int = fftSize / 2
|
||
for k: Int in 1..<halfSize { buf[phase: k] = 0.0 }
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
for i: Int in 0..<ctx.binCount {
|
||
ctx.phases[i] = Float.pi / 4
|
||
}
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
|
||
let expected: Float = Float.pi / 4
|
||
for k: Int in 1..<halfSize {
|
||
#expect(abs(buf[phase: k] - expected) < 1e-6,
|
||
"phase of bin \(k) not written: \(buf[phase: k])")
|
||
}
|
||
}
|
||
|
||
// MARK: - Frame index
|
||
|
||
@Test func frameIndexIncrementsAcrossHops() {
|
||
let fftSize: Int = 64
|
||
var observedIndices: [Int] = []
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
observedIndices.append(ctx.frameIndex)
|
||
}
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
for _ in 0..<5 {
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
}
|
||
#expect(observedIndices == [0, 1, 2, 3, 4])
|
||
}
|
||
|
||
// MARK: - Guard conditions
|
||
|
||
@Test func fftSizeMismatchIsBypassed() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: 128) // different fftSize
|
||
buf[magnitude: 1] = 99.0
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
for i: Int in 0..<ctx.binCount { ctx.magnitudes[i] = 0 }
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
// Buffer untouched — fftSize mismatch bypasses process.
|
||
#expect(buf[magnitude: 1] == 99.0)
|
||
}
|
||
|
||
@Test func invalidBufferIsBypassed() {
|
||
let fftSize: Int = 64
|
||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||
buf[magnitude: 4] = 5.0
|
||
buf.invalidate()
|
||
|
||
let proc: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: 32, sampleRate: 44100
|
||
) { ctx in
|
||
for i: Int in 0..<ctx.binCount { ctx.magnitudes[i] = 0 }
|
||
}
|
||
proc.process(spectral: buf, engine: makeTestEngine())
|
||
#expect(buf[magnitude: 4] == 5.0)
|
||
}
|
||
|
||
// MARK: - SpectralChain integration
|
||
|
||
/// Polar identity closure: round-trip through SpectralChain.Audio
|
||
/// should be equivalent to no-processor path within round-trip tolerance.
|
||
@Test func identityClosureRoundTripEquivalentToNoProcessor() throws {
|
||
let fftSize: Int = 64
|
||
let hopSize: Int = 32
|
||
let blockSize: Int = hopSize
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let dcInput: Double = 0.4
|
||
|
||
let chainNone: SpectralChain.Audio = SpectralChain.Audio(input: dcInput, fftSize: fftSize, hopSize: hopSize)
|
||
let identity: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: hopSize, sampleRate: engine.sampleRate
|
||
) { _ in }
|
||
let chainIdentity: SpectralChain.Audio = SpectralChain.Audio(
|
||
input: dcInput, fftSize: fftSize, hopSize: hopSize, stages: [.mono(identity)]
|
||
)
|
||
|
||
let warmUpBlocks: Int = 4
|
||
let measureBlocks: Int = 4
|
||
var sumNone: Double = 0.0
|
||
var sumId: Double = 0.0
|
||
for block: Int in 0..<(warmUpBlocks + measureBlocks) {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let outNone: [[Double]] = chainNone.calculatedSamples(graph: g, count: blockSize)
|
||
let outId: [[Double]] = chainIdentity.calculatedSamples(graph: g, count: blockSize)
|
||
if block >= warmUpBlocks {
|
||
for i: Int in 0..<blockSize {
|
||
sumNone += outNone[0][i]
|
||
sumId += outId[0][i]
|
||
}
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let n: Double = Double(measureBlocks * blockSize)
|
||
let meanNone: Double = sumNone / n
|
||
let meanId: Double = sumId / n
|
||
// Both chains should produce the same mean level; the identity closure
|
||
// must not shift amplitude. Tolerance 0.01 is tighter than DCmatch tolerance.
|
||
#expect(abs(meanNone - meanId) < 0.01,
|
||
"Identity closure shifted mean from \(meanNone) to \(meanId)")
|
||
}
|
||
|
||
/// Half-magnitude polar closure: output drops by 6 dB ±0.1 dB.
|
||
@Test func halfMagnitudeDropsSixDB() throws {
|
||
let fftSize: Int = 64
|
||
let hopSize: Int = 32
|
||
let blockSize: Int = hopSize
|
||
let sineFreq: Double = 440.0
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let osc: OscSin.Audio = OscSin.Audio(freq: sineFreq)
|
||
let chainFull: SpectralChain.Audio = SpectralChain.Audio(input: osc, fftSize: fftSize, hopSize: hopSize)
|
||
|
||
let osc2: OscSin.Audio = OscSin.Audio(freq: sineFreq)
|
||
let halver: SpectralPolarClosure = SpectralPolarClosure(
|
||
fftSize: fftSize, hopSize: hopSize, sampleRate: engine.sampleRate
|
||
) { ctx in
|
||
for i: Int in 0..<ctx.binCount { ctx.magnitudes[i] *= 0.5 }
|
||
ctx.dcValue.pointee *= 0.5
|
||
ctx.nyquistValue.pointee *= 0.5
|
||
}
|
||
let chainHalf: SpectralChain.Audio = SpectralChain.Audio(
|
||
input: osc2, fftSize: fftSize, hopSize: hopSize, stages: [.mono(halver)]
|
||
)
|
||
|
||
let warmUpBlocks: Int = 4
|
||
let measureBlocks: Int = 8
|
||
var rmsFull: Double = 0.0
|
||
var rmsHalf: Double = 0.0
|
||
for block: Int in 0..<(warmUpBlocks + measureBlocks) {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
let outFull: [[Double]] = chainFull.calculatedSamples(graph: g, count: blockSize)
|
||
let outHalf: [[Double]] = chainHalf.calculatedSamples(graph: g, count: blockSize)
|
||
if block >= warmUpBlocks {
|
||
for i: Int in 0..<blockSize {
|
||
let f: Double = outFull[0][i]
|
||
let h: Double = outHalf[0][i]
|
||
rmsFull += f * f
|
||
rmsHalf += h * h
|
||
}
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let n: Double = Double(measureBlocks * blockSize)
|
||
let rmsFullVal: Double = (rmsFull / n).squareRoot()
|
||
let rmsHalfVal: Double = (rmsHalf / n).squareRoot()
|
||
|
||
guard rmsFullVal > 1e-6 else { return } // skip if no signal
|
||
let dbDrop: Double = 20 * log10(rmsHalfVal / rmsFullVal)
|
||
// Expect ≈ −6 dB; allow ±1 dB for round-trip tolerance.
|
||
#expect(abs(dbDrop - (-6.0)) < 1.0,
|
||
"Expected ≈ -6 dB drop, got \(dbDrop) dB")
|
||
}
|
||
}
|