5987ec7b78
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>
319 lines
13 KiB
Swift
319 lines
13 KiB
Swift
// MIDIMappingSheet.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import SwiftUI
|
|
import LyrebirdShoeGaze
|
|
import LyrebirdUI
|
|
|
|
/// The MIDI mapping panel: shows connected devices and lets the user bind expression-pedal CCs to
|
|
/// pedal knobs via MIDI Learn.
|
|
///
|
|
/// Presented from the transport-row ``MIDIIndicatorView``. Rows are generated by walking the live
|
|
/// `ShoeGaze.boards` / `board.pedals` and each pedal's controls, so bindings address controls
|
|
/// positionally. A Switches section binds Program Change to toggle actions; an Expression section
|
|
/// binds Control Change to continuous pedal controls.
|
|
public struct MIDIMappingSheet: View {
|
|
|
|
// MARK: - Constants
|
|
|
|
private static let sheetMinWidth: CGFloat = 460
|
|
private static let sheetMinHeight: CGFloat = 520
|
|
private static let rowVerticalPadding: CGFloat = 2
|
|
private static let switchRowControlSpacing: CGFloat = 8
|
|
|
|
// MARK: - Inputs
|
|
|
|
private let controller: MIDIController
|
|
private let shoeGaze: ShoeGaze
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
/// Creates a MIDI mapping sheet.
|
|
///
|
|
/// - Parameters:
|
|
/// - controller: The MIDI controller that owns the connection, learn state, and bindings.
|
|
/// - shoeGaze: The rig whose boards / pedals / knobs are enumerated as binding targets.
|
|
public init(controller: MIDIController, shoeGaze: ShoeGaze) {
|
|
self.controller = controller
|
|
self.shoeGaze = shoeGaze
|
|
}
|
|
|
|
// MARK: - Body
|
|
|
|
public var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
devicesSection
|
|
switchesSection
|
|
expressionSection
|
|
}
|
|
.navigationTitle("MIDI / Control Mapping")
|
|
.toolbar {
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("Done") {
|
|
controller.cancelLearn()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.frame(minWidth: MIDIMappingSheet.sheetMinWidth, minHeight: MIDIMappingSheet.sheetMinHeight)
|
|
}
|
|
|
|
// MARK: - Devices
|
|
|
|
private var devicesSection: some View {
|
|
Section("Devices") {
|
|
if controller.isConnected {
|
|
ForEach(controller.connectedDeviceNames, id: \.self) { name in
|
|
Label(name, systemImage: "pianokeys")
|
|
.foregroundStyle(Color.green)
|
|
}
|
|
} else {
|
|
Label("No MIDI device detected", systemImage: "pianokeys.inverse")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Row models
|
|
|
|
/// A Program-Change switch row: a toggle action + its display label. `id` is globally unique so
|
|
/// SwiftUI never reuses a row view across the two sections (which would leak the wrong Learn
|
|
/// action into a row).
|
|
private struct SwitchRowModel: Identifiable {
|
|
let id: String
|
|
let action: MIDIMap.Action
|
|
let label: String
|
|
}
|
|
|
|
/// A Control-Change row: a continuous target (address-only, hints `nil`) + its display label.
|
|
private struct CCRowModel: Identifiable {
|
|
let id: String
|
|
let target: MIDIMap.CCTarget
|
|
let label: String
|
|
}
|
|
|
|
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-input-mute", action: .inputMute, label: "Input Mute"),
|
|
SwitchRowModel(id: "sw-all", action: .allBoards, label: "All Boards On"),
|
|
]
|
|
for board: PedalBoard in shoeGaze.boards {
|
|
let boardID: UUID = board.id
|
|
rows.append(SwitchRowModel(id: "sw-solo-\(boardID)",
|
|
action: .boardSolo(boardID: boardID),
|
|
label: "\(board.label) · Solo"))
|
|
rows.append(SwitchRowModel(id: "sw-aud-\(boardID)",
|
|
action: .boardAudible(boardID: boardID),
|
|
label: "\(board.label) · Audible"))
|
|
rows.append(SwitchRowModel(id: "sw-byp-\(boardID)",
|
|
action: .boardBypass(boardID: boardID),
|
|
label: "\(board.label) · Bypass"))
|
|
for pedal: Pedal in board.pedals {
|
|
rows.append(SwitchRowModel(
|
|
id: "sw-pb-\(pedal.id)",
|
|
action: .pedalBypass(pedalID: pedal.id),
|
|
label: "\(board.label) · \(pedal.displayName) · Bypass"))
|
|
// Loopers expose their transport footswitches as mappable switch actions.
|
|
if pedal is LooperPedal {
|
|
var looperActions: [(String, String, MIDIMap.PedalActionKind)] = [
|
|
("rec", "Record", .looperRecordStop),
|
|
("stop", "Stop", .looperStop),
|
|
("cue", "Cue Stop", .looperCueStop),
|
|
("rev", "Reverse", .looperReverse),
|
|
("sync", "Sync", .looperSyncToggle),
|
|
("reset", "Reset", .looperReset),
|
|
]
|
|
// A/B/C/D track taps (select / mute), matching the timeline dots.
|
|
for i: Int in 0 ..< LooperPedal.trackCount {
|
|
let letter: String = LooperPedal.trackLabel(i)
|
|
looperActions.append(("track\(letter)", "Track \(letter)", .looperTrack(index: i)))
|
|
}
|
|
for (slug, name, kind): (String, String, MIDIMap.PedalActionKind) in looperActions {
|
|
rows.append(SwitchRowModel(
|
|
id: "sw-la-\(slug)-\(pedal.id)",
|
|
action: .pedalAction(pedalID: pedal.id, kind: kind),
|
|
label: "\(board.label) · \(pedal.displayName) · \(name)"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return rows
|
|
}
|
|
|
|
private var ccRowModels: [CCRowModel] {
|
|
var rows: [CCRowModel] = [
|
|
CCRowModel(id: "cc-rigvol", target: .rigVolume, label: "Rig · Volume"),
|
|
]
|
|
for board: PedalBoard in shoeGaze.boards {
|
|
// Board master level (the board fader).
|
|
rows.append(CCRowModel(
|
|
id: "cc-blvl-\(board.id)",
|
|
target: .boardLevel(boardID: board.id, boardLabelHint: nil),
|
|
label: "\(board.label) · Level"))
|
|
for pedal: Pedal in board.pedals {
|
|
for controlPair: (offset: Int, element: PedalControl) in pedal.ccMappableControls.enumerated() {
|
|
rows.append(CCRowModel(
|
|
id: "cc-\(pedal.id)-\(controlPair.offset)",
|
|
target: .pedalControl(
|
|
pedalID: pedal.id,
|
|
controlIndex: controlPair.offset,
|
|
pedalNameHint: nil,
|
|
controlLabelHint: nil),
|
|
label: "\(pedal.displayName) · \(controlPair.element.label)"))
|
|
}
|
|
}
|
|
}
|
|
return rows
|
|
}
|
|
|
|
// MARK: - Switches (Program Change)
|
|
|
|
private var switchesSection: some View {
|
|
Section("Switches (footswitch / key)") {
|
|
ForEach(switchRowModels) { row in
|
|
switchRow(row)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A switch row carries two independent trigger sources for one toggle action: a MIDI Program
|
|
/// Change (footswitch) and a computer key. Each has its own learn / clear; the detail line
|
|
/// summarises whichever are bound, or the "Listening…" prompt for the armed source.
|
|
private func switchRow(_ row: SwitchRowModel) -> some View {
|
|
let isArmedPC: Bool = controller.learnTarget == .action(row.action)
|
|
let isArmedKey: Bool = controller.learnTarget == .key(row.action)
|
|
let pcBinding: MIDIMap.SwitchBinding? = controller.switchBinding(for: row.action)
|
|
let keyBinding: MIDIMap.KeyBinding? = controller.keyBinding(for: row.action)
|
|
let isArmed: Bool = isArmedPC || isArmedKey
|
|
return HStack(spacing: MIDIMappingSheet.switchRowControlSpacing) {
|
|
VStack(alignment: .leading, spacing: 1) {
|
|
Text(row.label)
|
|
Text(switchBindingLabel(isArmedPC: isArmedPC, isArmedKey: isArmedKey,
|
|
pc: pcBinding, key: keyBinding))
|
|
.font(.caption)
|
|
.foregroundStyle(isArmed ? Color.orange : .secondary)
|
|
}
|
|
Spacer()
|
|
if pcBinding != nil {
|
|
Button("Clear PC") { controller.clearSwitchBinding(for: row.action) }
|
|
.buttonStyle(.borderless)
|
|
}
|
|
Button(isArmedPC ? "Cancel" : "PC") {
|
|
if isArmedPC {
|
|
controller.cancelLearn()
|
|
} else {
|
|
controller.armSwitchLearn(row.action)
|
|
}
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.tint(.orange)
|
|
if keyBinding != nil {
|
|
Button("Clear Key") { controller.clearKeyBinding(for: row.action) }
|
|
.buttonStyle(.borderless)
|
|
}
|
|
Button(isArmedKey ? "Cancel" : "Key") {
|
|
if isArmedKey {
|
|
controller.cancelLearn()
|
|
} else {
|
|
controller.armKeyLearn(row.action)
|
|
}
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.tint(.orange)
|
|
}
|
|
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
|
|
}
|
|
|
|
private func switchBindingLabel(
|
|
isArmedPC: Bool,
|
|
isArmedKey: Bool,
|
|
pc: MIDIMap.SwitchBinding?,
|
|
key: MIDIMap.KeyBinding?
|
|
) -> String {
|
|
if isArmedPC { return "Listening — press a footswitch…" }
|
|
if isArmedKey { return "Listening — press a key…" }
|
|
var parts: [String] = []
|
|
if let pc: MIDIMap.SwitchBinding = pc {
|
|
let channelText: String = pc.channel.map { " · ch \($0 + 1)" } ?? ""
|
|
parts.append("PC \(pc.program)\(channelText)")
|
|
}
|
|
if let key: MIDIMap.KeyBinding = key {
|
|
parts.append("Key \(keyLabel(key))")
|
|
}
|
|
return parts.isEmpty ? "Unassigned" : parts.joined(separator: " · ")
|
|
}
|
|
|
|
/// A key binding formatted with its modifier glyphs, e.g. `A`, `⇧A`, `⌃A` (macOS modifier order:
|
|
/// control, option, shift).
|
|
private func keyLabel(_ binding: MIDIMap.KeyBinding) -> String {
|
|
var prefix: String = ""
|
|
if binding.modifiers.contains(.control) { prefix += "⌃" }
|
|
if binding.modifiers.contains(.option) { prefix += "⌥" }
|
|
if binding.modifiers.contains(.shift) { prefix += "⇧" }
|
|
return prefix + binding.key.uppercased()
|
|
}
|
|
|
|
// MARK: - Expression (CC)
|
|
|
|
private var expressionSection: some View {
|
|
Section("Expression (Control Change)") {
|
|
if shoeGaze.boards.isEmpty {
|
|
Text("Add a pedal to assign expression control.")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
ForEach(ccRowModels) { row in
|
|
ccRow(row)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func ccRow(_ row: CCRowModel) -> some View {
|
|
let isArmed: Bool = controller.learnTarget == .cc(row.target)
|
|
let binding: MIDIMap.CCBinding? = controller.ccBinding(for: row.target)
|
|
return HStack {
|
|
VStack(alignment: .leading, spacing: 1) {
|
|
Text(row.label)
|
|
Text(ccBindingLabel(isArmed: isArmed, binding: binding))
|
|
.font(.caption)
|
|
.foregroundStyle(isArmed ? Color.orange : .secondary)
|
|
}
|
|
Spacer()
|
|
if binding != nil {
|
|
Toggle("Invert", isOn: Binding(
|
|
get: { controller.isCCInverted(for: row.target) },
|
|
set: { controller.setCCInverted(for: row.target, $0) }
|
|
))
|
|
.toggleStyle(.button)
|
|
.tint(.orange)
|
|
Button("Clear") { controller.clearCCBinding(for: row.target) }
|
|
.buttonStyle(.borderless)
|
|
}
|
|
Button(isArmed ? "Cancel" : "Learn") {
|
|
if isArmed {
|
|
controller.cancelLearn()
|
|
} else {
|
|
controller.armCCLearn(row.target)
|
|
}
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.tint(.orange)
|
|
}
|
|
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
|
|
}
|
|
|
|
private func ccBindingLabel(isArmed: Bool, binding: MIDIMap.CCBinding?) -> String {
|
|
if isArmed { return "Listening — move an expression pedal…" }
|
|
guard let binding: MIDIMap.CCBinding = binding else { return "Unassigned" }
|
|
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
|
|
let invertText: String = binding.inverted ? " · inverted" : ""
|
|
return "CC \(binding.controller)\(channelText)\(invertText)"
|
|
}
|
|
}
|