added volume control

This commit is contained in:
Amish Shah
2015-12-12 17:52:25 +00:00
parent df368168ee
commit d66b765933
5 changed files with 56 additions and 58 deletions

View File

@@ -62,22 +62,14 @@ var AudioEncoder = (function () {
return "help"; return "help";
}; };
AudioEncoder.prototype.encodeStream = function encodeStream(stream) { AudioEncoder.prototype.encodeStream = function encodeStream(stream, options) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', '-', '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] }); var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', '-', '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1', '-af', 'volume=' + (options.volume || 1)], { stdio: ['pipe', 'pipe', 'ignore'] });
stream.pipe(enc.stdin); stream.pipe(enc.stdin);
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
instream: stream,
channels: 2
});
resolve({ resolve({
proc: enc, proc: enc,
stream: enc.stdout, stream: enc.stdout,
@@ -87,30 +79,23 @@ var AudioEncoder = (function () {
}); });
enc.stdout.on("end", function () { enc.stdout.on("end", function () {
callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function () { enc.stdout.on("close", function () {
callback("close");
reject("close"); reject("close");
}); });
}); });
}; };
AudioEncoder.prototype.encodeFile = function encodeFile(file) { AudioEncoder.prototype.encodeFile = function encodeFile(file, options) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', file, '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] }); var enc = _child_process2["default"].spawn(self.getCommand(), ['-loglevel', '0', '-i', file, '-f', 's16le', '-ar', '48000', '-ac', 2, 'pipe:1', '-af', '"volume=' + (options.volume || 1) + '"'], { stdio: ['pipe', 'pipe', 'ignore'] });
console.log(['-loglevel', '0', '-i', file, '-f', 's16le', '-ar', '48000', '-af', '"volume=' + (options.volume || 1) + '"', '-ac', 2, 'pipe:1'].join(" "));
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
channels: 2
});
resolve({ resolve({
proc: enc, proc: enc,
stream: enc.stdout, stream: enc.stdout,
@@ -120,13 +105,11 @@ var AudioEncoder = (function () {
enc.stdout.on("end", function () { enc.stdout.on("end", function () {
console.log("end"); console.log("end");
callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function () { enc.stdout.on("close", function () {
console.log("close"); console.log("close");
callback("close");
reject("close"); reject("close");
}); });
}); });

View File

@@ -233,11 +233,17 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.playFile = function playFile(stream) { VoiceConnection.prototype.playFile = function playFile(stream) {
var _this = this; var _this = this;
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1]; var options = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var callback = arguments.length <= 2 || arguments[2] === undefined ? function (err, str) {} : arguments[2];
var self = this; var self = this;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
_this.encoder.encodeFile(stream)["catch"](error).then(function (data) { _this.encoder.encodeFile(stream, options)["catch"](error).then(function (data) {
self.streamProc = data.proc; self.streamProc = data.proc;
var intent = self.playStream(data.stream, 2); var intent = self.playStream(data.stream, 2);
resolve(intent); resolve(intent);
@@ -255,11 +261,17 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.playRawStream = function playRawStream(stream) { VoiceConnection.prototype.playRawStream = function playRawStream(stream) {
var _this2 = this; var _this2 = this;
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1]; var options = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var callback = arguments.length <= 2 || arguments[2] === undefined ? function (err, str) {} : arguments[2];
var self = this; var self = this;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
_this2.encoder.encodeStream(stream)["catch"](error).then(function (data) { _this2.encoder.encodeStream(stream, options)["catch"](error).then(function (data) {
self.streamProc = data.proc; self.streamProc = data.proc;
self.instream = data.instream; self.instream = data.instream;
var intent = self.playStream(data.stream); var intent = self.playStream(data.stream);

View File

@@ -41,7 +41,7 @@ export default class AudioEncoder {
return "help"; return "help";
} }
encodeStream(stream, callback = function (err, buffer) { }) { encodeStream(stream, options) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var enc = cpoc.spawn(self.getCommand(), [ var enc = cpoc.spawn(self.getCommand(), [
@@ -50,18 +50,13 @@ export default class AudioEncoder {
'-f', 's16le', '-f', 's16le',
'-ar', '48000', '-ar', '48000',
'-ac', 2, '-ac', 2,
'pipe:1' 'pipe:1',
'-af', 'volume=' + (options.volume || 1)
], {stdio: ['pipe', 'pipe', 'ignore']}); ], {stdio: ['pipe', 'pipe', 'ignore']});
stream.pipe(enc.stdin); stream.pipe(enc.stdin);
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
instream: stream,
channels : 2
});
resolve({ resolve({
proc: enc, proc: enc,
stream: enc.stdout, stream: enc.stdout,
@@ -71,18 +66,16 @@ export default class AudioEncoder {
}); });
enc.stdout.on("end", function () { enc.stdout.on("end", function () {
callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function () { enc.stdout.on("close", function () {
callback("close");
reject("close"); reject("close");
}); });
}); });
} }
encodeFile(file, callback = function (err, buffer) { }) { encodeFile(file, options) {
var self = this; var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var enc = cpoc.spawn(self.getCommand(), [ var enc = cpoc.spawn(self.getCommand(), [
@@ -91,15 +84,21 @@ export default class AudioEncoder {
'-f', 's16le', '-f', 's16le',
'-ar', '48000', '-ar', '48000',
'-ac', 2, '-ac', 2,
'pipe:1' 'pipe:1',
], {stdio: ['pipe', 'pipe', 'ignore']}); '-af', '"volume=' + (options.volume || 1)+'"'
], { stdio: ['pipe', 'pipe', 'ignore'] });
console.log([
'-loglevel', '0',
'-i', file,
'-f', 's16le',
'-ar', '48000',
'-af', '"volume=' + (options.volume || 1) + '"',
'-ac', 2,
'pipe:1',
].join(" "));
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
channels : 2
});
resolve({ resolve({
proc: enc, proc: enc,
stream: enc.stdout, stream: enc.stdout,
@@ -109,13 +108,11 @@ export default class AudioEncoder {
enc.stdout.on("end", function () { enc.stdout.on("end", function () {
console.log("end"); console.log("end");
callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function () { enc.stdout.on("close", function () {
console.log("close"); console.log("close");
callback("close");
reject("close"); reject("close");
}); });
}); });

View File

@@ -205,11 +205,16 @@ export default class VoiceConnection extends EventEmitter {
}) })
} }
playFile(stream, callback = function (err, str) { }) { playFile(stream, options=false, callback = function (err, str) { }) {
var self = this; var self = this;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.encoder this.encoder
.encodeFile(stream) .encodeFile(stream, options)
.catch(error) .catch(error)
.then(data => { .then(data => {
self.streamProc = data.proc; self.streamProc = data.proc;
@@ -225,11 +230,16 @@ export default class VoiceConnection extends EventEmitter {
}) })
} }
playRawStream(stream, callback = function (err, str) { }) { playRawStream(stream, options=false, callback = function (err, str) { }) {
var self = this; var self = this;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.encoder this.encoder
.encodeStream(stream) .encodeStream(stream, options)
.catch(error) .catch(error)
.then(data => { .then(data => {
self.streamProc = data.proc; self.streamProc = data.proc;

View File

@@ -33,9 +33,9 @@ client.on("message", msg => {
if (msg.content.startsWith("$play")) { if (msg.content.startsWith("$play")) {
var url = msg.content.split(" ")[1]; var url = msg.content.split(" ")[1];
client.voiceConnection.playFile(url); client.voiceConnection.playFile(url, {
volume : 0.1
console.log(request.get(url).end()); });
} }
@@ -49,10 +49,6 @@ console.log("INIT");
client.on("debug", console.log); client.on("debug", console.log);
client.on("unknown", p => {
console.log(p);
});
client.login(process.env["ds_email"], process.env["ds_password"]).catch(console.log); client.login(process.env["ds_email"], process.env["ds_password"]).catch(console.log);