From b29c8848ef8c708b27937ddf5b5ee8b3689e1e51 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sat, 11 Jul 2026 16:03:50 -0700 Subject: [PATCH] Review fix: exact-silence input mute (hard kill-switch gate) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit External review found the lagged mute gate leaks a faint transient (~-54 dB) on its first blocks: Lag seeds its state at 0, so gating on a lagged value never starts at exactly zero on a rig that comes up muted — the moment the mute is meant to keep things quiet. Measured directly via an NRT render. - Gate the input with a hard, un-lagged kill switch `(inputMute - 1) * -1` in both HardwareInputPassThrough and TestLoopTap. A muted node is then exactly silent from the first sample; a kill-switch mute is expected to be abrupt. Drop the now-unused muteLagTime constant. - Flip the library default to `inputMuted = false` (audible). Muting is the app's Start-time policy (EngineConfigView already re-asserts muted on user Start), so headless/offline library use isn't forced silent and render tests that route the test-loop through a board no longer depend on a leak to see signal. The pulsing glow now also starts at engine Start rather than before it. - Tests: add render-level mutedInputRendersSilent (exact silence) and unmutedInputRendersSignal; update the default-state and MIDI-latch expectations; drop the StereoRouting un-mutes that the leak had made necessary. - Plan doc: correct the mute default / gate description and the UserDefaults note. 463 tests pass; both app targets build. Co-Authored-By: Claude Opus 4.8 --- .../Input/HardwareInputPassThrough.swift | 13 ++-- .../LyrebirdShoeGaze/Input/TestLoopTap.swift | 13 ++-- Sources/LyrebirdShoeGaze/ShoeGaze.swift | 12 ++-- .../InputMuteTests.swift | 62 ++++++++++++++++--- .../MIDIControllerPCTests.swift | 10 +-- .../StereoRoutingTests.swift | 6 -- .../2.1.1-input-mute-and-io-persistence.md | 21 ++++--- 7 files changed, 89 insertions(+), 48 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Input/HardwareInputPassThrough.swift b/Sources/LyrebirdShoeGaze/Input/HardwareInputPassThrough.swift index 1427dd7..7908b7a 100644 --- a/Sources/LyrebirdShoeGaze/Input/HardwareInputPassThrough.swift +++ b/Sources/LyrebirdShoeGaze/Input/HardwareInputPassThrough.swift @@ -38,11 +38,6 @@ final class HardwareInputPassThrough: SynthesizerBase { /// Index of the board head bus to write to. private let outputBusIndex: Int - // MARK: - Constants - - /// Lag time in seconds for the mute transition — smooths audible ↔ muted to prevent clicks. - private let muteLagTime: Double = 0.02 - // MARK: - Init /// Creates a pass-through that copies `inputBusIndex` to `outputBusIndex`. @@ -60,9 +55,11 @@ final class HardwareInputPassThrough: SynthesizerBase { // MARK: - Build override func build() { - // Gate the copied signal by a lagged mute: mute 0 → gain 1 (audible), 1 → gain 0 (silent). - let muteLagged: Lag.Control = Lag.Control(input: $inputMute, time: muteLagTime) - let muteGain: any ValidGeneratorInput = (muteLagged - 1.0) * -1.0 + // Hard kill-switch gate: `1 − inputMute`, so mute 0 → gain 1 (audible), 1 → gain 0 (exactly + // silent). Unlagged on purpose — a lagged gate never settles to exactly zero on its first + // blocks and leaks a faint transient right when the mute is meant to keep things quiet; a + // kill-switch mute is expected to be abrupt anyway. + let muteGain: any ValidGeneratorInput = ($inputMute - 1.0) * -1.0 output(bus: outputBusIndex, source: input(bus: inputBusIndex) * muteGain) } } diff --git a/Sources/LyrebirdShoeGaze/Input/TestLoopTap.swift b/Sources/LyrebirdShoeGaze/Input/TestLoopTap.swift index f890ea8..a263898 100644 --- a/Sources/LyrebirdShoeGaze/Input/TestLoopTap.swift +++ b/Sources/LyrebirdShoeGaze/Input/TestLoopTap.swift @@ -38,11 +38,6 @@ final class TestLoopTap: SynthesizerBase { /// Index of the board head bus to write to. private let outputBusIndex: Int - // MARK: - Constants - - /// Lag time in seconds for the mute transition — smooths audible ↔ muted to prevent clicks. - private let muteLagTime: Double = 0.02 - // MARK: - Init /// Creates a tap that copies `inputBusIndex` (the global test bus) to `outputBusIndex` @@ -60,9 +55,11 @@ final class TestLoopTap: SynthesizerBase { // MARK: - Build override func build() { - // Gate the copied signal by a lagged mute: mute 0 → gain 1 (audible), 1 → gain 0 (silent). - let muteLagged: Lag.Control = Lag.Control(input: $inputMute, time: muteLagTime) - let muteGain: any ValidGeneratorInput = (muteLagged - 1.0) * -1.0 + // Hard kill-switch gate: `1 − inputMute`, so mute 0 → gain 1 (audible), 1 → gain 0 (exactly + // silent). Unlagged on purpose — a lagged gate never settles to exactly zero on its first + // blocks and leaks a faint transient right when the mute is meant to keep things quiet; a + // kill-switch mute is expected to be abrupt anyway. + let muteGain: any ValidGeneratorInput = ($inputMute - 1.0) * -1.0 output(bus: outputBusIndex, source: input(bus: inputBusIndex) * muteGain) } } diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index 9bec918..e34785c 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -436,9 +436,11 @@ public final class ShoeGaze { public private(set) var rigMuted: Bool = false /// `true` when the **input** feeding every board is muted — the incoming guitar / mic signal, - /// distinct from ``rigMuted`` (the output). Defaults `true` and is re-asserted on each user - /// engine start so a fresh start is silent until the player un-mutes. See ``setInputMuted(_:)``. - public private(set) var inputMuted: Bool = true + /// distinct from ``rigMuted`` (the output). Defaults `false` (audible); the app re-asserts it + /// muted on each user engine start (see `EngineConfigView`) so a fresh start is silent until the + /// player un-mutes, without forcing silence on headless / offline library use. See + /// ``setInputMuted(_:)``. + public private(set) var inputMuted: Bool = false /// Identities of boards currently in per-board full bypass. Observable so UI toggles refresh; /// authoritative bypass value lives on each board's `boardBypassNode`. Transient (not persisted). @@ -596,8 +598,8 @@ public final class ShoeGaze { /// distinct from the rig-output ``setRigMuted(_:)``. Applied to every board's input writer /// (test-loop tap or hardware pass-through), smoothed to avoid clicks. /// - /// Defaults muted (``inputMuted`` starts `true`) and is re-asserted on each user engine start - /// so a fresh start is silent until the player un-mutes — preventing accidental input noise. + /// 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) { diff --git a/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift b/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift index d78697c..3a10856 100644 --- a/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift +++ b/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift @@ -16,9 +16,9 @@ import LyrebirdUI @Suite("InputMute") struct InputMuteTests { - /// A fresh session defaults to input-muted, and every board's input writer comes up muted so a - /// just-started engine is silent until the player un-mutes. - @Test func defaultsMutedAndDrivesInputNodes() throws { + /// A fresh session defaults audible at the library level (the app mutes on engine Start), and + /// muting drives every board's input writer to the silent gain. + @Test func defaultsAudibleThenMuteDrivesInputNodes() 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) @@ -28,10 +28,14 @@ struct InputMuteTests { try board.add(VolumePedal()) try sg.finalizeBoard(board) - #expect(sg.inputMuted) + #expect(!sg.inputMuted) + let audible: Double = 0.0 + #expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible }) + #expect(!sg.inputMuteNodeValues.isEmpty) + + sg.setInputMuted(true) let muted: Double = 1.0 #expect(sg.inputMuteNodeValues.allSatisfy { $0 == muted }) - #expect(!sg.inputMuteNodeValues.isEmpty) } } @@ -82,7 +86,47 @@ struct InputMuteTests { } } - /// The live-input pass-through path is driven identically to the test-loop tap path. + /// A muted input renders **exactly** silent through a full render — the kill-switch gate reaches + /// zero with no residual. A lagged gate leaked a faint transient on its first blocks; the hard + /// gate does not. + @Test func mutedInputRendersSilent() throws { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.25, numAudioBuses: 256) + defer { try? FileManager.default.removeItem(at: url) } + let sg: ShoeGaze = ShoeGaze(engine: engine) // input defaults muted + + try withExtendedLifetime(sg) { + let board: PedalBoard = try sg.addBoard() + try board.add(VolumePedal()) + try sg.finalizeBoard(board) + sg.setInputMuted(true) + + try engine.start() + let samples: [Float] = readStereoCAF(url: url) + #expect(!hasNonSilentOutput(samples), + "a muted input must render silent — the kill-switch gate leaves no residual") + } + } + + /// 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) + 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) + sg.setInputMuted(false) + + try engine.start() + let samples: [Float] = readStereoCAF(url: url) + #expect(hasNonSilentOutput(samples), + "an un-muted input must pass the test-loop signal through") + } + } + + /// The live-input pass-through path is driven by `setInputMuted` identically to the test-loop tap. @Test func liveInputPassThroughIsMuted() throws { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.05, @@ -98,8 +142,12 @@ struct InputMuteTests { try board.add(VolumePedal()) try sg.finalizeBoard(board) + let audible: Double = 0.0 + #expect(!sg.inputMuted) + #expect(sg.inputMuteNodeValues == [audible]) + + sg.setInputMuted(true) let muted: Double = 1.0 - #expect(sg.inputMuted) #expect(sg.inputMuteNodeValues == [muted]) } } diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift index 68a75c8..ac5d55c 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift @@ -85,18 +85,18 @@ struct MIDIControllerPCTests { defer { try? FileManager.default.removeItem(at: url) } withExtendedLifetime(sg) { - // A fresh session starts input-muted by default. - #expect(sg.inputMuted == true, "input defaults muted") + // A fresh library session starts audible (the app mutes on engine Start). + #expect(sg.inputMuted == false, "input defaults audible at the library level") controller.armSwitchLearn(.inputMute) controller.ingestForTesting(.programChange(channel: 0, program: 2)) // learn only - #expect(sg.inputMuted == true, "learn must not toggle") + #expect(sg.inputMuted == false, "learn must not toggle") controller.ingestForTesting(.programChange(channel: 0, program: 2)) - #expect(sg.inputMuted == false, "first press un-mutes the input") + #expect(sg.inputMuted == true, "first press mutes the input") #expect(sg.rigMuted == false, "input mute must not touch the rig-output mute") controller.ingestForTesting(.programChange(channel: 0, program: 2)) - #expect(sg.inputMuted == true, "second press re-mutes the input") + #expect(sg.inputMuted == false, "second press un-mutes the input") } } diff --git a/Tests/LyrebirdShoeGazeTests/StereoRoutingTests.swift b/Tests/LyrebirdShoeGazeTests/StereoRoutingTests.swift index ece1502..3b0eb8c 100644 --- a/Tests/LyrebirdShoeGazeTests/StereoRoutingTests.swift +++ b/Tests/LyrebirdShoeGazeTests/StereoRoutingTests.swift @@ -70,7 +70,6 @@ struct StereoRoutingTests { #expect(board.outputBusRight != nil, "Stereo board should have a right tail bus") try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() let samples: [Float] = readStereoCAF(url: url) @@ -104,7 +103,6 @@ struct StereoRoutingTests { #expect(delay.outputBusRight != nil, "Auto-stereoized pedal should have a right output bus") try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() let samples: [Float] = readStereoCAF(url: url) @@ -130,7 +128,6 @@ struct StereoRoutingTests { #expect(board.width == 2, "Stereo-native pedal should widen a mono board") try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() let samples: [Float] = readStereoCAF(url: url) @@ -159,7 +156,6 @@ struct StereoRoutingTests { try board.add(ReverbPedal()) // mono pedal after merge, width-1 try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() let samples: [Float] = readStereoCAF(url: url) @@ -207,7 +203,6 @@ struct StereoRoutingTests { #expect(board.width == 2, "collectStereo should make the board stereo") try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() return deinterleave(readStereoCAF(url: url)) @@ -297,7 +292,6 @@ struct StereoRoutingTests { #expect(board.outputBusRight == nil) try shoeGaze.finalizeBoard(board) - shoeGaze.setInputMuted(false) // input defaults muted; un-mute so the test-loop signal renders try engine.start() let samples: [Float] = readStereoCAF(url: url) diff --git a/docs/plans/2.1.1-input-mute-and-io-persistence.md b/docs/plans/2.1.1-input-mute-and-io-persistence.md index 0e81caa..ac9fc16 100644 --- a/docs/plans/2.1.1-input-mute-and-io-persistence.md +++ b/docs/plans/2.1.1-input-mute-and-io-persistence.md @@ -8,8 +8,10 @@ input configuration survive relaunch. A hard mute on the **input** feeding every board (the guitar / mic signal), distinct from the existing rig-**output** mute. Goals: -- Defaults **muted**, and is **re-asserted muted every time the engine is (user-)started**, - so a fresh start is silent until the player un-mutes — kills accidental ruckus. +- The app **re-asserts it muted every time the engine is (user-)started**, so a fresh start + is silent until the player un-mutes — kills accidental ruckus. (The library-level default is + *audible*; muting is the app's Start-time policy, so headless / offline library use isn't + forced silent.) - A toggle button sits **next to the Start/Stop transport**. - **MIDI mappable** (Program Change + computer key), like Rig Mute. - Also default the input source to **In 0** (live input) instead of the test-loop arpeggio. @@ -18,12 +20,13 @@ from the existing rig-**output** mute. Goals: Each board's input writer (`TestLoopTap` in test-loop mode, `HardwareInputPassThrough` in live mode) gains an `@UIParam inputMute` (0 = audible, 1 = muted). `build()` multiplies -the copied signal by a lagged mute gain (20 ms, click-free), mirroring -`BoardOutputMaster.rigOutputGainStage()`. +the copied signal by a **hard** gain `(inputMute − 1)·−1` — an un-lagged kill switch. (A lagged +gate never settles to exactly zero on its first blocks and leaks a faint transient at the very +moment the mute is meant to keep things quiet; a kill-switch mute is expected to be abrupt.) ### ShoeGaze -- `public private(set) var inputMuted: Bool = true` +- `public private(set) var inputMuted: Bool = false` (app mutes on Start) - `setInputMuted(_:)` drives every `testLoopTaps` + `inputPassThroughs` mute node; `toggleInputMuted()`. - `addBoard()` sets each new input node's mute to the current `inputMuted` so rebuilds stay consistent. @@ -68,10 +71,10 @@ Fix — make the rig the single source of truth: unchanged. - **Input changes autosave.** `applyInputSource` / the engine-config `onConfigApplied` call `autosave()`, so a changed input or I/O layout is captured into the rig immediately. -- **Removed both UserDefaults paths.** The `ShoeGaze.inputSelection` / `ShoeGaze.inputChannels` - / `ShoeGaze.outputChannels` keys and their read/write sites (view + both app targets) are - deleted. The app targets build the engine at the default layout; `RigStore.restore` supplies - the rig's layout. +- **No UserDefaults path.** The `ShoeGaze.inputSelection` key and its read/write sites (builder + view + iOS model) are removed; the engine channel counts are never persisted to UserDefaults at + all. The app targets build the engine at the default layout; `RigStore.restore` supplies the + rig's saved layout. - **Fresh-launch default.** When there is no autosaved rig, the seed paths (`AppDelegate.seedInitialBoard` / iOS `seedFallbackBoard`) set `inputSource = .liveInput(0)` when the engine has an input channel, so a first launch listens to the instrument rather