From 854d6dd6a26ef1b61ad5c00e8f09657538c67db5 Mon Sep 17 00:00:00 2001 From: Enrique Hernandez Date: Thu, 4 Mar 2021 09:20:07 -0600 Subject: [PATCH] remove stream pause and resume method calls --- src/background/audio.ts | 4 +- tests/audio.js | 88 ++++++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/background/audio.ts b/src/background/audio.ts index ac41d40..b3ba491 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -54,7 +54,7 @@ export function initAudioIO(): void { if (ao != null) { ao.end(); - } + } ao = new portAudio.AudioIO({ outOptions: audioOptions }); ao.start(); } @@ -77,7 +77,7 @@ const updateRecorder = (_event, record: boolean): void => { } } -// listen for space key up/down event. +// listen for space key up/down event. ipcMain.removeAllListeners('update-recorder'); ipcMain.on('update-recorder', updateRecorder); diff --git a/tests/audio.js b/tests/audio.js index 7ffe6d0..ee6ae26 100644 --- a/tests/audio.js +++ b/tests/audio.js @@ -10,6 +10,11 @@ const encoding = 'LINEAR16'; const sampleRateHertz = 16000; const languageCode = 'en-US'; +const audioContainer = { + input: '', + buffers: [] +} + const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, @@ -20,7 +25,6 @@ const config = { * Note that transcription is limited to 60 seconds audio. * Use a GCS file for audio longer than 1 minute. */ - async function transcribeSpeech (audio) { const request = { config: config, @@ -40,7 +44,8 @@ async function transcribeSpeech (audio) { console.log(`Transcription: ${transcription}`); } -const audioInput = []; +let record = true; + // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream const ia = new portAudio.AudioIO({ inOptions: { @@ -54,8 +59,13 @@ const ia = new portAudio.AudioIO({ ia.setEncoding('base64'); ia.start(); ia.on('data', (chunk) => { - const buf = Buffer.from(chunk, 'base64'); - audioInput.push(buf); + if (record) { + console.log('recording data') + audioContainer.input += chunk; + // audioContainer.buffers.push(Buffer.from(chunk, 'base64')); + } else { + if (audioContainer.input.length) audioContainer.input = ""; + } }); const ao = new portAudio.AudioIO({ @@ -69,40 +79,38 @@ const ao = new portAudio.AudioIO({ }); ao.start(); -async function processAudio() { - - const buf = Buffer.concat(audioInput); - play(audioInput); - - while(audioInput.length) { audioInput.pop() } +let counter = 0; +let tests = []; - transcribeSpeech({ - content: buf - }); +function testCallback() { + tests.forEach(t => console.log(t)) + console.log(tests[0]) + play(tests[0]) } - - function play(input) { +function play(bufArray) { let i = 0; // format buffers into half their size to account for writable highwaterMark. - const audio = bufSplit(input); + const audio = bufSplit(bufArray); // call this fuction after last audio chunk has been written. const callback = () => { // TODO: clear portAudio writable buffer on write end. + console.log('Finished writing test number %d', counter) + if (counter === 3) { + console.log('finished test writing!') + } } write(); // iterate through audio array and write buffers to portAudio writable. function write() { - let chunk; let ok = true; do { - chunk = audio[i]; if (i === audio.length - 1) { // write last chunk. - ao.write(chunk, null, callback); + ao.write(audio[i], null, callback); } else { // check for backpreassure. - ok = ao.write(chunk, null); + ok = ao.write(audio[i], null); } i++; } while (i < audio.length && ok); @@ -125,25 +133,41 @@ function bufSplit(input){ return result; } +async function test() { + transcribeSpeech({ + content: Buffer.from(audioContainer.input, 'base64') + }); + tests.push(audioContainer.buffers) + counter++; + console.log('audio string length:', audioContainer.input.length) + console.log('buffers written: ', audioContainer.buffers.length) +} + setTimeout(async () => { - ia.pause(); - processAudio(); + record = false; + test() }, 3000); setTimeout(() => { - ia.resume(); -}, 5000) + record = true; +}, 6000) setTimeout(async () => { - ia.pause(); - processAudio(); -}, 8000); + record = false; + test(); +}, 9000); setTimeout(() => { - ia.resume(); -}, 10000) + record = true; +}, 11000) + +setTimeout(async () => { + record = false; + test(); +}, 14000); setTimeout(async () => { - ia.pause(); - processAudio(); -}, 15000); + ia.quit(); + // testCallback() + return; +}, 16000); -- 2.43.0