forked from takispig/db-meter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (81 loc) · 3.29 KB
/
script.js
File metadata and controls
99 lines (81 loc) · 3.29 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
93
94
95
96
97
98
99
let localDbValues = []; // array to store db values for each loop withing the refresh_rate
let refresh_rate = 500;
let color = 'green';
let stream;
let offset = 0;
let date;
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then((stream) => {
const context = new AudioContext();
const source = context.createMediaStreamSource(stream);
const processor = context.createScriptProcessor(2048, 1, 1);
const analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0.8;
analyser.fftSize = 256;
source.connect(analyser);
analyser.connect(processor);
processor.connect(context.destination);
processor.onaudioprocess = () => {
let data = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(data);
let rms = 0;
for (var i = 0; i < data.length; i++) {
if (data[i]>120) data[i] = 120
rms += data[i]*data[i]
}
rms = Math.sqrt(rms / data.length);
console.log("RMS: " + rms)
offset = parseInt(document.getElementById("offset").value);
document.getElementById("offset_value").innerText = offset;
value = rms + offset;
localDbValues.push(value);
};
})
// update the volume every refresh_rate m.seconds
let updateDb = function() {
window.clearInterval(interval);
const db = document.getElementById("db");
let volume = Math.round(localDbValues.reduce((a,b) => a+b) / localDbValues.length);
//var volume = Math.round(Math.max.apply(null, localDbValues));
if (!isFinite(volume)) volume = 0; // we don't want/need negative decibels in that case
db.innerText = volume;
localDbValues = []; // clear previous values
changeColor(volume);
changeUpdateRate();
interval = window.setInterval(updateDb, refresh_rate);
}
let interval = window.setInterval(updateDb, refresh_rate);
// change the visualization colors according to the dbValue
function changeColor(decibels) {
if (decibels < 50) {color = 'green'}
else if (decibels >=50 && decibels <70) {color = 'yellow'}
else if (decibels >= 70 && decibels <90) {color = 'orange'}
else {color = 'red'}
//document.getElementById("visuals").style.height = dbValue + "px";
document.getElementById("visuals").style.width = decibels*2/10 + "rem";
if (decibels>=70) document.getElementById("visuals").style.background = 'red';
else document.getElementById("visuals").style.background = 'black';
document.getElementById("db").style.color = color;
}
// change update rate
function changeUpdateRate() {
refresh_rate = Number(document.getElementById("refresh_rate").value);
document.getElementById("refresh_value").innerText = refresh_rate;
window.setInterval(function() {
updateDb;
}, refresh_rate);
}
// update the date of last project's version
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let repos = JSON.parse(this.responseText);
repos.forEach((repo)=>{
if (repo.name == "db-meter") {
date = new Date(repo.pushed_at);
document.getElementById("date").innerText = date.toLocaleDateString();
}
});
}
};
xhttp.open("GET", "https://api.github.com/users/takispig/repos", true);
xhttp.send();