diff --git a/src/background/audio.ts b/src/background/audio.ts index e1e7562..ac41d40 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -9,7 +9,11 @@ const portAudio = require('naudiodon'); let ai: typeof portAudio.AudioIO | null = null; let ao: typeof portAudio.AudioIO | null = null; -let audioInput: Buffer[] = []; +const audioInput: Buffer[] = []; +const audioContainer = { + input: '', +} +const audio: Buffer | null = null; const encoding = "base64"; const audioOptions = { channelCount: 1, @@ -19,8 +23,8 @@ const audioOptions = { closeOnError: false, } -backgroundMitt.once('window-active', (state: boolean) => { - if(!state) { +backgroundMitt.once('window-active', (active: boolean) => { + if(!active) { stopStream(); } }); @@ -30,7 +34,6 @@ export function initAudioIO(): void { if (ai != null) { ai.quit(); - audioInput = []; } ai = new portAudio.AudioIO({ inOptions: audioOptions }); @@ -44,8 +47,9 @@ export function initAudioIO(): void { ai.on('data', (chunk: string) => { // turn string base64 data into buffer object. - const buf = Buffer.from(chunk, encoding); - audioInput.push(buf); + audioContainer.input += chunk; + // const buf = Buffer.from(chunk, encoding); + // audioInput.push(buf); }); if (ao != null) { @@ -59,15 +63,17 @@ export function initAudioIO(): void { const updateRecorder = (_event, record: boolean): void => { if (record) { // resume mic data flow on space bar key down. - while(audioInput.length) { audioInput.pop() } + // 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 audio = Buffer.concat(audioInput); - sendAudio(audio); + const buf = Buffer.from(audioContainer.input, encoding); + sendAudio(buf); // clear audioInput. - while(audioInput.length) { audioInput.pop() } + audioContainer.input = ''; + // while(audioInput.length) { audioInput.pop() } } }