@kattowo/distube 6.0.2 → 6.2.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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as discord_js from 'discord.js';
2
- import { Snowflake, Message, GuildTextBasedChannel, VoiceBasedChannel, VoiceState, Guild, GuildMember, Interaction, Collection, Client, ClientOptions } from 'discord.js';
2
+ import { VoiceBasedChannel, Snowflake, Message, GuildTextBasedChannel, VoiceState, Guild, GuildMember, Interaction, Collection, Client, ClientOptions } from 'discord.js';
3
3
  import { AudioResource, AudioPlayer, VoiceConnection } from '@discordjs/voice';
4
4
  import { TypedEmitter } from 'tiny-typed-emitter';
5
5
  import { ChildProcess } from 'node:child_process';
@@ -83,7 +83,51 @@ declare enum Events {
83
83
  /**
84
84
  * Emitted to provide debug information from DisTube's operation.
85
85
  */
86
- DEBUG = "debug"
86
+ DEBUG = "debug",
87
+ /**
88
+ * Emitted when a guild stream is paused.
89
+ */
90
+ PAUSE_SONG = "pauseSong",
91
+ /**
92
+ * Emitted when a paused guild stream is resumed.
93
+ */
94
+ RESUME_SONG = "resumeSong",
95
+ /**
96
+ * Emitted when a guild stream is seeked to a new position.
97
+ */
98
+ SEEK_SONG = "seekSong",
99
+ /**
100
+ * Emitted when the volume of a guild stream is changed.
101
+ */
102
+ SET_VOLUME = "setVolume",
103
+ /**
104
+ * Emitted after the songs of a {@link Queue} have been shuffled.
105
+ */
106
+ SHUFFLE_QUEUE = "shuffleQueue",
107
+ /**
108
+ * Emitted when a song is skipped.
109
+ */
110
+ SKIP_SONG = "skipSong",
111
+ /**
112
+ * Emitted when the audio filters of a {@link Queue} are changed.
113
+ */
114
+ CHANGE_FILTER = "changeFilter",
115
+ /**
116
+ * Emitted when the bot is moved to another voice channel.
117
+ */
118
+ VOICE_CHANNEL_CHANGE = "voiceChannelChange",
119
+ /**
120
+ * Emitted when DisTube starts pre-buffering / pre-fetching the next song in a {@link Queue}.
121
+ */
122
+ PRELOAD_SONG = "preloadSong",
123
+ /**
124
+ * Emitted when the next song in a {@link Queue} has been pre-buffered and is ready.
125
+ */
126
+ PRELOADED_SONG = "preloadedSong",
127
+ /**
128
+ * Emitted when pre-buffering the next song in a {@link Queue} fails.
129
+ */
130
+ PRELOAD_ERROR = "preloadError"
87
131
  }
88
132
  /**
89
133
  * The arguments of every {@link Events} event.
@@ -146,6 +190,50 @@ type DisTubeEvents = {
146
190
  * Emitted when a song starts playing.
147
191
  */
148
192
  [Events.PLAY_SONG]: [queue: Queue, song: Song];
