add audio playback + buf split function
This commit is contained in:
parent
83263d5f23
commit
31e097b27f
3 changed files with 67 additions and 44 deletions
|
|
@ -60,9 +60,8 @@ ia.setEncoding('base64');
|
|||
ia.start();
|
||||
ia.on('data', (chunk) => {
|
||||
if (record) {
|
||||
console.log('recording data')
|
||||
console.log('recording data');
|
||||
audioContainer.input += chunk;
|
||||
// audioContainer.buffers.push(Buffer.from(chunk, 'base64'));
|
||||
} else {
|
||||
if (audioContainer.input.length) audioContainer.input = "";
|
||||
}
|
||||
|
|
@ -83,22 +82,32 @@ let counter = 0;
|
|||
const tests = [];
|
||||
|
||||
function testCallback() {
|
||||
tests.forEach(t => console.log(t))
|
||||
console.log(tests[0])
|
||||
play(tests[0])
|
||||
}
|
||||
|
||||
function play(bufArray) {
|
||||
function splitArrayIntoChunksOfLen(buf, len) {
|
||||
let chunks = [];
|
||||
let i = 0;
|
||||
let L = len;
|
||||
console.log(buf.byteLength)
|
||||
while(i < buf.byteLength) {
|
||||
chunks.push(buf.slice(i, L));
|
||||
i = L;
|
||||
L += len;
|
||||
}
|
||||
|
||||
chunks.forEach(c => console.log(c))
|
||||
return chunks;
|
||||
}
|
||||
|
||||
function play(input) {
|
||||
let i = 0;
|
||||
const buf = Buffer.from(input, 'base64');
|
||||
// format buffers into half their size to account for writable highwaterMark.
|
||||
const audio = bufSplit(bufArray);
|
||||
const audio = splitArrayIntoChunksOfLen(buf, 8192);
|
||||
// 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.
|
||||
|
|
@ -137,7 +146,7 @@ async function test() {
|
|||
transcribeSpeech({
|
||||
content: Buffer.from(audioContainer.input, 'base64')
|
||||
});
|
||||
tests.push(audioContainer.buffers)
|
||||
tests.push(audioContainer.input)
|
||||
counter++;
|
||||
console.log('audio string length:', audioContainer.input.length)
|
||||
console.log('buffers written: ', audioContainer.buffers.length)
|
||||
|
|
@ -146,28 +155,28 @@ async function test() {
|
|||
setTimeout(async () => {
|
||||
record = false;
|
||||
test()
|
||||
}, 3000);
|
||||
|
||||
setTimeout(() => {
|
||||
record = true;
|
||||
}, 6000)
|
||||
|
||||
setTimeout(async () => {
|
||||
record = false;
|
||||
test();
|
||||
}, 9000);
|
||||
|
||||
setTimeout(() => {
|
||||
record = true;
|
||||
}, 11000)
|
||||
|
||||
setTimeout(async () => {
|
||||
record = false;
|
||||
test();
|
||||
}, 14000);
|
||||
}, 4000);
|
||||
|
||||
// setTimeout(() => {
|
||||
// record = true;
|
||||
// }, 6000)
|
||||
//
|
||||
// setTimeout(async () => {
|
||||
// record = false;
|
||||
// test();
|
||||
// }, 9000);
|
||||
//
|
||||
// setTimeout(() => {
|
||||
// record = true;
|
||||
// }, 11000)
|
||||
//
|
||||
// setTimeout(async () => {
|
||||
// record = false;
|
||||
// test();
|
||||
// }, 14000);
|
||||
//
|
||||
setTimeout(async () => {
|
||||
ia.quit();
|
||||
// testCallback()
|
||||
testCallback()
|
||||
return;
|
||||
}, 16000);
|
||||
}, 6000);
|
||||
|
|
|
|||
Loading…
Reference in a new issue