Files
Lyrebird/docs/engine/bus-system.md
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

4.5 KiB

Bus System

Pre-allocated at SynthesisEngine.start(). Never resized at runtime.

Memory Layout

// Audio buses: 2D array, [busIndex][sampleIndex]
var audioBuses: [[Float]]  // [numAudioBuses][blockSize]

// Control buses: 1D array, one value per bus
var controlBuses: [Float]  // [numControlBuses]

Reserved Bus Layout

Audio buses:
  0 ..< numOutputChannels                                       → hardware output
  numOutputChannels ..< numOutputChannels + numInputChannels    → hardware input
  numOutputChannels + numInputChannels ..< numAudioBuses        → user buses

Control buses:
  0 ..< numControlBuses    → all user-addressable (none reserved)

Per-Cycle Semantics

At the top of each render callback:

  1. Zero all audio buses — audioBuses[i] = [Float](repeating: 0, count: blockSize)
  2. Copy hardware input — write input samples into the reserved input buses
  3. Retain control buses — no zeroing; values persist until overwritten

At the bottom of each render callback: 4. Copy output buses to hardware — read buses 0..<numOutputChannels into the AudioBufferList

In Generator

Reads from one or more consecutive buses. The channel count is fixed at init — no RT allocation.

// Single-channel (default)
In.Audio(bus: 5)               // 1-channel generator
In.Control(bus: 10)            // 1-channel, interpolated ramp

// Multi-channel — reads N consecutive buses, returns N channels
In.Audio(bus: 4, channels: 3)  // reads audioBuses[4], [5], [6]
In.Control(bus: 10, channels: 2) // reads controlBuses[10] and [11]

// AudioBus object — channel count comes from the allocation
In.Audio(bus: myAudioBus)      // channels == myAudioBus.channelCount

floatValue always returns the value of the first channel (bus index). Each In.Control channel maintains its own ramp state (previousOutputValue exposes channel 0).

Out Generator

Accumulates into a bus. Uses += so multiple Synthesizers writing to the same bus mix.

// Audio rate — mono source writes one bus
for i in 0..<blockSize {
    engine.audioBuses[busIndex][i] += inputBlock[i]
}

// Control rate
engine.controlBuses[busIndex] += inputValue

Multi-channel Out

When the source returns N channels, Out writes channel ch to bus + ch:

// Stereo source (e.g. Pan2, GeneratorArray of 2) writes buses 0 and 1:
Out.Audio(bus: 0, source: Pan2(input: osc, pan: 0.0))

Mono sources are unaffected — they return one channel and write only bus.

Multi-channel Expansion

GeneratorArray wraps N generators and returns an N-channel [[Double]]. Create one with the expand() helper in build():

let oscs = expand([440.0, 880.0]) { OscSin.Audio(freq: $0) }
output(bus: 0, source: oscs)  // bus 0 = 440 Hz, bus 1 = 880 Hz

Read N consecutive buses with input(bus:channels:):

output(bus: 0, source: input(bus: 2, channels: 2) * 0.5)

See docs/stdlib/multichannel.md for the full MCE guide.

Bus Allocator

SynthesisEngine.busAllocator manages user buses so you don't track raw integers manually. Hardware output/input buses are pre-reserved at init; allocation starts from the first free bus after them.

// Allocate a stereo user bus
let stereo = try engine.busAllocator.allocateAudioBus(channelCount: 2)
let ctrl   = try engine.busAllocator.allocateControlBus()

// Use with convenience inits on In / Out
let reader = In.Audio(bus: stereo)                       // reads stereo.index (channel 0)
let multi  = In.Audio.multi(bus: stereo)                 // GeneratorArray — both channels
let writer = Out.Audio(source: someSignal, bus: stereo)  // clamped to stereo.channelCount
let ctrlIn = In.Control(bus: ctrl)

// Return when done
engine.busAllocator.releaseAudioBus(stereo)
engine.busAllocator.releaseControlBus(ctrl)

Thread safety: BusAllocator is protected by an internal NSLock; safe from any language-side thread. Never call from the RT audio thread.

BusAllocatorError.noFreeBus(channelCount:) — thrown when no contiguous block of the requested size is available.

AudioBus and ControlBus

Plain value types. Pass directly to In.Audio(bus:), In.Audio.multi(bus:), Out.Audio(source:bus:), In.Control(bus:), and Out.Control(source:bus:). They are not ValidGeneratorInput — for bus reading use the In generator wrappers.

Defaults

Parameter Default
numAudioBuses 128
numControlBuses 4096
blockSize Configurable at init (e.g. 512)