Share one global test signal across all boards

Each board previously got its own TestLoopSource, so multiple boards ran
independently-phased arpeggios and any rebuild restarted them out of sync —
audible polyrhythms. Now a single global TestLoopSource writes a shared test
bus, and each board gets a lightweight TestLoopTap (added .after the source)
copying that bus into its head bus. The shared source survives board
add/remove, so the test signal stays phase-locked and never restarts.

- ShoeGaze: globalTestSource + globalTestBus (lazy via ensureGlobalTestSource),
  per-board testLoopTaps; teardown removes taps + source and releases the bus.
- New TestLoopTap (generic bus copy, like HardwareInputPassThrough).
- ShoeGazeError.testBusUnavailable for an exhausted bus pool.
- Test: testLoop boards share the source via taps; removal leaves others intact.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-24 12:50:07 -07:00
parent 5a07a6d4b4
commit 8c601e2029
3 changed files with 189 additions and 19 deletions
@@ -86,6 +86,66 @@ struct ShoeGazeMultiBoardTests {
}
}
@Test func testLoopBoardsShareGlobalSourceViaTaps() throws {
// In .testLoop mode every board reads one shared global source through a TestLoopTap,
// rather than owning an independently-phased TestLoopSource — this is what keeps the
// boards phase-locked. Verify the head input node of each board is a tap, and that a
// board removal leaves the others (and their signal) intact.
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
let board2: PedalBoard = try shoeGaze.addBoard()
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
// Both boards feed from a shared tap, not their own source.
#expect(board1.headInputNode != nil)
#expect(board1.headInputNode is TestLoopTap)
#expect(board2.headInputNode is TestLoopTap)
#expect(!(board1.headInputNode is TestLoopSource))
// Removing one board does not disturb the other's input node.
shoeGaze.removeBoard(board1)
#expect(shoeGaze.boards.count == 1)
#expect(board2.headInputNode is TestLoopTap)
}
@Test func teardownReleasesGlobalTestBusForReuse() throws {
// teardown() must remove the global source AND release its bus, so a later addBoard()
// can re-allocate cleanly (guards against the global test bus being leaked).
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 16
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
// Repeatedly build a board then tear the whole rig down. With a small bus pool, a leaked
// global test bus on each cycle would eventually exhaust the allocator and throw.
for _ in 0 ..< 8 {
let board: PedalBoard = try shoeGaze.addBoard()
try board.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board)
#expect(board.headInputNode is TestLoopTap)
shoeGaze.teardown()
#expect(shoeGaze.boards.isEmpty)
}
// Still able to build after all the teardown cycles.
let finalBoard: PedalBoard = try shoeGaze.addBoard()
#expect(finalBoard.headInputNode is TestLoopTap)
}
@Test func activeBoardIndexTracksReorderAndRemoval() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(