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 = {
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;
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 = "";
}
})
app.quit();
});
}
-}
+}
\ No newline at end of file