]> Repos - mime-chat/commitdiff
testing audio test
authorEnrique Hernandez <hernandeze2@xavier.edu>
Wed, 3 Mar 2021 18:49:32 +0000 (12:49 -0600)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Wed, 3 Mar 2021 18:49:32 +0000 (12:49 -0600)
src/background/audio.ts
src/background/init.ts
src/background/window.ts

index 7b8066bcc65e4e8322c470cf9c4e3255763c322b..e1e75629eb9d89535fe276c004aad0587d6ba515 100644 (file)
@@ -4,10 +4,11 @@
 
 import { sendAudio } from './session'
 import { ipcMain } from "electron";
+import { backgroundMitt } from './emitter';
 const portAudio = require('naudiodon');
 
-let ai: typeof portAudio.AudioIO;
-let ao: typeof portAudio.AudioIO;
+let ai: typeof portAudio.AudioIO | null = null;
+let ao: typeof portAudio.AudioIO | null = null;
 let audioInput: Buffer[] = [];
 const encoding = "base64";
 const audioOptions = {
@@ -18,41 +19,47 @@ const audioOptions = {
     closeOnError: false,
 }
 
-// main function run by run.ts module.
+backgroundMitt.once('window-active', (state: boolean) => {
+    if(!state) {
+        stopStream();
+    }
+});
+
+// main audio function run by run.ts module.
 export function initAudioIO(): void {
-    // init portAudio readable stream once.
-    if (!ai) {
-        ai = new portAudio.AudioIO({ inOptions: audioOptions });
 
-        // base64 encoding needed for google speech to text.
-        ai.setEncoding(encoding);
+    if (ai != null) {
+        ai.quit();
+        audioInput = [];
+    }
 
-        // pause the portAudio data flow as soon as stream is initiated.
-        ai.start();
-        ai.pause();
+    ai = new portAudio.AudioIO({ inOptions: audioOptions });
 
-        ai.on('error', (e: Error) => {
-            console.log('Error recording audio', + e);
-        });
-        ai.on('data', (chunk: string) => {
-            // turn string base64 data into buffer object.
-            console.log('received string chunk of length', chunk.length)
-            const buf = Buffer.from(chunk, encoding);
-            audioInput.push(buf);
-        });
-    }
+    // base64 encoding needed for google speech to text.
+    ai.setEncoding(encoding);
 
-    // init portAudio writable stream once.
-    if (!ao) {
-        ao = new portAudio.AudioIO({ outOptions: audioOptions });
-        ao.start();
-    }
+    // pause the portAudio data flow as soon as stream is initiated.
+    ai.start();
+    ai.pause();
+
+    ai.on('data', (chunk: string) => {
+        // turn string base64 data into buffer object.
+        const buf = Buffer.from(chunk, encoding);
+        audioInput.push(buf);
+    });
+
+    if (ao != null) {
+        ao.end();
+    } 
+    ao = new portAudio.AudioIO({ outOptions: audioOptions });
+    ao.start();
 }
 
-// run this function on space bar key up and down.
+// callback run on space bar key up and down.
 const updateRecorder = (_event, record: boolean): void => {
     if (record) {
         // resume mic data flow on space bar key down.
+        while(audioInput.length) { audioInput.pop() }
         ai.resume();
     } else {
         // stop mic data flow on space bar key up.
@@ -60,7 +67,7 @@ const updateRecorder = (_event, record: boolean): void => {
         const audio = Buffer.concat(audioInput);
         sendAudio(audio);
         // clear audioInput.
-        while(audioInput.length) { audioInput.pop()}
+        while(audioInput.length) { audioInput.pop() }
     }
 }
 
@@ -103,9 +110,20 @@ export function play(input: Array<Buffer>): void {
     }
 }
 
+function stopStream() {
+    if(ai != null) {
+        ai.quit();
+        ai = null;
+    }
+    if (ao != null) {
+        ao.quit();
+        ao = null;
+    }
+}
+
 // utility function used by play func.
 function bufSplit(input: Array<Buffer>): Array<Buffer> {
-    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));
index d41ab56b1aeb2aa279879d9e67ecbfc0f1f84a67..c7e0082a9398ae07be11b99911540b40721e15e5 100644 (file)
@@ -1,3 +1,4 @@
+
 "use strict";
 
 import { app } from "electron";
@@ -6,7 +7,7 @@ import { backgroundMitt } from './emitter';
 
 let winActive: boolean;
 
-backgroundMitt.on('window-active', (state: boolean) => {
+backgroundMitt.once('window-active', (state: boolean) => {
     winActive =  state;
 });
 
@@ -23,7 +24,6 @@ export function initApp(dev: boolean): void {
       }
     });
 
-    // TODO: Debug activate functionaity: currently not working.
     // Might have to de-reference window object
     app.on("activate", () => {
       if (winActive === false) {
index 6d1002b3efaae1d50dbcaf4d1264b16a172152c3..fdf54034c883f31c5c5f75d21a3f723391136afa 100644 (file)
@@ -110,4 +110,4 @@ export async function createWindow(options: WindowSettings): Promise<void> {
     });
 
   });
-};
+}