remove stream pause and resume method calls
This commit is contained in:
parent
cb68e84c0e
commit
854d6dd6a2
2 changed files with 58 additions and 34 deletions
|
|
@ -54,7 +54,7 @@ export function initAudioIO(): void {
|
|||
|
||||
if (ao != null) {
|
||||
ao.end();
|
||||
}
|
||||
}
|
||||
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
||||
ao.start();
|
||||
}
|
||||
|
|
@ -77,7 +77,7 @@ const updateRecorder = (_event, record: boolean): void => {
|
|||
}
|
||||
}
|
||||
|
||||
// listen for space key up/down event.
|
||||
// listen for space key up/down event.
|
||||
ipcMain.removeAllListeners('update-recorder');
|
||||
ipcMain.on('update-recorder', updateRecorder);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ const encoding = 'LINEAR16';
|
|||
const sampleRateHertz = 16000;
|
||||
const languageCode = 'en-US';
|
||||
|
||||
const audioContainer = {
|
||||
input: '',
|
||||
buffers: []
|
||||
}
|
||||
|
||||
const config = {
|
||||
encoding: encoding,
|
||||
sampleRateHertz: sampleRateHertz,
|
||||
|
|
@ -20,7 +25,6 @@ const config = {
|
|||
* Note that transcription is limited to 60 seconds audio.
|
||||
* Use a GCS file for audio longer than 1 minute.
|
||||
*/
|
||||
|
||||
async function transcribeSpeech (audio) {
|
||||
const request = {
|
||||
config: config,
|
||||
|
|
@ -40,7 +44,8 @@ async function transcribeSpeech (audio) {
|
|||
console.log(`Transcription: ${transcription}`);
|
||||
}
|
||||
|
||||
const audioInput = [];
|
||||
let record = true;
|
||||
|
||||
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
|
||||
const ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
|
|
@ -54,8 +59,13 @@ const ia = new portAudio.AudioIO({
|
|||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.on('data', (chunk) => {
|
||||
const buf = Buffer.from(chunk, 'base64');
|
||||
audioInput.push(buf);
|
||||
if (record) {
|
||||
console.log('recording data')
|
||||
audioContainer.input += chunk;
|
||||
// audioContainer.buffers.push(Buffer.from(chunk, 'base64'));
|
||||
} else {
|
||||
if (audioContainer.input.length) audioContainer.input = "";
|
||||
}
|
||||
});
|
||||
|
||||
const ao = new portAudio.AudioIO({
|
||||
|
|
@ -69,40 +79,38 @@ const ao = new portAudio.AudioIO({
|
|||
});
|
||||
ao.start();
|
||||
|
||||
async function processAudio() {
|
||||
let counter = 0;
|
||||
let tests = [];
|
||||
|
||||
const buf = Buffer.concat(audioInput);
|
||||
play(audioInput);
|
||||
|
||||
while(audioInput.length) { audioInput.pop() }
|
||||
|
||||
transcribeSpeech({
|
||||
content: buf
|
||||
});
|
||||
function testCallback() {
|
||||
tests.forEach(t => console.log(t))
|
||||
console.log(tests[0])
|
||||
play(tests[0])
|
||||
}
|
||||
|
||||
|
||||
function play(input) {
|
||||
function play(bufArray) {
|
||||
let i = 0;
|
||||
// format buffers into half their size to account for writable highwaterMark.
|
||||
const audio = bufSplit(input);
|
||||
const audio = bufSplit(bufArray);
|
||||
// call this fuction after last audio chunk has been written.
|
||||
const callback = () => {
|
||||
// TODO: clear portAudio writable buffer on write end.
|
||||
console.log('Finished writing test number %d', counter)
|
||||
if (counter === 3) {
|
||||
console.log('finished test writing!')
|
||||
}
|
||||
}
|
||||
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);
|
||||
ao.write(audio[i], null, callback);
|
||||
} else {
|
||||
// check for backpreassure.
|
||||
ok = ao.write(chunk, null);
|
||||
ok = ao.write(audio[i], null);
|
||||
}
|
||||
i++;
|
||||
} while (i < audio.length && ok);
|
||||
|
|
@ -125,25 +133,41 @@ function bufSplit(input){
|
|||
return result;
|
||||
}
|
||||
|
||||
async function test() {
|
||||
transcribeSpeech({
|
||||
content: Buffer.from(audioContainer.input, 'base64')
|
||||
});
|
||||
tests.push(audioContainer.buffers)
|
||||
counter++;
|
||||
console.log('audio string length:', audioContainer.input.length)
|
||||
console.log('buffers written: ', audioContainer.buffers.length)
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.pause();
|
||||
processAudio();
|
||||
record = false;
|
||||
test()
|
||||
}, 3000);
|
||||
|
||||
setTimeout(() => {
|
||||
ia.resume();
|
||||
}, 5000)
|
||||
record = true;
|
||||
}, 6000)
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.pause();
|
||||
processAudio();
|
||||
}, 8000);
|
||||
record = false;
|
||||
test();
|
||||
}, 9000);
|
||||
|
||||
setTimeout(() => {
|
||||
ia.resume();
|
||||
}, 10000)
|
||||
record = true;
|
||||
}, 11000)
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.pause();
|
||||
processAudio();
|
||||
}, 15000);
|
||||
record = false;
|
||||
test();
|
||||
}, 14000);
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.quit();
|
||||
// testCallback()
|
||||
return;
|
||||
}, 16000);
|
||||
|
|
|
|||
Loading…
Reference in a new issue