]> Repos - mime-chat/commitdiff
remove stream pause and resume method calls
authorEnrique Hernandez <hernandeze2@xavier.edu>
Thu, 4 Mar 2021 15:20:07 +0000 (09:20 -0600)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Thu, 4 Mar 2021 15:20:07 +0000 (09:20 -0600)
src/background/audio.ts
tests/audio.js

index ac41d4015524d86f313abac97e937d4c20083a74..b3ba49179b7f1858dc8e2a67e383e6b03be017ad 100644 (file)
@@ -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);
 
index 7ffe6d01ede88ff8d7bfeb9d2428b9b55da696eb..ee6ae26d0a70cb18b70b36422a5f2430db71c450 100644 (file)
@@ -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);