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

112 lines
4.4 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.
// SpectralMagnitudeMapTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("SpectralMagnitudeMap")
struct SpectralMagnitudeMapTests {
/// Identity transfer (linear ramp 0 → 1) should leave magnitudes unchanged
/// within float precision.
@Test func identityTransferPreservesMagnitudes() {
let fftSize: Int = 64
let halfSize: Int = fftSize / 2
let curve: LyrebirdBuffer = LyrebirdBuffer(count: 256)
curve.fill { i, n in Double(i) / Double(n - 1) }
let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve)
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
// Populate bins with known magnitudes; mag at testBin sets the maximum.
let inputs: [Float] = [0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0]
for k: Int in 1..<halfSize {
buf[magnitude: k] = inputs[k % inputs.count]
}
map.process(spectral: buf, engine: makeTestEngine())
for k: Int in 1..<halfSize {
let expected: Float = inputs[k % inputs.count]
let got: Float = buf[magnitude: k]
#expect(abs(got - expected) < 1e-3,
"Identity transfer should preserve mag[\(k)] = \(expected), got \(got)")
}
}
/// Constant 0.5 transfer collapses every bin to `0.5 × max` (the same value)
/// regardless of original magnitude — the transfer ignores the lookup index.
@Test func constantHalfTransferCollapsesToHalfMax() {
let fftSize: Int = 64
let halfSize: Int = fftSize / 2
let curve: LyrebirdBuffer = LyrebirdBuffer(count: 256)
curve.fill { _, _ in 0.5 }
let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve)
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
for k: Int in 1..<halfSize {
buf[magnitude: k] = Float(k) / Float(halfSize)
}
let maxMag: Float = Float(halfSize - 1) / Float(halfSize)
let expected: Float = 0.5 * maxMag
map.process(spectral: buf, engine: makeTestEngine())
for k: Int in 1..<halfSize {
let got: Float = buf[magnitude: k]
#expect(abs(got - expected) < 1e-4,
"Constant 0.5 transfer should collapse mag[\(k)] to 0.5 × max = \(expected), got \(got)")
}
}
/// A frame whose maximum magnitude is below the floor must be left untouched.
@Test func belowFloorIsUntouched() {
let fftSize: Int = 64
let halfSize: Int = fftSize / 2
let curve: LyrebirdBuffer = LyrebirdBuffer(count: 256)
curve.fill { _, _ in 0.0 } // would zero everything if it ran
let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve, magnitudeFloor: 1e-3)
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let tinyMag: Float = 1e-6
for k: Int in 1..<halfSize {
buf[magnitude: k] = tinyMag
}
map.process(spectral: buf, engine: makeTestEngine())
for k: Int in 1..<halfSize {
#expect(buf[magnitude: k] == tinyMag,
"Below-floor frame must not be touched at bin \(k), got \(buf[magnitude: k])")
}
}
/// Step transfer: zero below halfway, identity above. Quiet bins should be
/// zeroed; loud bins should pass.
@Test func stepTransferGate() {
let fftSize: Int = 64
let halfSize: Int = fftSize / 2
let curve: LyrebirdBuffer = LyrebirdBuffer(count: 256)
curve.fill { i, n in Double(i) / Double(n - 1) >= 0.5 ? Double(i) / Double(n - 1) : 0.0 }
let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve)
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
// Set max at one bin; everything else proportionally.
buf[magnitude: 5] = 1.0 // max → normalised 1.0 → above gate
buf[magnitude: 10] = 0.2 // 0.2 → below gate
buf[magnitude: 15] = 0.6 // 0.6 → above gate
map.process(spectral: buf, engine: makeTestEngine())
#expect(buf[magnitude: 5] > 0.5, "Loud bin 5 should pass through, got \(buf[magnitude: 5])")
#expect(buf[magnitude: 10] == 0.0, "Quiet bin 10 should be gated, got \(buf[magnitude: 10])")
#expect(buf[magnitude: 15] > 0.0, "Above-gate bin 15 should pass, got \(buf[magnitude: 15])")
}
}