-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (46 loc) · 2.14 KB
/
index.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
window.addEventListener('load', async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const context = new AudioContext();
const source = context.createMediaStreamSource(stream);
const analyser = context.createAnalyser();
source.connect(analyser);
analyser.connect(context.destination);
const height = 20;
const byteFrequencyData = new Uint8Array(analyser.frequencyBinCount);
const byteFrequencyDataCanvas = document.getElementById('byteFrequencyDataCanvas');
byteFrequencyDataCanvas.width = analyser.frequencyBinCount;
byteFrequencyDataCanvas.height = height;
const byteFrequencyDataContext = byteFrequencyDataCanvas.getContext('2d');
byteFrequencyDataContext.strokeWidth = 1;
const byteTimeDomainData = new Uint8Array(analyser.fftSize);
const byteTimeDomainDataCanvas = document.getElementById('byteTimeDomainDataCanvas');
byteTimeDomainDataCanvas.width = analyser.fftSize;
byteTimeDomainDataCanvas.height = height;
const byteTimeDomainDataContext = byteTimeDomainDataCanvas.getContext('2d');
byteTimeDomainDataContext.strokeWidth = 1;
window.requestAnimationFrame(function tick() {
analyser.getByteFrequencyData(byteFrequencyData);
for (let index = 0; index < byteFrequencyData.length; index++) {
const byte = byteFrequencyData[index];
byteFrequencyDataContext.strokeStyle = `rgb(${byte}, ${byte}, ${byte})`;
byteFrequencyDataContext.beginPath();
byteFrequencyDataContext.moveTo(index, 0);
byteFrequencyDataContext.lineTo(index, height);
byteFrequencyDataContext.stroke();
}
analyser.getByteTimeDomainData(byteTimeDomainData);
for (let index = 0; index < byteTimeDomainData.length; index++) {
const byte = byteTimeDomainData[index];
byteTimeDomainDataContext.strokeStyle = `rgb(${byte}, ${byte}, ${byte})`;
byteTimeDomainDataContext.beginPath();
byteTimeDomainDataContext.moveTo(index, 0);
byteTimeDomainDataContext.lineTo(index, height);
byteTimeDomainDataContext.stroke();
}
window.requestAnimationFrame(tick);
});
} catch (error) {
alert(error.toString());
}
});