Tidy up of code

This commit is contained in:
hydrabolt
2015-11-07 18:57:23 +00:00
parent af8da5b0a2
commit fc31df3fcf
2 changed files with 122 additions and 70 deletions

View File

@@ -32,6 +32,7 @@ var VoiceConnection = (function () {
this.udp = null;
this.playingIntent = null;
this.playing = false;
this.streamTime = 0;
this.init();
}
@@ -55,6 +56,7 @@ var VoiceConnection = (function () {
}
var retStream = new StreamIntent();
var onWarning = false;
self.playingIntent = retStream;
function send() {
@@ -70,9 +72,22 @@ var VoiceConnection = (function () {
if (!buffer) {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return;
}
if (buffer.length !== 1920) {
if (onWarning) {
retStream.emit("end");
stream.destroy();
self.setSpeaking(false);
return;
} else {
onWarning = true;
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return;
}
}
if (buffer && buffer.length === 1920) {
count++;
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
time + 9600 < 4294967295 ? time += 960 : time = 0;
@@ -81,14 +96,12 @@ var VoiceConnection = (function () {
var nextTime = startTime + count * length;
setTimeout(function () {
send();
}, length + (nextTime - Date.now()));
self.streamTime = count * length;
setTimeout(send, length + (nextTime - Date.now()));
if (!self.playing) self.setSpeaking(true);
} else {
retStream.emit("end");
self.setSpeaking(false);
}
retStream.emit("time", self.streamTime);
} catch (e) {
retStream.emit("error", e);
}
@@ -140,27 +153,42 @@ var VoiceConnection = (function () {
};
VoiceConnection.prototype.test = function test() {
this.playFile("C:/users/amish/desktop/audio.mp3").then(function (stream) {
stream.on("time", function (time) {
console.log("Time", time);
});
});
};
VoiceConnection.prototype.playFile = function playFile(stream) {
var _this = this;
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
var self = this;
this.encoder.encodeFile("C:/users/amish/desktop/audio.mp3")["catch"](error).then(function (stream) {
return new Promise(function (resolve, reject) {
_this.encoder.encodeFile(stream)["catch"](error).then(function (stream) {
var intent = self.playRawStream(stream);
intent.on("end", function () {
console.log("stream ended");
});
resolve(intent);
callback(null, intent);
});
function error() {
console.log("ERROR!");
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
reject(e);
callback(e);
}
});
};
VoiceConnection.prototype.init = function init() {
var _this = this;
var _this2 = this;
var self = this;
dns.lookup(this.endpoint, function (err, address, family) {
self.endpoint = address;
var vWS = self.vWS = new WebSocket("wss://" + _this.endpoint, null, { rejectUnauthorized: false });
var vWS = self.vWS = new WebSocket("wss://" + _this2.endpoint, null, { rejectUnauthorized: false });
var udpClient = self.udp = udp.createSocket("udp4");
var firstPacket = true;

View File

@@ -28,6 +28,7 @@ class VoiceConnection {
this.udp = null;
this.playingIntent = null;
this.playing = false;
this.streamTime = 0;
this.init();
}
@@ -51,6 +52,7 @@ class VoiceConnection {
}
var retStream = new StreamIntent();
var onWarning = false;
self.playingIntent = retStream;
function send() {
@@ -66,9 +68,22 @@ class VoiceConnection {
if (!buffer) {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return;
}
if (buffer.length !== 1920) {
if (onWarning) {
retStream.emit("end");
stream.destroy();
self.setSpeaking(false);
return;
} else {
onWarning = true;
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return;
}
}
if (buffer && buffer.length === 1920) {
count++;
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
time + 9600 < 4294967295 ? time += 960 : time = 0;
@@ -77,15 +92,14 @@ class VoiceConnection {
var nextTime = startTime + (count * length);
setTimeout(function () {
send();
}, length + (nextTime - Date.now()));
self.streamTime = count * length;
setTimeout(send, length + (nextTime - Date.now()));
if (!self.playing)
self.setSpeaking(true);
}else{
retStream.emit("end");
self.setSpeaking(false);
}
retStream.emit("time", self.streamTime);
} catch (e) {
retStream.emit("error", e);
@@ -138,22 +152,32 @@ class VoiceConnection {
}
test() {
this.playFile("C:/users/amish/desktop/audio.mp3")
.then(stream => {
stream.on("time", time => {
console.log("Time", time);
})
})
}
playFile(stream, callback = function (err, str) { }) {
var self = this;
return new Promise((resolve, reject) => {
this.encoder
.encodeFile("C:/users/amish/desktop/audio.mp3")
.encodeFile(stream)
.catch(error)
.then(stream => {
var intent = self.playRawStream(stream);
intent.on("end", ()=>{
console.log("stream ended");
});
resolve(intent);
callback(null, intent);
});
function error() {
console.log("ERROR!");
function error(e = true) {
reject(e);
callback(e);
}
})
}
init() {