Files
ShoeGaze/Sources/LyrebirdShoeGaze/Input/HardwareInputPassThrough.swift
T
josh 5987ec7b78 Add input mute + persist engine I/O counts
Input mute: a hard gate on the signal feeding every board (guitar / mic),
distinct from the rig-output mute. Each board input writer (TestLoopTap /
HardwareInputPassThrough) gains a lagged @UIParam mute gain; ShoeGaze holds
inputMuted (default true), drives every input node, and brings newly-added
boards up at the current state. A deliberate user Start re-asserts the mute so
a fresh engine is silent until the player un-mutes — cutting accidental noise.
Toggle sits beside the Start/Stop transport and is MIDI mappable
(MIDIMap.Action.inputMute). First launch now defaults to live In 0 instead of
the test-loop arpeggio.

I/O persistence: the engine's input/output channel counts now persist to
UserDefaults (written by EngineConfigView, restored by the app targets at
engine construction), so a multi-channel setup survives relaunch and a saved
live-input channel stays valid — fixing the input selection reverting to the
test loop after a restart.

Tests: InputMuteTests (default-muted, node driving, per-board adoption, live
path) + a MIDI PC input-mute latch test. StereoRouting render tests un-mute
before rendering. 460 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 15:17:51 -07:00

69 lines
2.7 KiB
Swift

// HardwareInputPassThrough.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Internal synthesizer that copies one hardware input bus to a board head bus.
///
/// When `ShoeGaze` is configured with `.liveInput`, a `HardwareInputPassThrough` is
/// added to the engine before the first pedal. It reads the hardware input bus at
/// `inputBusIndex` and writes its samples to the board's head bus at `outputBusIndex`.
/// This ensures the first pedal reads from a user-space bus that the board controls,
/// rather than from the raw hardware input bus directly.
///
/// - Note: The synthesizer must be removed from the engine before the board's
/// `teardown()` releases the head bus back to the allocator.
final class HardwareInputPassThrough: SynthesizerBase {
// MARK: - Parameters
/// Input mute — `0` = audible, `1` = muted (a hard gate on the incoming signal, distinct from
/// the rig-output mute). Driven across every board input node by `ShoeGaze.setInputMuted(_:)`,
/// which sets a newly-added node to the session's current mute state. Defaults audible so
/// direct users (e.g. `ChainRouter`) that don't drive the mute pass the signal through.
@UIParam var inputMute: Double = 0.0
/// The live `UIParamNode` backing `inputMute`, driven by `ShoeGaze.setInputMuted(_:)`.
var inputMuteNode: UIParamNode { $inputMute }
// MARK: - Routing
/// Index of the hardware input bus to read from (typically `engine.numOutputChannels`).
private let inputBusIndex: Int
/// 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`.
///
/// - Parameters:
/// - inputBusIndex: Hardware input bus index to read from.
/// Typically `engine.numOutputChannels` for the first input channel.
/// - outputBusIndex: Board head bus index to write to.
init(inputBusIndex: Int, outputBusIndex: Int) {
self.inputBusIndex = inputBusIndex
self.outputBusIndex = outputBusIndex
super.init()
}
// 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
output(bus: outputBusIndex, source: input(bus: inputBusIndex) * muteGain)
}
}