let ai: typeof portAudio.AudioIO | null = null;
let ao: typeof portAudio.AudioIO | null = null;
let audioInput: Buffer[] = [];
+const audioContainer = {
+ input: '',
+}
+let audio: Buffer | null = null;
const encoding = "base64";
const audioOptions = {
channelCount: 1,
closeOnError: false,
}
-backgroundMitt.once('window-active', (state: boolean) => {
- if(!state) {
+backgroundMitt.once('window-active', (active: boolean) => {
+ if(!active) {
stopStream();
}
});
if (ai != null) {
ai.quit();
- audioInput = [];
}
ai = new portAudio.AudioIO({ inOptions: audioOptions });
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) {
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() }
}
}