voice fixes

This commit is contained in:
Amish Shah
2015-12-05 20:57:08 +00:00
parent de19475ae0
commit d122184b6b
6 changed files with 87 additions and 50 deletions

1
.gitignore vendored
View File

@@ -36,5 +36,4 @@ build/Release
node_modules node_modules
test/auth.json test/auth.json
examples/auth.json examples/auth.json
test/msgbot.js
docs/_build docs/_build

View File

@@ -67,8 +67,7 @@ var AudioEncoder = (function () {
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(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. var enc = _child_process2["default"].spawn(self.getCommand(), ['-i', "-", '-f', 's16le', '-ar', '48000', '-ac', 1, 'pipe:1']);
"-af", "volume=1", "pipe:1", "-i", "-"]);
stream.pipe(enc.stdin); stream.pipe(enc.stdin);
@@ -102,8 +101,7 @@ var AudioEncoder = (function () {
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(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. var enc = _child_process2["default"].spawn(self.getCommand(), ['-i', file, '-f', 's16le', '-ar', '48000', '-ac', 1, 'pipe:1']);
"-af", "volume=1", "pipe:1", "-i", file]);
enc.stdout.once("readable", function () { enc.stdout.once("readable", function () {
callback(null, { callback(null, {
@@ -117,11 +115,13 @@ var AudioEncoder = (function () {
}); });
enc.stdout.on("end", function () { enc.stdout.on("end", function () {
console.log("end");
callback("end"); callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function () { enc.stdout.on("close", function () {
console.log("close");
callback("close"); callback("close");
reject("close"); reject("close");
}); });

View File

@@ -122,11 +122,10 @@ var VoiceConnection = (function (_EventEmitter) {
self.playingIntent = retStream; self.playingIntent = retStream;
function send() { function send() {
if (!self.playingIntent || !self.playing) { if (!self.playingIntent || !self.playing) {
self.setSpeaking(false); self.setSpeaking(false);
retStream.emit("end"); retStream.emit("end");
self; console.log("ending 1");
return; return;
} }
try { try {
@@ -137,6 +136,7 @@ var VoiceConnection = (function (_EventEmitter) {
if (onWarning) { if (onWarning) {
retStream.emit("end"); retStream.emit("end");
self.setSpeaking(false); self.setSpeaking(false);
console.log("ending 2");
return; return;
} else { } else {
onWarning = true; onWarning = true;

View File

@@ -3,36 +3,36 @@
import cpoc from "child_process"; import cpoc from "child_process";
var opus; var opus;
try{ try {
opus = require("node-opus"); opus = require("node-opus");
}catch(e){ } catch (e) {
// no opus! // no opus!
} }
export default class AudioEncoder { export default class AudioEncoder {
constructor(){ constructor() {
if(opus){ if (opus) {
this.opus = new opus.OpusEncoder(48000, 1); this.opus = new opus.OpusEncoder(48000, 1);
} }
this.choice = false; this.choice = false;
} }
opusBuffer(buffer){ opusBuffer(buffer) {
return this.opus.encode(buffer, 1920); return this.opus.encode(buffer, 1920);
} }
getCommand(force){ getCommand(force) {
if(this.choice && force) if (this.choice && force)
return choice; return choice;
var choices = ["avconv", "ffmpeg"]; var choices = ["avconv", "ffmpeg"];
for(var choice of choices){ for (var choice of choices) {
var p = cpoc.spawnSync(choice); var p = cpoc.spawnSync(choice);
if(!p.error){ if (!p.error) {
this.choice = choice; this.choice = choice;
return choice; return choice;
} }
@@ -41,74 +41,74 @@ export default class AudioEncoder {
return "help"; return "help";
} }
encodeStream(stream, callback=function(err, buffer){}){ encodeStream(stream, callback = function (err, buffer) { }) {
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(), [
"-f", "s16le", '-i', "-",
"-ar", "48000", '-f', 's16le',
"-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. '-ar', '48000',
"-af", "volume=1", '-ac', 1,
"pipe:1", 'pipe:1'
"-i", "-"
]); ]);
stream.pipe(enc.stdin); stream.pipe(enc.stdin);
enc.stdout.once("readable", function() { enc.stdout.once("readable", function () {
callback(null, { callback(null, {
proc : enc, proc: enc,
stream : enc.stdout, stream: enc.stdout,
instream : stream instream: stream
}); });
resolve({ resolve({
proc : enc, proc: enc,
stream : enc.stdout, stream: enc.stdout,
instream : stream instream: stream
}); });
}); });
enc.stdout.on("end", function() { enc.stdout.on("end", function () {
callback("end"); callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function() { enc.stdout.on("close", function () {
callback("close"); callback("close");
reject("close"); reject("close");
}); });
}); });
} }
encodeFile(file, callback=function(err, buffer){}){ encodeFile(file, callback = function (err, buffer) { }) {
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(), [
"-f", "s16le", '-i', file,
"-ar", "48000", '-f', 's16le',
"-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. '-ar', '48000',
"-af", "volume=1", '-ac', 1,
"pipe:1", 'pipe:1'
"-i", file
]); ]);
enc.stdout.once("readable", function() { enc.stdout.once("readable", function () {
callback(null, { callback(null, {
proc : enc, proc: enc,
stream : enc.stdout stream: enc.stdout
}); });
resolve({ resolve({
proc : enc, proc: enc,
stream : enc.stdout stream: enc.stdout
}); });
}); });
enc.stdout.on("end", function() { enc.stdout.on("end", function () {
console.log("end");
callback("end"); callback("end");
reject("end"); reject("end");
}); });
enc.stdout.on("close", function() { enc.stdout.on("close", function () {
console.log("close");
callback("close"); callback("close");
reject("close"); reject("close");
}); });

View File

@@ -93,11 +93,10 @@ export default class VoiceConnection extends EventEmitter {
self.playingIntent = retStream; self.playingIntent = retStream;
function send() { function send() {
if (!self.playingIntent || !self.playing) { if (!self.playingIntent || !self.playing) {
self.setSpeaking(false); self.setSpeaking(false);
retStream.emit("end"); retStream.emit("end");
self console.log("ending 1");
return; return;
} }
try { try {
@@ -108,6 +107,7 @@ export default class VoiceConnection extends EventEmitter {
if (onWarning) { if (onWarning) {
retStream.emit("end"); retStream.emit("end");
self.setSpeaking(false); self.setSpeaking(false);
console.log("ending 2");
return; return;
} else { } else {
onWarning = true; onWarning = true;

38
test/msgbot.js Normal file
View File

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