-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.html
85 lines (78 loc) · 2.12 KB
/
test.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<title>web-midi test</title>
<style type="text/css">
#midiin { height: 11em;}
</style>
</head>
<body>
<h1>web-midi test</h1>
<pre id=output>WebMIDI is not working</pre>
<pre id=midiin></pre>
</body>
<script><!--
var output = document.getElementById('output');
var midiin = document.getElementById('midiin');
var midilog = [];
var webmidi;
function printMidi(name) {
return function(msg) {
var s = name + ':';
for (var i = 0; i < msg.data.length; i++) s += (msg.data[i] <16 ? ' 0' : ' ') + msg.data[i].toString(16);
midilog.push(s);
if (midilog.length > 16) midilog.splice(0, 1);
midiin.innerHTML = midilog.join('\n');
}
}
function refresh() {
output.innerHTML = 'WebMIDI is working!\n';
if (webmidi.outputs.size) {
output.innerHTML += '\nMIDI-Out ports available: ' + webmidi.outputs.size + '\n';
var time = window.performance.now();
webmidi.outputs.forEach(function(port) {
output.innerHTML += ' ' + port.name +
'; manufacturer: ' + port.manufacturer +
'; version: ' + port.version +
'; state: ' + port.state + '\n';
port.send([0x90, 0x40, 0x7f], time);
time += 200;
port.send([0x80, 0x40, 0x00], time);
time += 200;
});
}
else {
output.innerHTML += '\nNo MIDI-Out ports available.\n';
}
if (webmidi.inputs.size) {
output.innerHTML += '\nMIDI-In ports available: ' + webmidi.inputs.size + '\n';
webmidi.inputs.forEach(function(port) {
port.onmidimessage = printMidi(port.name);
output.innerHTML += ' ' + port.name +
'; manufacturer: ' + port.manufacturer +
'; version: ' + port.version +
'; state: ' + port.state + '\n';
});
}
else {
output.innerHTML += '\nNo MIDI-In ports available.\n';
}
}
function fail(err) {
var s = 'Cannot start WebMIDI';
if (err) s += ': ' + err;
output.innerHTML = s;
}
function success(midiaccess) {
webmidi = midiaccess;
webmidi.onstatechange = refresh;
refresh();
}
try {
navigator.requestMIDIAccess().then(success, fail);
}
catch (err) {
output.innerHTML = 'Cannot start WebMIDI: ' + err;
}
--></script>
</html>