manual remove audioInput after write

This commit is contained in:
Enrique Hernandez 2021-03-02 08:42:30 -06:00
commit 4517752ed8
2 changed files with 13 additions and 7 deletions

View file

@ -7,7 +7,7 @@ const portAudio = require('naudiodon');
let ai: typeof portAudio.AudioIO; let ai: typeof portAudio.AudioIO;
let ao: typeof portAudio.AudioIO; let ao: typeof portAudio.AudioIO;
let audioInput: Buffer[] = []; const audioInput: Buffer[] = [];
const encoding = "base64"; const encoding = "base64";
const audioOptions = { const audioOptions = {
channelCount: 1, channelCount: 1,
@ -57,7 +57,8 @@ export const updateRecorder = (_event, record: boolean): void => {
// 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);
sendAudio(audio); play(audioInput);
// sendAudio(audio);
while (audioInput.length) { audioInput.pop(); } while (audioInput.length) { audioInput.pop(); }
} }
} }
@ -98,7 +99,7 @@ export const play = (input: Array<Buffer>) => {
// utility function used by play func // utility function used by play func
function bufSplit(input: Array<Buffer>): Array<Buffer> { function bufSplit(input: Array<Buffer>): Array<Buffer> {
let result: Buffer[] = []; const result: Buffer[] = [];
input.forEach((b: Buffer) => { input.forEach((b: Buffer) => {
// split buffer into two. // split buffer into two.
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length)); result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));

View file

@ -40,7 +40,7 @@ async function transcribeSpeech (audio) {
console.log(`Transcription: ${transcription}`); console.log(`Transcription: ${transcription}`);
} }
let audioInput = []; const audioInput = [];
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
const ia = new portAudio.AudioIO({ const ia = new portAudio.AudioIO({
inOptions: { inOptions: {
@ -74,7 +74,7 @@ async function processAudio() {
const buf = Buffer.concat(audioInput); const buf = Buffer.concat(audioInput);
play(audioInput); play(audioInput);
while(audioInput.length) { audioInput.pop() }; while(audioInput.length) { audioInput.pop() }
transcribeSpeech({ transcribeSpeech({
content: buf content: buf
@ -117,7 +117,7 @@ async function processAudio() {
// utility function used by play func // utility function used by play func
function bufSplit(input){ function bufSplit(input){
let result = []; const result = [];
input.forEach((b) => { input.forEach((b) => {
// split buffer into two. // split buffer into two.
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length)); result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
@ -140,5 +140,10 @@ setTimeout(async () => {
}, 8000); }, 8000);
setTimeout(() => { setTimeout(() => {
ia.resume();
}, 10000)
setTimeout(async () => {
ia.pause(); ia.pause();
}, 9000) processAudio();
}, 15000);