]> Repos - mime-chat/commitdiff
add stream pause and resume method calls
authorriqo <hernandeze2@xavier.edu>
Mon, 1 Feb 2021 16:32:25 +0000 (10:32 -0600)
committerriqo <hernandeze2@xavier.edu>
Mon, 1 Feb 2021 16:32:25 +0000 (10:32 -0600)
src/background.ts

index 71e7d6873f3e8947a258491664fe8ae76ecedfcd..1002d0dc173619b52446029d603f57f63153837a 100644 (file)
@@ -8,7 +8,6 @@ import * as path from "path";
 const fs = require('fs');
 import WebSocket from "ws";
 const portAudio = require('naudiodon');
-console.log(portAudio.getDevices());
 
 // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
 const audioOptions = {
@@ -16,23 +15,25 @@ const audioOptions = {
   sampleFormat: portAudio.SampleFormat16Bit,
   sampleRate: 44100,
   deviceId: -1, // Use -1 or omit the deviceId to select the default device
-  closeOnError: true // Close the stream if an audio error is detected, if set false then just log the error
+  closeOnError: false // Close the stream if an audio error is detected, if set false then just log the error
 }
 
-// Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream
-const ao = new portAudio.AudioIO({
-  outOptions: audioOptions 
-});
-
-let ai = new portAudio.AudioIO({
+const ai = new portAudio.AudioIO({
   inOptions: audioOptions
 });
+let audioData = "";
+ai.on('data', (buf: Buffer) => {
+  const base64Data = buf.toString('base64');
+  audioData += base64Data;
+}); 
+ai.start();
+ai.pause();
 
 const filename = "rawAudio.wav";
-
 // Create a write stream to write out to a raw audio file
 const writeStream = fs.createWriteStream(filename);
-ai.pipe(writeStream);
+// ai.pipe(writeStream);
+
 // Keep a global reference of the window object, if you don't, the window will
 // be closed automatically when the JavaScript object is garbage collected.
 let win: BrowserWindow | null;
@@ -90,31 +91,23 @@ function createWindow() {
 
     ipcMain.on('send-message', (Event: any, payload: any) => {
       ws.send(JSON.stringify(payload));
-    // win.setTitle("Listening...");
+      // win.setTitle("Listening...");
     })
 
-    let audioData = "";
     ipcMain.on('update-recorder', (Event: any, record: boolean) => {
      if (record) {
-       ai.start();
-       // ai.on('data', (buf: Buffer) => {
-       //    const base64Data = buf.toString('base64');
-       //    audioData += base64Data;
-       // }); 
+       ai.resume();
      } else {
-       ai.quit();
+       ai.pause();
 
        const audio = {
          content: fs.readFileSync(filename).toString('base64'), 
        }
        const message = {
          text: "",
-         audio
+         audio: audioData
        }
        ws.send(JSON.stringify(message))
-       ai =  new portAudio.AudioIO({
-         inOptions: audioOptions
-       })
        audioData = "";
      }
     })
@@ -170,4 +163,4 @@ if (isDevelopment) {
       app.quit();
     });
   }
-}
+}
\ No newline at end of file