Files
Lyrebird/Tests/LyrebirdTests/Generators/Triggers/TriggeredDelayControlTests.swift
T
2026-06-21 11:45:58 -07:00

100 lines
4.2 KiB
Swift

// TriggeredDelayControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("TriggeredDelay.Control")
struct TriggeredDelayControlTests {
let engine: SynthesisEngine
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let blockSize: Int = 64
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func rateIsControl() {
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
#expect(gen.rate == .control)
}
/// Without any rising edge, output holds 0.
@Test func noTriggerOutputsZero() {
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == 0.0)
}
/// Rising edge triggers countdown; after dur samples the block that contains the
/// firing position will have previousOutputValue = 1.0.
@Test func delayedPulseFires() {
// dur = exactly blockSize samples so the pulse fires in block 2.
let durSamples: Int = blockSize
let dur: Double = Double(durSamples) / 44100.0
let firstSerial: UInt64 = engine.segmentSerial
let trig: Closure = Closure { graph, i in
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0
}
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: trig, dur: dur)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// Block 1: counting down, output = 0.
#expect(gen.previousOutputValue == 0.0)
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// Block 2: countdown reached 0, pulse fired = 1.0.
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
"after delay expected 1.0 got \(gen.previousOutputValue)")
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// Block 3: back to idle, output = 0.
#expect(gen.previousOutputValue == 0.0,
"post-pulse block expected 0.0 got \(gen.previousOutputValue)")
}
/// Output block is flat (discrete, not ramped).
@Test func outputBlockIsFlat() {
// Fire in the first block so we can verify all samples.
let durSamples: Int = 1 // min dur; fires at sample 1 = within block.
let dur: Double = Double(durSamples) / 44100.0
let trig: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: trig, dur: dur)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
let block: [Double] = Array(out[0].prefix(blockSize))
// Pulse fired mid-block at sample 1; block-final value = 1.0 (after fire).
// All samples in the block should be 1.0 (flat output).
let finalValue: Double = gen.previousOutputValue
for i: Int in 0..<blockSize {
#expect(block[i] == finalValue,
"sample \(i) expected \(finalValue) got \(block[i])")
}
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
#expect(gen.floatValue(graph: graph) == 0.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
}
/// Fan-out: two calls in same segment return identical channel.
@Test func fanOutReturnsSameChannel() {
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
for i: Int in 0..<blockSize {
#expect(out1[i] == out2[i])
}
}
}