193
+ /**
194
+ * Emitted when a guild stream is paused.
195
+ */
196
+ [Events.PAUSE_SONG]: [queue: Queue, song: Song];
197
+ /**
198
+ * Emitted when a paused guild stream is resumed.
199
+ */
200
+ [Events.RESUME_SONG]: [queue: Queue, song: Song];
201
+ /**
202
+ * Emitted when a guild stream is seeked to a new position.
203
+ */
204
+ [Events.SEEK_SONG]: [queue: Queue, song: Song, position: number];
205
+ /**
206
+ * Emitted when the volume of a guild stream is changed.
207
+ */
208
+ [Events.SET_VOLUME]: [queue: Queue, volume: number, oldVolume: number];
209
+ /**
210
+ * Emitted after the songs of a {@link Queue} have been shuffled.
211
+ */
212
+ [Events.SHUFFLE_QUEUE]: [queue: Queue, songs: Song[]];
213
+ /**
214
+ * Emitted when a song is skipped.
215
+ */
216
+ [Events.SKIP_SONG]: [queue: Queue, song: Song];
217
+ /**
218
+ * Emitted when the audio filters of a {@link Queue} are changed.
219
+ */
220
+ [Events.CHANGE_FILTER]: [queue: Queue, newFilters: string[], oldFilters: string[]];
221
+ /**
222
+ * Emitted when the bot is moved to another voice channel.
223
+ */
224
+ [Events.VOICE_CHANNEL_CHANGE]: [queue: Queue, oldChannel: VoiceBasedChannel, newChannel: VoiceBasedChannel];
225
+ /**
226
+ * Emitted when DisTube starts pre-buffering / pre-fetching the next song in a {@link Queue}.
227
+ */
228
+ [Events.PRELOAD_SONG]: [queue: Queue, song: Song];
229
+ /**
230
+ * Emitted when the next song in a {@link Queue} has been pre-buffered and is ready.
231
+ */
232
+ [Events.PRELOADED_SONG]: [queue: Queue, song: Song];
233
+ /**
234
+ * Emitted when pre-buffering the next song in a {@link Queue} fails.
235
+ */
236
+ [Events.PRELOAD_ERROR]: [queue: Queue, song: Song, error: Error];
149
237
  };
150
238
  /**
151
239
  * The typed event listeners of a {@link DisTube} instance.
@@ -174,6 +262,12 @@ type DisTubeVoiceEvents = {
174
262
  * Emitted when the audio player becomes idle, i.e. the song is finished.
175
263
  */
176
264
  finish: () => Awaitable;
265
+ /**
266
+ * Emitted when the connection moves to another voice channel.
267
+ * @param oldChannel - The previous voice channel
268
+ * @param newChannel - The new voice channel
269
+ */
270
+ channelChange: (oldChannel: VoiceBasedChannel, newChannel: VoiceBasedChannel) => Awaitable;
177
271
  };
178
272
  /**
179
273
  * An FFmpeg audio filter object
@@ -213,6 +307,43 @@ type FilterResolvable = string | Filter;
213
307
  * @see {@link defaultFilters}
214
308
  */
215
309
  type Filters = Record<string, string>;
310
+ /**
311
+ * The validated {@link DisTubeOptions.preload} options of a {@link DisTube} instance.
312
+ */
313
+ type PreloadOptions = {
314
+ /**
315
+ * Whether or not preloading is enabled.
316
+ */
317
+ enabled: boolean;
318
+ /**
319
+ * The number of seconds remaining in the currently playing song before
320
+ * preloading starts.
321
+ */
322
+ threshold: number;
323
+ };
324
+ /**
325
+ * The validated {@link DisTubeOptions.crossfade} options of a {@link DisTube}
326
+ * instance.
327
+ */
328
+ type CrossfadeOptions = {
329
+ /**
330
+ * The default crossfade duration (in seconds) applied to new queues. `0`
331
+ * disables the crossfade. A {@link Queue} can override it at runtime through
332
+ * its `crossfade` property.
333
+ */
334
+ duration: number;
335
+ /**
336
+ * The FFmpeg `acrossfade` fade curve applied to both sides of the transition.
337
+ * @default "qsin" (equal-power)
338
+ */
339
+ curve: string;
340
+ /**
341
+ * How long (in seconds) before the fade window begins the merged transition
342
+ * stream is spawned, so its buffers are warm and the fade starts on time.
343
+ * @default 5
344
+ */
345
+ warmup: number;
346
+ };
216
347
  /**
217
348
  * DisTube options
218
349
  */
