-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.html
158 lines (140 loc) · 3.94 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<title>MIDI test</title>
<script src="https://cdn.jsdelivr.net/npm/jzz"></script>
<script src="https://cdn.jsdelivr.net/npm/jzz-synth-tiny"></script>
</head>
<body>
<h1>MIDI test</h1>
<p>
MIDI Out: <select id=selectmidiout></select>
</p><p>
Send MIDI (e.g. <tt>c0 10, 90 40 7f</tt>):
<input id=textinput></input>
<span id=reportspan></span>
</p>
<pre id=textoutput></pre>
<script><!--
var selectMidiOut = document.getElementById('selectmidiout');
var textInput = document.getElementById('textinput');
var textOutput = document.getElementById('textoutput');
var reportSpan = document.getElementById('reportspan');
var midiOutName = 'Not available';
var midiOutPort;
var output = [];
var previous = ['90 40 7f'];
var ptr = -1;
function setListbox(lb, s) {
for (var i = 0; i < lb.options.length; i++) if (lb.options[i].value == s) lb.options[i].selected = 1;
}
function onMidiOutSuccess() {
if (midiOutPort) {
midiOutPort.close();
}
midiOutPort = this;
midiOutName = this.name();
setListbox(selectMidiOut, midiOutName);
}
function onMidiOutFail() {
setListbox(selectMidiOut, midiOutName);
}
JZZ.synth.Tiny.register('Web Audio');
JZZ().and(function(){
var i;
for (i = 0; i < this.info().outputs.length; i++) {
selectMidiOut[i] = new Option(this.info().outputs[i].name);
}
if (!i) {
selectMidiOut[i] = new Option('Not available');
}
for (i = 0; i < this.info().inputs.length; i++) {
openMidiIn(this.info().inputs[i].name);
}
});
JZZ().openMidiOut().or(onMidiOutFail).and(onMidiOutSuccess);
function openMidiIn(name) {
JZZ().openMidiIn(name).connect(function(msg) {
print(name + ' => ' + format(msg));
});
}
function changeMidiOut() {
var name = selectMidiOut.options[selectMidiOut.selectedIndex].value;
if (name == midiOutName) return;
JZZ().openMidiOut(name).or(onMidiOutFail).and(onMidiOutSuccess);
}
function changeInput(e) {
if (e.which == 13) {
reportSpan.innerHTML = '';
var i;
try {
var group = parse(textInput.value.trim());
textInput.value = '';
ptr = -1;
var str = '';
for (i = 0; i < group.length; i++) {
if (str.length) str += ', ';
str += format(group[i]);
}
if (!previous.length || previous[previous.length - 1] != str) previous.push(str);
for (i = 0; i < group.length; i++) {
print(midiOutName + ' <= ' + format(group[i]));
if (midiOutPort) midiOutPort.send(group[i]);
}
}
catch (s) {
reportSpan.innerHTML = s.toString().replace(/&/, '&').replace(/</, '<').replace(/>/, '>');
}
}
else if (e.keyCode == 27) {
ptr = -1;
textInput.value = '';
}
else if (e.keyCode == 38) {
if (previous.length) {
if (ptr == -1) ptr = previous.length;
if (ptr) ptr--;
textInput.value = previous[ptr];
}
}
else if (e.keyCode == 40) {
if (previous.length && ptr != -1) {
if (ptr < previous.length - 1) ptr++;
textInput.value = previous[ptr];
}
}
}
function parse(input) {
if (!input) return;
var i, j, chunk, msg, x;
var group = [];
var data = input.split(/,\s*/);
for (i = 0; i < data.length; i++) {
if (data[i] == '') continue;
chunk = data[i].split(/\s+/);
if (!chunk.length) continue;
msg = [];
for (j = 0; j < chunk.length; j++) {
x = parseInt(chunk[j], 16);
if (x >= 0 && x <= 255) msg.push(x);
else {
throw('Invalid hex MIDI value:', chunk[j]);
}
}
if (msg.length) group.push(msg);
}
return group;
}
function format(msg) {
return msg.map(function(x) { return x < 16 ? '0' + x.toString(16) : x.toString(16); }).join(' ');
}
function print(str) {
output.push(str.replace(/&/, '&').replace(/</, '<').replace(/>/, '>'));
while (output.length > 10) output.shift();
textOutput.innerHTML = output.join('<br>');
}
selectMidiOut.addEventListener('change', changeMidiOut);
textInput.addEventListener('keydown', changeInput);
--></script>
</body>
</html>