-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.ts
43 lines (31 loc) · 1.25 KB
/
demo.ts
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
import { createTuner, Note } from "./src/"
const canvasEl = document.getElementById("visualizer")! as HTMLCanvasElement;
const noteEl = document.getElementById("note")! as HTMLDivElement;
const tuner = createTuner({
onNote(note: Note) {
noteEl.innerText = JSON.stringify(note, null, 2);
}
})
document.getElementById("start")?.addEventListener("click", () => tuner.start())
document.getElementById("stop")?.addEventListener("click", () => tuner.stop())
const bufferLength = tuner.analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
// Just get the low end of the spectrum
const displayLength = Math.sqrt(bufferLength) * 2;
const ctx = canvasEl.getContext("2d")!;
canvasEl.width = canvasEl.offsetWidth
canvasEl.height = canvasEl.offsetHeight
function visualize() {
tuner.analyser.getByteFrequencyData(dataArray);
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
const barWidth = (canvasEl.width / displayLength);
let x = 0;
for (let i = 0; i < displayLength; i++) {
const barHeight = canvasEl.height * (dataArray[i] / 255);
ctx.fillStyle = `rgb(60 20 ${barHeight + 50})`;
ctx.fillRect(x, canvasEl.height - barHeight, barWidth, barHeight);
x += barWidth;
}
requestAnimationFrame(visualize);
}
visualize();