LoginSignup
2
0

More than 5 years have passed since last update.

MIDIデバイスを認識して、メッセージを受信する

Last updated at Posted at 2018-04-18

MIDIデバイスを認識して、デバイス名をアラート画面に、メッセージをInnerHTMLで表示しています。

エラー時のコールバック関数は省いているのでシンプルです。

javascript
navigator.requestMIDIAccess().then(function(midiAccess) {
    var inputs = [];
    var inputIterator = midiAccess.inputs.values();
    for (var i = inputIterator.next(); !i.done; i = inputIterator.next()) {
        inputs.push(i.value);
    }
    alert(inputs[0].manufacturer + inputs[0].name);
    inputs[0].onmidimessage = function(e) {
        test.innerHTML = (e.data[0] + " " + e.data[1] + " " + e.data[2] + " ");
    };
});

ちなみに、ステータスバイトを上位と下位で分けて出力する場合はビット演算子を利用して、こんな感じにします。

function(e)内のプログラム
var highNibble = (e.data[0] & 240) >> 4;
var lowNibble = e.data[0] & 15;
test.innerHTML = (highNibble + " " + lowNibble + " " + e.data[1] + " " + e.data[2]);
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0