@mymusictaste/async-audiorecorder
Version:
node-audiorecorder with async/await & typescript support
48 lines • 1.61 kB
JavaScript
/// <reference path="./module.d.ts" />
import AudioRecorder from "node-audiorecorder";
import { DEFAULTS } from "./defaults.js";
import { listRecordingDevices } from "./listDevices.js";
class AsyncAudioRecorder extends AudioRecorder {
promise;
buffer = [];
static availableDevices = listRecordingDevices;
constructor(options, logger) {
const injectedOptions = DEFAULTS;
Object.assign({}, injectedOptions, options);
// note: original ctor also got some default options.
// see: https://github.com/RedKenrok/node-audiorecorder/blob/master/library/index.js
super(injectedOptions, logger);
this._reset();
this.on("close", () => this._reset());
}
_onData = (data) => this.buffer.push(data);
_reset = () => {
let successCbk;
let failedCbk;
this.promise = new Promise((resolve, reject) => {
successCbk = () => resolve(Buffer.concat(this.buffer));
failedCbk = reject;
this.on("close", successCbk);
this.on("error", failedCbk);
});
this.promise.finally(() => {
this.buffer = [];
this.off("close", successCbk);
this.off("error", failedCbk);
});
};
start() {
this._reset();
super.start();
this.stream()?.on("data", this._onData);
return this;
}
stop() {
this.stream()?.off("data", this._onData);
super.stop();
return this;
}
}
export { AsyncAudioRecorder };
export default AsyncAudioRecorder;
//# sourceMappingURL=index.js.map