// SignalChangedTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SignalChanged.Audio") struct SignalChangedAudioTests { 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: SignalChanged.Audio = SignalChanged.Audio(input: 0.0) #expect(gen.rate == .audio) } /// Constant input → no per-sample change → output all zeros. @Test func constantInputProducesZeros() { let gen: SignalChanged.Audio = SignalChanged.Audio(input: 0.5) let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0] // First sample: prev=0, current=0.5 → diff=0.5 > 0 → 1.0 (initial transition) #expect(out[0] == 1.0) // Subsequent samples: 0.5 == 0.5 → 0.0 for i: Int in 1.. 0 → fires. #expect(out[32] == 1.0) } /// State persists across blocks: a sample equal to the previous block's /// last sample produces no change at the start of the next block. @Test func prevSampleCarriesAcrossBlocks() { // Per-block-discriminated closure: block 1 = 1.0, block 2 = 1.0 // (continuous), so no change anywhere in block 2. let firstSerial: UInt64 = engine.segmentSerial let constInput: Closure = Closure { graph, i in graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 1.0 } let gen: SignalChanged.Audio = SignalChanged.Audio(input: constInput) let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0] // First sample of block 1: prev=0, current=1.0 → fires. #expect(out1[0] == 1.0) for i: Int in 1..