138 lines
5.0 KiB
Swift
138 lines
5.0 KiB
Swift
// SpectralBlurTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("SpectralBlur")
|
|
struct SpectralBlurTests {
|
|
|
|
@Test func radiusZeroIsBypass() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let halfSize: Int = fftSize / 2
|
|
for k: Int in 1..<halfSize {
|
|
buf[magnitude: k] = Float(k)
|
|
buf[phase: k] = 0.6
|
|
}
|
|
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 0)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
for k: Int in 1..<halfSize {
|
|
#expect(buf[magnitude: k] == Float(k), "bin \(k) altered with r=0")
|
|
}
|
|
}
|
|
|
|
@Test func deltaPeakSmearsToPlateau() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let halfSize: Int = fftSize / 2
|
|
// Single delta at bin 16.
|
|
let peakBin: Int = 16
|
|
for k: Int in 1..<halfSize { buf[magnitude: k] = 0 }
|
|
buf[magnitude: peakBin] = 1.0
|
|
|
|
let r: Int = 3
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: r)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
// The window of width 2r+1 = 7 around bin 16 should be a plateau of
|
|
// (1.0 / 7) ≈ 0.142857 — but the delta lives in only the centre bin,
|
|
// so each output i in [13, 19] sees the delta within its window:
|
|
// averaged value = 1 / (2r + 1) at the centre, but at edges the window
|
|
// shrinks: e.g. bin 13 has window [10, 16] = 7 bins, still includes the
|
|
// delta. Bin 19 has window [16, 22] = 7 bins, also includes delta.
|
|
// Bin 20 has window [17, 23] = 7 bins, no delta.
|
|
let expected: Float = 1.0 / 7.0
|
|
for k: Int in (peakBin - r)...(peakBin + r) {
|
|
#expect(abs(buf[magnitude: k] - expected) < 1e-5,
|
|
"bin \(k) plateau value \(buf[magnitude: k]) ≠ \(expected)")
|
|
}
|
|
for k: Int in 1..<(peakBin - r) {
|
|
#expect(buf[magnitude: k] == 0,
|
|
"bin \(k) outside window should be 0, got \(buf[magnitude: k])")
|
|
}
|
|
for k: Int in (peakBin + r + 1)..<halfSize {
|
|
#expect(buf[magnitude: k] == 0,
|
|
"bin \(k) outside window should be 0, got \(buf[magnitude: k])")
|
|
}
|
|
}
|
|
|
|
@Test func edgeBinsShrinkWindow() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let halfSize: Int = fftSize / 2
|
|
// Constant magnitude 1.0 on every bin (plus edge).
|
|
for k: Int in 1..<halfSize { buf[magnitude: k] = 1.0 }
|
|
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 5)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
// Average of constant 1.0 is still 1.0 at every output, even at edges,
|
|
// because window shrinks but values are uniform.
|
|
for k: Int in 1..<halfSize {
|
|
#expect(abs(buf[magnitude: k] - 1.0) < 1e-5)
|
|
}
|
|
}
|
|
|
|
@Test func phaseUntouched() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let halfSize: Int = fftSize / 2
|
|
for k: Int in 1..<halfSize {
|
|
buf[magnitude: k] = Float(k)
|
|
buf[phase: k] = 0.6
|
|
}
|
|
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 4)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
for k: Int in 1..<halfSize {
|
|
#expect(buf[phase: k] == 0.6, "phase on bin \(k) altered")
|
|
}
|
|
}
|
|
|
|
@Test func liveRadiusUpdate() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
let halfSize: Int = fftSize / 2
|
|
for k: Int in 1..<halfSize { buf[magnitude: k] = 0 }
|
|
buf[magnitude: 10] = 1.0
|
|
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 0)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
#expect(buf[magnitude: 10] == 1.0, "radius=0 should bypass")
|
|
|
|
// Restore peak (was preserved), set new radius.
|
|
b.radius = 2
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
// Now smeared.
|
|
#expect(abs(buf[magnitude: 10] - 1.0 / 5.0) < 1e-5)
|
|
}
|
|
|
|
@Test func radiusClampedToHalfSizeOver2() {
|
|
let fftSize: Int = 64
|
|
// halfSize = 32, max radius = 16.
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 999)
|
|
#expect(b.radius == 16)
|
|
}
|
|
|
|
@Test func invalidBufferIsBypassed() {
|
|
let fftSize: Int = 64
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
for k: Int in 1..<(fftSize / 2) { buf[magnitude: k] = 0 }
|
|
buf[magnitude: 10] = 1.0
|
|
buf.invalidate()
|
|
|
|
let b: SpectralBlur = SpectralBlur(fftSize: fftSize, radius: 4)
|
|
b.process(spectral: buf, engine: makeTestEngine())
|
|
#expect(buf[magnitude: 10] == 1.0)
|
|
}
|
|
}
|