Files
Lyrebird/Tests/LyrebirdPortableTests/SpectralGoldenValueTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

146 lines
6.6 KiB
Swift

// SpectralGoldenValueTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@_spi(LyrebirdExtensions) import LyrebirdSynth
// Golden values for the real-FFT path through `SpectralChain` — the only place
// the portable `vDSP_fft_zrip` / `vDSP_ctoz` / `vDSP_ztoc` replacements are
// exercised as *audio* rather than as isolated primitives.
//
// Two directions are pinned:
// • forward — the analysis magnitude spectrum of a bin-centred sine, which
// depends on the split-complex packing (DC and Nyquist share one complex
// slot) and on vDSP's factor-of-two real-FFT scaling. Get either wrong and
// the peak lands in the wrong bin or at the wrong height.
// • inverse — the resynthesised waveform after windowed overlap-add, which
// only comes back out as a clean unit-amplitude sine if forward and inverse
// agree on both conventions.
//
// Captured on macOS (arm64, vDSP-backed). Internals here are `Float`, so the
// tolerances are looser than the double-precision filter goldens.
@MainActor
@Suite("Portable DSP golden values — spectral")
struct SpectralGoldenValueTests {
private static let fftSize: Int = 64
private static let hopSize: Int = 32
private static let sampleRate: Double = 44100
/// Sine placed exactly on bin 8 so the analysis peak is unambiguous.
private static let analysisBin: Int = 8
private static var binFrequency: Double {
sampleRate * Double(analysisBin) / Double(fftSize)
}
/// Blocks needed to fill the analysis ring and reach steady-state overlap-add.
private static let warmUpBlocks: Int = 8
@Test func analysisMagnitudeSpectrumOfBinCentredSine() throws {
let engine: SynthesisEngine = makePortableTestEngine(
blockSize: Self.hopSize,
sampleRate: Self.sampleRate
)
try engine.start()
defer { engine.stop() }
let chain: SpectralChain.Audio = SpectralChain.Audio(
input: OscSin.Audio(freq: Self.binFrequency),
fftSize: Self.fftSize,
hopSize: Self.hopSize,
analyzeOnly: true
)
for _ in 0..<Self.warmUpBlocks {
let g: SynthGraph = SynthGraph(engine: engine)
_ = chain.calculatedSamples(graph: g, count: Self.hopSize)
engine.advanceSegmentSerial()
}
var magnitudes: [Double] = []
magnitudes.reserveCapacity(Self.fftSize / 2 + 1)
for bin: Int in 0...(Self.fftSize / 2) {
magnitudes.append(Double(chain.spectralBuffer.magnitude(at: bin)))
}
// Bins 0 and 32 (DC / Nyquist) are float-epsilon noise, not signal.
let golden: [Double] = [
+4.768371582031250e-07, +8.266706019639969e-02, +1.825404316186905e-01,
+3.267964720726013e-01, +5.753288269042969e-01, +1.103066682815552e+00,
+2.663390159606934e+00, +1.353493118286133e+01, +4.078461074829102e+01,
+1.354466724395752e+01, +2.683343172073364e+00, +1.134257316589355e+00,
+6.194505095481873e-01, +3.864569664001465e-01, +2.616786956787109e-01,
+1.872759908437729e-01, +1.394553184509277e-01, +1.069587841629982e-01,
+8.389673382043839e-02, +6.694645434617996e-02, +5.412036180496216e-02,
+4.416543245315552e-02, +3.626501560211182e-02, +2.986478805541992e-02,
+2.457809448242188e-02, +2.012777328491211e-02, +1.631212420761585e-02,
+1.297473907470703e-02, +9.998649358749390e-03, +7.285922765731812e-03,
+4.758469760417938e-03, +2.349850023165345e-03, +4.768371582031250e-07,
]
#expect(golden.count == Self.fftSize / 2 + 1)
// Float-precision magnitudes spanning ~5 decades: a small absolute floor
// for the near-zero bins plus a relative term for the peak.
expectMatchesGolden(
magnitudes, golden,
absolute: 1e-5, relative: 1e-4,
"SpectralChain analysis spectrum, sine on bin \(Self.analysisBin)"
)
// The peak must be bin 8 — a packing error would move it even if the
// per-bin tolerances above somehow passed.
var peakBin: Int = 0
for bin: Int in 1..<magnitudes.count where magnitudes[bin] > magnitudes[peakBin] {
peakBin = bin
}
#expect(peakBin == Self.analysisBin, "peak landed in bin \(peakBin)")
}
@Test func resynthesisReconstructsInputSine() throws {
let engine: SynthesisEngine = makePortableTestEngine(
blockSize: Self.hopSize,
sampleRate: Self.sampleRate
)
try engine.start()
defer { engine.stop() }
let chain: SpectralChain.Audio = SpectralChain.Audio(
input: OscSin.Audio(freq: Self.binFrequency),
fftSize: Self.fftSize,
hopSize: Self.hopSize
)
var resynthesised: [Double] = []
for _ in 0..<Self.warmUpBlocks {
let g: SynthGraph = SynthGraph(engine: engine)
let out: [[Double]] = chain.calculatedSamples(graph: g, count: Self.hopSize)
resynthesised += out[0].prefix(Self.hopSize)
engine.advanceSegmentSerial()
}
let steadyState: [Double] = Array(resynthesised.suffix(Self.hopSize))
// Unit-amplitude sine, 8 cycles per 64 samples: 0, √2/2, 1, √2/2, 0, …
let golden: [Double] = [
-3.450122409986989e-09, +7.071065306663513e-01, +9.999998807907104e-01,
+7.071067094802856e-01, +1.732368737350498e-08, -7.071065902709961e-01,
-9.999997615814209e-01, -7.071067690849304e-01, -2.017156930378405e-08,
+7.071066498756409e-01, +9.999998211860657e-01, +7.071067094802856e-01,
+1.311730279951462e-08, -7.071066498756409e-01, -1.000000119209290e+00,
-7.071068286895752e-01, +6.974591570951816e-09, +7.071067690849304e-01,
+1.000000119209290e+00, +7.071067094802856e-01, -1.933644000473578e-08,
-7.071067094802856e-01, -9.999998807907104e-01, -7.071067690849304e-01,
+1.925623216436634e-08, +7.071065902709961e-01, +9.999997615814209e-01,
+7.071066498756409e-01, -6.058438195566396e-09, -7.071065902709961e-01,
-9.999998807907104e-01, -7.071066498756409e-01,
]
#expect(golden.count == Self.hopSize)
// The round trip passes through `Float` twice; 1e-5 is ~5 significant
// digits, far tighter than any scaling or windowing error would survive.
expectMatchesGolden(
steadyState, golden,
absolute: 1e-5,
"SpectralChain round-trip resynthesis"
)
}
}