-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdj.js
232 lines (196 loc) · 6.06 KB
/
dj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"use strict";
const EventEmitter = require('events').EventEmitter;
const stream = require('stream');
const Playable = require('./interfaces/Playable.js');
const Constants = require('./Constants.js');
const VoiceUDPSocket = require('./ws/VoiceUDPSocket.js');
const AudioHelper = require('./audio/AudioHelper.js');
const Utils = require('./Utils.js');
var Promise = require("bluebird");
Promise.longStackTraces();
/**
* Represents a voice connection.
* @class
* @extends EventEmitter
*/
class DJ extends EventEmitter {
/** @private */
constructor(discord, guildId, channelId) {
super();
this._discord = discord; // Discord instance
this._guildId = guildId; // Guild ID
this._channelId = channelId; // Voice Channel ID
this._socket = null; // VoiceSocket instance
this._token = null; // Token
this._endpoint = null; // Endpoint URL
this._playableStream = null; // Loaded Playable stream
this._audioTimer = null; // Audio clock
this._state = 0; // 0: Stopped - 1: Paused - 2: Playing
this._currentFrame = 0; // Current Frame
this._audioOptions = {
frameduration: 60,
samplerate: 48000,
bitdepth: 16,
channels: 2
}; // Audio Options
this._audioOptions['samplecount'] = AudioHelper.getSampleCount(this._audioOptions);
this._audioOptions['readsize'] = AudioHelper.getReadSize(this._audioOptions['samplecount'], this._audioOptions);
/**
* Custom Data
* @member {?object}
*/
this.data = null;
this.on('update-connection', function(token, endpoint) {
this._token = token;
this._endpoint = endpoint;
// Reconnect to the new endpoint
if(this._socket != null && this._socket.url != endpoint) {
this.disconnect();
this.connect();
}
}.bind(this));
}
/**
* Guild ID
* @type {string}
* @readonly
*/
get guildId() {
return this._guildId;
}
/**
* Voice Channel ID
* @type {string}
* @readonly
*/
get channelId() {
return this._channelId;
}
/**
* Currently loaded audio stream
* @type {Stream}
* @readonly
*/
get audioStream() {
return this._playableStream;
}
/**
* Whether it's playing
* @type {boolean}
* @readonly
*/
get playing() {
return this._playableInterval != null;
}
/**
* Whether it's connected to the voice channel
* @type {boolean}
* @readonly
*/
get connected() {
return this._socket != null;
}
/**
* Connects to the voice channel
* @return {Promise}
*/
connect() {
if(this.connected) return Promise.reject('Already connected!');
return new Promise(function(resolve, reject) {
let createSocket = function(token, endpoint) {
let socket = new VoiceUDPSocket(this, endpoint, token, Constants.VOICE_ENCRYPTION_SODIUM);
socket.once('open', function() {
this._socket = socket;
resolve();
});
}.bind(this);
if(this._token == null || this._endpoint == null) {
this._discord._socket.connectVoice(this.guildId, this.channelId);
this.once('update-connection', createSocket);
} else {
createSocket(this._token, this._endpoint);
}
}.bind(this));
}
/**
* Disconnects from the voice channel
* @return {Promise}
*/
disconnect() {
if(!this.connected) return Promise.reject('Already disconnected!');
this._socket.terminate();
this._socket = null;
return Promise.resolve();
}
/**
* Starts buffering a playable
* @param {Playable} playable
* @return {Promise<Stream>}
*/
buffer(playable) {
return AudioHelper.buffer(this._discord, playable, this._audioOptions);
}
/**
* Loads a Playable or an Opus stream
* @param {(Playable|Stream)} p
* @return {Promise}
*/
load(p) {
if(p instanceof Playable) {
return this.buffer(p).then(function(stream) {
this._playableStream = stream;
}.bind(this));
} else if(Utils.isStream(p)) {
this._playableStream = p;
return Promise.resolve();
} else {
return Promise.reject('Invalid Playable or stream');
}
}
/** @private */
_processAudio() {
if(this._state != 2) return;
const nextTime = (this._currentFrame + 1) * this._audioOptions['frameduration'] + this.startTime;
const currentTime = Utils.hrtime();
if(currentTime < nextTime) {
const timeleft = Math.round(nextTime - currentTime);
if(this._audioTimer) clearTimeout(this._audioTimer);
this._audioTimer = setTimeout(this._processAudio.bind(this), Math.max(timeleft, 0));
return;
}
this._currentFrame++;
let buffer = this._playableStream.read(this._audioOptions['readsize']);
if(buffer == null) {
return; // No audio to be read.
} else if(!Buffer.isBuffer(buffer)) {
buffer = Buffer.from(buffer);
}
this._socket.sendAudio(buffer, this._audioOptions['samplecount']);
return setImmediate(this._processAudio.bind(this));
}
/**
* Starts playing or resume the loaded playable
*/
play() {
this._state = 2;
this._currentFrame = 0;
setImmediate(this._processAudio.bind(this));
}
/**
* Stops playing the loaded playable
*/
stop() {
this._state = 0;
clearTimeout(this._audioTimer);
this._playableInterval = null;
this._playableStream = null;
}
/**
* Pauses the loaded playable
*/
pause() {
this._state = 1;
clearTimeout(this._audioTimer);
this._playableInterval = null;
}
}