]> Repos - mime-chat/commitdiff
fix splash screen
authorriqo <hernandeze2@xavier.edu>
Thu, 4 Mar 2021 16:36:53 +0000 (10:36 -0600)
committerriqo <hernandeze2@xavier.edu>
Thu, 4 Mar 2021 16:36:53 +0000 (10:36 -0600)
src/App.vue
src/background/audio.ts
src/background/init.ts
src/background/window.ts
src/main.ts
src/modules/auth.ts
src/views/messenger.vue
tests/audio.js

index 17e28a121af9e3470f85f6299079acdb2b257b4f..3c96bf19049ccd42adddd0dcdf9b5767e2d13283 100644 (file)
@@ -1,5 +1,5 @@
 <template>
-  <div id="app" v-if="!loading">
+  <div id="app" v-if="ready">
 
     <button class="titlebar">
 
@@ -19,7 +19,7 @@
 </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"
@@ -28,9 +28,18 @@ export default defineComponent({
   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);
@@ -38,7 +47,7 @@ export default defineComponent({
 
     return {
         callNavbar,
-        loading
+        ready
     }
   }
 })
index b3ba49179b7f1858dc8e2a67e383e6b03be017ad..eba16caa9ea4aa1d42275e67a9d4f35a992b95dd 100644 (file)
@@ -4,16 +4,14 @@
 
 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,
@@ -23,64 +21,49 @@ const audioOptions = {
     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;
@@ -116,7 +99,7 @@ export function play(input: Array<Buffer>): void {
     }
 }
 
-function stopStream() {
+export function stopStream() {
     if(ai != null) {
         ai.quit();
         ai = null;
@@ -127,12 +110,3 @@ function stopStream() {
     }
 }
 
-// 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;
-}
index c7e0082a9398ae07be11b99911540b40721e15e5..0f60068dea08420b7e1e87f30ef6e5fbcb808d3b 100644 (file)
@@ -4,10 +4,11 @@
 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;
 });
 
@@ -20,11 +21,11 @@ export function initApp(dev: boolean): void {
     // 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();
index fdf54034c883f31c5c5f75d21a3f723391136afa..31c8850fb8a36a2d95b648e5ec2982ef8a64263b 100644 (file)
@@ -55,7 +55,7 @@ const windowMount = (): void => {
     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.
@@ -64,7 +64,7 @@ const windowDismount = (): void => {
     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) => {
 
@@ -88,6 +88,7 @@ export async function createWindow(options: WindowSettings): Promise<void> {
       }
     });
 
+
     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);
@@ -105,7 +106,10 @@ export async function createWindow(options: WindowSettings): Promise<void> {
     })
 
     win.webContents.on('did-finish-load', () => {
-      windowMount();      
+        if (win) win.webContents.send('window-ready', {
+            message: true
+        });
+      windowMount();
       resolve();
     });
 
index c03e8b6d47a03f51da61947a331d05231399d078..e1965fdccebc1090a45dc7173aed8c79b1592653 100644 (file)
@@ -7,6 +7,7 @@ import mitt from "mitt";
 import anime from "animejs";
 import { createApp } from "vue";
 
+
 // Handle events.
 const emitter = mitt();
 
index c541c3f44b015c9e76253ddf2a9a8f05b4605cd2..161545e947c981d3d9cff452548024f609e5d61e 100644 (file)
@@ -11,8 +11,6 @@ const state = reactive<AuthState>({
     error: undefined,
 });
 
-const loading = ref(true);
-
 const AUTH_KEY = 'crimata_token';
 
 const token = window.localStorage.getItem(AUTH_KEY);
@@ -22,7 +20,6 @@ if (token) {
 }
 
 const authToken = async () => {
-   loading.value = true;
    const { invoke  } = useIpc();
     try {
         const res = await invoke('auth-session', token);
@@ -35,7 +32,6 @@ const authToken = async () => {
         window.localStorage.removeItem(AUTH_KEY);
         state.accessToken = null;
     }
-    loading.value = false;
 }
 
 // authenticate on auth event
@@ -60,6 +56,5 @@ export const useAuth = () => {
         setToken,
         logout,
         ...toRefs(state), // accessToken, error
-        loading
     }
 }
index 25ebd75ccf986feda2f8511d75843afc2b940ca5..30c67f34e6307c9a6fcb21b2e01256f90f785116 100644 (file)
@@ -3,7 +3,7 @@
     <TextAudioInput />
 
     <!-- List of message bubbles. -->
-    <div id="messenger" >
+    <div id="messenger">
       <MessageItem v-for="message in messages" :message="message" :key="message.id"/>
     </div>
  
index ee6ae26d0a70cb18b70b36422a5f2430db71c450..31acfcacb5d90209d3636b2ae38e1ac3da5b1a8a 100644 (file)
@@ -71,7 +71,7 @@ ia.on('data', (chunk) => {
 const ao = new portAudio.AudioIO({
     outOptions: {
         sampleFormat: 16,
-            channelCount: 1,
+        channelCount: 1,
         sampleRate: 16000,
         deviceId: -1,
         closeOnError: false,