<template>
- <div id="app" v-if="!loading">
+ <div id="app" v-if="ready">
<button class="titlebar">
</template>
<script lang="ts">
-import {defineComponent} from 'vue';
+import { defineComponent, onMounted, onUnmounted, ref } from 'vue';
import { useIpc } from "@/modules/ipc";
import { useAuth } from "@/modules/auth";
import Splash from "@/components/splash.vue"
components: { Splash },
setup() {
-
const { invoke } = useIpc();
- const { loading } = useAuth();
+ const ready = ref(false);
+
+ onMounted(() => {
+ window.ipcRenderer.on('window-ready', (_event, payload: {message: boolean}) => {
+ ready.value = payload.message;
+ })
+ });
+
+ onUnmounted(() => {
+ window.ipcRenderer.removeAllListeners('window-ready')
+ })
const callNavbar = (action: string) => {
invoke('nav-bar', action);
return {
callNavbar,
- loading
+ ready
}
}
})
import { sendAudio } from './session'
import { ipcMain } from "electron";
-import { backgroundMitt } from './emitter';
const portAudio = require('naudiodon');
let ai: typeof portAudio.AudioIO | null = null;
let ao: typeof portAudio.AudioIO | null = null;
-const audioInput: Buffer[] = [];
+let record = false;
const audioContainer = {
input: '',
}
-const audio: Buffer | null = null;
const encoding = "base64";
const audioOptions = {
channelCount: 1,
closeOnError: false,
}
-backgroundMitt.once('window-active', (active: boolean) => {
- if(!active) {
- stopStream();
- }
-});
-
-// main audio function run by run.ts module.
-export function initAudioIO(): void {
+// callback run on space bar key up and down.
+const updateRecorder = (_event, payload: boolean): void => { record = payload };
- if (ai != null) {
- ai.quit();
- }
+// listen for space key up/down event.
+ipcMain.removeAllListeners('update-recorder');
+ipcMain.on('update-recorder', updateRecorder);
- ai = new portAudio.AudioIO({ inOptions: audioOptions });
+// utility function used by play func.
+function bufSplit(input: Array<Buffer>): Array<Buffer> {
+ const result: Buffer[] = [];
+ input.forEach((b: Buffer) => {
+ // split buffer into two.
+ result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
+ });
+ return result;
+}
- // base64 encoding needed for google speech to text.
- ai.setEncoding(encoding);
+// main audio function run by run.ts module.
+export function initAudioIO(): void {
- // pause the portAudio data flow as soon as stream is initiated.
- ai.start();
- ai.pause();
+ if (!ai) {
+ ai = new portAudio.AudioIO({ inOptions: audioOptions });
- ai.on('data', (chunk: string) => {
- // turn string base64 data into buffer object.
- audioContainer.input += chunk;
- // const buf = Buffer.from(chunk, encoding);
- // audioInput.push(buf);
- });
+ // base64 encoding needed for google speech to text.
+ ai.setEncoding(encoding);
+ ai.start();
- if (ao != null) {
- ao.end();
+ ai.on('data', (chunk: string) => {
+ if (record) {
+ console.log('recording data')
+ audioContainer.input += chunk;
+ } else {
+ if (audioContainer.input.length) audioContainer.input = "";
+ }
+ });
}
- ao = new portAudio.AudioIO({ outOptions: audioOptions });
- ao.start();
-}
-// callback run on space bar key up and down.
-const updateRecorder = (_event, record: boolean): void => {
- if (record) {
- // resume mic data flow on space bar key down.
- // while(audioInput.length) { audioInput.pop() }
- if(audioContainer.input.length) audioContainer.input = '';
- ai.resume();
- } else {
- // stop mic data flow on space bar key up.
- ai.pause();
- const buf = Buffer.from(audioContainer.input, encoding);
- sendAudio(buf);
- // clear audioInput.
- audioContainer.input = '';
- // while(audioInput.length) { audioInput.pop() }
+ if (!ao) {
+ ao = new portAudio.AudioIO({ outOptions: audioOptions });
+ ao.start();
}
}
-// listen for space key up/down event.
-ipcMain.removeAllListeners('update-recorder');
-ipcMain.on('update-recorder', updateRecorder);
-
// play audio buffers.
export function play(input: Array<Buffer>): void {
let i = 0;
}
}
-function stopStream() {
+export function stopStream() {
if(ai != null) {
ai.quit();
ai = null;
}
}
-// utility function used by play func.
-function bufSplit(input: Array<Buffer>): Array<Buffer> {
- const result: Buffer[] = [];
- input.forEach((b: Buffer) => {
- // split buffer into two.
- result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
- });
- return result;
-}
import { app } from "electron";
import { main } from './run';
import { backgroundMitt } from './emitter';
+import { stopStream } from './audio';
let winActive: boolean;
-backgroundMitt.once('window-active', (state: boolean) => {
+backgroundMitt.on('window-active', (state: boolean) => {
winActive = state;
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
+ stopStream();
app.quit();
}
});
- // Might have to de-reference window object
app.on("activate", () => {
if (winActive === false) {
main();
ipcMain.removeHandler('nav-bar'); // avoid setting duplicate handlers
ipcMain.handle('nav-bar', navBarHandler);
// render messages through ipc-renderer.
- backgroundMitt.once('ipc-renderer', renderMessage);
+ backgroundMitt.on('ipc-renderer', renderMessage);
}
// do this on window dismount.
backgroundMitt.emit('window-active', false);
}
-// function used by run.ts to create the main window.
+// function used by run.ts to create the main window.
export async function createWindow(options: WindowSettings): Promise<void> {
return new Promise((resolve, _reject) => {
}
});
+
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
})
win.webContents.on('did-finish-load', () => {
- windowMount();
+ if (win) win.webContents.send('window-ready', {
+ message: true
+ });
+ windowMount();
resolve();
});