-
Notifications
You must be signed in to change notification settings - Fork 3
/
midi-scratchpad.html
353 lines (314 loc) · 10.7 KB
/
midi-scratchpad.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>MIDI Scratchpad</title>
<script crossorigin="anonymous"
integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ=="
referrerpolicy="no-referrer" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
td {
vertical-align: top
}
td:first-child {
width: 200px;
}
label {
display: block;
}
textarea {
display: block;
}
textarea,
tt,
pre,
code {
font-family: Menlo, Consolas, mono-space;
font-size: 12px;
}
.statusBar {
background: #eee;
line-height: 18px;
padding: 5px;
font-size: 12px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
}
.statusBar b {
margin: 0 4px;
}
.statusBar tt {
display: inline-block;
background: #fff5;
border: 1px solid #0003;
white-space: pre;
padding: 0 2px;
width: 30px;
}
</style>
</head>
<body>
<h1>MIDI Scratchpad</h1>
<p>Send arbitrary messages to MIDI devices over WebMIDI.</p>
<table>
<tr>
<td>
<label for="midiOutput">MIDI Device:</label>
</td>
<td>
<select id="midiOutput"></select>
</td>
</tr>
<tr>
<td>
<label for="sysexBytes">Raw Data:</label>
<small>Enter hex-encoded MIDI message data. Separate messages by <b>;</b> symbol. Whitespace is ignored. Data is
persisted to localstorage.</small>
</td>
<td>
<textarea cols=80 id="sysexBytes" rows=20>f0 43 76 00 00 00 00 01 00 01 01 0c 01 04 04 07 00 00 02 07 02 0f 00 0b 01 05 00 00 00 00 01 07 00 0a 07 08 02 00 09 0f 00 0b 02 0e 00 03 00 06 00 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</textarea>
<div class="statusBar">
<b>Pos:</b>
<tt id="cursorBytePos"> </tt>
<b>Byte:</b>
Hex <tt id="cursorByteHex"> </tt>
Dec <tt id="cursorByteDec"> </tt>
<b>High/Low Pair:</b>
Hex <tt id="cursorBytePairHex"> </tt>
Dec <tt id="cursorBytePairDec"> </tt>
</div>
</td>
</tr>
<tr>
<td>
<label for="midiChecksumMethod">Checksum method:</label>
</td>
<td>
<select id="midiChecksumMethod">
<option value="yamaha">Yamaha</option>
</select>
</td>
</tr>
<tr>
<td>Total Checksum:</td>
<td>
<tt id="checksum"></tt>
</td>
</tr>
<tr>
<td>
<label for="midiChecksumMethod">Line Checksum:</label>
<small>Checksum is calculated for the line where the cursor is positioned in raw data.</small>
</td>
<td>
<tt id="checksumLine"></tt>
</td>
</tr>
<tr>
<td></td>
<td>
<button id="send">Send</button>
<button id="sendDsr">Send with DSR checksum</button>
</td>
</tr>
</table>
<script>
/****** MIDI device code ******/
let midi = null; // global MIDIAccess object
let midiOutput = null;
let midiOutputSelectorEl = document.querySelector('#midiOutput');
navigator.requestMIDIAccess({ sysex: true }).then(onMIDISuccess);
midiOutputSelectorEl.addEventListener('change', ev => {
midiOutput = midi.outputs.get(midiOutputSelectorEl.value);
localStorage.setItem('midiOutputDevice', midiOutputSelectorEl.value);
});
function onMIDISuccess(midiAccess) {
const logMidi = o => console.debug(`[MIDI ${o.type}] id: ${o.id.padEnd(14, ' ')} ` +
`manufacturer: ${o.manufacturer.padEnd(20, ' ')} name: ${o.name.padEnd(20, ' ')}`);
for (var entry of midiAccess.inputs) logMidi(entry[1]);
for (var entry of midiAccess.outputs) logMidi(entry[1]);
let midiOutputHtml = '';
for (let entry of midiAccess.outputs) {
let output = entry[1];
midiOutputHtml += `
<option value='${output.id}'>${output.name}</option>`;
}
midiOutputSelectorEl.innerHTML = midiOutputHtml;
midi = midiAccess;
const storedMidiDevice = localStorage.getItem('midiOutputDevice');
if (storedMidiDevice) {
midiOutputSelectorEl.value = storedMidiDevice;
midiOutputSelectorEl.dispatchEvent(new Event('change'));
}
}
/***** GUI code *****/
const sysexBytesEl = document.querySelector('#sysexBytes');
sysexBytesEl.addEventListener('input', onSysexBytesChanged);
const sendButtonEl = document.querySelector('#send');
sendButtonEl.addEventListener('click', onSendClicked);
const sendDsrButtonEl = document.querySelector('#sendDsr');
sendDsrButtonEl.addEventListener('click', onSendDsrClicked);
const checksumEl = document.querySelector('#checksum');
const checksumLineEl = document.querySelector('#checksumLine');
const cursorBytePosEl = document.querySelector('#cursorBytePos');
const cursorByteDecEl = document.querySelector('#cursorByteDec');
const cursorByteHexEl = document.querySelector('#cursorByteHex');
const cursorBytePairDecEl = document.querySelector('#cursorBytePairDec');
const cursorBytePairHexEl = document.querySelector('#cursorBytePairHex');
let currentLineText = '';
if (localStorage.getItem('sysexText')) {
sysexBytesEl.value = localStorage.getItem('sysexText');
}
// Populate initial checksum
onSysexBytesChanged();
function onSendClicked(e) {
// const bytes = textToYamahaSysex(sysexBytesEl.value);
const values = sysexBytesEl.value.split(';');
let delayMs = 0;
for (let value of values) {
const bytes = hexToBytes(value);
setTimeout(() => midiOutput.send(bytes), delayMs);
delayMs += 500;
}
}
function onSendDsrClicked(e) {
const values = sysexBytesEl.value.split(';');
let delayMs = 0;
for (let value of values) {
let bytes;
if (value.match(/^f0/i)) { // sysex message
const lines = value.split(',');
const header = hexToBytes(lines[0]);
const voiceNum = hexToBytes(lines[1]);
const data = hexToBytes(lines[2]);
const innerChecksum = splitByte(checksum(joinBytePairs(data)));
const outerChecksum = yamahaChecksum([...voiceNum, ...data, ...innerChecksum]);
bytes = [...header, ...voiceNum, ...data, ...innerChecksum, outerChecksum, 0xf7];
console.log('Sending composed DSR-2000 message...', bytesToHex(bytes));
} else {
bytes = hexToBytes(value);
}
setTimeout(() => midiOutput.send(bytes), delayMs);
delayMs += 500;
}
}
function onSysexBytesChanged(e) {
let text, checksum;
text = sysexBytesEl.value;
checksum = '';
try {
const bytes = hexToBytes(text);
checksum = bytesToHex([yamahaChecksum(bytes)]);
} catch (e) {
checksum = `-- (${e.message})`;
}
checksumEl.innerHTML = checksum;
localStorage.setItem('sysexText', text);
}
function onSysexBytesCursorUpdate(e) {
const val = sysexBytesEl.value;
const cursorPos = sysexBytesEl.selectionStart;
const cursorLinePos = val.substr(0, cursorPos).split('\n').length - 1;
// Get byte index of cursor position
let cursorBytePos = cursorPos;
let valCleaned = '';
for (let i = 0; i < cursorPos; i++) {
if (!'abcdefABCDEF0123456789'.includes(val[i])) cursorBytePos--;
}
cursorBytePos = cursorBytePos >> 1; // Divide by 2
cursorBytePosEl.innerHTML = cursorBytePos;
const bytes = hexToBytes(val);
if (cursorBytePos < bytes.length) {
const cursorByte = bytes[cursorBytePos];
cursorByteHexEl.innerHTML = bytesToHex([cursorByte]).padStart(2, '0');
cursorByteDecEl.innerHTML = cursorByte;
}
if (cursorBytePos < bytes.length - 2) {
const cursorBytePairPos = ((cursorBytePos - 1) >> 1) * 2 + 1;
const cursorBytePair = (bytes[cursorBytePairPos] << 4) + bytes[cursorBytePairPos + 1];
cursorBytePairHexEl.innerHTML = bytesToHex([cursorBytePair]).padStart(2, '0');
cursorBytePairDecEl.innerHTML = cursorBytePair;
} else {
cursorBytePairHexEl.innerHTML = '--';
cursorBytePairDecEl.innerHTML = '--';
}
const cursorLine = val.split('\n')[cursorLinePos];
const lineBytes = hexToBytes(cursorLine);
let checksum = '';
try {
checksum = bytesToHex([yamahaChecksum(lineBytes)]);
} catch (e) {
checksum = `-- (${e.message})`;
}
checksumLineEl.innerHTML = checksum;
}
// onSysexBytesCursorUpdate = _.debounce(onSysexBytesCursorUpdate, 33);
sysexBytesEl.addEventListener('click', onSysexBytesCursorUpdate);
sysexBytesEl.addEventListener('keydown', onSysexBytesCursorUpdate);
sysexBytesEl.addEventListener('keyup', onSysexBytesCursorUpdate);
function textToYamahaSysex(text) {
const bytes = hexToBytes(text);
if (bytes.length < 5) throw new Error('Message must be greater than 4 bytes.');
const checksum = yamahaChecksum(bytes);
bytes.push(checksum, 0xF7);
return bytes;
}
function hexToBytes(text) {
const bytes = [];
let hex = text.toLowerCase().replace(/[^abcdef0123456789]/gi, '');
if (hex.length % 2 != 0) hex += '0';
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return bytes;
}
function bytesToHex(bytes) {
if (!Array.isArray(bytes)) bytes = [bytes];
for (var hex = [], i = 0; i < bytes.length; i++) {
var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
hex.push((current >>> 4).toString(16));
hex.push((current & 0xF).toString(16));
}
return hex.join('');
}
// TODO...
function joinBytePairs(bytes) {
if (bytes.length % 2 != 0) throw new Error('Byte length must be multiple of 2.');
const joined = [];
for (let i = 0; i < bytes.length; i += 2) {
joined.push((bytes[i] << 4) + bytes[i + 1]);
}
// console.log('Raw bytes (high/low nibble):\n', bytesToHex(bytes));
// console.log('Joined bytes:\n', bytesToHex(joined));
return joined;
}
function splitByte(byte) {
if (byte < 0) byte += 256;
return [(byte >> 4) & 0x0f, byte & 0x0f];
}
function yamahaChecksum(bytes) {
// http://www.muzines.co.uk/articles/everything-you-ever-wanted-to-know-about-system-exclusive/5722
// checksum = ((NOT(sum AND 255)) AND 127)+1
let checksum = 0;
for (let i = 0; i < bytes.length; i++) {
checksum += bytes[i] || 0;
}
checksum = (~(checksum & 0xFF) + 1) & 0x7F;
return checksum;
}
function checksum(bytes) {
let checksum = 0;
for (let i = 0; i < bytes.length; i++) {
checksum += bytes[i] || 0;
}
// checksum = (~(checksum & 0xFF) + 1) & 0x7F;
checksum = ~(checksum & 0xFF);
return checksum;
}
</script>
</body>
</html>