From 230efb325ce09e805e03ea966cb5d91158cb6127 Mon Sep 17 00:00:00 2001 From: riqo Date: Fri, 6 Nov 2020 16:06:08 -0600 Subject: [PATCH] add ipcRenderer render-audio event to window object --- src/background.ts | 34 +++++++++++++++++++++------------- src/components/AI/index.vue | 6 ++++++ src/preload.ts | 29 +++++------------------------ 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/background.ts b/src/background.ts index 9204046..2aaec65 100644 --- a/src/background.ts +++ b/src/background.ts @@ -6,7 +6,9 @@ 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"; -import WebSocket from "ws"; + +// TODO: connect To Crimata-BE +// import WebSocket from "ws"; // const ws = new WebSocket("ws://localhost:8080"); // ws.on("open", function open() { @@ -16,18 +18,8 @@ import WebSocket from "ws"; // console.log("received message", message); // }); // -// const recorder = new SpeechRecorder(); -// -// recorder.start({ -// onAudio: (audio: object, speech: boolean) => { -// if (speech) { -// console.log(audio); -// ws.send(audio); -// } -// console.log("recording"); -// // writeStream.write(audio); -// } -// }); + +const recorder = new SpeechRecorder(); // 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. @@ -62,6 +54,22 @@ function createWindow() { win.loadURL("app://./index.html"); } + win.webContents.on('did-finish-load', () => { + if (win) { + recorder.start({ + onAudio: (audio: object, speech: boolean) => { + if (win) { + win.webContents.send('render-audio', { data: audio }) + } + if (speech) { + // TODO: send audio to crimata BE + // ws.send(audio); + } + } + }); + } + }) + win.on("closed", () => { win = null; }); diff --git a/src/components/AI/index.vue b/src/components/AI/index.vue index 9139461..02bb6a9 100644 --- a/src/components/AI/index.vue +++ b/src/components/AI/index.vue @@ -19,6 +19,7 @@ + { + console.log(audio); + }); + }, }); diff --git a/src/preload.ts b/src/preload.ts index a5c911d..9e33238 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -1,27 +1,8 @@ // All of the Node.js APIs are available in the preload process. // It has the same sandbox as a Chrome extension. -import ipcRenderer from "electron"; +import { ipcRenderer } from "electron"; -process.once("loaded", () => { - window.addEventListener("message", event => { - // do something with custom event - const message = event.data; - - if (message.myTypeField === "handle-audio-stream") { - ipcRenderer.send("audio-stream", message); - } - }); -}); - -window.addEventListener("DOMContentLoaded", () => { - const replaceText = (selector: string, text: string) => { - const element = document.getElementById(selector); - if (element) { - element.innerText = text; - } - }; - - for (const type of ["chrome", "node", "electron"]) { - replaceText(`${type}-version`, (process.versions as any)[type]); - } -}); +interface Window { + ipcRenderer: typeof ipcRenderer; +} +window.ipcRenderer = ipcRenderer; -- 2.43.0