mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
Tidy up of code
This commit is contained in:
@@ -32,6 +32,7 @@ var VoiceConnection = (function () {
|
|||||||
this.udp = null;
|
this.udp = null;
|
||||||
this.playingIntent = null;
|
this.playingIntent = null;
|
||||||
this.playing = false;
|
this.playing = false;
|
||||||
|
this.streamTime = 0;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ var VoiceConnection = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var retStream = new StreamIntent();
|
var retStream = new StreamIntent();
|
||||||
|
var onWarning = false;
|
||||||
self.playingIntent = retStream;
|
self.playingIntent = retStream;
|
||||||
|
|
||||||
function send() {
|
function send() {
|
||||||
@@ -70,25 +72,36 @@ var VoiceConnection = (function () {
|
|||||||
|
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer && buffer.length === 1920) {
|
if (buffer.length !== 1920) {
|
||||||
count++;
|
if (onWarning) {
|
||||||
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
|
retStream.emit("end");
|
||||||
time + 9600 < 4294967295 ? time += 960 : time = 0;
|
stream.destroy();
|
||||||
|
self.setSpeaking(false);
|
||||||
self.sendBuffer(buffer, sequence, time, function (e) {});
|
return;
|
||||||
|
} else {
|
||||||
var nextTime = startTime + count * length;
|
onWarning = true;
|
||||||
|
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||||
setTimeout(function () {
|
return;
|
||||||
send();
|
}
|
||||||
}, length + (nextTime - Date.now()));
|
|
||||||
if (!self.playing) self.setSpeaking(true);
|
|
||||||
} else {
|
|
||||||
retStream.emit("end");
|
|
||||||
self.setSpeaking(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count++;
|
||||||
|
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
|
||||||
|
time + 9600 < 4294967295 ? time += 960 : time = 0;
|
||||||
|
|
||||||
|
self.sendBuffer(buffer, sequence, time, function (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) {
|
} catch (e) {
|
||||||
retStream.emit("error", e);
|
retStream.emit("error", e);
|
||||||
}
|
}
|
||||||
@@ -140,27 +153,42 @@ var VoiceConnection = (function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
VoiceConnection.prototype.test = function test() {
|
VoiceConnection.prototype.test = function test() {
|
||||||
var self = this;
|
this.playFile("C:/users/amish/desktop/audio.mp3").then(function (stream) {
|
||||||
this.encoder.encodeFile("C:/users/amish/desktop/audio.mp3")["catch"](error).then(function (stream) {
|
stream.on("time", function (time) {
|
||||||
|
console.log("Time", time);
|
||||||
var intent = self.playRawStream(stream);
|
|
||||||
|
|
||||||
intent.on("end", function () {
|
|
||||||
console.log("stream ended");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
function error() {
|
};
|
||||||
console.log("ERROR!");
|
|
||||||
}
|
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;
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
_this.encoder.encodeFile(stream)["catch"](error).then(function (stream) {
|
||||||
|
|
||||||
|
var intent = self.playRawStream(stream);
|
||||||
|
resolve(intent);
|
||||||
|
callback(null, intent);
|
||||||
|
});
|
||||||
|
function error() {
|
||||||
|
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
|
||||||
|
|
||||||
|
reject(e);
|
||||||
|
callback(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
VoiceConnection.prototype.init = function init() {
|
VoiceConnection.prototype.init = function init() {
|
||||||
var _this = this;
|
var _this2 = this;
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
dns.lookup(this.endpoint, function (err, address, family) {
|
dns.lookup(this.endpoint, function (err, address, family) {
|
||||||
self.endpoint = address;
|
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 udpClient = self.udp = udp.createSocket("udp4");
|
||||||
|
|
||||||
var firstPacket = true;
|
var firstPacket = true;
|
||||||
|
|||||||
@@ -28,10 +28,11 @@ class VoiceConnection {
|
|||||||
this.udp = null;
|
this.udp = null;
|
||||||
this.playingIntent = null;
|
this.playingIntent = null;
|
||||||
this.playing = false;
|
this.playing = false;
|
||||||
|
this.streamTime = 0;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
stopPlaying(){
|
stopPlaying() {
|
||||||
this.playingIntent = null;
|
this.playingIntent = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,16 +46,17 @@ class VoiceConnection {
|
|||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
var length = 20;
|
var length = 20;
|
||||||
|
|
||||||
if(self.playingIntent){
|
if (self.playingIntent) {
|
||||||
self.stopPlaying();
|
self.stopPlaying();
|
||||||
}
|
}
|
||||||
|
|
||||||
var retStream = new StreamIntent();
|
var retStream = new StreamIntent();
|
||||||
|
var onWarning = false;
|
||||||
self.playingIntent = retStream;
|
self.playingIntent = retStream;
|
||||||
|
|
||||||
function send() {
|
function send() {
|
||||||
if(self.playingIntent && self.playingIntent !== retStream){
|
if (self.playingIntent && self.playingIntent !== retStream) {
|
||||||
console.log("ending it!");
|
console.log("ending it!");
|
||||||
self.setSpeaking(false);
|
self.setSpeaking(false);
|
||||||
retStream.emit("end");
|
retStream.emit("end");
|
||||||
@@ -63,41 +65,53 @@ class VoiceConnection {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
var buffer = stream.read(1920);
|
var buffer = stream.read(1920);
|
||||||
|
|
||||||
if(!buffer){
|
if (!buffer) {
|
||||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
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) => { });
|
if (buffer.length !== 1920) {
|
||||||
|
if (onWarning) {
|
||||||
var nextTime = startTime + (count * length);
|
retStream.emit("end");
|
||||||
|
stream.destroy();
|
||||||
setTimeout(function () {
|
self.setSpeaking(false);
|
||||||
send();
|
return;
|
||||||
}, length + (nextTime - Date.now()));
|
} else {
|
||||||
if(!self.playing)
|
onWarning = true;
|
||||||
self.setSpeaking(true);
|
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||||
}else{
|
return;
|
||||||
retStream.emit("end");
|
}
|
||||||
self.setSpeaking(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
} catch (e) {
|
||||||
retStream.emit("error", e);
|
retStream.emit("error", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.setSpeaking(true);
|
self.setSpeaking(true);
|
||||||
send();
|
send();
|
||||||
|
|
||||||
return retStream;
|
return retStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
setSpeaking(value){
|
setSpeaking(value) {
|
||||||
this.playing = value;
|
this.playing = value;
|
||||||
this.vWS.send(JSON.stringify({
|
this.vWS.send(JSON.stringify({
|
||||||
op: 5,
|
op: 5,
|
||||||
@@ -138,22 +152,32 @@ class VoiceConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test() {
|
test() {
|
||||||
var self = this;
|
this.playFile("C:/users/amish/desktop/audio.mp3")
|
||||||
this.encoder
|
|
||||||
.encodeFile("C:/users/amish/desktop/audio.mp3")
|
|
||||||
.catch(error)
|
|
||||||
.then(stream => {
|
.then(stream => {
|
||||||
|
stream.on("time", time => {
|
||||||
var intent = self.playRawStream(stream);
|
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(e = true) {
|
||||||
});
|
reject(e);
|
||||||
function error() {
|
callback(e);
|
||||||
console.log("ERROR!");
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
|||||||
Reference in New Issue
Block a user