-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchpad.js
194 lines (189 loc) · 6.94 KB
/
launchpad.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(function($){
var Observable = $.Observable = function(){
this.observers = {};
};
Observable.prototype.on = function(event, callback){
if (typeof callback !== 'function') return;
(this.observers[event] = this.observers[event] || []).push(callback);
};
Observable.prototype.emit = function(event){
var args = Array.prototype.slice.call(arguments, 1);
(this.observers[event] || []).forEach(function(callback){
callback.apply(undefined, args);
});
};
})(window.launchpad = window.launchpad || {});
;(function($){
var Button = $.Button = function(channel, note, midiAdapter){
$.Observable.call(this);
this.channel = channel;
this.note = note;
this.midiAdapter = midiAdapter;
this.id = this.note;
if (!this.isControl()) {
this.x = this.note % 16;
this.y = Math.floor(this.note / 16);
} else {
this.id -= 104;
}
};
Button.prototype = Object.create($.Observable.prototype);
Button.prototype.constructor = Button;
Button.prototype.turn = function(color){
this.paint($.defaults.paintNames[color]);
};
Button.prototype.paint = function(colors){
colors = $.extend(colors, { 'red': 0, 'green': 0 });
var velocity = (colors.green % 4) * 16 + (colors.red % 4);
this.send(velocity);
};
Button.prototype.send = function(velocity){
this.midiAdapter.send(this.channel, this.note, velocity);
};
Button.prototype.isControl = function(){
return this.channel === 176;
};
})(window.launchpad = window.launchpad || {});
;(function($, undefined){
var defaults = $.defaults = {};
function selectItem(name){
return {
'from': function(items){
var item = items.next();
while (!item.done) {
if (item.value.name === name) {
return item.value;
}
item = items.next();
}
return undefined;
}
};
}
defaults.paintNames = {
'red': { 'red': 3, 'green': 0 },
'green': { 'red': 0, 'green': 3 },
'orange': { 'red': 3, 'green': 3 }
};
defaults.name = 'Launchpad Mini MIDI 1';
defaults.sysex = false;
defaults.midiAdapterFactory = function(accept, reject){
if (!navigator.requestMIDIAccess){
reject();
} else {
var padName = this.name;
navigator.requestMIDIAccess({ sysex: this.sysex })
.then(function(midiAccess){
return {
'input': selectItem(padName).from(midiAccess.inputs.values()),
'output': selectItem(padName).from(midiAccess.outputs.values())
};
})
.then(function(io){
return new $.MidiAdapter(io.input, io.output);
})
.then(function(midiAdapter){
return new $.Launchpad(midiAdapter);
})
.then(function(pad){
accept(pad);
})
.catch(reject);
}
};
})(window.launchpad = window.launchpad || {});
;(function($){
$.extend = function extend(){
return Array.prototype.slice.call(arguments).reduce(function(result, dictionary){
for (var key in dictionary){
if (dictionary.hasOwnProperty(key) && !result.hasOwnProperty(key)) {
result[key] = dictionary[key];
}
}
return result;
}, {});
};
})(window.launchpad = window.launchpad || {});
;(function($){
$.connect = function(options){
options = $.extend(options || {}, $.defaults);
return new Promise(options.midiAdapterFactory.bind(options));
};
})(window.launchpad = window.launchpad || {});
;(function($){
var MidiAdapter = $.MidiAdapter = function(input, output){
$.Observable.call(this);
this.input = input;
this.output = output;
this.input.onmidimessage = this.onMidiMessageCallback.bind(this);
};
MidiAdapter.prototype = Object.create($.Observable.prototype);
MidiAdapter.prototype.constructor = MidiAdapter;
MidiAdapter.prototype.onMidiMessageCallback = function(message){
var channel = message.data[0];
var note = message.data[1];
var velocity = message.data[2];
this.emit('input', channel, note, velocity);
};
MidiAdapter.prototype.send = function(channel, note, velocity){
this.output.send([channel, note, velocity]);
};
})(window.launchpad = window.launchpad || {});
;(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 || {});
;(function($){
$.version = '1.0.0';
})(window.launchpad = window.launchpad || {});