-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
95 lines (77 loc) · 2.64 KB
/
node_helper.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** Porcupine module **/
"use strict"
const path = require("path")
const Porcupine = require('bumblebee-hotword-node');
var NodeHelper = require("node_helper")
// Logging function to log MMM-Porcupine output, in this case it is binding the
// output of the current script to the console with the [PORCUPINE] context
var _log = function() {
var context = "[PORCUPINE]"
return Function.prototype.bind.call(console.log, console, context)
}()
// Logging
var log = function() {
//do nothing
}
// export functions for use elsewhere
module.exports = NodeHelper.create({
// Start function
start: function () {
console.log("[PORCUPINE] Starting...")
this.config = {}
this.running = false
this.porcupine = null
},
socketNotificationReceived: function(notification, payload) {
switch(notification) {
case "INIT":
// set the internal config to the payload received in socket notification
this.config = payload
this.initialize()
break
case "START":
// if we get a START socket notification, tell Porcupine to start listening
if (!this.running) this.activate()
break
case "STOP":
// If we get a STOP socket notification, tell Porcupine to stop listening
if (this.running) this.deactivate()
break
}
},
initialize: function() {
// if config has debug=true then start in debug mode, else dont
var debug = (this.config.debug) ? this.config.debug : false
if (debug == true) log = _log
// Create a new porcupine instance
this.porcupine = new Porcupine()
var hotword = this.config.hotword
// Inform the user of the hotword currently in use + sensitivity
console.log('USING HOTWORD:', hotword)
console.log('SENSITIVITY:', this.config.sensitivity)
// Add the hotword
const data = require(`./hotwords/${hotword}`);
this.porcupine.addHotword(hotword, data, config.sensitivity);
// Set the sensitivity for the hotword detection
this.porcupine.setSensitivity(this.config.sensitivity)
// DOESN'T WORK (only normal JS version)
//this.porcupine.setMicVolume(this.config.micVolume)
// Listen for hotword detection events, when we receive the event:
// send a socketNotification
this.porcupine.on('hotword', (hotword) => {
this.sendSocketNotification("DETECTED")
console.log('DETECTED:', hotword);
});
log("Initialized...")
},
// Tell Porcupine to start listening
activate: function() {
this.porcupine.start()
this.running = true
},
// Tell Porcupine to stop listening
deactivate: function() {
this.porcupine.stop()
this.running = false
},
})