add speechrecorder to background
This commit is contained in:
parent
51c7f63526
commit
3f52457956
3 changed files with 43 additions and 4 deletions
|
|
@ -6,12 +6,14 @@ import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
|
||||||
const isDevelopment = process.env.NODE_ENV !== "production";
|
const isDevelopment = process.env.NODE_ENV !== "production";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { SpeechRecorder } from "speech-recorder";
|
import { SpeechRecorder } from "speech-recorder";
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
// TODO: connect To Crimata-BE
|
// TODO: connect To Crimata-BE
|
||||||
import WebSocket from "ws";
|
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
|
// 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.
|
// 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) => {
|
ipcMain.on('update-menu-bar', (Event: any, payload: any) => {
|
||||||
// win.setTitle("Listening...");
|
// 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", () => {
|
win.on("closed", () => {
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,11 @@ export default function useInputController() {
|
||||||
function keyupHandler(e: any) {
|
function keyupHandler(e: any) {
|
||||||
if (e.key === " " && visualizeStream.value) {
|
if (e.key === " " && visualizeStream.value) {
|
||||||
console.log("stop recording");
|
console.log("stop recording");
|
||||||
recorder.stop();
|
// recorder.stop();
|
||||||
|
window.postMessage({
|
||||||
|
myTypeField: 'update-recorder',
|
||||||
|
record: false
|
||||||
|
}, '*')
|
||||||
visualizeStream.value = false;
|
visualizeStream.value = false;
|
||||||
paths.value = []
|
paths.value = []
|
||||||
audioBubbleWidth.value = 10;
|
audioBubbleWidth.value = 10;
|
||||||
|
|
@ -55,7 +59,6 @@ export default function useInputController() {
|
||||||
recorder = initRecorder(stream);
|
recorder = initRecorder(stream);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
// determine whether to render text or audio based one the keyboard input
|
// determine whether to render text or audio based one the keyboard input
|
||||||
watch(input, (input, prevInput) => {
|
watch(input, (input, prevInput) => {
|
||||||
if (prevInput !== "" || prevInput.length > 0) {
|
if (prevInput !== "" || prevInput.length > 0) {
|
||||||
|
|
@ -67,7 +70,12 @@ export default function useInputController() {
|
||||||
if (input === " " && visualizeStream.value === false) {
|
if (input === " " && visualizeStream.value === false) {
|
||||||
console.log("record MediaStream");
|
console.log("record MediaStream");
|
||||||
visualizeStream.value = true;
|
visualizeStream.value = true;
|
||||||
recorder.start();
|
// recorder.start();
|
||||||
|
|
||||||
|
window.postMessage({
|
||||||
|
myTypeField: 'update-recorder',
|
||||||
|
record: true
|
||||||
|
}, '*')
|
||||||
expandBubble(audioBubbleWidth);
|
expandBubble(audioBubbleWidth);
|
||||||
visualizeStreamAsPaths(stream, paths, audioBubbleWidth);
|
visualizeStreamAsPaths(stream, paths, audioBubbleWidth);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ process.once("loaded", () => {
|
||||||
if (message.myTypeField === "update-menu-bar") {
|
if (message.myTypeField === "update-menu-bar") {
|
||||||
ipcRenderer.send("update-menu-bar", message);
|
ipcRenderer.send("update-menu-bar", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message.myTypeField === "update-recorder") {
|
||||||
|
ipcRenderer.send("update-recorder", message.record);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue