Looper: MIDI-mappable Sync, cycling through the switch options

Add a looperSyncToggle PedalActionKind (dispatched to LooperPedal.cycleSyncMode)
and a Sync row in the MIDI mapping sheet. A mapped switch/Program Change
oscillates the control through its positions on each press, via a reusable
PedalSwitch.nextValue(after:) that advances to the next option and wraps — so
this generalises to any multi-position switch, not just the two-state Sync.

Tests: cycleSyncModeOscillatesThroughOptions, pedalActionCyclesLooperSync.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 17:24:50 -07:00
parent 5dafc00d98
commit 3ef39a7a0d
7 changed files with 52 additions and 0 deletions
@@ -401,6 +401,8 @@ public final class MIDIController {
(pedal as? LooperPedal)?.toggleStopAtLoopEnd()
case .looperReverse:
(pedal as? LooperPedal)?.toggleReverse()
case .looperSyncToggle:
(pedal as? LooperPedal)?.cycleSyncMode()
}
}
@@ -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
@@ -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
}
}
@@ -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() {
@@ -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 {