forked from alexkh13/emma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (43 loc) · 1.14 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
46
47
48
49
50
51
52
53
const path = require('path');
const fs = require('fs');
const SoundDetector = require('./sound-detector');
const hey = require('./hey');
const syncHey = require('./sync-hey');
function timestamp(){
ts = Date();
// Tue Jan 09 2024 17:03:41 GMT +0200(Israel Standard Time)
ts = ts.replace(/GMT.*/g, "");
return ts + "|";
}
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' })));
console.log("MAX_RMS_AMPLITUDE: ", config.MAX_RMS_AMPLITUDE);
}
catch (err) {
console.error("couldn't read config.json", err);
}
}
let soundDetector = new SoundDetector(config);
soundDetector.start();
let detections = 0;
hey.on('reset', () => {
console.log("resetting detections");
detections = 0;
});
soundDetector.on("detected", ({ duration, max, rms }) => {
console.log(timestamp() + " detected " + rms + ", detections: " + detections)
if (rms > config.MAX_RMS_AMPLITUDE) {
if (++detections > 2) {
hey.send();
}
}
});