130 lines
4.4 KiB
Swift
130 lines
4.4 KiB
Swift
// SpectrumCaptureTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("SpectrumCapture")
|
|
final class SpectrumCaptureTests {
|
|
|
|
// MARK: - Init
|
|
|
|
@Test func initBinCount() {
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: 1024)
|
|
#expect(capture.fftSize == 1024)
|
|
#expect(capture.binCount == 513) // 1024/2 + 1
|
|
}
|
|
|
|
@Test func initBinCountDefaultFFTSize() {
|
|
let capture: SpectrumCapture = SpectrumCapture()
|
|
#expect(capture.fftSize == 2048)
|
|
#expect(capture.binCount == 1025)
|
|
}
|
|
|
|
// MARK: - Initial snapshot
|
|
|
|
@Test func snapshotInitiallyZero() {
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: 512)
|
|
let snap: [Float] = capture.snapshot()
|
|
#expect(snap.count == 257) // 512/2 + 1
|
|
#expect(snap.allSatisfy { $0 == 0 })
|
|
}
|
|
|
|
// MARK: - Round-trip through process()
|
|
|
|
@Test func processUpdatesComplexBins() {
|
|
let fftSize: Int = 512
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
spectral[magnitude: 1] = 0.5
|
|
spectral[magnitude: 10] = 1.0
|
|
spectral[magnitude: 50] = 0.25
|
|
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap: [Float] = capture.snapshot()
|
|
|
|
#expect(abs(snap[1] - 0.5) < 1e-6)
|
|
#expect(abs(snap[10] - 1.0) < 1e-6)
|
|
#expect(abs(snap[50] - 0.25) < 1e-6)
|
|
}
|
|
|
|
@Test func processDCAndNyquistStoredAsAbsoluteValue() {
|
|
let fftSize: Int = 512
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
spectral.dcValue = -0.8 // negative — should be stored as 0.8
|
|
spectral.nyquistValue = 0.6
|
|
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap: [Float] = capture.snapshot()
|
|
|
|
#expect(abs(snap[0] - 0.8) < 1e-6) // bin 0 = |DC|
|
|
#expect(abs(snap[fftSize / 2] - 0.6) < 1e-6) // bin halfSize = |Nyquist|
|
|
}
|
|
|
|
@Test func negativeDCStoredAbsolute() {
|
|
let fftSize: Int = 256
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
spectral.dcValue = -1.0
|
|
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap: [Float] = capture.snapshot()
|
|
#expect(abs(snap[0] - 1.0) < 1e-6)
|
|
}
|
|
|
|
// MARK: - Multi-hop updates
|
|
|
|
@Test func consecutiveHopsUpdateSnapshot() {
|
|
let fftSize: Int = 256
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
spectral[magnitude: 5] = 0.3
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap1: [Float] = capture.snapshot()
|
|
#expect(abs(snap1[5] - 0.3) < 1e-6)
|
|
|
|
spectral[magnitude: 5] = 0.7
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap2: [Float] = capture.snapshot()
|
|
#expect(abs(snap2[5] - 0.7) < 1e-6)
|
|
}
|
|
|
|
@Test func snapshotCountMatchesBinCount() {
|
|
let fftSize: Int = 1024
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
let snap: [Float] = capture.snapshot()
|
|
#expect(snap.count == capture.binCount)
|
|
}
|
|
|
|
@Test func manyHopsAllBinsUpdated() {
|
|
let fftSize: Int = 256
|
|
let halfSize: Int = fftSize / 2
|
|
let capture: SpectrumCapture = SpectrumCapture(fftSize: fftSize)
|
|
let spectral: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
|
|
let expected: Float = Float(0.42)
|
|
for k: Int in 1..<halfSize {
|
|
spectral[magnitude: k] = expected
|
|
}
|
|
// Drive 10 hops to stress the ping-pong
|
|
for _ in 0..<10 {
|
|
capture.process(spectral: spectral, engine: makeTestEngine())
|
|
}
|
|
let snap: [Float] = capture.snapshot()
|
|
for k: Int in 1..<halfSize {
|
|
#expect(abs(snap[k] - expected) < 1e-6)
|
|
}
|
|
}
|
|
}
|