add speechrecorder to background

This commit is contained in:
riqo 2020-12-30 11:55:27 -05:00
commit 3f52457956
3 changed files with 43 additions and 4 deletions

View file

@ -6,12 +6,14 @@ import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
const isDevelopment = process.env.NODE_ENV !== "production";
import * as path from "path";
import { SpeechRecorder } from "speech-recorder";
const fs = require('fs');
// TODO: connect To Crimata-BE
import WebSocket from "ws";
// const recorder = new SpeechRecorder({ sampleRate: 16000, framesPerBuffer: 320 });
const recorder = new SpeechRecorder({ sampleRate: 16000, framesPerBuffer: 320, level: 0 });
// const writeStream = fs.createWriteStream("audio.raw");
// 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.
@ -73,6 +75,31 @@ function createWindow() {
ipcMain.on('update-menu-bar', (Event: any, payload: any) => {
// win.setTitle("Listening...");
})
let audioData: any[] = [];
ipcMain.on('update-recorder', (Event: any, record: boolean) => {
console.log('updating recorder', record)
if (record) {
recorder.start({
onAudio: (audio: any) => {
audioData.push(audio)
// writeStream.write(audio);
}
})
} else {
recorder.stop()
// send audio to BE
const message = {
text: '',
audio: audioData
}
ws.send(JSON.stringify(message))
console.log('sent', message)
// clear audio.raw file
audioData = [];
}
})
})
win.on("closed", () => {