Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio filters #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
VC_TIMOUT_DEFAULT = True #default template setting for VC timeout true= yes, timeout false= no timeout
ALLOW_VC_TIMEOUT_EDIT = True #allow or disallow editing the vc_timeout guild setting

# ffmpeg filters (https://ffmpeg.org/ffmpeg-filters.html#equalizer)
FILTERS = {
"clear": "dynaudnorm=f=200",
"bassboost": "bass=g=10",
"8d": "apulsator=hz=0.08",
"vaporwave": "aresample=48000,asetrate=48000*0.8",
"nightcore": "aresample=48000,asetrate=48000*1.25",
"phaser": "aphaser=in_gain=0.4",
"purebass": "bass=g=20,asubboost",
"tremolo": "tremolo",
"vibrato": "vibrato=f=6.5",
"reverse": "areverse",
"treble": "treble=g=5",
"surrounding": "surround",
"pulsator": "apulsator=hz=1",
"subboost": "asubboost",
"karaoke": "stereotools=mlev=0.03",
"flanger": "flanger",
"gate": "agate",
"haas": "haas",
"mcompand": "mcompand"
}

STARTUP_MESSAGE = "Starting Bot..."
STARTUP_COMPLETE_MESSAGE = "Startup Complete"
Expand Down Expand Up @@ -89,5 +111,7 @@
HELP_SHUFFLE_LONG = "Randomly sort the songs in the current queue"
HELP_CHANGECHANNEL_SHORT = "Change the bot channel"
HELP_CHANGECHANNEL_LONG = "Change the bot channel to the VC you are in"
HELP_FILTER_SHORT = "Apply filter to the queue"
HELP_FILTER_LONG = "Apply one of the following filters to the queue: \n {}".format(", ".join(FILTERS.keys()))

ABSOLUTE_PATH = '' #do not modify
29 changes: 27 additions & 2 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, bot, guild):

sett = utils.guild_to_settings[guild]
self._volume = sett.get('default_volume')
self.filters = []

self.timer = utils.Timer(self.timeout_handler)

Expand All @@ -43,6 +44,25 @@ def volume(self, value):
except Exception as e:
pass

# Return ffmpeg filter string
def filter_string(self):
if self.filters:
return '-af "dynaudnorm=f=200,' + ','.join([config.FILTERS[af] for af in self.filters]) + '"'
else:
return '-af "dynaudnorm=f=200"'

async def restart_player(self):
current_song = self.current_song # Save current song cause voice_client.stop() will clear it
self.guild.voice_client.stop()
self.current_song = current_song
self.guild.voice_client.play(
discord.FFmpegPCMAudio(
self.current_song.base_url,
before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
options=self.filter_string()),
after=lambda e: self.next_song(e)
)

async def register_voice_channel(self, channel):
await channel.connect(reconnect=True, timeout=None)

Expand Down Expand Up @@ -102,8 +122,13 @@ async def play_song(self, song):

self.playlist.playhistory.append(self.current_song)

self.guild.voice_client.play(discord.FFmpegPCMAudio(
song.base_url, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'), after=lambda e: self.next_song(e))
self.guild.voice_client.play(
discord.FFmpegPCMAudio(
song.base_url,
before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
options=self.filter_string()),
after=lambda e: self.next_song(e)
)

self.guild.voice_client.source = discord.PCMVolumeTransformer(
self.guild.voice_client.source)
Expand Down
30 changes: 30 additions & 0 deletions musicbot/commands/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@ async def _volume(self, ctx, *args):
except:
await ctx.send("Error: Volume must be a number 1-100")

@commands.command(name='filter', description=config.HELP_FILTER_LONG, help=config.HELP_FILTER_SHORT)
async def _filter(self, ctx, *args):
audiocontroller = utils.guild_to_audiocontroller[ctx.guild]

if ctx.guild is None:
await ctx.send(config.NO_GUILD_MESSAGE)
return

if await utils.play_check(ctx) == False:
return

if len(args) == 0:
await ctx.send(f':information_source: | Current filters: `{", ".join(audiocontroller.filters)}`')
return

if args[0] == 'off':
audiocontroller.filters = []
await audiocontroller.restart_player()
await ctx.send(':no_entry_sign: | All filters disabled!')
elif args[0] in config.FILTERS:
if args[0] not in audiocontroller.filters:
audiocontroller.filters.append(args[0])
await audiocontroller.restart_player()
await ctx.send(f':notes: | Filter `{args[0]}` enabled!')
else:
audiocontroller.filters.remove(args[0])
await audiocontroller.restart_player()
await ctx.send(f':no_entry_sign: | Filter `{args[0]}` disabled!')
else:
await ctx.send(":warning: | Invalid filter!")

def setup(bot):
bot.add_cog(Music(bot))