Files
ShoeGaze/Sources/LyrebirdShoeGaze/Routing/StereoRouteCollector.swift
T
josh f89229282f 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>
2026-06-21 16:48:25 -07:00

143 lines
5.8 KiB
Swift

// 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)
}
}