Adding volume wrapper

This commit is contained in:
Aaron Scherer
2016-02-09 15:12:21 -08:00
parent c8acc17d8e
commit 3c64cfce4a
7 changed files with 266 additions and 80 deletions

View File

@@ -10,12 +10,16 @@ var _child_process = require("child_process");
var _child_process2 = _interopRequireDefault(_child_process);
// no opus!
var _VolumeTransformer = require("./VolumeTransformer");
var _VolumeTransformer2 = _interopRequireDefault(_VolumeTransformer);
var opus;
try {
opus = require("node-opus");
} catch (e) {
// no opus!
}
} catch (e) {}
var AudioEncoder = (function () {
function AudioEncoder() {
@@ -49,7 +53,6 @@ var AudioEncoder = (function () {
};
AudioEncoder.prototype.getCommand = function getCommand(force) {
if (this.choice && force) return choice;
var choices = ["avconv", "ffmpeg"];
@@ -79,74 +82,88 @@ var AudioEncoder = (function () {
};
AudioEncoder.prototype.encodeStream = function encodeStream(stream, options) {
var self = this;
var _this = this;
return new Promise(function (resolve, reject) {
var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', '-', '-f', 's16le', '-ar', '48000', '-af', 'volume=' + (options.volume || 1), '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] });
_this.volume = new _VolumeTransformer2["default"](options.volume || 1);
var enc = _child_process2["default"].spawn(_this.getCommand(), ['-loglevel', '0', '-i', '-', '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] });
stream.pipe(enc.stdin);
enc.stdout.pipe(_this.volume);
enc.stdout.once("readable", function () {
_this.volume.once("readable", function () {
resolve({
proc: enc,
stream: enc.stdout,
stream: _this.volume,
instream: stream,
channels: 2
});
});
enc.stdout.on("end", function () {
_this.volume.on("end", function () {
reject("end");
});
enc.stdout.on("close", function () {
_this.volume.on("close", function () {
reject("close");
});
});
};
AudioEncoder.prototype.encodeFile = function encodeFile(file, options) {
var self = this;
return new Promise(function (resolve, reject) {
var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', file, '-f', 's16le', '-ar', '48000', '-af', 'volume=' + (options.volume || 1), '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] });
var _this2 = this;
enc.stdout.once("readable", function () {
return new Promise(function (resolve, reject) {
_this2.volume = new _VolumeTransformer2["default"](options.volume || 1);
var enc = _child_process2["default"].spawn(_this2.getCommand(), ['-loglevel', '0', '-i', file, '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] });
enc.stdout.pipe(_this2.volume);
_this2.volume.once("readable", function () {
resolve({
proc: enc,
stream: enc.stdout,
stream: _this2.volume,
channels: 2
});
});
enc.stdout.on("end", function () {
_this2.volume.on("end", function () {
reject("end");
});
enc.stdout.on("close", function () {
_this2.volume.on("close", function () {
reject("close");
});
});
};
AudioEncoder.prototype.encodeArbitraryFFmpeg = function encodeArbitraryFFmpeg(ffmpegOptions) {
var self = this;
var _this3 = this;
return new Promise(function (resolve, reject) {
_this3.volume = new _VolumeTransformer2["default"](1);
// add options discord.js needs
var options = ffmpegOptions.concat(['-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1']);
var enc = _child_process2["default"].spawn(self.getCommand(), options, { stdio: ['pipe', 'pipe', 'ignore'] });
var enc = _child_process2["default"].spawn(_this3.getCommand(), options, { stdio: ['pipe', 'pipe', 'ignore'] });
enc.stdout.once("readable", function () {
enc.stdout.pipe(_this3.volume);
_this3.volume.once("readable", function () {
resolve({
proc: enc,
stream: enc.stdout,
stream: _this3.volume,
channels: 2
});
});
enc.stdout.on("end", function () {
_this3.volume.on("end", function () {
reject("end");
});
enc.stdout.on("close", function () {
_this3.volume.on("close", function () {
reject("close");
});
});