-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
45 lines (40 loc) · 1.2 KB
/
index.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
var http = require('http');
var _ = require('underscore');
var pcm = require('pcm-boilerplate');
var qmean = require('compute-qmean');
var mean = require('compute-incrmmean');
function NoiseDetection(options, callback) {
var streamDecoder = new pcm.StreamDecoder(options.format);
var rmsAvg = mean(2000);
function toDecibel(rms) {
return 20*(Math.log(rms) / Math.log(10));
}
function processBlock() {
var block = streamDecoder.read();
var samples = [];
var rms;
var db;
if (block) {
_.forEach(block[0], function (sample) {
samples.push(sample);
});
rms = qmean(samples);
rmsAvg(rms);
dB = toDecibel(rms);
if (options.triggerLevel) {
if (dB > toDecibel(rmsAvg()) + options.triggerLevel) {
callback(dB);
}
} else {
callback(dB);
}
}
}
this.start = function() {
http.get(options.url, function (source) {
source.pipe(streamDecoder);
streamDecoder.on('readable', processBlock);
});
}
}
module.exports = NoiseDetection;