Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift
T
josh 1893576718 Add MIDI foot-controller control; board/rig bypass; iOS fixes
Headline: MIDI control for a Behringer FCB1010 (four phases).
- MIDIController (Lyrebird MIDI client): connection indicator via source
  polling, background-queue->main-actor hop, MIDI Learn.
- Volume and Wah pedals (Wah reuses the SVF bandpass sweep, driven by
  pedal position instead of an envelope follower).
- CC -> any pedal knob (0-127 mapped across the knob range).
- Program Change -> pedal/board/rig toggles: software latch for booleans,
  exclusive radio for board solo. Positional addressing with display-name
  hints; stale/out-of-range bindings are skipped, not fired.
- Per-rig MIDIMap embedded in ShoeGazeState as an optional field (old rigs
  decode to nil; no preset version bump). Central MIDI Mapping sheet with
  Learn/Clear per control.

Also included (same working branch):
- Full board bypass and rig bypass (dry passthrough via cascaded
  crossfades in BoardOutputMaster), with UI toggles.
- Horizontal pedal picker grouped by category in filter order.
- Drag a palette pedal onto a chain card to insert before it
  (ShoeGaze.insertPedal); tap still appends.
- iOS: guard macOS-only .menuStyle(.borderlessButton) to silence the
  _UIReparentingView warning; iCloud-unavailable alert; keep the device
  awake while audio runs.

Tests: 24 added (Volume/Wah, MIDIMap Codable incl. legacy decode,
CC and PC controller behavior, insert/bypass API). Full suite green;
macOS and iOS app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 08:18:09 -07:00

218 lines
8.1 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` / `pedal.knobs`, so bindings address controls positionally.
/// (The Program-Change switch section is added in a later phase.)
public struct MIDIMappingSheet: View {
// MARK: - Constants
private static let sheetMinWidth: CGFloat = 460
private static let sheetMinHeight: CGFloat = 520
private static let rowVerticalPadding: CGFloat = 2
// 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 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: - Switches (Program Change)
private var switchesSection: some View {
Section("Switches (Program Change)") {
switchRow(action: .rigBypass, label: "Rig Bypass")
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { boardPair in
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
switchRow(action: .boardSolo(boardIndex: boardIndex), label: "\(board.label) · Solo")
switchRow(action: .boardAudible(boardIndex: boardIndex), label: "\(board.label) · Audible")
switchRow(action: .boardBypass(boardIndex: boardIndex), label: "\(board.label) · Bypass")
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pedalPair in
switchRow(
action: .pedalBypass(boardIndex: boardIndex, pedalIndex: pedalPair.offset),
label: "\(board.label) · \(pedalPair.element.displayName) · Bypass"
)
}
}
}
}
private func switchRow(action: MIDIMap.Action, label: String) -> some View {
let isArmed: Bool = controller.learnTarget == .action(action)
let binding: MIDIMap.SwitchBinding? = controller.switchBinding(for: action)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text(label)
Text(switchBindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)
}
Spacer()
if binding != nil {
Button("Clear") { controller.clearSwitchBinding(for: action) }
.buttonStyle(.borderless)
}
Button(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armSwitchLearn(action)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func switchBindingLabel(isArmed: Bool, binding: MIDIMap.SwitchBinding?) -> String {
if isArmed { return "Listening — press a footswitch…" }
guard let binding: MIDIMap.SwitchBinding = binding else { return "Unassigned" }
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
return "PC \(binding.program)\(channelText)"
}
// 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(Array(shoeGaze.boards.enumerated()), id: \.offset) { boardPair in
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pedalPair in
pedalRows(boardIndex: boardIndex, pedalIndex: pedalPair.offset, pedal: pedalPair.element)
}
}
}
}
@ViewBuilder
private func pedalRows(boardIndex: Int, pedalIndex: Int, pedal: Pedal) -> some View {
ForEach(Array(pedal.knobs.enumerated()), id: \.offset) { knobPair in
knobRow(
boardIndex: boardIndex,
pedalIndex: pedalIndex,
knobIndex: knobPair.offset,
pedalName: pedal.displayName,
knobLabel: knobPair.element.label
)
}
}
private func knobRow(
boardIndex: Int,
pedalIndex: Int,
knobIndex: Int,
pedalName: String,
knobLabel: String
) -> some View {
let isArmed: Bool = controller.learnTarget == .cc(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
)
let binding: MIDIMap.CCBinding? = controller.ccBinding(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text("\(pedalName) · \(knobLabel)")
Text(bindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)
}
Spacer()
if binding != nil {
Button("Clear") {
controller.clearCCBinding(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
)
}
.buttonStyle(.borderless)
}
Button(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armCCLearn(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func bindingLabel(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)" } ?? ""
return "CC \(binding.controller)\(channelText)"
}
}