1 Commits

Author SHA1 Message Date
josh 63d6e0746f v1.2.4 release: bus-rate guard on Out/In/ReplaceOut constructors 2026-07-14 16:12:53 -07:00
7 changed files with 242 additions and 64 deletions
+42
View File
@@ -44,3 +44,45 @@ func _resolvedChannelCount(_ bus: any ValidBus) -> Int {
bus.channelCount == Int.max ? 1 : max(1, bus.channelCount)
}
/// Traps at graph-build time when a ``ControlBus`` is handed to an **audio**-rate bus generator
/// (`Out.Audio` / `ReplaceOut.Audio` / `In.Audio`).
///
/// Those generators address the **audio**-bus pool by index. A `ControlBus`'s `index` lives in the
/// separate control-bus index space, so writing/reading it through an audio-rate generator hits
/// audio memory at that number — silently colliding with unrelated audio buses (including the
/// hardware output buses at low indices). There is no correct use for it: reach for the control-rate
/// variant (`Out.Control` / `ReplaceOut.Control` / `In.Control`, or `SynthesizerBase`'s
/// `controlOutput(bus:source:)` / `controlInput(bus:)`).
///
/// Runs only at `init` (graph construction, main thread) — never on the RT render path. A plain
/// `Int` bus is the deliberate raw-index escape hatch and is intentionally not checked.
@inline(__always)
func _preconditionAudioRateBus(_ bus: any ValidBus) {
precondition(
!(bus is ControlBus),
"A ControlBus was passed to an audio-rate bus generator (Out.Audio / ReplaceOut.Audio / "
+ "In.Audio), which addresses the audio-bus pool at that index and collides with unrelated "
+ "audio buses. Use the control-rate variant (Out.Control / ReplaceOut.Control / In.Control, "
+ "or controlOutput(bus:source:) / controlInput(bus:))."
)
}
/// Traps at graph-build time when an ``AudioBus`` is handed to a **control**-rate bus generator
/// (`Out.Control` / `ReplaceOut.Control` / `In.Control`).
///
/// The mirror of ``_preconditionAudioRateBus(_:)``: control-rate generators address the
/// **control**-bus pool by index, so an `AudioBus`'s `index` reaches control memory at that number
/// and collides with unrelated control buses. Use the audio-rate variant (`Out.Audio` /
/// `ReplaceOut.Audio` / `In.Audio`, or `output(bus:source:)` / `input(bus:)`). `Int` buses are not
/// checked. `init`-time only, never RT.
@inline(__always)
func _preconditionControlRateBus(_ bus: any ValidBus) {
precondition(
!(bus is AudioBus),
"An AudioBus was passed to a control-rate bus generator (Out.Control / ReplaceOut.Control / "
+ "In.Control), which addresses the control-bus pool at that index and collides with "
+ "unrelated control buses. Use the audio-rate variant (Out.Audio / ReplaceOut.Audio / "
+ "In.Audio, or output(bus:source:) / input(bus:))."
)
}
+21 -12
View File
@@ -30,8 +30,9 @@ public enum In {
/// Reads from one or more consecutive audio buses.
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index (1 channel),
/// an ``AudioBus`` allocation (channel count taken from the bus), or a ``ControlBus``.
/// The `bus` parameter takes a plain `Int` index (1 channel) or an ``AudioBus`` allocation
/// (channel count taken from the bus). A ``ControlBus`` traps — `In.Audio` reads the audio-bus
/// pool; use ``In/Control`` for a control bus.
///
/// ```swift
/// In.Audio(bus: 5) // reads audioBuses[5], returns 1 channel
@@ -42,8 +43,8 @@ public enum In {
/// `floatValue` always returns the value of the first channel (bus).
///
/// - Parameters:
/// - bus: Source bus — `Int` index (1 channel), ``AudioBus`` (reads `channelCount`
/// consecutive buses), or ``ControlBus``.
/// - bus: Source bus — `Int` index (1 channel) or ``AudioBus`` (reads `channelCount`
/// consecutive buses). A ``ControlBus`` traps; use ``In/Control``.
/// - channels: Only available on the `Int`-index overload. Number of consecutive buses
/// to read. Defaults to `1`. Fixed at init — no RT allocation.
public final class Audio: _Base, AudioGenerator {
@@ -51,9 +52,10 @@ public enum In {
/// Reads from a ``ValidBus``.
///
/// - Parameters:
/// - bus: Source bus — `Int` index (1 channel), ``AudioBus`` (uses `channelCount`),
/// or ``ControlBus``.
/// - bus: Source bus — `Int` index (1 channel) or ``AudioBus`` (uses `channelCount`).
/// A ``ControlBus`` traps; use ``In/Control``.
public init(bus: any ValidBus) {
_preconditionAudioRateBus(bus)
let ch: Int = _resolvedChannelCount(bus)
super.init(bus: bus.index, channels: ch)
}
@@ -126,7 +128,12 @@ public enum In {
/// Creates a `GeneratorArray` of independent single-channel generators, one per channel of `bus`.
/// Prefer `In.Audio(bus:)` when a single multi-output generator is sufficient.
///
/// Takes an `Int` index or an ``AudioBus``; a ``ControlBus`` traps (this reads the audio-bus
/// pool). The `Int` delegate below can't see the original typed bus, so the guard is applied
/// here before delegating.
public static func multi(bus: any ValidBus) -> GeneratorArray {
_preconditionAudioRateBus(bus)
let ch: Int = _resolvedChannelCount(bus)
return multi(bus: bus.index, channels: ch)
}
@@ -137,8 +144,9 @@ public enum In {
/// Reads from one or more consecutive control buses and delivers interpolated audio-rate
/// ramps to downstream generators.
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index (1 channel),
/// a ``ControlBus`` allocation (channel count taken from the bus), or an ``AudioBus``.
/// The `bus` parameter takes a plain `Int` index (1 channel) or a ``ControlBus`` allocation
/// (channel count taken from the bus). An ``AudioBus`` traps — `In.Control` reads the
/// control-bus pool; use ``In/Audio`` for an audio bus.
///
/// ```swift
/// In.Control(bus: 10) // reads controlBuses[10], interpolated ramp
@@ -153,8 +161,8 @@ public enum In {
/// in steady state.
///
/// - Parameters:
/// - bus: Source bus — `Int` index (1 channel), ``ControlBus`` (reads `channelCount`
/// consecutive buses with independent ramp state per channel), or ``AudioBus``.
/// - bus: Source bus — `Int` index (1 channel) or ``ControlBus`` (reads `channelCount`
/// consecutive buses with independent ramp state per channel). An ``AudioBus`` traps; use ``In/Audio``.
/// - channels: Only available on the `Int`-index overload. Number of consecutive buses
/// to read. Defaults to `1`. Fixed at init — no RT allocation.
public final class Control: _Base, ControlGenerator {
@@ -172,9 +180,10 @@ public enum In {
/// Reads from a ``ValidBus``.
///
/// - Parameters:
/// - bus: Source bus — `Int` index (1 channel), ``ControlBus`` (uses `channelCount`),
/// or ``AudioBus``.
/// - bus: Source bus — `Int` index (1 channel) or ``ControlBus`` (uses `channelCount`).
/// An ``AudioBus`` traps; use ``In/Audio``.
public init(bus: any ValidBus) {
_preconditionControlRateBus(bus)
let ch: Int = _resolvedChannelCount(bus)
_previousOutputValues = [Double](repeating: 0.0, count: ch)
super.init(bus: bus.index, channels: ch)
+16 -15
View File
@@ -40,9 +40,9 @@ public enum Out {
/// Accumulates into one or more consecutive audio buses using +=.
/// Multiple ``Synthesizer`` instances writing to the same bus mix additively.
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index, an ``AudioBus``
/// allocation, or (unusually) a ``ControlBus``. When `bus` is an ``AudioBus``, writing
/// is automatically clamped to `bus.channelCount` channels.
/// The `bus` parameter takes a plain `Int` index or an ``AudioBus`` allocation (writes are
/// clamped to `bus.channelCount` channels). Passing a ``ControlBus`` traps — an audio `Out`
/// addresses the audio-bus pool, so use ``Out/Control`` for a control bus.
///
/// ## Multi-channel sources
///
@@ -57,9 +57,8 @@ public enum Out {
/// ```
///
/// - Parameters:
/// - bus: Destination bus — `Int` index (no channel clamping), ``AudioBus`` (clamped to
/// `channelCount`), or ``ControlBus``. Passed to `In.Audio` or the `output(bus:source:)`
/// helper.
/// - bus: Destination bus — `Int` index (no channel clamping) or ``AudioBus`` (clamped to
/// `channelCount`). A ``ControlBus`` traps; use ``Out/Control`` instead.
/// - source: Signal to write. Multi-channel sources write to consecutive buses starting at
/// `bus.index`.
public final class Audio: _Base, AudioGenerator, RootRegistering {
@@ -73,13 +72,14 @@ public enum Out {
}
/// - Parameters:
/// - bus: Destination bus — `Int` index, ``AudioBus``, or ``ControlBus``.
/// When `bus` is an ``AudioBus``, writing is clamped to `bus.channelCount` channels.
/// - bus: Destination bus — `Int` index or ``AudioBus`` (writing clamped to
/// `bus.channelCount`). A ``ControlBus`` traps; use ``Out/Control`` instead.
/// - source: Signal to write. Multi-channel sources write to consecutive buses.
public init(
bus: any ValidBus,
source: any ValidGeneratorInput
) {
_preconditionAudioRateBus(bus)
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount)
_registerAsRootIfBuilding()
}
@@ -115,9 +115,9 @@ public enum Out {
/// a sink whose output is a control bus write). `previousOutputValue` is required by
/// the ControlGenerator protocol but is not used.
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index, a ``ControlBus``
/// allocation, or an ``AudioBus``. When `bus` is a ``ControlBus``, writing is clamped to
/// `bus.channelCount` channels.
/// The `bus` parameter takes a plain `Int` index or a ``ControlBus`` allocation (writes are
/// clamped to `bus.channelCount` channels). Passing an ``AudioBus`` traps — a control `Out`
/// addresses the control-bus pool, so use ``Out/Audio`` for an audio bus.
///
/// ## Multi-channel sources
///
@@ -131,8 +131,8 @@ public enum Out {
/// ```
///
/// - Parameters:
/// - bus: Destination bus — `Int` index (1 channel, no clamping), ``ControlBus`` (clamped
/// to `channelCount`), or ``AudioBus``.
/// - bus: Destination bus — `Int` index (1 channel, no clamping) or ``ControlBus`` (clamped
/// to `channelCount`). An ``AudioBus`` traps; use ``Out/Audio`` instead.
/// - source: Signal to write. The last sample of each channel block is accumulated into
/// the corresponding control bus. Multi-channel sources write to consecutive buses.
public final class Control: _Base, ControlGenerator, RootRegistering {
@@ -147,14 +147,15 @@ public enum Out {
public override var outputChannelCount: Int { _channels }
/// - Parameters:
/// - bus: Destination bus — `Int` index, ``ControlBus``, or ``AudioBus``.
/// When `bus` is a ``ControlBus``, writing is clamped to `bus.channelCount` channels.
/// - bus: Destination bus — `Int` index or ``ControlBus`` (writing clamped to
/// `bus.channelCount`). An ``AudioBus`` traps; use ``Out/Audio`` instead.
/// A plain `Int` writes a single channel.
/// - source: Signal to write. Multi-channel sources write to consecutive buses.
public init(
bus: any ValidBus,
source: any ValidGeneratorInput
) {
_preconditionControlRateBus(bus)
let ch: Int = _resolvedChannelCount(bus)
self._channels = ch
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount, channels: ch)
@@ -60,10 +60,10 @@ public enum ReplaceOut {
/// Overwrites one or more consecutive audio buses with the source signal
/// (wipes any prior writes to those buses in the current block).
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index, an
/// ``AudioBus`` allocation, or (unusually) a ``ControlBus``. When `bus` is
/// an ``AudioBus``, writing is automatically clamped to `bus.channelCount`
/// channels.
/// The `bus` parameter takes a plain `Int` index or an ``AudioBus`` allocation
/// (writing clamped to `bus.channelCount` channels). A ``ControlBus`` traps —
/// an audio `ReplaceOut` addresses the audio-bus pool; use ``ReplaceOut/Control``
/// for a control bus.
///
/// ## Multi-channel sources
///
@@ -81,8 +81,8 @@ public enum ReplaceOut {
/// ```
///
/// - Parameters:
/// - bus: Destination bus — `Int` index (no channel clamping), ``AudioBus``
/// (clamped to `channelCount`), or ``ControlBus``.
/// - bus: Destination bus — `Int` index (no channel clamping) or ``AudioBus``
/// (clamped to `channelCount`). A ``ControlBus`` traps; use ``ReplaceOut/Control``.
/// - source: Signal to write. Multi-channel sources overwrite consecutive
/// buses starting at `bus.index`.
public final class Audio: _Base, AudioGenerator, RootRegistering {
@@ -102,13 +102,14 @@ public enum ReplaceOut {
}
/// - Parameters:
/// - bus: Destination bus — `Int` index, ``AudioBus``, or ``ControlBus``.
/// When `bus` is an ``AudioBus``, writing is clamped to `bus.channelCount` channels.
/// - bus: Destination bus — `Int` index or ``AudioBus`` (writing clamped to
/// `bus.channelCount`). A ``ControlBus`` traps; use ``ReplaceOut/Control``.
/// - source: Signal to write. Multi-channel sources overwrite consecutive buses.
public init(
bus: any ValidBus,
source: any ValidGeneratorInput
) {
_preconditionAudioRateBus(bus)
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount)
_registerAsRootIfBuilding()
}
@@ -151,9 +152,10 @@ public enum ReplaceOut {
/// control bus write). `previousOutputValue` is required by the
/// ControlGenerator protocol but is not used.
///
/// The `bus` parameter accepts any ``ValidBus``: a plain `Int` index, a
/// ``ControlBus`` allocation, or an ``AudioBus``. When `bus` is a
/// ``ControlBus``, writing is clamped to `bus.channelCount` channels.
/// The `bus` parameter takes a plain `Int` index or a ``ControlBus`` allocation
/// (writing clamped to `bus.channelCount` channels). An ``AudioBus`` traps —
/// a control `ReplaceOut` addresses the control-bus pool; use ``ReplaceOut/Audio``
/// for an audio bus.
///
/// ## Multi-channel sources
///
@@ -163,8 +165,8 @@ public enum ReplaceOut {
/// usage.
///
/// - Parameters:
/// - bus: Destination bus — `Int` index (1 channel, no clamping),
/// ``ControlBus`` (clamped to `channelCount`), or ``AudioBus``.
/// - bus: Destination bus — `Int` index (1 channel, no clamping) or
/// ``ControlBus`` (clamped to `channelCount`). An ``AudioBus`` traps; use ``ReplaceOut/Audio``.
/// - source: Signal to write. The last sample of each channel block is
/// written to the corresponding control bus.
public final class Control: _Base, ControlGenerator, RootRegistering {
@@ -176,8 +178,8 @@ public enum ReplaceOut {
///
/// - Note: Channel-count policy intentionally mirrors ``Out/Control``: a
/// plain `Int` bus resolves to 1 channel (extra source channels are
/// silently dropped); a ``ControlBus`` or ``AudioBus`` resolves to its
/// explicit `channelCount`. ``ReplaceOut/Audio`` differs — a plain
/// silently dropped); a ``ControlBus`` resolves to its explicit
/// `channelCount`. ``ReplaceOut/Audio`` differs — a plain
/// `Int` bus there writes all source channels via `_maxChannels =
/// Int.max`. Use a typed bus allocation if you want explicit
/// multi-channel writes from a plain index.
@@ -186,13 +188,14 @@ public enum ReplaceOut {
public override var outputChannelCount: Int { _channels }
/// - Parameters:
/// - bus: Destination bus — `Int` index, ``ControlBus``, or ``AudioBus``.
/// When `bus` is a ``ControlBus``, writing is clamped to `bus.channelCount` channels.
/// - bus: Destination bus — `Int` index or ``ControlBus`` (writing clamped to
/// `bus.channelCount`). An ``AudioBus`` traps; use ``ReplaceOut/Audio``.
/// - source: Signal to write. Multi-channel sources overwrite consecutive buses.
public init(
bus: any ValidBus,
source: any ValidGeneratorInput
) {
_preconditionControlRateBus(bus)
let ch: Int = _resolvedChannelCount(bus)
self._channels = ch
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount, channels: ch)
@@ -338,14 +338,10 @@ public final class LyrebirdProcess: @unchecked Sendable {
onRelease: ((LyrebirdProcessContext) -> Void)? = nil,
onFinished: (() -> Void)? = nil,
) {
// `procOut` is `any ValidBus` so callers may pass `Int` or `AudioBus`.
// A `ControlBus` is also a `ValidBus` but routing audio samples into
// the control bus address space is semantically invalid — trap at init
// (off-RT, so `is`-check is allowed).
precondition(
!(procOut is ControlBus),
"[LyrebirdProcess '\(id)'] procOut must be an Int or AudioBus — ControlBus routes audio samples into control bus memory."
)
// `procOut` addresses the audio-bus pool, so an accidental `ControlBus` here would route
// audio into control-bus memory. Share the same audio-rate bus guard the BusIO generators
// use (trap at init, off-RT, so the `is`-check is allowed).
_preconditionAudioRateBus(procOut)
self.id = id
_envelope = envelope
@@ -406,8 +406,9 @@ open class SynthesizerBase: Synthesizer {
/// Create an `Out` generator for the given audio bus and register it as an output.
///
/// `bus` accepts any ``ValidBus``: a plain `Int` index, an ``AudioBus`` allocation
/// (channel count enforced automatically), or a ``ControlBus``.
/// `bus` takes a plain `Int` index or an ``AudioBus`` allocation (channel count enforced
/// automatically). A ``ControlBus`` traps — this writes the audio-bus pool; use
/// ``controlOutput(bus:source:)`` for a control bus.
///
/// ```swift
/// output(bus: 0, source: sig) // plain Int bus
@@ -436,10 +437,9 @@ open class SynthesizerBase: Synthesizer {
/// an output. **Overwrites** the bus content (wipes any prior writes in the
/// current block).
///
/// `bus` accepts any ``ValidBus``: a plain `Int` index, an ``AudioBus``
/// allocation, or a ``ControlBus``. When `bus` is an ``AudioBus``, writing
/// is clamped to `bus.channelCount` channels. Multi-channel sources
/// overwrite consecutive buses starting at `bus.index`.
/// `bus` takes a plain `Int` index or an ``AudioBus`` allocation (writing clamped to
/// `bus.channelCount` channels; multi-channel sources overwrite consecutive buses from
/// `bus.index`). A ``ControlBus`` traps — this writes the audio-bus pool.
///
/// ```swift
/// // In-place amp scale on a 4-channel bus.
@@ -457,8 +457,9 @@ open class SynthesizerBase: Synthesizer {
/// Create a `Out.Control` generator for the given control bus and register it as an output.
///
/// `bus` accepts any ``ValidBus``: a plain `Int` index, a ``ControlBus`` allocation
/// (channel count enforced automatically), or an ``AudioBus``.
/// `bus` takes a plain `Int` index or a ``ControlBus`` allocation (channel count enforced
/// automatically). An ``AudioBus`` traps — this writes the control-bus pool; use
/// ``output(bus:source:)`` for an audio bus.
///
/// ```swift
/// controlOutput(bus: 10, source: lfo) // plain Int bus
@@ -471,8 +472,9 @@ open class SynthesizerBase: Synthesizer {
/// Create and return an `In.Audio` generator for the given audio bus.
///
/// `bus` accepts any ``ValidBus``. When `bus` is an ``AudioBus``, the returned generator
/// reads `bus.channelCount` consecutive buses. A plain `Int` reads a single bus.
/// `bus` takes a plain `Int` index (single bus) or an ``AudioBus`` (reads
/// `bus.channelCount` consecutive buses). A ``ControlBus`` traps — this reads the audio-bus
/// pool; use ``controlInput(bus:)`` for a control bus.
///
/// ```swift
/// input(bus: 2) // single-channel In.Audio
@@ -484,8 +486,9 @@ open class SynthesizerBase: Synthesizer {
/// Create and return an `In.Control` generator for the given control bus.
///
/// `bus` accepts any ``ValidBus``. When `bus` is a ``ControlBus``, the returned generator
/// reads `bus.channelCount` consecutive buses. A plain `Int` reads a single bus.
/// `bus` takes a plain `Int` index (single bus) or a ``ControlBus`` (reads
/// `bus.channelCount` consecutive buses). An ``AudioBus`` traps — this reads the control-bus
/// pool; use ``input(bus:)`` for an audio bus.
///
/// ```swift
/// controlInput(bus: 10) // single-channel In.Control
@@ -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)
}
}