-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidi.js
36 lines (30 loc) · 957 Bytes
/
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
var midiEnabled = false;
var useMidiRelease = false;
function getMIDIMessage(midiMessage) {
//console.log(midiMessage);
if (midiMessage.data[0] == 144 && midiMessage.data[2] != 0) { // NOTE_ON
document.getElementById("attack").value = Math.min(10, 10 - midiMessage.data[2] / 128 * 14);
keysPressed = [65, 68];
}
else if (midiMessage.data[0] == 128 || midiMessage.data[2] == 0) { // NOTE_OFF
if (useMidiRelease) {
document.getElementById("release").value = Math.min(4, 4 - midiMessage.data[2] / 128 * 7);
}
keysPressed = [];
}
}
function onMIDIFailure() {
console.log("Could not access your MIDI devices.");
}
function onMIDISuccess(midiAccess) {
console.log(midiAccess);
var inputs = midiAccess.inputs;
console.log(inputs);
for (var input of midiAccess.inputs.values()) {
input.onmidimessage = getMIDIMessage;
}
midiEnabled = true;
}
function reqMidi() {
navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);
}