1.0.0 release
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
// 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.
|
||||
@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..<count).map { engine.audioBuses[2][$0] }
|
||||
|
||||
advanceBlock(engine)
|
||||
// Clear buses for the fan-out test
|
||||
for i: Int in 0..<count {
|
||||
engine.audioBuses[0][i] = 0.0
|
||||
engine.audioBuses[1][i] = 0.0
|
||||
}
|
||||
|
||||
// Fan-out: same osc → two Out generators
|
||||
let fanOsc: OscSin.Audio = OscSin.Audio(freq: freq) // fresh instance, same initial phase (0)
|
||||
let fanOutA: Out.Audio = Out.Audio(bus: 0, source: fanOsc)
|
||||
let fanOutB: Out.Audio = Out.Audio(bus: 1, source: fanOsc)
|
||||
let fanGraph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = fanOutA.calculatedSamples(graph: fanGraph, count: count)
|
||||
_ = fanOutB.calculatedSamples(graph: fanGraph, count: count)
|
||||
let fanSamples: [Float] = (0..<count).map { engine.audioBuses[0][$0] }
|
||||
|
||||
// Fan-out output must match the single-consumer reference — no phase doubling.
|
||||
for i: Int in 0..<count {
|
||||
#expect(abs(fanSamples[i] - refSamples[i]) < 1e-12,
|
||||
"sample \(i): fan=\(fanSamples[i]) ref=\(refSamples[i])")
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Fan-out via SynthesizerBase (integration path)
|
||||
|
||||
@Test func engineEvaluateSegmentIncreasesSerial() throws {
|
||||
// processBlock → evaluateSegment → segmentSerial &+= 1
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
try engine.start()
|
||||
let serialBefore: UInt64 = engine.segmentSerial
|
||||
engine.processBlock(inputSamples: [])
|
||||
#expect(engine.segmentSerial == serialBefore + 1)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func fanOutViaProcessBlockProducesSameSamplesOnBothBuses() throws {
|
||||
// A Synthesizer that wires one OscSin to two buses through the real evaluateSegment
|
||||
// path — proves serial increment + fan-out caching work end-to-end.
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
try engine.start()
|
||||
|
||||
final class FanOutSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: 440.0)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: osc))
|
||||
addOutputGenerator(Out.Audio(bus: 1, source: osc))
|
||||
}
|
||||
}
|
||||
|
||||
let synth: FanOutSynth = FanOutSynth()
|
||||
try engine.add(synth)
|
||||
engine.processBlock(inputSamples: [])
|
||||
|
||||
let blockSize: Int = engine.blockSize
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(engine.audioBuses[0][i] == engine.audioBuses[1][i],
|
||||
"sample \(i): bus0=\(engine.audioBuses[0][i]) bus1=\(engine.audioBuses[1][i])")
|
||||
}
|
||||
|
||||
// Buses must not be all-zero (signal is actually running)
|
||||
let anyNonZero: Bool = (0..<blockSize).contains { engine.audioBuses[0][$0] != 0.0 }
|
||||
#expect(anyNonZero)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - floatValue → calculatedSamples single-evaluation guarantee
|
||||
|
||||
@Test func floatValuePrimesFullBlockCache() {
|
||||
// floatValue routes through calculatedSamples(count: blockSize), priming the cache.
|
||||
// A subsequent calculatedSamples(count ≤ blockSize) in the same segment must be a
|
||||
// cache hit — no second state-advance.
|
||||
let blockSize: Int = 8
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// freq=0, phase=0.25 → constant 1.0 (phase never changes — any double-advance is invisible)
|
||||
let constOsc: OscSin.Audio = OscSin.Audio(freq: 0.0, phase: 0.25)
|
||||
let fv: Double = constOsc.floatValue(graph: graph)
|
||||
#expect(abs(fv - 1.0) < 1e-6)
|
||||
// Should hit cache — same result
|
||||
let result: [[Double]] = constOsc.calculatedSamples(graph: graph, count: 4)
|
||||
#expect(result[0].prefix(4).allSatisfy { abs($0 - 1.0) < 1e-6 })
|
||||
}
|
||||
|
||||
@Test func floatValueThenCalculatedSamplesDoesNotDoubleAdvancePhase() throws {
|
||||
// Proves the floatValue-then-calculatedSamples case does NOT double the phase advance.
|
||||
// Strategy: compare a generator that had floatValue + calculatedSamples called against
|
||||
// a reference that had only calculatedSamples called. Both must produce the same output.
|
||||
let blockSize: Int = 16
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
|
||||
let freq: Double = 440.0
|
||||
|
||||
// Reference: only calculatedSamples, no floatValue call.
|
||||
let refOsc: OscSin.Audio = OscSin.Audio(freq: freq)
|
||||
let refGraph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let refResult: [[Double]] = refOsc.calculatedSamples(graph: refGraph, count: blockSize)
|
||||
|
||||
advanceBlock(engine)
|
||||
|
||||
// Subject: floatValue called first (primes cache), then calculatedSamples.
|
||||
let subjOsc: OscSin.Audio = OscSin.Audio(freq: freq)
|
||||
let subjGraph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = subjOsc.floatValue(graph: subjGraph) // primes cache via calculatedSamples(blockSize)
|
||||
let subjResult: [[Double]] = subjOsc.calculatedSamples(graph: subjGraph, count: blockSize)
|
||||
|
||||
// Results must be bit-identical — same starting phase, same frequency, single evaluation.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(refResult[0][i] - subjResult[0][i]) < 1e-12,
|
||||
"double-advance at sample \(i): ref=\(refResult[0][i]) subj=\(subjResult[0][i])")
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user