diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index beb5aa5..263c1b2 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -107,6 +107,11 @@ open class Pedal: SynthesizerBase { /// Override in a widener or stereo-native subclass. open var channelKind: PedalChannelKind { .mono } + /// Called on the main thread when the audio engine is stopped by the user (via + /// `ShoeGaze.stopEngine()`), not during a transient stop-around-rebuild. Stateful pedals + /// (e.g. the looper) override this to return to a clean state. Default: no-op. + open func engineDidStop() {} + /// 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 9902fdc..53859eb 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift @@ -85,6 +85,10 @@ public final class LooperPedal: Pedal { /// LED state for the Loop footswitch: `1` while overdubbing a track. @UIParam public var overdubIndicator: Double = 0.0 + /// LED state for the Reset footswitch: `1` whenever there is something to reset (any state + /// other than clear). Kept in sync by `phase`'s `didSet`. + @UIParam public var resetIndicator: Double = 0.0 + // MARK: - Constants /// Number of loop tracks (A–D). @@ -124,7 +128,9 @@ public final class LooperPedal: Pedal { case stopped } - private var phase: Phase = .clear + private var phase: Phase = .clear { + didSet { resetIndicator = phase == .clear ? 0.0 : 1.0 } + } /// The track the Loop footswitch records / overdubs into (`0` = A). private var selectedTrack: Int = 0 @@ -148,6 +154,11 @@ public final class LooperPedal: Pedal { private var transportRoutine: LyrebirdRoutine? private var stopRoutine: LyrebirdRoutine? + /// Transient notice shown in place of the state description for `noticeDuration` seconds. + private var noticeText: String? + private var noticeExpiry: Date? + private static let noticeDuration: TimeInterval = 4.0 + /// Monotonic action generation guarding deferred beat/stop callbacks (see the single-track /// history — `LyrebirdRoutine.stop()` cannot recall an already-enqueued main-thread hop). private(set) var transportGeneration: Int = 0 @@ -185,7 +196,7 @@ public final class LooperPedal: Pedal { dynamicLabel: { [weak self] in self?.cueStopButtonCaption ?? "Cue Stop" }) { [weak self] in self?.toggleStopAtLoopEnd() }, PedalButton(label: "Reverse", indicator: $reverse, dynamicLabel: { [weak self] in self?.reverseButtonCaption ?? "Reverse" }) { [weak self] in self?.toggleReverse() }, - PedalButton(label: "Reset") { [weak self] in self?.resetLoop() }, + PedalButton(label: "Reset", indicator: $resetIndicator) { [weak self] in self?.resetLoop() }, ] } @@ -261,8 +272,12 @@ public final class LooperPedal: Pedal { /// Caption for the Reverse footswitch (the direction the next press switches to). public var reverseButtonCaption: String { reverse >= 0.5 ? "Forward" : "Reverse" } - /// One-line description of what the pedal is doing now, for the state indicator. + /// One-line description of what the pedal is doing now, for the state indicator. A transient + /// notice (e.g. the engine-stop reset) takes precedence for a few seconds. public var stateDescription: String { + if let notice: String = noticeText, let expiry: Date = noticeExpiry, Date() < expiry { + return notice + } let armedSuffix: String = isStopAtLoopEndArmed ? " · stop at end" : "" switch phase { case .clear: return "Empty — record track A" @@ -451,6 +466,18 @@ public final class LooperPedal: Pedal { phase = .stopped } + /// When the engine is stopped by the user, drop any recorded loop and return to the clear + /// 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() { + guard phase != .clear else { return } + resetLoop() + let message: String = "Looper reset — engine stopped." + noticeText = message + noticeExpiry = Date().addingTimeInterval(LooperPedal.noticeDuration) + LyrebirdMessageCenter.shared.info(message, source: displayName) + } + /// Returns the pedal to the clear state: all tracks cleared, track A selected, length reset. /// If sound is playing it acts as an immediate stop-and-clear. public func resetLoop() { diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index 109f13c..6e91227 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -195,6 +195,41 @@ 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)" diff --git a/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift b/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift index 52eb50b..a04777d 100644 --- a/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift +++ b/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift @@ -190,7 +190,7 @@ public struct EngineConfigView: View { } private func stopEngine() { - shoeGaze.engine.stop() + shoeGaze.stopEngine() transportTick.toggle() onConfigApplied() } diff --git a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift index 094e770..d955334 100644 --- a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift @@ -381,6 +381,28 @@ struct LooperPedalTests { } } } + @Test func engineStopResetsLooperAndPostsNotice() 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() // record A + pedal.pressLoop() // playing + #expect(pedal.transportState == .playing) + + pedal.engineDidStop() + #expect(pedal.transportState == .clear, "engine stop returns the looper to clear") + #expect(pedal.loopFrameCount == 0) + #expect(pedal.stateDescription == "Looper reset — engine stopped.", + "a transient notice is shown after the engine-stop reset") + + // A no-op when already clear (no spurious notice). + let clearPedal: LooperPedal = LooperPedal() + clearPedal.engineDidStop() + #expect(clearPedal.transportState == .clear) + } } + } + @Test func stopResumeAndCueStop() throws { let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) = try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0)