// TrigControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Trig.Control") struct TrigControlTests { 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: Trig.Control = Trig.Control(input: 0.0, dur: 0.1) #expect(gen.rate == .control) } /// Constant-zero input produces no rising edge; output holds 0. @Test func noTriggerOutputsZero() { let gen: Trig.Control = Trig.Control(input: 0.0, dur: 0.1) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(gen.previousOutputValue == 0.0) } /// Rising edge at sample 16 causes output to hold the captured level for the block. @Test func risingEdgeCapturesLevel() { let durSamples: Int = blockSize * 2 // multi-block hold let dur: Double = Double(durSamples) / 44100.0 // Input is 0 for samples 0..15, then 0.16 for the rest. let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.16 } let gen: Trig.Control = Trig.Control(input: input, dur: dur) _ = gen.calculatedSamples(graph: graph, count: blockSize) // The block contains an edge at sample 16 — level 0.16 should be captured. #expect(abs(gen.previousOutputValue - 0.16) < 1e-12, "previousOutputValue expected 0.16 got \(gen.previousOutputValue)") } /// Countdown advances once per block via bypass; expires after dur. @Test func subBlockBypassCallsOncePerBlock() { // dur slightly longer than one block so we can test 2-block expiry. let durSamples: Int = blockSize + 10 let dur: Double = Double(durSamples) / 44100.0 let firstSerial: UInt64 = engine.segmentSerial // Input = 1.0 at sample 0 of block 1 only. let input: Closure = Closure { graph, i in graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0 } let gen: Trig.Control = Trig.Control(input: input, dur: dur) _ = gen.calculatedSamples(graph: graph, count: blockSize) // After block 1: still within dur, level = 1.0. #expect(abs(gen.previousOutputValue - 1.0) < 1e-12) advanceBlock(engine) _ = gen.calculatedSamples(graph: graph, count: blockSize) // After block 2: dur has expired, level = 0. #expect(gen.previousOutputValue == 0.0, "after dur expired expected 0.0 got \(gen.previousOutputValue)") } /// Boundary: a hold whose duration equals exactly one block stays active /// across every sample, so the block-final value is the captured level — /// not 0 (regression for inferring output from post-loop `remaining`). @Test func holdSpanningExactlyOneBlockOutputsLevel() { let durSamples: Int = blockSize // hold == one block exactly let dur: Double = Double(durSamples) / 44100.0 // Edge at sample 0 (prevTrig defaults to 0, x = 0.7). let input: Closure = Closure { _, _ in 0.7 } let gen: Trig.Control = Trig.Control(input: input, dur: dur) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.7) < 1e-12, "full-block hold expected 0.7 got \(gen.previousOutputValue)") } /// floatValue returns previousOutputValue. @Test func floatValueReturnsPreviousOutputValue() { let gen: Trig.Control = Trig.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) } /// Output block is flat (discrete, not ramped). @Test func outputBlockIsFlat() { let durSamples: Int = blockSize * 2 let dur: Double = Double(durSamples) / 44100.0 // Input goes from 0 to 0.7 at sample 0. let input: Closure = Closure { _, i in i == 0 ? 0.7 : 0.0 } // Actually input needs to STAY > 0 for the full dur. Use constant 0.7 after edge. let input2: Closure = Closure { _, _ in 0.7 } let gen: Trig.Control = Trig.Control(input: input2, dur: dur) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let block: [Double] = Array(out[0].prefix(blockSize)) // After trigger fires at sample 0 (prevTrig=0, x=0.7), captured level = 0.7. // All block samples should be 0.7. for i: Int in 0..