minor tweaks to audioInput
This commit is contained in:
parent
d113de2aa1
commit
f43960b3a1
1 changed files with 15 additions and 9 deletions
|
|
@ -10,6 +10,10 @@ const portAudio = require('naudiodon');
|
||||||
let ai: typeof portAudio.AudioIO | null = null;
|
let ai: typeof portAudio.AudioIO | null = null;
|
||||||
let ao: typeof portAudio.AudioIO | null = null;
|
let ao: typeof portAudio.AudioIO | null = null;
|
||||||
let audioInput: Buffer[] = [];
|
let audioInput: Buffer[] = [];
|
||||||
|
const audioContainer = {
|
||||||
|
input: '',
|
||||||
|
}
|
||||||
|
let audio: Buffer | null = null;
|
||||||
const encoding = "base64";
|
const encoding = "base64";
|
||||||
const audioOptions = {
|
const audioOptions = {
|
||||||
channelCount: 1,
|
channelCount: 1,
|
||||||
|
|
@ -19,8 +23,8 @@ const audioOptions = {
|
||||||
closeOnError: false,
|
closeOnError: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
backgroundMitt.once('window-active', (state: boolean) => {
|
backgroundMitt.once('window-active', (active: boolean) => {
|
||||||
if(!state) {
|
if(!active) {
|
||||||
stopStream();
|
stopStream();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -30,7 +34,6 @@ export function initAudioIO(): void {
|
||||||
|
|
||||||
if (ai != null) {
|
if (ai != null) {
|
||||||
ai.quit();
|
ai.quit();
|
||||||
audioInput = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
||||||
|
|
@ -44,8 +47,9 @@ export function initAudioIO(): void {
|
||||||
|
|
||||||
ai.on('data', (chunk: string) => {
|
ai.on('data', (chunk: string) => {
|
||||||
// turn string base64 data into buffer object.
|
// turn string base64 data into buffer object.
|
||||||
const buf = Buffer.from(chunk, encoding);
|
audioContainer.input += chunk;
|
||||||
audioInput.push(buf);
|
// const buf = Buffer.from(chunk, encoding);
|
||||||
|
// audioInput.push(buf);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ao != null) {
|
if (ao != null) {
|
||||||
|
|
@ -59,15 +63,17 @@ export function initAudioIO(): void {
|
||||||
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() }
|
// while(audioInput.length) { audioInput.pop() }
|
||||||
|
if(audioContainer.input.length) audioContainer.input = '';
|
||||||
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 buf = Buffer.from(audioContainer.input, encoding);
|
||||||
sendAudio(audio);
|
sendAudio(buf);
|
||||||
// clear audioInput.
|
// clear audioInput.
|
||||||
while(audioInput.length) { audioInput.pop() }
|
audioContainer.input = '';
|
||||||
|
// while(audioInput.length) { audioInput.pop() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue