@mymusictaste/async-audiorecorder
Version:
node-audiorecorder with async/await & typescript support
55 lines • 2.16 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncAudioRecorder = void 0;
/// <reference path="./module.d.ts" />
const node_audiorecorder_1 = __importDefault(require("node-audiorecorder"));
const defaults_js_1 = require("./defaults.js");
const listDevices_js_1 = require("./listDevices.js");
class AsyncAudioRecorder extends node_audiorecorder_1.default {
constructor(options, logger) {
const injectedOptions = defaults_js_1.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.buffer = [];
this._onData = (data) => this.buffer.push(data);
this._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);
});
};
this._reset();
this.on("close", () => this._reset());
}
start() {
var _a;
this._reset();
super.start();
(_a = this.stream()) === null || _a === void 0 ? void 0 : _a.on("data", this._onData);
return this;
}
stop() {
var _a;
(_a = this.stream()) === null || _a === void 0 ? void 0 : _a.off("data", this._onData);
super.stop();
return this;
}
}
AsyncAudioRecorder.availableDevices = listDevices_js_1.listRecordingDevices;
exports.AsyncAudioRecorder = AsyncAudioRecorder;
exports.default = AsyncAudioRecorder;
//# sourceMappingURL=index.js.map