From 4517752ed8a4a7bd5752ee1ea6927dbe9ffdbb5e Mon Sep 17 00:00:00 2001 From: Enrique Hernandez Date: Tue, 2 Mar 2021 08:42:30 -0600 Subject: [PATCH] manual remove audioInput after write --- src/background/audio.ts | 7 ++++--- tests/audio.js | 13 +++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/background/audio.ts b/src/background/audio.ts index 50c052f..4739b0c 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -7,7 +7,7 @@ const portAudio = require('naudiodon'); let ai: typeof portAudio.AudioIO; let ao: typeof portAudio.AudioIO; -let audioInput: Buffer[] = []; +const audioInput: Buffer[] = []; const encoding = "base64"; const audioOptions = { channelCount: 1, @@ -57,7 +57,8 @@ export const updateRecorder = (_event, record: boolean): void => { // stop mic data flow on space bar key up. ai.pause(); const audio = Buffer.concat(audioInput); - sendAudio(audio); + play(audioInput); + // sendAudio(audio); while (audioInput.length) { audioInput.pop(); } } } @@ -98,7 +99,7 @@ export const play = (input: Array) => { // utility function used by play func function bufSplit(input: Array): Array { - let result: Buffer[] = []; + const result: Buffer[] = []; input.forEach((b: Buffer) => { // split buffer into two. result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length)); diff --git a/tests/audio.js b/tests/audio.js index c4d645f..7ffe6d0 100644 --- a/tests/audio.js +++ b/tests/audio.js @@ -40,7 +40,7 @@ async function transcribeSpeech (audio) { console.log(`Transcription: ${transcription}`); } -let audioInput = []; +const audioInput = []; // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream const ia = new portAudio.AudioIO({ inOptions: { @@ -74,7 +74,7 @@ async function processAudio() { const buf = Buffer.concat(audioInput); play(audioInput); - while(audioInput.length) { audioInput.pop() }; + while(audioInput.length) { audioInput.pop() } transcribeSpeech({ content: buf @@ -117,7 +117,7 @@ async function processAudio() { // utility function used by play func function bufSplit(input){ - let result = []; + const result = []; input.forEach((b) => { // split buffer into two. result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length)); @@ -140,5 +140,10 @@ setTimeout(async () => { }, 8000); setTimeout(() => { + ia.resume(); +}, 10000) + +setTimeout(async () => { ia.pause(); -}, 9000) + processAudio(); +}, 15000); -- 2.43.0