メディアコントロールにMIDI信号を使う

メディアコントロールにMIDI信号を使う

「再生」や「停止」など、いくつかのコントロールキーを備えた実際のMIDIキーボードがあります。押すと、MIDIバスを介してそれぞれMIDIコード115と116が送信されます。 「再生」を押すと、再生を開始するためにこれらのコマンドをLinuxの一般的なメディアコントロール(再生と一時停止)にリンクできますか?

他のMIDIキー(上/下など)を対応するキーボード対応(上/下矢印など)に接続できますか?

ベストアンサー1

コメントでは、dirktはカスタムプログラムの作成を提案しました。だから私はMIDIコントローラから入力を読み、必要なキーストロークをシミュレートする短い概念証明スクリプトをPythonで書いた。 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()

必要:

おすすめ記事