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:
2026-06-21 16:48:25 -07:00
parent ca4cabb42f
commit f89229282f
14 changed files with 1486 additions and 34 deletions
@@ -0,0 +1,33 @@
// PedalChannelKind.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Declares how a ``Pedal`` relates its input channels to its output channels.
///
/// The board uses this to decide how many buses to hand a pedal and how its
/// `build()` should be dispatched:
///
/// - `mono`: one channel in, one channel out. A `mono` pedal is *stereoizable* —
/// when the board places it in a stereo (width-2) region it runs the pedal's
/// `effect(_:)` once per channel, giving two independent DSP graphs that share
/// the pedal's knobs. This is the default and covers every ordinary pedal.
/// - `widener`: mono in, two distinct wet outs (1 → 2). Widens the chain from
/// mono to stereo. Only valid in a width-1 region; the board rejects a widener
/// in an already-stereo region (width is capped at 2).
/// - `stereoNative`: two channels in, two channels out, with cross-channel
/// coupling that cannot be expressed as two independent monos (e.g. a
/// ping-pong delay). One instance reads both input buses and writes both
/// output buses.
public enum PedalChannelKind {
/// One in, one out. Stereoized per-channel by the board when in a stereo region.
case mono
/// Mono in, two distinct wet outs (1 → 2). Widens the chain to stereo.
case widener
/// Two in, two out with cross-channel coupling (e.g. ping-pong delay).
case stereoNative
}
@@ -0,0 +1,32 @@
// StereoRoute.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Output routing mode for a stereo split region (see ``StereoSplitSection``).
///
/// Selects how the left chain, right chain, and the clean pre-split signal map to the
/// final stereo output pair. Transitions between modes are click-free — the
/// ``StereoRouteCollector`` lags each mode gain over 20 ms.
///
/// - `stereo`: normal operation — left chain → left output, right chain → right output.
/// - `leftOnly`: **centered** solo of the left chain — the left chain is sent to *both*
/// output channels, so it plays full and centered rather than only in the left speaker.
/// - `rightOnly`: centered solo of the right chain (both output channels).
/// - `bypass`: the clean pre-split signal passes to both output channels; both branch
/// chains are muted.
public enum StereoRoute: String, Codable, CaseIterable {
/// Left chain → left, right chain → right.
case stereo
/// Centered solo of the left chain (both speakers).
case leftOnly
/// Centered solo of the right chain (both speakers).
case rightOnly
/// Clean pre-split signal through both channels; branches muted.
case bypass
}
@@ -0,0 +1,142 @@
// StereoRouteCollector.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Produces the final stereo output pair of a stereo split region according to a
/// ``StereoRoute``.
///
/// Reads the left branch tail (`inL`), the right branch tail (`inR`), and the clean
/// pre-split tap (`dry`), then mixes them into two output buses. The active route is
/// represented by four scalar gains — exactly one is `1.0`, the rest `0.0` — each lagged
/// through a ``Lag.Control`` so switching routes crossfades click-free:
///
/// ```
/// outL = inL·(aStereo + aLeftSolo) + inR·aRightSolo + dry·aBypass
/// outR = inR·(aStereo + aRightSolo) + inL·aLeftSolo + dry·aBypass
/// ```
///
/// | route | outL | outR | meaning |
/// |-------------|------|------|-------------------------------------------|
/// | `stereo` | inL | inR | left chain → left, right chain → right |
/// | `leftOnly` | inL | inL | centered solo of the left chain |
/// | `rightOnly` | inR | inR | centered solo of the right chain |
/// | `bypass` | dry | dry | clean pre-split signal; branches muted |
///
/// Solo routes are *centered* (the soloed chain feeds both output channels) so a soloed
/// chain plays full and centered rather than lopsided in one speaker.
public final class StereoRouteCollector: SynthesizerBase {
// MARK: - Route gains
/// Gain for the `stereo` route (left→left, right→right). `1.0` = active.
@UIParam public var gainStereo: Double = 1.0
/// Gain for the `leftOnly` route (left chain to both channels). `1.0` = active.
@UIParam public var gainLeftSolo: Double = 0.0
/// Gain for the `rightOnly` route (right chain to both channels). `1.0` = active.
@UIParam public var gainRightSolo: Double = 0.0
/// Gain for the `bypass` route (clean pre-split signal to both channels). `1.0` = active.
@UIParam public var gainBypass: Double = 0.0
// MARK: - Routing
/// Audio bus index of the left branch tail.
public internal(set) var inputBusLeft: Int
/// Audio bus index of the right branch tail.
public internal(set) var inputBusRight: Int
/// Audio bus index of the clean pre-split tap.
public internal(set) var dryTapBus: Int
/// Audio bus index for the left output channel.
public internal(set) var outputBusLeft: Int
/// Audio bus index for the right output channel.
public internal(set) var outputBusRight: Int
// MARK: - Constants
/// Lag time in seconds for route-gain transitions. Smooths route switches to prevent clicks.
private let routeLagTime: Double = 0.02
// MARK: - Init
/// Creates a stereo route collector.
///
/// - Parameters:
/// - inputBusLeft: Audio bus index of the left branch tail.
/// - inputBusRight: Audio bus index of the right branch tail.
/// - dryTapBus: Audio bus index of the clean pre-split tap.
/// - outputBusLeft: Audio bus index for the left output channel.
/// - outputBusRight: Audio bus index for the right output channel.
public init(
inputBusLeft: Int,
inputBusRight: Int,
dryTapBus: Int,
outputBusLeft: Int,
outputBusRight: Int
) {
self.inputBusLeft = inputBusLeft
self.inputBusRight = inputBusRight
self.dryTapBus = dryTapBus
self.outputBusLeft = outputBusLeft
self.outputBusRight = outputBusRight
super.init()
}
// MARK: - Route convenience
/// The active routing mode, derived from (and applied to) the four route gains.
///
/// - Getting: returns the route whose gain is highest (defaults to `.stereo` on a tie).
/// - Setting: sets the chosen route's gain to `1.0` and the others to `0.0`. The
/// collector lags each gain, so the transition is click-free.
public var route: StereoRoute {
get {
var best: StereoRoute = .stereo
var bestGain: Double = gainStereo
if gainLeftSolo > bestGain { best = .leftOnly; bestGain = gainLeftSolo }
if gainRightSolo > bestGain { best = .rightOnly; bestGain = gainRightSolo }
if gainBypass > bestGain { best = .bypass; bestGain = gainBypass }
return best
}
set {
gainStereo = (newValue == .stereo) ? 1.0 : 0.0
gainLeftSolo = (newValue == .leftOnly) ? 1.0 : 0.0
gainRightSolo = (newValue == .rightOnly) ? 1.0 : 0.0
gainBypass = (newValue == .bypass) ? 1.0 : 0.0
}
}
// MARK: - Build
/// Builds the signal graph: route-weighted mix of left, right, and dry into two outputs.
public override func build() {
let inL: In.Audio = input(bus: inputBusLeft)
let inR: In.Audio = input(bus: inputBusRight)
let dry: In.Audio = input(bus: dryTapBus)
let aStereo: Lag.Control = Lag.Control(input: $gainStereo, time: routeLagTime)
let aLeftSolo: Lag.Control = Lag.Control(input: $gainLeftSolo, time: routeLagTime)
let aRightSolo: Lag.Control = Lag.Control(input: $gainRightSolo, time: routeLagTime)
let aBypass: Lag.Control = Lag.Control(input: $gainBypass, time: routeLagTime)
// Left output: left chain in stereo + left solo, right chain in right solo, dry in bypass.
let outL: any ValidGeneratorInput =
(inL * (aStereo + aLeftSolo)) + (inR * aRightSolo) + (dry * aBypass)
// Right output: right chain in stereo + right solo, left chain in left solo, dry in bypass.
let outR: any ValidGeneratorInput =
(inR * (aStereo + aRightSolo)) + (inL * aLeftSolo) + (dry * aBypass)
output(bus: outputBusLeft, source: outL)
output(bus: outputBusRight, source: outR)
}
}
@@ -0,0 +1,100 @@
// StereoSplitSection.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// A stereo split region inside a ``PedalBoard``.
///
/// Created by ``PedalBoard/splitStereo()``. Holds the ``StereoSplitter`` that fans the mono
/// tail out, the two independently authored branch sub-chains that *become* the stereo
/// channels, and (after ``PedalBoard/collectStereo(_:)``) the ``StereoRouteCollector`` that
/// resolves the output ``route``.
///
/// Unlike a ``ParallelSection`` (whose branches are summed back to mono by a ``Merger``), the
/// two branches here map directly to the left and right output channels — the board's width
/// becomes 2 after `collectStereo(_:)`.
///
/// ## Typical usage
///
/// ```swift
/// let s = try board.splitStereo()
/// try s.left.add(DelayPedal())
/// try s.right.add(DelayPedal())
/// try board.collectStereo(s)
/// s.route = .leftOnly // centered solo of the left chain
/// ```
@MainActor
public final class StereoSplitSection {
// MARK: - Nodes and chains
/// The splitter that fans the pre-split mono signal into both branches plus the dry tap.
public let splitter: StereoSplitter
/// The left branch sub-chain. Its tail becomes the left output channel.
public let left: BranchChain
/// The right branch sub-chain. Its tail becomes the right output channel.
public let right: BranchChain
/// Shared tail pointer updated by both branch chains; used by ``PedalBoard/collectStereo(_:)``
/// to place the collector after the last pedal added across either branch.
internal let sharedTail: SharedTail
/// The clean pre-split tap bus carried to the collector for the `bypass` route.
internal let dryTapBus: AudioBus
/// The route collector, set by ``PedalBoard/collectStereo(_:)``. `nil` until then.
internal var collector: StereoRouteCollector?
/// Route requested before the collector exists; applied when `collectStereo(_:)` wires it.
private var pendingRoute: StereoRoute = .stereo
// MARK: - Init
/// Creates a stereo split section. Called internally by ``PedalBoard/splitStereo()``.
///
/// - Parameters:
/// - splitter: The ``StereoSplitter`` feeding the two branches and the dry tap.
/// - left: The left branch sub-chain.
/// - right: The right branch sub-chain.
/// - sharedTail: The shared tail tracking pointer used by both branch chains.
/// - dryTapBus: The clean pre-split tap bus.
init(
splitter: StereoSplitter,
left: BranchChain,
right: BranchChain,
sharedTail: SharedTail,
dryTapBus: AudioBus
) {
self.splitter = splitter
self.left = left
self.right = right
self.sharedTail = sharedTail
self.dryTapBus = dryTapBus
}
// MARK: - Route
/// The output routing mode for this region. Settable from code or UI at any time; the
/// transition is click-free. Before ``PedalBoard/collectStereo(_:)`` wires the collector
/// the value is held and applied on wiring.
public var route: StereoRoute {
get { collector?.route ?? pendingRoute }
set {
pendingRoute = newValue
collector?.route = newValue
}
}
/// Applies the pending route to the freshly wired collector. Called by
/// ``PedalBoard/collectStereo(_:)``.
internal func bind(collector: StereoRouteCollector) {
self.collector = collector
collector.route = pendingRoute
}
}
@@ -0,0 +1,79 @@
// StereoSplitter.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Fans one mono input bus out to two branch buses plus a dry-tap bus.
///
/// Unlike ``Splitter`` (which gates each branch and is consumed by a summing ``Merger``),
/// `StereoSplitter` is the head of a stereo split region: the two branches are processed
/// independently and become the left and right output channels, while the dry tap carries
/// the clean pre-split signal forward so the region's ``StereoRouteCollector`` can offer a
/// `bypass` route. All three outputs carry the full input signal — gating and routing are
/// performed downstream by the collector, not here.
///
/// ## Signal graph
///
/// ```
/// inputBus → In.Audio ──┬── → outputBusLeft (left branch input)
/// ├── → outputBusRight (right branch input)
/// └── → dryTapBus (clean pre-split signal)
/// ```
///
/// - Parameters:
/// - inputBus: Audio bus index to read the mono input from.
/// - outputBusLeft: Audio bus index for the left branch input.
/// - outputBusRight: Audio bus index for the right branch input.
/// - dryTapBus: Audio bus index for the clean pre-split tap.
public final class StereoSplitter: SynthesizerBase {
// MARK: - Routing
/// Audio bus index from which the mono input is read.
public internal(set) var inputBus: Int
/// Audio bus index for the left branch input.
public internal(set) var outputBusLeft: Int
/// Audio bus index for the right branch input.
public internal(set) var outputBusRight: Int
/// Audio bus index for the clean pre-split tap.
public internal(set) var dryTapBus: Int
// MARK: - Init
/// Creates a stereo splitter that fans one input to two branches plus a dry tap.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the mono input from.
/// - outputBusLeft: Audio bus index for the left branch input.
/// - outputBusRight: Audio bus index for the right branch input.
/// - dryTapBus: Audio bus index for the clean pre-split tap.
public init(
inputBus: Int,
outputBusLeft: Int,
outputBusRight: Int,
dryTapBus: Int
) {
self.inputBus = inputBus
self.outputBusLeft = outputBusLeft
self.outputBusRight = outputBusRight
self.dryTapBus = dryTapBus
super.init()
}
// MARK: - Build
/// Builds the signal graph: input copied to both branch buses and the dry-tap bus.
public override func build() {
let inSig: In.Audio = input(bus: inputBus)
output(bus: outputBusLeft, source: inSig)
output(bus: outputBusRight, source: inSig)
output(bus: dryTapBus, source: inSig)
}
}