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

92 lines
3.2 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.
// Lag2UpDownTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Lag2UpDown")
struct Lag2UpDownTests {
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: Lag2UpDown.Audio = Lag2UpDown.Audio(input: 1.0, lagUp: 0.1, lagDown: 0.01)
#expect(gen.rate == .audio)
}
@Test func controlRate() {
let gen: Lag2UpDown.Control = Lag2UpDown.Control(input: 1.0, lagUp: 0.1, lagDown: 0.01)
#expect(gen.rate == .control)
}
// MARK: 2. Output length
@Test func audioOutputLength() {
let gen: Lag2UpDown.Audio = Lag2UpDown.Audio(input: 1.0, lagUp: 0.1, lagDown: 0.01)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(result.count == 1)
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
// MARK: 3. Equal times matches Lag2
@Test func equalTimesMatchesLag2() {
let t: Double = 0.05
let lag2ud: Lag2UpDown.Audio = Lag2UpDown.Audio(input: 1.0, lagUp: t, lagDown: t)
let lag2: Lag2.Audio = Lag2.Audio(input: 1.0, time: t)
for _ in 0..<10 {
let g: SynthGraph = makeSynthGraph(engine: engine)
let r1: [[Double]] = lag2ud.calculatedSamples(graph: g, count: blockSize)
let r2: [[Double]] = lag2.calculatedSamples(graph: g, count: blockSize)
#expect(abs(r1[0][blockSize - 1] - r2[0][blockSize - 1]) < 1e-10)
advanceBlock(engine)
}
}
// MARK: 4. Asymmetry preserved over two stages
@Test func slowRiseWithLargeUpTime() {
// Large lagUp means slow convergence toward 1.
let gen: Lag2UpDown.Audio = Lag2UpDown.Audio(input: 1.0, lagUp: 0.5, lagDown: 0.001)
for _ in 0..<20 {
_ = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
advanceBlock(engine)
}
// After 20 blocks × 64 = 1280 samples (< 0.03 s), should still be well below 1
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
#expect(result[0][blockSize - 1] < 0.5)
}
// MARK: 5. Control previousOutputValue
@Test func previousOutputValueUpdates() {
let gen: Lag2UpDown.Control = Lag2UpDown.Control(input: 1.0, lagUp: 0.1, lagDown: 0.01)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == result[0][blockSize - 1])
}
// MARK: 6. GeneratorEnum
@Test func generatorEnumAudioType() {
let _: Lag2UpDown.AudioType = Lag2UpDown.Audio(input: 1.0, lagUp: 0.1, lagDown: 0.01)
}
@Test func generatorEnumControlType() {
let _: Lag2UpDown.ControlType = Lag2UpDown.Control(input: 1.0, lagUp: 0.1, lagDown: 0.01)
}
}