add audio playback + buf split function
This commit is contained in:
parent
83263d5f23
commit
31e097b27f
3 changed files with 67 additions and 44 deletions
|
|
@ -34,13 +34,18 @@ ipcMain.removeAllListeners('update-recorder');
|
||||||
ipcMain.on('update-recorder', updateRecorder);
|
ipcMain.on('update-recorder', updateRecorder);
|
||||||
|
|
||||||
// utility function used by play func.
|
// utility function used by play func.
|
||||||
function bufSplit(input: Array<Buffer>): Array<Buffer> {
|
function bufSplit(buf: Buffer, len: number): Array<Buffer> {
|
||||||
const result: Buffer[] = [];
|
let chunks = [];
|
||||||
input.forEach((b: Buffer) => {
|
let i = 0;
|
||||||
// split buffer into two.
|
let L = len;
|
||||||
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
|
|
||||||
});
|
while(i < buf.byteLength) {
|
||||||
return result;
|
chunks.push(buf.slice(i, L));
|
||||||
|
i = L;
|
||||||
|
L += len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
// main audio function run by run.ts module.
|
// main audio function run by run.ts module.
|
||||||
|
|
@ -55,6 +60,7 @@ export function initAudioIO(): void {
|
||||||
|
|
||||||
ai.on('data', (chunk: string) => {
|
ai.on('data', (chunk: string) => {
|
||||||
if (record) {
|
if (record) {
|
||||||
|
console.log('recording...')
|
||||||
audioContainer.input += chunk;
|
audioContainer.input += chunk;
|
||||||
} else {
|
} else {
|
||||||
if (audioContainer.input.length) audioContainer.input = "";
|
if (audioContainer.input.length) audioContainer.input = "";
|
||||||
|
|
@ -69,10 +75,13 @@ export function initAudioIO(): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// play audio buffers.
|
// play audio buffers.
|
||||||
export function play(input: Array<Buffer>): void {
|
export function play(input: string): void {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
|
console.log(Buffer.from(input, encoding));
|
||||||
// format buffers into half their size to account for writable highwaterMark.
|
// format buffers into half their size to account for writable highwaterMark.
|
||||||
const audio = bufSplit(input);
|
const audio = bufSplit(Buffer.from(input), 8192);
|
||||||
|
console.log(audio);
|
||||||
|
|
||||||
// call this fuction after last audio chunk has been written.
|
// call this fuction after last audio chunk has been written.
|
||||||
const callback = () => {
|
const callback = () => {
|
||||||
// TODO: clear portAudio writable buffer on write end.
|
// TODO: clear portAudio writable buffer on write end.
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,12 @@
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import { backgroundMitt } from './emitter';
|
import { backgroundMitt } from './emitter';
|
||||||
import { ipcMain } from "electron";
|
import { ipcMain } from "electron";
|
||||||
|
import { play } from './audio';
|
||||||
|
|
||||||
interface RenderMessage {
|
interface RenderMessage {
|
||||||
content: {
|
content: {
|
||||||
text: string;
|
text: string;
|
||||||
audio: boolean | Buffer;
|
audio: string;
|
||||||
};
|
};
|
||||||
context: string;
|
context: string;
|
||||||
modifier: string;
|
modifier: string;
|
||||||
|
|
@ -67,8 +68,10 @@ const receiveMessage = (message: string): void => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed: RenderMessage = JSON.parse(message);
|
const parsed: RenderMessage = JSON.parse(message);
|
||||||
console.log('message received', parsed);
|
console.log('message received', parsed.content.text);
|
||||||
if (parsed.content.audio) {
|
if (parsed.content.audio) {
|
||||||
|
console.log('audio received');
|
||||||
|
play(parsed.content.audio);
|
||||||
}
|
}
|
||||||
backgroundMitt.emit('ipc-renderer', {
|
backgroundMitt.emit('ipc-renderer', {
|
||||||
endpoint: 'render-message',
|
endpoint: 'render-message',
|
||||||
|
|
@ -78,10 +81,12 @@ const receiveMessage = (message: string): void => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendMessage = (_event, payload: TextMessage): void => {
|
const sendMessage = (_event, payload: TextMessage): void => {
|
||||||
|
console.log('sending message')
|
||||||
socket.send(JSON.stringify(payload));
|
socket.send(JSON.stringify(payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendAudio = (content: Buffer) => {
|
export const sendAudio = (content: Buffer) => {
|
||||||
|
console.log('sending audio')
|
||||||
socket.send(content);
|
socket.send(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,8 @@ ia.setEncoding('base64');
|
||||||
ia.start();
|
ia.start();
|
||||||
ia.on('data', (chunk) => {
|
ia.on('data', (chunk) => {
|
||||||
if (record) {
|
if (record) {
|
||||||
console.log('recording data')
|
console.log('recording data');
|
||||||
audioContainer.input += chunk;
|
audioContainer.input += chunk;
|
||||||
// audioContainer.buffers.push(Buffer.from(chunk, 'base64'));
|
|
||||||
} else {
|
} else {
|
||||||
if (audioContainer.input.length) audioContainer.input = "";
|
if (audioContainer.input.length) audioContainer.input = "";
|
||||||
}
|
}
|
||||||
|
|
@ -83,22 +82,32 @@ let counter = 0;
|
||||||
const tests = [];
|
const tests = [];
|
||||||
|
|
||||||
function testCallback() {
|
function testCallback() {
|
||||||
tests.forEach(t => console.log(t))
|
|
||||||
console.log(tests[0])
|
|
||||||
play(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;
|
let i = 0;
|
||||||
|
const buf = Buffer.from(input, 'base64');
|
||||||
// format buffers into half their size to account for writable highwaterMark.
|
// 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.
|
// call this fuction after last audio chunk has been written.
|
||||||
const callback = () => {
|
const callback = () => {
|
||||||
// TODO: clear portAudio writable buffer on write end.
|
// 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();
|
write();
|
||||||
// iterate through audio array and write buffers to portAudio writable.
|
// iterate through audio array and write buffers to portAudio writable.
|
||||||
|
|
@ -137,7 +146,7 @@ async function test() {
|
||||||
transcribeSpeech({
|
transcribeSpeech({
|
||||||
content: Buffer.from(audioContainer.input, 'base64')
|
content: Buffer.from(audioContainer.input, 'base64')
|
||||||
});
|
});
|
||||||
tests.push(audioContainer.buffers)
|
tests.push(audioContainer.input)
|
||||||
counter++;
|
counter++;
|
||||||
console.log('audio string length:', audioContainer.input.length)
|
console.log('audio string length:', audioContainer.input.length)
|
||||||
console.log('buffers written: ', audioContainer.buffers.length)
|
console.log('buffers written: ', audioContainer.buffers.length)
|
||||||
|
|
@ -146,28 +155,28 @@ async function test() {
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
record = false;
|
record = false;
|
||||||
test()
|
test()
|
||||||
}, 3000);
|
}, 4000);
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
record = true;
|
|
||||||
}, 6000)
|
|
||||||
|
|
||||||
setTimeout(async () => {
|
|
||||||
record = false;
|
|
||||||
test();
|
|
||||||
}, 9000);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
record = true;
|
|
||||||
}, 11000)
|
|
||||||
|
|
||||||
setTimeout(async () => {
|
|
||||||
record = false;
|
|
||||||
test();
|
|
||||||
}, 14000);
|
|
||||||
|
|
||||||
|
// setTimeout(() => {
|
||||||
|
// record = true;
|
||||||
|
// }, 6000)
|
||||||
|
//
|
||||||
|
// setTimeout(async () => {
|
||||||
|
// record = false;
|
||||||
|
// test();
|
||||||
|
// }, 9000);
|
||||||
|
//
|
||||||
|
// setTimeout(() => {
|
||||||
|
// record = true;
|
||||||
|
// }, 11000)
|
||||||
|
//
|
||||||
|
// setTimeout(async () => {
|
||||||
|
// record = false;
|
||||||
|
// test();
|
||||||
|
// }, 14000);
|
||||||
|
//
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
ia.quit();
|
ia.quit();
|
||||||
// testCallback()
|
testCallback()
|
||||||
return;
|
return;
|
||||||
}, 16000);
|
}, 6000);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue