// BusAllocatorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @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.. 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") } }