From b78b5392c1be2c0f8cdc4e18004b0012881b363d Mon Sep 17 00:00:00 2001 From: riqo Date: Mon, 1 Feb 2021 10:32:25 -0600 Subject: [PATCH] add stream pause and resume method calls --- src/background.ts | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/background.ts b/src/background.ts index 71e7d68..1002d0d 100644 --- a/src/background.ts +++ b/src/background.ts @@ -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 -- 2.43.0