@zygout/eris 0.19.0 → 0.19.2
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 -20
- package/README.md +71 -80
- package/esm.mjs +72 -72
- package/index.d.ts +4407 -4397
- package/index.js +75 -75
- package/lib/Client.js +4708 -4683
- package/lib/Constants.d.ts +990 -988
- package/lib/Constants.js +1163 -1161
- package/lib/command/Command.js +552 -552
- package/lib/command/CommandClient.js +460 -460
- package/lib/errors/DiscordHTTPError.js +63 -63
- package/lib/errors/DiscordRESTError.js +77 -77
- package/lib/gateway/Shard.js +3029 -3029
- package/lib/gateway/ShardManager.js +164 -164
- package/lib/rest/Endpoints.js +172 -172
- package/lib/rest/RequestHandler.js +459 -459
- package/lib/structures/ApplicationCommand.js +125 -125
- package/lib/structures/AutoModerationRule.js +143 -143
- package/lib/structures/AutocompleteInteraction.js +80 -80
- package/lib/structures/Base.js +81 -81
- package/lib/structures/CategoryChannel.js +77 -77
- package/lib/structures/Channel.js +98 -98
- package/lib/structures/CommandInteraction.js +473 -473
- package/lib/structures/ComponentInteraction.js +538 -538
- package/lib/structures/DMChannel.js +278 -278
- package/lib/structures/Entitlement.js +77 -77
- package/lib/structures/ExtendedUser.js +46 -46
- package/lib/structures/ForumChannel.js +24 -24
- package/lib/structures/GroupChannel.js +106 -106
- package/lib/structures/Guild.js +1677 -1677
- package/lib/structures/GuildAuditLogEntry.js +190 -190
- package/lib/structures/GuildChannel.js +128 -128
- package/lib/structures/GuildIntegration.js +99 -99
- package/lib/structures/GuildPreview.js +97 -97
- package/lib/structures/GuildScheduledEvent.js +176 -176
- package/lib/structures/GuildTemplate.js +76 -76
- package/lib/structures/GuildTextableChannel.js +260 -260
- package/lib/structures/Interaction.js +133 -133
- package/lib/structures/Invite.js +143 -143
- package/lib/structures/Lobby.js +108 -108
- package/lib/structures/LobbyMember.js +59 -59
- package/lib/structures/MediaChannel.js +216 -216
- package/lib/structures/Member.js +371 -371
- package/lib/structures/Message.js +708 -708
- package/lib/structures/ModalSubmitInteraction.js +478 -478
- package/lib/structures/NewsChannel.js +34 -34
- package/lib/structures/NewsThreadChannel.js +12 -12
- package/lib/structures/Permission.js +80 -80
- package/lib/structures/PermissionOverwrite.js +32 -32
- package/lib/structures/PingInteraction.js +39 -39
- package/lib/structures/PrivateThreadChannel.js +25 -25
- package/lib/structures/PublicThreadChannel.js +26 -26
- package/lib/structures/Role.js +163 -163
- package/lib/structures/SKU.js +80 -80
- package/lib/structures/SoundboardSound.js +79 -79
- package/lib/structures/StageChannel.js +59 -59
- package/lib/structures/StageInstance.js +55 -55
- package/lib/structures/Subscription.js +69 -69
- package/lib/structures/TextChannel.js +202 -202
- package/lib/structures/ThreadChannel.js +143 -143
- package/lib/structures/ThreadMember.js +59 -59
- package/lib/structures/UnavailableGuild.js +26 -26
- package/lib/structures/UnknownInteraction.js +502 -502
- package/lib/structures/User.js +296 -296
- package/lib/structures/VoiceChannel.js +166 -166
- package/lib/structures/VoiceState.js +84 -84
- package/lib/util/BrowserWebSocket.js +76 -76
- package/lib/util/Bucket.js +89 -89
- package/lib/util/Collection.js +202 -202
- package/lib/util/MultipartData.js +70 -70
- package/lib/util/Opus.js +33 -33
- package/lib/util/SequentialBucket.js +83 -83
- package/lib/util/emitDeprecation.js +26 -26
- package/lib/voice/Piper.js +300 -300
- package/lib/voice/SharedStream.js +238 -238
- package/lib/voice/VoiceConnection.js +1440 -1440
- package/lib/voice/VoiceConnectionManager.js +151 -151
- package/lib/voice/VoiceDataStream.js +22 -22
- package/lib/voice/streams/BaseTransformer.js +40 -40
- package/lib/voice/streams/DCAOpusTransformer.js +78 -78
- package/lib/voice/streams/FFmpegDuplex.js +192 -192
- package/lib/voice/streams/FFmpegOggTransformer.js +35 -35
- package/lib/voice/streams/FFmpegPCMTransformer.js +26 -26
- package/lib/voice/streams/OggOpusTransformer.js +108 -108
- package/lib/voice/streams/PCMOpusTransformer.js +61 -61
- package/lib/voice/streams/VolumeTransformer.js +50 -50
- package/lib/voice/streams/WebmOpusTransformer.js +258 -258
- package/package.json +83 -82
package/lib/voice/Piper.js
CHANGED
|
@@ -1,300 +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;
|
|
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;
|