-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrogram.js
56 lines (43 loc) · 1.98 KB
/
spectrogram.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
let spectrogramCanvas;
let spectrogramContext;
let analyserSpectrogram;
function initSpectrogram(context, filter) {
spectrogramCanvas = document.getElementById('spectrogramCanvas');
spectrogramContext = spectrogramCanvas.getContext('2d');
analyserSpectrogram = new AnalyserNode(context);
filter.connect(analyserSpectrogram);
analyserSpectrogram.fftSize = 2048;
drawSpectrogram();
}
function drawSpectrogram() {
const bufferLength = analyserSpectrogram.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
const canvasWidth = spectrogramCanvas.width;
const canvasHeight = spectrogramCanvas.height;
function renderFrame() {
requestAnimationFrame(renderFrame);
analyserSpectrogram.getByteFrequencyData(dataArray);
spectrogramContext.fillStyle = 'rgb(255, 255, 255)';
spectrogramContext.fillRect(0, 0, canvasWidth, canvasHeight);
const barWidth = canvasWidth / bufferLength;
let x = 0;
for (let i = 0; i < bufferLength; i++) {
const barHeight = dataArray[i] * canvasHeight / 256;
// Create a linear gradient for the fill style
const gradient = spectrogramContext.createLinearGradient(0, canvasHeight - barHeight, 0, canvasHeight);
gradient.addColorStop(0, 'rgb(' + (dataArray[i] + 100) + ', 50, 50)');
gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
spectrogramContext.fillStyle = gradient;
// Use fill() method instead of fillRect() to draw the bars
spectrogramContext.beginPath();
spectrogramContext.moveTo(x, canvasHeight - barHeight);
spectrogramContext.lineTo(x, canvasHeight);
spectrogramContext.lineTo(x + barWidth, canvasHeight);
spectrogramContext.lineTo(x + barWidth, canvasHeight - barHeight);
spectrogramContext.closePath();
spectrogramContext.fill();
x += barWidth + 1;
}
}
renderFrame();
}