-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmidi.js
77 lines (67 loc) · 2.17 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
"use strict";
if (navigator.requestMIDIAccess){
navigator.requestMIDIAccess().then( onMIDIInit, onMIDIReject );
}
else{
console.error("DOH! No MIDI support present in your browser.");
}
function onMIDIInit (midi){
// midi.inputs
// midi.onstatechange
// midi.outputs
// midi.sysexEnabled
console.log("Successfully Initialized MIDI");
// var foundString = "Found " + midi.inputs.size + " inputs and " + midi.outputs.size + " outputs.";
// console.log(foundString);
// console.log("Sysex is", midi.sysexEnabled ? "enabled" : "disabled");
midi.outputs.forEach(function(output){
console.log("Output id:", output.id, output);
if (output.manufacturer === "Teensyduino"){
console.log("Found Teensy", output);
window.teensy = output;
output.open();
}
});
midi.inputs.forEach(function(input){
onMIDIConect(input);
})
midi.onstatechange = function(event){
console.log("MIDIConnectionEvent on port", event.port);
if (event.port.type === 'input' && event.port.connection === "open"){
onMIDIConect(event.port);
}else if (event.port.type === 'output' && event.port.connection === "closed" && event.port.manufacturer === "Teensyduino"){
console.log("Found Teensy", event.port);
window.teensy = event.port;
event.port.open();
}
}
}
function onMIDIConect(input){
console.log("Input id:", input.id, input);
input.onmidimessage = function(event){
var channel = (event.data[0]-176)+1;
console.log("Parsed", channel);
triggerPlayer(channel);
}
}
function onMIDIReject (error){
console.error(error);
return;
}
function makeMidiMsg(ch, state){
var controlByte = 0x01;
var stateByte = state ? 0x7F : 0x00;
var channelByte = 0xB0 + ch;
return [channelByte, controlByte, stateByte]
}
window.switchOnLED = function(ch){
console.log('setting led', ch);
if (window.teensy && typeof window.teensy.send === 'function'){
var channels = [0,1,2];
channels.forEach(function(thisCh){
window.teensy.send(makeMidiMsg(thisCh,thisCh === ch), window.performance.now())
});
}else{
console.log("couldn't find teensy", teensy);
}
}