From edb978c176fd38edbbacc27f2b0517539fc0335a Mon Sep 17 00:00:00 2001 From: Evan Cohen Date: Wed, 6 Mar 2019 08:30:01 +0700 Subject: [PATCH] An arecord helper to restart a failed audio stream terminate old arecord process when restarting a bit of cleanup use 1.5GB limit --- index.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1222e97..06a750d 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ const ERROR = { INVALID_INDEX: "INVALID_INDEX" } +const ARECORD_FILE_LIMIT = 1500000000 // 1.5 GB + const CloudSpeechRecognizer = {} CloudSpeechRecognizer.init = recognizer => { const csr = new stream.Writable() @@ -147,15 +149,50 @@ Sonus.init = (options, recognizer) => { } Sonus.start = sonus => { - sonus.mic = record.start({ + sonus.mic = Recorder(sonus) + + if(sonus.recordProgram === "arecord"){ + ArecordHelper.init(sonus) + } + + sonus.mic.pipe(sonus.detector) + sonus.started = true +} + +const Recorder = (sonus) => { + return record.start({ threshold: 0, device: sonus.device || null, recordProgram: sonus.recordProgram || "rec", verbose: false }) +} + +const ArecordHelper = {byteCount: 0} +ArecordHelper.init = (sonus) => { + ArecordHelper.track(sonus) +} + +ArecordHelper.track = (sonus) => { + sonus.mic.on('data', data => { + ArecordHelper.byteCount += data.length + + // When we get to arecord wav file limit, reset + if(ArecordHelper.byteCount > ARECORD_FILE_LIMIT){ + ArecordHelper.restart(sonus) + } + }) +} +ArecordHelper.restart = (sonus) => { + sonus.mic.unpipe(sonus.detector) + record.stop() + + // Restart the audio recording + sonus.mic = Recorder(sonus) + ArecordHelper.byteCount = 0 + ArecordHelper.track(sonus) sonus.mic.pipe(sonus.detector) - sonus.started = true } Sonus.trigger = (sonus, index, hotword) => sonus.trigger(index, hotword)