// LegacyMIDIMap.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation /// Decoder for the **pre-UUID** `MIDIMap` format, which addressed pedals and boards by chain /// position (`boardIndex` / `pedalIndex`) rather than by stable id. /// /// Used only on load: `ShoeGazeState.init(from:)` tries the current id-based `MIDIMap` first and, /// if that fails, decodes this legacy shape and calls ``migrated(boards:)`` to convert each /// positional binding to the id of the pedal/board now sitting at that position. Because the saved /// positions are stable, this reconstructs correct id-bindings on every load — even though the ids /// themselves are freshly assigned to a legacy rig each time it is decoded. /// /// Bindings whose indices fall outside the loaded rig are dropped. struct LegacyMIDIMap: Codable { var switches: [LegacySwitchBinding] var ccs: [LegacyCCBinding] enum LegacyAction: Codable { case rigBypass case rigMute case allBoards case boardSolo(boardIndex: Int) case boardAudible(boardIndex: Int) case boardBypass(boardIndex: Int) case pedalBypass(boardIndex: Int, pedalIndex: Int) case pedalAction(boardIndex: Int, pedalIndex: Int, kind: MIDIMap.PedalActionKind) case tunerToggle case tapTempo } enum LegacyCCTarget: Codable { case pedalControl( boardIndex: Int, pedalIndex: Int, controlIndex: Int, pedalNameHint: String?, controlLabelHint: String? ) case boardLevel(boardIndex: Int, boardLabelHint: String?) case rigVolume } struct LegacySwitchBinding: Codable { var program: UInt8 var channel: UInt8? var action: LegacyAction var displayNameHint: String? } struct LegacyCCBinding: Codable { var controller: UInt8 var channel: UInt8? var target: LegacyCCTarget var inverted: Bool } // MARK: - Migration /// Converts this positional map to the current id-based ``MIDIMap`` using the ids of the pedals /// and boards in `boards` (whose ids were freshly assigned when the rig was decoded). Bindings /// addressing an out-of-range board/pedal are dropped. func migrated(boards: [BoardState]) -> MIDIMap { func boardID(_ index: Int) -> UUID? { guard index >= 0, index < boards.count else { return nil } return boards[index].id } func pedalID(_ boardIndex: Int, _ pedalIndex: Int) -> UUID? { guard boardIndex >= 0, boardIndex < boards.count else { return nil } let pedals: [PedalState] = boards[boardIndex].pedals guard pedalIndex >= 0, pedalIndex < pedals.count else { return nil } return pedals[pedalIndex].id } let migratedSwitches: [MIDIMap.SwitchBinding] = switches.compactMap { binding in guard let action: MIDIMap.Action = migrate(binding.action, boardID: boardID, pedalID: pedalID) else { return nil } return MIDIMap.SwitchBinding( program: binding.program, channel: binding.channel, action: action, displayNameHint: binding.displayNameHint ) } let migratedCCs: [MIDIMap.CCBinding] = ccs.compactMap { binding in guard let target: MIDIMap.CCTarget = migrate(binding.target, boardID: boardID, pedalID: pedalID) else { return nil } return MIDIMap.CCBinding( controller: binding.controller, channel: binding.channel, target: target, inverted: binding.inverted ) } return MIDIMap(switches: migratedSwitches, ccs: migratedCCs) } private func migrate( _ action: LegacyAction, boardID: (Int) -> UUID?, pedalID: (Int, Int) -> UUID? ) -> MIDIMap.Action? { switch action { case .rigBypass: return .rigBypass case .rigMute: return .rigMute case .allBoards: return .allBoards case .tunerToggle: return .tunerToggle case .tapTempo: return .tapTempo case .boardSolo(let b): guard let id: UUID = boardID(b) else { return nil } return .boardSolo(boardID: id) case .boardAudible(let b): guard let id: UUID = boardID(b) else { return nil } return .boardAudible(boardID: id) case .boardBypass(let b): guard let id: UUID = boardID(b) else { return nil } return .boardBypass(boardID: id) case .pedalBypass(let b, let p): guard let id: UUID = pedalID(b, p) else { return nil } return .pedalBypass(pedalID: id) case .pedalAction(let b, let p, let kind): guard let id: UUID = pedalID(b, p) else { return nil } return .pedalAction(pedalID: id, kind: kind) } } private func migrate( _ target: LegacyCCTarget, boardID: (Int) -> UUID?, pedalID: (Int, Int) -> UUID? ) -> MIDIMap.CCTarget? { switch target { case .rigVolume: return .rigVolume case .pedalControl(let b, let p, let controlIndex, let nameHint, let labelHint): guard let id: UUID = pedalID(b, p) else { return nil } return .pedalControl(pedalID: id, controlIndex: controlIndex, pedalNameHint: nameHint, controlLabelHint: labelHint) case .boardLevel(let b, let labelHint): guard let id: UUID = boardID(b) else { return nil } return .boardLevel(boardID: id, boardLabelHint: labelHint) } } }