Files
2026-06-21 11:45:58 -07:00

106 lines
3.7 KiB
Swift

// SpectralConvolveTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("SpectralConvolve")
struct SpectralConvolveTests {
private static func angularDelta(_ a: Float, _ b: Float) -> Float {
let twoPi: Float = 2 * Float.pi
var d: Float = a - b
if d > Float.pi { d -= twoPi }
else if d < -Float.pi { d += twoPi }
return d
}
@Test func magnitudesMultiplyPhasesAddOnSingleBin() {
let fftSize: Int = 32
let live: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let other: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
live[magnitude: k] = 0
live[phase: k] = 0
other[magnitude: k] = 0
other[phase: k] = 0
}
let testBin: Int = 5
live[magnitude: testBin] = 2.0
live[phase: testBin] = 0.7
other[magnitude: testBin] = 3.0
other[phase: testBin] = 1.2
let c: SpectralConvolve = SpectralConvolve(spectralBuffer: live, other: other)
c.process(spectral: live, engine: makeTestEngine())
#expect(abs(live[magnitude: testBin] - 6.0) < 1e-5,
"mag product: \(live[magnitude: testBin]) ≠ 6.0")
let dPhase: Float = Self.angularDelta(live[phase: testBin], 1.9) // 0.7 + 1.2
#expect(abs(dPhase) < 1e-5,
"phase sum: residual \(dPhase)")
}
@Test func dcAndNyquistMultiplyAsSignedScalars() {
let fftSize: Int = 32
let live: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let other: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
live.dcValue = 2.0
live.nyquistValue = -1.0
other.dcValue = -3.0
other.nyquistValue = 4.0
let c: SpectralConvolve = SpectralConvolve(spectralBuffer: live, other: other)
c.process(spectral: live, engine: makeTestEngine())
#expect(live.dcValue == -6.0)
#expect(live.nyquistValue == -4.0)
}
@Test func zeroOtherProducesSilence() {
let fftSize: Int = 32
let live: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let other: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
live[magnitude: k] = Float(k)
live[phase: k] = 0.5
}
// other already initialised to all zeros.
let c: SpectralConvolve = SpectralConvolve(spectralBuffer: live, other: other)
c.process(spectral: live, engine: makeTestEngine())
for k: Int in 1..<halfSize {
#expect(live[magnitude: k] == 0.0, "bin \(k) not silenced")
}
}
@Test func eitherBufferInvalidClearsLive() {
let fftSize: Int = 32
let live: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let other: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
live[magnitude: k] = Float(k)
other[magnitude: k] = 1.0
}
live.dcValue = 5
live.nyquistValue = -3
let c: SpectralConvolve = SpectralConvolve(spectralBuffer: live, other: other)
other.invalidate()
c.process(spectral: live, engine: makeTestEngine())
#expect(live.dcValue == 0.0)
#expect(live.nyquistValue == 0.0)
for k: Int in 1..<halfSize {
#expect(live[magnitude: k] == 0.0)
}
}
}