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:
@@ -0,0 +1,49 @@
|
||||
// TestLoopTap.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import LyrebirdSynth
|
||||
|
||||
/// Internal synthesizer that copies the shared global test-loop bus to a board head bus.
|
||||
///
|
||||
/// In `.testLoop` mode every board reads the **same** global ``TestLoopSource`` (so all boards
|
||||
/// stay phase-locked instead of each running an independently-phased arpeggio). A `TestLoopTap`
|
||||
/// per board reads the global test bus at `inputBusIndex` and writes it to the board's head bus
|
||||
/// at `outputBusIndex`, so the first pedal still reads from a board-owned bus.
|
||||
///
|
||||
/// Added `.after(_:)` the global source so it evaluates once the source has written the test bus
|
||||
/// for the current block.
|
||||
///
|
||||
/// - Note: Must be removed from the engine before the board's `teardown()` releases the head bus.
|
||||
final class TestLoopTap: SynthesizerBase {
|
||||
|
||||
// MARK: - Routing
|
||||
|
||||
/// Index of the shared global test-loop bus to read from.
|
||||
private let inputBusIndex: Int
|
||||
|
||||
/// Index of the board head bus to write to.
|
||||
private let outputBusIndex: Int
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a tap that copies `inputBusIndex` (the global test bus) to `outputBusIndex`
|
||||
/// (a board head bus).
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - inputBusIndex: Shared global test-loop bus index to read from.
|
||||
/// - outputBusIndex: Board head bus index to write to.
|
||||
init(inputBusIndex: Int, outputBusIndex: Int) {
|
||||
self.inputBusIndex = inputBusIndex
|
||||
self.outputBusIndex = outputBusIndex
|
||||
super.init()
|
||||
}
|
||||
|
||||
// MARK: - Build
|
||||
|
||||
override func build() {
|
||||
output(bus: outputBusIndex, source: input(bus: inputBusIndex))
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,10 @@ import LyrebirdUI
|
||||
///
|
||||
/// ## Input routing
|
||||
///
|
||||
/// When `inputSource == .testLoop`, a `TestLoopSource` is added to the engine
|
||||
/// **before** the first pedal on each board (plain `engine.add` places it at the
|
||||
/// head of `mainGroup`, so it evaluates first).
|
||||
/// When `inputSource == .testLoop`, a **single** shared `TestLoopSource` writes a global
|
||||
/// test bus, and each board gets a `TestLoopTap` (added `.after(_:)` the source) copying that
|
||||
/// bus into the board's head bus. One shared source keeps every board phase-locked and survives
|
||||
/// board add/remove, so the arpeggio never restarts into a polyrhythm.
|
||||
///
|
||||
/// When `inputSource == .liveInput(channel:)`, a `HardwareInputPassThrough` synthesizer
|
||||
/// copies hardware input `channel` (engine bus `numOutputChannels + channel`) to the
|
||||
@@ -96,7 +97,14 @@ public final class ShoeGaze {
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private var testLoopSources: [TestLoopSource] = []
|
||||
/// The single shared test-loop source feeding every board in `.testLoop` mode, and the bus
|
||||
/// it writes. One global source keeps all boards phase-locked (no per-board polyrhythm) and
|
||||
/// survives board add/remove so its arpeggio never restarts. `nil` outside `.testLoop` mode.
|
||||
private var globalTestSource: TestLoopSource?
|
||||
private var globalTestBus: AudioBus?
|
||||
|
||||
/// Per-board taps copying the global test bus into each board's head bus.
|
||||
private var testLoopTaps: [TestLoopTap] = []
|
||||
private var inputPassThroughs: [HardwareInputPassThrough] = []
|
||||
private var outputMasters: [BoardOutputMaster] = []
|
||||
|
||||
@@ -149,16 +157,19 @@ public final class ShoeGaze {
|
||||
// Route the input source to the board head bus.
|
||||
switch inputSource {
|
||||
case .testLoop:
|
||||
let source: TestLoopSource = TestLoopSource(
|
||||
targetBus: board.headBus.index,
|
||||
sampleRate: engine.sampleRate
|
||||
// All boards share one global test source so they stay phase-locked. Each board
|
||||
// gets a tap copying the shared test bus into its head bus, added `.after(_:)` the
|
||||
// source so it sees the source's output for the current block.
|
||||
let (source, testBus): (TestLoopSource, AudioBus) = try ensureGlobalTestSource()
|
||||
let tap: TestLoopTap = TestLoopTap(
|
||||
inputBusIndex: testBus.index,
|
||||
outputBusIndex: board.headBus.index
|
||||
)
|
||||
// Plain add → head of mainGroup (evaluates before any pedal).
|
||||
try engine.add(source)
|
||||
testLoopSources.append(source)
|
||||
// Record the source as the head input node so PedalBoard.add(_:) can chain
|
||||
// the first pedal after it automatically (without needing an explicit `after:`).
|
||||
board.headInputNode = source
|
||||
try engine.add(tap, position: NodePosition.after(source))
|
||||
testLoopTaps.append(tap)
|
||||
// Record the tap as the head input node so PedalBoard.add(_:) can chain the first
|
||||
// pedal after it automatically (without needing an explicit `after:`).
|
||||
board.headInputNode = tap
|
||||
|
||||
case .liveInput(let channel):
|
||||
// Hardware input channel `channel` lives at bus numOutputChannels + channel.
|
||||
@@ -181,6 +192,36 @@ public final class ShoeGaze {
|
||||
return board
|
||||
}
|
||||
|
||||
/// Lazily creates the single shared test-loop source and its bus, returning both.
|
||||
///
|
||||
/// Created once per `.testLoop` session and reused by every board's tap, so the arpeggio
|
||||
/// phase is continuous across board add/remove (no per-board polyrhythm). Torn down by
|
||||
/// ``teardown()`` (and thus by ``setInputSource(_:)`` when leaving test-loop mode).
|
||||
///
|
||||
/// The source and bus are committed to `globalTestSource` / `globalTestBus` only after the
|
||||
/// engine add succeeds — if `engine.add` throws, the freshly allocated bus is released so it
|
||||
/// is never orphaned.
|
||||
private func ensureGlobalTestSource() throws -> (source: TestLoopSource, bus: AudioBus) {
|
||||
if let source: TestLoopSource = globalTestSource, let bus: AudioBus = globalTestBus {
|
||||
return (source, bus)
|
||||
}
|
||||
let testBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let source: TestLoopSource = TestLoopSource(
|
||||
targetBus: testBus.index,
|
||||
sampleRate: engine.sampleRate
|
||||
)
|
||||
do {
|
||||
// Plain add → head of mainGroup (evaluates before its taps and any pedal).
|
||||
try engine.add(source)
|
||||
} catch {
|
||||
engine.busAllocator.releaseAudioBus(testBus)
|
||||
throw error
|
||||
}
|
||||
globalTestBus = testBus
|
||||
globalTestSource = source
|
||||
return (source, testBus)
|
||||
}
|
||||
|
||||
/// Connects the output master for a previously prepared board and adds it to the engine.
|
||||
///
|
||||
/// Call this after all pedals have been added to the board via `board.add(_:)`.
|
||||
@@ -333,10 +374,12 @@ public final class ShoeGaze {
|
||||
outputMasters.removeAll { $0 === master }
|
||||
}
|
||||
|
||||
// Remove the board's input source (writer of the head bus).
|
||||
if let testLoop: TestLoopSource = board.headInputNode as? TestLoopSource {
|
||||
engine.remove(testLoop)
|
||||
testLoopSources.removeAll { $0 === testLoop }
|
||||
// Remove the board's input node (writer of the head bus). In `.testLoop` mode this is a
|
||||
// tap; the shared global source is left running so the remaining boards keep their
|
||||
// phase-locked test signal.
|
||||
if let tap: TestLoopTap = board.headInputNode as? TestLoopTap {
|
||||
engine.remove(tap)
|
||||
testLoopTaps.removeAll { $0 === tap }
|
||||
} else if let passThrough: HardwareInputPassThrough = board.headInputNode as? HardwareInputPassThrough {
|
||||
engine.remove(passThrough)
|
||||
inputPassThroughs.removeAll { $0 === passThrough }
|
||||
@@ -377,6 +420,13 @@ public final class ShoeGaze {
|
||||
/// try engine.start()
|
||||
/// ```
|
||||
///
|
||||
/// > Note: In `.testLoop` mode a `ChainRouter` allocates **its own** test source (shared by
|
||||
/// > the router's two boards via the splitter, so those two stay phase-locked to each other).
|
||||
/// > That source is independent of the global test source used by ``addBoard()``-created
|
||||
/// > boards, so a router's boards are *not* phase-locked to the main board list. Routers are
|
||||
/// > a separate multi-board path (not used by the standard builder UI); unifying them onto the
|
||||
/// > global source is intentionally out of scope here.
|
||||
///
|
||||
/// - Returns: A ``ChainRouter`` with both boards ready to receive pedals.
|
||||
/// - Throws:
|
||||
/// - `ShoeGazeError.liveInputRequiresInputChannels` when `inputSource == .liveInput`
|
||||
@@ -440,10 +490,21 @@ public final class ShoeGaze {
|
||||
|
||||
// Step 1: Remove all engine nodes that write to board buses.
|
||||
// Order within this step does not matter; all must finish before bus release.
|
||||
for source: TestLoopSource in testLoopSources {
|
||||
for tap: TestLoopTap in testLoopTaps {
|
||||
engine.remove(tap)
|
||||
}
|
||||
testLoopTaps.removeAll()
|
||||
|
||||
// Remove the shared global test source and release its bus (after its taps are removed,
|
||||
// preserving remove-before-release ordering).
|
||||
if let source: TestLoopSource = globalTestSource {
|
||||
engine.remove(source)
|
||||
}
|
||||
testLoopSources.removeAll()
|
||||
globalTestSource = nil
|
||||
if let testBus: AudioBus = globalTestBus {
|
||||
engine.busAllocator.releaseAudioBus(testBus)
|
||||
}
|
||||
globalTestBus = nil
|
||||
|
||||
for passThrough: HardwareInputPassThrough in inputPassThroughs {
|
||||
engine.remove(passThrough)
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user