-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoormidi.js
170 lines (149 loc) · 5.06 KB
/
poormidi.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// https://github.com/tadfmac/poormidi
// CCby4.0 D.F.Mac.TripArts Music.
// poormidi.js (Very Poor) Web MIDI API Wrapper
// For Google Chrome Only :D
// 2015.02.23 Change for P&P on Chrome Canary !!!! (W.I.P. and experimental now)
// 2015.06.07 P&P feature has now supported.
// 2015.08.08 add sendCtlChange()
// 2015.08.08 On sendNoteOn(), sendNoteOff() and sendCtlChange(),
// arguments are now able to set midi-channel,
// and also these can be omitted.
// See also comments in this code, for details.
(function () {
poormidi = function(){
this.midi = null;
this.inputs = [];
this.outputs = [];
this.timer = null;
this.success = function(access){
console.log("poormidi.success()");
this.midi = access;
this.refreshPorts();
this.midi.onstatechange = this.onStateChange;
}.bind(this);
this.failure = function(msg){
console.log("poormidi.failure(): "+msg);
}.bind(this);
this.onMidiEvent = function(e){
console.log("poormidi.onMidiEvent()");
}.bind(this);
this.setHandler = function(func){
console.log("poormidi.setHandler()");
this.onMidiEvent = func.bind(this);
}.bind(this);
this.sendNoteOn = function(){
console.log("poormidi.sendNoteOn()");
var note = 0;
var velocity = 100;
var channel = 0;
if(arguments.length == 1){
// midi.sendNoteOn(note);
note = arguments[0];
}else if(arguments.length == 2){
// midi.sendNoteOn(note,velocity);
note = arguments[0];
velocity = arguments[1];
}else if(arguments.length == 3){
// midi.sendNoteOn(channel,note,velocity);
channel = arguments[0] & 0x0f;
note = arguments[1];
velocity = arguments[2];
}else{
console.log("poormidi.sendNoteOn:parameter error!!");
return;
}
if(this.outputs.length > 0){
for(var cnt=0;cnt<this.outputs.length;cnt++){
console.log("poormidi.sendNoteOn() output to :"+this.outputs[cnt].name);
this.outputs[cnt].send([0x90|channel,note&0x7f,velocity&0x7f]);
}
}
}.bind(this);
this.sendNoteOff = function(){
console.log("poormidi.sendNoteOff()");
var note = 0;
var channel = 0;
if(arguments.length == 1){
// midi.sendNoteOff(note);
note = arguments[0];
}else if(arguments.length == 2){
// midi.sendNoteOff(channel,note);
channel = arguments[0] & 0x0f;
note = arguments[1];
}else{
console.log("poormidi.sendNoteOff:parameter error!!");
return;
}
if(this.outputs.length > 0){
for(var cnt=0;cnt<this.outputs.length;cnt++){
console.log("poormidi.sendNoteOff() output to :"+this.outputs[cnt].name);
this.outputs[cnt].send([0x80|channel,note,0]);
}
}
}.bind(this);
this.sendCtlChange = function(){
console.log("poormidi.sendCtlChange()");
var channel = 0;
var number = 0;
var value = 0;
if(arguments.length == 2){
// midi.sendCtlChange(number,value);
number = arguments[0];
value = arguments[1];
}else if(arguments.length == 3){
// midi.sendCtlChange(channel,number,value);
channel = arguments[0] & 0x0f;
number = arguments[1];
value = arguments[2];
}else{
console.log("poormidi.sendCtlChange:parameter error!!");
return;
}
if(this.outputs.length > 0){
for(var cnt=0;cnt<this.outputs.length;cnt++){
console.log("poormidi.sendCtlChange() output to :"+this.outputs[cnt].name);
this.outputs[cnt].send([0xB0|channel,number&0x7f,value&0x7f]);
}
}
}.bind(this);
this.onStateChange = function(){
console.log("poormidi.onStateChange()");
if(this.timer != null){
clearTimeout(this.timer);
}
this.timer = setTimeout(function(){
this.refreshPorts();
this.timer = null;
}.bind(this),300);
}.bind(this);
this.refreshPorts = function(){
console.log("poormidi.refreshPorts()");
this.inputs = [];
this.outputs = [];
// inputs
var it = this.midi.inputs.values();
for(var o = it.next(); !o.done; o = it.next()){
this.inputs.push(o.value);
console.log("input port: "+o.value.name);
}
console.log("poormidi.refreshPorts() inputs: "+this.inputs.length);
for(var cnt=0;cnt<this.inputs.length;cnt++){
this.inputs[cnt].onmidimessage = this.onMidiEvent;
}
// outputs
var ot = this.midi.outputs.values();
for(var o = ot.next(); !o.done; o = ot.next()){
this.outputs.push(o.value);
console.log("output port: "+o.value.name);
}
console.log("poormidi.refreshPorts() outputs: "+this.outputs.length);
}.bind(this);
this.onConnect = function(e){
console.log("poormidi.onConnect()");
}
this.onDisConnect = function(e){
console.log("poormidi.onDisConnect()");
}
navigator.requestMIDIAccess().then(this.success,this.failure);
};
}).call(this);