@@ -276,6 +407,62 @@ type DisTubeOptions = {
276
407
  * @default 10000
277
408
  */
278
409
  stuckThreshold?: number;
410
+ /**
411
+ * Pre-fetch stream information and prepare the stream for the next song in
412
+ * the queue before the current song finishes playing, to reduce the delay
413
+ * between two songs.
414
+ *
415
+ * Can be a simple `boolean` to toggle it with the default threshold, or an
416
+ * object to configure it:
417
+ * - `enabled`: whether or not preloading is enabled (@default false)
418
+ * - `threshold`: the number of seconds remaining in the currently playing
419
+ * song before preloading starts (@default 15)
420
+ * @default false
421
+ */
422
+ preload?: boolean | {
423
+ /**
424
+ * Whether or not preloading is enabled.
425
+ */
426
+ enabled?: boolean;
427
+ /**
428
+ * The number of seconds remaining in the currently playing song before
429
+ * preloading starts.
430
+ */
431
+ threshold?: number;
432
+ };
433
+ /**
434
+ * Crossfade the end of the current song with the beginning of the next one,
435
+ * instead of a hard switch between two streams.
436
+ *
437
+ * The transition is a single FFmpeg process that mixes both songs with an
438
+ * `acrossfade` filter, so the fade is seamless (no gaps, no extra decode
439
+ * round-trips). Because the fade length must be prepared in advance, the
440
+ * next song's stream is resolved even when preloading is disabled.
441
+ *
442
+ * Can be an object to configure it:
443
+ * - `duration`: the default crossfade duration in seconds for new queues
444
+ * (@default 0). Each {@link Queue} can override it at runtime with its
445
+ * `crossfade` property, which is what bots typically bind to a guild
446
+ * setting.
447
+ * - `curve`: the FFmpeg `acrossfade` fade curve (@default "qsin")
448
+ * - `warmup`: seconds before the fade window when the transition stream is
449
+ * spawned to warm up its buffers (@default 5)
450
+ * @default { duration: 0, curve: "qsin", warmup: 5 }
451
+ */
452
+ crossfade?: {
453
+ /**
454
+ * The default crossfade duration (in seconds) for new queues (0 disables).
455
+ */
456
+ duration?: number;
457
+ /**
458
+ * The FFmpeg `acrossfade` fade curve applied to both songs.
459
+ */
460
+ curve?: string;
461
+ /**
462
+ * Seconds before the fade window when the transition stream is spawned.
463
+ */
464
+ warmup?: number;
465
+ };
279
466
  /**
280
467
  * FFmpeg options
281
468
  */
@@ -714,6 +901,36 @@ declare const MAX_REDIRECT_DEPTH = 5;
714
901
  * Default DisTube audio filters.
715
902
  */
716
903
  declare const defaultFilters: Filters;
904
+ /**
905
+ * Default number of seconds remaining in the currently playing song before preloading starts.
906
+ */
907
+ declare const DEFAULT_PRELOAD_THRESHOLD_SECS = 15;
908
+ /**
909
+ * Default crossfade duration in seconds for a {@link Queue} (0 disables the crossfade).
910
+ */
911
+ declare const DEFAULT_CROSSFADE_DURATION_SECS = 0;
912
+ /**
913
+ * Maximum crossfade duration in seconds a {@link Queue} may request.
914
+ */
915
+ declare const MAX_CROSSFADE_DURATION_SECS = 12;
916
+ /**
917
+ * Default FFmpeg `acrossfade` fade curve of the crossfade transition.
918
+ *
919
+ * `qsin` is an equal-power curve: the two songs mix without the -3dB dip of a
920
+ * linear (triangular) curve.
921
+ */
922
+ declare const DEFAULT_CROSSFADE_CURVE = "qsin";
923
+ /**
924
+ * Default number of seconds before the fade window when the merged crossfade
925
+ * transition stream is spawned to warm up its buffers.
926
+ */
927
+ declare const DEFAULT_CROSSFADE_WARMUP_SECS = 5;
928
+ /**
929
+ * How far (in seconds) the playback position may drift from the expected
930
+ * crossfade boundary before the transition is aborted and the regular
931
+ * song-switch takes over.
932
+ */
933
+ declare const CROSSFADE_DRIFT_TOLERANCE_SECS = 0.75;
717
934
  /**
718
935
  * The default options of a {@link DisTube} instance.
719
936
  *
@@ -730,6 +947,12 @@ declare const defaultOptions: {
730
947
  joinNewVoiceChannel: true;
731
948
  unhandledError: "log";
732
949
  stuckThreshold: number;
950
+ preload: false;
951
+ crossfade: {
952
+ duration: number;
953
+ curve: string;
954
+ warmup: number;
955
+ };
733
956
  };
734
957
 
735
958
  /**
@@ -1112,6 +1335,16 @@ declare class Options {
1112
1335
  * @default 10000
1113
1336
  */
1114
1337
  stuckThreshold: number;
1338
+ /**
1339
+ * The preload options of the DisTube instance.
1340
+ * @default { enabled: false, threshold: 15 }
1341
+ */
1342
+ preload: PreloadOptions;
1343
+ /**
1344
+ * The crossfade options of the DisTube instance.
1345
+ * @default { duration: 0, curve: "qsin", warmup: 5 }
1346
+ */
1347
+ crossfade: CrossfadeOptions;
1115
1348
  /**
1116
1349
  * The FFmpeg options of the DisTube instance.
1117
1350
  */
@@ -1138,6 +1371,43 @@ interface StreamOptions {
1138
1371
  * @default 0
1139
1372
  */
1140
1373
  seek?: number;
1374
+ /**
1375
+ * Crossfade transition options.
1376
+ *
1377
+ * When set, the stream mixes the primary URL (the song that is fading out,
1378
+ * seeked to its final `duration` seconds) and the secondary URL (the song
1379
+ * that is fading in) with an FFmpeg `acrossfade` filter graph, so the two
1380
+ * songs crossfade seamlessly in a single FFmpeg process.
1381
+ */
1382
+ crossfade?: CrossfadeStreamOptions;
1383
+ }
1384
+ /**
1385
+ * Options describing a crossfade transition stream
1386
+ */
1387
+ interface CrossfadeStreamOptions {
1388
+ /**
1389
+ * The fade duration in seconds.
1390
+ */
1391
+ duration: number;
1392
+ /**
1393
+ * The initial seek of the first (fading out) input, in seconds. It must be
1394
+ * the position where the fade window begins (`song duration - duration`).
1395
+ */
1396
+ primarySeek: number;
1397
+ /**
1398
+ * The stream URL of the second (fading in) input.
1399
+ */
1400
+ secondaryUrl: string;
1401
+ /**
1402
+ * The FFmpeg `acrossfade` fade curve of the first (fading out) input.
1403
+ * @default "qsin"
1404
+ */
1405
+ outCurve?: string;
1406
+ /**
1407
+ * The FFmpeg `acrossfade` fade curve of the second (fading in) input.
1408
+ * @default "qsin"
1409
+ */
1410
+ inCurve?: string;
1141
1411
  }
1142
1412
  /**
1143
1413
  * Check that FFmpeg is installed and can be spawned.
@@ -1477,6 +1747,26 @@ declare class QueueManager extends GuildIdManager<Queue> {
1477
1747
  playSong(queue: Queue, emitPlaySong?: boolean, options?: {
1478
1748
  keepSongOnError?: boolean;
1479
1749
  }): Promise<void>;
1750
+ /**
1751
+ * Schedule preloading the next song in the queue before the current song finishes.
1752
+ * @param queue - The guild queue
1753
+ */
1754
+ schedulePreload(queue: Queue): void;
1755
+ /**
1756
+ * Preload the next song in the queue.
1757
+ * @param queue - The guild queue
1758
+ */
1759
+ preloadNextSong(queue: Queue): Promise<void>;
1760
+ /**
1761
+ * Schedule the crossfade into the next song: a single FFmpeg process mixes
1762
+ * the current song (seeked to its final fade window) with the next song, and
1763
+ * replaces the playback stream exactly when the fade window begins.
1764
+ *
1765
+ * Canceled automatically whenever the preloaded state is invalidated
1766
+ * (pause, seek, stop...), as both schedulers share the same generation.
1767
+ * @param queue - The guild queue
1768
+ */
1769
+ scheduleCrossfade(queue: Queue): void;
1480
1770
  }
