-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.js
293 lines (272 loc) · 7.74 KB
/
midi.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var log = console.log.bind(console),
keyData = document.getElementById('key_data'),
midi;
var AudioContext = AudioContext || webkitAudioContext; // for ios/safari
var context = new AudioContext();
var btnBox = document.getElementById('content'),
btn = document.getElementsByClassName('button');
var data, cmd, channel, type, note, velocity;
// request MIDI access
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess({
sysex: false
}).then(onMIDISuccess, onMIDIFailure);
} else {
alert("No MIDI support in your browser.");
}
// add event listeners
document.addEventListener('keydown', keyController);
document.addEventListener('keyup', keyController);
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('mousedown', clickPlayOn);
btn[i].addEventListener('mouseup', clickPlayOff);
}
// prepare audio files
for (var i = 0; i < btn.length; i++) {
addAudioProperties(btn[i]);
}
// this maps the MIDI key value (60 - 64) to our samples
var sampleMap = {
key60: 1,
key61: 2,
key62: 3,
key63: 4,
key64: 5
};
// user interaction, mouse click
function clickPlayOn(e) {
e.target.classList.add('active');
e.target.play();
}
function clickPlayOff(e) {
e.target.classList.remove('active');
}
// qwerty keyboard controls. [q,w,e,r,t]
function keyController(e) {
if (e.type == "keydown") {
switch (e.keyCode) {
case 81:
btn[0].classList.add('active');
btn[0].play();
break;
case 87:
btn[1].classList.add('active');
btn[1].play();
break;
case 69:
btn[2].classList.add('active');
btn[2].play();
break;
case 82:
btn[3].classList.add('active');
btn[3].play();
break;
case 84:
btn[4].classList.add('active');
btn[4].play();
break;
default:
//console.log(e);
}
} else if (e.type == "keyup") {
switch (e.keyCode) {
case 81:
btn[0].classList.remove('active');
break;
case 87:
btn[1].classList.remove('active');
break;
case 69:
btn[2].classList.remove('active');
break;
case 82:
btn[3].classList.remove('active');
break;
case 84:
btn[4].classList.remove('active');
break;
default:
//console.log(e.keyCode);
}
}
}
// midi functions
function onMIDISuccess(midiAccess) {
midi = midiAccess;
var inputs = midi.inputs.values();
// loop through all inputs
for (var input = inputs.next(); input && !input.done; input = inputs.next()) {
// listen for midi messages
input.value.onmidimessage = onMIDIMessage;
// this just lists our inputs in the console
listInputs(input);
}
// listen for connect/disconnect message
midi.onstatechange = onStateChange;
}
function onMIDIMessage(event) {
data = event.data,
cmd = data[0] >> 4,
channel = data[0] & 0xf,
type = data[0] & 0xf0, // channel agnostic message type. Thanks, Phil Burk.
note = data[1],
velocity = data[2];
// with pressure and tilt off
// note off: 128, cmd: 8
// note on: 144, cmd: 9
// pressure / tilt on
// pressure: 176, cmd 11:
// bend: 224, cmd: 14
if(velocity > 0 && type == 144){
noteOn(note,velocity);
}
if(velocity == 0 && type == 144 ){
noteOff(note,velocity);
}
/* switch (type) {
case 144: // noteOn message
noteOn(note, velocity);
break;
case 128: // noteOff message
noteOff(note, velocity);
break;
}
*/
//console.log('data', data, 'cmd', cmd, 'channel', channel);
//logger(keyData, 'key data', data);
}
function onStateChange(event) {
var port = event.port,
state = port.state,
name = port.name,
type = port.type;
if (type == "input") console.log("name", name, "port", port, "state", state);
}
function listInputs(inputs) {
var input = inputs.value;
log("Input port : [ type:'" + input.type + "' id: '" + input.id +
"' manufacturer: '" + input.manufacturer + "' name: '" + input.name +
"' version: '" + input.version + "']");
}
function noteOn(midiNote, velocity) {
var noteColor = "#FFFFFF";
var key = getKey(midiNote);
console.log(key);
console.log(midiNote);
switch(key){
case 0:
noteColor = "#FF0000";
break;
case 1:
noteColor = "#FB6B22"
break;
case 2:
noteColor = "#FF7F00";
break;
case 3:
noteColor = "#FFC000";
break;
case 4:
noteColor = "#FFFF00";
break;
case 5:
noteColor = "#00FF00";
break;
case 6:
noteColor = "#00AF50";
break;
case 7:
noteColor = "#0000FF";
break;
case 8:
noteColor = "#0329E4";
break;
case 9:
noteColor = "#4B0082";
break;
case 10:
noteColor = "#B15CFF";
break;
case 11:
noteColor = "#9400D3";
break;
}
document.getElementById("midi_color").style.backgroundColor = noteColor;
//player(midiNote, velocity);
}
function noteOff(midiNote, velocity) {
document.getElementById("midi_color").style.backgroundColor = "#FFFFFF";
//player(midiNote, velocity);
}
function getKey(midiNote){
for(var i = 0; i < 12; i++){
for(var j = 0; j < 11; j++){
if(midiNote == i+j*12){
return i;
}
}
}
}
function player(note, velocity) {
var sample = sampleMap['key' + note];
if (sample) {
if (type == (0x80 & 0xf0) || velocity == 0) { //QuNexus always returns 144
btn[sample - 1].classList.remove('active');
return;
}
btn[sample - 1].classList.add('active');
btn[sample - 1].play(velocity);
}
}
function onMIDIFailure(e) {
log("No access to MIDI devices or your browser doesn't support WebMIDI API. Please use WebMIDIAPIShim " + e);
}
// audio functions
// We'll go over these in detail in future posts
function loadAudio(object, url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function () {
context.decodeAudioData(request.response, function (buffer) {
object.buffer = buffer;
});
}
request.send();
}
function addAudioProperties(object) {
object.name = object.id;
object.source = object.dataset.sound;
loadAudio(object, object.source);
object.play = function (volume) {
var s = context.createBufferSource();
var g = context.createGain();
var v;
s.buffer = object.buffer;
s.playbackRate.value = randomRange(0.5, 2);
if (volume) {
v = rangeMap(volume, 1, 127, 0.2, 2);
s.connect(g);
g.gain.value = v * v;
g.connect(context.destination);
} else {
s.connect(context.destination);
}
s.start();
object.s = s;
}
}
// utility functions
function randomRange(min, max) {
return Math.random() * (max + min) + min;
}
function rangeMap(x, a1, a2, b1, b2) {
return ((x - a1) / (a2 - a1)) * (b2 - b1) + b1;
}
function frequencyFromNoteNumber(note) {
return 440 * Math.pow(2, (note - 69) / 12);
}
function logger(container, label, data) {
messages = label + " [channel: " + (data[0] & 0xf) + ", cmd: " + (data[0] >> 4) + ", type: " + (data[0] & 0xf0) + " , note: " + data[1] + " , velocity: " + data[2] + "]";
container.textContent = messages;
}