-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvolume_detector.js
45 lines (35 loc) · 1.1 KB
/
volume_detector.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
'use strict';
function VolumeDetector(f, v){
this.isPlaying = false;
this.volumeThreshold = v;
this.frequencyThreshold = f;
this.frequencyBuff = null;
}
VolumeDetector.prototype.togglePlay = function(){
this.isPlaying = !this.isPlaying;
};
VolumeDetector.prototype.updateVolumeThreshold = function(v){
this.volumeThreshold = v;
};
VolumeDetector.prototype.updateFrequencyThreshold = function(v){
this.frequencyThreshold = v;
};
VolumeDetector.prototype._reachFrequency = function(){
for(let i=this.frequencyThreshold;i < this.frequencyBuff.length;i++){
if(this.frequencyBuff[i] > 0){
return true;
}
}
return false;
}
VolumeDetector.prototype.updateAverageVolume = function(){
let values = 0,
length = this.frequencyBuff.length;
for (let i = 0; i < length; i++) {
values += this.frequencyBuff[i];
}
this.averageVolume = Math.round(values / length);
};
VolumeDetector.prototype.reachThreshold = function(){
return this._reachFrequency() && this.averageVolume > this.volumeThreshold;
};