-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
448 lines (375 loc) · 12.9 KB
/
index.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!--
Copyright (c) 2021, Stereokai
All rights reserved.
This source code is licensed under the GPLv3 license found in the
LICENSE.md file in the root directory of this source tree.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MIDI Fighter Twister Color Designer</title>
<meta name="description" content="Color designer for MIDI Fighter Twister">
<meta name="author" content="Stereokai">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/webmidi.min.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<style>
a[target="_blank"]::after {
content: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAQElEQVR42qXKwQkAIAxDUUdxtO6/RBQkQZvSi8I/pL4BoGw/XPkh4XigPmsUgh0626AjRsgxHTkUThsG2T/sIlzdTsp52kSS1wAAAABJRU5ErkJggg==);
margin: 0 3px;
}
.switch {
position: relative;
display: inline-block;
width: 4ex;
height: 2ex;
border: 1px solid grey;
}
.switch:focus {
outline: 5px auto Highlight;
outline: 5px auto -webkit-focus-ring-color;
}
input[type="checkbox"] {
position: absolute;
opacity: 0;
pointer-events: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.slider:before {
position: absolute;
content: "";
height: 1.7ex;
width: 1.7ex;
left: 0.1ex;
bottom: 0.1ex;
background-color: firebrick;
}
input:checked + .switch > .slider:before {
background-color: limegreen;
}
input:focus + .switch > .slider:before {
outline: 5px auto Highlight;
outline: 5px auto -webkit-focus-ring-color;
}
input:checked + .switch > .slider:before {
transform: translateX(2.1ex);
}
</style>
</head>
<body>
<div style="display: inline-block; position: relative;">
<fieldset style="display: inline-block; position: relative">
<legend>MIDI Fighter Twister Color Designer</legend>
<legend style="position: absolute; bottom: -1.3ex; background: white;">Connection status: <span style="font-weight: bold; color: firebrick" id="status">Disconnected</span></legend>
<p>
<label>Bank
<input id="bank" type="number" min="1" max="4" value="1">
</label>
<button id="copy" style="border: 2px solid; border-radius: 3px">Copy colors to clipboard</button>
<button id="reset">Reset</button>
</p>
<hr>
<div>
<input id="realearnWrap" type="checkbox">
<label for="realearnWrap" class="switch">
<span class="slider"></span>
</label>
<span style="vertical-align: top">Wrap with ReaLearn <code><b>on_activate</b></code> command</span>
</div>
<hr>
<fieldset style="display: inline-block;">
<legend><b>Advanced</b> - See Twister utility</legend>
<p>
<input id="colorChannel" type="number" min="1" max="16" value="2" style="float: left; margin-right: 5px;">
<label for="colorChannel">Color MIDI Channel (default is 2,<br>setting is per knob)</label>
</p>
<p>
<input id="systemChannel" type="number" min="1" max="16" value="4">
<label for="systemChannel>">System MIDI Channel (default is 4)</label>
</p>
</fieldset>
<br>
</fieldset>
<div style="position: absolute; padding: 10px;">
<h3>What is this?</h3>
This is a fun tool for designing color layouts for MIDI Fighter Twister.
<p>Just connect your MFT, and twist the knobs!</p>
<p>Once you are done creating your pretty color scheme, press the "copy colors" button. This will copy a MIDI command (aka message), that you can use to configure MFT to display your color scheme on-demand with your favorite MIDI software, for example, <a href="https://www.helgoboss.org/projects/realearn/" target="_blank" title="Opens in a new tab">ReaLearn</a>.</p>
<p>This tool uses Web MIDI and as of 2021 works on Chrome, Edge and Opera.</p>
<p><a href="https://github.com/stereokai/midi-fighter-twister-color-designer" target="_blank" title="Opens in a new tab">Source code</a><span style="float: right">Made by Stereokai.</span><p>
</div>
</div>
<script>
let isConnected = false;
const statusIndicator = document.querySelector('#status');
class MFT {
static DEVICE_NAME = "Midi Fighter Twister";
static BANKS = 4;
static KNOBS = 16;
static CHANGE_BANK = 127;
static MIN_COLOR_VALUE = 0;
static MAX_COLOR_VALUE = 127;
static MIN_NOTE_VELOCITY = 0.007874015748031496;
static MAX_NOTE_VELOCITY = 1;
static MIN_VELOCITY_SENSITIVE_KNOB_VALUE = 47;
static MAX_VELOCITY_SENSITIVE_KNOB_VALUE = 81;
static RELATIVE_FACTOR = 64;
static FALSE_RELATIVE_DETECTION_THRESHOLD = 3;
// Statuses
static STANDBY = 0;
static LISTENING_FOR_RELATIVE = 1;
static RELATIVE = 2;
static ABSOLUTE = 3;
static FALSE_RELATIVE = 4;
// Events
static CONNECTED = 'connected';
static DISCONNECTED = 'disconnected';
static CONTROL_CHANGE = 'controlchange';
static NOTE_ON = 'noteon';
constructor() {
this.reset();
}
reset() {
this.knobs = Array.from({
length: MFT.KNOBS
}, () => ({
touched: false,
value: 0
}));
this.status = MFT.STANDBY;
this.currentKnob = 0;
this.outOfRangeInput = 0;
this.lastInput = {
knob: 0
}
}
}
const mft = new MFT();
// Linear interpolations
const lerp = (x, y, a) => x * (1 - a) + y * a; // pass range and fraction and get relative number from range
const clamp = (a, min = 0, max = 1) => Math.min(max, Math.max(min, a)); // limit number within range
const invlerp = (x, y, a) => clamp((a - x) / (y - x)); // inverse of lerp (pass number and get fraction) from range
const range = (x1, y1, x2, y2, a) => lerp(x2, y2, invlerp(x1, y1, a)); // interpolate a number between 2 ranges
WebMidi.enable(err => {
if (err) {
console.log("Error: WebMidi could not be enabled.", err);
} else {
WebMidi.addListener(MFT.CONNECTED, e => {
if (e && e.port && e.port.name === MFT.DEVICE_NAME) {
if (getInput()) {
if (isConnected) { return; }
setConnectionStatus(true);
getInput().addListener(MFT.NOTE_ON, 'all', e => {
if (!isConnected) { return; }
const knob = e.data[1];
let value = range(
MFT.MIN_NOTE_VELOCITY,
MFT.MAX_NOTE_VELOCITY,
MFT.MIN_COLOR_VALUE,
MFT.MAX_COLOR_VALUE,
e.velocity
);
value = Math.round(value);
// Change LED color in MFT
setKnobColor(knob, value);
// Infer and update actual bank automatically
inferAndUpdateBank(knob);
// Store knob state
storeKnob(knob, value);
});
getInput().addListener(MFT.CONTROL_CHANGE, 'all', e => {
if (!isConnected) { return; }
if (e.channel == getSystemChannel()) { return; }
const knob = e.controller.number;
let value = e.value;
// Infer and update actual bank automatically
inferAndUpdateBank(knob);
// Identify in real-time if knob is absolute or relative (ENC 3FH/41H):
// Reset scan cycle if user has changed a knob
if (knob != mft.lastInput.knob) {
mft.status = MFT.STANDBY;
}
// Prevent out of range input & absolute knobs identified as relative
if (!isPotentiallyRelative(value)) {
if (mft.status == MFT.LISTENING_FOR_RELATIVE || mft.status == MFT.RELATIVE) {
mft.outOfRangeInput++;
if (mft.outOfRangeInput == MFT.FALSE_RELATIVE_DETECTION_THRESHOLD) {
mft.outOfRangeInput = 0;
mft.status = MFT.FALSE_RELATIVE;
}
return;
}
}
// Identify relative knobs
if (isPotentiallyRelative(value) && mft.status < MFT.ABSOLUTE) {
// Reduce out of range count when we get an in range input
!!mft.outOfRangeInput && mft.outOfRangeInput--;
// Count 3 consecutive "up" or "down" inputs
if (mft.status < MFT.RELATIVE) {
mft.lastInput.knob = knob;
mft.status++;
return;
}
// After 3 consecutive inputs, it's good enough to infer relative
const lastValue = getKnob(knob).value;
// Transform relative value to absolute value
value = clamp(lastValue + (value - MFT.RELATIVE_FACTOR), MFT.MIN_COLOR_VALUE, MFT.MAX_COLOR_VALUE);
} else {
// This knob is absolute
mft.status = MFT.ABSOLUTE;
mft.lastInput.knob = knob;
}
// Change LED color in MFT
setKnobColor(knob, value);
// Store knob state
storeKnob(knob, value);
});
}
}
});
WebMidi.addListener(MFT.DISCONNECTED, e => {
if (e && e.port && e.port.name === MFT.DEVICE_NAME) {
setConnectionStatus(false);
}
});
}
});
function getInput() {
return WebMidi.getInputByName(MFT.DEVICE_NAME);
}
function getOutput() {
return WebMidi.getOutputByName(MFT.DEVICE_NAME);
}
function isRelativeKnobValue(value) {
return value >= 61 && value <= 67;
}
function isPotentiallyRelative(value) {
// These are not actual min and max values but good enough for this purpose
return value >= MFT.MIN_VELOCITY_SENSITIVE_KNOB_VALUE && value <= MFT.MAX_VELOCITY_SENSITIVE_KNOB_VALUE;
}
function decToHex(number) {
return number.toString(16).toUpperCase().padStart(2, '0');
}
function setConnectionStatus(status) {
if (status) {
isConnected = true;
statusIndicator.innerText = "Connected";
statusIndicator.style.color = "limegreen";
} else {
isConnected = false;
statusIndicator.innerText = "Disconnected";
statusIndicator.style.color = "firebrick";
}
}
function getColorChannel() {
return document.querySelector('#colorChannel').value | 0
}
function getSystemChannel() {
return document.querySelector('#systemChannel').value | 0
}
function shouldReaLearnWrap() {
return !!document.querySelector('#realearnWrap').checked;
}
function getChannelMidi(midiChannelIndex) {
const minMidiChannel = 175;
return decToHex(minMidiChannel + midiChannelIndex);
}
function getKnob(knob) {
return mft.knobs[knob % MFT.KNOBS];
}
function storeKnob (knob, value) {
getKnob(knob).value = value;
getKnob(knob).touched = true;
}
function setKnobColor(knob, value) {
getOutput().sendControlChange(knob, value, getColorChannel());
}
function inferAndUpdateBank(knob) {
const currentBank = (knob / MFT.KNOBS | 0) + 1; // Which bank are we on?
setBank(currentBank);
}
function setBank(bank) {
if (bank >= 1 && bank <= MFT.BANKS) {
document.querySelector("#bank").value = bank;
return true;
}
return false;
}
function getBank() {
return document.querySelector('#bank').value | 0;
}
function getBankOffset() {
return (getBank() - 1) * MFT.KNOBS;
}
function getRealKnobIndex(knobIndex) {
return knobIndex + getBankOffset();
}
function getMidiMessage(knobIndex, value) {
return `${getChannelMidi(getColorChannel())} ${decToHex(getRealKnobIndex(knobIndex))} ${decToHex(value)}`;
}
function reaLearnWrap(string) {
return `on_activate:
send_midi_feedback:
- raw: ${string}`;
}
function getColorSchemeMidi() {
return mft.knobs.map((knob, knobIndex) => {
if (!knob.touched) return false;
return getMidiMessage(knobIndex, knob.value);
}).filter(c => c).join(' ');
}
function copyColorScheme() {
let scheme = getColorSchemeMidi();
if (shouldReaLearnWrap()) {
scheme = reaLearnWrap(scheme);
}
if (!scheme) {
window.alert("Nothing to copy, make some changes!");
return;
}
(async () => {
const result = await navigator.permissions.query({
name: 'clipboard-write'
});
if (result.state == 'granted' || result.state == 'prompt') {
try {
await navigator.clipboard.writeText(scheme);
window.alert('Colors MIDI copied to clipboard!');
} catch (err) {
console.error('Error: Failed to copy, ', err);
}
} else {
console.log('Error: no permission to copy');
}
})();
}
function reset() {
mft.reset();
mft.knobs.forEach((knob, knobIndex) => {
getOutput().sendControlChange(getRealKnobIndex(knobIndex), 0, getColorChannel());
});
}
function changeBank(e) {
const bank = (e.target.value | 0) - 1;
getOutput().sendControlChange(bank, MFT.CHANGE_BANK, getSystemChannel());
mft.reset();
}
function toggleReaLearnWrap(e) {
e && e.target.blur();
}
document.querySelector('#copy').onclick = copyColorScheme;
document.querySelector('#reset').onclick = reset;
document.querySelector('#bank').onclick = changeBank;
document.querySelector('#bank').onblur = changeBank;
document.querySelector('#bank').onclick = changeBank;
document.querySelector('#realearnWrap').onclick = toggleReaLearnWrap;
</script>
</body>
</html>