Add rig-wide output volume + hard mute, MIDI-mappable

Volume is a CC-mappable [0,1] gain applied after every board's output; mute
is a separate hard gate (not the existing dry bypass), togglable via
Program Change. Both are lagged to avoid clicks and fan out across all
boards from a single ShoeGaze-level setter.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 19:28:09 -07:00
parent d4664b3c1f
commit 75edf44b64
10 changed files with 230 additions and 11 deletions
@@ -52,6 +52,20 @@ final class BoardOutputMaster: SynthesizerBase {
/// The live `UIParamNode` backing `rigBypass` (rig-wide full bypass).
var rigBypassNode: UIParamNode { $rigBypass }
/// Rig-wide output volume in [0, 1], applied to the final output. Driven across every board by
/// `ShoeGaze.setRigVolume(_:)`. Defaults to `1` (unity).
@UIParam var rigVolume: Double = 1.0
/// Rig-wide output mute — `0` = audible, `1` = silenced (a hard gate, not a dry bypass). Driven
/// across every board by `ShoeGaze.setRigMuted(_:)`. Defaults to `0`.
@UIParam var rigMute: Double = 0.0
/// The live `UIParamNode` backing `rigVolume` (rig-wide output volume).
var rigVolumeNode: UIParamNode { $rigVolume }
/// The live `UIParamNode` backing `rigMute` (rig-wide output mute).
var rigMuteNode: UIParamNode { $rigMute }
// MARK: - Routing
/// Left / mono board output bus index.
@@ -102,19 +116,24 @@ final class BoardOutputMaster: SynthesizerBase {
// seconds rather than clicking. The user master-gain knob is applied directly.
let smoothedSelect: Lag.Control = Lag.Control(input: $selectGain, time: $crossfadeTime)
// Rig-wide output volume × mute, shared across both channels (like `smoothedSelect`).
let rigOutputGain: any ValidGeneratorInput = rigOutputGainStage()
// The dry input tap — read once, used as the bypass source for both channels (the head
// bus is mono, so a bypassed stereo board passes the same dry signal to both sides).
let dry: In.Audio = input(bus: dryInputBusIndex)
let boardOutLeft: In.Audio = input(bus: boardOutputBusIndex)
let switchedLeft: any ValidGeneratorInput = applyBypass(wet: boardOutLeft, dry: dry)
let gainedLeft: any ValidGeneratorInput = (switchedLeft * smoothedSelect) * $masterGain
let gainedLeft: any ValidGeneratorInput =
((switchedLeft * smoothedSelect) * $masterGain) * rigOutputGain
if let rightIndex: Int = boardOutputBusRightIndex {
// Stereo board: left tail → hardware 0, right tail → hardware 1, no pan spread.
let boardOutRight: In.Audio = input(bus: rightIndex)
let switchedRight: any ValidGeneratorInput = applyBypass(wet: boardOutRight, dry: dry)
let gainedRight: any ValidGeneratorInput = (switchedRight * smoothedSelect) * $masterGain
let gainedRight: any ValidGeneratorInput =
((switchedRight * smoothedSelect) * $masterGain) * rigOutputGain
output(bus: 0, source: gainedLeft)
output(bus: 1, source: gainedRight)
} else {
@@ -123,6 +142,16 @@ final class BoardOutputMaster: SynthesizerBase {
}
}
/// Rig-wide output gain: the smoothed rig volume gated by the smoothed mute. Mute `0` → gain 1
/// (audible), mute `1` → gain 0 (silent). Both lagged to avoid clicks.
private func rigOutputGainStage() -> any ValidGeneratorInput {
let rigVol: Lag.Control = Lag.Control(input: $rigVolume, time: bypassLagTime)
let muteLagged: Lag.Control = Lag.Control(input: $rigMute, time: bypassLagTime)
// (mute − 1) × −1 ⇒ 0→1, 1→0, using only generator−scalar and generator×scalar.
let muteGain: any ValidGeneratorInput = (muteLagged - 1.0) * -1.0
return rigVol * muteGain
}
/// Routes `wet` to `dry` when either the per-board or rig-wide bypass is engaged. Two cascaded
/// equal-power crossfades (each lagged to prevent clicks) give an independent "OR" of the two
/// bypass sources: rig bypass overrides even a board that is otherwise engaged.
@@ -207,19 +207,28 @@ public final class MIDIController {
/// Captures a learned CC binding for the armed control and clears the Learn state.
private func captureCC(controller: UInt8, channel: UInt8, target: MIDIMap.CCTarget) {
learnTarget = nil
guard let shoeGaze: ShoeGaze = shoeGaze,
let resolved: ResolvedCC = resolveCC(target) else { return }
guard let shoeGaze: ShoeGaze = shoeGaze else { return }
// `rigVolume` is applied via a setter (it has no single node) and is always valid, so it
// needs no resolve; pedal/board targets resolve to re-stamp their display-name hints.
let hinted: MIDIMap.CCTarget
if case .rigVolume = target {
hinted = .rigVolume
} else {
guard let resolved: ResolvedCC = resolveCC(target) else { return }
hinted = resolved.hinted
}
let binding: MIDIMap.CCBinding = MIDIMap.CCBinding(
controller: controller,
channel: channel,
target: resolved.hinted
target: hinted
)
var map: MIDIMap = shoeGaze.midiMap ?? MIDIMap.empty
// One binding per control — re-learning a control replaces its binding. A single CC may
// drive MULTIPLE controls on purpose (e.g. map one expression pedal to two board levels and
// invert one for a crossfade), so we deliberately do NOT steal by controller number.
map.ccs.removeAll { $0.target.addresses(resolved.hinted) }
map.ccs.removeAll { $0.target.addresses(hinted) }
map.ccs.append(binding)
shoeGaze.midiMap = map
onMapChanged?()
@@ -230,10 +239,15 @@ public final class MIDIController {
guard let shoeGaze: ShoeGaze = shoeGaze, let map: MIDIMap = shoeGaze.midiMap else { return }
for binding: MIDIMap.CCBinding in map.ccs where binding.controller == controller {
if let boundChannel: UInt8 = binding.channel, boundChannel != channel { continue }
guard let resolved: ResolvedCC = resolveCC(binding.target) else { continue }
let ccMax: Double = 127.0
var norm: Double = Double(value) / ccMax
if binding.inverted { norm = 1.0 - norm }
if case .rigVolume = binding.target {
shoeGaze.setRigVolume(norm) // rig volume spans [0, 1] directly
continue
}
guard let resolved: ResolvedCC = resolveCC(binding.target) else { continue }
let mapped: Double = resolved.range.lowerBound
+ norm * (resolved.range.upperBound - resolved.range.lowerBound)
resolved.node.update(mapped)
@@ -273,6 +287,9 @@ public final class MIDIController {
if let labelHint: String = labelHint, labelHint != board.label { return nil }
let hinted: MIDIMap.CCTarget = .boardLevel(boardIndex: boardIndex, boardLabelHint: board.label)
return ResolvedCC(node: node, range: boardLevelRange, hinted: hinted)
case .rigVolume:
// Applied via `ShoeGaze.setRigVolume` (fans out to every board), not a single node.
return nil
}
}
@@ -335,6 +352,8 @@ public final class MIDIController {
switch action {
case .rigBypass:
shoeGaze.toggleRigBypassed()
case .rigMute:
shoeGaze.toggleRigMuted()
case .allBoards:
// One-shot: cancel any solo and bring every board back up.
shoeGaze.selectAllBoards(crossfadeSeconds: boardActionCrossfade)
@@ -360,7 +379,7 @@ public final class MIDIController {
/// A display-name hint for an action, captured at learn time for stale-binding detection.
private func hint(for action: MIDIMap.Action) -> String? {
switch action {
case .rigBypass, .allBoards:
case .rigBypass, .rigMute, .allBoards:
return nil
case .boardSolo(let index), .boardAudible(let index), .boardBypass(let index):
return board(at: index)?.label
@@ -41,6 +41,8 @@ public struct MIDIMap: Codable, Equatable, Sendable {
/// so boolean actions latch in software; `boardSolo` is exclusive (radio).
public enum Action: Codable, Equatable, Sendable {
case rigBypass
/// Mutes/unmutes the rig output (a hard silence, not a bypass).
case rigMute
/// Cancels any solo and makes every board audible again (the "All" button).
case allBoards
case boardSolo(boardIndex: Int)
@@ -106,6 +108,9 @@ public struct MIDIMap: Codable, Equatable, Sendable {
/// A board's master output level (the board fader).
case boardLevel(boardIndex: Int, boardLabelHint: String?)
/// The rig-wide output volume (applies to the whole rig).
case rigVolume
/// Whether two targets address the same control, ignoring the display-name hints (used to
/// find and replace a binding regardless of the name captured at learn time).
public func addresses(_ other: CCTarget) -> Bool {
@@ -114,6 +119,8 @@ public struct MIDIMap: Codable, Equatable, Sendable {
return b1 == b2 && p1 == p2 && c1 == c2
case let (.boardLevel(b1, _), .boardLevel(b2, _)):
return b1 == b2
case (.rigVolume, .rigVolume):
return true
default:
return false
}
@@ -113,6 +113,14 @@ public final class PedalBoard {
/// driven across every board by `ShoeGaze.setRigBypassed(_:)`. `nil` until `finalizeBoard(_:)`.
public internal(set) var rigBypassNode: UIParamNode?
/// The live rig-wide output-volume `UIParamNode` ([0, 1]), driven across every board by
/// `ShoeGaze.setRigVolume(_:)`. `nil` until `finalizeBoard(_:)`.
public internal(set) var rigVolumeNode: UIParamNode?
/// The live rig-wide output-mute `UIParamNode` (`0` = audible, `1` = silenced), driven across
/// every board by `ShoeGaze.setRigMuted(_:)`. `nil` until `finalizeBoard(_:)`.
public internal(set) var rigMuteNode: UIParamNode?
/// The output master synth for this board. Set by `ShoeGaze.finalizeBoard(_:)`;
/// used by `ShoeGaze.removeBoard(_:)` to tear down just this board's master.
internal var outputMaster: BoardOutputMaster?
+40
View File
@@ -270,6 +270,8 @@ public final class ShoeGaze {
board.crossfadeTimeNode = master.crossfadeTimeNode
board.boardBypassNode = master.boardBypassNode
board.rigBypassNode = master.rigBypassNode
board.rigVolumeNode = master.rigVolumeNode
board.rigMuteNode = master.rigMuteNode
// If a board is currently soloed, a newly-finalized board must come up silent
// (select-gain 0) so it doesn't leak audio past the active solo.
@@ -284,6 +286,9 @@ public final class ShoeGaze {
if rigBypassed {
master.rigBypassNode.update(1.0)
}
// Bring a newly-finalized board up at the current rig volume / mute.
master.rigVolumeNode.update(rigVolume)
master.rigMuteNode.update(rigMuted ? 1.0 : 0.0)
}
// MARK: - Board selection / crossfade
@@ -296,6 +301,13 @@ public final class ShoeGaze {
/// A live performance toggle (not persisted); cleared on input-source / engine rebuild.
public private(set) var rigBypassed: Bool = false
/// Rig-wide output volume in [0, 1], applied to every board's output. Survives an input-source
/// rebuild (re-applied to each rebuilt board in `finalizeBoard`).
public private(set) var rigVolume: Double = 1.0
/// `true` when the rig output is muted (hard-silenced, not bypassed). Survives a rebuild.
public private(set) var rigMuted: 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).
public private(set) var bypassedBoards: Set<ObjectIdentifier> = []
@@ -420,6 +432,34 @@ public final class ShoeGaze {
setRigBypassed(!rigBypassed)
}
/// Sets the rig-wide output volume (clamped to [0, 1]), applied to every board. Smoothed per
/// board to avoid clicks.
///
/// - Parameter volume: Output volume in [0, 1].
public func setRigVolume(_ volume: Double) {
let clamped: Double = min(max(volume, 0.0), 1.0)
for managed: PedalBoard in boards {
managed.rigVolumeNode?.update(clamped)
}
rigVolume = clamped
}
/// Mutes or un-mutes the rig output — a hard gate to silence, distinct from the dry-passthrough
/// ``setRigBypassed(_:)``. Applied to every board, smoothed to avoid clicks.
///
/// - Parameter muted: `true` to silence the output, `false` to restore it.
public func setRigMuted(_ muted: Bool) {
for managed: PedalBoard in boards {
managed.rigMuteNode?.update(muted ? 1.0 : 0.0)
}
rigMuted = muted
}
/// Toggles the rig output mute. See ``setRigMuted(_:)``.
public func toggleRigMuted() {
setRigMuted(!rigMuted)
}
/// 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.
///
@@ -151,10 +151,38 @@ public struct BoardBarView: View {
.monospacedDigit()
.foregroundStyle(Color.white.opacity(BoardBarView.labelOpacity))
}
rigVolumeRow
}
.frame(width: BoardBarView.controlColumnWidth)
}
/// Rig-wide output volume + mute (applies to the whole rig, after every board).
private var rigVolumeRow: some View {
HStack(spacing: BoardBarView.cellInnerSpacing) {
Text("Vol")
.font(.caption2)
.foregroundStyle(Color.white.opacity(BoardBarView.labelOpacity))
Slider(
value: Binding(
get: { shoeGaze.rigVolume },
set: { shoeGaze.setRigVolume($0) }
),
in: 0.0 ... 1.0
)
.frame(minWidth: BoardBarView.crossfadeSliderMinWidth)
.disabled(shoeGaze.rigMuted)
Button {
shoeGaze.toggleRigMuted()
} label: {
Image(systemName: shoeGaze.rigMuted ? "speaker.slash.fill" : "speaker.wave.2.fill")
}
.buttonStyle(.bordered)
.tint(shoeGaze.rigMuted ? .red : .orange)
.help(shoeGaze.rigMuted ? "Unmute the rig output" : "Mute the rig output")
}
}
// MARK: - Board cell
@ViewBuilder
@@ -99,6 +99,7 @@ public struct MIDIMappingSheet: View {
private var switchRowModels: [SwitchRowModel] {
var rows: [SwitchRowModel] = [
SwitchRowModel(id: "sw-rig", action: .rigBypass, label: "Rig Bypass"),
SwitchRowModel(id: "sw-mute", action: .rigMute, label: "Rig Mute"),
SwitchRowModel(id: "sw-all", action: .allBoards, label: "All Boards On"),
]
for boardPair: (offset: Int, element: PedalBoard) in shoeGaze.boards.enumerated() {
@@ -124,7 +125,9 @@ public struct MIDIMappingSheet: View {
}
private var ccRowModels: [CCRowModel] {
var rows: [CCRowModel] = []
var rows: [CCRowModel] = [
CCRowModel(id: "cc-rigvol", target: .rigVolume, label: "Rig · Volume"),
]
for boardPair: (offset: Int, element: PedalBoard) in shoeGaze.boards.enumerated() {
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
@@ -17,10 +17,11 @@ struct BoardEditAPITests {
/// Builds an NRT-backed `ShoeGaze` with a single finalized board of the given pedal types.
private func makeRig(
pedals: [String],
numAudioBuses: Int = 128
numAudioBuses: Int = 128,
duration: Double = 0.1
) throws -> (ShoeGaze, SynthesisEngine, URL, PedalBoard) {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.1,
duration: duration,
numAudioBuses: numAudioBuses
)
let sg: ShoeGaze = ShoeGaze(engine: engine)
@@ -281,6 +282,57 @@ struct BoardEditAPITests {
#expect(board2.rigBypassNode?.value == 1.0, "new board comes up rig-bypassed")
}
@Test func rigVolumeClampsAndAppliesToAllBoards() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])
defer { try? FileManager.default.removeItem(at: url) }
#expect(sg.rigVolume == 1.0, "rig volume starts at unity")
sg.setRigVolume(0.5)
#expect(sg.rigVolume == 0.5)
#expect(board.rigVolumeNode?.value == 0.5)
sg.setRigVolume(2.0)
#expect(sg.rigVolume == 1.0, "rig volume clamps to 1")
sg.setRigVolume(-1.0)
#expect(sg.rigVolume == 0.0, "rig volume clamps to 0")
}
@Test func rigMuteSilencesOutputWithoutBypassing() throws {
// Mute is smoothed by a short `Lag` to avoid a click, so the tail (well past the lag time)
// must settle silent — the render must be long enough for that lag to fully resolve.
let (sg, engine, url, _): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"], duration: 1.0)
defer { try? FileManager.default.removeItem(at: url) }
#expect(!sg.rigMuted, "rig starts unmuted")
sg.toggleRigMuted()
#expect(sg.rigMuted, "rig reports muted after toggle")
#expect(!sg.rigBypassed, "mute must not engage the dry bypass")
let samples: [Float] = try render(engine, url)
let tail: [Float] = Array(samples.suffix(samples.count / 4))
#expect(!hasNonSilentOutput(tail), "muted rig should settle to silence once the lag resolves")
sg.setRigMuted(false)
#expect(!sg.rigMuted, "rig should unmute")
}
@Test func boardFinalizedWhileRigVolumeAndMuteSetComesUpMatching() throws {
let (sg, _, url, _): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])
defer { try? FileManager.default.removeItem(at: url) }
sg.setRigVolume(0.25)
sg.setRigMuted(true)
let board2: PedalBoard = try sg.addBoard()
try board2.add(DelayPedal())
try sg.finalizeBoard(board2)
#expect(board2.rigVolumeNode?.value == 0.25, "new board picks up current rig volume")
#expect(board2.rigMuteNode?.value == 1.0, "new board picks up current rig mute")
}
@Test func rigRebuildClearsTransientBypass() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive", "Delay"], numAudioBuses: 256)
@@ -165,6 +165,22 @@ struct MIDIControllerCCTests {
}
}
@Test func ccCanTargetRigVolume() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armCCLearn(.rigVolume)
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 0)) // learn
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 127))
#expect(abs(sg.rigVolume - 1.0) < 1e-9, "CC 127 should drive rig volume to max")
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 0))
#expect(abs(sg.rigVolume - 0.0) < 1e-9, "CC 0 should drive rig volume to min")
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 64))
#expect(abs(sg.rigVolume - 64.0 / 127.0) < 1e-9, "CC 64 should map mid-range")
}
}
@Test func unboundCCIsIgnored() throws {
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
@@ -63,6 +63,23 @@ struct MIDIControllerPCTests {
}
}
@Test func rigMuteLatchesOnEachPressAndIsSeparateFromBypass() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armSwitchLearn(.rigMute)
controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn only
#expect(sg.rigMuted == false, "learn must not toggle")
controller.ingestForTesting(.programChange(channel: 0, program: 1))
#expect(sg.rigMuted == true, "first press mutes")
#expect(sg.rigBypassed == false, "mute must not engage bypass")
controller.ingestForTesting(.programChange(channel: 0, program: 1))
#expect(sg.rigMuted == false, "second press unmutes")
}
}
@Test func pedalBypassLatches() throws {
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }