From 9584fe8ebe7b1cb97200d7a13e8763b1f4babba2 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 17 Sep 2023 18:14:20 +0200 Subject: [PATCH] feat: added equalizer filters to /filters command --- config/default.js | 139 ++++++++++++++++-- src/interactions/commands/player/filters.ts | 48 +++--- .../components/filters-select-menu.ts | 60 +++++++- src/types/configTypes.d.ts | 4 + 4 files changed, 213 insertions(+), 38 deletions(-) diff --git a/config/default.js b/config/default.js index 79dd680..b6f92ae 100644 --- a/config/default.js +++ b/config/default.js @@ -205,6 +205,12 @@ module.exports.ffmpegFilterOptions = { value: 'silenceremove', description: 'Remove silence from start of tracks.', emoji: '🔇' + }, + { + label: 'Fade in', + value: 'fadein', + description: 'Fade in the audio at the start of tracks.', + emoji: '📈' } ] }; @@ -212,61 +218,61 @@ module.exports.ffmpegFilterOptions = { module.exports.biquadFilterOptions = { availableFilters: [ { - label: 'Basic Low Pass (Approx.)', + label: 'Basic low pass (approx.)', value: 'SinglePoleLowPassApprox', description: 'Low frequencies pass, approximate.', emoji: '🔉' }, { - label: 'Basic Low Pass', + label: 'Basic low pass', value: 'SinglePoleLowPass', description: 'Low frequencies pass.', emoji: '🔊' }, { - label: 'Advanced Low Pass', + label: 'Advanced low pass', value: 'LowPass', description: 'Enhanced low frequency pass.', emoji: '🔊' }, { - label: 'High Pass', + label: 'High pass', value: 'HighPass', description: 'High frequencies pass.', emoji: '🔊' }, { - label: 'Band Pass', + label: 'Band pass', value: 'BandPass', description: 'Specific frequency range pass.', emoji: '🔊' }, { - label: 'Notch Filter', + label: 'Notch filter', value: 'Notch', description: 'Reduces specific frequency.', emoji: '🔊' }, { - label: 'Phase Adjuster', + label: 'Phase adjuster', value: 'AllPass', description: 'Alters sound phase.', emoji: '🔊' }, { - label: 'Low Shelf', + label: 'Low shelf', value: 'LowShelf', description: 'Boosts/reduces low frequencies.', emoji: '🔊' }, { - label: 'High Shelf', + label: 'High shelf', value: 'HighShelf', description: 'Boosts/reduces high frequencies.', emoji: '🔊' }, { - label: 'Peaking Equalizer', + label: 'Peaking equalizer', value: 'PeakingEQ', description: 'Adjusts specific frequencies.', emoji: '🔊' @@ -274,6 +280,119 @@ module.exports.biquadFilterOptions = { ] }; +module.exports.equalizerFilterOptions = { + availableFilters: [ + { + label: 'Flat', + value: 'Flat', + description: 'Neutral sound balance', + emoji: '🔉' + }, + { + label: 'Classical', + value: 'Classical', + description: 'Optimized for orchestras', + emoji: '🎻' + }, + { + label: 'Club', + value: 'Club', + description: 'Boosted bass and treble', + emoji: '🎵' + }, + { + label: 'Dance', + value: 'Dance', + description: 'High bass and treble', + emoji: '💃' + }, + { + label: 'Full bass', + value: 'FullBass', + description: 'Maximized bass response', + emoji: '🔊' + }, + { + label: 'Full bass & treble', + value: 'FullBassTreble', + description: 'Bass and treble boost', + emoji: '🎵' + }, + { + label: 'Full treble', + value: 'FullTreble', + description: 'Maximized treble response', + emoji: '🎶' + }, + { + label: 'Headphones', + value: 'Headphones', + description: 'Optimized for headphones', + emoji: '🎧' + }, + { + label: 'Large hall', + value: 'LargeHall', + description: 'Echo effect for halls', + emoji: '🏛️' + }, + { + label: 'Live', + value: 'Live', + description: 'Imitates live music', + emoji: '🎤' + }, + { + label: 'Party', + value: 'Party', + description: 'Balanced for loudness', + emoji: '🎉' + }, + { + label: 'Pop', + value: 'Pop', + description: 'Optimized for pop music', + emoji: '🎤' + }, + { + label: 'Reggae', + value: 'Reggae', + description: 'Optimized for reggae music', + emoji: '🎷' + }, + { + label: 'Rock', + value: 'Rock', + description: 'Optimized for rock music', + emoji: '🎸' + }, + { + label: 'Ska', + value: 'Ska', + description: 'Optimized for ska music', + emoji: '🎷' + }, + { + label: 'Soft', + value: 'Soft', + description: 'Softened sound balance', + emoji: '🎶' + }, + { + label: 'Soft rock', + value: 'SoftRock', + description: 'Optimized for soft rock', + emoji: '🎸' + }, + { + label: 'Techno', + value: 'Techno', + description: 'Optimized for techno music', + emoji: '🎹' + } + ] +}; + // Configuration for load testing. Enables the bot to simulate certain behaviors for testing purposes. // If enabled, the bot will join the specified channels and play specified track. module.exports.loadTestOptions = { diff --git a/src/interactions/commands/player/filters.ts b/src/interactions/commands/player/filters.ts index e624ab0..cbf15c5 100644 --- a/src/interactions/commands/player/filters.ts +++ b/src/interactions/commands/player/filters.ts @@ -17,13 +17,19 @@ import { } from 'discord.js'; import { Logger } from 'pino'; import { BaseSlashCommandInteraction } from '../../../classes/interactions'; -import { BiquadFilterOptions, FFmpegFilterOptions, FilterOption } from '../../../types/configTypes'; +import { + BiquadFilterOptions, + EqualizerFilterOptions, + FFmpegFilterOptions, + FilterOption +} from '../../../types/configTypes'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); const biquadFilterOptions: BiquadFilterOptions = config.get('biquadFilterOptions'); +const equalizerFilterOptions: EqualizerFilterOptions = config.get('equalizerFilterOptions'); class FiltersCommand extends BaseSlashCommandInteraction { constructor() { @@ -66,9 +72,7 @@ class FiltersCommand extends BaseSlashCommandInteraction { case 'biquad': return await this.handleBiquadFilters(logger, interaction, queue); case 'equalizer': - // TODO: implement - logger.debug('Handling equalizer filters.'); - return await this.tempNotImplementedResponse(logger, interaction); + return await this.handleEqualizerFilters(logger, interaction); case 'disable': return await this.disableAllFiltersAndRespondWithSuccess(logger, interaction, queue); } @@ -129,6 +133,24 @@ class FiltersCommand extends BaseSlashCommandInteraction { return await this.sendFiltersEmbed(logger, interaction, 'biquad', filterOptions); } + private async handleEqualizerFilters(logger: Logger, interaction: ChatInputCommandInteraction): Promise { + logger.debug('Handling biquad filters.'); + const filterOptions: StringSelectMenuOptionBuilder[] = []; + + equalizerFilterOptions.availableFilters.forEach((filter: FilterOption) => { + filterOptions.push( + new StringSelectMenuOptionBuilder() + .setLabel(filter.label) + .setDescription(filter.description) + .setValue(filter.value) + .setEmoji(filter.emoji) + .setDefault(false) + ); + }); + + return await this.sendFiltersEmbed(logger, interaction, 'equalizer', filterOptions); + } + private async sendFiltersEmbed( logger: Logger, interaction: ChatInputCommandInteraction, @@ -218,24 +240,6 @@ class FiltersCommand extends BaseSlashCommandInteraction { components: [] }); } - - private async tempNotImplementedResponse( - logger: Logger, - interaction: ChatInputCommandInteraction - ): Promise { - logger.debug('Responding with info embed.'); - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${this.embedOptions.icons.bot} Coming soon**\n` + - 'This functionality has not yet been implemented, and will be available soon.' - ) - .setColor(this.embedOptions.colors.info) - ], - components: [] - }); - } } export default new FiltersCommand(); diff --git a/src/interactions/components/filters-select-menu.ts b/src/interactions/components/filters-select-menu.ts index 1edffe0..2f62728 100644 --- a/src/interactions/components/filters-select-menu.ts +++ b/src/interactions/components/filters-select-menu.ts @@ -1,15 +1,21 @@ import config from 'config'; -import { BiquadFilters, GuildQueue, QueueFilters, useQueue } from 'discord-player'; +import { BiquadFilters, EqualizerConfigurationPreset, GuildQueue, QueueFilters, useQueue } from 'discord-player'; import { EmbedBuilder, Message, StringSelectMenuInteraction } from 'discord.js'; import { Logger } from 'pino'; import { BaseComponentInteraction } from '../../classes/interactions'; -import { BiquadFilterOptions, FFmpegFilterOptions, FilterOption } from '../../types/configTypes'; +import { + EqualizerFilterOptions, + BiquadFilterOptions, + FFmpegFilterOptions, + FilterOption +} from '../../types/configTypes'; import { BaseComponentParams, BaseComponentReturnType } from '../../types/interactionTypes'; import { checkQueueExists } from '../../utils/validation/queueValidator'; import { checkInVoiceChannel, checkSameVoiceChannel } from '../../utils/validation/voiceChannelValidator'; const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); const biquadFilterOptions: BiquadFilterOptions = config.get('biquadFilterOptions'); +const equalizerFilterOptions: EqualizerFilterOptions = config.get('equalizerFilterOptions'); class FiltersSelectMenuComponent extends BaseComponentInteraction { constructor() { @@ -44,9 +50,7 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { case 'biquad': return await this.handleBiquadFilterToggle(logger, selectMenuInteraction, queue, filterType); case 'equalizer': - // TODO: implement - logger.info('equalizer'); - break; + return await this.handleEqualizerFilterToggle(logger, selectMenuInteraction, queue, filterType); default: logger.warn(`Unknown filter type '${filterType}'.`); return; @@ -78,12 +82,28 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { return await this.sendFiltersToggledSuccessEmbed(logger, selectMenuInteraction, filterType); } + private async handleEqualizerFilterToggle( + logger: Logger, + selectMenuInteraction: StringSelectMenuInteraction, + queue: GuildQueue, + filterType: string + ): Promise { + this.toggleEqualizerFilter(selectMenuInteraction, queue, logger); + + logger.debug('Responding with success embed.'); + return await this.sendFiltersToggledSuccessEmbed(logger, selectMenuInteraction, filterType); + } + private toggleFfmpegFilters( selectMenuInteraction: StringSelectMenuInteraction, queue: GuildQueue, logger: Logger ): void { try { + if (!queue.filters.ffmpeg) { + throw new Error('FFmpeg filter is not enabled.'); + } + queue.filters.ffmpeg.toggle(selectMenuInteraction.values as (keyof QueueFilters)[]); logger.debug(`Enabled filters ${selectMenuInteraction.values.join(', ')}.`); } catch (error) { @@ -98,6 +118,11 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { ): void { try { queue.filters.biquad!.enable(); + + if (!queue.filters.biquad) { + throw new Error('Biquad filter is not enabled.'); + } + queue.filters.biquad!.setGain(5); queue.filters.biquad!.setFilter(selectMenuInteraction.values[0] as BiquadFilters); logger.debug(`Enabled filters ${selectMenuInteraction.values.join(', ')}.`); @@ -106,6 +131,28 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { } } + private toggleEqualizerFilter( + selectMenuInteraction: StringSelectMenuInteraction, + queue: GuildQueue, + logger: Logger + ): void { + try { + queue.filters.equalizer!.enable(); + + const inputFilter = selectMenuInteraction.values[0] as keyof typeof EqualizerConfigurationPreset; + const eqFilter = EqualizerConfigurationPreset[inputFilter]; + + if (!queue.filters.equalizer) { + throw new Error('Equalizer filter is not enabled.'); + } + + queue.filters.equalizer!.setEQ(eqFilter); + logger.debug(`Enabled filters ${selectMenuInteraction.values.join(', ')}.`); + } catch (error) { + logger.error(error, 'Unhandled error while toggling filters.'); + } + } + private resetFilters(queue: GuildQueue, logger: Logger): void { if (queue.filters.ffmpeg.filters.length > 0) { queue.filters.ffmpeg.setFilters(false); @@ -159,7 +206,8 @@ class FiltersSelectMenuComponent extends BaseComponentInteraction { availableFilters = biquadFilterOptions.availableFilters; break; case 'equalizer': - break; // TODO: implement + availableFilters = equalizerFilterOptions.availableFilters; + break; default: break; } diff --git a/src/types/configTypes.d.ts b/src/types/configTypes.d.ts index 381c04f..538e8fe 100644 --- a/src/types/configTypes.d.ts +++ b/src/types/configTypes.d.ts @@ -124,6 +124,10 @@ export type BiquadFilterOptions = { availableFilters: FFmpegFilterOption[]; }; +export type EqualizerFilterOptions = { + availableFilters: FFmpegFilterOption[]; +}; + export type LoadTestOptions = { enabled: boolean; trackUrl: string;