5522109689
New DemoMode.stereo: Overdrive → splitStereo(Delay | Ping-Pong) → collectStereo, finalized to stereo output. DemoRootView renders the pre-split board + StereoWidthBadge above StereoSplitSectionView, so the four StereoRoute modes are selectable live. No new files / no .pbxproj change — the demo already links LyrebirdShoeGazeUI. BUILD SUCCEEDED. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
318 lines
11 KiB
Swift
318 lines
11 KiB
Swift
// ShoeGazeDemoController.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#if os(macOS)
|
|
import Observation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
import LyrebirdShoeGaze
|
|
|
|
// MARK: - DemoMode
|
|
|
|
/// The active showcase mode displayed in the demo window.
|
|
///
|
|
/// Each case corresponds to one full-window showcase of a distinct ShoeGaze feature.
|
|
/// Switching modes tears down the current audio graph and rebuilds it fresh.
|
|
enum DemoMode: String, CaseIterable, Identifiable {
|
|
|
|
/// Two full boards, each with four pedals (Board 1: Overdrive → Delay → Reverb → Modulation;
|
|
/// Board 2: Fuzz → Tremolo → Phaser → Compressor).
|
|
case boards = "Boards"
|
|
|
|
/// A single board inside `BoardEditorView` with preset version snapshots pre-populated.
|
|
case editor = "Editor"
|
|
|
|
/// A single board with a ``ParallelSection`` (Overdrive → split → Delay|Fuzz → merge → Reverb).
|
|
case parallel = "Parallel"
|
|
|
|
/// A ``ChainRouter`` that splits one input across two independent boards (A: Overdrive + Delay;
|
|
/// B: Fuzz + Reverb) and recombines them through a shared master stage.
|
|
case router = "Router"
|
|
|
|
/// A board with a ``StereoSplitSection`` (Overdrive → splitStereo(Delay | PingPong) →
|
|
/// collectStereo) showcasing the four ``StereoRoute`` modes and stereo output.
|
|
case stereo = "Stereo"
|
|
|
|
var id: String { rawValue }
|
|
|
|
/// Short human-readable title used in the mode picker.
|
|
var title: String {
|
|
switch self {
|
|
case .boards: return "Boards"
|
|
case .editor: return "Editor"
|
|
case .parallel: return "Parallel"
|
|
case .router: return "Router"
|
|
case .stereo: return "Stereo"
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ShoeGazeDemoController
|
|
|
|
/// Main-actor observable controller that owns the engine and active ShoeGaze session.
|
|
///
|
|
/// A single `ShoeGaze` instance plus optional `router` / `parallelSection` hold the
|
|
/// current showcase. Switching modes via ``select(_:)`` or changing the input source via
|
|
/// ``setInputSource(_:)`` calls ``rebuild()`` which:
|
|
///
|
|
/// 1. Stops the engine if it is running.
|
|
/// 2. Calls `shoeGaze.teardown()` to remove all nodes and release all buses.
|
|
/// 3. Replaces `shoeGaze` with a fresh instance.
|
|
/// 4. Builds the new mode-specific graph.
|
|
/// 5. Restarts the engine if it was running before the rebuild.
|
|
///
|
|
/// This guarantees the engine never holds stale references across mode switches.
|
|
@MainActor
|
|
@Observable
|
|
final class ShoeGazeDemoController {
|
|
|
|
// MARK: - Public state
|
|
|
|
/// The synthesis engine. Callers must not call `engine.start()` or `engine.stop()`
|
|
/// directly — use ``start()`` and ``stop()`` so `isRunning` stays consistent.
|
|
let engine: SynthesisEngine
|
|
|
|
/// The active ShoeGaze session. Replaced on every ``rebuild()`` call.
|
|
private(set) var shoeGaze: ShoeGaze
|
|
|
|
/// Non-nil only in `.router` mode, after ``buildRouter()`` succeeds.
|
|
private(set) var router: ChainRouter?
|
|
|
|
/// Non-nil only in `.parallel` mode, after ``buildParallel()`` succeeds.
|
|
private(set) var parallelSection: ParallelSection?
|
|
|
|
/// Non-nil only in `.stereo` mode, after ``buildStereo()`` succeeds.
|
|
private(set) var stereoSection: StereoSplitSection?
|
|
|
|
/// Stores named board snapshots for the `.editor` showcase.
|
|
let versionStore: BoardVersionStore = BoardVersionStore()
|
|
|
|
/// Currently selected showcase mode.
|
|
private(set) var mode: DemoMode = .boards
|
|
|
|
/// Current input source policy.
|
|
private(set) var inputSource: InputSource = .testLoop
|
|
|
|
/// `true` while the engine is running. Derived from the engine (the single source
|
|
/// of truth) so it stays correct regardless of which control starts/stops it.
|
|
var isRunning: Bool { engine.isRunning }
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates a controller, builds the default `.boards` showcase, and does NOT start the engine.
|
|
///
|
|
/// - Parameter engine: A pre-configured `SynthesisEngine` owned by the caller. The
|
|
/// engine must not be running when this initialiser is called.
|
|
init(engine: SynthesisEngine) {
|
|
self.engine = engine
|
|
self.shoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
|
buildBoards()
|
|
}
|
|
|
|
// MARK: - Mode selection
|
|
|
|
/// Switches to the given mode if different from the current one, triggering a full rebuild.
|
|
///
|
|
/// - Parameter newMode: The showcase to activate.
|
|
func select(_ newMode: DemoMode) {
|
|
guard newMode != mode else { return }
|
|
mode = newMode
|
|
rebuild()
|
|
}
|
|
|
|
// MARK: - Input source
|
|
|
|
/// Changes the input source policy and triggers a full rebuild.
|
|
///
|
|
/// - Parameter source: `.testLoop` for the built-in test signal or `.liveInput(channel:)`
|
|
/// for hardware microphone/instrument input on the given channel.
|
|
func setInputSource(_ source: InputSource) {
|
|
inputSource = source
|
|
rebuild()
|
|
}
|
|
|
|
// MARK: - Transport
|
|
|
|
/// Starts the engine. Updates ``isRunning`` on success; prints on failure.
|
|
func start() {
|
|
do {
|
|
try engine.start()
|
|
} catch {
|
|
print("[ShoeGazeDemoController] engine.start() failed: \(error)")
|
|
}
|
|
}
|
|
|
|
/// Stops the engine and updates ``isRunning``.
|
|
func stop() {
|
|
engine.stop()
|
|
}
|
|
|
|
/// Toggles audio output on/off (mute). Muting silences output by stopping the engine;
|
|
/// unmuting restarts it. `isRunning == false` is the "muted" (silent) state.
|
|
func toggleMute() {
|
|
if isRunning {
|
|
stop()
|
|
} else {
|
|
start()
|
|
}
|
|
}
|
|
|
|
// MARK: - Rebuild
|
|
|
|
/// Tears down the current session and rebuilds the active mode.
|
|
///
|
|
/// The engine is stopped before teardown and restarted afterward if it was running,
|
|
/// satisfying the remove-before-release invariant documented on `ShoeGaze.teardown()`.
|
|
private func rebuild() {
|
|
let wasRunning: Bool = isRunning
|
|
if wasRunning { stop() }
|
|
|
|
shoeGaze.teardown()
|
|
router = nil
|
|
parallelSection = nil
|
|
stereoSection = nil
|
|
|
|
shoeGaze = ShoeGaze(engine: engine, inputSource: inputSource)
|
|
|
|
switch mode {
|
|
case .boards: buildBoards()
|
|
case .editor: buildEditor()
|
|
case .parallel: buildParallel()
|
|
case .router: buildRouter()
|
|
case .stereo: buildStereo()
|
|
}
|
|
|
|
if wasRunning { start() }
|
|
}
|
|
|
|
// MARK: - Mode builders
|
|
|
|
/// `.boards` — three full boards covering all twelve pedal types.
|
|
///
|
|
/// Board 1: Overdrive → Delay → Reverb → Modulation
|
|
/// Board 2: Fuzz → Tremolo → Phaser → Compressor
|
|
/// Board 3: Mu-Tron → Auto-Wah → Feedback Delay → Multi-Tap Delay
|
|
private func buildBoards() {
|
|
do {
|
|
let board1: PedalBoard = try shoeGaze.addBoard()
|
|
board1.label = "Board 1"
|
|
try board1.add(OverdrivePedal())
|
|
try board1.add(DelayPedal())
|
|
try board1.add(ReverbPedal())
|
|
try board1.add(ModulationPedal())
|
|
try shoeGaze.finalizeBoard(board1)
|
|
|
|
let board2: PedalBoard = try shoeGaze.addBoard()
|
|
board2.label = "Board 2"
|
|
try board2.add(FuzzPedal())
|
|
try board2.add(TremoloPedal())
|
|
try board2.add(PhaserPedal())
|
|
try board2.add(CompressorPedal())
|
|
try shoeGaze.finalizeBoard(board2)
|
|
|
|
let board3: PedalBoard = try shoeGaze.addBoard()
|
|
board3.label = "Board 3"
|
|
try board3.add(MuTronPedal())
|
|
try board3.add(AutoWahPedal())
|
|
try board3.add(FeedbackDelayPedal())
|
|
try board3.add(MultiTapDelayPedal())
|
|
try shoeGaze.finalizeBoard(board3)
|
|
} catch {
|
|
print("[ShoeGazeDemoController] buildBoards failed: \(error)")
|
|
}
|
|
}
|
|
|
|
/// `.editor` — one board inside `BoardEditorView` with two pre-captured versions.
|
|
///
|
|
/// The pre-populated version entries ("Crunch" and "Clean Shimmer") ensure the
|
|
/// Versions menu is not empty when the showcase first appears.
|
|
private func buildEditor() {
|
|
do {
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
board.label = "Editor Board"
|
|
try board.add(OverdrivePedal())
|
|
try board.add(FuzzPedal())
|
|
try board.add(DelayPedal())
|
|
try board.add(ReverbPedal())
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
// Pre-populate two named snapshots so the Versions menu is immediately useful.
|
|
versionStore.capture(board, name: "Crunch")
|
|
versionStore.capture(board, name: "Clean Shimmer")
|
|
} catch {
|
|
print("[ShoeGazeDemoController] buildEditor failed: \(error)")
|
|
}
|
|
}
|
|
|
|
/// `.parallel` — Overdrive → split(Delay | Fuzz) → merge → Reverb.
|
|
///
|
|
/// Stores the `ParallelSection` in `parallelSection` so `DemoRootView` can display
|
|
/// `ParallelSectionView`.
|
|
private func buildParallel() {
|
|
do {
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
board.label = "Parallel Board"
|
|
try board.add(OverdrivePedal())
|
|
|
|
let section: ParallelSection = try board.split()
|
|
try section.branchA.add(DelayPedal())
|
|
try section.branchB.add(FuzzPedal())
|
|
try board.merge(section)
|
|
|
|
try board.add(ReverbPedal())
|
|
try shoeGaze.finalizeBoard(board)
|
|
|
|
parallelSection = section
|
|
} catch {
|
|
print("[ShoeGazeDemoController] buildParallel failed: \(error)")
|
|
}
|
|
}
|
|
|
|
/// `.router` — ChainRouter with two independent chains: A (Overdrive + Delay),
|
|
/// B (Fuzz + Reverb), merged through a shared master stage.
|
|
///
|
|
/// Stores the `ChainRouter` in `router` so `DemoRootView` can display
|
|
/// `ChainRouterView`.
|
|
private func buildRouter() {
|
|
do {
|
|
let r: ChainRouter = try shoeGaze.makeChainRouter()
|
|
try r.addToA(OverdrivePedal())
|
|
try r.addToA(DelayPedal())
|
|
try r.addToB(FuzzPedal())
|
|
try r.addToB(ReverbPedal())
|
|
try r.finalize()
|
|
router = r
|
|
} catch {
|
|
print("[ShoeGazeDemoController] buildRouter failed: \(error)")
|
|
}
|
|
}
|
|
|
|
/// `.stereo` — Overdrive → splitStereo(left: Delay | right: Ping-Pong) → collectStereo.
|
|
///
|
|
/// Demonstrates the stereo split mechanism: a mono signal fanned into two independently
|
|
/// authored chains that become the left and right output channels, with the four
|
|
/// ``StereoRoute`` modes (stereo / left / right / bypass) selectable live. Stores the
|
|
/// `StereoSplitSection` so `DemoRootView` can display `StereoSplitSectionView`.
|
|
private func buildStereo() {
|
|
do {
|
|
let board: PedalBoard = try shoeGaze.addBoard()
|
|
board.label = "Stereo Board"
|
|
try board.add(OverdrivePedal())
|
|
|
|
let section: StereoSplitSection = try board.splitStereo()
|
|
try section.left.add(DelayPedal())
|
|
try section.right.add(PingPongDelayPedal())
|
|
try board.collectStereo(section)
|
|
|
|
try shoeGaze.finalizeBoard(board)
|
|
stereoSection = section
|
|
} catch {
|
|
print("[ShoeGazeDemoController] buildStereo failed: \(error)")
|
|
}
|
|
}
|
|
}
|
|
#endif
|