audio restart fixed
This commit is contained in:
parent
be61d521de
commit
7d23347ce5
4 changed files with 76 additions and 106 deletions
|
|
@ -1,5 +1,9 @@
|
|||
"use strict";
|
||||
/* eslint @typescript-eslint/no-var-requires: "off" */
|
||||
import { sendMessage } from './session'
|
||||
const portAudio = require('naudiodon');
|
||||
|
||||
let ia: typeof portAudio.AudioIO;
|
||||
let audioInput: string[] = [];
|
||||
|
||||
function processAudio(audioInput: Array<string>): Buffer {
|
||||
|
|
@ -8,42 +12,39 @@ function processAudio(audioInput: Array<string>): Buffer {
|
|||
return Buffer.from(audio, "base64");
|
||||
}
|
||||
|
||||
|
||||
const updateRecorder = async (Event: any, record: boolean): void => {
|
||||
export const updateRecorder = (_event, record: boolean): void => {
|
||||
if (record) {
|
||||
ia.resume();
|
||||
} else {
|
||||
ia.pause();
|
||||
const audio = processAudio(audioInput);
|
||||
// emmit ws event with audio
|
||||
// sendAudio(audio);
|
||||
sendMessage(audio);
|
||||
audioInput = [];
|
||||
}
|
||||
}
|
||||
|
||||
export const initRecorder = () => {
|
||||
|
||||
const ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
}
|
||||
});
|
||||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.pause();
|
||||
ia.on('error', (e: Error) => {
|
||||
console.log('Error recording audio', + e);
|
||||
});
|
||||
ia.on('data', (chunk: string) => {
|
||||
audioInput.push(chunk);
|
||||
});
|
||||
|
||||
export function useIA() {
|
||||
return {
|
||||
ia,
|
||||
processAudio
|
||||
if (!ia) {
|
||||
ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
}
|
||||
});
|
||||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.pause();
|
||||
ia.on('error', (e: Error) => {
|
||||
console.log('Error recording audio', + e);
|
||||
});
|
||||
ia.on('data', (chunk: string) => {
|
||||
audioInput.push(chunk);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,40 +1,10 @@
|
|||
"use strict";
|
||||
|
||||
import { createWindow } from './window';
|
||||
import { ipcMain, BrowserWindow } from "electron";
|
||||
import { initSession } from './session';
|
||||
import { backgroundMitt } from './emitter';
|
||||
// const portAudio = require('naudiodon');
|
||||
import { initRecorder } from './audio';
|
||||
|
||||
interface RenderMessage {
|
||||
content: string;
|
||||
context: string;
|
||||
subContext: string;
|
||||
modifiers: string;
|
||||
time: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IpcRendererPayload {
|
||||
endpoint: string;
|
||||
message: RenderMessage | null;
|
||||
}
|
||||
|
||||
let win: BrowserWindow | null;
|
||||
let activeSession = false;
|
||||
|
||||
backgroundMitt.on('ipc-renderer', (payload: IpcRendererPayload) => {
|
||||
if (win)
|
||||
win.webContents.send(payload.endpoint, {
|
||||
message: payload.message
|
||||
});
|
||||
});
|
||||
|
||||
const windowDismount = (): void => {
|
||||
win = null;
|
||||
backgroundMitt.emit('window-active', false);
|
||||
ipcMain.removeAllListeners('send-message');
|
||||
}
|
||||
let socket = false;
|
||||
|
||||
/*
|
||||
* The main function will be run after electron app is ready.
|
||||
|
|
@ -42,55 +12,17 @@ const windowDismount = (): void => {
|
|||
export async function main() {
|
||||
|
||||
// create main window.
|
||||
if (!win) win = await createWindow({
|
||||
await createWindow({
|
||||
width: 350,
|
||||
height: 525,
|
||||
resizable: true
|
||||
});
|
||||
|
||||
// Instantiate socket session with crimata-platorm.
|
||||
if (!activeSession) initSession();
|
||||
if (!socket) initSession();
|
||||
socket = true;
|
||||
|
||||
activeSession = true;
|
||||
// Begin audio stream.
|
||||
initRecorder();
|
||||
|
||||
// Handle window close.
|
||||
win.on("closed", windowDismount);
|
||||
|
||||
// TODO: FIX recorder restart bug
|
||||
// let audioInput: string[] = [];
|
||||
// const updateRecorder = async (Event: any, record: boolean): void => {
|
||||
// if (record) {
|
||||
// ia.resume();
|
||||
// } else {
|
||||
// ia.pause();
|
||||
// const audio = processAudio(audioInput);
|
||||
// sendAudio(audio);
|
||||
// audioInput = [];
|
||||
// }
|
||||
// }
|
||||
|
||||
// function processAudio(audioInput: Array<string>): Buffer {
|
||||
// let audio = '';
|
||||
// audioInput.forEach(chunk => audio += chunk);
|
||||
// return Buffer.from(audio, "base64");
|
||||
// }
|
||||
|
||||
// const ia = new portAudio.AudioIO({
|
||||
// inOptions: {
|
||||
// channelCount: 1,
|
||||
// sampleFormat: 16,
|
||||
// sampleRate: 16000,
|
||||
// deviceId: -1,
|
||||
// closeOnError: false,
|
||||
// }
|
||||
// });
|
||||
// ia.setEncoding('base64');
|
||||
// ia.start();
|
||||
// ia.pause();
|
||||
// ia.on('error', (e: Error) => {
|
||||
// console.log('Error recording audio', + e);
|
||||
// });
|
||||
// ia.on('data', (chunk: string) => {
|
||||
// audioInput.push(chunk);
|
||||
// });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ const authSession = async (_event, payload: string | UserCreds | null): Promise<
|
|||
reject(res);
|
||||
} else {
|
||||
auth = true;
|
||||
console.log(res);
|
||||
resolve(res);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { BrowserWindow, ipcMain } from "electron";
|
|||
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
||||
import { backgroundMitt } from './emitter';
|
||||
import { sendMessage } from './session';
|
||||
import { updateRecorder } from './audio';
|
||||
import * as path from "path";
|
||||
|
||||
interface WindowSettings {
|
||||
|
|
@ -17,6 +18,22 @@ interface TextMessage {
|
|||
content: string;
|
||||
}
|
||||
|
||||
interface RenderMessage {
|
||||
content: string;
|
||||
context: string;
|
||||
subContext: string;
|
||||
modifiers: string;
|
||||
time: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IpcRendererPayload {
|
||||
endpoint: string;
|
||||
message: RenderMessage | null;
|
||||
}
|
||||
|
||||
let win: BrowserWindow | null;
|
||||
|
||||
const handleMessage = (_event, arg: TextMessage | Buffer ) => {
|
||||
sendMessage(arg);
|
||||
}
|
||||
|
|
@ -24,11 +41,29 @@ const handleMessage = (_event, arg: TextMessage | Buffer ) => {
|
|||
const windowMount = (): void => {
|
||||
backgroundMitt.emit('window-active', true);
|
||||
ipcMain.on('send-message', handleMessage);
|
||||
ipcMain.on('update-recorder', updateRecorder);
|
||||
}
|
||||
|
||||
export const createWindow = async (options: WindowSettings): Promise<BrowserWindow> => {
|
||||
const windowDismount = (): void => {
|
||||
win = null;
|
||||
backgroundMitt.emit('window-active', false);
|
||||
ipcMain.removeAllListeners('send-message');
|
||||
ipcMain.removeAllListeners('update-recorder');
|
||||
}
|
||||
|
||||
backgroundMitt.on('ipc-renderer', (payload: IpcRendererPayload) => {
|
||||
if (win)
|
||||
win.webContents.send(payload.endpoint, {
|
||||
message: payload.message
|
||||
});
|
||||
});
|
||||
|
||||
export const createWindow = async (options: WindowSettings): Promise<void> => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
const win: BrowserWindow = new BrowserWindow({
|
||||
|
||||
if (win) resolve();
|
||||
|
||||
win= new BrowserWindow({
|
||||
width: options.width,
|
||||
height: options.height,
|
||||
resizable: options.resizable,
|
||||
|
|
@ -50,9 +85,12 @@ export const createWindow = async (options: WindowSettings): Promise<BrowserWind
|
|||
win.loadURL("app://./index.html");
|
||||
}
|
||||
|
||||
// Handle window close.
|
||||
win.on("closed", windowDismount);
|
||||
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
windowMount();
|
||||
resolve(win);
|
||||
if (win)resolve();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue