// FanOutCachingTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Tests that verify fan-out caching correctness. /// /// The core invariant: when a single generator instance is consumed by multiple downstream /// generators within the same engine segment (same `segmentSerial`), its `_computeSamples` /// is invoked exactly once and the cached result is shared. This prevents the "double phase /// advance" artefact that one oscillator feeding two `Out` generators would otherwise produce. @MainActor @Suite("Fan-out caching") struct FanOutCachingTests { // MARK: - Serial cache invalidation @Test func firstCallComputesSamples() { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) let graph: SynthGraph = makeSynthGraph(engine: engine) let osc: OscSin.Audio = OscSin.Audio(freq: 0.0, phase: 0.25) // freq=0 → constant at sin(π/2)=1.0 let result: [[Double]] = osc.calculatedSamples(graph: graph, count: 4) #expect(result[0].prefix(4).allSatisfy { abs($0 - 1.0) < 1e-6 }) } @Test func secondCallInSameSegmentReturnsCachedResult() { // With the same segmentSerial, a second call must not re-compute (no phase advance). let engine: SynthesisEngine = makeTestEngine(blockSize: 8) let graph: SynthGraph = makeSynthGraph(engine: engine) let osc: OscSin.Audio = OscSin.Audio(freq: 440.0) let result1: [[Double]] = osc.calculatedSamples(graph: graph, count: 4) let result2: [[Double]] = osc.calculatedSamples(graph: graph, count: 4) // Identical arrays — cache hit, no recomputation. for i: Int in 0..<4 { #expect(result1[0][i] == result2[0][i]) } } @Test func newSegmentInvalidatesCache() { // After advanceBlock, the same generator must recompute (oscillator phase advances). let engine: SynthesisEngine = makeTestEngine(blockSize: 8) let graph: SynthGraph = makeSynthGraph(engine: engine) let osc: OscSin.Audio = OscSin.Audio(freq: 440.0) let block1: [[Double]] = osc.calculatedSamples(graph: graph, count: 4) advanceBlock(engine) let block2: [[Double]] = osc.calculatedSamples(graph: graph, count: 4) // Blocks differ because the oscillator advanced phase. let identical: Bool = (0..<4).allSatisfy { block1[0][$0] == block2[0][$0] } #expect(!identical) } // MARK: - Fan-out: shared generator feeds two consumers @Test func sharedOscillatorsProduceIdenticalOutputForBothConsumers() throws { // Single OscSin → Out.Audio(bus:0) + Out.Audio(bus:1) // Both buses must contain bit-identical samples — proves _computeSamples ran once. let engine: SynthesisEngine = makeTestEngine(blockSize: 16) try engine.start() let freq: Double = 440.0 let osc: OscSin.Audio = OscSin.Audio(freq: freq) let outL: Out.Audio = Out.Audio(bus: 0, source: osc) let outR: Out.Audio = Out.Audio(bus: 1, source: osc) let graph: SynthGraph = makeSynthGraph(engine: engine) _ = outL.calculatedSamples(graph: graph, count: 16) _ = outR.calculatedSamples(graph: graph, count: 16) // Bus 0 and Bus 1 must be identical. for i: Int in 0..<16 { #expect(engine.audioBuses[0][i] == engine.audioBuses[1][i], "sample \(i): bus0=\(engine.audioBuses[0][i]) bus1=\(engine.audioBuses[1][i])") } engine.stop() } @Test func sharedOscillatorPhaseAdvancesOnlyOncePerSegment() throws { // Two consumers in the same segment must not double-advance the oscillator's phase. // Strategy: compare two-consumer output against a single-consumer reference. // If phase is doubled, output frequency is doubled — not bit-identical. let engine: SynthesisEngine = makeTestEngine(blockSize: 16) try engine.start() let freq: Double = 440.0 let count: Int = 16 // Reference: single osc → single Out (correct phase) let refOsc: OscSin.Audio = OscSin.Audio(freq: freq) let refOut: Out.Audio = Out.Audio(bus: 2, source: refOsc) let refGraph: SynthGraph = makeSynthGraph(engine: engine) _ = refOut.calculatedSamples(graph: refGraph, count: count) let refSamples: [Float] = (0..