From ebab92f61dffb7dca48ae13630b811858c6bd65e Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Wed, 10 Aug 2016 15:46:09 -0500 Subject: [PATCH] Allow mid-song volume changes (#346) --- cogs/audio.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cogs/audio.py b/cogs/audio.py index 655d5ef4b..b3cc04da6 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -342,9 +342,7 @@ class Audio: song_filename = os.path.join(self.cache_path, filename) use_avconv = self.settings["AVCONV"] - volume = self.get_server_settings(server)["VOLUME"] / 100 - options = \ - '-filter "volume=volume={}" -b:a 64k -bufsize 64k'.format(volume) + options = '-b:a 64k -bufsize 64k' try: voice_client.audio_player.process.kill() @@ -359,6 +357,10 @@ class Audio: voice_client.audio_player = voice_client.create_ffmpeg_player( song_filename, use_avconv=use_avconv, options=options) + # Set initial volume + vol = self.get_server_settings(server)['VOLUME'] / 100 + voice_client.audio_player.volume = vol + return voice_client # Just for ease of use, it's modified in-place # TODO: _current_playlist @@ -1012,17 +1014,28 @@ class Audio: @audioset.command(pass_context=True, name="volume", no_pm=True) @checks.mod_or_permissions(manage_messages=True) - async def audioset_volume(self, ctx, percent: int): - """Sets the volume (0 - 100)""" + async def audioset_volume(self, ctx, percent: int=None): + """Sets the volume (0 - 100) + Note: volume may be set up to 200 but you may experience clipping.""" server = ctx.message.server - if percent >= 0 and percent <= 100: + if percent is None: + vol = self.get_server_settings(server)['VOLUME'] + msg = "Volume is currently set to %d%%" % vol + elif percent >= 0 and percent <= 200: self.set_server_setting(server, "VOLUME", percent) - await self.bot.say("Volume is now set at " + str(percent) + - ". It will take effect after the current" - " track.") + msg = "Volume is now set to %d." % percent + if percent > 100: + msg += "\nWarning: volume levels above 100 may result in clipping" + + # Set volume of playing audio + vc = self.voice_client(server) + if vc: + vc.audio_player.volume = percent / 100 + self.save_settings() else: - await self.bot.say("Volume must be between 0 and 100.") + msg = "Volume must be between 0 and 100." + await self.bot.say(msg) @audioset.command(pass_context=True, name="vote", no_pm=True, hidden=True, enabled=False)