From 426b847ccecc51b0725e81c1652cdade213612e4 Mon Sep 17 00:00:00 2001 From: Enrique Hernandez Date: Wed, 3 Mar 2021 11:20:16 -0600 Subject: [PATCH] minor improvements to background audio & window modules --- src/background/audio.ts | 65 ++++++++++++++++++++++------------------ src/background/window.ts | 64 ++++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 59 deletions(-) diff --git a/src/background/audio.ts b/src/background/audio.ts index 4424d44..7b8066b 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -3,6 +3,7 @@ "use strict"; import { sendAudio } from './session' +import { ipcMain } from "electron"; const portAudio = require('naudiodon'); let ai: typeof portAudio.AudioIO; @@ -17,39 +18,39 @@ const audioOptions = { closeOnError: false, } -// run every time the main function launches. -export const initAudioIO = () => { - // init portAudio readable stream once. - if (!ai) { - ai = new portAudio.AudioIO({ inOptions: audioOptions }); +// main function run by run.ts module. +export function initAudioIO(): void { + // init portAudio readable stream once. + if (!ai) { + ai = new portAudio.AudioIO({ inOptions: audioOptions }); - // base64 encoding needed for google speech to text. - ai.setEncoding(encoding); + // base64 encoding needed for google speech to text. + ai.setEncoding(encoding); - // pause the portAudio data flow as soon as stream is initiated. - ai.start(); - ai.pause(); + // pause the portAudio data flow as soon as stream is initiated. + ai.start(); + ai.pause(); - ai.on('error', (e: Error) => { - console.log('Error recording audio', + e); - }); - ai.on('data', (chunk: string) => { - // turn string base64 data into buffer object. - console.log('received string chunk of length', chunk.length) - const buf = Buffer.from(chunk, encoding); - audioInput.push(buf); - }); - } + ai.on('error', (e: Error) => { + console.log('Error recording audio', + e); + }); + ai.on('data', (chunk: string) => { + // turn string base64 data into buffer object. + console.log('received string chunk of length', chunk.length) + const buf = Buffer.from(chunk, encoding); + audioInput.push(buf); + }); + } - // init portAudio writable stream once. - if (!ao) { - ao = new portAudio.AudioIO({ outOptions: audioOptions }); - ao.start(); - } + // init portAudio writable stream once. + if (!ao) { + ao = new portAudio.AudioIO({ outOptions: audioOptions }); + ao.start(); + } } // run this function on space bar key up and down. -export const updateRecorder = (_event, record: boolean): void => { +const updateRecorder = (_event, record: boolean): void => { if (record) { // resume mic data flow on space bar key down. ai.resume(); @@ -58,12 +59,17 @@ export const updateRecorder = (_event, record: boolean): void => { ai.pause(); const audio = Buffer.concat(audioInput); sendAudio(audio); - audioInput = []; + // clear audioInput. + while(audioInput.length) { audioInput.pop()} } } +// listen for space key up/down event. +ipcMain.removeAllListeners('update-recorder'); +ipcMain.on('update-recorder', updateRecorder); + // play audio buffers. -export const play = (input: Array) => { +export function play(input: Array): void { let i = 0; // format buffers into half their size to account for writable highwaterMark. const audio = bufSplit(input); @@ -76,6 +82,7 @@ export const play = (input: Array) => { function write() { let chunk: Buffer; let ok = true; + do { chunk = audio[i]; if (i === audio.length - 1) { @@ -96,7 +103,7 @@ export const play = (input: Array) => { } } -// utility function used by play func +// utility function used by play func. function bufSplit(input: Array): Array { let result: Buffer[] = []; input.forEach((b: Buffer) => { diff --git a/src/background/window.ts b/src/background/window.ts index b84a742..6d1002b 100644 --- a/src/background/window.ts +++ b/src/background/window.ts @@ -3,7 +3,6 @@ import { BrowserWindow, ipcMain } from "electron"; import { createProtocol } from "vue-cli-plugin-electron-builder/lib"; import { backgroundMitt } from './emitter'; -import { updateRecorder } from './audio'; import * as path from "path"; interface WindowSettings { @@ -28,31 +27,48 @@ interface IpcRendererPayload { let win: BrowserWindow | null; +// Util function to handle window close & minimize. +const navBarHandler = (_event, action: string): void => { + switch(action) { + case 'close': + if (win) win.close(); + break; + case 'min': + if (win) win.minimize(); + break; + } +} + +// Util function to render message on ipc-renderer event. +const renderMessage = (payload: IpcRendererPayload): void => { + if (win) { + win.webContents.send(payload.endpoint, { + message: payload.message + }); + } +} + +// Do this on window mount. const windowMount = (): void => { - backgroundMitt.emit('window-active', true); - ipcMain.removeAllListeners('update-recorder'); - ipcMain.on('update-recorder', updateRecorder); - // handle renderer auth-token event - ipcMain.removeHandler('nav-bar'); // avoid setting duplicate handlers - ipcMain.handle('nav-bar', navBarHandler); + backgroundMitt.emit('window-active', true); + // handle win nav-bar event. + ipcMain.removeHandler('nav-bar'); // avoid setting duplicate handlers + ipcMain.handle('nav-bar', navBarHandler); + // render messages through ipc-renderer. + backgroundMitt.once('ipc-renderer', renderMessage); } +// do this on window dismount. const windowDismount = (): void => { - win = null; - backgroundMitt.emit('window-active', false); - ipcMain.removeAllListeners('update-recorder'); + win = null; + backgroundMitt.emit('window-active', false); } -backgroundMitt.on('ipc-renderer', (payload: IpcRendererPayload) => { - if (win) - win.webContents.send(payload.endpoint, { - message: payload.message - }); -}); - -export const createWindow = async (options: WindowSettings): Promise => { +// function used by run.ts to create the main window. +export async function createWindow(options: WindowSettings): Promise { return new Promise((resolve, _reject) => { + // avoid creating duplicate windows. if (win) resolve(); win = new BrowserWindow({ @@ -95,15 +111,3 @@ export const createWindow = async (options: WindowSettings): Promise => { }); }; - -// call this function on nav bar click -function navBarHandler(_event, action: string) { - switch(action) { - case 'close': - if (win) win.close(); - break; - case 'min': - if (win) win.minimize(); - break; - } -} -- 2.43.0