183 lines
7.8 KiB
Swift
183 lines
7.8 KiB
Swift
// OutGeneratorTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@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..<engine.blockSize {
|
||
#expect(engine.audioBuses[0][i] == 0.0)
|
||
}
|
||
}
|
||
|
||
@Test func respectsBusOffset() {
|
||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 4)
|
||
_ = gen.calculatedSamples(graph: graph, count: 4)
|
||
// Samples 0..3 should remain 0
|
||
for i: Int in 0..<4 {
|
||
#expect(engine.audioBuses[0][i] == 0.0)
|
||
}
|
||
// Samples 4..7 should be 0.5
|
||
for i: Int in 4..<8 {
|
||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||
}
|
||
}
|
||
|
||
@Test func writesToCorrectBus() {
|
||
let gen: Out.Audio = Out.Audio(bus: 3, source: ConstantAudioGen(0.7))
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||
// Bus 3 should have values
|
||
for i: Int in 0..<8 {
|
||
#expect(abs(engine.audioBuses[3][i] - 0.7) < 1e-6)
|
||
}
|
||
// Bus 0 should remain 0
|
||
for i: Int in 0..<8 {
|
||
#expect(engine.audioBuses[0][i] == 0.0)
|
||
}
|
||
}
|
||
|
||
@Test func passesThroughSamples() {
|
||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3))
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||
#expect(result[0].allSatisfy { abs($0 - 0.3) < 1e-6 })
|
||
}
|
||
}
|
||
|
||
@Suite("ControlOut")
|
||
struct ControlOutTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
let gen: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||
#expect(gen.rate == .control)
|
||
}
|
||
|
||
@Test func accumulatesIntoControlBus() {
|
||
let gen: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||
#expect(abs(engine.controlBuses[0] - 0.3) < 1e-6)
|
||
}
|
||
|
||
@Test func accumulatesAdditively() {
|
||
// Two independent ControlOut instances writing the same value must sum to 2×value.
|
||
// (Fan-out cache: same instance called twice in one segment is a no-op second call.)
|
||
let genA: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||
let genB: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = genA.calculatedSamples(graph: graph, count: 8)
|
||
_ = genB.calculatedSamples(graph: graph, count: 8)
|
||
#expect(abs(engine.controlBuses[0] - 0.6) < 1e-5)
|
||
}
|
||
|
||
@Test func writesToCorrectBus() {
|
||
let gen: Out.Control = Out.Control(bus: 5, source: 1.2 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||
#expect(abs(engine.controlBuses[5] - 1.2) < 1e-6)
|
||
#expect(engine.controlBuses[0] == 0.0)
|
||
}
|
||
|
||
/// Return value must be the source value, NOT the post-accumulated bus state.
|
||
/// If the return were a bus readback, a second call on a new block (e.g. sub-block
|
||
/// splitting) would return the already-accumulated total instead of the contribution.
|
||
@Test func returnsSourceValueNotBusReadback() {
|
||
let count: Int = 4
|
||
let gen: Out.Control = Out.Control(bus: 0, source: 0.4 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
// Block 1: writes 0.4 to bus, returns source value 0.4
|
||
let result1: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
// Advance serial to simulate a new engine block (sub-block split scenario)
|
||
advanceBlock(engine)
|
||
// Block 2: writes another 0.4 to bus, must return 0.4 not the bus total
|
||
let result2: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
// Both calls must return the source value (0.4), not the running bus total (0.4, 0.8).
|
||
// The pre-allocated buffer is larger than count; only the first `count` elements are valid.
|
||
#expect(result1[0].prefix(count).allSatisfy { abs($0 - 0.4) < 1e-6 })
|
||
#expect(result2[0].prefix(count).allSatisfy { abs($0 - 0.4) < 1e-6 })
|
||
// Bus has accumulated both contributions correctly.
|
||
#expect(abs(engine.controlBuses[0] - 0.8) < 1e-5)
|
||
}
|
||
|
||
/// Two independent ControlOut instances writing to the same bus sum correctly (polyphony).
|
||
@Test func twoSourcesAccumulateCorrectly() {
|
||
let genA: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||
let genB: Out.Control = Out.Control(bus: 0, source: 0.5 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = genA.calculatedSamples(graph: graph, count: 8)
|
||
_ = genB.calculatedSamples(graph: graph, count: 8)
|
||
#expect(abs(engine.controlBuses[0] - 0.8) < 1e-5)
|
||
}
|
||
|
||
/// Out.Control must write to the bus exactly once per audio block even when
|
||
/// controlBlockSize < blockSize. The ControlGenerator sub-block loop must be bypassed.
|
||
@Test func singleBusWritePerBlockInSubBlockMode() throws {
|
||
// blockSize=64, controlBlockSize=8 → 8 sub-blocks. Bus must accumulate 0.5 once, not 8×.
|
||
let subEngine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: 64, controlBlockSize: 8,
|
||
sampleRate: 44100,
|
||
numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try subEngine.start()
|
||
let gen: Out.Control = Out.Control(bus: 0, source: 0.5 as Double)
|
||
let graph: SynthGraph = makeSynthGraph(engine: subEngine)
|
||
_ = gen.calculatedSamples(graph: graph, count: 64)
|
||
#expect(abs(subEngine.controlBuses[0] - 0.5) < 1e-6)
|
||
}
|
||
}
|