From 67d443422019cfe903b8589145b6a8801a52c9c6 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Fri, 3 Jul 2026 16:34:45 -0700 Subject: [PATCH] =?UTF-8?q?Looper:=20address=20review=20=E2=80=94=20router?= =?UTF-8?q?=20pedals=20+=20doc=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit allPedals() (used by stopEngine / teardown to notify pedals) now also walks ChainRouter branch boards, so a looper placed in a parallel-router branch gets engineDidStop() / rigWillTeardown() like any other. Factor the per-board walk into pedals(in:). Restore addBoard()'s doc comment (it was displaced above stopEngine when that method was inserted) and document the engine-stopped precondition on Pedal.rigWillTeardown(). Co-Authored-By: Claude Opus 4.8 --- Sources/LyrebirdShoeGaze/Pedal.swift | 4 ++ Sources/LyrebirdShoeGaze/ShoeGaze.swift | 81 ++++++++++++++----------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index 58832bd..8ce1b3b 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -121,6 +121,10 @@ open class Pedal: SynthesizerBase { /// Called on the main thread just before the rig is torn down (rig load / clear / input-source /// change), while the pedal is still live. Stateful pedals override this to stop any background /// work and return to a clean state before their instance is discarded. Default: no-op. + /// + /// - Important: Fired from `ShoeGaze.teardown()`, which — like all node-removal paths — must + /// run with the engine already stopped. An override that clears buffers or stops RT-adjacent + /// work relies on that precondition; do not call `teardown()` with the engine running. open func rigWillTeardown() {} /// Number of standard enclosure widths this pedal's card occupies in the UI. Defaults diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index 1ae342c..95834f1 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -178,6 +178,52 @@ public final class ShoeGaze { // MARK: - Board management + /// Stops the audio engine as a deliberate user action: notifies every pedal via + /// ``Pedal/engineDidStop()`` (so stateful pedals like the looper return to a clean state and + /// post a notice) and then stops the engine. Use this for the user-facing Stop control — not + /// for transient stop-around-rebuild sequences, which call `engine.stop()` directly so + /// pedals keep their state across the restart. + public func stopEngine() { + for pedal: Pedal in allPedals() { + pedal.engineDidStop() + } + engine.stop() + } + + /// Every live `Pedal` across all main boards and chain-router branches. + private func allPedals() -> [Pedal] { + var result: [Pedal] = [] + for board: PedalBoard in boards { + result.append(contentsOf: pedals(in: board)) + } + for router: ChainRouter in routers { + result.append(contentsOf: pedals(in: router.boardA)) + result.append(contentsOf: pedals(in: router.boardB)) + } + return result + } + + /// Every live `Pedal` in one board, including pedals inside its parallel and stereo-split + /// branch sections (branch pedals live in their section objects, not as top-level segments). + private func pedals(in board: PedalBoard) -> [Pedal] { + var result: [Pedal] = [] + for segment: BoardSegment in board.orderedSegments { + switch segment { + case .pedal(let pedal): + result.append(pedal) + case .parallel(let section): + result.append(contentsOf: section.branchA.pedals) + result.append(contentsOf: section.branchB.pedals) + case .stereoSplit(let section), .stereoUnzip(let section): + result.append(contentsOf: section.left.pedals) + result.append(contentsOf: section.right.pedals) + case .mergeToMono: + break + } + } + return result + } + /// Allocates a new `PedalBoard` connected to the engine, wires the input source, /// and returns the board ready for pedals to be added via `board.add(_:)`. /// @@ -195,41 +241,6 @@ public final class ShoeGaze { /// - `BusAllocatorError.noFreeBus` if no mono bus is available. /// - `EngineAddError` if the engine rejects a node insertion. @discardableResult - /// Stops the audio engine as a deliberate user action: notifies every pedal via - /// ``Pedal/engineDidStop()`` (so stateful pedals like the looper return to a clean state and - /// post a notice) and then stops the engine. Use this for the user-facing Stop control — not - /// for transient stop-around-rebuild sequences, which call `engine.stop()` directly so - /// pedals keep their state across the restart. - public func stopEngine() { - for pedal: Pedal in allPedals() { - pedal.engineDidStop() - } - engine.stop() - } - - /// Every live `Pedal` across all boards, including pedals inside parallel and stereo-split - /// branches (branch pedals live in their section objects, not as top-level segments). - private func allPedals() -> [Pedal] { - var result: [Pedal] = [] - for board: PedalBoard in boards { - for segment: BoardSegment in board.orderedSegments { - switch segment { - case .pedal(let pedal): - result.append(pedal) - case .parallel(let section): - result.append(contentsOf: section.branchA.pedals) - result.append(contentsOf: section.branchB.pedals) - case .stereoSplit(let section), .stereoUnzip(let section): - result.append(contentsOf: section.left.pedals) - result.append(contentsOf: section.right.pedals) - case .mergeToMono: - break - } - } - } - return result - } - public func addBoard() throws -> PedalBoard { let board: PedalBoard = try PedalBoard(engine: engine, clock: clock) board.label = "Board \(boards.count + 1)"