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
+173
View File
@@ -0,0 +1,173 @@
# 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)
Deferred (follow-up): SwiftUI views for the route segmented control and stereo width indicators;
preset round-trip of stereo split topology.