Review fix: prime input-mute ramp + mute before engine start

Two blocking findings from external review:

1. UIParamNode itself ramps from previousOutputValue (seeded at the compile-time
   default, advanced only by RT render — update() doesn't touch it) to the target
   over the first block. So a node told to start muted still ramped full-gain→0
   across block 0; the earlier "hard gate" only moved the leak from Lag to the
   param's own ramp. The mutedInputRendersSilent test passed by accident (the
   test-loop tone is quiet in that first block).
   Fix: prime previousOutputValue to the target. Race-free before a node is
   spliced into the graph (addBoard) and safe on a spliced node while the engine
   is stopped; a running engine keeps the ramp as a click-free live toggle. New
   setInputMuteGain(_:muted:seedRamp:) / primeInputMute helpers; setInputMuted
   seeds only when engine.isRunning == false.

2. EngineConfigView.startEngine called setInputMuted(true) AFTER engine.start(),
   so the engine came up unmuted for the first blocks. Moved it before start()
   (engine stopped there, so the gain is seeded with no ramp).

Also: correct the setInputMuted doc (abrupt kill-switch, not "smoothed").
Regression test stoppedMutePrimesNodesSoTheresNoStartRamp asserts the ramp start
equals the target (no pending ramp) after a stopped mute and for a board added
while muted. 464 tests pass; both app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 21:34:46 -07:00
parent b29c8848ef
commit e36074e9d3
3 changed files with 80 additions and 10 deletions
+39 -6
View File
@@ -301,8 +301,10 @@ public final class ShoeGaze {
inputBusIndex: testBus.index,
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))
tap.inputMuteNode.update(inputMuted ? 1.0 : 0.0)
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:`).
@@ -317,8 +319,9 @@ public final class ShoeGaze {
inputBusIndex: hardwareInputBusIndex,
outputBusIndex: board.headBus.index
)
// Prime the mute gain before splicing the node in (see `primeInputMute`).
primeInputMute(passThrough.inputMuteNode, muted: inputMuted)
try engine.add(passThrough)
passThrough.inputMuteNode.update(inputMuted ? 1.0 : 0.0)
inputPassThroughs.append(passThrough)
board.headInputNode = passThrough
}
@@ -596,23 +599,45 @@ public final class ShoeGaze {
/// Mutes or un-mutes the **input** feeding every board — the incoming guitar / mic signal,
/// distinct from the rig-output ``setRigMuted(_:)``. Applied to every board's input writer
/// (test-loop tap or hardware pass-through), smoothed to avoid clicks.
/// (test-loop tap or hardware pass-through) as an abrupt kill-switch gate (unlagged, so a
/// muted input is exactly silent — see the note in `HardwareInputPassThrough.build()`).
///
/// The app re-asserts this muted on each user engine start (see `EngineConfigView`) so a fresh
/// start is silent until the player un-mutes — preventing accidental input noise.
///
/// - Parameter muted: `true` to silence the input, `false` to let it through.
public func setInputMuted(_ muted: Bool) {
let value: Double = muted ? 1.0 : 0.0
// Seed the ramp start only while the engine is stopped: then the next Start renders the new
// mute state from block zero with no audible→muted (or muted→audible) ramp — which is exactly
// what "muted on Start" needs. While the engine is running, seeding `previousOutputValue`
// would race the RT thread; leaving it lets the one-block ramp act as a click-free toggle.
let seedRamp: Bool = !engine.isRunning
for tap: TestLoopTap in testLoopTaps {
tap.inputMuteNode.update(value)
setInputMuteGain(tap.inputMuteNode, muted: muted, seedRamp: seedRamp)
}
for passThrough: HardwareInputPassThrough in inputPassThroughs {
passThrough.inputMuteNode.update(value)
setInputMuteGain(passThrough.inputMuteNode, muted: muted, seedRamp: seedRamp)
}
inputMuted = muted
}
/// Primes a freshly-created input-mute node (not yet spliced into the graph) to the given mute
/// state so its first render block has no ramp. Race-free because the node is not yet evaluated
/// by the RT thread. See ``setInputMuteGain(_:muted:seedRamp:)``.
private func primeInputMute(_ node: UIParamNode, muted: Bool) {
setInputMuteGain(node, muted: muted, seedRamp: true)
}
/// Sets an input node's mute gain. `update` stores the atomic target the RT thread ramps toward
/// from `previousOutputValue`; that seed is only advanced by RT render, so `update` alone leaves
/// a one-block ramp from the node's prior value. When `seedRamp` is `true` (node not yet spliced,
/// or engine stopped) we also seed `previousOutputValue` to the target so there is no ramp.
private func setInputMuteGain(_ node: UIParamNode, muted: Bool, seedRamp: Bool) {
let value: Double = muted ? 1.0 : 0.0
node.update(value)
if seedRamp { node.previousOutputValue = value }
}
/// Toggles the input mute. See ``setInputMuted(_:)``.
public func toggleInputMuted() {
setInputMuted(!inputMuted)
@@ -626,6 +651,14 @@ public final class ShoeGaze {
+ inputPassThroughs.map { $0.inputMuteNode.value }
}
/// The `previousOutputValue` (ramp start) of every board input-mute node. Exposed for tests to
/// confirm a stopped-engine mute is *primed* — ramp start equal to the target — so the next
/// render can't ramp from the prior value and leak a block. See ``setInputMuteGain(_:muted:seedRamp:)``.
var inputMutePreviousValues: [Double] {
testLoopTaps.map { $0.inputMuteNode.previousOutputValue }
+ inputPassThroughs.map { $0.inputMuteNode.previousOutputValue }
}
/// Removes a single board and its associated input source and output master from
/// the engine, then releases the board's buses. The other boards are unaffected.
///
@@ -193,6 +193,12 @@ public struct EngineConfigView: View {
)
}
}
// A deliberate user Start comes up with the input muted so accidental noise can't leak
// the instant the engine spins up. Set it BEFORE start (engine is stopped here, so the
// gain is seeded with no ramp) — asserting it after start would leave the first render
// blocks unmuted. Transient stop/start-around-rebuild paths call `engine.start()`
// directly and are unaffected — they preserve the current mute state.
shoeGaze.setInputMuted(true)
try shoeGaze.engine.start()
} catch {
handleStartError(error)
@@ -211,10 +217,6 @@ public struct EngineConfigView: View {
onConfigApplied()
return
}
// A deliberate user start comes up with the input muted so accidental noise can't leak the
// instant the engine spins up — the player un-mutes when ready. Transient stop/start-around-
// rebuild paths call `engine.start()` directly and are unaffected.
shoeGaze.setInputMuted(true)
transportTick.toggle()
onConfigApplied()
}