我有一个物理 MIDI 键盘,它还具有一些控制键,例如“播放”和“停止”。当按下时,它们分别通过 MIDI 总线发送 MIDI 代码 115 和 116。是否可以将这些命令连接到 Linux 的常用媒体控件(播放和暂停),以便当我按下“播放”时,播放开始?
是否还可以将其他 MIDI 键(例如,向上/向下)连接到其各自的键盘对应键(例如,向上/向下箭头)?
答案1
在评论中,dirkt 建议编写一个自定义程序。因此,我用 Python 编写了一个简短的概念验证脚本,它从 MIDI 控制器读取输入,然后模拟所需的按键。我在 Ubuntu 20.04 上测试过。不幸的是,它需要超级用户权限才能运行,否则/dev/uinput
无法打开进行写入。
import mido
from evdev import uinput, ecodes as e
def press_playpause():
"""Simulate pressing the "play" key"""
with uinput.UInput() as ui:
ui.write(e.EV_KEY, e.KEY_PLAY, 1)
ui.syn()
def clear_event_queue(inport):
"""Recent events are stacked up in the event queue
and read when opening the port. Avoid processing these
by clearing the queue.
"""
while inport.receive(block=False) is not None:
pass
device_name = mido.get_input_names()[0] # you may change this line
print("Device name:", device_name)
MIDI_CODE_PLAY = 115
MIDI_VALUE_ON = 127
with mido.open_input(name=device_name) as inport:
clear_event_queue(inport)
print("Waiting for MIDI events...")
for msg in inport:
print(msg)
if (hasattr(msg, "value")
and msg.value == MIDI_VALUE_ON
and hasattr(msg, "control")
and msg.control == MIDI_CODE_PLAY):
press_playpause()
要求: