Files
Lyrebird/Tests/LyrebirdTests/Generators/Spectral/SpectralDiffuseTests.swift
T
2026-06-21 11:45:58 -07:00

160 lines
6.0 KiB
Swift

// SpectralDiffuseTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("SpectralDiffuse")
struct SpectralDiffuseTests {
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
}
private static func fillBins(_ buf: SpectralBuffer, phase: Float = 0.4) {
let halfSize: Int = buf.fftSize / 2
for k: Int in 1..<halfSize {
buf[magnitude: k] = Float(k)
buf[phase: k] = phase
}
}
@Test func amountZeroIsBypass() {
let fftSize: Int = 64
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf)
let halfSize: Int = fftSize / 2
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 0, seed: 12345)
d.process(spectral: buf, engine: makeTestEngine())
for k: Int in 1..<halfSize {
#expect(buf[phase: k] == 0.4, "bin \(k) phase changed with amount=0")
}
}
@Test func magnitudeUntouched() {
let fftSize: Int = 64
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf)
let halfSize: Int = fftSize / 2
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 1, seed: 42)
d.process(spectral: buf, engine: makeTestEngine())
for k: Int in 1..<halfSize {
#expect(buf[magnitude: k] == Float(k), "bin \(k) magnitude altered")
}
}
@Test func amountOnePhaseDistributionRoughlyUniform() {
// With amount=1, output phases over many bins + many hops should
// approximately fill (-π, π] uniformly.
let fftSize: Int = 1024
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize { buf[magnitude: k] = 1 }
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 1, seed: 99)
// Run several hops, collect a histogram of bin 100 phase across hops.
var bins: [Int] = [Int](repeating: 0, count: 8) // 8 angular buckets over (-π, π]
let twoPi: Float = 2 * Float.pi
let hops: Int = 4_000
for _ in 0..<hops {
d.process(spectral: buf, engine: makeTestEngine())
let p: Float = buf[phase: 100]
// Map p ∈ (-π, π] to [0, 8).
var u: Float = (p + Float.pi) / twoPi
if u >= 1 { u = 0.999_999 }
if u < 0 { u = 0 }
let idx: Int = min(7, max(0, Int(u * 8)))
bins[idx] += 1
}
// Each bucket should hold roughly hops/8 = 500. Tolerate ±50%.
for (i, c) in bins.enumerated() {
#expect(c > 200 && c < 800,
"bucket \(i) count \(c) outside expected range — distribution not uniform")
}
}
@Test func deterministicWithFixedSeed() {
let fftSize: Int = 64
let buf1: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
let buf2: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf1)
Self.fillBins(buf2)
let d1: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 0.7, seed: 0xABCDEF)
let d2: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 0.7, seed: 0xABCDEF)
d1.process(spectral: buf1, engine: makeTestEngine())
d2.process(spectral: buf2, engine: makeTestEngine())
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
#expect(buf1[phase: k] == buf2[phase: k],
"deterministic seed mismatch on bin \(k)")
}
}
@Test func intermediateAmountMovesByFractionOfDelta() {
let fftSize: Int = 64
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf)
let pre: Float = 0.4
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 0.5, seed: 7)
d.process(spectral: buf, engine: makeTestEngine())
// For amount=0.5, every bin moved exactly half the angular distance to
// its drawn target. We can't predict the target, but |delta| should be
// ≤ π/2 (since full delta is ≤ π and we moved half).
let halfSize: Int = fftSize / 2
for k: Int in 1..<halfSize {
let d_: Float = abs(Self.angularDelta(buf[phase: k], pre))
#expect(d_ <= Float.pi * 0.5 + 1e-5,
"bin \(k) moved \(d_) > π/2 with amount=0.5")
}
}
@Test func liveAmountUpdate() {
let fftSize: Int = 64
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf)
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 0, seed: 1)
d.process(spectral: buf, engine: makeTestEngine())
#expect(buf[phase: 5] == 0.4, "amount=0 should bypass")
d.amount = 1.0
d.process(spectral: buf, engine: makeTestEngine())
// Highly likely to differ from 0.4 with amount=1 (probability of exact
// match is essentially zero with float-precision random draw).
#expect(buf[phase: 5] != 0.4)
}
@Test func amountClampedToUnitRange() {
let fftSize: Int = 64
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 5.0)
#expect(d.amount == 1.0)
let d2: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: -1.0)
#expect(d2.amount == 0.0)
}
@Test func invalidBufferIsBypassed() {
let fftSize: Int = 64
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
Self.fillBins(buf)
buf.invalidate()
let d: SpectralDiffuse = SpectralDiffuse(fftSize: fftSize, amount: 1.0, seed: 1)
d.process(spectral: buf, engine: makeTestEngine())
#expect(buf[phase: 5] == 0.4)
}
}