Skip to content

Commit 43d3a3d

Browse files
committed
An arecord helper to restart a failed audio stream
terminate old arecord process when restarting a bit of cleanup use 1.5GB limit
1 parent 1b3ca0d commit 43d3a3d

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

index.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const ERROR = {
99
INVALID_INDEX: "INVALID_INDEX"
1010
}
1111

12+
const ARECORD_FILE_LIMIT = 1500000000 // 1.5 GB
13+
1214
const CloudSpeechRecognizer = {}
1315
CloudSpeechRecognizer.init = recognizer => {
1416
const csr = new stream.Writable()
@@ -147,15 +149,50 @@ Sonus.init = (options, recognizer) => {
147149
}
148150

149151
Sonus.start = sonus => {
150-
sonus.mic = record.start({
152+
sonus.mic = Recorder(sonus)
153+
154+
if(sonus.recordProgram === "arecord"){
155+
ArecordHelper.init(sonus)
156+
}
157+
158+
sonus.mic.pipe(sonus.detector)
159+
sonus.started = true
160+
}
161+
162+
const Recorder = (sonus) => {
163+
return record.start({
151164
threshold: 0,
152165
device: sonus.device || null,
153166
recordProgram: sonus.recordProgram || "rec",
154167
verbose: false
155168
})
169+
}
170+
171+
const ArecordHelper = {byteCount: 0}
172+
ArecordHelper.init = (sonus) => {
173+
ArecordHelper.track(sonus)
174+
}
175+
176+
ArecordHelper.track = (sonus) => {
177+
sonus.mic.on('data', data => {
178+
ArecordHelper.byteCount += data.length
179+
180+
// When we get to arecord wav file limit, reset
181+
if(ArecordHelper.byteCount > ARECORD_FILE_LIMIT){
182+
ArecordHelper.restart(sonus)
183+
}
184+
})
185+
}
156186

187+
ArecordHelper.restart = (sonus) => {
188+
sonus.mic.unpipe(sonus.detector)
189+
record.stop()
190+
191+
// Restart the audio recording
192+
sonus.mic = Recorder(sonus)
193+
ArecordHelper.byteCount = 0
194+
ArecordHelper.track(sonus)
157195
sonus.mic.pipe(sonus.detector)
158-
sonus.started = true
159196
}
160197

161198
Sonus.trigger = (sonus, index, hotword) => sonus.trigger(index, hotword)

0 commit comments

Comments
 (0)