refactor(rigs): address external-review findings
- Recall router state in applyState: the lightweight (no-rebuild) recall path now positionally applies router branch pedal params and the splitter/merger blend, instead of silently dropping them for routed rigs. Test added. - Simplify first-run seeding: RigLibraryStore.save persists asynchronously (best-effort, logged not thrown), so the previous per-entry rollback could never fire for a real disk failure. Replaced with an honest best-effort save + working-rig fallback, and a comment stating the semantics. - Move FactoryRigBuild to its own file (one public type per file). - Document that a router's output master gain is intentionally not persisted, matching BoardState's treatment of board master gain. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,10 @@
|
||||
/// selection (and any continuous blend between them); the merger's two levels capture the output mix.
|
||||
///
|
||||
/// Persisted as part of ``ShoeGazeState/routers`` so a routed multi-board rig survives save / load.
|
||||
///
|
||||
/// > Note: The router's output-stage master gain (`masterGainNode`) is intentionally not captured,
|
||||
/// > matching the existing behaviour for a plain board's `masterGainNode` in `BoardState` — output
|
||||
/// > level is treated as a live mix control, not part of the saved rig.
|
||||
public struct ChainRouterState: Codable, Equatable {
|
||||
|
||||
/// Pedals on board A (the first branch), in chain order.
|
||||
|
||||
@@ -97,15 +97,3 @@ public struct FactoryRig: Identifiable, Sendable {
|
||||
self.build = .session(sessionBuild)
|
||||
}
|
||||
}
|
||||
|
||||
/// How a ``FactoryRig`` builds its graph at seed time.
|
||||
public enum FactoryRigBuild: Sendable {
|
||||
|
||||
/// Appends pedals to a single pre-made, empty board; the seeding driver calls `addBoard` before
|
||||
/// and `finalizeBoard` after.
|
||||
case singleBoard(@MainActor (_ session: ShoeGaze, _ board: PedalBoard) throws -> Void)
|
||||
|
||||
/// Builds the whole graph against the session (boards and/or routers), owning its own
|
||||
/// `addBoard`/`finalizeBoard` and `makeChainRouter`/`finalize` lifecycle.
|
||||
case session(@MainActor (_ session: ShoeGaze) throws -> Void)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// FactoryRigBuild.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/// How a ``FactoryRig`` builds its graph at seed time.
|
||||
public enum FactoryRigBuild: Sendable {
|
||||
|
||||
/// Appends pedals to a single pre-made, empty board; the seeding driver calls `addBoard` before
|
||||
/// and `finalizeBoard` after.
|
||||
case singleBoard(@MainActor (_ session: ShoeGaze, _ board: PedalBoard) throws -> Void)
|
||||
|
||||
/// Builds the whole graph against the session (boards and/or routers), owning its own
|
||||
/// `addBoard`/`finalizeBoard` and `makeChainRouter`/`finalize` lifecycle.
|
||||
case session(@MainActor (_ session: ShoeGaze) throws -> Void)
|
||||
}
|
||||
@@ -410,6 +410,19 @@ extension ShoeGaze {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Routers recall positionally too: their branch pedals' params and the splitter/merger
|
||||
// blend. Topology (pedal counts/types) is assumed identical for a lightweight recall — a
|
||||
// mismatch is skipped, exactly as for boards. A `rebuildsOnChange` selector inside a router
|
||||
// branch is not handled here (routers have no per-branch rebuild path); use `loadRig(from:)`
|
||||
// for that.
|
||||
for (router, routerState): (ChainRouter, ChainRouterState) in zip(routers, state.routers ?? []) {
|
||||
applyLinearParams(routerState.boardA, to: router.boardA.pedals)
|
||||
applyLinearParams(routerState.boardB, to: router.boardB.pedals)
|
||||
router.splitter.branchAEnabled = routerState.branchA
|
||||
router.splitter.branchBEnabled = routerState.branchB
|
||||
router.merger?.levelA = routerState.mergerLevelA
|
||||
router.merger?.levelB = routerState.mergerLevelB
|
||||
}
|
||||
// A recall restores each pedal's tempo-sync state but not its derived values (no clock is
|
||||
// consulted in `Pedal.applyState`), so recompute them against the rig's current tempo —
|
||||
// mirroring the full `loadRig(from:)` path.
|
||||
@@ -418,6 +431,15 @@ extension ShoeGaze {
|
||||
}
|
||||
}
|
||||
|
||||
/// Positionally applies pedal parameter snapshots to a live linear pedal chain (e.g. a router
|
||||
/// branch). A position whose live pedal type doesn't match the snapshot is skipped, matching the
|
||||
/// silent-skip semantics of the board recall path.
|
||||
private func applyLinearParams(_ states: [PedalState], to pedals: [Pedal]) {
|
||||
for (pedal, state): (Pedal, PedalState) in zip(pedals, states) where pedal.displayName == state.type {
|
||||
pedal.applyState(state)
|
||||
}
|
||||
}
|
||||
|
||||
/// Positionally applies segment parameter snapshots to live board segments, including
|
||||
/// branch pedals and split routing modes. Type or topology mismatches at any position are
|
||||
/// skipped silently (lightweight recall is for topologically identical rigs).
|
||||
|
||||
@@ -46,39 +46,20 @@ public enum RigLibraryBootstrap {
|
||||
// working session is intentionally left as-is (no `currentRigID`) — the user picks a starter
|
||||
// rig from the sidebar to load it, rather than the highlight pointing at a rig the working
|
||||
// graph has not actually loaded.
|
||||
let entries: [RigEntry]
|
||||
do {
|
||||
entries = try FactoryRigLibrary.makeEntries()
|
||||
} catch {
|
||||
// Building the shipped rigs is deterministic and should never fail at runtime; if it
|
||||
// somehow does, fall back to seeding the working rig so the library is not left empty.
|
||||
print("[RigLibraryBootstrap] building factory rigs failed: \(error)")
|
||||
seedWorkingRig(store, seedFrom: shoeGaze, seedName: seedName)
|
||||
return
|
||||
}
|
||||
|
||||
// Save sequentially, rolling back on failure. Seeding is gated on `store.entries.isEmpty`,
|
||||
// so a partially-saved library would be frozen forever (never retried, never completed). If
|
||||
// any save fails, delete the entries saved this run so the library returns to empty and a
|
||||
// later launch retries the full seed. (This is best-effort, not truly atomic: if a rollback
|
||||
// `delete` itself fails, a partial set can still be left behind and frozen — the residual
|
||||
// hole a non-transactional store cannot fully close.)
|
||||
var savedThisRun: [RigEntry] = []
|
||||
// Seed the shipped starter rigs. `store.save` updates the in-memory library synchronously
|
||||
// (so the session sees every rig immediately) and persists to disk asynchronously — a disk
|
||||
// failure is logged by the store, not thrown here, so there is no per-entry rollback to do.
|
||||
// `save` throws only on a synchronous encode failure of this deterministic rig data, which
|
||||
// should not happen at runtime; if the build or an encode somehow fails, fall back to
|
||||
// seeding the working rig so the library is never left empty on first run.
|
||||
do {
|
||||
let entries: [RigEntry] = try FactoryRigLibrary.makeEntries()
|
||||
for entry: RigEntry in entries {
|
||||
try store.save(entry)
|
||||
savedThisRun.append(entry)
|
||||
}
|
||||
} catch {
|
||||
print("[RigLibraryBootstrap] seeding factory rigs failed after "
|
||||
+ "\(savedThisRun.count)/\(entries.count); rolling back: \(error)")
|
||||
for entry: RigEntry in savedThisRun {
|
||||
do {
|
||||
try store.delete(entry)
|
||||
} catch {
|
||||
print("[RigLibraryBootstrap] rollback of '\(entry.name)' failed: \(error)")
|
||||
}
|
||||
}
|
||||
print("[RigLibraryBootstrap] seeding factory rigs failed: \(error)")
|
||||
seedWorkingRig(store, seedFrom: shoeGaze, seedName: seedName)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,32 @@ struct ChainRouterPersistenceTests {
|
||||
#expect(hasNonSilentOutput(samples), "rebuilt router produced silence")
|
||||
}
|
||||
|
||||
@Test func applyStateRecallsRouterParamsLive() throws {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let session: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let router: ChainRouter = try session.makeChainRouter()
|
||||
let fuzz: FuzzPedal = FuzzPedal()
|
||||
fuzz.fuzz = 20.0
|
||||
try router.addToA(fuzz)
|
||||
try router.addToB(ReverbPedal())
|
||||
router.mode = .a
|
||||
try router.finalize()
|
||||
|
||||
let original: ShoeGazeState = session.captureState()
|
||||
|
||||
// Mutate live: change a router pedal param and flip the A/B selection.
|
||||
fuzz.fuzz = 55.0
|
||||
router.mode = .b
|
||||
|
||||
// Lightweight recall should restore the router pedal param and blend without a rebuild.
|
||||
session.applyState(original)
|
||||
|
||||
#expect(fuzz.fuzz == 20.0, "router pedal param was not recalled")
|
||||
#expect(router.splitter.branchAEnabled == 1.0, "branch A level not recalled")
|
||||
#expect(router.splitter.branchBEnabled == 0.0, "branch B level not recalled")
|
||||
}
|
||||
|
||||
@Test func routerlessRigStoresNoRoutersKey() throws {
|
||||
// A plain single-board session must not gain a `routers` payload (byte-compat with rigs
|
||||
// saved before router persistence existed).
|
||||
|
||||
Reference in New Issue
Block a user