// ReplaceOutAudioGeneratorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("ReplaceOut.Audio") struct ReplaceOutAudioGeneratorTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 16) init() throws { try engine.start() } @Test func rateIsAudio() { let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5)) #expect(gen.rate == .audio) } @Test func overwritesBus() { let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5)) let graph: SynthGraph = makeSynthGraph(engine: engine) _ = gen.calculatedSamples(graph: graph, count: 8) for i: Int in 0..<8 { #expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6) } } @Test func wipesPriorAdditiveWrites() { // Sequence: additive Out writes 0.3, then ReplaceOut writes 0.5. // Final bus must equal 0.5 (not 0.8). let outGen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3)) let repGen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5)) let graph: SynthGraph = makeSynthGraph(engine: engine) _ = outGen.calculatedSamples(graph: graph, count: 8) _ = repGen.calculatedSamples(graph: graph, count: 8) for i: Int in 0..<8 { #expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6) } } @Test func additiveAfterReplaceAccumulates() { // Sequence: ReplaceOut writes 0.5, then additive Out adds 0.3. // Final bus must equal 0.8. let repGen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5)) let outGen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3)) let graph: SynthGraph = makeSynthGraph(engine: engine) _ = repGen.calculatedSamples(graph: graph, count: 8) _ = outGen.calculatedSamples(graph: graph, count: 8) for i: Int in 0..<8 { #expect(abs(engine.audioBuses[0][i] - 0.8) < 1e-6) } } @Test func writesOnlyCountSamples() { let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5)) let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 0) _ = gen.calculatedSamples(graph: graph, count: 4) for i: Int in 0..<4 { #expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6) } for i: Int in 4..