-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
DynamicThresholdCalculator.ts
67 lines (57 loc) · 1.91 KB
/
DynamicThresholdCalculator.ts
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
import ConfigProvider from "../../shared/configProvider";
import debug from '../../shared/debug';
/**
* Dynamic Threshold Calculator:
* Dynamically detect the volume level silence has
*
* Related: https://github.com/vantezzen/skip-silence/issues/36
*/
export default class DynamicThresholdCalculator {
// Current dynamic threshold
threshold = 10;
// Extension configuration
config : ConfigProvider;
// Volume of the previous 500 samples to better calculate the threshold
previousSamples : number[] = [];
/**
* Set up the calculator
*
* @param config Config to use
*/
constructor(config : ConfigProvider) {
this.config = config;
}
/**
* Let the calculator inspect a new sample.
* This will be used to adjust the calculated threshold
*/
calculate() {
if (this.previousSamples.length < 20) {
// Not enough data yet
return;
}
const currentTreshold = this.threshold;
const sortedSamples = this.previousSamples.sort((a, b) => a - b);
const lowest10Percentile = sortedSamples[Math.floor(this.previousSamples.length * 0.1)];
const delta = Math.abs(this.threshold - lowest10Percentile);
if (lowest10Percentile > this.threshold) {
this.threshold += delta * 0.1;
} else if (lowest10Percentile < this.threshold) {
// Threshold should decrease faster so we can better adapt to fast volume changes
this.threshold -= delta * 0.4;
}
debug(`Threshold update:
Old: ${currentTreshold}
New: ${this.threshold}
Lowest 10th: ${lowest10Percentile}
Delta: ${delta}
Samples: ${this.previousSamples.length}
Min Sample: ${Math.min(...this.previousSamples)}
Max Sample: ${Math.max(...this.previousSamples)}`, sortedSamples);
this.config.set('silence_threshold', this.threshold);
// Make sure the samples array only contains 100 samples max
while(this.previousSamples.length > 100) {
this.previousSamples.shift();
}
}
}