-
Notifications
You must be signed in to change notification settings - Fork 1
/
meter.js
133 lines (107 loc) · 3.06 KB
/
meter.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
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
var meterCanvas = null;
var leftAnalyser = null;
var leftWaveform = null;
var rightAnalyser = null;
var rightWaveform = null;
var tailArea = 1; // in seconds
var decayRatio = null;
var stickTime = 2;
var leftMeter = {
value: 0,
peak: 0,
peakTime: 0,
stuckValue: 0,
timeout: 0
};
var rightMeter = {
value: 0,
peak: 0,
peakTime: 0,
stuckValue: 0,
timeout: 0
};
var meterUpdating = false;
function initMeter() {
meterCanvas = document.getElementById("meter");
var splitter = ac.createChannelSplitter();
masterFader.connect(splitter);
leftAnalyser = ac.createAnalyser();
leftAnalyser.fftSize = 2048;
splitter.connect(leftAnalyser,0,0);
leftWaveform = new Float32Array(leftAnalyser.frequencyBinCount);
rightAnalyser = ac.createAnalyser();
rightAnalyser.fftSize = 2048;
splitter.connect(rightAnalyser,1,0);
rightWaveform = new Float32Array(rightAnalyser.frequencyBinCount);
decayRatio = Math.exp(-1 / (ac.sampleRate * tailArea));
paintMeters();
}
function updateMeters() {
var now = ac.currentTime;
leftAnalyser.getFloatTimeDomainData(leftWaveform);
rightAnalyser.getFloatTimeDomainData(rightWaveform);
leftWaveform.reverse();
rightWaveform.reverse();
updateMeter(now, leftWaveform, leftMeter);
updateMeter(now, rightWaveform, rightMeter);
paintMeters();
}
function clearMeters() {
leftMeter.value = 0;
rightMeter.value = 0;
paintMeters();
}
function updateMeter(now, waveform, meter) {
var oldDecay = Math.exp((meter.peakTime - now) / tailArea);
var oldValue = meter.peak * oldDecay;
meter.value = oldValue;
// waveform[0] is at time (now - 1 / sampleRate)
var decay = decayRatio;
for (var i = 0; i < waveform.length; i++) {
var sample = Math.abs(waveform[i]);
if (sample > meter.value) {
meter.value = sample;
}
var value = sample * decay;
if (value > oldValue) {
oldValue = value;
meter.peak = sample;
meter.peakTime = now - (i + 1) / ac.sampleRate;
}
decay *= decayRatio;
}
if (meter.value >= meter.stuckValue) {
meter.stuckValue = meter.value;
if (meter.timeout != 0) {
clearTimeout(meter.timeout);
meter.timeout = 0;
}
}
if (meter.timeout == 0) {
meter.timeout = setTimeout(unstickMeter, 1000 * stickTime, meter);
}
}
function unstickMeter(meter) {
meter.stuckValue = meter.value;
meter.timeout = 0;
paintMeters();
}
function paintMeters() {
var dc = meterCanvas.getContext("2d");
dc.fillStyle = "black";
dc.fillRect(0, 0, 100, 5);
dc.fillRect(0, 6, 100, 5);
dc.fillStyle = leftMeter.value > 1 ? "red" : "lime";
dc.fillRect(0, 0, 100 * Math.min(1, leftMeter.value), 5);
dc.fillStyle = rightMeter.value > 1 ? "red" : "lime";
dc.fillRect(0, 6, 100 * Math.min(1, rightMeter.value), 5);
var peakWidth = 3;
dc.fillStyle = leftMeter.stuckValue > 1 ? "red" : "lime";
dc.fillRect(
100 * Math.min(1, leftMeter.stuckValue) - peakWidth, 0, peakWidth, 5
);
dc.fillStyle = rightMeter.stuckValue > 1 ? "red" : "lime";
dc.fillRect(
100 * Math.min(1, rightMeter.stuckValue) - peakWidth, 6, peakWidth, 5
);
}