-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-midi.html
79 lines (75 loc) · 2.06 KB
/
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
<dom-module id="web-midi">
<script>
Polymer({
is: 'web-midi',
properties: {
midi: {
type: Object,
notify: true
},
devices: {
type: String,
notify: true,
reflectToAttribute: false
},
value: {
type: String,
value: '',
reflectToAttribute: true
}
},
ready: function() {
navigator.requestMIDIAccess()
.then( (midiAccess, msg)=>{
if(!!msg) {
this._onMIDIFailure(msg);
return;
}
this._onMIDISuccess(midiAccess);
});
},
_onMIDISuccess: function(midiAccess) {
// converts midiAcess to Input and Output devices
this.midi = midiAccess;
this._listDevices(midiAccess);
},
_onMIDIFailure: function(msg) {
console.log( "Failed to get MIDI access - " + msg );
},
_listDevices: function(midiAccess) {
var inputDevices = [],
outputDevices = [];
for (var entry of midiAccess.inputs) {
var input = entry[1];
inputDevices.push({
id: input.id,
manufacturer: input.manufacturer,
name: input.name,
version: input.version
});
input.onmidimessage = this._onMIDIMessage.bind(this);
}
for (var entry of midiAccess.outputs) {
var output = entry[1];
outputDevices.push({
id: output.id,
manufacturer: output.manufacturer,
name: output.name,
version: output.version
});
}
this.devices = new Object();
this.devices['input'] = inputDevices;
this.devices['output'] = outputDevices;
},
_onMIDIMessage( event ) {
var str = '';
for (var i=0; i < event.data.length; i++) {
str += event.data[i].toString(16) + " ";
}
this.value = str;
this.fire('change',{value: str});
}
});
</script>
</dom-module>