add manual clear of audio input array
This commit is contained in:
parent
346bfcba01
commit
35d603d303
2 changed files with 58 additions and 9 deletions
|
|
@ -58,7 +58,7 @@ export const updateRecorder = (_event, record: boolean): void => {
|
||||||
ai.pause();
|
ai.pause();
|
||||||
const audio = Buffer.concat(audioInput);
|
const audio = Buffer.concat(audioInput);
|
||||||
sendAudio(audio);
|
sendAudio(audio);
|
||||||
audioInput = [];
|
while (audioInput.length) { audioInput.pop(); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ const config = {
|
||||||
* Note that transcription is limited to 60 seconds audio.
|
* Note that transcription is limited to 60 seconds audio.
|
||||||
* Use a GCS file for audio longer than 1 minute.
|
* Use a GCS file for audio longer than 1 minute.
|
||||||
*/
|
*/
|
||||||
let audio;
|
|
||||||
|
|
||||||
async function transcribeSpeech (audio) {
|
async function transcribeSpeech (audio) {
|
||||||
const request = {
|
const request = {
|
||||||
|
|
@ -54,30 +53,81 @@ const ia = new portAudio.AudioIO({
|
||||||
});
|
});
|
||||||
ia.setEncoding('base64');
|
ia.setEncoding('base64');
|
||||||
ia.start();
|
ia.start();
|
||||||
ia.on('error', (e) => {
|
|
||||||
console.log('error recording audio', + e);
|
|
||||||
});
|
|
||||||
ia.on('data', (chunk) => {
|
ia.on('data', (chunk) => {
|
||||||
|
|
||||||
const buf = Buffer.from(chunk, 'base64');
|
const buf = Buffer.from(chunk, 'base64');
|
||||||
audioInput.push(buf);
|
audioInput.push(buf);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const ao = new portAudio.AudioIO({
|
||||||
|
outOptions: {
|
||||||
|
sampleFormat: 16,
|
||||||
|
channelCount: 1,
|
||||||
|
sampleRate: 16000,
|
||||||
|
deviceId: -1,
|
||||||
|
closeOnError: false,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ao.start();
|
||||||
|
|
||||||
async function processAudio() {
|
async function processAudio() {
|
||||||
|
|
||||||
const buf = Buffer.concat(audioInput);
|
const buf = Buffer.concat(audioInput);
|
||||||
|
play(audioInput);
|
||||||
|
|
||||||
audioInput = [];
|
while(audioInput.length) { audioInput.pop() };
|
||||||
|
|
||||||
transcribeSpeech({
|
transcribeSpeech({
|
||||||
content: buf
|
content: buf
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function play(input) {
|
||||||
|
let i = 0;
|
||||||
|
// format buffers into half their size to account for writable highwaterMark.
|
||||||
|
const audio = bufSplit(input);
|
||||||
|
// call this fuction after last audio chunk has been written.
|
||||||
|
const callback = () => {
|
||||||
|
// TODO: clear portAudio writable buffer on write end.
|
||||||
|
}
|
||||||
|
write();
|
||||||
|
// iterate through audio array and write buffers to portAudio writable.
|
||||||
|
function write() {
|
||||||
|
let chunk;
|
||||||
|
let ok = true;
|
||||||
|
do {
|
||||||
|
chunk = audio[i];
|
||||||
|
if (i === audio.length - 1) {
|
||||||
|
// write last chunk.
|
||||||
|
ao.write(chunk, null, callback);
|
||||||
|
} else {
|
||||||
|
// check for backpreassure.
|
||||||
|
ok = ao.write(chunk, null);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} while (i < audio.length && ok);
|
||||||
|
|
||||||
|
if (i < audio.length) {
|
||||||
|
// Had to stop early!
|
||||||
|
// Write some more once it drains.
|
||||||
|
ao.once('drain', write);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility function used by play func
|
||||||
|
function bufSplit(input){
|
||||||
|
let result = [];
|
||||||
|
input.forEach((b) => {
|
||||||
|
// split buffer into two.
|
||||||
|
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
ia.pause();
|
ia.pause();
|
||||||
processAudio();
|
processAudio();
|
||||||
// ia.resume();
|
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
@ -87,7 +137,6 @@ setTimeout(() => {
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
ia.pause();
|
ia.pause();
|
||||||
processAudio();
|
processAudio();
|
||||||
// ia.resume();
|
|
||||||
}, 8000);
|
}, 8000);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue