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

95 lines
4.1 KiB
Swift

// HenonSteppedAudioTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - HenonSteppedAudioTests
@MainActor
@Suite("HenonStepped.Audio")
struct HenonSteppedAudioTests {
let engine: SynthesisEngine
let sr: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func deterministicSteppedIterations() {
// freq == sampleRate → dphase = 1, one iteration per sample, .none holds the
// segment start. With a=1.4, b=0.3, x0=x1=0 the warm-up iterations are
// 1, -0.4, 1.076, -0.7408864; after 4-iteration priming the held outputs are
// -0.4, 1.076, -0.7408864, ...
let henon: HenonStepped.Audio = HenonStepped.Audio(freq: sr, a: 1.4, b: 0.3,
x0: 0.0, x1: 0.0, interpolation: .none)
let g: SynthGraph = makeSynthGraph(engine: engine)
let s: [[Double]] = henon.calculatedSamples(graph: g, count: engine.blockSize)
#expect(abs(s[0][0] - (-0.4)) < 1e-9)
#expect(abs(s[0][1] - 1.076) < 1e-9)
#expect(abs(s[0][2] - (-0.7408864)) < 1e-9)
}
@Test func chaoticOrbitStaysBounded() {
let henon: HenonStepped.Audio = HenonStepped.Audio(freq: sr) // canonical a/b
let samples: [Double] = collectBlocks(henon, engine: engine, blocks: 50)
#expect(samples.allSatisfy { $0.isFinite })
#expect(samples.allSatisfy { abs($0) < 2.0 })
// Not a constant — the orbit visits distinct values.
#expect(Set(samples.map { ($0 * 1e6).rounded() }).count > 10)
}
@Test func allInterpolationModesFinite() {
for mode: TableInterpolation in [.none, .linear, .cubic] {
let henon: HenonStepped.Audio = HenonStepped.Audio(freq: 4000.0, interpolation: mode)
let samples: [Double] = collectBlocks(henon, engine: engine, blocks: 20)
#expect(samples.allSatisfy { $0.isFinite }, "mode \(mode) non-finite")
}
}
@Test func freqZeroHoldsConstant() {
// dphase = 0 → never iterates past priming → constant held output.
let henon: HenonStepped.Audio = HenonStepped.Audio(freq: 0.0, interpolation: .linear)
let samples: [Double] = collectBlocks(henon, engine: engine, blocks: 4)
let first: Double = samples.first ?? .nan
#expect(samples.allSatisfy { $0 == first })
}
@Test func twoInstancesProduceIdenticalOutput() throws {
let h1: HenonStepped.Audio = HenonStepped.Audio(freq: 8000.0)
let s1: [Double] = collectBlocks(h1, engine: engine, blocks: 10)
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
try engine2.start()
let h2: HenonStepped.Audio = HenonStepped.Audio(freq: 8000.0)
let s2: [Double] = collectBlocks(h2, engine: engine2, blocks: 10)
#expect(s1 == s2)
}
@Test func participatesInFanOutCache() {
let henon: HenonStepped.Audio = HenonStepped.Audio(freq: 8000.0)
let g: SynthGraph = makeSynthGraph(engine: engine)
let r1: [[Double]] = henon.calculatedSamples(graph: g, count: engine.blockSize)
let r2: [[Double]] = henon.calculatedSamples(graph: g, count: engine.blockSize)
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
}
@Test func mulAddScalesAndOffsets() {
let plain: HenonStepped.Audio = HenonStepped.Audio(freq: sr, interpolation: .none)
let g1: SynthGraph = makeSynthGraph(engine: engine)
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
let scaled: HenonStepped.Audio = HenonStepped.Audio(freq: sr, interpolation: .none,
mul: 2.0, 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 * 2.0 + 1.0)) < 1e-12)
}
}