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

86 lines
3.5 KiB
Swift

// GbmanSteppedAudioTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - GbmanSteppedAudioTests
@MainActor
@Suite("GbmanStepped.Audio")
struct GbmanSteppedAudioTests {
let engine: SynthesisEngine
let sr: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func deterministicSteppedIterations() {
// freq == sampleRate, .none. xi=1.2, yi=2.1: iterates 0.1, -0.1, 1.0, 2.1, …
// → held outputs -0.1, 1.0, 2.1, …
let gb: GbmanStepped.Audio = GbmanStepped.Audio(freq: sr, interpolation: .none)
let g: SynthGraph = makeSynthGraph(engine: engine)
let s: [[Double]] = gb.calculatedSamples(graph: g, count: engine.blockSize)
#expect(abs(s[0][0] - (-0.1)) < 1e-9)
#expect(abs(s[0][1] - 1.0) < 1e-9)
#expect(abs(s[0][2] - 2.1) < 1e-9)
}
@Test func freqZeroHoldsSecondIteration() {
let gb: GbmanStepped.Audio = GbmanStepped.Audio(freq: 0.0)
let samples: [Double] = collectBlocks(gb, engine: engine, blocks: 4)
#expect(samples.allSatisfy { abs($0 - (-0.1)) < 1e-9 })
}
@Test func chaoticOrbitStaysBounded() {
let gb: GbmanStepped.Audio = GbmanStepped.Audio(freq: sr)
let samples: [Double] = collectBlocks(gb, engine: engine, blocks: 50)
#expect(samples.allSatisfy { $0.isFinite })
#expect(samples.allSatisfy { abs($0) < 10.0 })
#expect(Set(samples.map { ($0 * 1e4).rounded() }).count > 10)
}
@Test func allInterpolationModesFinite() {
for mode: TableInterpolation in [.none, .linear, .cubic] {
let gb: GbmanStepped.Audio = GbmanStepped.Audio(freq: 4000.0, interpolation: mode)
let samples: [Double] = collectBlocks(gb, engine: engine, blocks: 20)
#expect(samples.allSatisfy { $0.isFinite }, "mode \(mode) non-finite")
}
}
@Test func twoInstancesProduceIdenticalOutput() throws {
let a1: GbmanStepped.Audio = GbmanStepped.Audio(freq: 8000.0)
let s1: [Double] = collectBlocks(a1, engine: engine, blocks: 10)
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
try engine2.start()
let a2: GbmanStepped.Audio = GbmanStepped.Audio(freq: 8000.0)
let s2: [Double] = collectBlocks(a2, engine: engine2, blocks: 10)
#expect(s1 == s2)
}
@Test func participatesInFanOutCache() {
let gb: GbmanStepped.Audio = GbmanStepped.Audio(freq: 8000.0)
let g: SynthGraph = makeSynthGraph(engine: engine)
let r1: [[Double]] = gb.calculatedSamples(graph: g, count: engine.blockSize)
let r2: [[Double]] = gb.calculatedSamples(graph: g, count: engine.blockSize)
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
}
@Test func mulAddScalesAndOffsets() {
let plain: GbmanStepped.Audio = GbmanStepped.Audio(freq: 0.0)
let g1: SynthGraph = makeSynthGraph(engine: engine)
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
let scaled: GbmanStepped.Audio = GbmanStepped.Audio(freq: 0.0, mul: 0.5, add: 1.0)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let s: Double = scaled.calculatedSamples(graph: g2, count: engine.blockSize)[0][0]
#expect(abs(s - (p * 0.5 + 1.0)) < 1e-12)
}
}