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');
|
const portAudio = require('naudiodon');
|
||||||
|
|
||||||
|
let ia: typeof portAudio.AudioIO;
|
||||||
let audioInput: string[] = [];
|
let audioInput: string[] = [];
|
||||||
|
|
||||||
function processAudio(audioInput: Array<string>): Buffer {
|
function processAudio(audioInput: Array<string>): Buffer {
|
||||||
|
|
@ -8,42 +12,39 @@ function processAudio(audioInput: Array<string>): Buffer {
|
||||||
return Buffer.from(audio, "base64");
|
return Buffer.from(audio, "base64");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const updateRecorder = (_event, record: boolean): void => {
|
||||||
const updateRecorder = async (Event: any, record: boolean): void => {
|
|
||||||
if (record) {
|
if (record) {
|
||||||
ia.resume();
|
ia.resume();
|
||||||
} else {
|
} else {
|
||||||
ia.pause();
|
ia.pause();
|
||||||
const audio = processAudio(audioInput);
|
const audio = processAudio(audioInput);
|
||||||
// emmit ws event with audio
|
sendMessage(audio);
|
||||||
// sendAudio(audio);
|
|
||||||
audioInput = [];
|
audioInput = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const initRecorder = () => {
|
||||||
|
|
||||||
const ia = new portAudio.AudioIO({
|
if (!ia) {
|
||||||
inOptions: {
|
ia = new portAudio.AudioIO({
|
||||||
channelCount: 1,
|
inOptions: {
|
||||||
sampleFormat: 16,
|
channelCount: 1,
|
||||||
sampleRate: 16000,
|
sampleFormat: 16,
|
||||||
deviceId: -1,
|
sampleRate: 16000,
|
||||||
closeOnError: false,
|
deviceId: -1,
|
||||||
}
|
closeOnError: false,
|
||||||
});
|
}
|
||||||
ia.setEncoding('base64');
|
});
|
||||||
ia.start();
|
ia.setEncoding('base64');
|
||||||
ia.pause();
|
ia.start();
|
||||||
ia.on('error', (e: Error) => {
|
ia.pause();
|
||||||
console.log('Error recording audio', + e);
|
ia.on('error', (e: Error) => {
|
||||||
});
|
console.log('Error recording audio', + e);
|
||||||
ia.on('data', (chunk: string) => {
|
});
|
||||||
audioInput.push(chunk);
|
ia.on('data', (chunk: string) => {
|
||||||
});
|
audioInput.push(chunk);
|
||||||
|
});
|
||||||
export function useIA() {
|
|
||||||
return {
|
|
||||||
ia,
|
|
||||||
processAudio
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,10 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import { createWindow } from './window';
|
import { createWindow } from './window';
|
||||||
import { ipcMain, BrowserWindow } from "electron";
|
|
||||||
import { initSession } from './session';
|
import { initSession } from './session';
|
||||||
import { backgroundMitt } from './emitter';
|
import { initRecorder } from './audio';
|
||||||
// const portAudio = require('naudiodon');
|
|
||||||
|
|
||||||
interface RenderMessage {
|
let socket = false;
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The main function will be run after electron app is ready.
|
* The main function will be run after electron app is ready.
|
||||||
|
|
@ -42,55 +12,17 @@ const windowDismount = (): void => {
|
||||||
export async function main() {
|
export async function main() {
|
||||||
|
|
||||||
// create main window.
|
// create main window.
|
||||||
if (!win) win = await createWindow({
|
await createWindow({
|
||||||
width: 350,
|
width: 350,
|
||||||
height: 525,
|
height: 525,
|
||||||
resizable: true
|
resizable: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// Instantiate socket session with crimata-platorm.
|
// 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);
|
reject(res);
|
||||||
} else {
|
} else {
|
||||||
auth = true;
|
auth = true;
|
||||||
console.log(res);
|
|
||||||
resolve(res);
|
resolve(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { BrowserWindow, ipcMain } from "electron";
|
||||||
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
|
||||||
import { backgroundMitt } from './emitter';
|
import { backgroundMitt } from './emitter';
|
||||||
import { sendMessage } from './session';
|
import { sendMessage } from './session';
|
||||||
|
import { updateRecorder } from './audio';
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
interface WindowSettings {
|
interface WindowSettings {
|
||||||
|
|
@ -17,6 +18,22 @@ interface TextMessage {
|
||||||
content: string;
|
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 ) => {
|
const handleMessage = (_event, arg: TextMessage | Buffer ) => {
|
||||||
sendMessage(arg);
|
sendMessage(arg);
|
||||||
}
|
}
|
||||||
|
|
@ -24,11 +41,29 @@ const handleMessage = (_event, arg: TextMessage | Buffer ) => {
|
||||||
const windowMount = (): void => {
|
const windowMount = (): void => {
|
||||||
backgroundMitt.emit('window-active', true);
|
backgroundMitt.emit('window-active', true);
|
||||||
ipcMain.on('send-message', handleMessage);
|
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) => {
|
return new Promise((resolve, _reject) => {
|
||||||
const win: BrowserWindow = new BrowserWindow({
|
|
||||||
|
if (win) resolve();
|
||||||
|
|
||||||
|
win= new BrowserWindow({
|
||||||
width: options.width,
|
width: options.width,
|
||||||
height: options.height,
|
height: options.height,
|
||||||
resizable: options.resizable,
|
resizable: options.resizable,
|
||||||
|
|
@ -50,9 +85,12 @@ export const createWindow = async (options: WindowSettings): Promise<BrowserWind
|
||||||
win.loadURL("app://./index.html");
|
win.loadURL("app://./index.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle window close.
|
||||||
|
win.on("closed", windowDismount);
|
||||||
|
|
||||||
win.webContents.on('did-finish-load', () => {
|
win.webContents.on('did-finish-load', () => {
|
||||||
windowMount();
|
windowMount();
|
||||||
resolve(win);
|
if (win)resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue