Files
Lyrebird/Sources/Lyrebird/Synthesizer/SynthGraph.swift
T
2026-06-01 18:27:40 -07:00

123 lines
5.3 KiB
Swift

// SynthGraph.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Context passed to every `calculatedSamples` and `floatValue` call during graph evaluation.
///
/// `engine` provides access to bus memory. `busOffset` is the sample offset within the
/// current hardware block (non-zero during sub-block splitting). `_parameterTable` carries
/// a reference to the current Synthesizer's parameter inputs (populated by `SynthesizerBase`
/// from `@LiveParam` / `@UIParam` properties via `Mirror` reflection at `build()` time).
///
/// `_scratchBuses` is non-nil when this graph is being evaluated inside a `ParallelGroup`
/// child slot. `Out.Audio` checks this field and routes writes to the scratch buffer instead
/// of `engine.audioBuses`. After all children complete, `ParallelGroup` sums scratch into
/// the real buses via `vDSP_vadd`. Passing `nil` (the default) preserves the original
/// single-threaded write path.
///
/// ## RT-safety note
///
/// `SynthGraph` is a value type, but `_parameterTable` and `_scratchBuses` are reference
/// types. Passing `SynthGraph` by value on the RT thread costs one ARC retain each for
/// those two objects — not N retains for each declared parameter or bus element.
public struct SynthGraph {
/// Engine providing bus memory for In/Out generators.
public let engine: SynthesisEngine
/// Sample offset within the current block (set by sub-block splitter; 0 for full blocks).
public let busOffset: Int
/// Reference to the Synthesizer's parameter table.
///
/// `internal` because it is an implementation detail of the parameter system —
/// only `SynthesizerBase.evaluate` reads from it directly. User code reaches the
/// parameter nodes through `@LiveParam` / `@UIParam` projected values (`$name`).
internal let _parameterTable: ParameterTable
/// Non-nil when this graph context is running inside a `ParallelGroup` child slot.
///
/// `Out.Audio` writes to `_scratchBuses` instead of `engine.audioBuses` when this is
/// set. `SynthesizerBase.evaluate` forwards the field to its `localGraph` so the entire
/// generator DAG underneath a child Synthesizer writes to the correct scratch slot.
internal let _scratchBuses: ScratchBuses?
// MARK: - Public init (for tests and non-RT contexts)
/// Creates a `SynthGraph` with an empty parameter table — suitable for test code and
/// any non-RT context that does not need a Synthesizer's parameter context.
///
/// - Warning: not for use on the RT audio thread (allocates a fresh `ParameterTable`).
public init(
engine: SynthesisEngine,
busOffset: Int = 0
) {
self.engine = engine
self.busOffset = busOffset
self._scratchBuses = nil
self._parameterTable = ParameterTable()
}
// MARK: - Internal init (RT path — no dict copy)
/// Creates a `SynthGraph` by reference to an existing `ParameterTable`.
///
/// Used exclusively by `SynthesizerBase.evaluate` on the RT thread. Passing the
/// pre-existing `ParameterTable` by reference avoids copying the parameter dictionary
/// and limits the ARC cost to a single retain for the table object.
///
/// `scratchBuses` is forwarded from the incoming graph when called from
/// `SynthesizerBase.evaluate`, ensuring the correct bus destination is propagated
/// through nested synthesizer evaluation.
internal init(
engine: SynthesisEngine,
busOffset: Int,
parameterTable: ParameterTable,
scratchBuses: ScratchBuses? = nil
) {
self.engine = engine
self.busOffset = busOffset
self._parameterTable = parameterTable
self._scratchBuses = scratchBuses
}
// MARK: - Bus write routing (Out.Audio uses this instead of engine directly)
/// Accumulate `count` `Double` (Double) samples into an audio bus at `offset`.
///
/// Routes to the active scratch slot when running inside a `ParallelGroup` child;
/// otherwise delegates to `engine.addToAudioBus` (the normal single-threaded path).
/// This indirection is the key change that makes `Out.Audio` parallel-safe.
internal func addSamplesToAudioBus(
_ bus: Int,
offset: Int,
samples: [Double],
count: Int
) {
if let scratch: ScratchBuses = _scratchBuses {
scratch.accumulate(samples, bus: bus, offset: offset, count: count)
} else {
engine.addToAudioBus(bus, offset: offset, samples: samples, count: count)
}
}
/// Overwrite `count` `Double` samples into an audio bus at `offset`, wiping
/// any prior writes in `[offset, offset + count)`.
///
/// Routes to the active scratch slot when running inside a `ParallelGroup`
/// child (in which case ReplaceOut only overwrites this child's scratch
/// contribution — see `ScratchBuses.replace`). Otherwise delegates to
/// `engine.setAudioBus`, which scrubs the engine bus directly.
internal func setSamplesOnAudioBus(
_ bus: Int,
offset: Int,
samples: [Double],
count: Int
) {
if let scratch: ScratchBuses = _scratchBuses {
scratch.replace(samples, bus: bus, offset: offset, count: count)
} else {
engine.setAudioBus(bus, offset: offset, samples: samples, count: count)
}
}
}