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,126 @@
// OctaveUpSplitterPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Widener pedal: clean signal on the left, an octave-up fuzz on the right (1 → 2).
///
/// A ``PedalChannelKind/widener``. It takes a mono input and produces two distinct wet
/// channels, widening the board chain from mono to stereo:
///
/// - **Left**: the dry signal, passed through clean.
/// - **Right**: an octave-up voice produced by full-wave rectification (`Abs`, which doubles
/// the perceived frequency), DC-removed, driven into a `tanh` saturator, and tone-shaped —
/// the classic "Octavia"-style octave fuzz.
///
/// After this pedal the chain is stereo; subsequent mono pedals auto-stereoize across both
/// channels, so you can, for example, add a delay that processes the clean and octave voices
/// independently.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - drive: Pre-gain into the octave-channel saturator. Higher = thicker octave fuzz.
/// Range `[1, 40]`. Defaults to `12.0`.
/// - tone: Octave-channel low-pass cutoff in Hz. Range `[500, 8000]`. Defaults to `4000.0`.
public final class OctaveUpSplitterPedal: Pedal {
// MARK: - Parameters
/// Pre-gain into the octave-channel saturator. Range [1, 40]; higher = thicker octave fuzz.
@UIParam public var drive: Double = 12.0
/// Octave-channel tone low-pass cutoff in Hz. Range [500, 8000].
@UIParam public var tone: Double = 4000.0
// MARK: - Constants
/// Gain applied to the rectified signal before drive, compensating the level drop from
/// full-wave rectification.
private let rectifyGain: Double = 2.0
/// DC-removal high-pass cutoff in Hz. Rectification introduces a DC offset that must be
/// removed before the octave voice reaches the output.
private let dcBlockFreq: Double = 30.0
/// Q factor for the tone low-pass (Butterworth, no resonant peak).
private let toneQ: Double = 0.707
/// High-pass Q (Butterworth).
private let dcBlockQ: Double = 0.707
/// Output trim after the saturator (`tanh` output is roughly ±1).
private let outputTrim: Double = 0.6
// MARK: - UI knobs
/// Drive knob range (octave-channel saturator pre-gain).
private let driveRange: ClosedRange<Double> = 1.0 ... 40.0
/// Tone knob range in Hz (octave-channel low-pass cutoff).
private let toneRange: ClosedRange<Double> = 500.0 ... 8000.0
// MARK: - Channel kind
/// This pedal widens mono → stereo.
public override var channelKind: PedalChannelKind { .widener }
// MARK: - Init
/// Creates an octave-up splitter pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the dry input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the left output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(
name: "Octave Splitter",
inputBus: inputBus,
outputBus: outputBus,
initialMix: 1.0
)
}
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Drive", node: $drive, range: driveRange),
PedalKnob(label: "Tone", node: $tone, range: toneRange),
]
}
// MARK: - Effect
/// Produces the clean left channel and the octave-up right channel.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: `(left: clean dry, right: octave-up fuzz)`.
public override func wideEffect(_ dry: any ValidGeneratorInput)
-> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) {
// Full-wave rectification doubles the perceived frequency (octave up).
let rectified: Abs.Audio = Abs.Audio(dry, mul: rectifyGain)
// Remove the DC offset rectification introduces before it reaches the output.
let dcBlocked: RBJHighPass.Audio = RBJHighPass.Audio(
input: rectified,
freq: dcBlockFreq,
q: dcBlockQ
)
// Drive into a saturator for the octave-fuzz texture, then tone-shape.
let driven: any ValidGeneratorInput = dcBlocked * $drive
let saturated: Tanh.Audio = Tanh.Audio(driven, mul: outputTrim)
let shaped: RBJLowPass.Audio = RBJLowPass.Audio(
input: saturated,
freq: $tone,
q: toneQ
)
return (left: dry, right: shaped)
}
}
@@ -0,0 +1,136 @@
// PingPongDelayPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Stereo-native ping-pong delay: echoes bounce left → right across the stereo field.
///
/// A ``PedalChannelKind/stereoNative`` pedal — a single instance reads both input channels,
/// couples them, and writes both outputs (it cannot be expressed as two independent mono
/// pedals). When placed in a mono region the mono input broadcasts to both channels and the
/// chain widens to stereo; in a stereo region it sums the two inputs as the delay feed.
///
/// ## Signal chain (inside `stereoEffect(left:right:)`)
///
/// ```
/// feed = (left + right) · 0.5
/// leftWet = DelayLineFeedback(feed, delay: time, decayTime: feedback) // echoes at t, 2t, 3t …
/// rightWet = DelayLine(leftWet, delay: time) // L delayed one tap → t later
/// ```
///
/// The right channel is the left channel's echo train delayed by one further tap, so each
/// repeat lands first on the left, then on the right — the characteristic ping-pong bounce.
/// The cross-channel dependency (right derived from the processed left) is what makes this
/// stereo-native rather than two independent delays.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - time: Delay time per tap in seconds. Range `[0.05, 1.0]`. Defaults to `0.3`.
/// - feedback: Feedback as decay time in seconds (time for echoes to fall to −60 dB).
/// Range `[0.05, 6.0]`. Defaults to `1.2`.
public final class PingPongDelayPedal: Pedal {
// MARK: - Parameters
/// Delay time per tap in seconds. Range [0.05, 1.0].
@UIParam public var time: Double = 0.3
/// Feedback decay time in seconds (time to reach −60 dB amplitude). Range [0.05, 6.0].
@UIParam public var feedback: Double = 1.2
// MARK: - Constants
/// Maximum supported delay time in seconds. Sizes both delay lines at init.
private let maxDelayTime: Double = 1.0
/// Amplitude target for the `decayTime` calculation (−60 dB).
private let targetAmp: Double = 0.001
/// Gain applied when summing the two input channels into the mono delay feed.
private let feedGain: Double = 0.5
/// Default wet/dry blend — partial so dry and echoes coexist.
private static let defaultMix: Double = 0.4
// MARK: - UI knobs
/// Delay-time knob range in seconds.
private let timeRange: ClosedRange<Double> = 0.05 ... 1.0
/// Feedback (decay-time) knob range in seconds.
private let feedbackRange: ClosedRange<Double> = 0.05 ... 6.0
// MARK: - Channel kind
/// This pedal is stereo-native (cross-channel coupling).
public override var channelKind: PedalChannelKind { .stereoNative }
// MARK: - Init
/// Creates a ping-pong delay pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the left/mono input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the left output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(
name: "Ping-Pong",
inputBus: inputBus,
outputBus: outputBus,
initialMix: PingPongDelayPedal.defaultMix
)
}
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Time", node: $time, range: timeRange),
PedalKnob(label: "Feedback", node: $feedback, range: feedbackRange),
]
}
// MARK: - Effect
/// Builds the cross-coupled ping-pong delay. The right channel is the left echo train
/// delayed by one further tap, producing the left → right bounce.
///
/// - Parameters:
/// - left: Left dry channel.
/// - right: Right dry channel (equals `left` when entering a mono region).
/// - Returns: `(left: feedback echoes, right: echoes delayed one tap)`.
public override func stereoEffect(
left: any ValidGeneratorInput,
right: any ValidGeneratorInput
) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) {
// Sum both inputs to a mono feed; a mono region passes left == right (no change).
let feed: any ValidGeneratorInput = (left + right) * feedGain
let leftWet: DelayLineFeedback = DelayLineFeedback(
input: feed,
maxDelay: maxDelayTime,
delay: $time,
decayTime: $feedback,
targetAmp: targetAmp,
sampleRate: sampleRate,
interpolation: .linear
)
// Right is the left echo train delayed one more tap → the L → R bounce.
let rightWet: DelayLine = DelayLine(
input: leftWet,
maxDelay: maxDelayTime,
delay: $time,
sampleRate: sampleRate,
interpolation: .linear
)
return (left: leftWet, right: rightWet)
}
}