mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
Optimisations
This commit is contained in:
@@ -12,6 +12,7 @@ var fs = require("fs");
|
||||
var ffmpeg = require('fluent-ffmpeg');
|
||||
var AudioEncoder = require("./AudioEncoder.js");
|
||||
var VoicePacket = require("./VoicePacket.js");
|
||||
var StreamIntent = require("./StreamIntent.js");
|
||||
|
||||
var VoiceConnection = (function () {
|
||||
function VoiceConnection(channel, client, session, token, server, endpoint) {
|
||||
@@ -29,13 +30,18 @@ var VoiceConnection = (function () {
|
||||
this.opus = new Opus.OpusEncoder(48000, 1);
|
||||
this.encoder = new AudioEncoder();
|
||||
this.udp = null;
|
||||
this.playingIntent = null;
|
||||
this.playing = false;
|
||||
this.init();
|
||||
}
|
||||
|
||||
VoiceConnection.prototype.stopPlaying = function stopPlaying() {
|
||||
this.playingIntent = null;
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.playRawStream = function playRawStream(stream) {
|
||||
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
|
||||
var startTime = Date.now();
|
||||
var sequence = 0;
|
||||
@@ -44,32 +50,64 @@ var VoiceConnection = (function () {
|
||||
|
||||
var length = 20;
|
||||
|
||||
function send() {
|
||||
try {
|
||||
count++;
|
||||
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
|
||||
time + 9600 < 4294967295 ? time += 960 : time = 0;
|
||||
|
||||
self.sendBuffer(stream.read(1920), sequence, time, function (e) {
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
var nextTime = startTime + count * length;
|
||||
|
||||
setTimeout(function () {
|
||||
send();
|
||||
}, length + (nextTime - Date.now()));
|
||||
} catch (e) {}
|
||||
if (self.playingIntent) {
|
||||
self.stopPlaying();
|
||||
}
|
||||
|
||||
self.vWS.send(JSON.stringify({
|
||||
var retStream = new StreamIntent();
|
||||
self.playingIntent = retStream;
|
||||
|
||||
function send() {
|
||||
if (self.playingIntent && self.playingIntent !== retStream) {
|
||||
console.log("ending it!");
|
||||
self.setSpeaking(false);
|
||||
retStream.emit("end");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
|
||||
var buffer = stream.read(1920);
|
||||
|
||||
if (!buffer) {
|
||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||
}
|
||||
|
||||
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, function (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);
|
||||
}
|
||||
} catch (e) {
|
||||
retStream.emit("error", e);
|
||||
}
|
||||
}
|
||||
self.setSpeaking(true);
|
||||
send();
|
||||
|
||||
return retStream;
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.setSpeaking = function setSpeaking(value) {
|
||||
this.playing = value;
|
||||
this.vWS.send(JSON.stringify({
|
||||
op: 5,
|
||||
d: {
|
||||
speaking: true,
|
||||
speaking: value,
|
||||
delay: 0
|
||||
}
|
||||
}));
|
||||
send();
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.sendPacket = function sendPacket(packet) {
|
||||
@@ -86,14 +124,13 @@ var VoiceConnection = (function () {
|
||||
}
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.sendBuffer = function sendBuffer(buffer, sequence, timestamp, callback) {
|
||||
VoiceConnection.prototype.sendBuffer = function sendBuffer(rawbuffer, sequence, timestamp, callback) {
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
try {
|
||||
|
||||
var buffer = self.encoder.opusBuffer(buffer);
|
||||
var buffer = self.encoder.opusBuffer(rawbuffer);
|
||||
var packet = new VoicePacket(buffer, sequence, timestamp, self.vWSData.ssrc);
|
||||
|
||||
return self.sendPacket(packet, callback);
|
||||
} catch (e) {
|
||||
self.playing = false;
|
||||
@@ -106,7 +143,11 @@ var VoiceConnection = (function () {
|
||||
var self = this;
|
||||
this.encoder.encodeFile("C:/users/amish/desktop/audio.mp3")["catch"](error).then(function (stream) {
|
||||
|
||||
self.playRawStream(stream);
|
||||
var intent = self.playRawStream(stream);
|
||||
|
||||
intent.on("end", function () {
|
||||
console.log("stream ended");
|
||||
});
|
||||
});
|
||||
function error() {
|
||||
console.log("ERROR!");
|
||||
|
||||
Reference in New Issue
Block a user