-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidiout.js
49 lines (40 loc) · 1.09 KB
/
midiout.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
class MidiOut {
constructor(midiOutNamePort){
MidiOut.namePort = midiOutNamePort;
if (navigator.requestMIDIAccess)
navigator.requestMIDIAccess().then(this.success, this.failure);
}
get outputMIDI() {
return this.moutputMIDI;
}
set outputMIDI(value){
this.moutputMIDI = value;
}
success(midi) {
MidiOut.allmidi = midi;
for (var out of MidiOut.allmidi.outputs.values()) {
if (out.name == MidiOut.namePort) {
MidiOut.moutputMIDI = out;
break;
} else {
throw `"${MidiOut.namePort}" not a MIDI output.`;
}
}
}
sendNotes(notesObject) {
if (MidiOut.moutputMIDI != undefined) {
Object.keys(notesObject.notes).forEach(function(k) {
MidiOut.moutputMIDI.send([0x90, notesObject.notes[k], notesObject.vel]);
});
Object.keys(notesObject.notes).forEach(function(k) {
MidiOut.moutputMIDI.send([0x80, notesObject.notes[k], 0x00],performance.now()+notesObject.dur);
});
}
}
failure() {
throw 'No MIDI found';
}
onMIDIMessage (message) {
console.log('onMIDIMessage ', message.data);
}
}