]> Repos - mime-chat/commitdiff
minor tweaks to audioInput
authorEnrique Hernandez <hernandeze2@xavier.edu>
Wed, 3 Mar 2021 20:29:00 +0000 (14:29 -0600)
committerEnrique Hernandez <hernandeze2@xavier.edu>
Wed, 3 Mar 2021 20:29:00 +0000 (14:29 -0600)
src/background/audio.ts

index e1e75629eb9d89535fe276c004aad0587d6ba515..a34a59dcfa8c624b7b9ebfb8cada90147ec208c5 100644 (file)
@@ -10,6 +10,10 @@ const portAudio = require('naudiodon');
 let ai: typeof portAudio.AudioIO | null = null;
 let ao: typeof portAudio.AudioIO | null = null;
 let audioInput: Buffer[] = [];
+const audioContainer = {
+    input: '',
+}
+let audio: Buffer | null = null;
 const encoding = "base64";
 const audioOptions = {
     channelCount: 1,
@@ -19,8 +23,8 @@ const audioOptions = {
     closeOnError: false,
 }
 
-backgroundMitt.once('window-active', (state: boolean) => {
-    if(!state) {
+backgroundMitt.once('window-active', (active: boolean) => {
+    if(!active) {
         stopStream();
     }
 });
@@ -30,7 +34,6 @@ export function initAudioIO(): void {
 
     if (ai != null) {
         ai.quit();
-        audioInput = [];
     }
 
     ai = new portAudio.AudioIO({ inOptions: audioOptions });
@@ -44,8 +47,9 @@ export function initAudioIO(): void {
 
     ai.on('data', (chunk: string) => {
         // turn string base64 data into buffer object.
-        const buf = Buffer.from(chunk, encoding);
-        audioInput.push(buf);
+        audioContainer.input += chunk;
+        // const buf = Buffer.from(chunk, encoding);
+        // audioInput.push(buf);
     });
 
     if (ao != null) {
@@ -59,15 +63,17 @@ export function initAudioIO(): void {
 const updateRecorder = (_event, record: boolean): void => {
     if (record) {
         // resume mic data flow on space bar key down.
-        while(audioInput.length) { audioInput.pop() }
+        // while(audioInput.length) { audioInput.pop() }
+        if(audioContainer.input.length) audioContainer.input = '';
         ai.resume();
     } else {
         // stop mic data flow on space bar key up.
         ai.pause();
-        const audio = Buffer.concat(audioInput);
-        sendAudio(audio);
+        const buf = Buffer.from(audioContainer.input, encoding);
+        sendAudio(buf);
         // clear audioInput.
-        while(audioInput.length) { audioInput.pop() }
+        audioContainer.input = '';
+        // while(audioInput.length) { audioInput.pop() }
     }
 }