Skip to content

Audio crossfade

DisTube supports audio crossfade between consecutive songs. Instead of a hard cut where one stream stops, DisTube resolves the next song, and a new stream starts, crossfade creates a single merged transition stream using FFmpeg’s acrossfade filter.

The ending track fades out while the beginning of the next track fades in simultaneously, at 48kHz stereo 16-bit PCM throughout.

Crossfade is disabled by default (0 seconds). You can configure a global default for new queues when instantiating DisTube:

import { DisTube } from "@kattowo/distube";
const distube = new DisTube(client, {
crossfade: {
duration: 6, // Default crossfade duration in seconds (0 to 12)
curve: "qsin", // Fade curve: "qsin" (equal-power), "tri", "esin", etc.
warmup: 5, // Spawn the transition stream 5s early to warm up buffers
},
});
Option Type Default Description
duration number 0 The default crossfade duration in seconds for new queues (0 disables). Clamped between 0 and 12.
curve string "qsin" The FFmpeg acrossfade fade curve applied to both tracks. "qsin" is an equal-power quarter-sine curve that avoids the -3dB center dip of linear fading.
warmup number 5 How many seconds before the fade window begins the transition stream is spawned. This allows both input streams to buffer into memory before the boundary.
preparePrimaryInput function undefined An optional adapter rewriting the fading-out song’s input right before the transition stream is spawned. See seek-hostile sources.

Each Queue can read and adjust its crossfade duration independently, which makes it easy to bind crossfade to a per-guild database setting:

const queue = distube.getQueue(guildId);
// Read the current crossfade duration in seconds (0 = disabled)
console.log(queue.crossfade);
// Change crossfade for this queue (0 to 12 seconds)
queue.crossfade = 8;
// Optionally re-arm the crossfade immediately for the current song
// (useful if the duration was changed mid-playback)
queue.scheduleCrossfade();

The library arms the schedule itself after playSong, when a song is added to the queue, when the queue resumes, and when it is shuffled. Arming the same schedule twice is a no-op, so a bot that re-applies the stored guild settings does not spawn a second transition.

The fade-out input of the transition FFmpeg is seeked near the end of the fading-out song, a few seconds before the fade window. Streaming inputs that cannot fast-seek — HLS playlists, for example — force FFmpeg to re-download the whole stream and throw away everything before the seek position. On a slow source that download can outlast the warm-up window, and the fade never has a chance to open.

The optional crossfade.preparePrimaryInput adapter is invoked right before the transition stream is spawned, and can replace the fading-out song’s input with one that starts at the boundary:

const distube = new DisTube(client, {
crossfade: {
duration: 6,
// Rewrite an HLS url into a local playlist that holds only the tail of
// the song. The example shapes of the return value:
//
// - `url` — the replacement input url or path
// - `seek` — the initial seek inside the replacement input, in seconds
// relative to it; omit it when the input starts exactly at the boundary
// - `inputArgs` — FFmpeg input options replacing the default ones, which
// are tuned for http(s) streaming urls
// - `primaryStartTime` — the media timestamp of the fade boundary in the
// replacement input. Set it when the input starts before the requested
// position (a source that can only start on a keyframe, for example) so
// FFmpeg trims the extra audio before the fade.
preparePrimaryInput: async (url, { seek, song }) => {
if (!isPlaylistUrl(url)) return null; // fall back to the url as-is
const local = await trimToTail(url, seek, song.duration);
return {
url: local.playlistPath,
// FFmpeg input options of the replacement input. They replace the
// defaults of the streaming urls.
inputArgs: { analyzeduration: 0, f: "mpegts" },
};
},
},
});

seek and primaryStartTime are different positions. seek is relative to the replacement input; primaryStartTime is the position of the fade boundary on the media’s own timeline. The input keeps its timestamps, so it can be trimmed to the same boundary it would have had in the original stream.

  1. Warmup pre-spawn: A few seconds before the fade window starts (controlled by crossfade.warmup), DisTube pre-spawns an FFmpeg process with two inputs:
    • Input 0: the current song seeked to its tail (duration - crossfade).
    • Input 1: the next song from its start.
  2. Boundary swap: The boundary timer is armed slightly before the fade window. When it fires, DisTube waits for the media clock (Queue#currentTime) to reach the boundary, so a slow start or a short network stall neither skips nor replays audio. The swap is accepted within 0.75s after the boundary; a lag of more than 3s, or a wait longer than 4s, falls back to the regular transition. A timer that fires after the boundary cannot recover the audio that already played, so it falls back too.
  3. Queue progression: At the swap boundary, the queue emits finishSong for the outgoing track and playSong for the incoming track, shifting the queue state as if the song had naturally ended.
  4. Safety & Fallback: If the playback drifts excessively, or if a user pauses, seeks, skips, stops, shuffles, or jumps tracks during the fade window, the pending transition is automatically canceled and DisTube falls back to a standard track switch. A seek during the warm-up kills the buffered transition and arms a new schedule from the new position.

The crossfade is opportunistic: every failure degrades gracefully to the regular transition, never to a broken playback or a stalled queue.

A transition stream that has not produced any audio when the fade window opens (one of its FFmpeg inputs buffering forever, for example) does not take over the playback. Such a stream is aborted, the crossfade state is cleared, and the song keeps its standard transition — the queue never sits in a buffering state waiting for the fade.

Resolving the next song can take longer than the time left before the fade. When the boundary is already reached by the time the warm-up finishes, DisTube does not spawn the transition stream: the swap would kill it immediately and, on a source that keeps a session open, leave that session behind. The queue uses the regular transition instead.

The fading-out input is padded to the fade duration

Section titled “The fading-out input is padded to the fade duration”

The reported song duration is rounded to the second while the media is not. When the media ends slightly before the boundary plus the fade duration, acrossfade would receive a shorter first input and emit no frame at all, which makes the transition look dead. The trimmed input is padded with silence to the fade duration, so a short tail fades out instead of aborting the crossfade.

A transition FFmpeg that dies mid-warmup is reported, not fatal anymore: the queue clears its crossfade state and falls back to the regular transition. The FFMPEG_EXITED error of such a failure carries the tail of the FFmpeg stderr output, so the exact reason (a hostile input, an unsupported codec of the replacement input…) shows up in the message instead of an exit code alone.

The queue volume is applied to a stream at the moment it is created, not only when it is played. A warm-up stream buffers PCM long before the boundary, and audio that sits in that buffer would otherwise play at full volume, bursting at the fade boundary before the correct gain takes over.

A related hand-over detail: when the player switches resources, @discordjs/voice tears down the pipeline of the outgoing stream, which surfaces as ERR_STREAM_PREMATURE_CLOSE on it. That is the normal hand-over, not a crossfade failure.

Both inputs are normalized to 32-bit float before the acrossfade filter, so equal-power curves sum without the 16-bit integer wrap-around, and a limiter keeps the mixed peaks below 0 dBFS. The same fade is safe on loud dynamics-heavy tracks (EDM, dariacore, hyperflip) as it is on acoustic ones.