Files
Lyrebird/Tests/LyrebirdTests/LagTests.swift
T
2026-06-01 18:27:40 -07:00

135 lines
4.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// LagTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Lag")
struct LagTests {
let engine: SynthesisEngine
let blockSize: Int = 64
let sampleRate: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
var graph: SynthGraph { makeSynthGraph(engine: engine) }
// MARK: 1. Rate
@Test func audioRate() {
let gen: Lag.Audio = Lag.Audio(input: 1.0, time: 0.1)
#expect(gen.rate == .audio)
}
@Test func controlRate() {
let gen: Lag.Control = Lag.Control(input: 1.0, time: 0.1)
#expect(gen.rate == .control)
}
// MARK: 2. Output length
@Test func audioOutputLength() {
let gen: Lag.Audio = Lag.Audio(input: 1.0, time: 0.05)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(result.count == 1)
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
@Test func controlOutputLength() {
let gen: Lag.Control = Lag.Control(input: 1.0, time: 0.05)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(result.count == 1)
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
// MARK: 3. DC steady-state convergence
@Test func audioConvergesToTarget() throws {
// Feed constant 1.0 for many blocks; output must exceed 0.999 after 2× lagTime.
let lagTime: Double = 0.01
let gen: Lag.Audio = Lag.Audio(input: 1.0, time: lagTime)
let blocksNeeded: Int = Int(ceil(2.0 * lagTime * sampleRate / Double(blockSize))) + 1
var last: Double = 0.0
for _ in 0..<blocksNeeded {
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
last = result[0][blockSize - 1]
advanceBlock(engine)
}
#expect(last > 0.999)
}
@Test func controlConvergesToTarget() throws {
let lagTime: Double = 0.01
let gen: Lag.Control = Lag.Control(input: 1.0, time: lagTime)
let blocksNeeded: Int = Int(ceil(2.0 * lagTime * sampleRate / Double(blockSize))) + 1
var last: Double = 0.0
for _ in 0..<blocksNeeded {
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
last = result[0][blockSize - 1]
advanceBlock(engine)
}
#expect(last > 0.999)
}
// MARK: 4. Zero time = transparent
@Test func zeroTimeIsTransparent() {
let gen: Lag.Audio = Lag.Audio(input: 1.0, time: 0.0)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(result[0][0] - 1.0) < 1e-10)
}
// MARK: 5. Fan-out caching
@Test func audioFanOut() {
let gen: Lag.Audio = Lag.Audio(input: 1.0, time: 0.05)
let g: SynthGraph = graph
let r1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
let r2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
#expect(r1[0][0] == r2[0][0])
}
@Test func controlFanOut() {
let gen: Lag.Control = Lag.Control(input: 1.0, time: 0.05)
let g: SynthGraph = graph
let r1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
let r2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
#expect(r1[0][0] == r2[0][0])
}
// MARK: 6. previousOutputValue
@Test func previousOutputValueUpdates() {
let gen: Lag.Control = Lag.Control(input: 1.0, time: 0.05)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == result[0][blockSize - 1])
}
// MARK: 7. mulAdd
@Test func mulAdd() {
// With time=0 (transparent), input=0.5, mul=2, add=1 → output = 0.5*2+1 = 2.0
let gen: Lag.Audio = Lag.Audio(input: 0.5, time: 0.0, mul: 2.0, add: 1.0)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(result[0][0] - 2.0) < 1e-10)
}
// MARK: 8. GeneratorEnum
@Test func generatorEnumAudioType() {
let _: Lag.AudioType = Lag.Audio(input: 1.0, time: 0.1)
}
@Test func generatorEnumControlType() {
let _: Lag.ControlType = Lag.Control(input: 1.0, time: 0.1)
}
}