-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscilloscope.js
68 lines (59 loc) · 1.37 KB
/
oscilloscope.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
"use strict";
(function() {
var
max,
PI2 = Math.PI / 2,
pinchPerc = 1 / 2,
HeightPerc = 1 / 4
;
ui.visualizerAdd(
"Oscilloscope", "2d",
function( info ) {
var
dat,
y,
i = 0,
ctx = info.ctxCanvas,
w = ctx.canvas.width,
h = ctx.canvas.height,
data = info.data,
len = data.length,
base = len * pinchPerc,
scaling = h * HeightPerc,
mult = w / len
;
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle =
"rgba(" +
Math.round( 255 - max * 255 ) + "," +
Math.round( max * 64 ) + "," +
Math.round( max * 255 ) + "," +
( .95 - .25 * ( 1 - Math.cos( max * PI2 ) ) ) +
")"
;
ctx.fillRect( 0, 0, w, h );
max = 0;
info.analyser.getByteTimeDomainData( data );
ctx.globalCompositeOperation = "source-over";
ctx.save();
ctx.translate( 0, h / 2 );
ctx.beginPath();
ctx.moveTo( 0, 0 );
for ( ; i < len; ++i ) {
dat = ( -128 + data[ i ] ) / 128;
max = Math.max( Math.abs( dat ), max );
y = dat * scaling;
if ( i < base || i >= len - base ) {
// Pinch the extremities.
y *= .5 - Math.cos( ( i < base ? i : len - i ) / base * Math.PI ) / 2;
}
ctx.lineTo( i * mult, y );
}
ctx.lineJoin = "round";
ctx.lineWidth = 1 + Math.round( 3 * max );
ctx.strokeStyle = "rgba(255,255,255," + Math.min( max * 3, 1 ) + ")";
ctx.stroke();
ctx.restore();
}
);
})();