-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_in.js
77 lines (64 loc) · 1.7 KB
/
midi_in.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
var midiListeners = [];
window.addEventListener('load', function() {
// patch up prefixes
window.AudioContext=window.AudioContext||window.webkitAudioContext;
context = new AudioContext();
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess().then( onSuccess, onError);
} else {
console.log("It looks like your browser does not support MIDI")
}
console.log("loaded midi bits");
} );
function setupInput() {
var deviceDetected = false;
var inputs = midiAccess.inputs.values();
for (var input = inputs.next();
input && !input.done;
input = inputs.next()) {
input.value.onmidimessage = MIDIEventHandler;
deviceDetected = true;
}
}
function onSuccess(midi) {
console.log("MIDI ready!");
midiAccess = midi;
setupInput();
midiAccess.onstatechange = setupInput;
}
function onError(err) {
console.log("There was a problem initialising MIDI stuff");
}
function MIDIEventHandler(event) {
// we don't need the message channel
var message = event.data[0] & 0xf0;
var note = event.data[1];
var velocity = event.data[2];
if (0x90 == message) {
if (0 != velocity) {
console.log("velocity is " + velocity);
console.log("note is " + note);
noteOn(note);
return;
}
// velocity of 0 is a note-off
noteOff(note);
}
if (0x80 == message) {
noteOff(note);
return;
}
}
function noteOn(noteNumber) {
var conversion = midiMap[event.data[1]];
if (conversion) {
// notify listeners of press
for (i = 0; i < midiListeners.length; i++) {
// TODO: we need more information than just the 'note'
midiListeners[i](conversion);
}
}
}
function noteOff(noteNumber) {
// TODO: notify listeners
}