feat(preboard): shared pre-board chain feeding every board

Add a pre-board stage: one linear pedal chain that processes the input once,
before the board split, and feeds every board — for front-of-chain gates /
drive / EQ, and multi-board "voice spray" rigs (one source → N panned boards →
Blend).

Routing: a stable mono feed bus every board taps. `FeedPassThrough` copies the
input source — or, when a pre-chain exists, that chain's mono output — onto the
feed bus (`ensureFeed`/`buildFeed`/`tearDownFeed`). The pre-chain is modeled as
an unfinalized `PedalBoard` fed from the source; a stereo tail folds to mono
(board heads are mono). Empty pre-chain = passthrough, byte-for-byte
behavior-preserving. addBoard taps the feed bus; teardown releases it
remove-before-release.

State: additive `ShoeGazeState.preChain` (`decodeIfPresent` → nil for old rigs,
no format bump; `hasSameRigContent` includes it). `captureState` reads the LIVE
pre-pedals so knob tweaks are saved; `rebuildAllBoards` sets the pre-chain
before building boards.

Edit API: appendPrePedal / insertPrePedal / removePrePedal / movePrePedal —
each folds in live knob values, then a full ordering-safe rig rebuild
(`setPreBoardChain`, mirrors setInputSource; rolls back on failure). Undo: a
pre-chain topology change (compared by id sequence, so knob-only edits stay on
the surgical path) rebuilds from the snapshot, stopping/starting the engine.
`allPedals()` now includes the pre-chain so pre-pedal knob undo resolves.

UI: an inline pre-board strip above the boards — hidden to a "+ Pre" button when
empty; otherwise the pre-pedals (keyed by stable id) with a per-pedal remove and
a "feeds all boards" divider. Palette adds route to the pre-board via a flag
(reset on any dismissal). Add / remove go through the undo wrapper.

Known scope: a pre-board edit resets transient/looper state (full rebuild, like
input-switch — setup-time); chain-router branches read the input directly and
are not fed by the pre-board. Both documented.

