From d122184b6b685fcd9ba3c0b6f937226adf7c6c77 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sat, 5 Dec 2015 20:57:08 +0000 Subject: [PATCH] voice fixes --- .gitignore | 1 - lib/Voice/AudioEncoder.js | 8 ++-- lib/Voice/VoiceConnection.js | 4 +- src/Voice/AudioEncoder.js | 82 ++++++++++++++++++------------------ src/Voice/VoiceConnection.js | 4 +- test/msgbot.js | 38 +++++++++++++++++ 6 files changed, 87 insertions(+), 50 deletions(-) create mode 100644 test/msgbot.js diff --git a/.gitignore b/.gitignore index 36326a28f..e48f00973 100644 --- a/.gitignore +++ b/.gitignore @@ -36,5 +36,4 @@ build/Release node_modules test/auth.json examples/auth.json -test/msgbot.js docs/_build \ No newline at end of file diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index 4b8489b9b..37031d327 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -67,8 +67,7 @@ var AudioEncoder = (function () { var self = this; return new Promise(function (resolve, reject) { - var enc = _child_process2["default"].spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. - "-af", "volume=1", "pipe:1", "-i", "-"]); + var enc = _child_process2["default"].spawn(self.getCommand(), ['-i', "-", '-f', 's16le', '-ar', '48000', '-ac', 1, 'pipe:1']); stream.pipe(enc.stdin); @@ -102,8 +101,7 @@ var AudioEncoder = (function () { var self = this; return new Promise(function (resolve, reject) { - var enc = _child_process2["default"].spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. - "-af", "volume=1", "pipe:1", "-i", file]); + var enc = _child_process2["default"].spawn(self.getCommand(), ['-i', file, '-f', 's16le', '-ar', '48000', '-ac', 1, 'pipe:1']); enc.stdout.once("readable", function () { callback(null, { @@ -117,11 +115,13 @@ 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"); }); diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index ac43034ea..ac4125ee3 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -122,11 +122,10 @@ var VoiceConnection = (function (_EventEmitter) { self.playingIntent = retStream; function send() { - if (!self.playingIntent || !self.playing) { self.setSpeaking(false); retStream.emit("end"); - self; + console.log("ending 1"); return; } try { @@ -137,6 +136,7 @@ var VoiceConnection = (function (_EventEmitter) { if (onWarning) { retStream.emit("end"); self.setSpeaking(false); + console.log("ending 2"); return; } else { onWarning = true; diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index 8e1f86b2a..df94630a6 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -3,36 +3,36 @@ import cpoc from "child_process"; var opus; -try{ +try { opus = require("node-opus"); -}catch(e){ +} catch (e) { // no opus! } export default class AudioEncoder { - constructor(){ - if(opus){ + constructor() { + if (opus) { this.opus = new opus.OpusEncoder(48000, 1); } this.choice = false; } - opusBuffer(buffer){ + opusBuffer(buffer) { return this.opus.encode(buffer, 1920); } - getCommand(force){ + getCommand(force) { - if(this.choice && force) + if (this.choice && force) return choice; var choices = ["avconv", "ffmpeg"]; - for(var choice of choices){ + for (var choice of choices) { var p = cpoc.spawnSync(choice); - if(!p.error){ + if (!p.error) { this.choice = choice; return choice; } @@ -41,74 +41,74 @@ export default class AudioEncoder { return "help"; } - encodeStream(stream, callback=function(err, buffer){}){ + encodeStream(stream, callback = function (err, buffer) { }) { var self = this; return new Promise((resolve, reject) => { - var enc = cpoc.spawn(self.getCommand() , [ - "-f", "s16le", - "-ar", "48000", - "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. - "-af", "volume=1", - "pipe:1", - "-i", "-" + var enc = cpoc.spawn(self.getCommand(), [ + '-i', "-", + '-f', 's16le', + '-ar', '48000', + '-ac', 1, + 'pipe:1' ]); stream.pipe(enc.stdin); - enc.stdout.once("readable", function() { + enc.stdout.once("readable", function () { callback(null, { - proc : enc, - stream : enc.stdout, - instream : stream + proc: enc, + stream: enc.stdout, + instream: stream }); resolve({ - proc : enc, - stream : enc.stdout, - instream : stream + proc: enc, + stream: enc.stdout, + instream: stream }); }); - enc.stdout.on("end", function() { + enc.stdout.on("end", function () { callback("end"); reject("end"); }); - enc.stdout.on("close", function() { + enc.stdout.on("close", function () { callback("close"); reject("close"); }); }); } - encodeFile(file, callback=function(err, buffer){}){ + encodeFile(file, callback = function (err, buffer) { }) { var self = this; return new Promise((resolve, reject) => { - var enc = cpoc.spawn(self.getCommand() , [ - "-f", "s16le", - "-ar", "48000", - "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. - "-af", "volume=1", - "pipe:1", - "-i", file + var enc = cpoc.spawn(self.getCommand(), [ + '-i', file, + '-f', 's16le', + '-ar', '48000', + '-ac', 1, + 'pipe:1' ]); - enc.stdout.once("readable", function() { + enc.stdout.once("readable", function () { callback(null, { - proc : enc, - stream : enc.stdout + proc: enc, + stream: enc.stdout }); resolve({ - proc : enc, - stream : enc.stdout + proc: enc, + stream: enc.stdout }); }); - enc.stdout.on("end", function() { + enc.stdout.on("end", function () { + console.log("end"); callback("end"); reject("end"); }); - enc.stdout.on("close", function() { + enc.stdout.on("close", function () { + console.log("close"); callback("close"); reject("close"); }); diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index 4e4cb01bf..1505c620a 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -93,11 +93,10 @@ export default class VoiceConnection extends EventEmitter { self.playingIntent = retStream; function send() { - if (!self.playingIntent || !self.playing) { self.setSpeaking(false); retStream.emit("end"); - self + console.log("ending 1"); return; } try { @@ -108,6 +107,7 @@ export default class VoiceConnection extends EventEmitter { if (onWarning) { retStream.emit("end"); self.setSpeaking(false); + console.log("ending 2"); return; } else { onWarning = true; diff --git a/test/msgbot.js b/test/msgbot.js new file mode 100644 index 000000000..fff9ee268 --- /dev/null +++ b/test/msgbot.js @@ -0,0 +1,38 @@ +/* global describe */ +/* global process */ + +var Discord = require("../"); +var client = new Discord.Client(); +var request = require("request"); + +client.on("ready", () => { + console.log("ready"); +}); + +client.on("message", msg => { + + if(!msg.sender.equals(client.user)) + console.log("received message from " + msg.sender.username); + + if (msg.content === "$bind") { + msg.channel.server.channels.get("type", "voice").join(); + } + + if (msg.content.startsWith("$play")) { + var url = msg.content.split(" ")[1]; + + client.voiceConnection.playRawStream(request(url)); + + } + + if (msg.content === "$$$") { + client.sendMessage(msg.sender, "hi!"); + } + +}); + +console.log("INIT"); + +client.on("debug", console.log) + +client.login(process.env["ds_email"], process.env["ds_password"]).catch(console.log); \ No newline at end of file