Files
Lyrebird/Tests/LyrebirdTests/SpectralGateTests.swift
T
2026-06-01 18:27:40 -07:00

145 lines
5.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SpectralGateTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("SpectralGate")
struct SpectralGateTests {
private static func fillRamp(_ buf: SpectralBuffer) {
// mag k = k * 0.1, phase k = 0.7
buf.dcValue = 0.05
buf.nyquistValue = -0.3
let halfSize: Int = buf.fftSize / 2
for k: Int in 1..<halfSize {
buf[magnitude: k] = Float(k) * 0.1
buf[phase: k] = 0.7
}
}
@Test func floorZeroHardGatesBelowThreshold() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 0.5, floor: 0)
g.process(spectral: buf, engine: makeTestEngine())
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
let expected: Float = (Float(k) * 0.1) < 0.5 ? 0.0 : Float(k) * 0.1
#expect(buf[magnitude: k] == expected,
"bin \(k) gate failed: got \(buf[magnitude: k]) expected \(expected)")
}
}
@Test func floorOneIsBypass() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 999.0, floor: 1.0)
g.process(spectral: buf, engine: makeTestEngine())
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
#expect(buf[magnitude: k] == Float(k) * 0.1,
"bin \(k) altered with floor=1 (bypass)")
}
#expect(buf.dcValue == 0.05)
#expect(buf.nyquistValue == -0.3)
}
@Test func dcAndNyquistGatedOnAbsPreserveSign() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
// DC = 0.05 (below threshold 0.1), Nyquist = -0.3 (above threshold).
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 0.1, floor: 0)
g.process(spectral: buf, engine: makeTestEngine())
#expect(buf.dcValue == 0.0)
#expect(buf.nyquistValue == -0.3) // |−0.3| > 0.1, passes through with sign.
}
@Test func phaseUntouched() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 0.5, floor: 0)
g.process(spectral: buf, engine: makeTestEngine())
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
#expect(buf[phase: k] == 0.7, "phase on bin \(k) altered")
}
}
@Test func intermediateFloorScales() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 0.5, floor: 0.25)
g.process(spectral: buf, engine: makeTestEngine())
// Bin 3: mag 0.3 < 0.5 → 0.3 * 0.25 = 0.075.
#expect(abs(buf[magnitude: 3] - 0.075) < 1e-6)
// Bin 8: mag 0.8 ≥ 0.5 → unchanged.
#expect(buf[magnitude: 8] == 0.8)
}
@Test func dbConvenienceMatchesLinear() {
let fftSize: Int = 32
let buf1: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let buf2: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf1)
Self.fillRamp(buf2)
let lin: Double = pow(10.0, -20.0 / 20.0) // 0.1
let gLin: SpectralGate = SpectralGate(fftSize: fftSize, threshold: lin, floor: 0)
let gDB: SpectralGate = SpectralGate(fftSize: fftSize, thresholdDB: -20.0, floor: 0)
gLin.process(spectral: buf1, engine: makeTestEngine())
gDB.process(spectral: buf2, engine: makeTestEngine())
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
#expect(abs(buf1[magnitude: k] - buf2[magnitude: k]) < 1e-6)
}
}
@Test func liveThresholdUpdate() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 999.0, floor: 0)
g.process(spectral: buf, engine: makeTestEngine())
// All gated to 0.
#expect(buf[magnitude: 5] == 0.0)
// Refill, lower threshold.
Self.fillRamp(buf)
g.threshold = 0.0
g.process(spectral: buf, engine: makeTestEngine())
// Nothing gated.
#expect(buf[magnitude: 5] == 0.5)
}
@Test func invalidBufferIsBypassed() {
let fftSize: Int = 32
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillRamp(buf)
buf.invalidate()
let g: SpectralGate = SpectralGate(fftSize: fftSize, threshold: 999.0, floor: 0)
g.process(spectral: buf, engine: makeTestEngine())
#expect(buf[magnitude: 5] == 0.5)
}
}