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

99 lines
3.3 KiB
Swift

// Decay2Tests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Decay2")
struct Decay2Tests {
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: Decay2.Audio = Decay2.Audio(input: 1.0, attackTime: 0.01, decayTime: 0.5)
#expect(gen.rate == .audio)
}
@Test func controlRate() {
let gen: Decay2.Control = Decay2.Control(input: 1.0, attackTime: 0.01, decayTime: 0.5)
#expect(gen.rate == .control)
}
// MARK: 2. Output length
@Test func audioOutputLength() {
let gen: Decay2.Audio = Decay2.Audio(input: 1.0, attackTime: 0.01, decayTime: 0.5)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(result.count == 1)
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
// MARK: 3. First sample is 0 when both integrators start at zero
@Test func firstSampleIsZero() {
let gen: Decay2.Audio = Decay2.Audio(input: 1.0, attackTime: 0.01, decayTime: 0.5)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
// At n=0 both integrators start at 0; yD[0]=x+bD*0=x, yA[0]=x+bA*0=x → output = 0
#expect(abs(result[0][0]) < 1e-10)
}
// MARK: 4. Output eventually becomes positive (decay integrator dominates after attack)
@Test func outputBecomesPositive() {
let gen: Decay2.Audio = Decay2.Audio(input: 1.0, attackTime: 0.001, decayTime: 0.5)
var maxVal: Double = 0.0
for _ in 0..<20 {
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
maxVal = max(maxVal, result[0][blockSize - 1])
advanceBlock(engine)
}
#expect(maxVal > 0)
}
// MARK: 5. Equal times = zero output (decay - attack cancel)
@Test func equalTimesGivesZero() {
let t: Double = 0.1
let gen: Decay2.Audio = Decay2.Audio(input: 1.0, attackTime: t, decayTime: t)
for _ in 0..<10 {
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
// Both integrators identical → difference = 0 every sample
for i: Int in 0..<blockSize {
#expect(abs(result[0][i]) < 1e-10)
}
}
}
// MARK: 6. Control previousOutputValue
@Test func previousOutputValueUpdates() {
let gen: Decay2.Control = Decay2.Control(input: 1.0, attackTime: 0.01, decayTime: 0.5)
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == result[0][blockSize - 1])
}
// MARK: 7. GeneratorEnum
@Test func generatorEnumAudioType() {
let _: Decay2.AudioType = Decay2.Audio(input: 1.0, attackTime: 0.01, decayTime: 0.5)
}
@Test func generatorEnumControlType() {
let _: Decay2.ControlType = Decay2.Control(input: 1.0, attackTime: 0.01, decayTime: 0.5)
}
}