From: Andrew Gundersen Date: Sat, 6 Mar 2021 16:53:04 +0000 (-0600) Subject: audio playback X-Git-Tag: v0.9~50^2~3 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=e8bb64e56a9e1d497039fadcb1d2d3971b21fa9b;p=mime-chat audio playback --- diff --git a/src/background/audio.ts b/src/background/audio.ts index 313f6f1..f267177 100644 --- a/src/background/audio.ts +++ b/src/background/audio.ts @@ -33,7 +33,7 @@ const updateRecorder = (_event, payload: boolean): void => { ipcMain.removeAllListeners('update-recorder'); ipcMain.on('update-recorder', updateRecorder); -// utility function used by play func. +// Split Buffer into an array of len-sized Buffers. function bufSplit(buf: Buffer, len: number): Array { let chunks = []; let i = 0; @@ -75,19 +75,19 @@ export function initAudioIO(): void { } // play audio buffers. -export function play(input: string): void { +export function play(input: Buffer): void { let i = 0; - console.log(Buffer.from(input, encoding)); - // format buffers into half their size to account for writable highwaterMark. - const audio = bufSplit(Buffer.from(input), 8192); - console.log(audio); + + const audio = bufSplit(input, 8192); // 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. + + // Iterate through audio array and write buffers to portAudio writable. function write() { let chunk: Buffer; let ok = true; diff --git a/src/background/session.ts b/src/background/session.ts index bd42b34..75f9433 100644 --- a/src/background/session.ts +++ b/src/background/session.ts @@ -32,6 +32,7 @@ let socket: WebSocket; let success = false; let auth = false; + const authSession = async (_event, payload: string | UserCreds | null): Promise => { return new Promise((resolve, reject) => { @@ -60,6 +61,17 @@ const authSession = async (_event, payload: string | UserCreds | null): Promise< }); }; +// function parseHexString(str: string) { +// var result = []; +// while (str.length >= 8) { +// result.push(parseInt(str.substring(0, 8), 16)); + +// str = str.substring(8, str.length); +// } + +// return result; +// } + const receiveMessage = (message: string): void => { while (!auth) { @@ -67,12 +79,16 @@ const receiveMessage = (message: string): void => { return; } + // Parse the message. const parsed: RenderMessage = JSON.parse(message); console.log('message received', parsed.content.text); + + // If there is audio, play it. if (parsed.content.audio) { - console.log('audio received'); - play(parsed.content.audio); + const audioBytes = Buffer.from(parsed.content.audio, 'hex'); + play(audioBytes); } + backgroundMitt.emit('ipc-renderer', { endpoint: 'render-message', message: parsed diff --git a/src/components/content.vue b/src/components/content.vue index e69de29..e623548 100644 --- a/src/components/content.vue +++ b/src/components/content.vue @@ -0,0 +1,19 @@ + + + + + \ No newline at end of file