51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
const { spawn } = require('child_process');
|
|
const path = require('path');
|
|
|
|
let wakewordProcess = null;
|
|
let running = false;
|
|
|
|
function startWakeword({ onWake }) {
|
|
if (running) return;
|
|
running = true;
|
|
|
|
const scriptPath = path.join(__dirname, '../python/wakeword.py');
|
|
|
|
const proc = spawn('python3', [scriptPath], {
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
});
|
|
wakewordProcess = proc;
|
|
|
|
let lineBuffer = '';
|
|
proc.stdout.on('data', (data) => {
|
|
lineBuffer += data.toString();
|
|
const lines = lineBuffer.split('\n');
|
|
lineBuffer = lines.pop();
|
|
for (const line of lines) {
|
|
if (line.trim() === 'WAKE') onWake();
|
|
}
|
|
});
|
|
|
|
proc.stderr.on('data', (data) => {
|
|
console.error('[Wakeword]', data.toString().trim());
|
|
});
|
|
|
|
proc.on('close', (code, signal) => {
|
|
if (proc !== wakewordProcess) return;
|
|
running = false;
|
|
wakewordProcess = null;
|
|
if (code !== 0 && signal === null) {
|
|
setTimeout(() => startWakeword({ onWake }), 2000);
|
|
}
|
|
});
|
|
}
|
|
|
|
function stopWakeword() {
|
|
if (wakewordProcess) {
|
|
wakewordProcess.kill();
|
|
wakewordProcess = null;
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
module.exports = { startWakeword, stopWakeword };
|