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);
|
||||
|
||||
// utility function used by play func.
|
||||
function bufSplit(input: Array<Buffer>): Array<Buffer> {
|
||||
const result: Buffer[] = [];
|
||||
input.forEach((b: Buffer) => {
|
||||
// split buffer into two.
|
||||
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
|
||||
});
|
||||
return result;
|
||||
function bufSplit(buf: Buffer, len: number): Array<Buffer> {
|
||||
let chunks = [];
|
||||
let i = 0;
|
||||
let L = len;
|
||||
|
||||
while(i < buf.byteLength) {
|
||||
chunks.push(buf.slice(i, L));
|
||||
i = L;
|
||||
L += len;
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// main audio function run by run.ts module.
|
||||
|
|
@ -55,6 +60,7 @@ export function initAudioIO(): void {
|
|||
|
||||
ai.on('data', (chunk: string) => {
|
||||
if (record) {
|
||||
console.log('recording...')
|
||||
audioContainer.input += chunk;
|
||||
} else {
|
||||
if (audioContainer.input.length) audioContainer.input = "";
|
||||
|
|
@ -69,10 +75,13 @@ export function initAudioIO(): void {
|
|||
}
|
||||
|
||||
// play audio buffers.
|
||||
export function play(input: Array<Buffer>): void {
|
||||
export function play(input: string): void {
|
||||
let i = 0;
|
||||
console.log(Buffer.from(input, encoding));
|
||||
// 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.
|
||||
const callback = () => {
|
||||
// TODO: clear portAudio writable buffer on write end.
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@
|
|||
import WebSocket from 'ws';
|
||||
import { backgroundMitt } from './emitter';
|
||||
import { ipcMain } from "electron";
|
||||
import { play } from './audio';
|
||||
|
||||
interface RenderMessage {
|
||||
content: {
|
||||
text: string;
|
||||
audio: boolean | Buffer;
|
||||
audio: string;
|
||||
};
|
||||
context: string;
|
||||
modifier: string;
|
||||
|
|
@ -67,8 +68,10 @@ const receiveMessage = (message: string): void => {
|
|||
}
|
||||
|
||||
const parsed: RenderMessage = JSON.parse(message);
|
||||
console.log('message received', parsed);
|
||||
console.log('message received', parsed.content.text);
|
||||
if (parsed.content.audio) {
|
||||
console.log('audio received');
|
||||
play(parsed.content.audio);
|
||||
}
|
||||
backgroundMitt.emit('ipc-renderer', {
|
||||
endpoint: 'render-message',
|
||||
|
|
@ -78,10 +81,12 @@ const receiveMessage = (message: string): void => {
|
|||
};
|
||||
|
||||
const sendMessage = (_event, payload: TextMessage): void => {
|
||||
console.log('sending message')
|
||||
socket.send(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
export const sendAudio = (content: Buffer) => {
|
||||
console.log('sending audio')
|
||||
socket.send(content);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue