// SampleLoopTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("SampleLoop") struct SampleLoopTests { /// y[n] = x[n] + a·y[n-1] with a constant input converges to x/(1-a). @Test func singlePoleFeedbackConverges() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) try engine.start() let a: Double = 0.5 let node: SampleLoop<[Double]> = SampleLoop( makeState: { n in [Double](repeating: 0.0, count: n) }, inputs: [1.0] ) { z, ins, numChannels, out, n, _ in for i: Int in 0.. 1.98 && block[7] < 2.0) // → 2.0 } /// Feedback state carries across block boundaries. @Test func statePersistsAcrossBlocks() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 4) try engine.start() let node: SampleLoop<[Double]> = SampleLoop( makeState: { n in [Double](repeating: 0.0, count: n) }, inputs: [1.0] ) { z, ins, numChannels, out, n, _ in for i: Int in 0.. = SampleLoop( makeState: { _ in 0 }, inputs: [stereoIn, 3.0] // stereo + mono scalar ) { _, ins, numChannels, out, n, _ in for i: Int in 0.. 3.0 && result[0][0] < 4.0) } /// An explicit `channels:` up-mixes a pure source with no inputs to infer from. @Test func explicitChannelsForPureSource() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 4) try engine.start() let node: SampleLoop = SampleLoop( makeState: { _ in 0 }, inputs: [], channels: 3 ) { _, _, numChannels, out, n, _ in for ch: Int in 0.. SampleLoop<[Double]> { SampleLoop(makeState: { n in [Double](repeating: 0.0, count: n) }, inputs: []) { s, _, _, out, n, _ in for i: Int in 0.. = makeCounter() let b: SampleLoop<[Double]> = makeCounter() _ = a.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4) advanceBlock(engine) _ = a.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4) // a advanced twice _ = b.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4) // b advanced once #expect(abs(a.state[0] - 8.0) < 1e-12) #expect(abs(b.state[0] - 4.0) < 1e-12) } /// `mul` / `add` scale the body's output after the fact. @Test func mulAddScalesOutput() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 4) try engine.start() let node: SampleLoop = SampleLoop( makeState: { _ in 0 }, inputs: [3.0], mul: 2.0, add: 1.0 ) { _, ins, numChannels, out, n, _ in for i: Int in 0.. = SampleLoop(makeState: { _ in 0 }, inputs: [osc]) { _, ins, nc, out, n, _ in for i: Int in 0.. UnsafeRawPointer? { osc._result[0].withUnsafeBufferPointer { UnsafeRawPointer($0.baseAddress) } } _ = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8) let base1: UnsafeRawPointer? = oscRowBase() advanceBlock(engine) _ = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8) let base2: UnsafeRawPointer? = oscRowBase() #expect(base1 != nil) #expect(base1 == base2) // no CoW reallocation of the input's buffer } /// Regression: the private input scratch and the output buffer are never reallocated /// across blocks (both pre-allocated at init). @Test func scratchAndOutputBuffersStableAcrossBlocks() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) try engine.start() let node: SampleLoop = SampleLoop(makeState: { _ in 0 }, inputs: [0.5]) { _, ins, nc, out, n, _ in for i: Int in 0.. UnsafeRawPointer? { node._result[0].withUnsafeBufferPointer { UnsafeRawPointer($0.baseAddress) } } _ = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8) let base1: UnsafeRawPointer? = outBase() advanceBlock(engine) _ = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8) let base2: UnsafeRawPointer? = outBase() #expect(base1 != nil) #expect(base1 == base2) } /// The output buffer is pre-zeroed: a body that writes only part of the block leaves /// silence in the gaps, never stale data from the previous block. @Test func outputPreZeroedBetweenBlocks() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 4) try engine.start() let node: SampleLoop = SampleLoop(makeState: { _ in 0 }, inputs: []) { call, _, _, out, n, _ in let writeCount: Int = (call == 0) ? n : 1 for i: Int in 0..