1481
1771
 
1482
1772
  /**
@@ -2224,6 +2514,53 @@ declare class Queue extends DisTubeBase {
2224
2514
  * @internal
2225
2515
  */
2226
2516
  _listeners?: DisTubeVoiceEvents;
2517
+ /**
2518
+ * Preload timer
2519
+ * @internal
2520
+ */
2521
+ _preloadTimeout?: NodeJS.Timeout;
2522
+ /**
2523
+ * Preloaded song
2524
+ * @internal
2525
+ */
2526
+ _preloadedSong?: Song;
2527
+ /**
2528
+ * Preloaded stream
2529
+ * @internal
2530
+ */
2531
+ _preloadedStream?: DisTubeStream;
2532
+ /**
2533
+ * In-flight preload task
2534
+ * @internal
2535
+ */
2536
+ _preloading?: Promise<void>;
2537
+ /**
2538
+ * Bumped whenever the preloaded state is invalidated, so an in-flight
2539
+ * preload task can detect that its result is no longer needed.
2540
+ * @internal
2541
+ */
2542
+ _preloadGeneration: number;
2543
+ /**
2544
+ * Crossfade boundary timer
2545
+ * @internal
2546
+ */
2547
+ _crossfadeTimeout?: NodeJS.Timeout;
2548
+ /**
2549
+ * The merged transition stream that is warming up before the fade window
2550
+ * starts
2551
+ * @internal
2552
+ */
2553
+ _crossfadeWarmup?: DisTubeStream;
2554
+ /**
2555
+ * Clear any preloaded song and cancel pending preload timer.
2556
+ * @internal
2557
+ */
2558
+ _clearPreload(): void;
2559
+ /**
2560
+ * Cancel a pending crossfade and kill its warming-up transition stream.
2561
+ * @internal
2562
+ */
2563
+ _crossfadeCancel(): void;
2227
2564
  /**
2228
2565
  * Create a queue for the guild
2229
2566
  * @param distube - DisTube
@@ -2258,6 +2595,17 @@ declare class Queue extends DisTubeBase {
2258
2595
  * What time in the song is playing (in seconds).
2259
2596
  */
