-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
test-web-midi.html
91 lines (83 loc) · 2.39 KB
/
test-web-midi.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
86
87
88
89
90
91
<!DOCTYPE html>
<html>
<head>
<title>web-midi test</title>
<script src="javascript/JZZ.js"></script>
<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>
<script><!--
var output = document.getElementById('output');
var midiin = document.getElementById('midiin');
var midilog = [];
var webmidi;
var _time = Date.now || function () { return new Date().getTime(); };
var _startTime = _time();
var _now = typeof performance != 'undefined' && performance.now ?
function() { return performance.now(); } : function() { return _time() - _startTime; };
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 = _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>
</body>
</html>