135 lines
3.1 KiB
TypeScript
135 lines
3.1 KiB
TypeScript
/* eslint @typescript-eslint/no-var-requires: "off" */
|
|
|
|
"use strict";
|
|
|
|
import { sendAudio } from './session'
|
|
import { ipcMain } from "electron";
|
|
import { backgroundMitt } from './emitter';
|
|
|
|
const portAudio = require('naudiodon');
|
|
|
|
let ai: typeof portAudio.AudioIO | null = null;
|
|
let ao: typeof portAudio.AudioIO | null = null;
|
|
let record = false;
|
|
const audioContainer = {
|
|
input: '',
|
|
}
|
|
const encoding = "hex";
|
|
const audioOptions = {
|
|
channelCount: 1,
|
|
sampleFormat: 16,
|
|
sampleRate: 16000,
|
|
deviceId: -1,
|
|
closeOnError: false,
|
|
}
|
|
|
|
// callback run on space bar key up and down.
|
|
const updateRecorder = (_event, payload: {
|
|
isRecording: boolean;
|
|
uid: string | null;
|
|
}): void => {
|
|
record = payload.isRecording;
|
|
if (!record) {
|
|
if (payload.uid) sendAudio(audioContainer.input, payload.uid);
|
|
}
|
|
};
|
|
|
|
// listen for space key up/down event.
|
|
ipcMain.removeAllListeners('update-recorder');
|
|
ipcMain.on('update-recorder', updateRecorder);
|
|
|
|
// Split Buffer into an array of len-sized Buffers.
|
|
function bufSplit(buf: Buffer, len: number): Array<Buffer> {
|
|
const 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.
|
|
export function initAudioIO(): void {
|
|
|
|
if (!ai) {
|
|
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
|
|
|
// base64 encoding needed for google speech to text.
|
|
ai.setEncoding(encoding);
|
|
ai.start();
|
|
|
|
ai.on('data', (chunk: string) => {
|
|
if (record) {
|
|
console.log('recording...')
|
|
audioContainer.input += chunk;
|
|
} else {
|
|
if (audioContainer.input.length) audioContainer.input = "";
|
|
}
|
|
});
|
|
}
|
|
|
|
if (!ao) {
|
|
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
|
ao.start();
|
|
}
|
|
}
|
|
|
|
// play audio buffers.
|
|
export function play(input: Buffer): void {
|
|
let i = 0;
|
|
|
|
const audio = bufSplit(input, 8192);
|
|
|
|
// Called on end of write.
|
|
const callback = () => {
|
|
|
|
// We stop audio playback anim.
|
|
backgroundMitt.emit('ipc-renderer', {
|
|
endpoint: 'stop-playback-anim'
|
|
});
|
|
|
|
}
|
|
|
|
write();
|
|
|
|
// Iterate through audio array and write buffers to portAudio writable.
|
|
function write() {
|
|
let chunk: Buffer;
|
|
let ok = true;
|
|
|
|
do {
|
|
chunk = audio[i];
|
|
if (i === audio.length - 1) {
|
|
// write last chunk.
|
|
ao.write(chunk, null, callback);
|
|
} else {
|
|
// check for backpreassure.
|
|
ok = ao.write(chunk, null);
|
|
}
|
|
i++;
|
|
} while (i < audio.length && ok);
|
|
|
|
if (i < audio.length) {
|
|
// Had to stop early!
|
|
// Write some more once it drains.
|
|
ao.once('drain', write);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function stopStream() {
|
|
if(ai != null) {
|
|
ai.quit();
|
|
ai = null;
|
|
}
|
|
if (ao != null) {
|
|
ao.quit();
|
|
ao = null;
|
|
}
|
|
}
|
|
|