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