-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.js
70 lines (62 loc) · 2.06 KB
/
visualizer.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
function visualizer(audio, canvas, options) {
if(!audio || !canvas)
return console.log('Please pass in an audio and canvas node');
// setup visualizer
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var canvasCtx = canvas.getContext('2d');
var analyzer = audioCtx.createAnalyser();
var source = audioCtx.createMediaElementSource(audio);
source.connect(analyzer);
source.connect(audioCtx.destination);
var animationRequest = null;
var resizeTimeoutRequest = null;
// process options
options = options || {};
var barWidth = options.barWidth || 4;
var barSpacing = options.barSpacing || 1;
analyzer.fftSize = options.fftSize || 1024;
var resizeDelay = options.resizeDelay || 200;
// more setup
var bufferLength = analyzer.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
function render() {
animationRequest = requestAnimationFrame(render);
var width = canvas.width;
var height = canvas.height;
var x = 0;
var barHeight;
var energy;
canvasCtx.clearRect(0, 0, width, height);
analyzer.getByteFrequencyData(dataArray);
for(var i = 0; x + barWidth + barSpacing < width && i < bufferLength; i++) {
energy = dataArray[i] - 128;
barHeight = Math.round(energy / 127.0 * height);
canvasCtx.fillStyle = 'rgb(82, 139,' + dataArray[i] + ')';
canvasCtx.fillRect(x, height - barHeight, barWidth, height);
x += barWidth + barSpacing;
}
}
function updateSize() {
canvas.height = canvas.parentNode.clientHeight;
canvas.width = canvas.parentNode.clientWidth;
}
var instance = {};
instance.pause = function() {
if (animationRequest) {
cancelAnimationFrame(animationRequest);
animationRequest = null;
}
};
instance.start = function() {
console.log('resizing visualizer');
updateSize();
render();
};
instance.resize = function() {
instance.pause();
clearTimeout(resizeTimeoutRequest);
resizeTimeoutRequest = setTimeout(instance.start, resizeDelay);
};
return instance;
}
export default visualizer;