// ParallelSection.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// A parallel signal-routing section inside a ``PedalBoard``. /// /// A `ParallelSection` is created by ``PedalBoard/split()`` and holds the two independent /// branch sub-chains plus the ``Splitter`` node that feeds them. After adding pedals to each /// branch, pass the section to ``PedalBoard/merge(_:)`` to combine the branches back into /// the main board chain. /// /// ## Typical usage /// /// ```swift /// let section = try board.split() /// try section.branchA.add(DelayPedal()) /// try section.branchB.add(FuzzPedal()) /// try board.merge(section) /// // Further board.add(_:) calls chain after the merger. /// ``` /// /// ## Evaluation order /// /// Both branches share a single `SharedTail` tracking pointer so all pedals from both /// branches are inserted in a flat linear sequence in the node tree. Signal-flow parallelism /// is expressed via independent buses, not via tree structure: /// /// ``` /// … → Splitter → [branchA pedals] → [branchB pedals] → Merger → … /// ``` /// /// The merger is inserted after the `SharedTail` — i.e. after the last pedal added across /// either branch — so all branch pedals have written their output buses before the merger reads /// them. This guarantees correctness regardless of the order in which the caller adds pedals to /// the two branches. @MainActor public final class ParallelSection { // MARK: - Nodes and chains /// The splitter that routes the pre-section signal into the two branches. public let splitter: Splitter /// Sub-chain A of this parallel section. public let branchA: BranchChain /// Sub-chain B of this parallel section. public let branchB: BranchChain /// Shared tail pointer updated by both branch chains. Used by ``PedalBoard/merge(_:)`` /// to determine the node after which the merger should be inserted. internal let sharedTail: SharedTail // MARK: - Init /// Creates a parallel section. Called internally by ``PedalBoard/split()``. /// /// - Parameters: /// - splitter: The ``Splitter`` node that fans out into the two branches. /// - branchA: The first branch sub-chain, fed by the splitter's A output bus. /// - branchB: The second branch sub-chain, fed by the splitter's B output bus. /// - sharedTail: The shared tail tracking pointer used by both branch chains. init( splitter: Splitter, branchA: BranchChain, branchB: BranchChain, sharedTail: SharedTail ) { self.splitter = splitter self.branchA = branchA self.branchB = branchB self.sharedTail = sharedTail } }