Add computer-keyboard control mapping
Bind letter/digit keys (with Shift/Control/Option; Command reserved for menu shortcuts) to the same rig toggle Actions a MIDI Program-Change footswitch drives. Keys reuse MIDIController's perform(_:) dispatch via a new .key learn target and ingestKey(_:modifiers:). Key capture is platform-specific: macOS installs an AppKit NSEvent.addLocalMonitorForEvents(.keyDown) monitor (SwiftUI .onKeyPress only delivers to the focused view); iOS uses .onKeyPress with the root made focusable/focused, reclaiming focus when a text-field modal closes. Both gate on the first responder not being a text field so typing is never hijacked, filter key-repeat, and reserve Command for the menu. MIDIMap gains keys:[KeyBinding] and a KeyModifiers OptionSet, both with tolerant Codable decode so old rigs (no keys / no modifiers) still load with switches/ccs intact. The mapping sheet is unified: each switch row shows its footswitch (PC) and key bindings with per-source learn/clear, and the sheet is retitled "MIDI / Control Mapping". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
// MIDIControllerKeyTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
import LyrebirdSynth
|
||||
import LyrebirdUI
|
||||
@testable import LyrebirdShoeGaze
|
||||
|
||||
@MainActor
|
||||
@Suite("MIDIController key bindings")
|
||||
struct MIDIControllerKeyTests {
|
||||
|
||||
/// Builds an engine + a one-board rig (a single Volume pedal) + a controller.
|
||||
private func makeRig() throws -> (MIDIController, ShoeGaze, URL) {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
||||
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
||||
let board: PedalBoard = try sg.addBoard()
|
||||
board.label = "Board 1"
|
||||
try board.add(VolumePedal())
|
||||
try sg.finalizeBoard(board)
|
||||
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
||||
return (controller, sg, url)
|
||||
}
|
||||
|
||||
@Test func learnCapturesKeyAndFiresCallback() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
var changed: Bool = false
|
||||
controller.onMapChanged = { changed = true }
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
let consumed: Bool = controller.ingestKey("b")
|
||||
|
||||
#expect(consumed, "the learn press is consumed")
|
||||
#expect(controller.learnTarget == nil)
|
||||
#expect(changed)
|
||||
#expect(controller.keyBinding(for: .rigBypass)?.key == "b")
|
||||
#expect(sg.midiMap?.keys.count == 1)
|
||||
#expect(sg.rigBypassed == false, "learn must not toggle")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func keyLatchesOnEachPress() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b") // learn only
|
||||
#expect(sg.rigBypassed == false)
|
||||
|
||||
#expect(controller.ingestKey("b"))
|
||||
#expect(sg.rigBypassed == true, "first press engages")
|
||||
#expect(controller.ingestKey("b"))
|
||||
#expect(sg.rigBypassed == false, "second press releases")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func caseInsensitiveMatch() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b")
|
||||
#expect(controller.ingestKey("B"), "an uppercase press matches a lowercase binding")
|
||||
#expect(sg.rigBypassed == true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func shiftDistinguishesTwoBindingsOnTheSameKey() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b", modifiers: [])
|
||||
controller.armKeyLearn(.rigMute)
|
||||
_ = controller.ingestKey("b", modifiers: [.shift])
|
||||
|
||||
#expect(sg.midiMap?.keys.count == 2, "b and ⇧b are independent bindings")
|
||||
|
||||
#expect(controller.ingestKey("b", modifiers: []))
|
||||
#expect(sg.rigBypassed == true, "unmodified b drives bypass")
|
||||
#expect(sg.rigMuted == false, "unmodified b must not fire the ⇧b binding")
|
||||
|
||||
#expect(controller.ingestKey("b", modifiers: [.shift]))
|
||||
#expect(sg.rigMuted == true, "⇧b drives mute")
|
||||
#expect(sg.rigBypassed == true, "⇧b must not re-fire the plain-b binding")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func modifierMismatchIsNotConsumed() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b", modifiers: [.shift]) // learn ⇧b
|
||||
#expect(controller.ingestKey("b", modifiers: []) == false, "plain b does not match ⇧b")
|
||||
#expect(controller.ingestKey("b", modifiers: [.control]) == false, "⌃b does not match ⇧b")
|
||||
#expect(sg.rigBypassed == false)
|
||||
#expect(controller.ingestKey("b", modifiers: [.shift]), "⇧b matches")
|
||||
#expect(sg.rigBypassed == true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func unboundKeyIsNotConsumed() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b")
|
||||
#expect(controller.ingestKey("z") == false, "an unbound key falls through")
|
||||
#expect(sg.rigBypassed == false)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func digitKeyBinds() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("3")
|
||||
#expect(controller.keyBinding(for: .rigBypass)?.key == "3")
|
||||
#expect(controller.ingestKey("3"))
|
||||
#expect(sg.rigBypassed == true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func nonAlphanumericKeyRejected() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
// Return, Tab, and Escape are single Characters but not letters/digits — they must not be
|
||||
// learnable, or binding one would silently swallow it everywhere in the app.
|
||||
#expect(controller.ingestKey("\r") == false)
|
||||
#expect(controller.ingestKey("\t") == false)
|
||||
#expect(controller.ingestKey("\u{1B}") == false)
|
||||
#expect(controller.ingestKey(" ") == false)
|
||||
#expect(controller.learnTarget != nil, "a rejected press leaves learn armed")
|
||||
#expect(sg.midiMap?.keys.isEmpty ?? true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func multiCharacterInputRejected() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
#expect(controller.ingestKey("ab") == false, "a multi-character press is ignored")
|
||||
#expect(controller.learnTarget != nil, "an ignored press leaves learn armed")
|
||||
#expect(sg.midiMap?.keys.isEmpty ?? true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func keyBindingSurvivesSaveLoadAndStillFires() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
let preset: URL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".json")
|
||||
defer { try? FileManager.default.removeItem(at: preset) }
|
||||
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b")
|
||||
|
||||
try sg.saveRig(to: preset)
|
||||
try sg.loadRig(from: preset)
|
||||
|
||||
#expect(sg.midiMap?.keys.first?.key == "b", "the key binding survives a file round-trip")
|
||||
#expect(controller.ingestKey("b"), "the restored binding still fires")
|
||||
#expect(sg.rigBypassed == true)
|
||||
withExtendedLifetime(sg) { }
|
||||
}
|
||||
|
||||
@Test func clearRemovesBinding() throws {
|
||||
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
withExtendedLifetime(sg) {
|
||||
controller.armKeyLearn(.rigBypass)
|
||||
_ = controller.ingestKey("b")
|
||||
#expect(controller.keyBinding(for: .rigBypass) != nil)
|
||||
|
||||
controller.clearKeyBinding(for: .rigBypass)
|
||||
#expect(controller.keyBinding(for: .rigBypass) == nil)
|
||||
#expect(controller.ingestKey("b") == false, "a cleared key no longer fires")
|
||||
#expect(sg.rigBypassed == false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,16 @@ struct MIDIMapCodableTests {
|
||||
channel: 0,
|
||||
target: .boardLevel(boardID: Self.boardID, boardLabelHint: "Board 1")
|
||||
),
|
||||
],
|
||||
keys: [
|
||||
MIDIMap.KeyBinding(key: "b", action: .rigBypass, displayNameHint: "Rig"),
|
||||
MIDIMap.KeyBinding(key: "b", modifiers: [.shift], action: .rigMute),
|
||||
MIDIMap.KeyBinding(
|
||||
key: "r",
|
||||
modifiers: [.control, .option],
|
||||
action: .pedalAction(pedalID: Self.looperID, kind: .looperRecordStop),
|
||||
displayNameHint: "Looper"
|
||||
),
|
||||
]
|
||||
)
|
||||
}
|
||||
@@ -79,4 +89,43 @@ struct MIDIMapCodableTests {
|
||||
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
|
||||
#expect(decoded.midiMap == nil, "legacy rig JSON should decode with a nil midiMap")
|
||||
}
|
||||
|
||||
@Test func keyBindingsSurviveArchiveDocumentRoundTrip() throws {
|
||||
// The `.shoegaze` archive / share path wraps a RigEntry → ShoeGazeState → MIDIMap. Verify a
|
||||
// key binding survives a full document encode/decode, not just a bare MIDIMap round-trip.
|
||||
let state: ShoeGazeState = ShoeGazeState(boards: [], midiMap: sampleMap())
|
||||
let entry: RigEntry = RigEntry(name: "Keys", state: state)
|
||||
let document: ShoeGazeDocument = ShoeGazeDocument(rig: entry)
|
||||
let data: Data = try document.data()
|
||||
let decoded: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data)
|
||||
let restored: MIDIMap? = decoded.rigEntries?.first?.state.midiMap
|
||||
#expect(restored?.keys == sampleMap().keys)
|
||||
}
|
||||
|
||||
@Test func keyBindingWithoutModifiersDecodesUnmodified() throws {
|
||||
// A key binding saved before modifiers existed has a `key` + `action` but no `modifiers`.
|
||||
// It must decode as an unmodified binding, not fail and drop the map.
|
||||
let json: String = #"""
|
||||
{"switches":[],"ccs":[],"keys":[{"key":"b","action":{"rigBypass":{}}}]}
|
||||
"""#
|
||||
let data: Data = Data(json.utf8)
|
||||
let decoded: MIDIMap = try JSONDecoder().decode(MIDIMap.self, from: data)
|
||||
#expect(decoded.keys.count == 1)
|
||||
#expect(decoded.keys.first?.key == "b")
|
||||
#expect(decoded.keys.first?.modifiers == [])
|
||||
}
|
||||
|
||||
@Test func mapWithoutKeysDecodesWithEmptyKeysNotDropped() throws {
|
||||
// A rig saved before keyboard mapping existed has a midiMap with switches/ccs but no `keys`.
|
||||
// It must decode with keys == [] AND keep its switch/cc bindings (a missing-key failure that
|
||||
// fell through to the malformed path would silently drop the whole map).
|
||||
let json: String = #"""
|
||||
{"version":1,"boards":[],"midiMap":{"switches":[{"program":3,"action":{"rigBypass":{}}}],"ccs":[]}}
|
||||
"""#
|
||||
let data: Data = Data(json.utf8)
|
||||
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
|
||||
#expect(decoded.midiMap?.keys == [], "absent keys should decode to an empty array")
|
||||
#expect(decoded.midiMap?.switches.count == 1, "switch bindings must survive the missing keys")
|
||||
#expect(decoded.midiMap?.switches.first?.program == 3)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user