From e36074e9d30ea85762a0e7d63b141eb94b85f8cc Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sat, 11 Jul 2026 21:34:46 -0700 Subject: [PATCH] Review fix: prime input-mute ramp + mute before engine start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Sources/LyrebirdShoeGaze/ShoeGaze.swift | 45 ++++++++++++++++--- .../LyrebirdShoeGazeUI/EngineConfigView.swift | 10 +++-- .../InputMuteTests.swift | 35 +++++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index e34785c..37d7cf3 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -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. /// diff --git a/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift b/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift index b45edeb..3bb494c 100644 --- a/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift +++ b/Sources/LyrebirdShoeGazeUI/EngineConfigView.swift @@ -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() } diff --git a/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift b/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift index 3a10856..c5deb2e 100644 --- a/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift +++ b/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift @@ -107,6 +107,41 @@ struct InputMuteTests { } } + /// Muting while the engine is stopped **primes** every input node — its ramp start (`previous + /// OutputValue`) equals the muted target — so the next Start renders muted from block zero with no + /// audible→muted ramp. Guards against the `UIParamNode` one-block ramp leaking a fresh node's + /// prior (audible) value. A freshly-added board is primed the same way. + @Test func stoppedMutePrimesNodesSoTheresNoStartRamp() throws { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256) + defer { try? FileManager.default.removeItem(at: url) } + let sg: ShoeGaze = ShoeGaze(engine: engine) + + try withExtendedLifetime(sg) { + let board: PedalBoard = try sg.addBoard() + try board.add(VolumePedal()) + try sg.finalizeBoard(board) + + // Default audible: node value and ramp start both at 0 (no pending ramp). + let audible: Double = 0.0 + #expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible }) + #expect(sg.inputMutePreviousValues.allSatisfy { $0 == audible }) + + // Mute while stopped: both the target and the ramp start move to 1 → no leak on Start. + sg.setInputMuted(true) + let muted: Double = 1.0 + #expect(sg.inputMuteNodeValues.allSatisfy { $0 == muted }) + #expect(sg.inputMutePreviousValues.allSatisfy { $0 == muted }, + "a stopped-engine mute must prime the ramp start so the first render can't ramp") + + // A board added while muted comes up primed muted too. + let second: PedalBoard = try sg.addBoard() + try second.add(VolumePedal()) + try sg.finalizeBoard(second) + #expect(sg.inputMutePreviousValues.allSatisfy { $0 == muted }) + #expect(sg.inputMuteNodeValues.count == 2) + } + } + /// Un-muting the input lets the test-loop signal through — the gate opens fully. @Test func unmutedInputRendersSignal() throws { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.25, numAudioBuses: 256)