Files
Lyrebird/Tests/LyrebirdTests/SampleLoopTests.swift
T

208 lines
9.3 KiB
Swift

// 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..<n {
for ch: Int in 0..<numChannels {
let y: Double = ins[0][ch][i] + a * z[ch]
z[ch] = y
out[ch][i] = y
}
}
}
let block: [Double] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8)[0]
#expect(abs(block[0] - 1.0) < 1e-12) // 1 + 0.5·0
#expect(abs(block[1] - 1.5) < 1e-12) // 1 + 0.5·1
#expect(abs(block[2] - 1.75) < 1e-12) // 1 + 0.5·1.5
#expect(block[7] > 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..<n {
for ch: Int in 0..<numChannels {
let y: Double = ins[0][ch][i] + 0.5 * z[ch]
z[ch] = y
out[ch][i] = y
}
}
}
let block1: [Double] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)[0]
advanceBlock(engine)
let block2: [Double] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)[0]
#expect(abs(block2[0] - (1.0 + 0.5 * block1[3])) < 1e-12)
#expect(abs(node.state[0] - block2[3]) < 1e-12)
}
/// Output channel count is inferred from the widest input, and a mono input broadcasts
/// to every output channel.
@Test func infersChannelsAndBroadcastsMonoInput() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
try engine.start()
let stereoIn: Pan2 = Pan2(input: 1.0, pan: 0.0) // 2-row generator
let node: SampleLoop<Int> = SampleLoop(
makeState: { _ in 0 },
inputs: [stereoIn, 3.0] // stereo + mono scalar
) { _, ins, numChannels, out, n, _ in
for i: Int in 0..<n {
for ch: Int in 0..<numChannels {
out[ch][i] = ins[0][ch][i] + ins[1][ch][i] // mono input 1 broadcast to both
}
}
}
#expect(node.outputChannelCount == 2)
let result: [[Double]] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)
#expect(result.count == 2)
// Pan2 at pan 0 → equal-power ≈ 0.707 per channel; + 3.0 broadcast mono.
#expect(abs(result[0][0] - (result[1][0])) < 1e-9) // symmetric channels
#expect(result[0][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<Int> = SampleLoop(
makeState: { _ in 0 },
inputs: [],
channels: 3
) { _, _, numChannels, out, n, _ in
for ch: Int in 0..<numChannels {
for i: Int in 0..<n { out[ch][i] = Double(ch) }
}
}
#expect(node.outputChannelCount == 3)
let result: [[Double]] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)
#expect(result.count == 3)
#expect(result[0][0] == 0.0 && result[1][0] == 1.0 && result[2][0] == 2.0)
}
/// Two instances keep independent state (graph-copy-per-instance model).
@Test func instancesAreIndependent() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
try engine.start()
func makeCounter() -> SampleLoop<[Double]> {
SampleLoop(makeState: { n in [Double](repeating: 0.0, count: n) }, inputs: []) { s, _, _, out, n, _ in
for i: Int in 0..<n {
s[0] += 1.0
out[0][i] = s[0]
}
}
}
let a: SampleLoop<[Double]> = 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<Int> = SampleLoop(
makeState: { _ in 0 },
inputs: [3.0],
mul: 2.0,
add: 1.0
) { _, ins, numChannels, out, n, _ in
for i: Int in 0..<n {
for ch: Int in 0..<numChannels { out[ch][i] = ins[0][ch][i] } // 3.0
}
}
let block: [Double] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)[0]
#expect(Array(block.prefix(4)) == [7.0, 7.0, 7.0, 7.0]) // 3·2 + 1
}
/// Regression: a `Generator` input's own `_result` buffer must not be copied-on-write
/// across blocks. Aliasing the input row in a stored property (instead of copying into
/// private scratch) would leave the row referenced twice at the input's next in-place
/// write, reallocating its buffer — a per-block RT-thread allocation. A stable base
/// address across blocks proves no reallocation occurred.
@Test func generatorInputIsNotCopiedOnWriteAcrossBlocks() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
try engine.start()
let osc: OscSin.Audio = OscSin.Audio(freq: 100.0)
let node: SampleLoop<Int> = SampleLoop(makeState: { _ in 0 }, inputs: [osc]) { _, ins, nc, out, n, _ in
for i: Int in 0..<n {
for ch: Int in 0..<nc { out[ch][i] = ins[0][ch][i] }
}
}
func oscRowBase() -> 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<Int> = SampleLoop(makeState: { _ in 0 }, inputs: [0.5]) { _, ins, nc, out, n, _ in
for i: Int in 0..<n {
for ch: Int in 0..<nc { out[ch][i] = ins[0][ch][i] }
}
}
func outBase() -> 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<Int> = SampleLoop(makeState: { _ in 0 }, inputs: []) { call, _, _, out, n, _ in
let writeCount: Int = (call == 0) ? n : 1
for i: Int in 0..<writeCount { out[0][i] = 9.0 }
call += 1
}
_ = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)
advanceBlock(engine)
let block2: [Double] = node.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 4)[0]
#expect(Array(block2.prefix(4)) == [9.0, 0.0, 0.0, 0.0])
}
}