519 lines
22 KiB
Swift
519 lines
22 KiB
Swift
// FFTTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("FFT Infrastructure")
|
|
struct FFTTests {
|
|
|
|
// MARK: - SpectralBuffer — bin access
|
|
|
|
@Test func spectralBufferBinCount() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
#expect(buf.binCount == 33) // fftSize/2 + 1
|
|
}
|
|
|
|
@Test func spectralBufferMagnitudeDC() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.dcValue = 3.0
|
|
#expect(buf.magnitude(at: 0) == 3.0)
|
|
}
|
|
|
|
@Test func spectralBufferMagnitudeNegativeDC() {
|
|
// DC may be negative; magnitude returns abs.
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.dcValue = -3.0
|
|
#expect(buf.magnitude(at: 0) == 3.0)
|
|
#expect(buf.dcValue == -3.0)
|
|
}
|
|
|
|
@Test func spectralBufferMagnitudeNyquist() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.nyquistValue = 4.0
|
|
#expect(buf.magnitude(at: 32) == 4.0) // bin == fftSize/2
|
|
}
|
|
|
|
@Test func spectralBufferMagnitudeGeneralBin() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf[magnitude: 1] = 5.0
|
|
buf[phase: 1] = 0.0
|
|
#expect(abs(buf.magnitude(at: 1) - 5.0) < 1e-5)
|
|
}
|
|
|
|
@Test func spectralBufferPhaseDCIsZero() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.dcValue = 5.0
|
|
#expect(buf.phase(at: 0) == 0.0)
|
|
}
|
|
|
|
@Test func spectralBufferPhaseNyquistIsZero() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.nyquistValue = 5.0
|
|
#expect(buf.phase(at: 32) == 0.0)
|
|
}
|
|
|
|
@Test func spectralBufferPhaseGeneralBin() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
let expected: Float = Float.pi / 4 // 45 degrees
|
|
buf[phase: 1] = expected
|
|
#expect(abs(buf.phase(at: 1) - expected) < 1e-5)
|
|
}
|
|
|
|
@Test func spectralBufferSetMagnitudePreservesPhase() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
let originalPhase: Float = atan2f(4.0, 3.0) // corresponds to (3, 4i)
|
|
buf[magnitude: 2] = 5.0
|
|
buf[phase: 2] = originalPhase
|
|
buf.setMagnitude(10.0, at: 2)
|
|
#expect(abs(buf.magnitude(at: 2) - 10.0) < 1e-4)
|
|
#expect(abs(buf.phase(at: 2) - originalPhase) < 1e-4)
|
|
}
|
|
|
|
@Test func spectralBufferSetMagnitudeOnZeroBin() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64) // _mag[5] = 0, _phase[5] = 0
|
|
buf.setMagnitude(7.0, at: 5)
|
|
#expect(abs(buf.magnitude(at: 5) - 7.0) < 1e-4)
|
|
// Phase defaults to 0 when original magnitude was zero
|
|
#expect(abs(buf.phase(at: 5)) < 1e-5)
|
|
}
|
|
|
|
@Test func spectralBufferSetMagnitudePreservesSignDC() {
|
|
// setMagnitude on DC/Nyquist must preserve sign (sign encodes phase 0 or π).
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.dcValue = -3.0
|
|
buf.setMagnitude(5.0, at: 0)
|
|
#expect(buf.dcValue == -5.0, "Negative DC sign must be preserved after setMagnitude")
|
|
#expect(buf.magnitude(at: 0) == 5.0)
|
|
|
|
buf.dcValue = 3.0
|
|
buf.setMagnitude(5.0, at: 0)
|
|
#expect(buf.dcValue == 5.0, "Positive DC sign must be preserved after setMagnitude")
|
|
}
|
|
|
|
@Test func spectralBufferSetMagnitudePreservesSignNyquist() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.nyquistValue = -2.0
|
|
buf.setMagnitude(4.0, at: 32) // bin == fftSize/2
|
|
#expect(buf.nyquistValue == -4.0, "Negative Nyquist sign must be preserved after setMagnitude")
|
|
#expect(buf.magnitude(at: 32) == 4.0)
|
|
}
|
|
|
|
@Test func spectralBufferIsValid() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
#expect(buf.isValid)
|
|
buf.invalidate()
|
|
#expect(!buf.isValid)
|
|
}
|
|
|
|
// MARK: - Rectangular ↔ polar round-trip
|
|
|
|
@Test func spectralBufferPolarRoundTrip() throws {
|
|
// _writeFromRectangular → _readToRectangular must recover input within float tolerance.
|
|
let fftSize: Int = 64
|
|
let halfSize: Int = fftSize / 2
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
var inRealp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
var inImagp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
inRealp[0] = 3.0; inImagp[0] = -2.0 // DC and Nyquist (signed)
|
|
inRealp[1] = 3.0; inImagp[1] = 4.0 // bin 1: mag=5, phase=atan2(4,3)
|
|
inRealp[5] = 1.0; inImagp[5] = 0.0 // bin 5: mag=1, phase=0
|
|
inRealp[10] = 0.0; inImagp[10] = -1.0 // bin 10: mag=1, phase=-π/2
|
|
|
|
inRealp.withUnsafeBufferPointer { rp in
|
|
guard let rpBase: UnsafePointer<Float> = rp.baseAddress else { return }
|
|
inImagp.withUnsafeBufferPointer { ip in
|
|
guard let ipBase: UnsafePointer<Float> = ip.baseAddress else { return }
|
|
buf._writeFromRectangular(rpBase, imagp: ipBase)
|
|
}
|
|
}
|
|
|
|
var outRealp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
var outImagp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
outRealp.withUnsafeMutableBufferPointer { rp in
|
|
guard let rpBase: UnsafeMutablePointer<Float> = rp.baseAddress else { return }
|
|
outImagp.withUnsafeMutableBufferPointer { ip in
|
|
guard let ipBase: UnsafeMutablePointer<Float> = ip.baseAddress else { return }
|
|
buf._readToRectangular(realp: rpBase, imagp: ipBase)
|
|
}
|
|
}
|
|
|
|
let tolerance: Float = 1e-5
|
|
#expect(abs(outRealp[0] - 3.0) < tolerance, "DC round-trip")
|
|
#expect(abs(outImagp[0] - (-2.0)) < tolerance, "Nyquist round-trip")
|
|
#expect(abs(outRealp[1] - 3.0) < tolerance, "Bin 1 real round-trip")
|
|
#expect(abs(outImagp[1] - 4.0) < tolerance, "Bin 1 imag round-trip")
|
|
#expect(abs(outRealp[5] - 1.0) < tolerance, "Bin 5 real round-trip")
|
|
#expect(abs(outImagp[5] - 0.0) < tolerance, "Bin 5 imag round-trip")
|
|
#expect(abs(outRealp[10] - 0.0) < tolerance, "Bin 10 real round-trip")
|
|
#expect(abs(outImagp[10] - (-1.0)) < tolerance, "Bin 10 imag round-trip")
|
|
}
|
|
|
|
@Test func spectralBufferDCNyquistPreservedThroughConversion() {
|
|
// DC and Nyquist are signed scalars — must pass through unchanged (no abs).
|
|
let fftSize: Int = 64
|
|
let halfSize: Int = fftSize / 2
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
var inRealp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
var inImagp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
inRealp[0] = -7.5 // negative DC
|
|
inImagp[0] = 3.25 // positive Nyquist
|
|
|
|
inRealp.withUnsafeBufferPointer { rp in
|
|
guard let rpBase: UnsafePointer<Float> = rp.baseAddress else { return }
|
|
inImagp.withUnsafeBufferPointer { ip in
|
|
guard let ipBase: UnsafePointer<Float> = ip.baseAddress else { return }
|
|
buf._writeFromRectangular(rpBase, imagp: ipBase)
|
|
}
|
|
}
|
|
|
|
#expect(buf.dcValue == -7.5, "DC signed value must be preserved exactly")
|
|
#expect(buf.nyquistValue == 3.25, "Nyquist signed value must be preserved exactly")
|
|
|
|
var outRealp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
var outImagp: [Float] = [Float](repeating: 0, count: halfSize)
|
|
outRealp.withUnsafeMutableBufferPointer { rp in
|
|
guard let rpBase: UnsafeMutablePointer<Float> = rp.baseAddress else { return }
|
|
outImagp.withUnsafeMutableBufferPointer { ip in
|
|
guard let ipBase: UnsafeMutablePointer<Float> = ip.baseAddress else { return }
|
|
buf._readToRectangular(realp: rpBase, imagp: ipBase)
|
|
}
|
|
}
|
|
|
|
#expect(outRealp[0] == -7.5, "DC must survive polar round-trip unchanged")
|
|
#expect(outImagp[0] == 3.25, "Nyquist must survive polar round-trip unchanged")
|
|
}
|
|
|
|
// MARK: - SpectralMagnitudeScale
|
|
|
|
@Test func magnitudeScaleHalvesAllMagnitudes() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
buf.dcValue = 10.0 // DC (positive)
|
|
buf.nyquistValue = 5.0 // Nyquist
|
|
buf[magnitude: 1] = 10.0 // complex bin 1
|
|
|
|
let scaler: SpectralMagnitudeScale = SpectralMagnitudeScale(scale: 0.5)
|
|
scaler.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
#expect(abs(buf.magnitude(at: 0) - 5.0) < 1e-4, "DC magnitude should be halved")
|
|
#expect(abs(buf.magnitude(at: 32) - 2.5) < 1e-4, "Nyquist magnitude should be halved")
|
|
#expect(abs(buf.magnitude(at: 1) - 5.0) < 1e-4, "Bin 1 magnitude (was 10) should be halved")
|
|
}
|
|
|
|
// MARK: - SpectralFreeze
|
|
|
|
@Test func freezeInactivePassesThrough() {
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
let freeze: SpectralFreeze = SpectralFreeze(fftSize: 64)
|
|
buf.dcValue = 99.0
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // not frozen — should not modify buffer
|
|
#expect(abs(buf.dcValue - 99.0) < 1e-5)
|
|
}
|
|
|
|
@Test func freezeHoldsDCMagnitude() {
|
|
// dcValue must be captured at freeze time and replayed verbatim.
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: 64)
|
|
let freeze: SpectralFreeze = SpectralFreeze(fftSize: 64)
|
|
|
|
buf.dcValue = 42.0
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // phase-tracking hop (not frozen)
|
|
|
|
freeze.freeze() // request freeze
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // freeze activates, live frame unchanged
|
|
#expect(freeze.isFrozen)
|
|
|
|
buf.dcValue = 0.0 // simulate new FFT hop overwriting DC
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // frozen hop: DC must be restored
|
|
#expect(abs(buf.dcValue - 42.0) < 1e-5,
|
|
"Frozen DC should be replayed unchanged")
|
|
}
|
|
|
|
@Test func freezeHoldsBinMagnitude() {
|
|
// Complex bin magnitude must stay constant each frozen hop regardless of phase.
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let freeze: SpectralFreeze = SpectralFreeze(fftSize: fftSize)
|
|
|
|
// Establish mag=5 at bin 1 (matches classic 3-4-5 right triangle).
|
|
buf[magnitude: 1] = 5.0
|
|
buf[phase: 1] = atan2f(4.0, 3.0)
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // phase-tracking: prevPhase[1] = atan2(4,3)
|
|
|
|
freeze.freeze()
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // freeze activates with frozenMag[1] = 5.0
|
|
#expect(freeze.isFrozen)
|
|
|
|
// Several frozen hops — phase advances but magnitude stays 5.0.
|
|
for hop: Int in 1...4 {
|
|
buf[magnitude: 1] = 0.0 // overwrite to verify frozen hop restores it
|
|
buf[phase: 1] = 0.0
|
|
freeze.process(spectral: buf, engine: makeTestEngine())
|
|
let mag: Float = buf.magnitude(at: 1)
|
|
#expect(abs(mag - 5.0) < 1e-4,
|
|
"Frozen bin 1 magnitude should be 5.0 on hop \(hop), got \(mag)")
|
|
}
|
|
}
|
|
|
|
@Test func freezePhaseAdvancesByDelta() {
|
|
// After freeze, each hop the bin phase must advance by the frozen phaseDelta.
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let freeze: SpectralFreeze = SpectralFreeze(fftSize: fftSize)
|
|
|
|
// Hop 1: phase 0
|
|
buf[magnitude: 1] = 1.0; buf[phase: 1] = 0.0
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // prevPhase[1] = 0
|
|
|
|
// Hop 2: phase = π/2 → phaseDelta = π/2
|
|
buf[magnitude: 1] = 1.0; buf[phase: 1] = Float.pi / 2
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // phaseDelta[1] = π/2
|
|
|
|
freeze.freeze()
|
|
freeze.process(spectral: buf, engine: makeTestEngine()) // freeze activates; frozenPhase[1] = π/2
|
|
#expect(freeze.isFrozen)
|
|
|
|
// Each frozen hop should advance phase by π/2 and keep magnitude 1.
|
|
let expectedDeltas: [Float] = [Float.pi, 3 * Float.pi / 2, 2 * Float.pi, 5 * Float.pi / 2]
|
|
for (hop, expectedPhase) in expectedDeltas.enumerated() {
|
|
freeze.process(spectral: buf, engine: makeTestEngine())
|
|
let rawPhase: Float = buf.phase(at: 1)
|
|
// Wrap both to (-π, π] for comparison.
|
|
var p: Float = expectedPhase
|
|
while p > Float.pi { p -= 2 * Float.pi }
|
|
while p < -Float.pi { p += 2 * Float.pi }
|
|
// Circular angular distance: π and -π are the same angle.
|
|
var angularDiff: Float = abs(rawPhase - p)
|
|
if angularDiff > Float.pi { angularDiff = 2 * Float.pi - angularDiff }
|
|
#expect(angularDiff < 1e-4,
|
|
"Frozen phase after hop \(hop + 1) should be ≈ \(p), got \(rawPhase)")
|
|
}
|
|
}
|
|
|
|
@Test func freezePreservesAcrossSubsequentHops() throws {
|
|
// Engine integration: DC bin stays frozen across multiple analysis hops.
|
|
let fftSize: Int = 128
|
|
let hopSize: Int = 64
|
|
let blockSize: Int = hopSize
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let freeze: SpectralFreeze = SpectralFreeze(fftSize: fftSize)
|
|
let analysis: SpectralChain.Audio = SpectralChain.Audio(
|
|
input: Double(1.0),
|
|
fftSize: fftSize,
|
|
hopSize: hopSize,
|
|
stages: [.mono(freeze)],
|
|
analyzeOnly: true
|
|
)
|
|
|
|
// Run 2 blocks so the ring is partially filled and first hop has fired.
|
|
_ = analysis.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
_ = analysis.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
|
|
let liveDC: Float = analysis.spectralBuffer.dcValue
|
|
#expect(liveDC != 0.0, "DC should be non-zero for constant input")
|
|
freeze.freeze() // request — RT thread captures on next hop
|
|
|
|
// Block 3: freeze activates (RT captures frozenDC ≈ liveDC from constant input).
|
|
_ = analysis.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
|
|
// Block 4: full frozen hop; DC must equal the captured value.
|
|
_ = analysis.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
|
|
#expect(abs(analysis.spectralBuffer.dcValue - liveDC) < 0.1,
|
|
"Frozen DC should be close to live DC for constant-input signal")
|
|
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - SpectralChain.Audio — analyze-only path
|
|
|
|
@Test func analyzeOnlyChainRateIsAudio() {
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 64, hopSize: 32,
|
|
analyzeOnly: true)
|
|
#expect(chain.rate == .audio)
|
|
}
|
|
|
|
@Test func analyzeOnlyChainOutputIsSilence() throws {
|
|
let blockSize: Int = 32
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
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..<blockSize {
|
|
#expect(out[0][i] == 0.0, "analyzeOnly chain should output silence at sample \(i)")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func chainWritesSpectralBufferAfterHop() throws {
|
|
// hopSize == blockSize: hop fires once per block.
|
|
let fftSize: Int = 64
|
|
let hopSize: Int = 32
|
|
let blockSize: Int = hopSize
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0),
|
|
fftSize: fftSize, hopSize: hopSize,
|
|
analyzeOnly: true)
|
|
|
|
// After one block the first hop fires — DC must be non-zero.
|
|
_ = chain.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(chain.spectralBuffer.dcValue != 0.0,
|
|
"DC bin must be non-zero after the first hop for constant input")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func chainDCBinDominatesForConstantInput() throws {
|
|
// After several hops the DC bin should clearly dominate all other bins.
|
|
let fftSize: Int = 128
|
|
let hopSize: Int = 64
|
|
let blockSize: Int = hopSize
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0),
|
|
fftSize: fftSize, hopSize: hopSize,
|
|
analyzeOnly: true)
|
|
|
|
// 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")
|
|
|
|
// DC should be larger than any other bin magnitude.
|
|
let halfSize: Int = fftSize / 2
|
|
for bin: Int in 1..<halfSize {
|
|
let binMag: Float = chain.spectralBuffer.magnitude(at: bin)
|
|
#expect(dcMag > binMag,
|
|
"DC magnitude \(dcMag) should exceed bin \(bin) magnitude \(binMag)")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - SpectralChain.Audio — round-trip
|
|
|
|
@Test func chainRateIsAudio() {
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(0.0), fftSize: 128, hopSize: 64)
|
|
#expect(chain.rate == .audio)
|
|
}
|
|
|
|
@Test func chainFirstBlockIsSilence() throws {
|
|
// One-hop latency: block 0 emits silence before any IFFT result is
|
|
// ready to read.
|
|
let fftSize: Int = 128
|
|
let hopSize: Int = 64
|
|
let blockSize: Int = hopSize
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: Double(1.0),
|
|
fftSize: fftSize, hopSize: hopSize)
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[0][i] == 0.0, "SpectralChain.Audio block 0 should be silent")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func chainRoundTripDCSignalReachesUnityGain() throws {
|
|
// 50% overlap Hanning OLA — round-trip mean should equal input.
|
|
let fftSize: Int = 128
|
|
let hopSize: Int = 64 // 50% overlap
|
|
let blockSize: Int = hopSize
|
|
let dcInput: Double = 0.5
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: dcInput,
|
|
fftSize: fftSize, hopSize: hopSize)
|
|
|
|
let warmUpBlocks: Int = 4
|
|
let measureBlocks: Int = 4
|
|
var steadyStateSum: Double = 0
|
|
var steadyStateCount: Int = 0
|
|
|
|
for block: Int in 0..<(warmUpBlocks + measureBlocks) {
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
|
if block >= warmUpBlocks {
|
|
for i: Int in 0..<blockSize {
|
|
steadyStateSum += out[0][i]
|
|
steadyStateCount += 1
|
|
}
|
|
}
|
|
advanceBlock(engine)
|
|
}
|
|
|
|
let mean: Double = steadyStateSum / Double(steadyStateCount)
|
|
#expect(abs(mean - dcInput) < 0.05,
|
|
"OLA round-trip mean \(mean) should be ≈ \(dcInput) (Hanning 50% OLA = unity gain)")
|
|
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func chainDeterministicSampleOutput() throws {
|
|
// Deterministic per-sample check: DC input 1.0 → round-trip output ≈ 1.0 per sample.
|
|
//
|
|
// Uses 75% overlap (hopSize = fftSize/4). At 75% overlap, Σ Hanning²[n-kH] = 1.5
|
|
// (constant per sample), satisfying the WOLA COLA condition.
|
|
let fftSize: Int = 128
|
|
let hopSize: Int = 32 // 75% overlap — Hanning² satisfies COLA (sum = 1.5, constant)
|
|
let blockSize: Int = hopSize
|
|
let dcInput: Double = 1.0
|
|
let tolerance: Double = 0.005
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(input: dcInput,
|
|
fftSize: fftSize, hopSize: hopSize)
|
|
|
|
// Warm up: fftSize/hopSize hops to fill the ring + extra to reach OLA steady state.
|
|
let warmUpBlocks: Int = 8
|
|
for _ in 0..<warmUpBlocks {
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
_ = chain.calculatedSamples(graph: g, count: blockSize)
|
|
advanceBlock(engine)
|
|
}
|
|
|
|
// Steady-state block: every sample must be ≈ 1.0.
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = chain.calculatedSamples(graph: g, count: blockSize)
|
|
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[0][i] - dcInput) < tolerance,
|
|
"SpectralChain.Audio steady-state sample \(i): expected ≈ \(dcInput), got \(out[0][i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
}
|