Merge remote-tracking branch 'upstream/indev' into indev

This commit is contained in:
abalabahaha
2015-12-05 16:57:13 -08:00
8 changed files with 244 additions and 75 deletions

View File

@@ -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){
this.opus = new opus.OpusEncoder(48000, 1);
constructor() {
if (opus) {
this.opus = new opus.OpusEncoder(48000, 2);
}
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,78 @@ 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', 2,
'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,
channels : 2
});
resolve({
proc : enc,
stream : enc.stdout,
instream : stream
proc: enc,
stream: enc.stdout,
instream: stream,
channels : 2
});
});
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', 2,
'pipe:1'
]);
enc.stdout.once("readable", function() {
enc.stdout.once("readable", function () {
callback(null, {
proc : enc,
stream : enc.stdout
proc: enc,
stream: enc.stdout,
channels : 2
});
resolve({
proc : enc,
stream : enc.stdout
proc: enc,
stream: enc.stdout,
channels : 2
});
});
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");
});

View File

@@ -73,7 +73,7 @@ export default class VoiceConnection extends EventEmitter {
}
}
playStream(stream) {
playStream(stream, channels=2) {
var self = this;
@@ -93,21 +93,21 @@ 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 {
var buffer = stream.read(1920);
var buffer = stream.read(1920 * channels);
if (!buffer) {
if (onWarning) {
retStream.emit("end");
self.setSpeaking(false);
console.log("ending 2");
return;
} else {
onWarning = true;
@@ -116,8 +116,8 @@ export default class VoiceConnection extends EventEmitter {
}
}
if(buffer.length !== 1920) {
var newBuffer = new Buffer(1920).fill(0);
if(buffer.length !== 1920 * channels) {
var newBuffer = new Buffer(1920 * channels).fill(0);
buffer.copy(newBuffer);
buffer = newBuffer;
}
@@ -213,7 +213,7 @@ export default class VoiceConnection extends EventEmitter {
.catch(error)
.then(data => {
self.streamProc = data.proc;
var intent = self.playStream(data.stream);
var intent = self.playStream(data.stream, 2);
resolve(intent);
callback(null, intent);