-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
143 lines (117 loc) · 3.2 KB
/
index.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
var formToCCs = {
"aDecay": 54,
"fDecay": 53,
"sustain": 64,
"fCut": 52,
"wave": 51,
"detune": 50,
"octave": 65,
"lDepth": 48,
"lRate": 49,
"lDest": 67,
"pwmSweep": 66,
"glide": 55,
"vcfEnvAmount": 56,
"lRand": 68,
"lNoteRetrig": 69,
"oscBWave": 70
};
var midi = {
onMIDISuccess: function(midiAccess) {
console.log('MIDI Access Object', midiAccess);
//console.log(this);
var self = this;
midiAccess.onstatechange = function(e) {
self.onMIDIAccessChange(e);
}
this.midiAccess = midiAccess;
this.inputs = {};
this.outputs = {};
this.initPorts();
},
initPorts: function() {
var self = this;
var inputs = this.midiAccess.inputs;
if (inputs.size > 0) {
inputs.forEach(
function(port, key) {
//console.log(port);
self.registerPort(port);
}
);
} else {
}
var outputs = this.midiAccess.outputs;
if (outputs.size > 0) {
outputs.forEach(
function(port, key) {
self.registerPort(port);
self.renderPort(port);
}
);
} else {
}
},
onMIDIAccessChange: function(e) {
console.log("MIDI access change: " + e);
//console.log(this);
var port = e.port;
if (port.type == "input") {
if (this.inputs[port.name] === undefined) {
this.registerPort(port);
}
} else {
if (this.outputs[port.name] === undefined) {
this.registerPort(port);
}
this.renderPort(port);
}
},
renderPort: function(port) {
var portOptions = $("#port").children();
if (port.state == "connected") {
if ((portOptions.length == 1) && (portOptions.get(0).text == "none")) {
$("#port").empty();
}
if (!$("#" + port.type + port.id).length) {
var html = '<option id="' + port.type + port.id + '" value="' + port.name + '">' + port.name + '</option>';
$("#port").append(html);
}
} else {
$("#" + port.type + port.id).remove();
if ($("#port").children().length == 0) {
$("#port").append('<option value="none">none</option>')
}
}
},
registerPort: function(port) {
var self = this;
if (port.type == "input") {
this.inputs[port.name] = port;
port.onmidimessage = function(m) { self.onMIDIMessage(m); };
} else {
this.outputs[port.name] = port;
}
port.onstatechange = function(e) { self.onPortStateChange(e); };
},
onMIDIFailure: function(e) {
// when we get a failed response, run this code
alert("No access to MIDI devices or your browser doesn't support WebMIDI API. Please use WebMIDIAPIShim " + e);
},
onPortStateChange: function(event) {
console.log("port state change: " + event);
},
onMIDIMessage: function(message) {
//console.log(message);
}
};
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess({
sysex: false
}).then(
function(midiAccess) { midi.onMIDISuccess(midiAccess); },
function(e) { midi.onMIDIFailure(e); }
);
} else {
alert("No MIDI support in your browser.");
}