From d728bfa72a2a4e727cc46bf2b1150e74177bd832 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Fri, 3 Jul 2026 15:25:12 -0700 Subject: [PATCH] Looper: also reset on rig teardown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a Pedal.rigWillTeardown() hook fired for every pedal at the top of ShoeGaze.teardown() (rig load / clear / input-source change), while the pedal is still live. LooperPedal resets and posts a notice, sharing the path with engineDidStop() — so a looper stops its background routines cleanly and logs before its instance is discarded across a rig change. Co-Authored-By: Claude Opus 4.8 --- Sources/LyrebirdShoeGaze/Pedal.swift | 5 +++++ Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift | 14 +++++++++++++- Sources/LyrebirdShoeGaze/ShoeGaze.swift | 7 +++++++ Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index 263c1b2..4d08d2d 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -112,6 +112,11 @@ open class Pedal: SynthesizerBase { /// (e.g. the looper) override this to return to a clean state. Default: no-op. open func engineDidStop() {} + /// 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. + open func rigWillTeardown() {} + /// Number of standard enclosure widths this pedal's card occupies in the UI. Defaults /// to `1`. A control-dense pedal (e.g. the looper) overrides to `2` for a wider card. /// The UI multiplies its base enclosure width by this factor; `1` is the normal stompbox. diff --git a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift index 53859eb..0f12135 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift @@ -470,9 +470,21 @@ public final class LooperPedal: Pedal { /// state — a stopped engine can't keep a loop running, and resuming into a half-recorded take /// is confusing. Posts a notice to the message log and briefly on the pedal's own card. public override func engineDidStop() { + resetAndNotify("engine stopped") + } + + /// The rig is being torn down (load / clear / input-source change) — reset before this looper + /// instance is discarded, stopping any background routines cleanly and logging a notice. + public override func rigWillTeardown() { + resetAndNotify("rig changed") + } + + /// Returns to the clear state and posts a notice (message log + briefly on the pedal card), + /// unless already clear. Shared by ``engineDidStop()`` and ``rigWillTeardown()``. + private func resetAndNotify(_ reason: String) { guard phase != .clear else { return } resetLoop() - let message: String = "Looper reset — engine stopped." + let message: String = "Looper reset — \(reason)." noticeText = message noticeExpiry = Date().addingTimeInterval(LooperPedal.noticeDuration) LyrebirdMessageCenter.shared.info(message, source: displayName) diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index 6e91227..1ae342c 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -674,6 +674,13 @@ public final class ShoeGaze { // board buses they reference are released. stopRecording() + // Notify pedals the rig is being torn down (rig load / clear / input-source change), so + // stateful pedals like the looper stop their background routines cleanly and post a + // notice before their instances are discarded. + for pedal: Pedal in allPedals() { + pedal.rigWillTeardown() + } + // Step 1: Remove all engine nodes that write to board buses. // Order within this step does not matter; all must finish before bus release. for tap: TestLoopTap in testLoopTaps { diff --git a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift index d955334..d1d7a70 100644 --- a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift @@ -403,6 +403,19 @@ struct LooperPedalTests { } } } + @Test func rigTeardownResetsLooper() throws { + let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) = + try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0) + withExtendedLifetime(engine) { withExtendedLifetime(clock) { + pedal.clock = nil + pedal.pressLoop() + pedal.pressLoop() // playing + pedal.rigWillTeardown() + #expect(pedal.transportState == .clear, "a rig teardown returns the looper to clear") + #expect(pedal.stateDescription == "Looper reset — rig changed.") + } } + } + @Test func stopResumeAndCueStop() throws { let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) = try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0)