Add looper pedal: beat-quantized transport, undo, reverse, timeline

Extend the looper into a double-width tempo-aware pedal:

- Beat-quantized transport: record start and loop close both snap to the
  next beat via a LyrebirdRoutine waiting on the shared clock; loop length
  is an exact whole-beat span. Falls back to immediate/unquantized when no
  clock is present.
- Loop-length read-outs (frames/seconds/beats), transport state, and a
  sample-clock-derived play-head position for the UI timeline.
- Overdub undo (pre-dub snapshot + restore), reverse playback (live rate
  flip), and feedback-decay overdub (overdub preLevel via a Feedback knob).
- Inject the shared LyrebirdClock + SynthesisEngine into Pedal via
  PedalBoard/ChainRouter so tempo-aware pedals can reach them.
- New double-width LooperTimelineView (state badge, length read-out,
  sweeping play-head) wired into PedalView via Pedal.widthUnits.

Concurrency hardening from RT-safety review: a transportGeneration counter
drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop()
cannot recall once enqueued; the undo restore is deferred past the RT
overdub gate-close; transport routines use non-blocking stop().

Note: this commit also snapshots pre-existing uncommitted work-in-progress
already in the tree (MIDI control refactor, Tuner, Tempo, preset changes),
which is interleaved with the looper changes through shared files and could
not be split into a separately-building commit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 09:22:20 -07:00
parent 097253b410
commit 947dfe427a
47 changed files with 3263 additions and 176 deletions
@@ -86,7 +86,7 @@ struct MIDIControllerPCTests {
withExtendedLifetime(sg) {
let node: UIParamNode = boards[0].pedals[0].$bypass
controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 0))
controller.armSwitchLearn(.pedalBypass(pedalID: boards[0].pedals[0].id))
controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn
#expect(node.value == 0.0)
controller.ingestForTesting(.programChange(channel: 0, program: 5))
@@ -101,7 +101,7 @@ struct MIDIControllerPCTests {
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armSwitchLearn(.boardBypass(boardIndex: 0))
controller.armSwitchLearn(.boardBypass(boardID: boards[0].id))
controller.ingestForTesting(.programChange(channel: 0, program: 3)) // learn
controller.ingestForTesting(.programChange(channel: 0, program: 3))
#expect(sg.isBoardBypassed(boards[0]) == true)
@@ -116,7 +116,7 @@ struct MIDIControllerPCTests {
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armSwitchLearn(.boardSolo(boardIndex: 1))
controller.armSwitchLearn(.boardSolo(boardID: boards[1].id))
controller.ingestForTesting(.programChange(channel: 0, program: 2)) // learn
controller.ingestForTesting(.programChange(channel: 0, program: 2)) // solo board 2
#expect(sg.activeBoardIndex == 1)
@@ -162,11 +162,12 @@ struct MIDIControllerPCTests {
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
// Bind to a pedal index that doesn't exist; applying it must be a safe no-op.
controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 9))
// Bind to a pedal id that doesn't exist in the rig; applying it must be a safe no-op.
let ghostID: UUID = UUID()
controller.armSwitchLearn(.pedalBypass(pedalID: ghostID))
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // learn
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // apply — no crash
#expect(controller.switchBinding(for: .pedalBypass(boardIndex: 0, pedalIndex: 9)) != nil)
#expect(controller.switchBinding(for: .pedalBypass(pedalID: ghostID)) != nil)
}
}
@@ -179,12 +180,12 @@ struct MIDIControllerPCTests {
withExtendedLifetime(sg) {
controller.armSwitchLearn(.rigBypass)
controller.ingestForTesting(.programChange(channel: 0, program: 8))
controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 0))
controller.armSwitchLearn(.pedalBypass(pedalID: boards[0].pedals[0].id))
controller.ingestForTesting(.programChange(channel: 0, program: 8)) // same PC, kept
// Both bindings survive — one PC drives two actions.
#expect(controller.switchBinding(for: .rigBypass)?.program == 8)
#expect(controller.switchBinding(for: .pedalBypass(boardIndex: 0, pedalIndex: 0))?.program == 8)
#expect(controller.switchBinding(for: .pedalBypass(pedalID: boards[0].pedals[0].id))?.program == 8)
#expect(sg.midiMap?.switches.count == 2)
// Firing the PC toggles both at once.
@@ -209,4 +210,60 @@ struct MIDIControllerPCTests {
#expect(sg.midiMap?.switches.count == 1)
}
}
@Test func tunerToggleFlipsEngagedState() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armSwitchLearn(.tunerToggle)
controller.ingestForTesting(.programChange(channel: 0, program: 10)) // learn only
#expect(!sg.tunerEngaged)
controller.ingestForTesting(.programChange(channel: 0, program: 10))
#expect(sg.tunerEngaged, "first press engages")
controller.ingestForTesting(.programChange(channel: 0, program: 10))
#expect(!sg.tunerEngaged, "second press disengages")
}
}
@Test func tapTempoRegistersEachPress() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armSwitchLearn(.tapTempo)
controller.ingestForTesting(.programChange(channel: 0, program: 11)) // learn only
#expect(sg.tapTimestamps.isEmpty)
controller.ingestForTesting(.programChange(channel: 0, program: 11))
#expect(sg.tapTimestamps.count == 1, "first press registers a tap")
controller.ingestForTesting(.programChange(channel: 0, program: 11))
#expect(sg.tapTimestamps.count == 2, "second press registers a second tap")
}
}
@Test func pedalActionDispatchesLooperRecordStop() 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: .looperRecordStop))
controller.ingestForTesting(.programChange(channel: 0, program: 12)) // learn only
#expect(looper.transportState == .empty)
// The looper was added via the board, so it carries the shared tempo clock and its
// transport is beat-quantized: the mapped press arms recording for the next beat.
// Asserting the arm proves the Program Change dispatched to the pedal's action.
controller.ingestForTesting(.programChange(channel: 0, program: 12))
#expect(looper.transportState == .armedRecord,
"mapped Program Change should dispatch the looper record/stop action")
}
}
}