// MIDIMap.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT /// A per-rig map of MIDI foot-controller bindings — which Program Change toggles which control, and /// which Control Change drives which pedal knob. /// /// Stored inside the rig (embedded in `ShoeGazeState`) so it travels with a saved rig. Controls are /// addressed **positionally** (board index, pedal index, knob index) since boards and pedals have no /// stable identity; the `*Hint` fields carry the resolved display name at learn time so a binding /// that no longer matches (chain edited) can be detected and skipped rather than mis-fire. public struct MIDIMap: Codable, Equatable, Sendable { /// Program-Change → action bindings (footswitch toggles). Populated in the switch phase. public var switches: [SwitchBinding] /// Control-Change → pedal-knob bindings (expression pedals). public var ccs: [CCBinding] /// An empty map (no bindings). public static let empty: MIDIMap = MIDIMap() /// Creates a MIDI map. /// /// - Parameters: /// - switches: Program-Change bindings. Defaults to empty. /// - ccs: Control-Change bindings. Defaults to empty. public init( switches: [SwitchBinding] = [], ccs: [CCBinding] = [] ) { self.switches = switches self.ccs = ccs } // MARK: - Action /// A toggle-style control a footswitch (Program Change) can drive. Program Change is momentary, /// so boolean actions latch in software; `boardSolo` is exclusive (radio). public enum Action: Codable, Equatable, Sendable { case rigBypass case boardSolo(boardIndex: Int) case boardAudible(boardIndex: Int) case boardBypass(boardIndex: Int) case pedalBypass(boardIndex: Int, pedalIndex: Int) } // MARK: - Switch binding /// A Program-Change number bound to a toggle ``Action``. public struct SwitchBinding: Codable, Equatable, Sendable { /// The Program Change number (0–127) that triggers the action. public var program: UInt8 /// The MIDI channel (0–15), or `nil` to match any channel (omni). public var channel: UInt8? /// The control to toggle. public var action: Action /// The resolved control's display name at learn time, for stale-binding detection. public var displayNameHint: String? /// Creates a switch binding. /// /// - Parameters: /// - program: Program Change number (0–127). /// - channel: MIDI channel (0–15), or `nil` for omni. Defaults to `nil`. /// - action: The toggle action to perform. /// - displayNameHint: Resolved control name at learn time. Defaults to `nil`. public init( program: UInt8, channel: UInt8? = nil, action: Action, displayNameHint: String? = nil ) { self.program = program self.channel = channel self.action = action self.displayNameHint = displayNameHint } } // MARK: - CC target /// Addresses a single pedal knob to be driven by an expression pedal. `knobIndex` indexes into /// the pedal's `knobs` list (effect knobs, then Mix and Level). public struct CCTarget: Codable, Equatable, Sendable { /// Index of the board in `ShoeGaze.boards`. public var boardIndex: Int /// Index of the pedal in `board.pedals`. public var pedalIndex: Int /// Index of the knob in `pedal.knobs`. public var knobIndex: Int /// The pedal's `displayName` at learn time, for stale-binding detection. public var pedalNameHint: String? /// The knob's label at learn time, for stale-binding detection. public var knobLabelHint: String? /// Creates a CC target. /// /// - Parameters: /// - boardIndex: Index of the board in `ShoeGaze.boards`. /// - pedalIndex: Index of the pedal in `board.pedals`. /// - knobIndex: Index of the knob in `pedal.knobs`. /// - pedalNameHint: Pedal display name at learn time. Defaults to `nil`. /// - knobLabelHint: Knob label at learn time. Defaults to `nil`. public init( boardIndex: Int, pedalIndex: Int, knobIndex: Int, pedalNameHint: String? = nil, knobLabelHint: String? = nil ) { self.boardIndex = boardIndex self.pedalIndex = pedalIndex self.knobIndex = knobIndex self.pedalNameHint = pedalNameHint self.knobLabelHint = knobLabelHint } } // MARK: - CC binding /// A Control-Change number bound to a pedal knob. The incoming value (0–127) is mapped across /// the knob's own range; `inverted` swaps heel/toe for reversed expression pedals. public struct CCBinding: Codable, Equatable, Sendable { /// The Control Change number (0–127). public var controller: UInt8 /// The MIDI channel (0–15), or `nil` to match any channel (omni). public var channel: UInt8? /// The pedal knob this CC drives. public var target: CCTarget /// When `true`, CC 0 maps to the knob's maximum and 127 to its minimum. public var inverted: Bool /// Creates a CC binding. /// /// - Parameters: /// - controller: Control Change number (0–127). /// - channel: MIDI channel (0–15), or `nil` for omni. Defaults to `nil`. /// - target: The pedal knob to drive. /// - inverted: Reverse the sweep direction. Defaults to `false`. public init( controller: UInt8, channel: UInt8? = nil, target: CCTarget, inverted: Bool = false ) { self.controller = controller self.channel = channel self.target = target self.inverted = inverted } } }