-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
test-web-midi.js
61 lines (51 loc) · 1.45 KB
/
test-web-midi.js
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
56
57
58
59
60
61
'use strict';
var navigator = require('.');
var midi;
var inputs;
var outputs;
function onMIDIFailure(msg){
console.log('Failed to get MIDI access - ' + msg);
process.exit(1);
}
function onMIDISuccess(midiAccess){
midi = midiAccess;
inputs = midi.inputs;
outputs = midi.outputs;
setTimeout(testOutputs, 500);
}
function testOutputs(){
console.log('Testing MIDI-Out ports...');
outputs.forEach(function(port){
console.log('id:', port.id, 'manufacturer:', port.manufacturer, 'name:', port.name, 'version:', port.version);
port.open();
port.send([0x90, 60, 0x7f]);
});
setTimeout(stopOutputs, 1000);
}
function stopOutputs(){
outputs.forEach(function(port){
port.send([0x80, 60, 0]);
});
testInputs();
}
function onMidiIn(ev){
var arr = [];
for(var i = 0; i < ev.data.length; i++){
arr.push((ev.data[i] < 16 ? '0' : '') + ev.data[i].toString(16));
}
console.log('MIDI:', arr.join(' '));
}
function testInputs(){
console.log('Testing MIDI-In ports...');
inputs.forEach(function(port){
console.log('id:', port.id, 'manufacturer:', port.manufacturer, 'name:', port.name, 'version:', port.version);
port.onmidimessage = onMidiIn;
});
setTimeout(stopInputs, 5000);
}
function stopInputs(){
console.log('Thank you!');
navigator.close(); // This will close MIDI inputs, otherwise Node.js will wait for MIDI input forever.
process.exit(0);
}
navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);