-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundmeter.html
More file actions
92 lines (78 loc) · 2.17 KB
/
soundmeter.html
File metadata and controls
92 lines (78 loc) · 2.17 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<style>
.channel {
width: 0px;
height: 20px;
background: green;
}
#soundmeter {
width: 200px;
border: 1px solid black;
}
</style>
<div id="soundmeter">
<div id="soundmeter_channel0" class="channel">
</div>
<div id="soundmeter_channel1" class="channel">
</div>
<div id="soundmeter_channel2" class="channel">
</div>
<div id="soundmeter_channel3" class="channel">
</div>
</div>
<script>
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
function SoundMetrics(attributes) {
this.peakLevel = attributes["PeakLevel"];
}
SoundMetrics.prototype.getPeakDB = function() {
return 10 * Math.log(this.peakLevel) / 2.302585092994046;
};
display = document.getElementById("soundmeter")
var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
var ws = new Socket("ws://localhost:9000/soundmeter.ws");
ws.onmessage = function(evt) {
metrics = eval(evt.data).map(function(attributes) {
return new SoundMetrics(attributes)
})
metrics.forEach(function(channelMetrics, channelIndex) {
channelDisplay = document.getElementById("soundmeter_channel" + channelIndex)
var peakDB = channelMetrics.getPeakDB();
var width = 0
if (peakDB > -40) {
width = (peakDB + 40) / 40.0 * 200;
}
/* channelDisplay.innerHTML = peakDB; */
channelDisplay.style.width = width + "px";
})
};
ws.onclose = function() { console.log("socket closed"); };
</script>
</body>