84 lines
3.6 KiB
Swift
84 lines
3.6 KiB
Swift
// StandardSteppedAudioTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - StandardSteppedAudioTests
|
|
|
|
@MainActor
|
|
@Suite("StandardStepped.Audio")
|
|
struct StandardSteppedAudioTests {
|
|
|
|
let engine: SynthesisEngine
|
|
let sr: Double = 44100
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func deterministicSteppedIterations() {
|
|
// freq == sampleRate, .none → held outputs are the warm-up iterates 2,3,…
|
|
let m: StandardStepped.Audio = StandardStepped.Audio(freq: sr, interpolation: .none)
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let s: [[Double]] = m.calculatedSamples(graph: g, count: engine.blockSize)
|
|
#expect(abs(s[0][0] - (2.2890283227337602)) < 1e-9)
|
|
#expect(abs(s[0][1] - (4.351601455163474)) < 1e-9)
|
|
}
|
|
|
|
@Test func freqZeroHoldsSecondIteration() {
|
|
let m: StandardStepped.Audio = StandardStepped.Audio(freq: 0.0)
|
|
let samples: [Double] = collectBlocks(m, engine: engine, blocks: 4)
|
|
#expect(samples.allSatisfy { abs($0 - (2.2890283227337602)) < 1e-9 })
|
|
}
|
|
|
|
@Test func chaoticOrbitStaysBounded() {
|
|
let m: StandardStepped.Audio = StandardStepped.Audio(freq: sr)
|
|
let samples: [Double] = collectBlocks(m, engine: engine, blocks: 50)
|
|
#expect(samples.allSatisfy { $0.isFinite })
|
|
#expect(samples.allSatisfy { $0 >= 0.0 && $0 < lyrebirdTwoPi }) // x in [0, 2pi)
|
|
#expect(Set(samples.map { ($0 * 1e4).rounded() }).count > 10)
|
|
}
|
|
|
|
@Test func allInterpolationModesFinite() {
|
|
for mode: TableInterpolation in [.none, .linear, .cubic] {
|
|
let m: StandardStepped.Audio = StandardStepped.Audio(freq: 4000.0, interpolation: mode)
|
|
let samples: [Double] = collectBlocks(m, engine: engine, blocks: 20)
|
|
#expect(samples.allSatisfy { $0.isFinite }, "mode \(mode) non-finite")
|
|
}
|
|
}
|
|
|
|
@Test func twoInstancesProduceIdenticalOutput() throws {
|
|
let a1: StandardStepped.Audio = StandardStepped.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: StandardStepped.Audio = StandardStepped.Audio(freq: 8000.0)
|
|
let s2: [Double] = collectBlocks(a2, engine: engine2, blocks: 10)
|
|
#expect(s1 == s2)
|
|
}
|
|
|
|
@Test func participatesInFanOutCache() {
|
|
let m: StandardStepped.Audio = StandardStepped.Audio(freq: 8000.0)
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let r1: [[Double]] = m.calculatedSamples(graph: g, count: engine.blockSize)
|
|
let r2: [[Double]] = m.calculatedSamples(graph: g, count: engine.blockSize)
|
|
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
|
|
}
|
|
|
|
@Test func mulAddScalesAndOffsets() {
|
|
let plain: StandardStepped.Audio = StandardStepped.Audio(freq: 0.0)
|
|
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
|
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
|
|
let scaled: StandardStepped.Audio = StandardStepped.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)
|
|
}
|
|
}
|