add recorder file
This commit is contained in:
parent
acbf85744a
commit
1a328b41b9
3 changed files with 84 additions and 13 deletions
|
|
@ -9,20 +9,11 @@ const fs = require('fs');
|
||||||
import WebSocket from "ws";
|
import WebSocket from "ws";
|
||||||
const portAudio = require('naudiodon');
|
const portAudio = require('naudiodon');
|
||||||
|
|
||||||
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
|
// const ai = new portAudio.AudioIO({
|
||||||
const audioOptions = {
|
// inOptions: audioOptions
|
||||||
channelCount: 2,
|
// });
|
||||||
sampleFormat: portAudio.sampleFormat16Bit,
|
|
||||||
sampleRate: 16000,
|
|
||||||
deviceId: -1, // Use -1 or omit the deviceId to select the default device
|
|
||||||
closeOnError: false // Close the stream if an audio error is detected, if set false then just log the error
|
|
||||||
}
|
|
||||||
|
|
||||||
const ai = new portAudio.AudioIO({
|
|
||||||
inOptions: audioOptions
|
|
||||||
});
|
|
||||||
//ai.pipe();
|
//ai.pipe();
|
||||||
ai.start();
|
// ai.start();
|
||||||
|
|
||||||
// Keep a global reference of the window object, if you don't, the window will
|
// Keep a global reference of the window object, if you don't, the window will
|
||||||
// be closed automatically when the JavaScript object is garbage collected.
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
|
|
|
||||||
79
src/background/recorder.ts
Normal file
79
src/background/recorder.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
|
const portAudio = require('naudiodon');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
|
const { Writable } = require('stream');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
|
const { StringDecoder } = require('string_decoder');
|
||||||
|
|
||||||
|
interface RecorderI {
|
||||||
|
start(): void;
|
||||||
|
stop(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class StringWritable extends Writable {
|
||||||
|
|
||||||
|
constructor(options: {
|
||||||
|
defaultEncoding: string;
|
||||||
|
}) {
|
||||||
|
super(options);
|
||||||
|
this._decoder = new StringDecoder(options && options.defaultEncoding);
|
||||||
|
this.data = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
_write(chunk: Buffer, encoding: string, callback: (error? : Error) => void) {
|
||||||
|
if (encoding === 'buffer') {
|
||||||
|
chunk = this._decoder.write(chunk);
|
||||||
|
}
|
||||||
|
this.data += chunk;
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
_final(callback: (error? : Error) => void) {
|
||||||
|
this.data += this._decoder.end();
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Recorder implements RecorderI {
|
||||||
|
|
||||||
|
private ia: any;
|
||||||
|
private micWritable: StringWritable;
|
||||||
|
|
||||||
|
constructor(options: {
|
||||||
|
|
||||||
|
}) {
|
||||||
|
this.ia = new portAudio.AudioIO({
|
||||||
|
inOptions: options
|
||||||
|
});
|
||||||
|
this.micWritable = new StringWritable({
|
||||||
|
defaultEncoding: 'binary'
|
||||||
|
});
|
||||||
|
this.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
start(): void {
|
||||||
|
console.log('inititate recording');
|
||||||
|
this.ia.pipe(this.micWritable);
|
||||||
|
this.ia.start();
|
||||||
|
this.ia.pause();
|
||||||
|
}
|
||||||
|
|
||||||
|
record(): void {
|
||||||
|
this.ia.resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
pause(): void {
|
||||||
|
this.ia.pause();
|
||||||
|
}
|
||||||
|
|
||||||
|
getData(): string {
|
||||||
|
return this.micWritable.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): string {
|
||||||
|
this.ia.quit();
|
||||||
|
return this.micWritable.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const speech = require('@google-cloud/speech');
|
const speech = require('@google-cloud/speech');
|
||||||
const portAudio = require('naudiodon');
|
const portAudio = require('naudiodon');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue