From fb3e2d2653db674bd772052570451daa77bba9cb Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Fri, 3 Jul 2026 17:07:48 -0700 Subject: [PATCH] Looper: make effect() idempotent across graph rebuilds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engine re-invokes build()/effect() via rebuildGraph() on every add — including the re-adds a board edit (reorder, insert/remove a neighbour, toggle a split) or a rig load performs on the same instance. effect() appended to the per-track arrays (buffers/players/overdubRecorders/overdubGates) each time, so a second build doubled them, desyncing the generators from the fixed-size state arrays and leaving the whole looper UI/transport wrong — a broken state that stuck and reproduced from a saved rig. Reset the arrays and transport state at the top of effect() (resetForRebuild) so a rebuild starts clean; the freshly re-allocated buffers are empty, so returning to the clear state is correct. Regression: rebuildDoesNotDoubleTrackGenerators (two effect() calls keep each array at trackCount). Co-Authored-By: Claude Opus 4.8 --- .../LyrebirdShoeGaze/Pedals/LooperPedal.swift | 43 +++++++++++++++++++ .../LooperPedalTests.swift | 18 ++++++++ 2 files changed, 61 insertions(+) diff --git a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift index e2e4f52..16a664f 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LooperPedal.swift @@ -229,6 +229,12 @@ public final class LooperPedal: Pedal { index >= 0 && index < trackHasContent.count && trackHasContent[index] } + /// Per-track generator array counts — internal only so a test can verify a graph rebuild + /// (`effect()` re-run) doesn't accumulate them. All four should equal `trackCount`. + var trackGeneratorCountsForTesting: (buffers: Int, players: Int, overdub: Int, gates: Int) { + (buffers.count, players.count, overdubRecorders.count, overdubGates.count) + } + /// `true` while a stop-at-loop-end is armed. public var isStopAtLoopEndArmed: Bool { cueStopArmed >= 0.5 } @@ -306,6 +312,13 @@ public final class LooperPedal: Pedal { // MARK: - Effect public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { + // `effect()` runs from `build()`, which the engine re-invokes via `rebuildGraph()` on + // EVERY add — including the re-adds that a board edit (reorder, insert/remove a neighbour, + // toggle a split) performs on this same instance. Start from a clean slate so the per-track + // generator arrays don't accumulate across rebuilds, and so stale transport state doesn't + // outlive the freshly re-allocated (empty) buffers built below. + resetForRebuild() + let maxFrames: Int = Int(LooperPedal.maxDurationSeconds * sampleRate) let playbackRate: any ValidGeneratorInput = LooperPedal.forwardRate - LooperPedal.reverseRateSpan * $reverse @@ -545,6 +558,36 @@ public final class LooperPedal: Pedal { // MARK: - Transport helpers + /// Clears the per-track generator arrays and returns transport state to clear, so a graph + /// rebuild (`build()` re-run) starts fresh instead of doubling the arrays or leaving state + /// that references the discarded buffers. Called at the top of ``effect(_:)``. + private func resetForRebuild() { + transportRoutine?.stop() + transportRoutine = nil + stopRoutine?.stop() + stopRoutine = nil + buffers.removeAll() + players.removeAll() + overdubRecorders.removeAll() + overdubGates.removeAll() + masterRecorder = nil + trackHasContent = [] + selectedTrack = 0 + overdubbingTrack = nil + masterLength = 0 + masterBeats = 0.0 + isPlaying = 0.0 + loopOn = 1.0 + reverse = 0.0 + playTrigger = 0.0 + stoppedIndicator = 0.0 + cueStopArmed = 0.0 + overdubIndicator = 0.0 + noticeText = nil + noticeExpiry = nil + phase = .clear + } + private func updateOverdubGates() { for i: Int in overdubGates.indices { overdubGates[i].value = (overdubbingTrack == i) ? 1.0 : 0.0 diff --git a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift index 9ac4e57..ee47d17 100644 --- a/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LooperPedalTests.swift @@ -173,6 +173,24 @@ struct LooperPedalTests { #expect(LooperPedal.trackCount == 4) } + @Test func rebuildDoesNotDoubleTrackGenerators() throws { + // effect() runs from build(), which the engine re-invokes on every add — including the + // re-adds a board edit performs on the same instance. It must be idempotent (not append + // to the per-track arrays each time), or the looper's generators/state desync. + let pedal: LooperPedal = LooperPedal() + pedal.sampleRate = 44100.0 + let dry: any ValidGeneratorInput = OscSin.Audio(freq: 100.0) + _ = pedal.effect(dry) + _ = pedal.effect(dry) // simulate a second build() + + let counts: (buffers: Int, players: Int, overdub: Int, gates: Int) = pedal.trackGeneratorCountsForTesting + #expect(counts.buffers == LooperPedal.trackCount) + #expect(counts.players == LooperPedal.trackCount) + #expect(counts.overdub == LooperPedal.trackCount) + #expect(counts.gates == LooperPedal.trackCount) + #expect(pedal.transportState == .clear) + } + @Test func looperHidesMixControl() throws { // The looper always passes the clean signal through and adds the loop on top, so the // wet/dry Mix knob is hidden; a plain pedal keeps it.