v1.2.4 release: bus-rate guard on Out/In/ReplaceOut constructors
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
// BusRateGuardTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
/// Guards the bus-rate check that keeps a typed bus from being handed to the wrong-rate bus I/O.
|
||||
///
|
||||
/// The failure mode this prevents: an audio-rate generator (`Out.Audio` / `In.Audio` /
|
||||
/// `ReplaceOut.Audio`) addresses the **audio**-bus pool by index, and a control-rate generator
|
||||
/// addresses the **control**-bus pool. Passing a `ControlBus` to an audio generator (or an
|
||||
/// `AudioBus` to a control generator) therefore writes/reads the *other* pool at that index —
|
||||
/// silently colliding with unrelated buses (a low control index lands on the hardware output
|
||||
/// buses). `Out.Audio(bus: someControlBus)` and the mirror now trap at construction via
|
||||
/// `_preconditionAudioRateBus` / `_preconditionControlRateBus`.
|
||||
///
|
||||
/// The traps themselves can't be exercised under Swift Testing (`precondition` terminates the
|
||||
/// process), so these tests instead confirm the *correct* pairings work and, crucially, that a
|
||||
/// control-rate write stays in the control pool — the exact "no leak into audio" property whose
|
||||
/// absence let a control write reach a hardware output channel.
|
||||
@MainActor
|
||||
@Suite("Bus rate guard")
|
||||
struct BusRateGuardTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// A control-rate `Out.Control` to a `ControlBus` writes the control pool and leaves the
|
||||
/// audio-bus pool at the same numeric index untouched — no cross-pool leak.
|
||||
@Test func controlOutWritesControlPoolNotAudio() {
|
||||
let controlIndex: Int = 0
|
||||
let value: Double = 0.5
|
||||
let gen: Out.Control = Out.Control(
|
||||
bus: ControlBus(index: controlIndex, channelCount: 1),
|
||||
source: ConstantControlGen(value)
|
||||
)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
|
||||
#expect(abs(Double(engine.controlBuses[controlIndex]) - value) < 1e-6,
|
||||
"Out.Control must write the control-bus pool")
|
||||
for i: Int in 0..<8 {
|
||||
#expect(engine.audioBuses[controlIndex][i] == 0,
|
||||
"Out.Control must not touch the audio-bus pool at the same index")
|
||||
}
|
||||
}
|
||||
|
||||
/// An audio-rate `Out.Audio` to a typed `AudioBus` still writes the audio pool (the typed
|
||||
/// audio bus passes the guard).
|
||||
@Test func audioOutWritesAudioPool() {
|
||||
let audioIndex: Int = 3
|
||||
let value: Double = 0.25
|
||||
let gen: Out.Audio = Out.Audio(
|
||||
bus: AudioBus(index: audioIndex, channelCount: 1),
|
||||
source: ConstantAudioGen(value)
|
||||
)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[audioIndex][i] - Float(value)) < 1e-6,
|
||||
"Out.Audio must write the audio-bus pool")
|
||||
}
|
||||
}
|
||||
|
||||
/// `In.Control` from a `ControlBus` reads the control pool (the typed control bus passes the
|
||||
/// guard and resolves to the control-bus address space).
|
||||
@Test func inControlReadsControlPool() {
|
||||
let controlIndex: Int = 2
|
||||
let value: Float = 0.75
|
||||
engine.controlBuses[controlIndex] = value
|
||||
let gen: In.Control = In.Control(bus: ControlBus(index: controlIndex, channelCount: 1))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
#expect(abs(gen.floatValue(graph: graph) - Double(value)) < 1e-6)
|
||||
}
|
||||
|
||||
/// `In.Audio.multi` — the factory that delegates to the raw-`Int` initializer — accepts a typed
|
||||
/// `AudioBus` (it now applies the guard before delegating, so a `ControlBus` would trap there
|
||||
/// too). It fans out one generator per channel.
|
||||
@Test func inAudioMultiAcceptsTypedAudioBus() {
|
||||
let channelCount: Int = 2
|
||||
let array: GeneratorArray = In.Audio.multi(bus: AudioBus(index: 3, channelCount: channelCount))
|
||||
#expect(array.generators.count == channelCount)
|
||||
}
|
||||
|
||||
/// A control-rate `ReplaceOut.Control` overwrites the control pool and leaves the audio-bus pool
|
||||
/// at the same numeric index untouched.
|
||||
@Test func replaceOutControlWritesControlPoolNotAudio() {
|
||||
let controlIndex: Int = 1
|
||||
let value: Double = 0.5
|
||||
let gen: ReplaceOut.Control = ReplaceOut.Control(
|
||||
bus: ControlBus(index: controlIndex, channelCount: 1),
|
||||
source: ConstantControlGen(value)
|
||||
)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
|
||||
#expect(abs(Double(engine.controlBuses[controlIndex]) - value) < 1e-6)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(engine.audioBuses[controlIndex][i] == 0,
|
||||
"ReplaceOut.Control must not touch the audio-bus pool at the same index")
|
||||
}
|
||||
}
|
||||
|
||||
/// The plain `Int` escape hatch is unchecked on both rates — an `Int` carries no pool intent,
|
||||
/// so it still constructs on either generator.
|
||||
@Test func intBusIsUncheckedOnBothRates() {
|
||||
let audioValue: Double = 0.4
|
||||
let controlValue: Double = 0.6
|
||||
let audioGen: Out.Audio = Out.Audio(bus: 4, source: ConstantAudioGen(audioValue))
|
||||
let controlGen: Out.Control = Out.Control(bus: 4, source: ConstantControlGen(controlValue))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = audioGen.calculatedSamples(graph: graph, count: 8)
|
||||
_ = controlGen.calculatedSamples(graph: graph, count: 8)
|
||||
|
||||
#expect(abs(engine.audioBuses[4][0] - Float(audioValue)) < 1e-6)
|
||||
#expect(abs(Double(engine.controlBuses[4]) - controlValue) < 1e-6)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user