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
@@ -0,0 +1,137 @@
// PreBoardChainTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("Pre-board chain")
struct PreBoardChainTests {
/// Builds an NRT-backed `ShoeGaze` with `boardCount` single-Overdrive boards.
private func makeRig(
boardCount: Int,
duration: Double = 0.1
) throws -> (ShoeGaze, SynthesisEngine, URL) {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 256)
let sg: ShoeGaze = ShoeGaze(engine: engine)
for _ in 0 ..< boardCount {
let board: PedalBoard = try sg.addBoard()
try board.add(OverdrivePedal())
try sg.finalizeBoard(board)
}
return (sg, engine, url)
}
private func render(_ engine: SynthesisEngine, _ url: URL) throws -> [Float] {
try engine.start()
return readStereoCAF(url: url)
}
@Test func preBoardChainRendersAndFeedsBoards() throws {
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2)
defer { try? FileManager.default.removeItem(at: url) }
try sg.appendPrePedal(.overdrive)
#expect(sg.preBoardStates.count == 1)
#expect(sg.preBoardPedals.count == 1, "the live pre-chain holds the added pedal")
#expect(sg.boards.count == 2, "pre-board edit leaves the boards in place")
let samples: [Float] = try render(engine, url)
#expect(hasNonSilentOutput(samples), "a pre-board pedal keeps the signal flowing to the boards")
}
@Test func aClosedPreBoardVolumeSilencesEveryBoard() throws {
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2)
defer { try? FileManager.default.removeItem(at: url) }
try sg.appendPrePedal(.volume)
// Close the pre-board volume (position 0 = silent) before start, so it is delivered flat.
let volume: Pedal = try #require(sg.preBoardPedals.first)
let state: PedalState = volume.captureState()
try #require(!state.knobValues.isEmpty)
volume.applyState(
PedalState(id: state.id, type: state.type, knobValues: [0.0],
switchValues: state.switchValues, bypass: state.bypass)
)
let samples: [Float] = try render(engine, url)
#expect(!hasNonSilentOutput(samples), "a closed pre-board volume silences the feed to every board")
}
@Test func removingTheLastPrePedalRestoresTheDirectFeed() throws {
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
defer { try? FileManager.default.removeItem(at: url) }
try sg.appendPrePedal(.overdrive)
#expect(sg.preBoardPedals.count == 1)
try sg.removePrePedal(at: 0)
#expect(sg.preBoardStates.isEmpty)
#expect(sg.preBoardPedals.isEmpty, "the pre-chain is gone")
let samples: [Float] = try render(engine, url)
#expect(hasNonSilentOutput(samples), "the board renders from the direct feed again")
}
@Test func preBoardChainRoundTripsThroughSaveLoad() throws {
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
defer { try? FileManager.default.removeItem(at: url) }
try sg.appendPrePedal(.overdrive)
try sg.appendPrePedal(.delay)
#expect(sg.preBoardStates.map { $0.type } == ["Overdrive", "Delay"])
// Tweak the LIVE first pre-pedal's knob — the save must capture it, not the stale default.
let knob: PedalKnob = try #require(sg.preBoardPedals.first?.knobs.first)
let sentinel: Double = knob.range.lowerBound + (knob.range.upperBound - knob.range.lowerBound) * 0.41
knob.node.update(sentinel)
let preset: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString + ".json")
defer { try? FileManager.default.removeItem(at: preset) }
try sg.saveRig(to: preset)
let (engine2, url2): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url2) }
let sg2: ShoeGaze = ShoeGaze(engine: engine2)
try sg2.loadRig(from: preset)
#expect(sg2.preBoardStates.map { $0.type } == ["Overdrive", "Delay"], "pre-chain round-trips")
#expect(sg2.preBoardPedals.count == 2, "the reloaded pre-chain is live")
let reloadedKnob: Double = try #require(sg2.preBoardPedals.first?.captureState().knobValues.first)
#expect(abs(reloadedKnob - sentinel) < 1e-9, "a pre-pedal knob tweak survives save/load")
try engine2.start()
#expect(hasNonSilentOutput(readStereoCAF(url: url2)), "reloaded pre-board rig renders")
}
@Test func addingASecondPrePedalKeepsTheFirstsLiveKnob() throws {
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
defer { try? FileManager.default.removeItem(at: url) }
try sg.appendPrePedal(.overdrive)
let knob: PedalKnob = try #require(sg.preBoardPedals.first?.knobs.first)
let sentinel: Double = knob.range.lowerBound + (knob.range.upperBound - knob.range.lowerBound) * 0.37
knob.node.update(sentinel)
// Adding a second pre-pedal rebuilds the whole pre-chain; the first pedal's live knob must be
// folded into the rebuild, not reset to its default.
try sg.appendPrePedal(.delay)
let firstKnob: Double = try #require(sg.preBoardPedals.first?.captureState().knobValues.first)
#expect(abs(firstKnob - sentinel) < 1e-9, "a prior pre-pedal's live knob survives adding another")
}
@Test func oldRigWithoutPreChainDecodesToEmpty() throws {
// A ShoeGazeState encoded without the preChain key (older rig) decodes to no pre-board chain.
let state: ShoeGazeState = ShoeGazeState(boards: [])
#expect(state.preChain == nil)
let data: Data = try JSONEncoder().encode(state)
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
#expect(decoded.preChain == nil, "absent/nil preChain stays nil")
}
}
@@ -386,6 +386,26 @@ struct UndoCoordinatorTests {
#expect(abs(selector.node.value - newValue) < 1e-9)
}
// MARK: - Undo of a pre-board edit
@Test func undoOfAddPrePedalRemovesItAndRedoBringsItBack() throws {
let (sg, undo, url, board): (ShoeGaze, UndoCoordinator, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])
defer { try? FileManager.default.removeItem(at: url) }
_ = board
try undo.perform("Add pre-pedal") { try sg.appendPrePedal(.reverb) }
#expect(sg.preBoardStates.map { $0.type } == ["Reverb"])
#expect(names(sg) == ["Overdrive"], "the board is untouched by a pre-board edit")
undo.undo()
#expect(sg.preBoardStates.isEmpty, "undo removes the pre-board pedal")
#expect(names(sg) == ["Overdrive"], "the board survives the pre-board undo")
undo.redo()
#expect(sg.preBoardStates.map { $0.type } == ["Reverb"], "redo re-adds the pre-board pedal")
}
// MARK: - Parameter resolution relies on unique labels per group
@Test func controlLabelsAreUniqueWithinEachGroupAcrossTheCatalogue() throws {