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