diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift index 0624ce0..6dd14d4 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift @@ -309,8 +309,10 @@ public final class MIDIController { displayNameHint: hint(for: action) ) var map: MIDIMap = shoeGaze.midiMap ?? MIDIMap.empty - // One action → one program, and reassigning a program steals it from any other action. - map.switches.removeAll { $0.action == action || $0.program == program } + // One binding per action — re-learning an action replaces its binding. A single footswitch + // (program) may drive MULTIPLE actions on purpose (a "scene": e.g. bypass two pedals, or + // solo a board and bypass another, from one press), so we do NOT steal by program number. + map.switches.removeAll { $0.action == action } map.switches.append(binding) shoeGaze.midiMap = map onMapChanged?() diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift index c7ce353..d543a6c 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift @@ -153,18 +153,42 @@ struct MIDIControllerPCTests { } } - @Test func reassigningProgramStealsFromPriorAction() throws { + @Test func oneProgramCanDriveMultipleActions() throws { + // A single footswitch (PC) may drive several toggles at once — a "scene". + let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = + try makeRig() + defer { try? FileManager.default.removeItem(at: url) } + + withExtendedLifetime(sg) { + controller.armSwitchLearn(.rigBypass) + controller.ingestForTesting(.programChange(channel: 0, program: 8)) + controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 0)) + controller.ingestForTesting(.programChange(channel: 0, program: 8)) // same PC, kept + + // Both bindings survive — one PC drives two actions. + #expect(controller.switchBinding(for: .rigBypass)?.program == 8) + #expect(controller.switchBinding(for: .pedalBypass(boardIndex: 0, pedalIndex: 0))?.program == 8) + #expect(sg.midiMap?.switches.count == 2) + + // Firing the PC toggles both at once. + let pedalBypass: UIParamNode = boards[0].pedals[0].$bypass + controller.ingestForTesting(.programChange(channel: 0, program: 8)) + #expect(sg.rigBypassed == true) + #expect(abs(pedalBypass.value - 1.0) < 1e-9) + } + } + + @Test func reLearningAnActionReplacesItsProgram() throws { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } withExtendedLifetime(sg) { controller.armSwitchLearn(.rigBypass) controller.ingestForTesting(.programChange(channel: 0, program: 8)) - controller.armSwitchLearn(.boardBypass(boardIndex: 0)) - controller.ingestForTesting(.programChange(channel: 0, program: 8)) // same PC steals it + controller.armSwitchLearn(.rigBypass) + controller.ingestForTesting(.programChange(channel: 0, program: 9)) // same action, new PC - #expect(controller.switchBinding(for: .rigBypass) == nil, "prior action loses the PC") - #expect(controller.switchBinding(for: .boardBypass(boardIndex: 0))?.program == 8) + #expect(controller.switchBinding(for: .rigBypass)?.program == 9, "re-learn replaces") #expect(sg.midiMap?.switches.count == 1) } }