-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (35 loc) · 863 Bytes
/
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
const path = require('path');
const fs = require('fs');
const SoundDetector = require('./sound-detector');
const hey = require('./hey');
const syncHey = require('./sync-hey');
setInterval(() => {
syncHey();
}, 60 * 1000);
syncHey();
const configPath = path.resolve(__dirname, 'config.json');
const config = {
MAX_RMS_AMPLITUDE: 0.5
};
if (fs.existsSync(configPath)) {
try {
Object.assign(config, JSON.parse(fs.readFileSync(configPath, { encoding: 'utf8' })));
}
catch(err) {
console.error("couldn't read config.json", err);
}
}
let soundDetector = new SoundDetector(config);
soundDetector.start();
let detections = 0;
hey.on('reset', () => {
detections = 0;
});
soundDetector.on("detected", ({duration,max,rms}) => {
console.log("detected " + rms);
if (rms > config.MAX_RMS_AMPLITUDE) {
if (++detections > 3) {
hey.send();
}
}
});