Tests: PreBoardChainTests (feeds boards, closed pre-Volume silences every board,
remove restores direct feed, save/load incl. a knob tweak, prior pre-pedal's
knob survives adding another, old-rig decode) + a pre-board undo test. Full
suite 844 green; both demos build. External review + caveman sweep: one blocking
(live knob sync) fixed + regression-tested, all non-blocking addressed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 19:18:04 -08:00
parent 1f5b70edc7
commit 9dfadb129d
9 changed files with 592 additions and 28 deletions
+154 -22
View File
@@ -192,11 +192,47 @@ public final class ShoeGaze {
private var globalTestSource: TestLoopSource?
private var globalTestBus: AudioBus?
/// Per-board taps copying the global test bus into each board's head bus.
/// Per-board taps copying the shared feed bus into each board's head bus.
private var testLoopTaps: [TestLoopTap] = []
private var inputPassThroughs: [HardwareInputPassThrough] = []
private var outputMasters: [BoardOutputMaster] = []
// MARK: - Pre-board chain
/// The shared **feed bus** every board taps. It carries the input source directly, or — when a
/// pre-board chain exists — that chain's mono output. Allocated lazily by ``ensureFeed()`` when
/// the first board is wired, and rebuilt with the rig on a pre-board edit (which reshapes it).
private var feedBus: AudioBus?
/// The node that writes ``feedBus``: copies the input source, or the pre-board chain's mono
/// output, onto the feed bus. Board taps read the feed bus and so must evaluate after this node.
private var feedWriter: FeedPassThrough?
/// The live pre-board processing chain (a ``PedalBoard`` whose mono output feeds every board),
/// or `nil` when there is no pre-board processing. Rebuilt from ``preBoardStates``.
private var preChainBoard: PedalBoard?
/// The tap that feeds the pre-board chain's head bus from the input source. `nil` when there is
/// no pre-board chain.
private var preChainFeedTap: FeedPassThrough?
/// Persisted pre-board pedal states — the serialization source of truth. The live
/// ``preChainBoard`` is rebuilt from these. Empty when there is no pre-board processing.
/// Set via the pre-board edit API and on rig load; read by ``ensureFeed()`` when wiring the feed.
public internal(set) var preBoardStates: [PedalState] = []
/// The live pedals currently in the pre-board chain, in signal-flow order; empty when there is no
/// pre-board processing. For UI rendering + inspection (the persisted truth is ``preBoardStates``).
public var preBoardPedals: [Pedal] { preChainBoard?.pedals ?? [] }
/// The pre-board states captured from the **live** pre-pedals (so knob / switch tweaks are
/// included), or the stored ``preBoardStates`` when there is no live chain. Use this — never the
/// raw ``preBoardStates`` — wherever the pre-chain is saved or rebuilt, so live edits aren't lost.
func capturePreBoardStates() -> [PedalState] {
guard let pre: PedalBoard = preChainBoard else { return preBoardStates }
return pre.pedals.map { $0.captureState() }
}
/// All chain routers created via ``makeChainRouter()``.
/// Routers are torn down (in reverse-creation order) by ``teardown()``.
///
@@ -248,6 +284,7 @@ public final class ShoeGaze {
/// Every live `Pedal` across all main boards and chain-router branches.
private func allPedals() -> [Pedal] {
var result: [Pedal] = []
result.append(contentsOf: preBoardPedals)
for board: PedalBoard in boards {
result.append(contentsOf: pedals(in: board))
}
@@ -310,38 +347,36 @@ public final class ShoeGaze {
let board: PedalBoard = try PedalBoard(engine: engine, clock: clock)
board.label = "Board \(boards.count + 1)"
// Route the input source to the board head bus.
// Every board taps the shared feed bus — the input source, or (when present) the pre-board
// chain's mono output. The tap evaluates after the feed writer so it reads a written bus.
let (feedBusIndex, feedNode): (Int, any Synthesizer) = try ensureFeed()
switch inputSource {
case .testLoop:
// 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()
// All boards share one feed (and one global test source upstream of it) so they stay
// phase-locked. Each board gets a tap copying the feed bus into its head bus.
let tap: TestLoopTap = TestLoopTap(
inputBusIndex: testBus.index,
inputBusIndex: feedBusIndex,
outputBusIndex: board.headBus.index
)
// Prime the mute gain before splicing the node in, so its very first render block
// reflects the current mute state with no ramp (see `primeInputMute`).
primeInputMute(tap.inputMuteNode, muted: inputMuted)
try engine.add(tap, position: NodePosition.after(source))
try engine.add(tap, position: NodePosition.after(feedNode))
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.
// A HardwareInputPassThrough copies it to the board's head bus so all pedals
// read from a board-owned bus (not the raw hardware input bus).
let hardwareInputBusIndex: Int = try liveInputBusIndex(channel: channel, engine: engine)
case .liveInput:
// Live input flows through the same feed bus (which carries the hardware input, or the
// pre-board chain fed from it). The per-board input mute stays on this tap.
let passThrough: HardwareInputPassThrough = HardwareInputPassThrough(
inputBusIndex: hardwareInputBusIndex,
inputBusIndex: feedBusIndex,
outputBusIndex: board.headBus.index
)
// Prime the mute gain before splicing the node in (see `primeInputMute`).
primeInputMute(passThrough.inputMuteNode, muted: inputMuted)
try engine.add(passThrough)
try engine.add(passThrough, position: NodePosition.after(feedNode))
inputPassThroughs.append(passThrough)
board.headInputNode = passThrough
}
@@ -383,6 +418,100 @@ public final class ShoeGaze {
return (source, testBus)
}
// MARK: - Pre-board feed wiring
/// Ensures the input source for the current ``inputSource`` exists and returns the bus it writes
/// plus the node that writes it (`nil` for live input — the hardware input bus is already
/// populated at block start, so nothing need evaluate before a reader).
private func ensureInputSourceBus() throws -> (bus: Int, node: (any Synthesizer)?) {
switch inputSource {
case .testLoop:
let (source, testBus): (TestLoopSource, AudioBus) = try ensureGlobalTestSource()
return (testBus.index, source)
case .liveInput(let channel):
let index: Int = try liveInputBusIndex(channel: channel, engine: engine)
return (index, nil)
}
}
/// Ensures the shared feed bus and its writer exist for the current input source and pre-board
/// state, returning the feed bus index and the node a board must order its input tap after.
@discardableResult
func ensureFeed() throws -> (bus: Int, node: any Synthesizer) {
if let feedBus: AudioBus = feedBus, let feedWriter: FeedPassThrough = feedWriter {
return (feedBus.index, feedWriter)
}
return try buildFeed()
}
/// Allocates the feed bus (if needed), builds the pre-board chain when ``preBoardStates`` is
/// non-empty (source → pedals → mono output), and creates the feed writer that copies the source
/// or the pre-chain output onto the feed bus. Returns the feed bus index + the feed writer node.
@discardableResult
private func buildFeed() throws -> (bus: Int, node: any Synthesizer) {
let (sourceBus, sourceNode): (Int, (any Synthesizer)?) = try ensureInputSourceBus()
let feed: AudioBus = try feedBus ?? engine.busAllocator.allocateAudioBus(channelCount: 1)
feedBus = feed
// What the feed writer reads: the source directly, or the pre-board chain's mono output.
var writerInputBus: Int = sourceBus
var writerAfter: (any Synthesizer)? = sourceNode
if !preBoardStates.isEmpty {
let pre: PedalBoard = try PedalBoard(engine: engine, clock: clock)
// Feed the pre-chain head from the input source.
let feedTap: FeedPassThrough = FeedPassThrough(
inputBusIndex: sourceBus,
outputBusIndex: pre.headBus.index
)
if let sourceNode: any Synthesizer = sourceNode {
try engine.add(feedTap, position: NodePosition.after(sourceNode))
} else {
try engine.add(feedTap)
}
pre.headInputNode = feedTap
preChainFeedTap = feedTap
for state: PedalState in preBoardStates {
guard let pedal: Pedal = PedalRegistry.makePedal(type: state.type) else { continue }
pedal.applyState(state)
try pre.add(pedal)
}
// The feed is mono (board heads are mono); fold any multi-channel pre-chain tail to mono.
if pre.width > 1 {
try pre.mergeToMono()
}
preChainBoard = pre
writerInputBus = pre.outputBus.index
writerAfter = pre.tailNode ?? feedTap
}
let writer: FeedPassThrough = FeedPassThrough(inputBusIndex: writerInputBus, outputBusIndex: feed.index)
if let after: any Synthesizer = writerAfter {
try engine.add(writer, position: NodePosition.after(after))
} else {
try engine.add(writer)
}
feedWriter = writer
return (feed.index, writer)
}
/// Removes the feed writer + pre-board chain nodes and releases the feed bus. Called from
/// ``teardown()`` (the whole feed is rebuilt from scratch on the next ``ensureFeed()``).
private func tearDownFeed() {
if let writer: FeedPassThrough = feedWriter { engine.remove(writer) }
feedWriter = nil
if let feedTap: FeedPassThrough = preChainFeedTap { engine.remove(feedTap) }
preChainFeedTap = nil
preChainBoard?.teardown()
preChainBoard = nil
if let feed: AudioBus = feedBus {
engine.busAllocator.releaseAudioBus(feed)
feedBus = nil
}
}
/// 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(_:)`.
@@ -837,9 +966,17 @@ public final class ShoeGaze {
engine.remove(tap)
}
testLoopTaps.removeAll()
for passThrough: HardwareInputPassThrough in inputPassThroughs {
engine.remove(passThrough)
}
inputPassThroughs.removeAll()
// Remove the shared global test source and release its bus (after its taps are removed,
// preserving remove-before-release ordering).
// Remove the feed writer + pre-board chain (the taps that read the feed bus are now gone)
// and release the feed bus, before the source it reads is removed.
tearDownFeed()
// Remove the shared global test source and release its bus (after the taps + feed that read
// it are removed, preserving remove-before-release ordering).
if let source: TestLoopSource = globalTestSource {
engine.remove(source)
}
@@ -849,11 +986,6 @@ public final class ShoeGaze {
}
globalTestBus = nil
for passThrough: HardwareInputPassThrough in inputPassThroughs {
engine.remove(passThrough)
}
inputPassThroughs.removeAll()
for master: BoardOutputMaster in outputMasters {
engine.remove(master)
}