diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift index 16dbdef..0624ce0 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift @@ -216,11 +216,10 @@ public final class MIDIController { target: resolved.hinted ) var map: MIDIMap = shoeGaze.midiMap ?? MIDIMap.empty - // One control → one CC, and reassigning a CC (same controller + channel) steals it from any - // other control so a given expression pedal drives exactly one thing. - map.ccs.removeAll { - $0.target.addresses(resolved.hinted) || ($0.controller == controller && $0.channel == channel) - } + // 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.append(binding) shoeGaze.midiMap = map onMapChanged?() @@ -254,9 +253,10 @@ public final class MIDIController { private func resolveCC(_ target: MIDIMap.CCTarget) -> ResolvedCC? { switch target { case let .pedalControl(boardIndex, pedalIndex, controlIndex, nameHint, labelHint): - guard let pedal: Pedal = pedal(boardIndex: boardIndex, pedalIndex: pedalIndex), - controlIndex >= 0, controlIndex < pedal.ccMappableControls.count else { return nil } - let control: PedalControl = pedal.ccMappableControls[controlIndex] + guard let pedal: Pedal = pedal(boardIndex: boardIndex, pedalIndex: pedalIndex) else { return nil } + let controls: [PedalControl] = pedal.ccMappableControls + guard controlIndex >= 0, controlIndex < controls.count else { return nil } + let control: PedalControl = controls[controlIndex] if let nameHint: String = nameHint, nameHint != pedal.displayName { return nil } if let labelHint: String = labelHint, labelHint != control.label { return nil } let hinted: MIDIMap.CCTarget = .pedalControl( diff --git a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift index b11060a..d6026d7 100644 --- a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift +++ b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift @@ -101,7 +101,7 @@ public struct MIDIMappingSheet: View { SwitchRowModel(id: "sw-rig", action: .rigBypass, label: "Rig Bypass"), SwitchRowModel(id: "sw-all", action: .allBoards, label: "All Boards On"), ] - for boardPair in shoeGaze.boards.enumerated() { + for boardPair: (offset: Int, element: PedalBoard) in shoeGaze.boards.enumerated() { let boardIndex: Int = boardPair.offset let board: PedalBoard = boardPair.element rows.append(SwitchRowModel(id: "sw-solo-\(boardIndex)", @@ -113,7 +113,7 @@ public struct MIDIMappingSheet: View { rows.append(SwitchRowModel(id: "sw-byp-\(boardIndex)", action: .boardBypass(boardIndex: boardIndex), label: "\(board.label) · Bypass")) - for pedalPair in board.pedals.enumerated() { + for pedalPair: (offset: Int, element: Pedal) in board.pedals.enumerated() { rows.append(SwitchRowModel( id: "sw-pb-\(boardIndex)-\(pedalPair.offset)", action: .pedalBypass(boardIndex: boardIndex, pedalIndex: pedalPair.offset), @@ -125,7 +125,7 @@ public struct MIDIMappingSheet: View { private var ccRowModels: [CCRowModel] { var rows: [CCRowModel] = [] - for boardPair in shoeGaze.boards.enumerated() { + for boardPair: (offset: Int, element: PedalBoard) in shoeGaze.boards.enumerated() { let boardIndex: Int = boardPair.offset let board: PedalBoard = boardPair.element // Board master level (the board fader). @@ -133,10 +133,10 @@ public struct MIDIMappingSheet: View { id: "cc-blvl-\(boardIndex)", target: .boardLevel(boardIndex: boardIndex, boardLabelHint: nil), label: "\(board.label) · Level")) - for pedalPair in board.pedals.enumerated() { + for pedalPair: (offset: Int, element: Pedal) in board.pedals.enumerated() { let pedalIndex: Int = pedalPair.offset let pedal: Pedal = pedalPair.element - for controlPair in pedal.ccMappableControls.enumerated() { + for controlPair: (offset: Int, element: PedalControl) in pedal.ccMappableControls.enumerated() { rows.append(CCRowModel( id: "cc-\(boardIndex)-\(pedalIndex)-\(controlPair.offset)", target: .pedalControl( diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index d5c17a8..1e0f8cc 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -874,17 +874,21 @@ public struct ShoeGazeBuilderView: View { // MARK: - Rig library - /// Highlights the persisted current rig once the library has loaded. Runs once; only sets the - /// highlight when nothing is selected yet and the saved id still exists in the library. + /// Highlights the library entry that matches the restored working rig, once the library has + /// loaded. Runs once. + /// + /// The working graph is restored from the autosave file independently of any persisted + /// selection, so the highlight is derived from the *actual* loaded graph rather than a separate + /// stored id (which could point at a different rig and mislabel what's shown). If the working + /// graph is an unsaved/edited session that matches no saved entry, nothing is highlighted. private func restoreCurrentRig() { guard !didRestoreRig, library.isLoaded else { return } didRestoreRig = true - guard currentRigID == nil, - let stored: String = UserDefaults.standard.string( - forKey: ShoeGazeBuilderView.currentRigDefaultsKey), - let id: UUID = UUID(uuidString: stored), - library.entries.contains(where: { $0.id == id }) else { return } - currentRigID = id + guard currentRigID == nil else { return } + let working: ShoeGazeState = shoeGaze.captureState() + if let match: RigEntry = library.entries.first(where: { $0.state == working }) { + currentRigID = match.id + } } /// Persists `currentRigID` so the same rig is highlighted on the next launch. diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift index 2291178..f2ac690 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift @@ -139,6 +139,32 @@ struct MIDIControllerCCTests { } } + @Test func oneCCDrivesMultipleControlsForCrossfade() throws { + // Assigning a second control to a CC must NOT steal it from the first — a single expression + // pedal can crossfade by driving two controls (one inverted). + let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig() + defer { try? FileManager.default.removeItem(at: url) } + + withExtendedLifetime(sg) { + // Control 0 = Position, control 1 = Mix on a Volume pedal. + controller.armCCLearn(pedalControl(0, 0, 0)) + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0)) + controller.armCCLearn(pedalControl(0, 0, 1)) + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0)) + controller.setCCInverted(for: pedalControl(0, 0, 1), true) + + // Both bindings survive — the same CC drives both controls. + #expect(sg.midiMap?.ccs.count == 2, "assigning a 2nd control must not steal the 1st") + #expect(controller.ccBinding(for: pedalControl(0, 0, 0)) != nil) + + let position: UIParamNode = board.pedals[0].ccMappableControls[0].node + let mix: UIParamNode = board.pedals[0].ccMappableControls[1].node + controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127)) + #expect(abs(position.value - 1.0) < 1e-9, "position sweeps up") + #expect(abs(mix.value - 0.0) < 1e-9, "inverted mix sweeps down — a crossfade") + } + } + @Test func unboundCCIsIgnored() throws { let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) }