Merge remote-tracking branch 'origin/main' into audio_test
This commit is contained in:
commit
695bda6c9a
3 changed files with 87 additions and 59 deletions
|
|
@ -3,11 +3,13 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import { sendAudio } from './session'
|
import { sendAudio } from './session'
|
||||||
|
import { ipcMain } from "electron";
|
||||||
|
import { backgroundMitt } from './emitter';
|
||||||
const portAudio = require('naudiodon');
|
const portAudio = require('naudiodon');
|
||||||
|
|
||||||
let ai: typeof portAudio.AudioIO;
|
let ai: typeof portAudio.AudioIO | null = null;
|
||||||
let ao: typeof portAudio.AudioIO;
|
let ao: typeof portAudio.AudioIO | null = null;
|
||||||
const audioInput: Buffer[] = [];
|
let audioInput: Buffer[] = [];
|
||||||
const encoding = "base64";
|
const encoding = "base64";
|
||||||
const audioOptions = {
|
const audioOptions = {
|
||||||
channelCount: 1,
|
channelCount: 1,
|
||||||
|
|
@ -17,10 +19,20 @@ const audioOptions = {
|
||||||
closeOnError: false,
|
closeOnError: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// run every time the main function launches.
|
backgroundMitt.once('window-active', (state: boolean) => {
|
||||||
export const initAudioIO = () => {
|
if(!state) {
|
||||||
// init portAudio readable stream once.
|
stopStream();
|
||||||
if (!ai) {
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// main audio function run by run.ts module.
|
||||||
|
export function initAudioIO(): void {
|
||||||
|
|
||||||
|
if (ai != null) {
|
||||||
|
ai.quit();
|
||||||
|
audioInput = [];
|
||||||
|
}
|
||||||
|
|
||||||
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
||||||
|
|
||||||
// base64 encoding needed for google speech to text.
|
// base64 encoding needed for google speech to text.
|
||||||
|
|
@ -30,41 +42,41 @@ export const initAudioIO = () => {
|
||||||
ai.start();
|
ai.start();
|
||||||
ai.pause();
|
ai.pause();
|
||||||
|
|
||||||
ai.on('error', (e: Error) => {
|
|
||||||
console.log('Error recording audio', + e);
|
|
||||||
});
|
|
||||||
ai.on('data', (chunk: string) => {
|
ai.on('data', (chunk: string) => {
|
||||||
// turn string base64 data into buffer object.
|
// turn string base64 data into buffer object.
|
||||||
console.log('received string chunk of length', chunk.length)
|
|
||||||
const buf = Buffer.from(chunk, encoding);
|
const buf = Buffer.from(chunk, encoding);
|
||||||
audioInput.push(buf);
|
audioInput.push(buf);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// init portAudio writable stream once.
|
if (ao != null) {
|
||||||
if (!ao) {
|
ao.end();
|
||||||
|
}
|
||||||
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
||||||
ao.start();
|
ao.start();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// run this function on space bar key up and down.
|
// callback run on space bar key up and down.
|
||||||
export const updateRecorder = (_event, record: boolean): void => {
|
const updateRecorder = (_event, record: boolean): void => {
|
||||||
if (record) {
|
if (record) {
|
||||||
// resume mic data flow on space bar key down.
|
// resume mic data flow on space bar key down.
|
||||||
|
while(audioInput.length) { audioInput.pop() }
|
||||||
ai.resume();
|
ai.resume();
|
||||||
} else {
|
} else {
|
||||||
// stop mic data flow on space bar key up.
|
// stop mic data flow on space bar key up.
|
||||||
ai.pause();
|
ai.pause();
|
||||||
const audio = Buffer.concat(audioInput);
|
const audio = Buffer.concat(audioInput);
|
||||||
play(audioInput);
|
sendAudio(audio);
|
||||||
// sendAudio(audio);
|
// clear audioInput.
|
||||||
while (audioInput.length) { audioInput.pop(); }
|
while(audioInput.length) { audioInput.pop() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listen for space key up/down event.
|
||||||
|
ipcMain.removeAllListeners('update-recorder');
|
||||||
|
ipcMain.on('update-recorder', updateRecorder);
|
||||||
|
|
||||||
// play audio buffers.
|
// play audio buffers.
|
||||||
export const play = (input: Array<Buffer>) => {
|
export function play(input: Array<Buffer>): void {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
// format buffers into half their size to account for writable highwaterMark.
|
// format buffers into half their size to account for writable highwaterMark.
|
||||||
const audio = bufSplit(input);
|
const audio = bufSplit(input);
|
||||||
|
|
@ -77,6 +89,7 @@ export const play = (input: Array<Buffer>) => {
|
||||||
function write() {
|
function write() {
|
||||||
let chunk: Buffer;
|
let chunk: Buffer;
|
||||||
let ok = true;
|
let ok = true;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
chunk = audio[i];
|
chunk = audio[i];
|
||||||
if (i === audio.length - 1) {
|
if (i === audio.length - 1) {
|
||||||
|
|
@ -97,7 +110,18 @@ export const play = (input: Array<Buffer>) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// utility function used by play func
|
function stopStream() {
|
||||||
|
if(ai != null) {
|
||||||
|
ai.quit();
|
||||||
|
ai = null;
|
||||||
|
}
|
||||||
|
if (ao != null) {
|
||||||
|
ao.quit();
|
||||||
|
ao = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility function used by play func.
|
||||||
function bufSplit(input: Array<Buffer>): Array<Buffer> {
|
function bufSplit(input: Array<Buffer>): Array<Buffer> {
|
||||||
const result: Buffer[] = [];
|
const result: Buffer[] = [];
|
||||||
input.forEach((b: Buffer) => {
|
input.forEach((b: Buffer) => {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
|
|
@ -6,7 +7,7 @@ import { backgroundMitt } from './emitter';
|
||||||
|
|
||||||
let winActive: boolean;
|
let winActive: boolean;
|
||||||
|
|
||||||
backgroundMitt.on('window-active', (state: boolean) => {
|
backgroundMitt.once('window-active', (state: boolean) => {
|
||||||
winActive = state;
|
winActive = state;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -23,7 +24,6 @@ export function initApp(dev: boolean): void {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Debug activate functionaity: currently not working.
|
|
||||||
// Might have to de-reference window object
|
// Might have to de-reference window object
|
||||||
app.on("activate", () => {
|
app.on("activate", () => {
|
||||||
if (winActive === false) {
|
if (winActive === false) {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
import { BrowserWindow, ipcMain } from "electron";
|
import { BrowserWindow, ipcMain } from "electron";
|
||||||
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
||||||
import { backgroundMitt } from './emitter';
|
import { backgroundMitt } from './emitter';
|
||||||
import { updateRecorder } from './audio';
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
interface WindowSettings {
|
interface WindowSettings {
|
||||||
|
|
@ -28,31 +27,48 @@ interface IpcRendererPayload {
|
||||||
|
|
||||||
let win: BrowserWindow | null;
|
let win: BrowserWindow | null;
|
||||||
|
|
||||||
const windowMount = (): void => {
|
// Util function to handle window close & minimize.
|
||||||
backgroundMitt.emit('window-active', true);
|
const navBarHandler = (_event, action: string): void => {
|
||||||
ipcMain.removeAllListeners('update-recorder');
|
switch(action) {
|
||||||
ipcMain.on('update-recorder', updateRecorder);
|
case 'close':
|
||||||
// handle renderer auth-token event
|
if (win) win.close();
|
||||||
ipcMain.removeHandler('nav-bar'); // avoid setting duplicate handlers
|
break;
|
||||||
ipcMain.handle('nav-bar', navBarHandler);
|
case 'min':
|
||||||
|
if (win) win.minimize();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const windowDismount = (): void => {
|
// Util function to render message on ipc-renderer event.
|
||||||
win = null;
|
const renderMessage = (payload: IpcRendererPayload): void => {
|
||||||
backgroundMitt.emit('window-active', false);
|
if (win) {
|
||||||
ipcMain.removeAllListeners('update-recorder');
|
|
||||||
}
|
|
||||||
|
|
||||||
backgroundMitt.on('ipc-renderer', (payload: IpcRendererPayload) => {
|
|
||||||
if (win)
|
|
||||||
win.webContents.send(payload.endpoint, {
|
win.webContents.send(payload.endpoint, {
|
||||||
message: payload.message
|
message: payload.message
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const createWindow = async (options: WindowSettings): Promise<void> => {
|
// Do this on window mount.
|
||||||
|
const windowMount = (): void => {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// function used by run.ts to create the main window.
|
||||||
|
export async function createWindow(options: WindowSettings): Promise<void> {
|
||||||
return new Promise((resolve, _reject) => {
|
return new Promise((resolve, _reject) => {
|
||||||
|
|
||||||
|
// avoid creating duplicate windows.
|
||||||
if (win) resolve();
|
if (win) resolve();
|
||||||
|
|
||||||
win = new BrowserWindow({
|
win = new BrowserWindow({
|
||||||
|
|
@ -94,16 +110,4 @@ export const createWindow = async (options: WindowSettings): Promise<void> => {
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue