diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift index cfd88ca..5cb6c3f 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift @@ -401,6 +401,8 @@ public final class MIDIController { (pedal as? LooperPedal)?.toggleStopAtLoopEnd() case .looperReverse: (pedal as? LooperPedal)?.toggleReverse() + case .looperSyncToggle: + (pedal as? LooperPedal)?.cycleSyncMode() } } diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIMap.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIMap.swift index 2c35264..691b8a7 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIMap.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIMap.swift @@ -83,6 +83,8 @@ public struct MIDIMap: Codable, Equatable, Sendable { case looperCueStop /// Looper: flips playback direction (`LooperPedal.toggleReverse()`). case looperReverse + /// Looper: cycles the Sync/Free transport mode (`LooperPedal.cycleSyncMode()`). + case looperSyncToggle } // MARK: - Switch binding diff --git a/Sources/LyrebirdShoeGaze/PedalSwitch.swift b/Sources/LyrebirdShoeGaze/PedalSwitch.swift index 6ce6b57..295cf4a 100644 --- a/Sources/LyrebirdShoeGaze/PedalSwitch.swift +++ b/Sources/LyrebirdShoeGaze/PedalSwitch.swift @@ -66,4 +66,15 @@ public struct PedalSwitch { self.node = node self.options = options } + + /// The value of the next option after the one nearest `current`, wrapping to the first — so a + /// momentary trigger (e.g. a MIDI switch) can oscillate through the positions on each press. + public func nextValue(after current: Double) -> Double { + guard !options.isEmpty else { return current } + let currentIndex: Int = options.indices.min(by: { + abs(options[$0].value - current) < abs(options[$1].value - current) + }) ?? 0 + let nextIndex: Int = (currentIndex + 1) % options.count + return options[nextIndex].value + } } diff --git a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift index 0148e7d..c189564 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift @@ -437,6 +437,13 @@ public final class LooperPedal: Pedal { // MARK: - Stops / reset + /// Advances the Sync mode through its options (Free ↔ Sync), wrapping — so a MIDI switch can + /// oscillate through the positions on each press, matching the segmented control. + public func cycleSyncMode() { + guard let sync: PedalSwitch = effectSwitches.first(where: { $0.node === $syncToBeat }) else { return } + syncToBeat = sync.nextValue(after: syncToBeat) + } + /// Flips playback direction. Engaging reverse ends any active overdub, since overdub records /// forward-in-time and can't follow a backward-reading play-head (see the class doc). public func toggleReverse() { diff --git a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift index 2ab0309..83a451a 100644 --- a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift +++ b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift @@ -125,6 +125,7 @@ public struct MIDIMappingSheet: View { ("stop", "Stop", .looperStop), ("cue", "Cue Stop", .looperCueStop), ("rev", "Reverse", .looperReverse), + ("sync", "Sync", .looperSyncToggle), ("reset", "Reset", .looperReset), ] for (slug, name, kind): (String, String, MIDIMap.PedalActionKind) in looperActions { diff --git a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift index 46aabae..01eccea 100644 --- a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift @@ -530,6 +530,15 @@ struct LooperPedalTests { } } } + @Test func cycleSyncModeOscillatesThroughOptions() throws { + let pedal: LooperPedal = LooperPedal() + #expect(pedal.syncToBeat == 1.0) // Sync by default + pedal.cycleSyncMode() + #expect(pedal.syncToBeat == 0.0, "cycle → Free") + pedal.cycleSyncMode() + #expect(pedal.syncToBeat == 1.0, "cycle wraps back → Sync") + } + @Test func stopResumeAndCueStop() throws { let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) = try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0) diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift index 1e7dc3c..9ee025a 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift @@ -286,4 +286,24 @@ struct MIDIControllerPCTests { #expect(looper.reverse == 1.0, "mapped Program Change should flip the looper's reverse") } } + + @Test func pedalActionCyclesLooperSync() throws { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256) + let sg: ShoeGaze = ShoeGaze(engine: engine) + let board: PedalBoard = try sg.addBoard() + let looper: LooperPedal = LooperPedal() + try board.add(looper) + try sg.finalizeBoard(board) + let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg) + defer { try? FileManager.default.removeItem(at: url) } + + withExtendedLifetime(sg) { + controller.armSwitchLearn(.pedalAction(pedalID: looper.id, kind: .looperSyncToggle)) + controller.ingestForTesting(.programChange(channel: 0, program: 21)) // learn only + #expect(looper.syncToBeat == 1.0) + + controller.ingestForTesting(.programChange(channel: 0, program: 21)) // fire → cycle + #expect(looper.syncToBeat == 0.0, "mapped Program Change should oscillate Sync through its options") + } + } }