// 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.. 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).. 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..