add main function for window

This commit is contained in:
Enrique Hernandez 2021-02-04 16:36:50 -06:00
commit 9143689c90
3 changed files with 47 additions and 36 deletions

View file

@ -26,8 +26,6 @@ function createWindow() {
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
// NOTE uncomment for dev tools on app launch
if (!process.env.IS_TEST) win.webContents.openDevTools();
} else {
createProtocol("app");
// Load the index.html when not in development

39
src/background/main.ts Normal file
View file

@ -0,0 +1,39 @@
import {Recorder} from './recorder';
import { ipcMain } from "electron";
/*
* this code will be run after window has finished loading
*/
export function main() {
// connect to crimata-platform ws server
// start recorder
const recorder = new Recorder({
channelCount: 1,
sampleFormat: 16,
sampleRate: 16000,
deviceId: -1,
closeOnError: false,
}, 'binary');
const updateRecorder = (Event: any, record: boolean) => {
if (record) {
console.log('recording')
recorder.resume();
} else {
console.log('stop recording')
recorder.pause();
const data = recorder.getData();
// TODO: send data()
}
}
const sendMessage = (Event: any, payload: any) => {
}
// event handlers
ipcMain.on('update-recorder', updateRecorder);
ipcMain.on('send-message', updateRecorder);
// send message
}

View file

@ -57,18 +57,19 @@ export class Recorder implements RecorderI {
defaultEncoding: encoding
});
try {
this.restart();
ipcMain.on('update-recorder', this._update);
this.start();
} catch(e) {
console.log('MEGA ERROR!');
}
}
restart() {
private start(): void {
this.ia = new portAudio.AudioIO({
inOptions: this.recorderOptions
});
this._start();
this.ia.pipe(this.micWritable);
this.ia.start();
this.pause();
}
pause(): void {
@ -80,40 +81,13 @@ export class Recorder implements RecorderI {
}
getData(): string {
return this.micWritable.data;
const data = this.micWritable.data;
this.micWritable.data = '';
return data;
}
stop(): string {
this.ia.quit();
return this.getData();
}
private _start(): void {
this.ia.pipe(this.micWritable);
this.ia.start();
this.pause();
}
private _update = (Event: any, record: boolean) => {
if (record) {
// check for stream status
console.log('recording')
this.resume();
// this.ia.pipe(this.micWritable);
} else {
console.log('stop recording')
// unpipe
this.ia.unpipe(this.micWritable);
// stop stream
this.ia.quit();
this.ia = null;
// get stream data
this.micWritable.on('finish', () => {
console.log('finished writing data');
})
this.restart();
}
}
}