405 lines
19 KiB
Swift
405 lines
19 KiB
Swift
// BusAllocatorTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("BusAllocator")
|
||
struct BusAllocatorTests {
|
||
|
||
// makeTestEngine: 2 out + 2 in → hardware audio buses 0–3 are reserved
|
||
// First allocatable audio bus is 4.
|
||
|
||
// MARK: - AudioBus allocation
|
||
|
||
@Test func allocateAudioBusReturnsBusAfterHardwareReservation() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2)
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(bus.index >= 4, "First allocatable bus must be after hardware buses 0–3")
|
||
#expect(bus.channelCount == 1)
|
||
}
|
||
|
||
@Test func allocateAudioBusContiguousMultiChannel() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2)
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 4)
|
||
#expect(bus.channelCount == 4)
|
||
// All 4 channels must be contiguous starting at bus.index
|
||
let bus2: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(bus2.index == bus.index + 4, "Next allocation must start right after the 4-channel block")
|
||
}
|
||
|
||
@Test func releaseAudioBusAllowsReuse() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2)
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let idx: Int = bus.index
|
||
engine.busAllocator.releaseAudioBus(bus)
|
||
let reused: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(reused.index == idx, "Released bus must be reused on next allocation")
|
||
}
|
||
|
||
@Test func hardwareBusesArePreReserved() throws {
|
||
// With 2 out + 2 in, buses 0–3 are hardware. Allocating a 4-channel block at index 0
|
||
// must not succeed — the allocator should skip past the reserved range.
|
||
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2)
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(bus.index >= 4, "Allocator must not return hardware buses 0–3")
|
||
}
|
||
|
||
@Test func allocateAudioBusThrowsWhenPoolExhausted() throws {
|
||
// Small pool: 4 buses, all reserved as hardware → no free buses remain
|
||
let engine: SynthesisEngine = makeTestEngine(
|
||
numOutputChannels: 2,
|
||
numInputChannels: 2,
|
||
numAudioBuses: 4
|
||
)
|
||
#expect(throws: BusAllocatorError.noFreeBus(channelCount: 1)) {
|
||
try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
}
|
||
}
|
||
|
||
@Test func allocateAudioBusThrowsWhenNoContiguousBlock() throws {
|
||
// Pool of 6 with 4 hardware → 2 free (buses 4 and 5). Requesting 3 must throw.
|
||
let engine: SynthesisEngine = makeTestEngine(
|
||
numOutputChannels: 2,
|
||
numInputChannels: 2,
|
||
numAudioBuses: 6
|
||
)
|
||
#expect(throws: BusAllocatorError.noFreeBus(channelCount: 3)) {
|
||
try engine.busAllocator.allocateAudioBus(channelCount: 3)
|
||
}
|
||
}
|
||
|
||
// MARK: - ControlBus allocation
|
||
|
||
@Test func allocateControlBusRoundTrip() throws {
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
#expect(bus.index >= 0 && bus.index < engine.numControlBuses)
|
||
#expect(bus.channelCount == 1)
|
||
engine.busAllocator.releaseControlBus(bus)
|
||
let reused: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
#expect(reused.index == bus.index, "Released control bus must be reused on next allocation")
|
||
}
|
||
|
||
@Test func allocateControlBusThrowsWhenPoolExhausted() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(numControlBuses: 2)
|
||
let b1: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
let b2: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
_ = b1; _ = b2
|
||
#expect(throws: BusAllocatorError.noFreeBus(channelCount: 1)) {
|
||
try engine.busAllocator.allocateControlBus()
|
||
}
|
||
}
|
||
|
||
@Test func allocateControlBusMultiChannelContiguous() throws {
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 3)
|
||
#expect(bus.channelCount == 3)
|
||
let next: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 1)
|
||
#expect(next.index == bus.index + 3, "Next allocation must start right after the 3-channel block")
|
||
}
|
||
|
||
@Test func allocateControlBusMultiChannelReleaseAllowsReuse() throws {
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 2)
|
||
let start: Int = bus.index
|
||
engine.busAllocator.releaseControlBus(bus)
|
||
let reused: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 2)
|
||
#expect(reused.index == start, "Released multi-channel control bus must be reused")
|
||
}
|
||
|
||
@Test func allocateControlBusThrowsWhenNoContiguousBlock() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(numControlBuses: 3)
|
||
// Allocate buses 0 and 2, leave bus 1 free → no 2-channel contiguous block starting at 0 or 2
|
||
let b0: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 1)
|
||
let b1: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 1)
|
||
let b2: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 1)
|
||
engine.busAllocator.releaseControlBus(b1)
|
||
_ = b0; _ = b2
|
||
#expect(throws: BusAllocatorError.noFreeBus(channelCount: 2)) {
|
||
try engine.busAllocator.allocateControlBus(channelCount: 2)
|
||
}
|
||
}
|
||
|
||
@Test func multiChannelControlBusInOutRoundTrip() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 2)
|
||
let src: Pan2 = Pan2(input: ConstantGeneratorInput(1.0 as Double), pan: 0.0)
|
||
let outGen: Out.Control = Out.Control(bus: bus, source: src)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
// Pan2 at pan=0 gives equal power split; both control buses should be non-zero
|
||
let v0: Float = engine.controlBuses[bus.index]
|
||
let v1: Float = engine.controlBuses[bus.index + 1]
|
||
#expect(v0 > 0, "Control bus channel 0 must receive Pan2 left output")
|
||
#expect(v1 > 0, "Control bus channel 1 must receive Pan2 right output")
|
||
|
||
// In.Control(bus:) with multi-channel ControlBus reads both channels
|
||
let inGen: In.Control = In.Control(bus: bus)
|
||
#expect(inGen.channels == 2, "In.Control(bus: ControlBus) must inherit channelCount")
|
||
}
|
||
|
||
// MARK: - In/Out overloads with AudioBus / ControlBus
|
||
|
||
@Test func inAudioWithAudioBusReadsCorrectBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
// Write a known value to the allocated bus
|
||
let expected: Float = 0.75
|
||
for i: Int in 0..<engine.blockSize {
|
||
engine.audioBuses[bus.index][i] = expected
|
||
}
|
||
|
||
let gen: In.Audio = In.Audio(bus: bus)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
#expect(out.count == 1)
|
||
#expect(out[0].prefix(engine.blockSize).allSatisfy { abs($0 - Double(expected)) < 1e-6 },
|
||
"In.Audio(bus: AudioBus) must read from the allocated bus index")
|
||
}
|
||
|
||
@Test func outAudioWithAudioBusWritesCorrectBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let signal: ConstantGeneratorInput = ConstantGeneratorInput(0.5 as Double)
|
||
|
||
let outGen: Out.Audio = Out.Audio(bus: bus, source: signal)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
let busValue: Float = Float(engine.audioBuses[bus.index][0])
|
||
#expect(abs(busValue - 0.5) < 1e-4,
|
||
"Out.Audio(bus:AudioBus, source:) must write to the allocated bus index")
|
||
}
|
||
|
||
@Test func inControlWithControlBusReadsCorrectBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
engine.controlBuses[bus.index] = 0.42
|
||
|
||
let gen: In.Control = In.Control(bus: bus)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let fv: Double = gen.floatValue(graph: graph)
|
||
#expect(abs(fv - 0.42) < 1e-5,
|
||
"In.Control(bus: ControlBus) must read the current engine control bus value")
|
||
}
|
||
|
||
@Test func outAudioChannelClampingPreventsAdjacentBusWrite() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
// Allocate a 1-channel bus; the next bus is separately allocated so a spill would corrupt it.
|
||
let bus1ch: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let adjacentBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(adjacentBus.index == bus1ch.index + 1, "Adjacent allocation must be contiguous")
|
||
|
||
// 2-channel source writing to a 1-channel AudioBus — channel 1 must be clamped.
|
||
let stereoSource: Pan2 = Pan2(input: ConstantGeneratorInput(1.0 as Double), pan: 0.0)
|
||
let outGen: Out.Audio = Out.Audio(bus: bus1ch, source: stereoSource)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
let adjacentSum: Float = engine.audioBuses[adjacentBus.index].prefix(engine.blockSize).reduce(0, +)
|
||
#expect(adjacentSum == 0,
|
||
"Out.Audio(bus:AudioBus, source:) must not write beyond bus.channelCount")
|
||
}
|
||
|
||
@Test func outControlWithControlBusWritesCorrectBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
let signal: ConstantGeneratorInput = ConstantGeneratorInput(0.77 as Double)
|
||
|
||
let outGen: Out.Control = Out.Control(bus: bus, source: signal)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
let busValue: Float = engine.controlBuses[bus.index]
|
||
#expect(abs(busValue - 0.77) < 1e-4,
|
||
"Out.Control(bus:ControlBus, source:) must write to the allocated control bus index")
|
||
}
|
||
|
||
@Test func multiChannelAudioBusConvenienceInit() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 2)
|
||
engine.audioBuses[bus.index][0] = 0.1
|
||
engine.audioBuses[bus.index + 1][0] = 0.9
|
||
|
||
let gens: GeneratorArray = In.Audio.multi(bus: bus)
|
||
#expect(gens.generators.count == 2, "In.Audio.multi(bus:) must return one generator per channel")
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let ch0: [[Double]] = gens[0].calculatedSamples(graph: graph, count: 1)
|
||
advanceBlock(engine)
|
||
let ch1: [[Double]] = gens[1].calculatedSamples(graph: graph, count: 1)
|
||
#expect(abs(ch0[0][0] - 0.1) < 1e-5, "Channel 0 must read bus.index")
|
||
#expect(abs(ch1[0][0] - 0.9) < 1e-5, "Channel 1 must read bus.index + 1")
|
||
}
|
||
|
||
// MARK: - ValidBus protocol
|
||
|
||
@Test func intConformsToValidBusWithIndexSelf() {
|
||
let bus: any ValidBus = 7
|
||
#expect(bus.index == 7)
|
||
#expect(bus.channelCount == Int.max, "Int bus must report Int.max channelCount")
|
||
}
|
||
|
||
@Test func audioBusConformsToValidBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let ab: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 3)
|
||
let vb: any ValidBus = ab
|
||
#expect(vb.index == ab.index)
|
||
#expect(vb.channelCount == 3)
|
||
}
|
||
|
||
@Test func controlBusConformsToValidBus() throws {
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let cb: ControlBus = try engine.busAllocator.allocateControlBus(channelCount: 2)
|
||
let vb: any ValidBus = cb
|
||
#expect(vb.index == cb.index)
|
||
#expect(vb.channelCount == 2)
|
||
}
|
||
|
||
@Test func inAudioWithIntBusSingleChannel() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 32)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
let gen: In.Audio = In.Audio(bus: 4 as Int)
|
||
#expect(gen.channels == 1, "Int bus must produce 1-channel In.Audio")
|
||
#expect(gen.bus == 4)
|
||
}
|
||
|
||
@Test func inControlWithIntBusSingleChannel() {
|
||
let gen: In.Control = In.Control(bus: 10 as Int)
|
||
#expect(gen.channels == 1, "Int bus must produce 1-channel In.Control")
|
||
#expect(gen.bus == 10)
|
||
}
|
||
|
||
@Test func outAudioIntBusWritesAllSourceChannels() throws {
|
||
// Int bus has channelCount = Int.max, so all source channels must pass through.
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 32)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let stereo: Pan2 = Pan2(input: ConstantGeneratorInput(1.0 as Double), pan: 0.0)
|
||
// Write to buses 4 and 5 via plain Int; both should be non-zero.
|
||
let outGen: Out.Audio = Out.Audio(bus: 4 as Int, source: stereo)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
let sum4: Float = engine.audioBuses[4].prefix(engine.blockSize).reduce(0, +)
|
||
let sum5: Float = engine.audioBuses[5].prefix(engine.blockSize).reduce(0, +)
|
||
#expect(sum4 > 0, "Int bus must not clamp channel 0")
|
||
#expect(sum5 > 0, "Int bus must not clamp channel 1 (no channelCount limit)")
|
||
}
|
||
|
||
@Test func outAudioAudioBusClampsToChannelCount() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 32)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let bus1ch: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let adjacent: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
#expect(adjacent.index == bus1ch.index + 1)
|
||
|
||
let stereo: Pan2 = Pan2(input: ConstantGeneratorInput(1.0 as Double), pan: 0.0)
|
||
let outGen: Out.Audio = Out.Audio(bus: bus1ch, source: stereo)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = outGen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||
|
||
let spillSum: Float = engine.audioBuses[adjacent.index].prefix(engine.blockSize).reduce(0, +)
|
||
#expect(spillSum == 0, "AudioBus channelCount must clamp writes — adjacent bus must be silent")
|
||
}
|
||
|
||
@Test func synthesizerBaseOutputAcceptsAudioBus() throws {
|
||
final class BusSynth: SynthesizerBase {
|
||
let bus: AudioBus
|
||
init(bus: AudioBus) { self.bus = bus; super.init() }
|
||
override func build() {
|
||
output(bus: bus, source: 0.5 as Double)
|
||
}
|
||
}
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let ab: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let synth: BusSynth = BusSynth(bus: ab)
|
||
try engine.add(synth)
|
||
engine.processBlock(inputSamples: [])
|
||
#expect(abs(engine.audioBuses[ab.index][0] - 0.5) < 1e-6,
|
||
"output(bus: AudioBus, source:) must write to the allocated bus")
|
||
}
|
||
|
||
@Test func synthesizerBaseControlOutputAcceptsControlBus() throws {
|
||
final class CtrlSynth: SynthesizerBase {
|
||
let bus: ControlBus
|
||
init(bus: ControlBus) { self.bus = bus; super.init() }
|
||
override func build() {
|
||
controlOutput(bus: bus, source: 0.33 as Double)
|
||
}
|
||
}
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let cb: ControlBus = try engine.busAllocator.allocateControlBus()
|
||
let synth: CtrlSynth = CtrlSynth(bus: cb)
|
||
try engine.add(synth)
|
||
engine.processBlock(inputSamples: [])
|
||
#expect(abs(engine.controlBuses[cb.index] - 0.33) < 1e-6,
|
||
"controlOutput(bus: ControlBus, source:) must write to the allocated bus")
|
||
}
|
||
|
||
@Test func synthesizerBaseInputAcceptsAudioBus() throws {
|
||
// Write 0.7 to bus, then read it back via input(bus: AudioBus)
|
||
final class ReadSynth: SynthesizerBase {
|
||
let src: Int; let dst: AudioBus
|
||
init(src: Int, dst: AudioBus) { self.src = src; self.dst = dst; super.init() }
|
||
override func build() {
|
||
output(bus: dst, source: input(bus: src))
|
||
}
|
||
}
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||
try engine.start()
|
||
defer { engine.stop() }
|
||
|
||
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||
// Pre-seed srcBus via another synth that writes 0.7
|
||
final class WriteSynth: SynthesizerBase {
|
||
let b: AudioBus; init(b: AudioBus) { self.b = b; super.init() }
|
||
override func build() { output(bus: b, source: 0.7 as Double) }
|
||
}
|
||
// engine.add uses addToHead: last-added evaluates first.
|
||
// Add reader first (tail), then writer (head) so writer evaluates before reader.
|
||
try engine.add(ReadSynth(src: srcBus.index, dst: dstBus)) // reader first → tail
|
||
try engine.add(WriteSynth(b: srcBus)) // writer last → head
|
||
engine.processBlock(inputSamples: [])
|
||
#expect(abs(engine.audioBuses[dstBus.index][0] - 0.7) < 1e-5,
|
||
"input(bus: AudioBus) must read from the allocated bus")
|
||
}
|
||
}
|