add stream pause and resume method calls

This commit is contained in:
riqo 2021-02-01 10:32:25 -06:00
commit b78b5392c1

View 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();
});
}
}
}