Looper: make effect() idempotent across graph rebuilds
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 <noreply@anthropic.com>
This commit is contained in:
@@ -229,6 +229,12 @@ public final class LooperPedal: Pedal {
|
|||||||
index >= 0 && index < trackHasContent.count && trackHasContent[index]
|
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.
|
/// `true` while a stop-at-loop-end is armed.
|
||||||
public var isStopAtLoopEndArmed: Bool { cueStopArmed >= 0.5 }
|
public var isStopAtLoopEndArmed: Bool { cueStopArmed >= 0.5 }
|
||||||
|
|
||||||
@@ -306,6 +312,13 @@ public final class LooperPedal: Pedal {
|
|||||||
// MARK: - Effect
|
// MARK: - Effect
|
||||||
|
|
||||||
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
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 maxFrames: Int = Int(LooperPedal.maxDurationSeconds * sampleRate)
|
||||||
let playbackRate: any ValidGeneratorInput =
|
let playbackRate: any ValidGeneratorInput =
|
||||||
LooperPedal.forwardRate - LooperPedal.reverseRateSpan * $reverse
|
LooperPedal.forwardRate - LooperPedal.reverseRateSpan * $reverse
|
||||||
@@ -545,6 +558,36 @@ public final class LooperPedal: Pedal {
|
|||||||
|
|
||||||
// MARK: - Transport helpers
|
// 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() {
|
private func updateOverdubGates() {
|
||||||
for i: Int in overdubGates.indices {
|
for i: Int in overdubGates.indices {
|
||||||
overdubGates[i].value = (overdubbingTrack == i) ? 1.0 : 0.0
|
overdubGates[i].value = (overdubbingTrack == i) ? 1.0 : 0.0
|
||||||
|
|||||||
@@ -173,6 +173,24 @@ struct LooperPedalTests {
|
|||||||
#expect(LooperPedal.trackCount == 4)
|
#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 {
|
@Test func looperHidesMixControl() throws {
|
||||||
// The looper always passes the clean signal through and adds the loop on top, so the
|
// 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.
|
// wet/dry Mix knob is hidden; a plain pedal keeps it.
|
||||||
|
|||||||
Reference in New Issue
Block a user