From df731445fdab99b9a3b69945fd8f6177988d6adf Mon Sep 17 00:00:00 2001 From: Snowflake107 Date: Sun, 20 Jun 2021 11:22:59 +0545 Subject: [PATCH] audio filters --- src/Structures/Queue.ts | 51 ++++++++++++++++----- src/VoiceInterface/BasicStreamDispatcher.ts | 2 +- src/types/types.ts | 1 + src/utils/AudioFilters.ts | 3 +- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index 494db44..588976b 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -19,9 +19,9 @@ class Queue { public playing = false; public metadata?: T = null; public repeatMode: QueueRepeatMode = 0; - public _streamTime: number = 0; + private _streamTime: number = 0; public _cooldownsTimeout = new Collection(); - private _activeFilters: any = {}; + private _activeFilters: any[] = []; /** * Queue constructor @@ -61,7 +61,7 @@ class Queue { useSafeSearch: false, disableAutoRegister: false, fetchBeforeQueued: false, - initialVolume: 50 + initialVolume: 100 } as PlayerOptions, options ); @@ -205,11 +205,34 @@ class Queue { get streamTime() { if (!this.connection) return 0; - return this._streamTime + this.connection.streamTime; + const playbackTime = this._streamTime + this.connection.streamTime; + const NC = this._activeFilters.includes("nightcore") ? 1.25 : null; + const VW = this._activeFilters.includes("vaporwave") ? 0.8 : null; + + if (NC && VW) return playbackTime * (NC + VW); + return NC ? playbackTime * NC : VW ? playbackTime * VW : playbackTime; } - async setFilters(filters: QueueFilters) { - if (!Object.keys(filters).length) return; + getFiltersEnabled() { + return AudioFilters.names.filter((x) => this._activeFilters.includes(x)); + } + + getFiltersDisabled() { + return AudioFilters.names.filter((x) => !this._activeFilters.includes(x)); + } + + async setFilters(filters?: QueueFilters) { + if (!filters || !Object.keys(filters).length) { + // reset filters + const streamTime = this.streamTime; + this._activeFilters = []; + return await this.play(this.current, { + immediate: true, + filtersUpdate: true, + seek: streamTime, + encoderArgs: [] + }); + } const _filters: any[] = []; @@ -217,12 +240,16 @@ class Queue { if (filters[filter as keyof QueueFilters] === true) _filters.push(filter); } + if (this._activeFilters.join("") === _filters.join("")) return; + const newFilters = AudioFilters.create(_filters); + const streamTime = this.streamTime; + this._activeFilters = _filters; return await this.play(this.current, { immediate: true, filtersUpdate: true, - seek: this.streamTime, + seek: streamTime, encoderArgs: ["-af", newFilters] }); } @@ -266,8 +293,8 @@ class Queue { // discord-ytdl-core opusEncoded: false, fmt: "s16le", - encoderArgs: options.encoderArgs ?? [], - seek: options.seek + encoderArgs: options.encoderArgs ?? this._activeFilters.length ? ["-af", AudioFilters.create(this._activeFilters)] : [], + seek: options.seek ? options.seek / 1000 : 0 }).on("error", (err) => (err.message.toLowerCase().includes("premature close") ? null : this.player.emit("error", this, err))); } else { stream = ytdl @@ -276,8 +303,8 @@ class Queue { { opusEncoded: false, fmt: "s16le", - encoderArgs: options.encoderArgs ?? [], - seek: options.seek + encoderArgs: options.encoderArgs ?? this._activeFilters.length ? ["-af", AudioFilters.create(this._activeFilters)] : [], + seek: options.seek ? options.seek / 1000 : 0 } ) .on("error", (err) => (err.message.toLowerCase().includes("premature close") ? null : this.player.emit("error", this, err))); @@ -291,7 +318,7 @@ class Queue { if (options.seek) this._streamTime = options.seek; const dispatcher = await this.connection.playStream(resource); - dispatcher.setVolume(options.encoderArgs && options.encoderArgs[1]?.includes("bass=g") ? this.options.initialVolume + 35 : this.options.initialVolume); + dispatcher.setVolume(this.options.initialVolume); // need to use these events here dispatcher.once("start", () => { diff --git a/src/VoiceInterface/BasicStreamDispatcher.ts b/src/VoiceInterface/BasicStreamDispatcher.ts index 1b33657..57fc0b9 100644 --- a/src/VoiceInterface/BasicStreamDispatcher.ts +++ b/src/VoiceInterface/BasicStreamDispatcher.ts @@ -160,7 +160,7 @@ class BasicStreamDispatcher extends EventEmitter { get streamTime() { if (!this.audioResource) return 0; - return Math.floor(this.audioResource.playbackDuration / 1000); + return this.audioResource.playbackDuration; } get paused() { diff --git a/src/types/types.ts b/src/types/types.ts index 2b4bd47..75861fa 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -19,6 +19,7 @@ export type QueueFilters = { reverse?: boolean; treble?: boolean; normalizer?: boolean; + normalizer2?: boolean; surrounding?: boolean; pulsator?: boolean; subboost?: boolean; diff --git a/src/utils/AudioFilters.ts b/src/utils/AudioFilters.ts index ccb12b3..55f8a3b 100644 --- a/src/utils/AudioFilters.ts +++ b/src/utils/AudioFilters.ts @@ -34,7 +34,7 @@ import { FiltersName } from "../types/types"; */ const FilterList = { - bassboost: "bass=g=20", + bassboost: "bass=g=20:f=110:w=0.3", "8D": "apulsator=hz=0.09", vaporwave: "aresample=48000,asetrate=48000*0.8", nightcore: "aresample=48000,asetrate=48000*1.25", @@ -44,6 +44,7 @@ const FilterList = { reverse: "areverse", treble: "treble=g=5", normalizer: "dynaudnorm=g=101", + normalizer2: "acompressor", surrounding: "surround", pulsator: "apulsator=hz=1", subboost: "asubboost",