@zygout/eris 0.0.0-stage → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +80 -3
- package/esm.mjs +72 -0
- package/index.d.ts +4397 -0
- package/index.js +75 -0
- package/lib/Client.js +4683 -0
- package/lib/Constants.d.ts +988 -0
- package/lib/Constants.js +1161 -0
- package/lib/command/Command.js +552 -0
- package/lib/command/CommandClient.js +460 -0
- package/lib/errors/DiscordHTTPError.js +63 -0
- package/lib/errors/DiscordRESTError.js +77 -0
- package/lib/gateway/Shard.js +3029 -0
- package/lib/gateway/ShardManager.js +164 -0
- package/lib/rest/Endpoints.js +172 -0
- package/lib/rest/RequestHandler.js +459 -0
- package/lib/structures/ApplicationCommand.js +125 -0
- package/lib/structures/AutoModerationRule.js +143 -0
- package/lib/structures/AutocompleteInteraction.js +80 -0
- package/lib/structures/Base.js +81 -0
- package/lib/structures/CategoryChannel.js +77 -0
- package/lib/structures/Channel.js +98 -0
- package/lib/structures/CommandInteraction.js +473 -0
- package/lib/structures/ComponentInteraction.js +538 -0
- package/lib/structures/DMChannel.js +278 -0
- package/lib/structures/Entitlement.js +77 -0
- package/lib/structures/ExtendedUser.js +46 -0
- package/lib/structures/ForumChannel.js +24 -0
- package/lib/structures/GroupChannel.js +106 -0
- package/lib/structures/Guild.js +1677 -0
- package/lib/structures/GuildAuditLogEntry.js +190 -0
- package/lib/structures/GuildChannel.js +128 -0
- package/lib/structures/GuildIntegration.js +99 -0
- package/lib/structures/GuildPreview.js +97 -0
- package/lib/structures/GuildScheduledEvent.js +176 -0
- package/lib/structures/GuildTemplate.js +76 -0
- package/lib/structures/GuildTextableChannel.js +260 -0
- package/lib/structures/Interaction.js +133 -0
- package/lib/structures/Invite.js +143 -0
- package/lib/structures/Lobby.js +108 -0
- package/lib/structures/LobbyMember.js +59 -0
- package/lib/structures/MediaChannel.js +216 -0
- package/lib/structures/Member.js +371 -0
- package/lib/structures/Message.js +708 -0
- package/lib/structures/ModalSubmitInteraction.js +478 -0
- package/lib/structures/NewsChannel.js +34 -0
- package/lib/structures/NewsThreadChannel.js +12 -0
- package/lib/structures/Permission.js +80 -0
- package/lib/structures/PermissionOverwrite.js +32 -0
- package/lib/structures/PingInteraction.js +39 -0
- package/lib/structures/PrivateThreadChannel.js +25 -0
- package/lib/structures/PublicThreadChannel.js +26 -0
- package/lib/structures/Role.js +163 -0
- package/lib/structures/SKU.js +80 -0
- package/lib/structures/SoundboardSound.js +79 -0
- package/lib/structures/StageChannel.js +59 -0
- package/lib/structures/StageInstance.js +55 -0
- package/lib/structures/Subscription.js +69 -0
- package/lib/structures/TextChannel.js +202 -0
- package/lib/structures/ThreadChannel.js +143 -0
- package/lib/structures/ThreadMember.js +59 -0
- package/lib/structures/UnavailableGuild.js +26 -0
- package/lib/structures/UnknownInteraction.js +502 -0
- package/lib/structures/User.js +296 -0
- package/lib/structures/VoiceChannel.js +166 -0
- package/lib/structures/VoiceState.js +84 -0
- package/lib/util/BrowserWebSocket.js +76 -0
- package/lib/util/Bucket.js +89 -0
- package/lib/util/Collection.js +202 -0
- package/lib/util/MultipartData.js +70 -0
- package/lib/util/Opus.js +33 -0
- package/lib/util/SequentialBucket.js +83 -0
- package/lib/util/emitDeprecation.js +26 -0
- package/lib/voice/Piper.js +300 -0
- package/lib/voice/SharedStream.js +238 -0
- package/lib/voice/VoiceConnection.js +1440 -0
- package/lib/voice/VoiceConnectionManager.js +151 -0
- package/lib/voice/VoiceDataStream.js +22 -0
- package/lib/voice/streams/BaseTransformer.js +40 -0
- package/lib/voice/streams/DCAOpusTransformer.js +78 -0
- package/lib/voice/streams/FFmpegDuplex.js +192 -0
- package/lib/voice/streams/FFmpegOggTransformer.js +35 -0
- package/lib/voice/streams/FFmpegPCMTransformer.js +26 -0
- package/lib/voice/streams/OggOpusTransformer.js +108 -0
- package/lib/voice/streams/PCMOpusTransformer.js +61 -0
- package/lib/voice/streams/VolumeTransformer.js +50 -0
- package/lib/voice/streams/WebmOpusTransformer.js +258 -0
- package/package.json +82 -6
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const Base = require("../structures/Base");
|
|
5
|
+
const DCAOpusTransformer = require("./streams/DCAOpusTransformer");
|
|
6
|
+
const FFmpegOggTransformer = require("./streams/FFmpegOggTransformer");
|
|
7
|
+
const FFmpegPCMTransformer = require("./streams/FFmpegPCMTransformer");
|
|
8
|
+
const FS = require("fs");
|
|
9
|
+
const HTTP = require("http");
|
|
10
|
+
const HTTPS = require("https");
|
|
11
|
+
const OggOpusTransformer = require("./streams/OggOpusTransformer");
|
|
12
|
+
const PassThroughStream = require("stream").PassThrough;
|
|
13
|
+
const PCMOpusTransformer = require("./streams/PCMOpusTransformer");
|
|
14
|
+
const Stream = require("stream").Stream;
|
|
15
|
+
const VolumeTransformer = require("./streams/VolumeTransformer");
|
|
16
|
+
const WebmOpusTransformer = require("./streams/WebmOpusTransformer");
|
|
17
|
+
|
|
18
|
+
let EventEmitter;
|
|
19
|
+
try {
|
|
20
|
+
EventEmitter = require("eventemitter3");
|
|
21
|
+
} catch {
|
|
22
|
+
EventEmitter = require("events").EventEmitter;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class Piper extends EventEmitter {
|
|
26
|
+
constructor(converterCommand, opusFactory) {
|
|
27
|
+
super();
|
|
28
|
+
|
|
29
|
+
this.reset();
|
|
30
|
+
|
|
31
|
+
this.converterCommand = converterCommand;
|
|
32
|
+
this._dataPackets = [];
|
|
33
|
+
this._dataPacketMax = 30;
|
|
34
|
+
this._dataPacketMin = 15;
|
|
35
|
+
this.encoding = false;
|
|
36
|
+
this.libopus = true;
|
|
37
|
+
|
|
38
|
+
this.opusFactory = opusFactory;
|
|
39
|
+
this.opus = null;
|
|
40
|
+
|
|
41
|
+
this.volumeLevel = 1;
|
|
42
|
+
|
|
43
|
+
this._retransformer = [];
|
|
44
|
+
|
|
45
|
+
this.addDataPacket = this.addDataPacket.bind(this);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get dataPacketCount() {
|
|
49
|
+
return this._dataPackets.length;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
addDataPacket(packet) {
|
|
53
|
+
if (!this.encoding) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (this._dataPackets.push(packet) < this._dataPacketMax && this._endStream && this._endStream.manualCB) {
|
|
57
|
+
process.nextTick(() => {
|
|
58
|
+
if (this._endStream && this._endStream.manualCB) {
|
|
59
|
+
this._endStream.transformCB();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
encode(source, options) {
|
|
66
|
+
if (this.encoding || this.streams.length) {
|
|
67
|
+
this.emit("error", new Error("Already encoding"));
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (typeof source === "string") {
|
|
72
|
+
if (options.format === "dca" || options.format === "ogg" || options.format === "webm" || options.format === "pcm") {
|
|
73
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
74
|
+
const passThrough = new PassThroughStream();
|
|
75
|
+
if (source.startsWith("http://")) {
|
|
76
|
+
HTTP.get(source, (res) => res.pipe(passThrough)).once("error", (e) => this.stop(e));
|
|
77
|
+
} else {
|
|
78
|
+
HTTPS.get(source, (res) => res.pipe(passThrough)).once("error", (e) => this.stop(e));
|
|
79
|
+
}
|
|
80
|
+
source = passThrough;
|
|
81
|
+
} else {
|
|
82
|
+
try {
|
|
83
|
+
FS.statSync(source);
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (err.code === "ENOENT") {
|
|
86
|
+
this.emit("error", new Error("That file does not exist."));
|
|
87
|
+
} else {
|
|
88
|
+
this.emit("error", new Error("An error occured trying to access that file."));
|
|
89
|
+
}
|
|
90
|
+
this.reset();
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
source = FS.createReadStream(source);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else if (!(source instanceof Stream) || !source.pipe) {
|
|
97
|
+
this.emit("error", new Error("Invalid source type"));
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._dataPacketMax = 30;
|
|
102
|
+
this._dataPacketMin = 15;
|
|
103
|
+
|
|
104
|
+
if (typeof source !== "string") {
|
|
105
|
+
this.streams.push(source.once("error", (e) => this.stop(e)));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// eslint-disable-next-line no-empty
|
|
109
|
+
if (options.format === "opusPackets") {
|
|
110
|
+
} else if (options.format === "dca") {
|
|
111
|
+
this.streams.push(source.pipe(new DCAOpusTransformer()).once("error", (e) => this.stop(e)));
|
|
112
|
+
} else if (options.format === "ogg") {
|
|
113
|
+
this.streams.push(source.pipe(new OggOpusTransformer()).once("error", (e) => this.stop(e)));
|
|
114
|
+
} else if (options.format === "webm") {
|
|
115
|
+
this.streams.push(source.pipe(new WebmOpusTransformer()).once("error", (e) => this.stop(e)));
|
|
116
|
+
} else if (!options.format || options.format === "pcm") {
|
|
117
|
+
if (options.inlineVolume) {
|
|
118
|
+
if (!options.format) {
|
|
119
|
+
if (!this.converterCommand) {
|
|
120
|
+
this.emit("error", new Error("FFmpeg/avconv was not found on this system. Playback of this audio format is impossible"));
|
|
121
|
+
this.reset();
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (typeof source === "string") {
|
|
125
|
+
this.streams.push(source = new FFmpegPCMTransformer({
|
|
126
|
+
command: this.converterCommand,
|
|
127
|
+
input: source,
|
|
128
|
+
encoderArgs: options.encoderArgs,
|
|
129
|
+
inputArgs: options.inputArgs,
|
|
130
|
+
}).once("error", (e) => this.stop(e)));
|
|
131
|
+
} else {
|
|
132
|
+
this.streams.push(source = source.pipe(new FFmpegPCMTransformer({
|
|
133
|
+
command: this.converterCommand,
|
|
134
|
+
encoderArgs: options.encoderArgs,
|
|
135
|
+
inputArgs: options.inputArgs,
|
|
136
|
+
})).once("error", (e) => this.stop(e)));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
this.streams.push(this.volume = source = source.pipe(new VolumeTransformer()).once("error", (e) => this.stop(e)));
|
|
140
|
+
this.volume.setVolume(this.volumeLevel);
|
|
141
|
+
this.streams.push(this.volume.pipe(new PCMOpusTransformer({
|
|
142
|
+
opusFactory: this.opusFactory,
|
|
143
|
+
frameSize: options.frameSize,
|
|
144
|
+
pcmSize: options.pcmSize,
|
|
145
|
+
})).once("error", (e) => this.stop(e)));
|
|
146
|
+
this._dataPacketMax = 1; // Live volume updating
|
|
147
|
+
this._dataPacketMin = 4;
|
|
148
|
+
} else {
|
|
149
|
+
if (this.libopus) {
|
|
150
|
+
if (typeof source === "string") {
|
|
151
|
+
this.streams.push(source = new FFmpegOggTransformer({
|
|
152
|
+
command: this.converterCommand,
|
|
153
|
+
input: source,
|
|
154
|
+
encoderArgs: options.encoderArgs,
|
|
155
|
+
inputArgs: options.inputArgs,
|
|
156
|
+
format: options.format,
|
|
157
|
+
frameDuration: options.frameDuration,
|
|
158
|
+
}).once("error", (e) => this.stop(e)));
|
|
159
|
+
} else {
|
|
160
|
+
this.streams.push(source = source.pipe(new FFmpegOggTransformer({
|
|
161
|
+
command: this.converterCommand,
|
|
162
|
+
encoderArgs: options.encoderArgs,
|
|
163
|
+
inputArgs: options.inputArgs,
|
|
164
|
+
format: options.format,
|
|
165
|
+
frameDuration: options.frameDuration,
|
|
166
|
+
})).once("error", (e) => this.stop(e)));
|
|
167
|
+
}
|
|
168
|
+
this.streams.push(source.pipe(new OggOpusTransformer()).once("error", (e) => this.stop(e)));
|
|
169
|
+
} else {
|
|
170
|
+
if (typeof source === "string") {
|
|
171
|
+
this.streams.push(source = new FFmpegPCMTransformer({
|
|
172
|
+
command: this.converterCommand,
|
|
173
|
+
input: source,
|
|
174
|
+
encoderArgs: options.encoderArgs,
|
|
175
|
+
inputArgs: options.inputArgs,
|
|
176
|
+
}).once("error", (e) => this.stop(e)));
|
|
177
|
+
} else {
|
|
178
|
+
this.streams.push(source = source.pipe(new FFmpegPCMTransformer({
|
|
179
|
+
command: this.converterCommand,
|
|
180
|
+
encoderArgs: options.encoderArgs,
|
|
181
|
+
inputArgs: options.inputArgs,
|
|
182
|
+
})).once("error", (e) => this.stop(e)));
|
|
183
|
+
}
|
|
184
|
+
this.streams.push(source.pipe(new PCMOpusTransformer({
|
|
185
|
+
opusFactory: this.opusFactory,
|
|
186
|
+
frameSize: options.frameSize,
|
|
187
|
+
pcmSize: options.pcmSize,
|
|
188
|
+
})).once("error", (e) => this.stop(e)));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
this.emit("error", new Error("Unrecognized format"));
|
|
193
|
+
this.reset();
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this._endStream = this.streams[this.streams.length - 1];
|
|
198
|
+
if (this._endStream.hasOwnProperty("manualCB")) {
|
|
199
|
+
this._endStream.manualCB = true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this._endStream.on("data", this.addDataPacket);
|
|
203
|
+
this._endStream.once("end", () => this.stop(null, source));
|
|
204
|
+
|
|
205
|
+
this.emit("start");
|
|
206
|
+
|
|
207
|
+
return (this.encoding = true);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
getDataPacket() {
|
|
211
|
+
if (this._dataPackets.length < this._dataPacketMin && this._endStream && this._endStream.manualCB) {
|
|
212
|
+
this._endStream.transformCB();
|
|
213
|
+
}
|
|
214
|
+
if (this._retransformer.length === 0) {
|
|
215
|
+
return this._dataPackets.shift();
|
|
216
|
+
} else {
|
|
217
|
+
// If we don't have an opus instance yet, create one.
|
|
218
|
+
if (!this.opus) {
|
|
219
|
+
this.opus = this.opusFactory();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const packet = this.opus.decode(this._dataPackets.shift());
|
|
223
|
+
for (let i = 0, num; i < packet.length - 1; i += 2) {
|
|
224
|
+
num = ~~(this._retransformer.shift() * packet.readInt16LE(i));
|
|
225
|
+
packet.writeInt16LE(num >= 32767 ? 32767 : num <= -32767 ? -32767 : num, i);
|
|
226
|
+
}
|
|
227
|
+
return this.opus.encode(packet, 3840 / 2 / 2);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
reset() {
|
|
232
|
+
if (this.streams) {
|
|
233
|
+
for (const stream of this.streams) {
|
|
234
|
+
if (typeof stream.destroy === "function") {
|
|
235
|
+
stream.destroy();
|
|
236
|
+
} else {
|
|
237
|
+
stream.unpipe();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
this.streams = [];
|
|
243
|
+
this._endStream = null;
|
|
244
|
+
this.volume = null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
resetPackets() {
|
|
248
|
+
// We no longer need this to convert inline volume, so... let it go.
|
|
249
|
+
if (this.opus) {
|
|
250
|
+
if (this.opus.delete) {
|
|
251
|
+
this.opus.delete();
|
|
252
|
+
}
|
|
253
|
+
this.opus = null;
|
|
254
|
+
}
|
|
255
|
+
this._dataPackets = [];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
setVolume(volume) {
|
|
259
|
+
this.volumeLevel = volume;
|
|
260
|
+
if (!this.volume) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.volume.setVolume(volume);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
stop(e, source) {
|
|
267
|
+
if (source && !this.streams.includes(source)) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (e) {
|
|
272
|
+
this.emit("error", e);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (this.throttleTimeout) {
|
|
276
|
+
clearTimeout(this.throttleTimeout);
|
|
277
|
+
this.throttleTimeout = null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (this.streams.length === 0) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (this._endStream) {
|
|
285
|
+
this._endStream.removeAllListeners("data");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
this.reset();
|
|
289
|
+
if (this.encoding) {
|
|
290
|
+
this.encoding = false;
|
|
291
|
+
this.emit("stop");
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
[util.inspect.custom]() {
|
|
296
|
+
return Base.prototype[util.inspect.custom].call(this);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
module.exports = Piper;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const Base = require("../structures/Base");
|
|
5
|
+
const Piper = require("./Piper");
|
|
6
|
+
const VoiceConnection = require("./VoiceConnection");
|
|
7
|
+
const Collection = require("../util/Collection");
|
|
8
|
+
const { createOpus } = require("../util/Opus");
|
|
9
|
+
|
|
10
|
+
let EventEmitter;
|
|
11
|
+
try {
|
|
12
|
+
EventEmitter = require("eventemitter3");
|
|
13
|
+
} catch {
|
|
14
|
+
EventEmitter = require("events").EventEmitter;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Represents a collection of VoiceConnections sharing an input stream
|
|
19
|
+
* @extends EventEmitter
|
|
20
|
+
* @prop {Object?} current The current stream
|
|
21
|
+
* @prop {Boolean} ended Whether the stream ended
|
|
22
|
+
* @prop {Boolean} playing Whether the voice connection is playing something
|
|
23
|
+
* @prop {Boolean} speaking Whether someone is speaking
|
|
24
|
+
* @prop {Number} volume The current volume level of the connection
|
|
25
|
+
*/
|
|
26
|
+
class SharedStream extends EventEmitter {
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
|
|
30
|
+
this.samplingRate = 48000;
|
|
31
|
+
this.frameDuration = 20;
|
|
32
|
+
this.channels = 2;
|
|
33
|
+
this.bitrate = 64000;
|
|
34
|
+
|
|
35
|
+
this.voiceConnections = new Collection(VoiceConnection);
|
|
36
|
+
|
|
37
|
+
if (!VoiceConnection._converterCommand.cmd) {
|
|
38
|
+
VoiceConnection._converterCommand.pickCommand();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.piper = new Piper(VoiceConnection._converterCommand.cmd, () => createOpus(this.samplingRate, this.channels, this.bitrate));
|
|
42
|
+
/**
|
|
43
|
+
* Fired when the shared stream encounters an error
|
|
44
|
+
* @event SharedStream#error
|
|
45
|
+
* @prop {Error} e The error
|
|
46
|
+
*/
|
|
47
|
+
this.piper.on("error", (e) => this.emit("error", e));
|
|
48
|
+
if (!VoiceConnection._converterCommand.libopus) {
|
|
49
|
+
this.piper.libopus = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.ended = true;
|
|
53
|
+
this.playing = false;
|
|
54
|
+
this.speaking = false;
|
|
55
|
+
|
|
56
|
+
this._send = this._send.bind(this);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get volume() {
|
|
60
|
+
return this.piper.volumeLevel;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Add a voice connection to the shared stream
|
|
65
|
+
* @arg {VoiceConnection} connection The voice connection to add
|
|
66
|
+
*/
|
|
67
|
+
add(connection) {
|
|
68
|
+
const _connection = this.voiceConnections.add(connection);
|
|
69
|
+
if (_connection.ready) {
|
|
70
|
+
_connection.setSpeaking(this.speaking);
|
|
71
|
+
} else {
|
|
72
|
+
_connection.once("ready", () => {
|
|
73
|
+
_connection.setSpeaking(this.speaking);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return _connection;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Play an audio or video resource. If playing from a non-opus resource, FFMPEG should be compiled with --enable-libopus for best performance. If playing from HTTPS, FFMPEG must be compiled with --enable-openssl
|
|
81
|
+
* @arg {ReadableStream | String} resource The audio or video resource, either a ReadableStream, URL, or file path
|
|
82
|
+
* @arg {Object} [options] Music options
|
|
83
|
+
* @arg {Array<String>} [options.encoderArgs] Additional encoder parameters to pass to ffmpeg/avconv (after -i)
|
|
84
|
+
* @arg {String} [options.format] The format of the resource. If null, FFmpeg will attempt to guess and play the format. Available options: "dca", "ogg", "webm", "pcm", "opusPackets", null
|
|
85
|
+
* @arg {Number} [options.frameDuration=60] The resource opus frame duration (required for DCA/Ogg)
|
|
86
|
+
* @arg {Number} [options.frameSize=2880] The resource opus frame size
|
|
87
|
+
* @arg {Boolean} [options.inlineVolume=false] Whether to enable on-the-fly volume changing. Note that enabling this leads to increased CPU usage
|
|
88
|
+
* @arg {Array<String>} [options.inputArgs] Additional input parameters to pass to ffmpeg/avconv (before -i)
|
|
89
|
+
* @arg {Number} [options.sampleRate=48000] The resource audio sampling rate
|
|
90
|
+
* @arg {Number} [options.voiceDataTimeout=2000] Timeout when waiting for voice data (-1 for no timeout)
|
|
91
|
+
*/
|
|
92
|
+
play(source, options = {}) {
|
|
93
|
+
options.format = options.format || null;
|
|
94
|
+
options.voiceDataTimeout = !isNaN(options.voiceDataTimeout) ? options.voiceDataTimeout : 2000;
|
|
95
|
+
options.inlineVolume = !!options.inlineVolume;
|
|
96
|
+
options.inputArgs = options.inputArgs || [];
|
|
97
|
+
options.encoderArgs = options.encoderArgs || [];
|
|
98
|
+
|
|
99
|
+
options.samplingRate = options.samplingRate || this.samplingRate;
|
|
100
|
+
options.frameDuration = options.frameDuration || this.frameDuration;
|
|
101
|
+
options.frameSize = options.frameSize || options.samplingRate * options.frameDuration / 1000;
|
|
102
|
+
options.pcmSize = options.pcmSize || options.frameSize * 2 * this.channels;
|
|
103
|
+
|
|
104
|
+
if (!this.piper.encode(source, options)) {
|
|
105
|
+
this.emit("error", new Error("Unable to encode source"));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.ended = false;
|
|
110
|
+
this.current = {
|
|
111
|
+
startTime: 0, // later
|
|
112
|
+
playTime: 0,
|
|
113
|
+
pausedTimestamp: 0,
|
|
114
|
+
pausedTime: 0,
|
|
115
|
+
bufferingTicks: 0,
|
|
116
|
+
options: options,
|
|
117
|
+
timeout: null,
|
|
118
|
+
buffer: null,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
this.playing = true;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Fired when the shared stream starts playing a stream
|
|
125
|
+
* @event SharedStream#start
|
|
126
|
+
*/
|
|
127
|
+
this.emit("start");
|
|
128
|
+
|
|
129
|
+
this._send();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Remove a voice connection from the shared stream
|
|
134
|
+
* @arg {VoiceConnection} connection The voice connection to remove
|
|
135
|
+
*/
|
|
136
|
+
remove(connection) {
|
|
137
|
+
return this.voiceConnections.remove(connection);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
setSpeaking(value) {
|
|
141
|
+
if ((value = !!value) != this.speaking) {
|
|
142
|
+
this.speaking = value;
|
|
143
|
+
for (const vc of this.voiceConnections.values()) {
|
|
144
|
+
vc.setSpeaking(value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Sets the volume of this shared stream if inline volume is enabled
|
|
151
|
+
* @arg {Number} volume The volume as a value from 0 (min) to 1 (max)
|
|
152
|
+
*/
|
|
153
|
+
setVolume(volume) {
|
|
154
|
+
this.piper.setVolume(volume);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Stop the bot from sending audio
|
|
159
|
+
*/
|
|
160
|
+
stopPlaying() {
|
|
161
|
+
if (this.ended) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this.ended = true;
|
|
165
|
+
if (this.current && this.current.timeout) {
|
|
166
|
+
clearTimeout(this.current.timeout);
|
|
167
|
+
this.current.timeout = null;
|
|
168
|
+
}
|
|
169
|
+
this.current = null;
|
|
170
|
+
this.piper.stop();
|
|
171
|
+
this.piper.resetPackets();
|
|
172
|
+
|
|
173
|
+
this.setSpeaking(this.playing = false);
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Fired when the shared stream finishes playing a stream
|
|
177
|
+
* @event SharedStream#end
|
|
178
|
+
*/
|
|
179
|
+
this.emit("end");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
_incrementSequences() {
|
|
183
|
+
for (const vc of this.voiceConnections.values()) {
|
|
184
|
+
vc.sequence = (vc.sequence + 1) & 0xFFFF;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
_incrementTimestamps(val) {
|
|
189
|
+
for (const vc of this.voiceConnections.values()) {
|
|
190
|
+
vc.timestamp = (vc.timestamp + val) >>> 0;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
_send() {
|
|
195
|
+
if (!this.piper.encoding && this.piper.dataPacketCount === 0) {
|
|
196
|
+
return this.stopPlaying();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
this._incrementTimestamps(this.current.options.frameSize);
|
|
200
|
+
|
|
201
|
+
this._incrementSequences();
|
|
202
|
+
|
|
203
|
+
if ((this.current.buffer = this.piper.getDataPacket())) {
|
|
204
|
+
if (this.current.startTime === 0) {
|
|
205
|
+
this.current.startTime = Date.now();
|
|
206
|
+
}
|
|
207
|
+
if (this.current.bufferingTicks > 0) {
|
|
208
|
+
this.current.bufferingTicks = 0;
|
|
209
|
+
this.setSpeaking(true);
|
|
210
|
+
}
|
|
211
|
+
} else if (this.current.options.voiceDataTimeout === -1 || this.current.bufferingTicks < this.current.options.voiceDataTimeout / (4 * this.current.options.frameDuration)) { // wait for data
|
|
212
|
+
if (++this.current.bufferingTicks === 1) {
|
|
213
|
+
this.setSpeaking(false);
|
|
214
|
+
} else {
|
|
215
|
+
this.current.pausedTime += 4 * this.current.options.frameDuration;
|
|
216
|
+
this._incrementTimestamps(3 * this.current.options.frameSize);
|
|
217
|
+
this.current.timeout = setTimeout(this._send, 4 * this.current.options.frameDuration);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
} else {
|
|
221
|
+
return this.stopPlaying();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this.voiceConnections.forEach((connection) => {
|
|
225
|
+
if (connection.ready && this.current.buffer) {
|
|
226
|
+
connection._sendAudioFrame(this.current.buffer);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
this.current.playTime += this.current.options.frameDuration;
|
|
230
|
+
this.current.timeout = setTimeout(this._send, Math.max(0, this.current.startTime + this.current.pausedTime + this.current.playTime - Date.now()));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
[util.inspect.custom]() {
|
|
234
|
+
return Base.prototype[util.inspect.custom].call(this);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
module.exports = SharedStream;
|