// OutGeneratorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Out (audio bus)") struct OutGeneratorTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 16) init() throws { try engine.start() } @Test func rateIsAudio() { let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5)) #expect(gen.rate == .audio) } @Test func accumulatesIntoBus() { let gen: Out.Audio = Out.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 accumulatesAdditively() { // Two independent Out instances writing the same value to the same bus must sum to 2×value. // (Fan-out cache: calling the same instance twice in one segment is a no-op on the // second call — use two distinct instances to exercise additive accumulation.) let genA: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5)) let genB: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5)) let graph: SynthGraph = makeSynthGraph(engine: engine) _ = genA.calculatedSamples(graph: graph, count: 8) _ = genB.calculatedSamples(graph: graph, count: 8) for i: Int in 0..<8 { #expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-6) } } @Test func writesOnlyCountSamples() { let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5)) let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 0) _ = gen.calculatedSamples(graph: graph, count: 4) // First 4 samples should be 0.5 for i: Int in 0..<4 { #expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6) } // Rest should remain 0 for i: Int in 4..