-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.js
103 lines (83 loc) Β· 2.48 KB
/
main.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
import { MidiClockInput, calculateBars } from "./midiclock.js";
import { StackedPolygonVisualizer } from "./viz.js";
const mainMessageEl = document.getElementById("main-msg");
const sideMessageEl = document.getElementById("side-msg");
const sourceSelectEl = document.getElementById("source-select");
const canvasEl = document.getElementById("main-canvas");
let midiclock = undefined;
function showMessage(msg) {
mainMessageEl.innerText = msg;
}
function populateMidiInputs(inputs) {
sourceSelectEl.onchange = (event) => selectMidiInput(event.target.value);
let first = true;
for (const input of inputs) {
const option = document.createElement("option");
option.text = `${input.manufacturer} ${input.name}`;
option.value = input.id;
if (first) {
option.selected = true;
selectMidiInput(input.id);
first = false;
}
sourceSelectEl.options.add(option);
}
}
function selectMidiInput(id) {
showMessage("WAITING FOR MIDI CLOCK SIGNAL");
midiclock.listenTo(id);
}
class ClockMessageTextVisualizer {
constructor(mainMessageEl, sideMessageEl) {
this.mainMessageEl = mainMessageEl;
this.sideMessageEl = sideMessageEl;
this.stopped = true;
}
start() {
this.stopped = false;
}
stop() {
this.stopped = true;
}
onClockMessage(msg) {
if (this.stopped) {
return;
}
const { fraction, beat, bar, segment } = calculateBars(msg.note);
if (msg.synchronized) {
this.mainMessageEl.innerText = `${segment}.${bar}.${beat}`;
} else {
const fractionFormatted = fraction.toFixed(2).slice(2);
this.mainMessageEl.innerText = `UNSYNCHRONIZED PHASE .${fractionFormatted}`;
}
if (msg.bpm) {
this.sideMessageEl.innerText = `${msg.bpm.toFixed(2)} BPM`;
}
}
}
(async function main() {
const textViz = new ClockMessageTextVisualizer(mainMessageEl, sideMessageEl);
textViz.start();
const viz = new StackedPolygonVisualizer(canvasEl);
viz.start();
let isRendering = true;
canvasEl.addEventListener("click", () => {
if (isRendering) {
viz.stop();
isRendering = false;
} else {
viz.start();
isRendering = true;
}
});
showMessage("REQUESTING MIDI ACCESS");
midiclock = new MidiClockInput();
midiclock.onClockMessage = (msg) => {
textViz.onClockMessage(msg);
viz.onClockMessage(msg);
};
await midiclock.requestAccess();
showMessage("NO MIDI INPUTS FOUND");
const inputs = await midiclock.getInputs();
populateMidiInputs(inputs);
})();