testing audio
This commit is contained in:
parent
426b847cce
commit
d113de2aa1
3 changed files with 50 additions and 32 deletions
|
|
@ -4,10 +4,11 @@
|
|||
|
||||
import { sendAudio } from './session'
|
||||
import { ipcMain } from "electron";
|
||||
import { backgroundMitt } from './emitter';
|
||||
const portAudio = require('naudiodon');
|
||||
|
||||
let ai: typeof portAudio.AudioIO;
|
||||
let ao: typeof portAudio.AudioIO;
|
||||
let ai: typeof portAudio.AudioIO | null = null;
|
||||
let ao: typeof portAudio.AudioIO | null = null;
|
||||
let audioInput: Buffer[] = [];
|
||||
const encoding = "base64";
|
||||
const audioOptions = {
|
||||
|
|
@ -18,41 +19,47 @@ const audioOptions = {
|
|||
closeOnError: false,
|
||||
}
|
||||
|
||||
// main function run by run.ts module.
|
||||
backgroundMitt.once('window-active', (state: boolean) => {
|
||||
if(!state) {
|
||||
stopStream();
|
||||
}
|
||||
});
|
||||
|
||||
// main audio function run by run.ts module.
|
||||
export function initAudioIO(): void {
|
||||
// init portAudio readable stream once.
|
||||
if (!ai) {
|
||||
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
||||
|
||||
// base64 encoding needed for google speech to text.
|
||||
ai.setEncoding(encoding);
|
||||
|
||||
// pause the portAudio data flow as soon as stream is initiated.
|
||||
ai.start();
|
||||
ai.pause();
|
||||
|
||||
ai.on('error', (e: Error) => {
|
||||
console.log('Error recording audio', + e);
|
||||
});
|
||||
ai.on('data', (chunk: string) => {
|
||||
// turn string base64 data into buffer object.
|
||||
console.log('received string chunk of length', chunk.length)
|
||||
const buf = Buffer.from(chunk, encoding);
|
||||
audioInput.push(buf);
|
||||
});
|
||||
if (ai != null) {
|
||||
ai.quit();
|
||||
audioInput = [];
|
||||
}
|
||||
|
||||
// init portAudio writable stream once.
|
||||
if (!ao) {
|
||||
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
||||
ao.start();
|
||||
}
|
||||
ai = new portAudio.AudioIO({ inOptions: audioOptions });
|
||||
|
||||
// base64 encoding needed for google speech to text.
|
||||
ai.setEncoding(encoding);
|
||||
|
||||
// pause the portAudio data flow as soon as stream is initiated.
|
||||
ai.start();
|
||||
ai.pause();
|
||||
|
||||
ai.on('data', (chunk: string) => {
|
||||
// turn string base64 data into buffer object.
|
||||
const buf = Buffer.from(chunk, encoding);
|
||||
audioInput.push(buf);
|
||||
});
|
||||
|
||||
if (ao != null) {
|
||||
ao.end();
|
||||
}
|
||||
ao = new portAudio.AudioIO({ outOptions: audioOptions });
|
||||
ao.start();
|
||||
}
|
||||
|
||||
// run this function on space bar key up and down.
|
||||
// callback run on space bar key up and down.
|
||||
const updateRecorder = (_event, record: boolean): void => {
|
||||
if (record) {
|
||||
// resume mic data flow on space bar key down.
|
||||
while(audioInput.length) { audioInput.pop() }
|
||||
ai.resume();
|
||||
} else {
|
||||
// stop mic data flow on space bar key up.
|
||||
|
|
@ -60,7 +67,7 @@ const updateRecorder = (_event, record: boolean): void => {
|
|||
const audio = Buffer.concat(audioInput);
|
||||
sendAudio(audio);
|
||||
// clear audioInput.
|
||||
while(audioInput.length) { audioInput.pop()}
|
||||
while(audioInput.length) { audioInput.pop() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,9 +110,20 @@ export function play(input: Array<Buffer>): void {
|
|||
}
|
||||
}
|
||||
|
||||
function stopStream() {
|
||||
if(ai != null) {
|
||||
ai.quit();
|
||||
ai = null;
|
||||
}
|
||||
if (ao != null) {
|
||||
ao.quit();
|
||||
ao = null;
|
||||
}
|
||||
}
|
||||
|
||||
// utility function used by play func.
|
||||
function bufSplit(input: Array<Buffer>): Array<Buffer> {
|
||||
let result: Buffer[] = [];
|
||||
const result: Buffer[] = [];
|
||||
input.forEach((b: Buffer) => {
|
||||
// split buffer into two.
|
||||
result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length));
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
import { app } from "electron";
|
||||
|
|
@ -6,7 +7,7 @@ import { backgroundMitt } from './emitter';
|
|||
|
||||
let winActive: boolean;
|
||||
|
||||
backgroundMitt.on('window-active', (state: boolean) => {
|
||||
backgroundMitt.once('window-active', (state: boolean) => {
|
||||
winActive = state;
|
||||
});
|
||||
|
||||
|
|
@ -23,7 +24,6 @@ export function initApp(dev: boolean): void {
|
|||
}
|
||||
});
|
||||
|
||||
// TODO: Debug activate functionaity: currently not working.
|
||||
// Might have to de-reference window object
|
||||
app.on("activate", () => {
|
||||
if (winActive === false) {
|
||||
|
|
|
|||
|
|
@ -110,4 +110,4 @@ export async function createWindow(options: WindowSettings): Promise<void> {
|
|||
});
|
||||
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue