From d113de2aa1b9e8519a990d833157b3117ec9d150 Mon Sep 17 00:00:00 2001 From: Enrique Hernandez Date: Wed, 3 Mar 2021 12:49:32 -0600 Subject: [PATCH] testing audio --- src/background/audio.ts | 76 +++++++++++++++++++++++++--------------- src/background/init.ts | 4 +-- src/background/window.ts | 2 +- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/background/audio.ts b/src/background/audio.ts index 7b8066b..e1e7562 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -4,10 +4,11 @@ import { sendAudio } from './session' import { ipcMain } from "electron"; +import { backgroundMitt } from './emitter'; const portAudio = require('naudiodon'); -let ai: typeof portAudio.AudioIO; -let ao: typeof portAudio.AudioIO; +let ai: typeof portAudio.AudioIO | null = null; +let ao: typeof portAudio.AudioIO | null = null; let audioInput: Buffer[] = []; const encoding = "base64"; const audioOptions = { @@ -18,41 +19,47 @@ const audioOptions = { closeOnError: false, } -// main function run by run.ts module. +backgroundMitt.once('window-active', (state: boolean) => { + if(!state) { + stopStream(); + } +}); + +// main audio 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); + if (ai != null) { + ai.quit(); + audioInput = []; + } - // pause the portAudio data flow as soon as stream is initiated. - ai.start(); - ai.pause(); + ai = new portAudio.AudioIO({ inOptions: audioOptions }); - 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); - }); - } + // base64 encoding needed for google speech to text. + ai.setEncoding(encoding); - // init portAudio writable stream once. - if (!ao) { - ao = new portAudio.AudioIO({ outOptions: audioOptions }); - ao.start(); - } + // pause the portAudio data flow as soon as stream is initiated. + ai.start(); + ai.pause(); + + ai.on('data', (chunk: string) => { + // turn string base64 data into buffer object. + const buf = Buffer.from(chunk, encoding); + audioInput.push(buf); + }); + + if (ao != null) { + ao.end(); + } + ao = new portAudio.AudioIO({ outOptions: audioOptions }); + ao.start(); } -// run this function on space bar key up and down. +// 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() } ai.resume(); } else { // stop mic data flow on space bar key up. @@ -60,7 +67,7 @@ const updateRecorder = (_event, record: boolean): void => { const audio = Buffer.concat(audioInput); sendAudio(audio); // clear audioInput. - while(audioInput.length) { audioInput.pop()} + while(audioInput.length) { audioInput.pop() } } } @@ -103,9 +110,20 @@ export function play(input: Array): void { } } +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): Array { - let result: 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)); diff --git a/src/background/init.ts b/src/background/init.ts index d41ab56..c7e0082 100644 --- a/src/background/init.ts +++ b/src/background/init.ts @@ -1,3 +1,4 @@ + "use strict"; import { app } from "electron"; @@ -6,7 +7,7 @@ import { backgroundMitt } from './emitter'; let winActive: boolean; -backgroundMitt.on('window-active', (state: boolean) => { +backgroundMitt.once('window-active', (state: boolean) => { 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 app.on("activate", () => { if (winActive === false) { diff --git a/src/background/window.ts b/src/background/window.ts index 6d1002b..fdf5403 100644 --- a/src/background/window.ts +++ b/src/background/window.ts @@ -110,4 +110,4 @@ export async function createWindow(options: WindowSettings): Promise { }); }); -}; +} -- 2.43.0