Add mono→stereo routing to PedalBoard
Introduce chain width propagation (mono=1 / stereo=2, capped at 2), represented as a pair of mono buses to reuse existing infrastructure. - Pedal: width-parameterized build() with channelKind (mono / widener / stereoNative); effect() runs per channel for auto-stereoize, shared knobs. - PedalBoard: width tracking, add() dispatch, mergeToMono(), splitStereo()/ collectStereo() with four route modes (stereo / leftOnly / rightOnly / bypass), centered solo, click-free lagged gains. - BoardOutputMaster: width-aware (Pan2 mono, direct L/R stereo). - New routing nodes: StereoSplitter, StereoRouteCollector, StereoSplitSection. - Reference pedals: OctaveUpSplitterPedal (widener), PingPongDelayPedal (stereo-native). - 12 stereo-routing tests; full suite 108/108. Design: docs/stereo-routing-design.md Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -65,10 +65,21 @@ public final class PedalBoard {
|
||||
/// All pedals in order of insertion (signal-flow order) in the main chain (not branches).
|
||||
public private(set) var pedals: [Pedal] = []
|
||||
|
||||
/// The bus that the last node in the chain writes to.
|
||||
/// The bus that the last node in the chain writes to (left / mono tail).
|
||||
/// Before any node is added, this is `headBus` (the board is a pass-through).
|
||||
public private(set) var outputBus: AudioBus
|
||||
|
||||
/// The right-channel tail bus when the chain is stereo (``width`` == 2), else `nil`.
|
||||
/// Set by a widener, a stereo-native pedal, an auto-stereoized pedal, or `collectStereo(_:)`;
|
||||
/// cleared by `mergeToMono()`.
|
||||
public private(set) var outputBusRight: AudioBus?
|
||||
|
||||
/// Current channel width of the chain tail: `1` = mono, `2` = stereo. Capped at 2.
|
||||
public private(set) var width: Int = 1
|
||||
|
||||
/// All stereo split sections created via `splitStereo()` (for inspection / UI binding).
|
||||
public private(set) var stereoSplitSections: [StereoSplitSection] = []
|
||||
|
||||
/// The node that writes the head bus (e.g. a `TestLoopSource` or input pass-through).
|
||||
/// Set by `ShoeGaze` after adding the input source. When non-nil, the first `add(_:)`
|
||||
/// call chains the first pedal after this node automatically (no `after:` arg needed).
|
||||
@@ -158,29 +169,223 @@ public final class PedalBoard {
|
||||
/// - `BusAllocatorError.noFreeBus` if no mono audio bus is available for output.
|
||||
/// - `EngineAddError` if the engine rejects the add (e.g. queue full, double-add).
|
||||
public func add(_ pedal: Pedal, after predecessor: (any Synthesizer)? = nil) throws {
|
||||
let pedalInputBus: AudioBus = outputBus
|
||||
let pedalOutputBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
allocatedBuses.append(pedalOutputBus)
|
||||
|
||||
pedal.inputBus = pedalInputBus.index
|
||||
pedal.outputBus = pedalOutputBus.index
|
||||
pedal.sampleRate = engine.sampleRate
|
||||
|
||||
// Maintain evaluation order: each node evaluates immediately after the previous.
|
||||
// `lastNode` tracks the most recently inserted node (pedal, splitter, or merger).
|
||||
// Falls back to the caller-supplied `predecessor` or `headInputNode` for the first
|
||||
// node. If none is available, plain add is used as a last resort.
|
||||
if let previous: any Synthesizer = lastNode {
|
||||
try engine.add(pedal, position: .after(previous))
|
||||
} else if let pred: any Synthesizer = predecessor ?? headInputNode {
|
||||
try engine.add(pedal, position: .after(pred))
|
||||
} else {
|
||||
try engine.add(pedal)
|
||||
// Each arm configures the pedal's buses, inserts it, advances the tail, and only then
|
||||
// records it in `pedals` — so a pedal is never tracked unless it was fully wired and
|
||||
// added to the engine.
|
||||
switch pedal.channelKind {
|
||||
case .mono:
|
||||
if width == 1 {
|
||||
// Mono region: one input, one output. Behaviour is identical to the original
|
||||
// single-bus path.
|
||||
let outLeft: AudioBus = try allocateInterNodeBus()
|
||||
pedal.inputBus = outputBus.index
|
||||
pedal.inputBusRight = nil
|
||||
pedal.outputBus = outLeft.index
|
||||
pedal.outputBusRight = nil
|
||||
try insertNode(pedal, after: predecessor)
|
||||
advanceTail(left: outLeft, right: nil, width: 1)
|
||||
} else {
|
||||
// Stereo region: auto-stereoize. The pedal runs `effect(_:)` per channel into
|
||||
// two new buses — one logical pedal, two independent DSP graphs, shared knobs.
|
||||
let rightTail: AudioBus = try requireRightTail()
|
||||
let outLeft: AudioBus = try allocateInterNodeBus()
|
||||
let outRight: AudioBus = try allocateInterNodeBus()
|
||||
pedal.inputBus = outputBus.index
|
||||
pedal.inputBusRight = rightTail.index
|
||||
pedal.outputBus = outLeft.index
|
||||
pedal.outputBusRight = outRight.index
|
||||
try insertNode(pedal, after: predecessor)
|
||||
advanceTail(left: outLeft, right: outRight, width: 2)
|
||||
}
|
||||
|
||||
case .widener:
|
||||
// 1 → 2. Only valid from a mono tail; width is capped at 2.
|
||||
guard width == 1 else { throw ShoeGazeError.stereoWidthExceeded }
|
||||
let outLeft: AudioBus = try allocateInterNodeBus()
|
||||
let outRight: AudioBus = try allocateInterNodeBus()
|
||||
pedal.inputBus = outputBus.index
|
||||
pedal.inputBusRight = nil
|
||||
pedal.outputBus = outLeft.index
|
||||
pedal.outputBusRight = outRight.index
|
||||
try insertNode(pedal, after: predecessor)
|
||||
advanceTail(left: outLeft, right: outRight, width: 2)
|
||||
|
||||
case .stereoNative:
|
||||
// 2 → 2 with cross-channel coupling. From a mono tail the input broadcasts to both
|
||||
// channels (right input left nil); from a stereo tail it reads both tails.
|
||||
let rightInput: Int? = (width == 2) ? try requireRightTail().index : nil
|
||||
let outLeft: AudioBus = try allocateInterNodeBus()
|
||||
let outRight: AudioBus = try allocateInterNodeBus()
|
||||
pedal.inputBus = outputBus.index
|
||||
pedal.inputBusRight = rightInput
|
||||
pedal.outputBus = outLeft.index
|
||||
pedal.outputBusRight = outRight.index
|
||||
try insertNode(pedal, after: predecessor)
|
||||
advanceTail(left: outLeft, right: outRight, width: 2)
|
||||
}
|
||||
|
||||
pedals.append(pedal)
|
||||
lastNode = pedal
|
||||
outputBus = pedalOutputBus
|
||||
}
|
||||
|
||||
/// Returns the right tail bus, enforcing the `width == 2 ⟺ outputBusRight != nil` invariant.
|
||||
/// Throws rather than crashing if the invariant is ever violated by a coding error.
|
||||
private func requireRightTail() throws -> AudioBus {
|
||||
guard let rightTail: AudioBus = outputBusRight else {
|
||||
throw ShoeGazeError.stereoWidthExceeded
|
||||
}
|
||||
return rightTail
|
||||
}
|
||||
|
||||
// MARK: - Stereo width helpers
|
||||
|
||||
/// Allocates a mono inter-node bus and tracks it for release on teardown.
|
||||
private func allocateInterNodeBus() throws -> AudioBus {
|
||||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
allocatedBuses.append(bus)
|
||||
return bus
|
||||
}
|
||||
|
||||
/// Inserts a node into the engine maintaining evaluation order, then records it as `lastNode`.
|
||||
///
|
||||
/// Inserts `.after(lastNode)` when a previous node exists, else `.after` the caller-supplied
|
||||
/// predecessor or `headInputNode`, else a plain add as a last resort.
|
||||
private func insertNode(_ node: any Synthesizer, after predecessor: (any Synthesizer)?) throws {
|
||||
if let previous: any Synthesizer = lastNode {
|
||||
try engine.add(node, position: .after(previous))
|
||||
} else if let pred: any Synthesizer = predecessor ?? headInputNode {
|
||||
try engine.add(node, position: .after(pred))
|
||||
} else {
|
||||
try engine.add(node)
|
||||
}
|
||||
lastNode = node
|
||||
}
|
||||
|
||||
/// Advances the chain tail to the given bus pair and width.
|
||||
private func advanceTail(left: AudioBus, right: AudioBus?, width: Int) {
|
||||
self.outputBus = left
|
||||
self.outputBusRight = right
|
||||
self.width = width
|
||||
}
|
||||
|
||||
/// Collapses a stereo (width-2) tail back to mono by summing the two tails (×0.5 each)
|
||||
/// through a ``Merger``. No-op when the chain is already mono.
|
||||
///
|
||||
/// - Throws:
|
||||
/// - `BusAllocatorError.noFreeBus` if no mono audio bus is available.
|
||||
/// - `EngineAddError` if the engine rejects the merger add.
|
||||
public func mergeToMono() throws {
|
||||
guard width == 2, let rightTail: AudioBus = outputBusRight else { return }
|
||||
let monoBus: AudioBus = try allocateInterNodeBus()
|
||||
let merger: Merger = Merger(
|
||||
inputBusA: outputBus.index,
|
||||
inputBusB: rightTail.index,
|
||||
outputBus: monoBus.index
|
||||
)
|
||||
// Sum with equal half-gains so two correlated channels do not double in level.
|
||||
merger.levelA = 0.5
|
||||
merger.levelB = 0.5
|
||||
try insertNode(merger, after: nil)
|
||||
routingNodes.append(merger)
|
||||
advanceTail(left: monoBus, right: nil, width: 1)
|
||||
}
|
||||
|
||||
// MARK: - Stereo split
|
||||
|
||||
/// Fans the current mono tail into two independently authored branch chains that become the
|
||||
/// stereo output pair, plus a clean dry tap for the `bypass` route.
|
||||
///
|
||||
/// Add pedals via `section.left.add(_:)` / `section.right.add(_:)`, then call
|
||||
/// `collectStereo(_:)` to wire the route collector and advance the board to width 2.
|
||||
/// Set `section.route` to choose the output mode (`stereo` / `leftOnly` / `rightOnly` /
|
||||
/// `bypass`) at any time.
|
||||
///
|
||||
/// - Returns: A ``StereoSplitSection`` with two empty branch chains.
|
||||
/// - Throws:
|
||||
/// - `ShoeGazeError.stereoWidthExceeded` if the board is already stereo.
|
||||
/// - `BusAllocatorError.noFreeBus` if no mono audio bus is available.
|
||||
/// - `EngineAddError` if the engine rejects the splitter add.
|
||||
public func splitStereo() throws -> StereoSplitSection {
|
||||
guard width == 1 else { throw ShoeGazeError.stereoWidthExceeded }
|
||||
let preBus: AudioBus = outputBus
|
||||
|
||||
// Branch input buses are owned (and released) by each BranchChain, not by the board,
|
||||
// mirroring `split()`. The dry tap is board-owned.
|
||||
let leftInBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let rightInBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let dryTapBus: AudioBus = try allocateInterNodeBus()
|
||||
|
||||
let splitter: StereoSplitter = StereoSplitter(
|
||||
inputBus: preBus.index,
|
||||
outputBusLeft: leftInBus.index,
|
||||
outputBusRight: rightInBus.index,
|
||||
dryTapBus: dryTapBus.index
|
||||
)
|
||||
|
||||
// Both branches share one tail pointer so their pedals form a flat linear sequence,
|
||||
// guaranteeing all branch pedals evaluate before the collector regardless of add order.
|
||||
// Construct and track the branch chains (which take ownership of `leftInBus`/`rightInBus`)
|
||||
// BEFORE the throwing engine insert below, so those buses are always released on teardown
|
||||
// even if the splitter add fails.
|
||||
let tail: SharedTail = SharedTail(node: splitter)
|
||||
let leftChain: BranchChain = BranchChain(
|
||||
engine: engine,
|
||||
inputBus: leftInBus,
|
||||
sampleRate: engine.sampleRate,
|
||||
sharedTail: tail
|
||||
)
|
||||
let rightChain: BranchChain = BranchChain(
|
||||
engine: engine,
|
||||
inputBus: rightInBus,
|
||||
sampleRate: engine.sampleRate,
|
||||
sharedTail: tail
|
||||
)
|
||||
branchChains.append(leftChain)
|
||||
branchChains.append(rightChain)
|
||||
|
||||
try insertNode(splitter, after: nil)
|
||||
routingNodes.append(splitter)
|
||||
|
||||
let section: StereoSplitSection = StereoSplitSection(
|
||||
splitter: splitter,
|
||||
left: leftChain,
|
||||
right: rightChain,
|
||||
sharedTail: tail,
|
||||
dryTapBus: dryTapBus
|
||||
)
|
||||
stereoSplitSections.append(section)
|
||||
return section
|
||||
}
|
||||
|
||||
/// Inserts the route collector for a stereo split section and advances the board to width 2.
|
||||
///
|
||||
/// The collector reads both branch tails and the dry tap, and writes the board's new left and
|
||||
/// right tail buses. It is inserted after the section's shared tail (the last pedal added
|
||||
/// across either branch), so all branch pedals have written their buses before it reads them.
|
||||
/// Branches with no pedals pass their split signal straight through.
|
||||
///
|
||||
/// - Parameter section: A section returned by `splitStereo()`.
|
||||
/// - Throws:
|
||||
/// - `BusAllocatorError.noFreeBus` if no mono audio bus is available.
|
||||
/// - `EngineAddError` if the engine rejects the collector add.
|
||||
public func collectStereo(_ section: StereoSplitSection) throws {
|
||||
let outLeft: AudioBus = try allocateInterNodeBus()
|
||||
let outRight: AudioBus = try allocateInterNodeBus()
|
||||
|
||||
let collector: StereoRouteCollector = StereoRouteCollector(
|
||||
inputBusLeft: section.left.tailBusIndex,
|
||||
inputBusRight: section.right.tailBusIndex,
|
||||
dryTapBus: section.dryTapBus.index,
|
||||
outputBusLeft: outLeft.index,
|
||||
outputBusRight: outRight.index
|
||||
)
|
||||
// Insert after the shared tail — the most recently added node across both branches.
|
||||
try engine.add(collector, position: .after(section.sharedTail.node))
|
||||
routingNodes.append(collector)
|
||||
lastNode = collector
|
||||
section.bind(collector: collector)
|
||||
advanceTail(left: outLeft, right: outRight, width: 2)
|
||||
}
|
||||
|
||||
// MARK: - Parallel routing
|
||||
|
||||
Reference in New Issue
Block a user