Język javascript obsługuje odczyt z portów MIDI

Najczęściej mówimy o instrumentach muzycznych podłączanych przez USB do komputera.

Podstawowy program API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
   navigator.requestMIDIAccess()
    .then(onMIDISuccess, onMIDIFailure);


 function startLoggingMIDIInput(midiAccess) {
  midiAccess.inputs.forEach((entry) => {
    entry.onmidimessage = onMIDIMessage;
  });
}


function onMIDISuccess(midiAccess) {
    console.log(midiAccess);
    for (const entry of midiAccess.inputs) {
    const input = entry[1];
    console.log(
      `Input port [type:'${input.type}']` +
        ` id:'${input.id}'` +
        ` manufacturer:'${input.manufacturer}'` +
        ` name:'${input.name}'` +
        ` version:'${input.version}'`,
    );
    }

    startLoggingMIDIInput(midiAccess);    

    var inputs = midiAccess.inputs;
    var outputs = midiAccess.outputs;
}


function onMIDIMessage(event) {
  let str = `MIDI message received at timestamp ${event.timeStamp}[${event.data.length} bytes]: `;
  for (const character of event.data) {
    str += `0x${character.toString(16)} `;
  }
  console.log(str);
}


function onMIDIFailure() {
    console.log('Could not access your MIDI devices.');
}
    </script>    
</body>
</html>