diff --git a/Sources/LyrebirdShoeGaze/Routing/StereoSplitSection.swift b/Sources/LyrebirdShoeGaze/Routing/StereoSplitSection.swift index 8f475c0..ae5a8d6 100644 --- a/Sources/LyrebirdShoeGaze/Routing/StereoSplitSection.swift +++ b/Sources/LyrebirdShoeGaze/Routing/StereoSplitSection.swift @@ -49,7 +49,8 @@ public final class StereoSplitSection { internal let dryTapBus: AudioBus /// The route collector, set by ``PedalBoard/collectStereo(_:)``. `nil` until then. - internal var collector: StereoRouteCollector? + /// 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 diff --git a/Sources/LyrebirdShoeGazeUI/StereoRouteControlView.swift b/Sources/LyrebirdShoeGazeUI/StereoRouteControlView.swift new file mode 100644 index 0000000..499b782 --- /dev/null +++ b/Sources/LyrebirdShoeGazeUI/StereoRouteControlView.swift @@ -0,0 +1,185 @@ +// StereoRouteControlView.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import SwiftUI +import LyrebirdUI +import LyrebirdShoeGaze + +/// A compact routing box for a ``StereoSplitSection`` exposing a 4-way ``StereoRoute`` +/// selector and live indicators for the active route and which output channels carry signal. +/// +/// The segmented `Picker` is driven by a computed `Binding` whose `get`/`set` +/// read and write `section.route`, so it stays in sync with programmatic changes. When the +/// section has been collected (its `collector` exists), the view also reads the collector's +/// `@Observable` route gains so the channel LEDs re-render as the route crossfades. +/// +/// Source LEDs show what is feeding the output for the active route: +/// - `stereo` → L and R branch LEDs lit (both chains feed their channel). +/// - `leftOnly` → L lit (left chain, centered to both channels). +/// - `rightOnly` → R lit (right chain, centered to both channels). +/// - `bypass` → DRY lit (clean pre-split signal; both branches muted). +/// +/// ## Usage +/// +/// ```swift +/// StereoRouteControlView(section: stereoSplitSection) +/// ``` +public struct StereoRouteControlView: View { + + // MARK: - Layout constants + + private static let containerCornerRadius: CGFloat = 10 + private static let containerPaddingH: CGFloat = 14 + private static let containerPaddingV: CGFloat = 10 + private static let containerBorderOpacity: Double = 0.55 + private static let containerBorderLineWidth: CGFloat = 1.0 + private static let sectionSpacing: CGFloat = 8 + private static let titleFontSize: CGFloat = 11 + private static let titleOpacity: Double = 0.55 + private static let ledRowSpacing: CGFloat = 16 + private static let ledLabelFontSize: CGFloat = 10 + private static let ledLabelOpacity: Double = 0.60 + private static let ledDiameter: CGFloat = 8 + private static let ledGlowRadius: CGFloat = 5 + private static let ledGlowOpacity: Double = 0.80 + private static let ledDimBrightness: Double = 0.12 + private static let ledRimOpacity: Double = 0.30 + private static let ledRimLineWidth: CGFloat = 0.5 + private static let ledPairSpacing: CGFloat = 6 + /// Background colour components for the routing-box enclosure (slate). + private static let backgroundRed: Double = 0.20 + private static let backgroundGreen: Double = 0.23 + private static let backgroundBlue: Double = 0.28 + /// Width allotted to each segmented label so the four route names fit. + private static let pickerMinWidth: CGFloat = 220 + + /// Short uppercase label shown in the segmented control for each route. + private static func segmentLabel(_ route: StereoRoute) -> String { + switch route { + case .stereo: return "STEREO" + case .leftOnly: return "L" + case .rightOnly: return "R" + case .bypass: return "BYP" + } + } + + // MARK: - Properties + + private let section: StereoSplitSection + + // MARK: - Init + + /// Creates a route control view for the given ``StereoSplitSection``. + /// + /// - Parameter section: The stereo split section whose ``StereoSplitSection/route`` is + /// displayed and edited. + public init(section: StereoSplitSection) { + self.section = section + } + + // MARK: - Body + + /// The active route, after touching the collector's `@Observable` route gains so SwiftUI + /// re-renders the LEDs as the route crossfades or changes programmatically. + private var liveRoute: StereoRoute { + _ = section.collector?.$gainStereo.value + _ = section.collector?.$gainLeftSolo.value + _ = section.collector?.$gainRightSolo.value + _ = section.collector?.$gainBypass.value + return section.route + } + + public var body: some View { + let routeBinding: Binding = Binding( + get: { section.route }, + set: { section.route = $0 } + ) + let active: StereoRoute = liveRoute + + VStack(spacing: StereoRouteControlView.sectionSpacing) { + Text("STEREO ROUTE") + .font(.system(size: StereoRouteControlView.titleFontSize, weight: .semibold, design: .monospaced)) + .foregroundStyle(Color.white.opacity(StereoRouteControlView.titleOpacity)) + + Picker("Route", selection: routeBinding) { + ForEach(StereoRoute.allCases, id: \.self) { route in + Text(StereoRouteControlView.segmentLabel(route)) + .tag(route) + } + } + .pickerStyle(.segmented) + .frame(minWidth: StereoRouteControlView.pickerMinWidth) + + sourceLEDs(active: active) + } + .padding(.horizontal, StereoRouteControlView.containerPaddingH) + .padding(.vertical, StereoRouteControlView.containerPaddingV) + .background( + RoundedRectangle(cornerRadius: StereoRouteControlView.containerCornerRadius) + .fill(Color( + red: StereoRouteControlView.backgroundRed, + green: StereoRouteControlView.backgroundGreen, + blue: StereoRouteControlView.backgroundBlue + )) + .overlay( + RoundedRectangle(cornerRadius: StereoRouteControlView.containerCornerRadius) + .stroke( + Color.white.opacity(StereoRouteControlView.containerBorderOpacity), + lineWidth: StereoRouteControlView.containerBorderLineWidth + ) + ) + ) + } + + // MARK: - Subviews + + /// L / R / DRY source LEDs reflecting what feeds the output for the active route. + private func sourceLEDs(active: StereoRoute) -> some View { + let leftLit: Bool = (active == .stereo || active == .leftOnly) + let rightLit: Bool = (active == .stereo || active == .rightOnly) + let dryLit: Bool = (active == .bypass) + return HStack(spacing: StereoRouteControlView.ledRowSpacing) { + sourceLED(label: "L", lit: leftLit) + sourceLED(label: "R", lit: rightLit) + sourceLED(label: "DRY", lit: dryLit) + } + } + + /// A single labelled LED dot for one signal source. + private func sourceLED(label: String, lit: Bool) -> some View { + VStack(spacing: StereoRouteControlView.ledPairSpacing) { + Text(label) + .font(.system(size: StereoRouteControlView.ledLabelFontSize, weight: .medium, design: .monospaced)) + .foregroundStyle(Color.white.opacity(StereoRouteControlView.ledLabelOpacity)) + ZStack { + if lit { + Circle() + .fill(Color.cyan.opacity(StereoRouteControlView.ledGlowOpacity)) + .frame( + width: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius, + height: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius + ) + .blur(radius: StereoRouteControlView.ledGlowRadius / 2) + } + Circle() + .fill(lit ? Color.cyan : Color(white: StereoRouteControlView.ledDimBrightness)) + .frame(width: StereoRouteControlView.ledDiameter, height: StereoRouteControlView.ledDiameter) + .overlay( + Circle() + .stroke( + Color.white.opacity(StereoRouteControlView.ledRimOpacity), + lineWidth: StereoRouteControlView.ledRimLineWidth + ) + .frame(width: StereoRouteControlView.ledDiameter, height: StereoRouteControlView.ledDiameter) + ) + } + .frame( + width: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius, + height: StereoRouteControlView.ledDiameter + StereoRouteControlView.ledGlowRadius + ) + } + } +} diff --git a/Sources/LyrebirdShoeGazeUI/StereoSplitSectionView.swift b/Sources/LyrebirdShoeGazeUI/StereoSplitSectionView.swift new file mode 100644 index 0000000..97994f5 --- /dev/null +++ b/Sources/LyrebirdShoeGazeUI/StereoSplitSectionView.swift @@ -0,0 +1,108 @@ +// StereoSplitSectionView.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import SwiftUI +import LyrebirdUI +import LyrebirdShoeGaze + +/// Renders a ``StereoSplitSection`` as a horizontal stereo rack. +/// +/// Layout (left → right): +/// 1. ``StereoRouteControlView`` — the 4-way ``StereoRoute`` selector and source LEDs. +/// 2. A central panel with two labelled branch rows that become the stereo channels: +/// - **L** row: one ``PedalView`` per pedal in `section.left`. +/// - **R** row: one ``PedalView`` per pedal in `section.right`. +/// Empty branches show a placeholder (the split signal passes straight through). +/// +/// The route collector and dry tap are owned by `PedalBoard`, not the section, so only +/// the route control and the two branch rows are rendered here. The central panel is +/// wrapped in a horizontal `ScrollView` so wide branch chains do not overflow. +/// +/// ## Usage +/// +/// ```swift +/// StereoSplitSectionView(section: stereoSplitSection) +/// ``` +public struct StereoSplitSectionView: View { + + // MARK: - Layout constants + + private static let outerSpacing: CGFloat = 16 + private static let branchStackSpacing: CGFloat = 12 + private static let pedalSpacing: CGFloat = 16 + private static let branchLabelFontSize: CGFloat = 11 + private static let branchLabelOpacity: Double = 0.55 + private static let emptyBranchOpacity: Double = 0.30 + private static let emptyBranchFontSize: CGFloat = 11 + private static let scrollPaddingH: CGFloat = 12 + private static let scrollPaddingV: CGFloat = 8 + private static let branchSeparatorOpacity: Double = 0.15 + private static let branchSeparatorHeight: CGFloat = 1 + private static let branchLabelWidth: CGFloat = 18 + + // MARK: - Properties + + private let section: StereoSplitSection + + // MARK: - Init + + /// Creates a stereo split section view. + /// + /// - Parameter section: The `StereoSplitSection` to render. Both branch pedal arrays + /// and the route are read from this instance. + public init(section: StereoSplitSection) { + self.section = section + } + + // MARK: - Body + + public var body: some View { + HStack(alignment: .top, spacing: StereoSplitSectionView.outerSpacing) { + StereoRouteControlView(section: section) + + ScrollView(.horizontal, showsIndicators: false) { + VStack(alignment: .leading, spacing: StereoSplitSectionView.branchStackSpacing) { + branchRow(label: "L", pedals: section.left.pedals) + + Rectangle() + .fill(Color.white.opacity(StereoSplitSectionView.branchSeparatorOpacity)) + .frame(height: StereoSplitSectionView.branchSeparatorHeight) + + branchRow(label: "R", pedals: section.right.pedals) + } + .padding(.horizontal, StereoSplitSectionView.scrollPaddingH) + .padding(.vertical, StereoSplitSectionView.scrollPaddingV) + } + } + } + + // MARK: - Subviews + + /// A labelled row of pedal cards for one stereo channel. + private func branchRow(label: String, pedals: [Pedal]) -> some View { + HStack(alignment: .bottom, spacing: StereoSplitSectionView.pedalSpacing) { + Text(label) + .font(.system( + size: StereoSplitSectionView.branchLabelFontSize, + weight: .semibold, + design: .monospaced + )) + .foregroundStyle(Color.white.opacity(StereoSplitSectionView.branchLabelOpacity)) + .frame(width: StereoSplitSectionView.branchLabelWidth, alignment: .leading) + + if pedals.isEmpty { + Text("(empty)") + .font(.system(size: StereoSplitSectionView.emptyBranchFontSize)) + .foregroundStyle(Color.white.opacity(StereoSplitSectionView.emptyBranchOpacity)) + } else { + ForEach(Array(pedals.enumerated()), id: \.offset) { pair in + let pedal: Pedal = pair.element + PedalView(pedal: pedal) + } + } + } + } +} diff --git a/Sources/LyrebirdShoeGazeUI/StereoWidthBadge.swift b/Sources/LyrebirdShoeGazeUI/StereoWidthBadge.swift new file mode 100644 index 0000000..adb7337 --- /dev/null +++ b/Sources/LyrebirdShoeGazeUI/StereoWidthBadge.swift @@ -0,0 +1,75 @@ +// StereoWidthBadge.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import SwiftUI + +/// A small pill badge indicating whether a chain point is mono (width 1) or stereo (width 2). +/// +/// Pass `PedalBoard.width` (or any channel count). Width `2` shows a cyan "STEREO" pill; +/// anything else shows a dim "MONO" pill. Width is structural (it changes only when pedals +/// are added or removed), so the badge does not need to observe live parameter state — the +/// surrounding board view rebuilds when the chain changes. +/// +/// ## Usage +/// +/// ```swift +/// StereoWidthBadge(width: board.width) +/// ``` +public struct StereoWidthBadge: View { + + // MARK: - Layout constants + + private static let fontSize: CGFloat = 9 + private static let paddingH: CGFloat = 7 + private static let paddingV: CGFloat = 3 + private static let cornerRadius: CGFloat = 4 + private static let stereoWidth: Int = 2 + private static let monoFillOpacity: Double = 0.14 + private static let stereoFillOpacity: Double = 0.85 + private static let monoTextOpacity: Double = 0.45 + private static let borderOpacity: Double = 0.30 + private static let borderLineWidth: CGFloat = 0.5 + + // MARK: - Properties + + private let width: Int + + // MARK: - Init + + /// Creates a width badge. + /// + /// - Parameter width: Channel count at this chain point (`PedalBoard.width`). `2` renders + /// the stereo badge; any other value renders the mono badge. + public init(width: Int) { + self.width = width + } + + // MARK: - Body + + public var body: some View { + let isStereo: Bool = (width == StereoWidthBadge.stereoWidth) + Text(isStereo ? "STEREO" : "MONO") + .font(.system(size: StereoWidthBadge.fontSize, weight: .bold, design: .monospaced)) + .foregroundStyle(isStereo ? Color.black : Color.white.opacity(StereoWidthBadge.monoTextOpacity)) + .padding(.horizontal, StereoWidthBadge.paddingH) + .padding(.vertical, StereoWidthBadge.paddingV) + .background( + RoundedRectangle(cornerRadius: StereoWidthBadge.cornerRadius) + .fill( + isStereo + ? Color.cyan.opacity(StereoWidthBadge.stereoFillOpacity) + : Color.white.opacity(StereoWidthBadge.monoFillOpacity) + ) + .overlay( + RoundedRectangle(cornerRadius: StereoWidthBadge.cornerRadius) + .stroke( + Color.white.opacity(StereoWidthBadge.borderOpacity), + lineWidth: StereoWidthBadge.borderLineWidth + ) + ) + ) + } +} diff --git a/docs/stereo-routing-design.md b/docs/stereo-routing-design.md index 3b41f1c..46ec371 100644 --- a/docs/stereo-routing-design.md +++ b/docs/stereo-routing-design.md @@ -169,5 +169,26 @@ Modified: - `BoardOutputMaster.swift` (width-aware output) - `ShoeGaze.swift` (`finalizeBoard` passes right tail bus) -Deferred (follow-up): SwiftUI views for the route segmented control and stereo width indicators; -preset round-trip of stereo split topology. +## UI + +`Sources/LyrebirdShoeGazeUI/`: +- `StereoRouteControlView` — 4-way `StereoRoute` segmented selector + L/R/DRY source LEDs, + bound to a `StereoSplitSection.route` (mirrors `SplitterView`). +- `StereoSplitSectionView` — route control + the two branch (L/R) pedal rows (mirrors + `ParallelSectionView`). +- `StereoWidthBadge` — MONO/STEREO pill driven by `PedalBoard.width`. + +These are standalone public views, like `SplitterView`/`ParallelSectionView`. `collector` is +exposed on `StereoSplitSection` so the route control can observe its `@Observable` route gains. + +## Presets + +A saved rig captures each board's *linear* `pedals` chain only (unchanged). Wideners and +stereo-native pedals added via `board.add(_:)` are in that chain, so they round-trip — the +mono → stereo width transition replays on load. `OctaveUpSplitterPedal` ("Octave Splitter") and +`PingPongDelayPedal` ("Ping-Pong") are registered in `PedalRegistry`. Split-region *topology* +(`splitStereo()` branches) is not serialized — the same limitation as parallel `split()`. + +Deferred (follow-up): wiring a `.stereo` mode into the Demo Xcode app (DemoRootView + +ShoeGazeDemoController + project), and serializing split-region topology (would also need to +cover parallel sections for consistency).