Looper: multi-slot + timeline + stop/cue-stop, quantized transport

Checkpoint before the multitrack redesign. Adds 4 loop slots (one player
swapped via setBuffer), a play-head timeline with A/B/C/D dots, Loop/Stop/
Cue Stop/Reverse stomp toggles below the knobs, beat-quantized transport,
overdub undo, reverse, and feedback-decay overdub — all reviewed for RT
safety (transportGeneration guards on deferred beat/undo/cue callbacks).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 14:14:46 -07:00
parent 947dfe427a
commit 465e3e6dfd
8 changed files with 600 additions and 203 deletions
@@ -297,8 +297,11 @@ struct LooperPedalTests {
let pedal: LooperPedal = LooperPedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Crossfade", "Playback", "Overdub", "Feedback"])
#expect(pedal.effectSwitches.isEmpty)
// Transport row: Loop / Stop / Cue Stop / Reverse, with the two toggles flanking the stops.
#expect(pedal.effectTransportButtons.map { $0.label } == ["Loop", "Stop", "Cue Stop", "Reverse"])
#expect(pedal.effectTransportButtons.filter { $0.style == .toggle }.map { $0.label } == ["Loop", "Reverse"])
#expect(pedal.effectButtons.map { $0.label } ==
["Record / Stop", "Overdub", "Undo", "Loop", "Reverse", "Reset"])
["Record / Stop", "Overdub", "Undo", "Slot", "Reset"])
}
@Test func looperIsDoubleWidth() throws {
@@ -437,6 +440,113 @@ struct LooperPedalTests {
}
}
@Test func slotsHoldIndependentLoopsAndSwitch() throws {
let sampleRate: Double = 44100.0
var captured: LooperPedal?
let samples: [Float] = try render(totalDuration: 1.0, sampleRate: sampleRate) { engine, source, pedal, scheduleAction in
captured = pedal
pedal.hostEngine = engine // enable the player buffer swap
pedal.toggleRecordStop() // record slot A (default)
try scheduleAction(0.1) { pedal.toggleRecordStop() } // close A → playing
try scheduleAction(0.12) { source.amplitude = 0.0 } // silence live input
try scheduleAction(0.3) { pedal.selectSlot(1) } // switch to empty slot B
try scheduleAction(0.6) { pedal.selectSlot(0) } // switch back to A
}
let stride: Int = 2
// While parked on empty slot B (input silenced), output should be silent.
let bStart: Int = Int(0.45 * sampleRate) * stride
let bEnd: Int = Int(0.58 * sampleRate) * stride
let onEmptyB: [Float] = Array(samples[bStart ..< min(bEnd, samples.count)])
#expect(!hasNonSilentOutput(onEmptyB), "Empty slot B with silenced input should be silent")
// After switching back to A, its recorded loop should be audible again.
let aStart: Int = Int(0.85 * sampleRate) * stride
let backOnA: [Float] = Array(samples[aStart...])
#expect(hasNonSilentOutput(backOnA), "Switching back to slot A should replay its loop")
let pedal: LooperPedal = try #require(captured)
#expect(pedal.activeSlotIndex == 0)
#expect(pedal.slotHasContent(0))
#expect(!pedal.slotHasContent(1))
}
@Test func cycleSlotWrapsThroughAllSlots() throws {
let (_, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) =
try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0)
withExtendedLifetime(clock) {
#expect(pedal.activeSlotIndex == 0)
for expected: Int in [1, 2, 3, 0] {
pedal.cycleSlot()
#expect(pedal.activeSlotIndex == expected)
}
}
}
@Test func stopPlaybackHaltsAndRecordStopResumes() throws {
let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) =
try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0)
withExtendedLifetime(engine) { withExtendedLifetime(clock) {
pedal.clock = nil // immediate (un-quantized) transport
pedal.toggleRecordStop() // recording
pedal.toggleRecordStop() // close → playing
#expect(pedal.transportState == .playing)
pedal.stopPlayback()
#expect(pedal.transportState == .stopped)
#expect(pedal.isPlaying == 0.0)
#expect(pedal.playbackPosition01() == nil)
pedal.toggleRecordStop() // Record / Stop resumes from a stopped loop
#expect(pedal.transportState == .playing)
#expect(pedal.isPlaying == 1.0)
} }
}
@Test func stopAtLoopEndArmsAndCancels() throws {
let sampleRate: Double = 44100.0
let (engine, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) =
try makeQuantizedFixture(bpm: 120.0, sampleRate: sampleRate)
withExtendedLifetime(engine) { withExtendedLifetime(clock) {
// Reach a playing loop through the quantized path.
pedal.toggleRecordStop()
pedal.beginRecording(at: SampleTime(sampleRate * 0.5))
pedal.toggleRecordStop()
pedal.finishRecordingAndPlay(at: SampleTime(sampleRate * 2.5))
#expect(pedal.transportState == .playing)
#expect(!pedal.isStopAtLoopEndArmed)
pedal.toggleStopAtLoopEnd()
#expect(pedal.isStopAtLoopEndArmed, "first press arms the end-stop")
pedal.toggleStopAtLoopEnd()
#expect(!pedal.isStopAtLoopEndArmed, "second press disarms it")
// An immediate Stop while armed cancels the pending end-stop.
pedal.toggleStopAtLoopEnd()
#expect(pedal.isStopAtLoopEndArmed)
pedal.stopPlayback()
#expect(pedal.transportState == .stopped)
#expect(!pedal.isStopAtLoopEndArmed)
} }
}
@Test func selectSlotIsNoOpWhileRecording() throws {
let (_, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) =
try makeQuantizedFixture(bpm: 120.0, sampleRate: 44100.0)
withExtendedLifetime(clock) {
// Drop the clock so transport is immediate (un-quantized): a record press lands
// directly in `.recording` rather than arming for the next beat.
pedal.clock = nil
pedal.toggleRecordStop()
#expect(pedal.transportState == .recording)
// Switching slots must be ignored mid-record — no active-slot change, no phase change.
pedal.selectSlot(1)
#expect(pedal.activeSlotIndex == 0)
#expect(pedal.transportState == .recording)
}
}
@Test func staleUndoRestoreIsDroppedWhenGenerationAdvances() throws {
let sampleRate: Double = 44100.0
let (_, clock, pedal): (SynthesisEngine, LyrebirdClock, LooperPedal) =
@@ -457,7 +567,7 @@ struct LooperPedalTests {
// The deferred restore must now be a no-op: it must not reintroduce audio into the
// buffer the user just cleared.
pedal.applyUndoRestore(snapshot, generation: staleGeneration)
pedal.applyUndoRestore(snapshot, slot: pedal.activeSlotIndex, generation: staleGeneration)
#expect(pedal.transportState == .empty)
#expect(pedal.loopFrameCount == 0)
}