// TestHelpers.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth func makeTestEngine( blockSize: Int = 512, sampleRate: Double = 44100, numOutputChannels: Int = 2, numInputChannels: Int = 2, numAudioBuses: Int = 128, numControlBuses: Int = 4096, eventQueueCapacity: Int = lyrebirdDefaultEventQueueCapacity ) -> SynthesisEngine { SynthesisEngine( blockSize: blockSize, sampleRate: sampleRate, numOutputChannels: numOutputChannels, numInputChannels: numInputChannels, numAudioBuses: numAudioBuses, numControlBuses: numControlBuses, eventQueueCapacity: eventQueueCapacity, driver: OfflineDriver() ) } /// Create a started engine suitable for step-sequencer tests (small blockSize for fine timing). func makeSeqEngine() throws -> SynthesisEngine { let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() return engine } func makeSynthGraph(engine: SynthesisEngine, busOffset: Int = 0) -> SynthGraph { SynthGraph(engine: engine, busOffset: busOffset) } // MARK: - Synthesizer test fixtures /// Writes a constant value to an audio bus every block. final class ConstantSynth: SynthesizerBase { let value: Double let outputBus: Int init(value: Double, outputBus: Int = 0) { self.value = value self.outputBus = outputBus super.init() } override func build() { addOutputGenerator(Out.Audio(bus: outputBus, source: value)) } } /// Reads from one audio bus and accumulates into another — for ordering tests. final class RoutingSynth: SynthesizerBase { let inputBus: Int let outputBus: Int init(inputBus: Int, outputBus: Int) { self.inputBus = inputBus self.outputBus = outputBus super.init() } override func build() { addOutputGenerator(Out.Audio(bus: outputBus, source: In.Audio(bus: inputBus))) } } /// Counts build() invocations. Does not subclass SynthesizerBase so evaluate() is freely /// implementable — SynthesizerBase.evaluate is final (by design), so overriding it in a /// test subclass would be a compile error. final class TrackingSynth: Synthesizer { var buildCallCount: Int = 0 var evaluateCallCount: Int = 0 func build() { buildCallCount += 1 } func evaluate(graph: SynthGraph, count: Int) { evaluateCallCount += 1 } } /// Drive a generator for `blocks` blocks and return exactly `blockSize` samples per block. func collectBlocks( _ gen: any Generator, engine: SynthesisEngine, blocks: Int ) -> [Double] { let bs: Int = engine.blockSize var out: [Double] = [] out.reserveCapacity(blocks * bs) for _ in 0.. Double { guard !samples.isEmpty else { return 0 } var sumSq: Double = 0.0 for s: Double in samples { sumSq += s * s } return (sumSq / Double(samples.count)).squareRoot() } /// Minimal Synthesizer conformer for Group isolation tests (no engine startup needed). /// Records how many times evaluate() is called and what count was passed. final class TrackingNode: Synthesizer { var evaluateCallCount: Int = 0 var lastCount: Int = 0 func build() {} func evaluate(graph: SynthGraph, count: Int) { evaluateCallCount += 1 lastCount = count } }