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

@@ -28,10 +28,11 @@ class VoiceConnection {
this.udp = null;
this.playingIntent = null;
this.playing = false;
this.streamTime = 0;
this.init();
}
stopPlaying(){
stopPlaying() {
this.playingIntent = null;
}
@@ -45,16 +46,17 @@ class VoiceConnection {
var count = 0;
var length = 20;
if(self.playingIntent){
if (self.playingIntent) {
self.stopPlaying();
}
var retStream = new StreamIntent();
var onWarning = false;
self.playingIntent = retStream;
function send() {
if(self.playingIntent && self.playingIntent !== retStream){
if (self.playingIntent && self.playingIntent !== retStream) {
console.log("ending it!");
self.setSpeaking(false);
retStream.emit("end");
@@ -63,41 +65,53 @@ class VoiceConnection {
try {
var buffer = stream.read(1920);
if(!buffer){
if (!buffer) {
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;
self.sendBuffer(buffer, sequence, time, (e) => { });
var nextTime = startTime + (count * length);
setTimeout(function () {
send();
}, length + (nextTime - Date.now()));
if(!self.playing)
self.setSpeaking(true);
}else{
retStream.emit("end");
self.setSpeaking(false);
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;
}
}
count++;
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
time + 9600 < 4294967295 ? time += 960 : time = 0;
self.sendBuffer(buffer, sequence, time, (e) => { });
var nextTime = startTime + (count * length);
self.streamTime = count * length;
setTimeout(send, length + (nextTime - Date.now()));
if (!self.playing)
self.setSpeaking(true);
retStream.emit("time", self.streamTime);
} catch (e) {
retStream.emit("error", e);
}
}
self.setSpeaking(true);
send();
return retStream;
}
setSpeaking(value){
setSpeaking(value) {
this.playing = value;
this.vWS.send(JSON.stringify({
op: 5,
@@ -138,22 +152,32 @@ class VoiceConnection {
}
test() {
var self = this;
this.encoder
.encodeFile("C:/users/amish/desktop/audio.mp3")
.catch(error)
this.playFile("C:/users/amish/desktop/audio.mp3")
.then(stream => {
var intent = self.playRawStream(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(stream)
.catch(error)
.then(stream => {
var intent = self.playRawStream(stream);
resolve(intent);
callback(null, intent);
intent.on("end", ()=>{
console.log("stream ended");
});
});
function error() {
console.log("ERROR!");
}
function error(e = true) {
reject(e);
callback(e);
}
})
}
init() {