2260
2597
  get currentTime(): number;
2598
+ /**
2599
+ * The crossfade duration (in seconds) between this song and the next one.
2600
+ *
2601
+ * `0` (the default) disables the crossfade. Changing it takes effect for the
2602
+ * next transition; call {@link Queue#scheduleCrossfade} to re-arm the
2603
+ * transition for the currently playing song immediately.
2604
+ * @throws {@link DisTubeError} When the value is not a number between `0`
2605
+ * and the maximum crossfade duration
2606
+ */
2607
+ get crossfade(): number;
2608
+ set crossfade(value: number);
2261
2609
  /**
2262
2610
  * Formatted {@link Queue#currentTime} string.
2263
2611
  */
@@ -2416,6 +2764,15 @@ declare class Queue extends DisTubeBase {
2416
2764
  * @throws {@link DisTubeError} When the queue has been stopped
2417
2765
  */
2418
2766
  play(emitPlaySong?: boolean): Promise<void>;
2767
+ /**
2768
+ * Re-arm the crossfade into the next song for the currently playing one.
2769
+ *
2770
+ * Useful after changing {@link Queue#crossfade} while a song is playing: the
2771
+ * fade may still fit into the remaining time. This is called automatically
2772
+ * after every song switch.
2773
+ * @returns Nothing
2774
+ */
2775
+ scheduleCrossfade(): void;
2419
2776
  /**
2420
2777
  * Restart the current stream with the current FFmpeg arguments, keeping the
2421
2778
  * playback position.
@@ -3256,5 +3613,5 @@ declare function handleUnhandledError(error: Error, behavior?: UnhandledErrorBeh
3256
3613
  */
3257
3614
  declare const checkEncryptionLibraries: () => Promise<boolean>;
3258
3615
 
3259
- export { AUDIO_CHANNELS, AUDIO_SAMPLE_RATE, BaseManager, DEFAULT_VOLUME, DisTube, DisTubeBase, DisTubeError, DisTubeHandler, DisTubeStream, DisTubeVoice, DisTubeVoiceManager, Events, ExtractorPlugin, FilterManager, GuildIdManager, HTTP_REDIRECT_CODES, InfoExtractorPlugin, JOIN_TIMEOUT_MS, MAX_REDIRECT_DEPTH, Options, PlayableExtractorPlugin, Playlist, Plugin, PluginError, PluginErrorCode, PluginType, Queue, QueueManager, RECONNECT_MAX_ATTEMPTS, RECONNECT_TIMEOUT_MS, RepeatMode, STUCK_THRESHOLD_MS, SearchResult, SearchResultType, Song, StreamServer, TaskQueue, checkEncryptionLibraries, checkFFmpeg, checkIntents, checkInvalidKey, DisTube as default, defaultFilters, defaultOptions, formatDuration, handleUnhandledError, isClientInstance, isGuildInstance, isMemberInstance, isMessageInstance, isNsfwChannel, isObject, isSnowflake, isSupportedVoiceChannel, isTextChannelInstance, isTruthy, isURL, isVoiceChannelEmpty, objectKeys, resolveGuildId, version };
3260
- export type { Awaitable, CustomPlaylistOptions, DisTubeEvents, DisTubeOptions, DisTubePlugin, DisTubeVoiceEvents, Disposable, FFmpegArg, FFmpegArgs, FFmpegOptions, Falsy, Filter, FilterResolvable, Filters, GuildIdResolvable, JumpOptions, KeyOf, PlayHandlerOptions, PlayOptions, PlaylistInfo, RelatedSong, ResolveOptions, ResolvePlaylistOptions, SearchOptions, SongInfo, StreamHandler, StreamOptions, StreamRequest, StreamResponse, StreamServerOptions, TypedDisTubeEvents, UnhandledErrorBehavior };
3616
+ export { AUDIO_CHANNELS, AUDIO_SAMPLE_RATE, BaseManager, CROSSFADE_DRIFT_TOLERANCE_SECS, DEFAULT_CROSSFADE_CURVE, DEFAULT_CROSSFADE_DURATION_SECS, DEFAULT_CROSSFADE_WARMUP_SECS, DEFAULT_PRELOAD_THRESHOLD_SECS, DEFAULT_VOLUME, DisTube, DisTubeBase, DisTubeError, DisTubeHandler, DisTubeStream, DisTubeVoice, DisTubeVoiceManager, Events, ExtractorPlugin, FilterManager, GuildIdManager, HTTP_REDIRECT_CODES, InfoExtractorPlugin, JOIN_TIMEOUT_MS, MAX_CROSSFADE_DURATION_SECS, MAX_REDIRECT_DEPTH, Options, PlayableExtractorPlugin, Playlist, Plugin, PluginError, PluginErrorCode, PluginType, Queue, QueueManager, RECONNECT_MAX_ATTEMPTS, RECONNECT_TIMEOUT_MS, RepeatMode, STUCK_THRESHOLD_MS, SearchResult, SearchResultType, Song, StreamServer, TaskQueue, checkEncryptionLibraries, checkFFmpeg, checkIntents, checkInvalidKey, DisTube as default, defaultFilters, defaultOptions, formatDuration, handleUnhandledError, isClientInstance, isGuildInstance, isMemberInstance, isMessageInstance, isNsfwChannel, isObject, isSnowflake, isSupportedVoiceChannel, isTextChannelInstance, isTruthy, isURL, isVoiceChannelEmpty, objectKeys, resolveGuildId, version };
3617
+ export type { Awaitable, CrossfadeOptions, CrossfadeStreamOptions, CustomPlaylistOptions, DisTubeEvents, DisTubeOptions, DisTubePlugin, DisTubeVoiceEvents, Disposable, FFmpegArg, FFmpegArgs, FFmpegOptions, Falsy, Filter, FilterResolvable, Filters, GuildIdResolvable, JumpOptions, KeyOf, PlayHandlerOptions, PlayOptions, PlaylistInfo, PreloadOptions, RelatedSong, ResolveOptions, ResolvePlaylistOptions, SearchOptions, SongInfo, StreamHandler, StreamOptions, StreamRequest, StreamResponse, StreamServerOptions, TypedDisTubeEvents, UnhandledErrorBehavior };