Files
2026-06-21 11:45:58 -07:00

81 lines
3.3 KiB
Swift

// LorenzLinearAudioTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - LorenzLinearAudioTests
@MainActor
@Suite("LorenzLinear.Audio")
struct LorenzLinearAudioTests {
let engine: SynthesisEngine
let sr: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func freqZeroHoldsSecondIterationValue() {
// dphase = 0 → output stays at the primed segment start h0 (= the 2nd Euler
// iteration). With s=10, r=28, b=2.667, h=0.02, xi=0.1: iterations are
// x1 = 0.08, x2 = 0.0752, so h0 = 0.0752.
let lorenz: LorenzLinear.Audio = LorenzLinear.Audio(freq: 0.0)
let samples: [Double] = collectBlocks(lorenz, engine: engine, blocks: 4)
#expect(samples.allSatisfy { abs($0 - 0.0752) < 1e-12 })
}
@Test func orbitFiniteAndEvolves() {
let lorenz: LorenzLinear.Audio = LorenzLinear.Audio(freq: sr)
let samples: [Double] = collectBlocks(lorenz, engine: engine, blocks: 200)
#expect(samples.allSatisfy { $0.isFinite })
// Canonical Lorenz x stays bounded to roughly ±25.
#expect(samples.allSatisfy { abs($0) < 30.0 })
#expect(Set(samples.map { ($0 * 1e4).rounded() }).count > 50)
}
@Test func allInterpolationModesFinite() {
for mode: TableInterpolation in [.none, .linear, .cubic] {
let lorenz: LorenzLinear.Audio = LorenzLinear.Audio(freq: 6000.0, interpolation: mode)
let samples: [Double] = collectBlocks(lorenz, engine: engine, blocks: 20)
#expect(samples.allSatisfy { $0.isFinite }, "mode \(mode) non-finite")
}
}
@Test func twoInstancesProduceIdenticalOutput() throws {
let l1: LorenzLinear.Audio = LorenzLinear.Audio(freq: 9000.0)
let s1: [Double] = collectBlocks(l1, engine: engine, blocks: 30)
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
try engine2.start()
let l2: LorenzLinear.Audio = LorenzLinear.Audio(freq: 9000.0)
let s2: [Double] = collectBlocks(l2, engine: engine2, blocks: 30)
#expect(s1 == s2)
}
@Test func participatesInFanOutCache() {
let lorenz: LorenzLinear.Audio = LorenzLinear.Audio(freq: 9000.0)
let g: SynthGraph = makeSynthGraph(engine: engine)
let r1: [[Double]] = lorenz.calculatedSamples(graph: g, count: engine.blockSize)
let r2: [[Double]] = lorenz.calculatedSamples(graph: g, count: engine.blockSize)
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
}
@Test func mulAddScalesAndOffsets() {
let plain: LorenzLinear.Audio = LorenzLinear.Audio(freq: 0.0)
let g1: SynthGraph = makeSynthGraph(engine: engine)
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
let scaled: LorenzLinear.Audio = LorenzLinear.Audio(freq: 0.0, mul: 0.05, add: 0.5)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let s: Double = scaled.calculatedSamples(graph: g2, count: engine.blockSize)[0][0]
#expect(abs(s - (p * 0.05 + 0.5)) < 1e-12)
}
}