From 15a84cce69f4349f68738c53f7994580aa45ba5e Mon Sep 17 00:00:00 2001 From: NoLogicAlan Date: Wed, 14 Oct 2026 02:30:32 +0300 Subject: [PATCH] Enhance audio processing: add event emitter max listeners, improve trackstream handling, and optimize loadstream probing --- index.mjs | 3 +++ src/music/audio/FluxerAudioBridge.mjs | 10 ++++++++-- src/music/audio/StreamPipeline.mjs | 11 ++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/index.mjs b/index.mjs index 176d28a..9c8f8e2 100644 --- a/index.mjs +++ b/index.mjs @@ -5,6 +5,9 @@ * graceful shutdown. All boot logic lives in src/core/Bot.mjs. */ +import { EventEmitter } from "node:events"; +EventEmitter.defaultMaxListeners = 100; + import { logger } from "./src/core/Logger.mjs"; import Remix from "./src/core/Bot.mjs"; import { initErrorChannel, reportCrash } from "./src/core/ErrorChannel.mjs"; diff --git a/src/music/audio/FluxerAudioBridge.mjs b/src/music/audio/FluxerAudioBridge.mjs index ddb3fff..7ea4eeb 100644 --- a/src/music/audio/FluxerAudioBridge.mjs +++ b/src/music/audio/FluxerAudioBridge.mjs @@ -64,6 +64,7 @@ export class FluxerAudioBridge extends EventEmitter { /** Called by prefetch on success: a working trackstream closes the circuit breaker. */ _tsOk() { tsBreakerOk(); } + _tsBreakerOpen() { return tsBreakerOpen(); } /** @param {LavalinkManager|null} lavalinkManager - LavalinkManager for trackstream/loadstream. */ constructor(lavalinkManager = null) { @@ -431,8 +432,13 @@ export class FluxerAudioBridge extends EventEmitter { } if (!/webm|opus/i.test(String(direct.format || ""))) { - logger.player("[AudioBridge] trackstream format not opus/webm (" + (direct.format || "?") + "), refreshing encoded track once..."); - const freshEncoded = trackInfo.url && trackInfo.url.startsWith("http") + // A fresh resolve only helps when YouTube handed us a non-WebM itag (googlevideo). + // Mirror sources (JioSaavn/Deezer/SoundCloud... AAC/MP3/MP4) return the same thing + // again, so skip the ~7-13s loadtracks round-trip for them. + const refreshable = /googlevideo\.com|youtube\.com|ytimg/i.test(String(direct.url || "")); + logger.player("[AudioBridge] trackstream format not opus/webm (" + (direct.format || "?") + ")" + + (refreshable ? ", refreshing encoded track once..." : " from a non-YouTube source, skipping refresh.")); + const freshEncoded = refreshable && trackInfo.url && trackInfo.url.startsWith("http") ? await this._resolveUrlViaLavalink(trackInfo.url, trackInfo.guildId) : null; if (stale()) return "stopped"; diff --git a/src/music/audio/StreamPipeline.mjs b/src/music/audio/StreamPipeline.mjs index d795dda..9e27ea2 100644 --- a/src/music/audio/StreamPipeline.mjs +++ b/src/music/audio/StreamPipeline.mjs @@ -10,6 +10,7 @@ * FluxerAudioBridge instance. */ +import { isLoadstreamEnabled } from "./AudioSettings.mjs"; import { logger } from "../../core/Logger.mjs"; import { WebMOpusMuxer, OPUS_FRAME_MS } from "./WebMOpusMuxer.mjs"; import { StreamPositioner } from "./StreamPositioner.mjs"; @@ -20,6 +21,7 @@ const { Encoder: PrismOpusEncoder, OggDemuxer: PrismOggDemuxer } = prismMedia.op /** Prefetched trackstream results keyed by encoded track (shared across players). */ const _tsCache = new Map(); const TS_CACHE_TTL_MS = 4 * 60_000; +let _lastProbeAt = 0; /** @type {number} @description PCM sample rate expected from NodeLink loadstream (48kHz stereo s16le). */ const PCM_RATE = 48000; @@ -313,6 +315,11 @@ const StreamPipeline = { if (enc) song.encoded = enc; } if (!song.encoded || _tsCache.has(song.encoded)) return; + // While trackstream is known-bad, probe at most once a minute (each probe is a hung request on the node). + if (this._tsBreakerOpen?.()) { + if (Date.now() - _lastProbeAt < 60_000) return; + _lastProbeAt = Date.now(); + } const info = { encoded: song.encoded, guildId }; const entry = { at: Date.now(), result: null, promise: null }; entry.promise = this._queryTrackstream(info).then( @@ -348,7 +355,9 @@ const StreamPipeline = { logger.player("[AudioBridge] Querying /v4/trackstream..."); - const body = await this._httpGetJson(url, headers); + // A healthy trackstream answers in a few seconds. When loadstream can take over, + // fail fast (8s) instead of making the listener wait the full 15s. + const body = await this._httpGetJson(url, headers, 0, isLoadstreamEnabled() ? 8_000 : 15_000); const directUrl = body?.url; if (!directUrl || typeof directUrl !== "string") {