-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.js
57 lines (55 loc) · 2.16 KB
/
pad.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
(function($){
var inputHandlers = [
{
'applies': function(channel, note, velocity){ return velocity === 127; },
'handle': function(pad, channel, note, velocity){
pad.emit('press', new $.Button(channel, note, pad.midiAdapter));
}
},
{
'applies': function(channel, note, velocity){ return velocity === 0; },
'handle': function(pad, channel, note, velocity){
pad.emit('release', new $.Button(channel, note, pad.midiAdapter));
}
}
];
var buttonLookups = [
{
'applies': function(args){ return args.length === 1; },
'lookup': function(pad, args){
return new $.Button(144, args[0], pad.midiAdapter);
}
},
{
'applies': function(args){ return args.length === 2; },
'lookup': function(pad, args){
return new $.Button(144, args[0] + 16 * args[1], pad.midiAdapter);
}
}
];
var Pad = $.Launchpad = function(midiAdapter){
$.Observable.call(this);
this.midiAdapter = midiAdapter;
this.midiAdapter.on('input', this.handleInput.bind(this));
};
Pad.prototype = Object.create($.Observable.prototype);
Pad.prototype.constructor = Pad;
Pad.prototype.handleInput = function(channel, note, velocity){
inputHandlers
.filter(function(handler){ return handler.applies(channel, note, velocity); })
.forEach(function(handler){ handler.handle(this, channel, note, velocity); }.bind(this));
};
Pad.prototype.clear = function(){
this.midiAdapter.send(176, 0, 0);
};
Pad.prototype.button = function(){
var args = Array.prototype.slice.call(arguments);
return buttonLookups
.filter(function(buttonLookup){ return buttonLookup.applies(args); })
.map(function(buttonLookup){ return buttonLookup.lookup(this, args); }.bind(this))
[0];
};
Pad.prototype.controlButton = function(id){
return new $.Button(176, 104 + id, this.midiAdapter);
};
})(window.launchpad = window.launchpad || {});