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

165 lines
6.9 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.
// TrigAudioTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("Trig.Audio")
struct TrigAudioTests {
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 rateIsAudio() {
let gen: Trig.Audio = Trig.Audio(input: 0.0, dur: 0.1)
#expect(gen.rate == .audio)
}
/// Constant-zero input produces no rising edge; output stays at 0.
@Test func noTriggerOutputsZero() {
let gen: Trig.Audio = Trig.Audio(input: 0.0, dur: 0.1)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<blockSize {
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
}
}
/// Rising edge at sample 16 outputs the input value at that frame for `dur` samples.
/// The `input` signal is both the trigger source and the captured level.
@Test func risingEdgeOutputsInputLevel() {
let durSamples: Int = 10
let dur: Double = Double(durSamples) / 44100.0
// Input is 0 for samples 0..15, then 0.16 for samples 16+.
// Rising edge fires at sample 16; level captured = 0.16.
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.16 }
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
// Before edge: 0.
for i: Int in 0..<16 {
#expect(out[i] == 0.0, "pre-edge sample \(i) expected 0.0 got \(out[i])")
}
// Edge and dur-1 more samples: level = 0.16.
for i: Int in 16..<(16 + durSamples) {
#expect(abs(out[i] - 0.16) < 1e-12,
"pulse sample \(i) expected 0.16 got \(out[i])")
}
// After pulse: 0.
for i: Int in (16 + durSamples)..<blockSize {
#expect(out[i] == 0.0, "post-pulse sample \(i) expected 0.0 got \(out[i])")
}
}
/// New rising edge during hold (input drops to 0 then goes positive again)
/// restarts timer and re-captures level.
@Test func newEdgeRestartsTimer() {
let durSamples: Int = 20
let dur: Double = Double(durSamples) / 44100.0
// First edge at sample 5 (input goes from 0 to 0.05).
// Second edge at sample 15 (input drops to 0 then goes to 0.15).
let input: Closure = Closure { _, i in
if i < 5 { return 0.0 }
if i < 10 { return 0.05 }
if i < 15 { return 0.0 }
return 0.15
}
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
// After second edge at 15 the level should be re-captured as 0.15.
// Check samples 15..34 (20 samples) for level 0.15.
for i: Int in 15..<(15 + durSamples) {
#expect(abs(out[i] - 0.15) < 1e-12,
"re-capture sample \(i) expected 0.15 got \(out[i])")
}
}
/// Sustained input > 0 (no falling edge) produces only one capture.
@Test func sustainedTrigProducesOneCapture() {
let durSamples: Int = 8
let dur: Double = Double(durSamples) / 44100.0
// Input goes from 0 to 0.10 at sample 10 and stays.
let input: Closure = Closure { _, i in i >= 10 ? 0.10 : 0.0 }
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
// Captured at i=10: level 0.10.
for i: Int in 10..<(10 + durSamples) {
#expect(abs(out[i] - 0.10) < 1e-12,
"sustained sample \(i) expected 0.10 got \(out[i])")
}
// After pulse ends, input still high but no new edge → output = 0.
for i: Int in (10 + durSamples)..<blockSize {
#expect(out[i] == 0.0,
"post-pulse sample \(i) expected 0.0 got \(out[i])")
}
}
/// State (remaining countdown) persists across block boundaries.
@Test func heldCountdownPersistsAcrossBlocks() {
// dur > 1 block: fire in block 1, still counting in block 2.
let durSamples: Int = 100
let dur: Double = Double(durSamples) / 44100.0
let firstSerial: UInt64 = engine.segmentSerial
// Input = 0.5 for sample 0 of block 1 only, 0 elsewhere.
let input: Closure = Closure { graph, i in
graph.engine.segmentSerial == firstSerial && i == 0 ? 0.5 : 0.0
}
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
advanceBlock(engine)
// Block 2: 64 more samples consumed from the 100-sample countdown.
// We fired at sample 0 in block 1, consuming 64 of the 100 samples.
// In block 2 the remaining = 100 - 64 = 36 samples should still be high.
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<36 {
#expect(abs(out2[i] - 0.5) < 1e-12,
"block-2 pulse sample \(i) expected 0.5 got \(out2[i])")
}
for i: Int in 36..<blockSize {
#expect(out2[i] == 0.0,
"block-2 post-pulse sample \(i) expected 0.0 got \(out2[i])")
}
}
/// Fan-out: two calls in the same segment return identical first channel.
@Test func fanOutReturnsSameChannel() {
let input: Closure = Closure { _, i in i == 16 ? 0.5 : 0.0 }
let gen: Trig.Audio = Trig.Audio(input: input, dur: 0.01)
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<blockSize {
#expect(out1[i] == out2[i])
}
}
/// mulAdd: output is original × 2 + 1 when mul=2, add=1.
@Test func mulAddApplied() {
let durSamples: Int = 20
let dur: Double = Double(durSamples) / 44100.0
let input: Closure = Closure { _, i in i == 0 ? 0.5 : 0.0 }
let genNoMulAdd: Trig.Audio = Trig.Audio(input: input, dur: dur, mul: 1.0, add: 0.0)
let genMulAdd: Trig.Audio = Trig.Audio(input: input, dur: dur, mul: 2.0, add: 1.0)
let outNone: [Double] = genNoMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
advanceBlock(engine)
let outMul: [Double] = genMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
// During the pulse period, outNone[i] = 0.5, outMul[i] should be 0.5*2+1 = 2.0.
for i: Int in 0..<durSamples {
#expect(abs(outMul[i] - (outNone[i] * 2.0 + 1.0)) < 1e-12,
"mul/add mismatch at sample \(i)")
}
}
}