Skip to content

Commit

Permalink
An arecord helper to restart a failed audio stream
Browse files Browse the repository at this point in the history
terminate old arecord process when restarting

a bit of cleanup

use 1.5GB limit
  • Loading branch information
evancohen committed Mar 6, 2019
1 parent 66da672 commit edb978c
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit edb978c

Please sign in to comment.