Files
Lyrebird/Tests/LyrebirdTests/ParallelGroupTests.swift

390 lines
14 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ParallelGroupTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// ParallelGroup tests verify:
// - All children are evaluated each segment
// - Multiple children writing to the same bus sum correctly (polyphony)
// - Children writing to different buses are independent
// - Results match sequential (Group) evaluation
// - Integration with engine.processBlock
// MARK: - Helpers
/// Synthesizer that writes a constant Double value to a single audio bus.
/// Wires the literal directly into `Out.Audio` — flat from the first block, no ramp.
/// Exercises the SynthesizerBase → scratchBuses forwarding path.
private final class ConstantBusSynth: SynthesizerBase {
let value: Double
let bus: Int
init(value: Double, bus: Int = 0) {
self.value = value
self.bus = bus
super.init()
}
override func build() {
addOutputGenerator(Out.Audio(bus: bus, source: value))
}
}
/// Synthesizer that records every evaluate() call (count and graph provided).
private final class RecordingSynth: Synthesizer {
var callCount: Int = 0
var lastCount: Int = 0
func build() {}
func evaluate(graph: SynthGraph, count: Int) {
callCount += 1
lastCount = count
}
}
// MARK: - Tests
@MainActor
@Suite("ParallelGroup")
struct ParallelGroupTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 4, numControlBuses: 16)
// MARK: - Structure
@Test func emptyGroupHasCountZero() {
let group: ParallelGroup = ParallelGroup(engine: engine)
#expect(group.count == 0)
}
@Test func emptyGroupIsEmpty() {
let group: ParallelGroup = ParallelGroup(engine: engine)
#expect(group.isEmpty == true)
}
@Test func emptyGroupEvaluatesSilently() {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.build()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
group.evaluate(graph: graph, count: 8)
// No crash, no assertion: success
}
@Test func addingOneChildIncrementsCount() {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(RecordingSynth())
#expect(group.count == 1)
}
@Test func addingMultipleChildrenIncrementsCount() {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(RecordingSynth())
group.add(RecordingSynth())
group.add(RecordingSynth())
#expect(group.count == 3)
}
@Test func addingChildMakesGroupNonEmpty() {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(RecordingSynth())
#expect(group.isEmpty == false)
}
// MARK: - All children evaluated
@Test func allChildrenAreEvaluated() throws {
let group: ParallelGroup = ParallelGroup(engine: engine)
let a: RecordingSynth = RecordingSynth()
let b: RecordingSynth = RecordingSynth()
let c: RecordingSynth = RecordingSynth()
group.add(a)
group.add(b)
group.add(c)
group.build()
try engine.start()
defer { engine.stop() }
engine.audioBuses = [[Float]](
repeating: [Float](repeating: 0, count: engine.blockSize),
count: engine.numAudioBuses
)
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
group.evaluate(graph: graph, count: 8)
#expect(a.callCount == 1)
#expect(b.callCount == 1)
#expect(c.callCount == 1)
}
@Test func evaluatePassesCorrectCountToChildren() throws {
let group: ParallelGroup = ParallelGroup(engine: engine)
let node: RecordingSynth = RecordingSynth()
group.add(node)
group.build()
try engine.start()
defer { engine.stop() }
engine.audioBuses = [[Float]](
repeating: [Float](repeating: 0, count: engine.blockSize),
count: engine.numAudioBuses
)
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
group.evaluate(graph: graph, count: 5)
#expect(node.lastCount == 5)
}
@Test func evaluateCalledTwiceCallsChildrenTwice() throws {
let group: ParallelGroup = ParallelGroup(engine: engine)
let node: RecordingSynth = RecordingSynth()
group.add(node)
group.build()
try engine.start()
defer { engine.stop() }
engine.audioBuses = [[Float]](
repeating: [Float](repeating: 0, count: engine.blockSize),
count: engine.numAudioBuses
)
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
group.evaluate(graph: graph, count: 8)
group.evaluate(graph: graph, count: 8)
#expect(node.callCount == 2)
}
// MARK: - Bus accumulation (polyphony)
@Test func singleChildWritesCorrectValueToBus() throws {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 3.0, bus: 0))
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 3.0) < 1e-5 })
}
@Test func twoChildrenSameBusSumCorrectly() throws {
// Both children write 1.0 to bus 0 → result should be 2.0
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
group.add(ConstantBusSynth(value: 1.0, bus: 0))
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 2.0) < 1e-5 })
}
@Test func eightVoicesPolyphonicAccumulation() throws {
// 8 voices × 0.5 → bus 0 = 4.0
let group: ParallelGroup = ParallelGroup(engine: engine)
for _ in 0..<8 {
group.add(ConstantBusSynth(value: 0.5, bus: 0))
}
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 4.0) < 1e-4 })
}
@Test func childrenWriteToDifferentBusesAreIndependent() throws {
// Child A → bus 0 = 1.0; Child B → bus 1 = 2.0; Child C → bus 2 = 3.0
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
group.add(ConstantBusSynth(value: 2.0, bus: 1))
group.add(ConstantBusSynth(value: 3.0, bus: 2))
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
let bus1: [Float] = engine.audioBuses[1]
let bus2: [Float] = engine.audioBuses[2]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 1.0) < 1e-5 })
#expect(bus1.prefix(8).allSatisfy { abs($0 - 2.0) < 1e-5 })
#expect(bus2.prefix(8).allSatisfy { abs($0 - 3.0) < 1e-5 })
}
@Test func resultMatchesSequentialGroupEvaluation() throws {
// Compare: ParallelGroup with N synths vs Group with same N synths
// Both should produce the same bus output
let N: Int = 4
let value: Double = 1.5
// Sequential engine
let seqEngine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 4, numControlBuses: 16)
let seqGroup: Group = Group()
for _ in 0..<N {
let s: ConstantBusSynth = ConstantBusSynth(value: value, bus: 0)
s.build()
seqGroup.addToTail(PreparedNode(s))
}
try seqEngine.start()
defer { seqEngine.stop() }
try seqEngine.add(seqGroup)
seqEngine.processBlock(inputSamples: [])
let seqResult: [Float] = Array(seqEngine.audioBuses[0].prefix(8))
// Parallel engine
let parEngine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 4, numControlBuses: 16)
let parGroup: ParallelGroup = ParallelGroup(engine: parEngine)
for _ in 0..<N {
parGroup.add(ConstantBusSynth(value: value, bus: 0))
}
try parEngine.start()
defer { parEngine.stop() }
try parEngine.add(parGroup)
parEngine.processBlock(inputSamples: [])
let parResult: [Float] = Array(parEngine.audioBuses[0].prefix(8))
for (s, p) in zip(seqResult, parResult) {
#expect(abs(s - p) < 1e-4)
}
}
// MARK: - Partial-block (busOffset > 0)
@Test func partialBlockEvaluationWritesToCorrectOffset() throws {
// evaluate with count=4 and busOffset=4 → only samples [4..7] should be non-zero
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
group.build() // must call before direct evaluate()
try engine.start()
defer { engine.stop() }
// Bus 0 is already zeroed by engine.start(); direct-evaluate at offset 4.
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 4)
group.evaluate(graph: graph, count: 4)
// Samples 0-3 untouched (engine zeroed them at start); samples 4-7 should be 1.0
for i: Int in 0..<4 {
#expect(engine.audioBuses[0][i] == 0.0, "sample \(i) should be 0")
}
for i: Int in 4..<8 {
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-5, "sample \(i) should be 1.0")
}
}
// MARK: - Integration: ParallelGroup added to engine via add()
@Test func parallelGroupInEngineProducesOutput() throws {
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 2.5, bus: 0))
group.add(ConstantBusSynth(value: 0.5, bus: 0))
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 3.0) < 1e-4 })
}
@Test func dirtyMaskWipesScratchBetweenBlocks() throws {
// Regression test for ScratchBuses.dirty correctness across blocks.
//
// After block 1, scratch[bus 0] holds 1.0 for every sample and dirty[0]=true.
// At the start of block 2, ParallelGroup.evaluate must call clearDirtyBuses,
// which must vDSP_vclr scratch[0] at the segment range before children write.
// If the dirty mask path failed to actually wipe scratch (e.g. reset the mask
// without clearing first), accumulate would add 1.0 atop the existing 1.0,
// the merge would push 2.0 to engine.audioBuses[0], and this test would fail.
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
try engine.start()
defer { engine.stop() }
try engine.add(group)
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
// Engine clears audioBuses at start of each processBlock, then ParallelGroup
// merges scratch. If scratch carried stale data, we'd see 2.0 here.
#expect(bus0.prefix(8).allSatisfy { abs($0 - 1.0) < 1e-5 },
"block 2 should produce 1.0 — scratch must be wiped between blocks")
}
@Test func dirtyMaskHandlesMultiSegmentEvaluation() throws {
// Direct-evaluate test: two segments in the same block at different
// busOffsets exercise the dirty-mask persistence path. Segment 1 writes
// to bus 0 at [0..4]; segment 2 writes to bus 0 at [4..8]. Between
// segments, clearDirtyBuses must wipe scratch[0] at segment 2's range
// (already zero — defensive wipe) and the mask must remain consistent
// so segment 2's merge correctly contributes bus 0 to engine.audioBuses.
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
group.build()
try engine.start()
defer { engine.stop() }
// Bus 0 starts cleared by engine.start(). The fan-out cache keys off
// `engine.segmentSerial`; bump it between segments so the second
// evaluate's Out._computeSamples actually runs (otherwise the cached
// segment-1 result is returned and no second bus write occurs —
// mimics what `evaluateSegment` does in production processBlock).
engine.advanceSegmentSerial()
let graphSeg1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
group.evaluate(graph: graphSeg1, count: 4)
engine.advanceSegmentSerial()
let graphSeg2: SynthGraph = SynthGraph(engine: engine, busOffset: 4)
group.evaluate(graph: graphSeg2, count: 4)
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-5,
"sample \(i) should be 1.0 after both segments")
}
}
@Test func parallelGroupCoexistsWithOtherSynthsInRootGroup() throws {
// ParallelGroup contributes 2.0 to bus 0; a plain ConstantSynth adds 1.0
let group: ParallelGroup = ParallelGroup(engine: engine)
group.add(ConstantBusSynth(value: 1.0, bus: 0))
group.add(ConstantBusSynth(value: 1.0, bus: 0))
let plain: ConstantBusSynth = ConstantBusSynth(value: 1.0, bus: 0)
try engine.start()
defer { engine.stop() }
try engine.add(group)
try engine.add(plain)
engine.processBlock(inputSamples: [])
// 2.0 (parallel) + 1.0 (plain) = 3.0
let bus0: [Float] = engine.audioBuses[0]
#expect(bus0.prefix(8).allSatisfy { abs($0 - 3.0) < 1e-4 })
}
}