# ShoeGaze Stereo Routing Design How ShoeGaze handles mono → stereo signal flow inside a `PedalBoard`. ## Background Until now every bus in ShoeGaze was mono (`channelCount: 1`). Pedals were mono-in / mono-out; the handful of stereo DSP blocks (Freeverb, Chorus) folded back to mono with `SumChannels` before leaving `effect(_:)`. Stereo existed only at the very end, where `BoardOutputMaster` used `Pan2` to spread the mono board signal across hardware buses 0/1. This design lets stereo *live inside* the chain: a point in the board can widen mono → stereo, downstream pedals process both channels, and the board emits a true stereo pair. ## Core model: width propagation A board chain carries a **width** — the number of parallel mono buses at the current tail: - `width == 1` → mono (one tail bus). - `width == 2` → stereo (two tail buses: left, right). Stereo is represented as a **pair of mono buses**, not a single 2-channel bus. This reuses all existing mono-bus infrastructure (`Splitter`/`Merger`/`BranchChain` already deal in pairs of mono buses) and makes per-channel duplication trivial. Width is **capped at 2** — a widener placed in a width-2 region is rejected. ``` PedalBoard.width: Int // 1 or 2 PedalBoard.outputBus: AudioBus // left / mono tail PedalBoard.outputBusRight: AudioBus? // right tail (non-nil only when width == 2) ``` ## Pedal: width-parameterized `Pedal` gains an optional right-channel bus pair and a channel-kind declaration. The mono fields (`inputBus`/`outputBus`) keep their meaning (channel 0 / left / mono), so every existing call site and preset is unchanged. When `outputBusRight != nil`, `build()` emits a second channel. ```swift public internal(set) var inputBus: Int // channel 0 (left / mono) public internal(set) var outputBus: Int // channel 0 (left / mono) public internal(set) var inputBusRight: Int? // channel 1 input (nil = mono / broadcast) public internal(set) var outputBusRight: Int? // channel 1 output (nil = mono region) public enum PedalChannelKind { case mono, widener, stereoNative } open var channelKind: PedalChannelKind { .mono } ``` ### Subclass hooks ```swift // mono (default): one channel in, one channel out. Called once per channel. open func effect(_ dry) -> wet // widener (1 → 2): mono in, two distinct wet outs. open func wideEffect(_ dry) -> (left, right) // default: (effect(dry), effect(dry)) // stereoNative (2 → 2): both channels in, coupled, two outs. open func stereoEffect(left, right) -> (left, right) // default: per-channel effect() ``` ### build() dispatch `build()` factors the existing mix → bypass-lag → level chain into one private `applyChain(dry:wet:)` helper, then dispatches on `channelKind`: | kind | left out | right out (only if `outputBusRight != nil`) | |---|---|---| | `mono` | `applyChain(in₀, effect(in₀))` | `applyChain(inR, effect(inR))` — independent graph | | `widener` | `applyChain(in₀, wideEffect.left)` | `applyChain(in₀, wideEffect.right)` | | `stereoNative` | `applyChain(inL, stereoEffect.left)` | `applyChain(inR, stereoEffect.right)` | `effect()` rebuilds its generator graph on each call, so calling it once per channel yields **independent L/R DSP with shared knobs** for free — no manual duplication. The `mix`/`bypass`/ `level` `@UIParam` nodes are read by both channels; each channel gets its own `Lag.Control` so bypass smoothing is per-channel but driven by the same target. For a width-1 (mono) region the output is bit-identical to the previous implementation. ## Four ways stereo appears 1. **Auto-stereoize** (`mono` pedal added while `width == 2`). The board hands it two input and two output buses; `build()` runs `effect()` per channel. One logical pedal, one knob set, two independent DSP graphs. This is "duplicates of a continuation" — the chain just stays stereo. 2. **Widener** (`widener` pedal, `width == 1` only). Mono in, two distinct outs → `width` becomes 2. Reference pedal: `OctaveUpSplitterPedal` (left = dry, right = `Abs`-rectified octave-up, classic Octavia). A widener in a `width == 2` region throws (cap-2 rule). 3. **Stereo-native** (`stereoNative` pedal). One instance reads both buses, couples them, writes both — for effects that can't be two independent monos. Reference pedal: `PingPongDelayPedal` (offset-tap L/R bounce). Entering at `width == 1` broadcasts the mono input to both channels and sets `width = 2`. 4. **`splitStereo()`** — manual fan-out into two independently authored mono chains that *become* the stereo pair (see below). Distinct from auto-stereoize: there you mirror one pedal across both channels; here you build each side by hand. `mergeToMono()` collapses `width 2 → 1` via a `Merger` (sum, ×0.5) when a mono tail is wanted again. ## splitStereo() with route modes `splitStereo()` fans the current mono tail into two `BranchChain`s (reusing the existing flat shared-tail machinery) plus a **dry tap** carried past the branches. `collectStereo(_:)` inserts a `StereoRouteCollector` that produces the final stereo pair according to a route mode and advances `width` to 2. The branches *are* the stereo channels — there is no sum-merge. ```swift let s = try board.splitStereo() try s.left.add(DelayPedal()) // left chain try s.right.add(DelayPedal()) // right chain try board.collectStereo(s) s.route = .leftOnly // settable from code or UI any time ``` ### StereoRoute ```swift public enum StereoRoute { case stereo, leftOnly, rightOnly, bypass } ``` `StereoRouteCollector` reads `inL` (left branch tail), `inR` (right branch tail), and `dry` (pre-split tap). The mode is held as four lagged scalar gains — exactly one active — so switching modes crossfades click-free over 20 ms: ``` outL = inL·(aStereo + aLeftSolo) + inR·aRightSolo + dry·aBypass outR = inR·(aStereo + aRightSolo) + inL·aLeftSolo + dry·aBypass ``` | route | outL | outR | meaning | |---|---|---|---| | `stereo` | inL | inR | normal — left chain → left, right chain → right | | `leftOnly` | inL | inL | **centered** solo of the left chain (both speakers) | | `rightOnly` | inR | inR | centered solo of the right chain | | `bypass` | dry | dry | clean pre-split signal through; branches muted | Solo is centered (mono to both speakers), not a channel mute, so a soloed chain is full and centered rather than lopsided in one speaker. ## Output stage `BoardOutputMaster` becomes width-aware: - `width == 1` → unchanged: `Pan2(mono, center)` → hardware 0/1. - `width == 2` → left tail → hardware 0, right tail → hardware 1, each scaled by `masterGain · select`. `finalizeBoard(_:)` passes the board's right tail bus (if any) to the master. ## What is unchanged - Mono boards behave bit-identically. All existing pedals are `mono` and run width-1 by default. - `PedalState` serializes parameter values only (no bus indices), so presets are unaffected. - `Splitter`/`Merger`/`ParallelSection`/`ChainRouter` keep their existing mono semantics; the stereo path adds new nodes rather than reinterpreting the old ones. ## Files New: - `Routing/PedalChannelKind.swift`, `Routing/StereoRoute.swift` - `Routing/StereoSplitter.swift` (1 in → A, B, dry tap) - `Routing/StereoRouteCollector.swift`, `Routing/StereoSplitSection.swift` - `Pedals/OctaveUpSplitterPedal.swift` (widener), `Pedals/PingPongDelayPedal.swift` (stereoNative) Modified: - `Pedal.swift` (right buses, channel kind, build dispatch) - `PedalBoard.swift` (width tracking, add dispatch, `mergeToMono`, `splitStereo`/`collectStereo`) - `BoardOutputMaster.swift` (width-aware output) - `ShoeGaze.swift` (`finalizeBoard` passes right tail bus) ## UI `Sources/LyrebirdShoeGazeUI/`: - `StereoRouteControlView` — 4-way `StereoRoute` segmented selector + L/R/DRY source LEDs, bound to a `StereoSplitSection.route` (mirrors `SplitterView`). - `StereoSplitSectionView` — route control + the two branch (L/R) pedal rows (mirrors `ParallelSectionView`). - `StereoWidthBadge` — MONO/STEREO pill driven by `PedalBoard.width`. These are standalone public views, like `SplitterView`/`ParallelSectionView`. `collector` is exposed on `StereoSplitSection` so the route control can observe its `@Observable` route gains. ## Presets A saved rig captures each board's full topology as an ordered `[BoardSegmentState]` — `PedalBoard.orderedSegments`, maintained by `add`/`split`/`splitStereo`/`mergeToMono`. A segment is a linear pedal, a parallel section (branch chains + `SplitMode`), a stereo split region (branch chains + `StereoRoute`), or a `mergeToMono` collapse. `captureState()` walks it; `loadRig`/`rebuildBoard` replay each segment via the matching `PedalBoard` API, so split regions and width transitions round-trip faithfully. `applyState` (lightweight recall) also walks segments, restoring branch-pedal params and routing modes positionally. `BoardState` decodes legacy presets (flat `pedals` array, no `segments`) by wrapping each pedal as a `.pedal` segment; new files encode `segments` only. `BoardSegmentState` wire discriminators are the case names — renames are a format break. Wideners and stereo-native pedals (`OctaveUpSplitterPedal` "Octave Splitter", `PingPongDelayPedal` "Ping-Pong") are registered in `PedalRegistry`. Unknown pedal types are skipped on load, including inside branches. The Builder app composes stereo chains directly from the palette — e.g. Overdrive → `splitStereo(Delay | Ping-Pong)` → `collectStereo`, with the four route modes selectable live. Deferred (follow-up): rendering the board's `orderedSegments` directly in a generic board view (today the UI composes pedal rows and section views separately).