Skip to content

Commit

Permalink
added volume control
Browse files Browse the repository at this point in the history
  • Loading branch information
amishshah committed Dec 12, 2015
1 parent df36816 commit d66b765
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 58 deletions.
29 changes: 6 additions & 23 deletions lib/Voice/AudioEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,14 @@ var AudioEncoder = (function () {
return "help";
};

AudioEncoder.prototype.encodeStream = function encodeStream(stream) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];

AudioEncoder.prototype.encodeStream = function encodeStream(stream, options) {
var self = this;
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);

enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
instream: stream,
channels: 2
});
resolve({
proc: enc,
stream: enc.stdout,
Expand All @@ -87,30 +79,23 @@ var AudioEncoder = (function () {
});

enc.stdout.on("end", function () {
callback("end");
reject("end");
});

enc.stdout.on("close", function () {
callback("close");
reject("close");
});
});
};

AudioEncoder.prototype.encodeFile = function encodeFile(file) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];

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', '-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 () {
callback(null, {
proc: enc,
stream: enc.stdout,
channels: 2
});
resolve({
proc: enc,
stream: enc.stdout,
Expand All @@ -120,13 +105,11 @@ var AudioEncoder = (function () {

enc.stdout.on("end", function () {
console.log("end");
callback("end");
reject("end");
});

enc.stdout.on("close", function () {
console.log("close");
callback("close");
reject("close");
});
});
Expand Down
20 changes: 16 additions & 4 deletions lib/Voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,17 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.playFile = function playFile(stream) {
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;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
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;
var intent = self.playStream(data.stream, 2);
resolve(intent);
Expand All @@ -255,11 +261,17 @@ var VoiceConnection = (function (_EventEmitter) {
VoiceConnection.prototype.playRawStream = function playRawStream(stream) {
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;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
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.instream = data.instream;
var intent = self.playStream(data.stream);
Expand Down
37 changes: 17 additions & 20 deletions src/Voice/AudioEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class AudioEncoder {
return "help";
}

encodeStream(stream, callback = function (err, buffer) { }) {
encodeStream(stream, options) {
var self = this;
return new Promise((resolve, reject) => {
var enc = cpoc.spawn(self.getCommand(), [
Expand All @@ -50,18 +50,13 @@ export default class AudioEncoder {
'-f', 's16le',
'-ar', '48000',
'-ac', 2,
'pipe:1'
'pipe:1',
'-af', 'volume=' + (options.volume || 1)
], {stdio: ['pipe', 'pipe', 'ignore']});

stream.pipe(enc.stdin);

enc.stdout.once("readable", function () {
callback(null, {
proc: enc,
stream: enc.stdout,
instream: stream,
channels : 2
});
resolve({
proc: enc,
stream: enc.stdout,
Expand All @@ -71,18 +66,16 @@ export default class AudioEncoder {
});

enc.stdout.on("end", function () {
callback("end");
reject("end");
});

enc.stdout.on("close", function () {
callback("close");
reject("close");
});
});
}

encodeFile(file, callback = function (err, buffer) { }) {
encodeFile(file, options) {
var self = this;
return new Promise((resolve, reject) => {
var enc = cpoc.spawn(self.getCommand(), [
Expand All @@ -91,15 +84,21 @@ export default class AudioEncoder {
'-f', 's16le',
'-ar', '48000',
'-ac', 2,
'pipe:1'
], {stdio: ['pipe', 'pipe', 'ignore']});
'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 () {
callback(null, {
proc: enc,
stream: enc.stdout,
channels : 2
});
resolve({
proc: enc,
stream: enc.stdout,
Expand All @@ -109,13 +108,11 @@ export default class AudioEncoder {

enc.stdout.on("end", function () {
console.log("end");
callback("end");
reject("end");
});

enc.stdout.on("close", function () {
console.log("close");
callback("close");
reject("close");
});
});
Expand Down
18 changes: 14 additions & 4 deletions src/Voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise((resolve, reject) => {
this.encoder
.encodeFile(stream)
.encodeFile(stream, options)
.catch(error)
.then(data => {
self.streamProc = data.proc;
Expand All @@ -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;
if (typeof options === "function") {
// options is the callback
callback = options;
options = {};
}
return new Promise((resolve, reject) => {
this.encoder
.encodeStream(stream)
.encodeStream(stream, options)
.catch(error)
.then(data => {
self.streamProc = data.proc;
Expand Down
10 changes: 3 additions & 7 deletions test/msgbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ client.on("message", msg => {
if (msg.content.startsWith("$play")) {
var url = msg.content.split(" ")[1];

client.voiceConnection.playFile(url);

console.log(request.get(url).end());
client.voiceConnection.playFile(url, {
volume : 0.1
});

}

Expand All @@ -49,10 +49,6 @@ console.log("INIT");

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);


Expand Down

0 comments on commit d66b765

Please sign in to comment.