-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meedee.js
73 lines (54 loc) · 1.8 KB
/
Meedee.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
62
63
64
65
66
67
68
69
70
71
72
73
//@author github.com/mog
var MIDI = (function() {
"use strict";
var _inputs = [],
_outputs = [],
_eventList = {};
function handleInput(event){
var data = event.data,
id = data[1],
val = data[2];
if (data[0] == 254 || data[0] == 248)
return;
if(_eventList[ id ])
for(var i = 0, len = _eventList[ id ].length; i < len; i++ )
_eventList[ id ][i]( val, event );
if(_eventList[ 'any' ])
for(i = 0, len = _eventList[ 'any' ].length; i < len; i++ )
_eventList[ 'any' ][i]( val, event );
}
(function init(){
navigator.requestMIDIAccess().then(function(midi) {
for (var i = midi.inputs.values(), o = i.next(); !o.done; o = i.next()) {
_inputs.push(o.value);
}
for (i = midi.outputs.values(), o = i.next(); !o.done; o = i.next()) {
_outputs.push(o.value);
}
_inputs.forEach(function(input) {
input.onmidimessage = handleInput;
});
});
})();
function bind(key, fn){
if(!_eventList[key])
_eventList[key] = [];
_eventList[key].push(fn);
}
function unbind(key, fn){
if(_eventList[ key ]){
for(var i = 0, len = _eventList[ id ].length; i < len; i++ ){
if(fn === _eventList[ id ][i])
_eventList[ id ].splice(i, 1);
break;
}
if(_eventList[ id ].length === 0)
delete _eventList[ id ];
}
}
return {
on:bind,
off:unbind,
inputs: _inputs
};
}());