Merge remote-tracking branch 'origin/main' into main

Merge audio code into new UI code
This commit is contained in:
Andrew Gundersen 2021-03-04 11:05:41 -06:00
commit 83263d5f23
8 changed files with 219 additions and 118 deletions

View file

@ -3,11 +3,15 @@
"use strict";
import { sendAudio } from './session'
import { ipcMain } from "electron";
const portAudio = require('naudiodon');
let ai: typeof portAudio.AudioIO;
let ao: typeof portAudio.AudioIO;
let audioInput: Buffer[] = [];
let ai: typeof portAudio.AudioIO | null = null;
let ao: typeof portAudio.AudioIO | null = null;
let record = false;
const audioContainer = {
input: '',
}
const encoding = "base64";
const audioOptions = {
channelCount: 1,
@ -17,53 +21,55 @@ 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 });
// callback run on space bar key up and down.
const updateRecorder = (_event, payload: boolean): void => {
record = payload;
if (!record) {
sendAudio(Buffer.from(audioContainer.input, 'base64'));
}
};
// base64 encoding needed for google speech to text.
ai.setEncoding(encoding);
// listen for space key up/down event.
ipcMain.removeAllListeners('update-recorder');
ipcMain.on('update-recorder', updateRecorder);
// 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);
// 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));
});
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();
}
return result;
}
// run this function on space bar key up and down.
export const updateRecorder = (_event, record: boolean): void => {
if (record) {
// resume mic data flow on space bar key down.
ai.resume();
} else {
// stop mic data flow on space bar key up.
ai.pause();
const audio = Buffer.concat(audioInput);
sendAudio(audio);
audioInput = [];
// main audio function run by run.ts module.
export function initAudioIO(): void {
if (!ai) {
ai = new portAudio.AudioIO({ inOptions: audioOptions });
// base64 encoding needed for google speech to text.
ai.setEncoding(encoding);
ai.start();
ai.on('data', (chunk: string) => {
if (record) {
audioContainer.input += chunk;
} else {
if (audioContainer.input.length) audioContainer.input = "";
}
});
}
if (!ao) {
ao = new portAudio.AudioIO({ outOptions: audioOptions });
ao.start();
}
}
// play audio buffers.
export const play = (input: Array<Buffer>) => {
export function play(input: Array<Buffer>): 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<Buffer>) => {
function write() {
let chunk: Buffer;
let ok = true;
do {
chunk = audio[i];
if (i === audio.length - 1) {
@ -96,12 +103,14 @@ export const play = (input: Array<Buffer>) => {
}
}
// utility function used by play func
function bufSplit(input: Array<Buffer>): Array<Buffer> {
let 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;
export function stopStream() {
if(ai != null) {
ai.quit();
ai = null;
}
if (ao != null) {
ao.quit();
ao = null;
}
}

View file

@ -1,8 +1,10 @@
"use strict";
import { app } from "electron";
import { main } from './run';
import { backgroundMitt } from './emitter';
import { stopStream } from './audio';
let winActive: boolean;
@ -19,12 +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();
}
});
// TODO: Debug activate functionaity: currently not working.
// Might have to de-reference window object
app.on("activate", () => {
if (winActive === false) {
main();

View file

@ -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.on('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<void> => {
// function used by run.ts to create the main window.
export async function createWindow(options: WindowSettings): Promise<void> {
return new Promise((resolve, _reject) => {
// avoid creating duplicate windows.
if (win) resolve();
win = new BrowserWindow({
@ -72,6 +88,7 @@ export const createWindow = async (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);
@ -89,21 +106,12 @@ export const createWindow = async (options: WindowSettings): Promise<void> => {
})
win.webContents.on('did-finish-load', () => {
windowMount();
if (win) win.webContents.send('window-ready', {
message: true
});
windowMount();
resolve();
});
});
};
// 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;
}
}