-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvumeter.html
148 lines (136 loc) · 4.88 KB
/
vumeter.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<!-- Analogue VU Meter (c) 2020 [email protected] -->
<html>
<head>
<meta charset="UTF-8">
<script lang="Javascript">
function SprungMass( mass, spring, damping ) {
var x = 0, v = 0;
// Model a sprung mass for the specified number of milliseconds.
this.model = function( force, xMin, xMax, millis ) {
for( var t = 0; t < millis; t++ ) {
if( x < xMin ) {
x = xMin;
if( v < 0 ) {
v = -v;
}
}
if( x > xMax ) {
x = xMax;
if( v > 0 ) {
v = -v;
}
}
var a = ( force - spring * x - damping * v ) / mass; // Acceleration due to force.
v = v + a * 0.001; // Change in velocity due to acceleration.
x = x + v * 0.001; // Change in displacement due to velocity.
}
return x;
}
}
function init() {
leftSpring = new SprungMass( 0.005, 1, 0.08 );
rightSpring = new SprungMass( 0.005, 1, 0.08 );
leftForce = 0;
rightForce = 0;
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
drawMeter( context, canvas.width / 2 );
background = context.getImageData( 0, 0, canvas.width / 2, canvas.width / 4 );
time = performance.now();
requestAnimationFrame(repaint);
navigator.mediaDevices.getUserMedia( { audio:true, video:false } ).then( openAudio );
}
function openAudio( mediaStream ) {
var audioContext = new AudioContext();
var mediaStreamSource = audioContext.createMediaStreamSource( mediaStream );
var scriptProcessor = audioContext.createScriptProcessor( 1024, 2, 0 );
scriptProcessor.onaudioprocess = onAudioProcess;
mediaStreamSource.connect( scriptProcessor );
}
function drawMeter( context, width ) {
// Draw background.
var gradient = context.createLinearGradient( 0, 0, 0, canvas.height );
gradient.addColorStop(0, '#806633');
gradient.addColorStop(1, '#FFCC66');
context.fillStyle = gradient;
context.fillRect(0, 0, width, width / 2 );
// Draw bottom box.
context.fillStyle = 'black';
context.fillRect( width * 5 / 16, width * 6 / 16, width * 6 / 16, 1 );
context.fillRect( width * 5 / 16, width * 7 / 16, width * 6 / 16, 1 );
context.fillRect( width * 5 / 16, width * 6 / 16, 1, width / 16 );
context.fillRect( width * 11 / 16, width * 6 / 16, 1, width / 16 );
// Draw ruler.
var x = width / 16;
var y = width / 8;
var w = width * 14 / 16;
var h = width / 16;
var segmentWidth = Math.round( w / 7 );
// First black segment
context.fillRect( x, y, segmentWidth, h );
// Ruler segments
for( var n = x + segmentWidth; n < x + segmentWidth * 6; n += segmentWidth ){
context.fillRect( n, y, segmentWidth, 1 );
// Big marks
context.fillRect( n, y, 1, h );
// Small marks
if( segmentWidth >= 12 ) {
for( var v = 1; v < 6; v++ ) {
context.fillRect( n + Math.round( segmentWidth * v / 6 ), y, 1, h / 2 );
}
}
}
// End red segment
context.fillStyle = '#AA0000';
context.fillRect( x + segmentWidth * 6, y, segmentWidth, h );
}
function drawNeedle( context, x, width, deflection ) {
var angle = deflection * Math.PI / 2 - Math.PI / 4;
var x1 = width / 2 + width * 8 / 16 * Math.sin( angle );
var y1 = width * 9 / 16 - width * 8 / 16 * Math.cos( angle );
var x2 = width / 2 + width * 3 / 16 * Math.tan( angle );
var y2 = width * 3 / 8;
context.beginPath();
context.moveTo( x + x1, y1 );
context.lineTo( x + x2, y2 );
context.stroke();
}
function onAudioProcess( event ) {
leftForce = getForce( getMaxAmplitude( event.inputBuffer.getChannelData( 0 ) ) );
rightForce = getForce( getMaxAmplitude( event.inputBuffer.getChannelData( 1 ) ) );
}
function getForce( amplitude ) {
// 6DBA = 10 ^ 0.3 ~= 1.995
var force = ( Math.log( amplitude ) / Math.log( 1.995 ) + 7 ) / 7;
if( force < 0 ) {
force = 0;
}
// Correct for straight ruler-marks.
return ( Math.atan( force * 2 - 1 ) * 4 / Math.PI + 1 ) / 2;
}
function getMaxAmplitude( audioBuf ) {
var maxAmp = 0;
for( var idx = 0, len = audioBuf.length; idx < len; idx++ ) {
var absAmp = Math.abs( audioBuf[ idx ] );
if( absAmp > maxAmp ) {
maxAmp = absAmp;
}
}
return maxAmp;
}
function repaint( millis ) {
var width = canvas.width / 2;
context.putImageData( background, 0, 0 );
drawNeedle( context, 0, width, leftSpring.model( leftForce, 0, 1, millis - time ) );
context.putImageData( background, width, 0 );
drawNeedle( context, width, width, rightSpring.model( rightForce, 0, 1, millis - time ) );
requestAnimationFrame( repaint );
time = millis;
}
</script>
</head>
<body style="background:black" onload="init()">
<canvas style="display:block;margin-left:auto;margin-right:auto;" id="canvas" width="640" height="160"></canvas>
</body>
</html>