Files
josh 1a09cf9cf8 Add Stereo Unzip: process a stereo signal's L/R independently
A stereo-output pedal (Ping-Pong, Octave Splitter) now shows a
"Stereo flow / Split L/R" toggle on its card:

- Stereo flow (default): unchanged broadcast behavior — following pedals
  process both channels as one stereo flow.
- Split L/R: inserts a Stereo Unzip region right after the pedal, routing
  the L channel into one lane and R into the other for independent
  processing, then recombining to stereo with the same route/solo/bypass
  control as the stereo split.

Engine/core:
- StereoUnzipSplitter: 2-in -> 3-out node (L->lane L, R->lane R, and a
  centered 0.5*L+0.5*R dry tap for the bypass route).
- PedalBoard.unzipStereo() mirrors splitStereo() but reads both tails and
  requires width 2; reuses collectStereo / StereoSplitSection /
  StereoRouteCollector. New stereoUnzipSections (cleared on teardown).
- BoardSegment/.stereoUnzip + StereoUnzipState round-trip via preset
  capture/rebuild/applyState.
- ShoeGaze.insertStereoUnzip(afterSegment:)/isStereoUnzip(afterSegment:)
  drive the toggle; invalid insert (mono tail) throws
  cannotUnzipMonoChain and the atomic rebuild keeps the board intact.
- StereoSplitSection.splitter relaxed to `any Synthesizer` (stored only)
  so it can hold either splitter type.

UI: per-pedal toggle + STEREO UNZIP region rendering in BoardFlowView.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 10:21:18 -07:00

103 lines
3.7 KiB
Swift

// 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 head node that feeds the two branches plus the dry tap — a ``StereoSplitter`` for a
/// mono split, or a ``StereoUnzipSplitter`` for a stereo unzip. Stored for reference only.
public let splitter: any Synthesizer
/// 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.
/// Exposed for UI binding (its route gains are `@Observable`).
public internal(set) 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 head node 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: any Synthesizer,
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
}
}