Implement Noisegate function to start and stop playing the audio stream

This commit is contained in:
Logan Cusano
2022-02-04 12:35:00 -05:00
parent 329dc8fff8
commit 0cac9c8f81
4 changed files with 129 additions and 70 deletions

143
bot.py
View File

@@ -21,6 +21,7 @@ class Bot(commands.Bot):
self.BOT_TOKEN = kwargs['Token']
self.Default_Channel_ID = kwargs['Channel_ID']
self.Default_Mention_Group = kwargs['Mention_Group']
self.Handler = kwargs['Handler']
# Init the audio devices list
self.Devices_List = sound.query_devices().items()
@@ -34,7 +35,10 @@ class Bot(commands.Bot):
# Init SDR Variables
self.system_os_type = None
self.sdr_started = False
self.GQRXHandler = GQRXHandler()
if self.Handler == "gqrx":
print("Starting gqrx handler")
self.GQRXHandler = GQRXHandler()
# Set linux or windows
self.check_os_type()
@@ -69,17 +73,18 @@ class Bot(commands.Bot):
self.load_opus()
if discord.opus.is_loaded():
# Create an audio stream from selected device
stream = sound.PCMStream()
channel = ctx.author.voice.channel
await ctx.send(f"Ok {str(member).capitalize()}, I'm joining {channel}")
# Ensure the selected device is available and start the audio stream
stream.change_device(self.DEVICE_ID)
# Join the voice channel with the audio stream
voice_connection = await channel.connect()
voice_connection.play(discord.PCMAudio(stream))
# Create an audio stream from selected device
stream = sound.PCMStream(voice_connection)
# Ensure the selected device is available and start the audio stream
stream.change_device(self.DEVICE_ID)
# Start the SDR and begin playing to the audio stream
self.start_sdr()
@@ -101,66 +106,68 @@ class Bot(commands.Bot):
# Stop the SDR so it can cool off
self.stop_sdr()
@self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. "
"\nExample command: '@ chfreq wfm 104700000\n"
"Example command: '@ chfreq fm 154785000",
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
# Possible band-types that can be used
possible_modes = ['wfm', 'fm']
member = member or ctx.author.display_name
if self.Handler == 'gqrx':
@self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. "
"\nExample command: '@ chfreq wfm 104700000\n"
"Example command: '@ chfreq fm 154785000",
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
# Possible band-types that can be used
possible_modes = ['wfm', 'fm']
member = member or ctx.author.display_name
# Check to make sure the frequency input matches the syntax needed
if len(freq) >= 6:
self.freq = freq
# Check to make sure the selected mode is valid
if mode in possible_modes:
self.mode = mode
# Check to make sure the frequency input matches the syntax needed
if len(freq) >= 6:
self.freq = freq
# Check to make sure the selected mode is valid
if mode in possible_modes:
self.mode = mode
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the mode to {str(self.mode).upper()} and frequency to"
f" {self.freq}")
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the mode to {str(self.mode).upper()} and frequency to"
f" {self.freq}")
# Reset the profile name since we have made a change to the freq
self.profile_name = None
# Reset the profile name since we have made a change to the freq
self.profile_name = None
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
await self.set_activity()
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
await self.set_activity()
else:
await ctx.send(f"{str(member).capitalize()}, {mode} is not valid. You may only enter 'fm' or 'wbfm'")
else:
await ctx.send(f"{str(member).capitalize()}, {mode} is not valid. You may only enter 'fm' or 'wbfm'")
else:
await ctx.send(f"{str(member).capitalize()}, {freq} is not valid. please refer to the help page '@ help chfreq'")
await ctx.send(f"{str(member).capitalize()}, {freq} is not valid. please refer to the help page '@ help chfreq'")
@self.command(name='chsquelch', help="Use this command to change the squelch for the frequency"
"the bot is listening to",
brief="Changes radio squelch")
async def chsquelch(ctx, squelch: float, member: discord.Member = None):
member = member or ctx.author.display_name
@self.command(name='chsquelch', help="Use this command to change the squelch for the frequency"
"the bot is listening to",
brief="Changes radio squelch")
async def chsquelch(ctx, squelch: float, member: discord.Member = None):
member = member or ctx.author.display_name
self.squelch = squelch
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}")
self.squelch = squelch
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}")
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
# If the SDR is started, restart it with the updates
if self.sdr_started:
self.start_sdr()
# Hidden admin commands
@self.command(name='saveprofile', hidden=True)
async def _saveprofile(ctx, profile_name: str, member: discord.Member = None):
member = member or ctx.author.display_name
await self.save_radio_config(profile_name)
await ctx.send(f"Ok {str(member).capitalize()}, I saved the current settings as {profile_name}")
@self.command(name='loadprofile', hidden=True)
async def _loadprofile(ctx, profile_name: str, member: discord.Member = None):
member = member or ctx.author.display_name
config_loaded = await self.load_radio_config(profile_name)
if config_loaded:
await ctx.send(f"Ok {str(member).capitalize()}, I loaded the settings saved as {profile_name}")
else:
await ctx.send(f"{str(member).capitalize()}, there is no profile with the name '{profile_name}'")
# Hidden admin commands
@self.command(name='saveprofile', hidden=True)
async def _saveprofile(ctx, profile_name: str, member: discord.Member = None):
member = member or ctx.author.display_name
await self.save_radio_config(profile_name)
await ctx.send(f"Ok {str(member).capitalize()}, I saved the current settings as {profile_name}")
@self.command(name='loadprofile', hidden=True)
async def _loadprofile(ctx, profile_name: str, member: discord.Member = None):
member = member or ctx.author.display_name
config_loaded = await self.load_radio_config(profile_name)
if config_loaded:
await ctx.send(f"Ok {str(member).capitalize()}, I loaded the settings saved as {profile_name}")
else:
await ctx.send(f"{str(member).capitalize()}, there is no profile with the name '{profile_name}'")
@self.command(name='reload', hidden=True)
async def _reload(ctx, module: str, member: discord.Member = None):
"""Reloads a module."""
@@ -230,26 +237,26 @@ class Bot(commands.Bot):
# Check to see if there is only one frequency
def start_sdr(self):
if type(self.freq) == str:
# Single freq sent
# Stop the SDR if it is running
self.stop_sdr()
if self.Handler == 'gqrx':
if type(self.freq) == str:
# Single freq sent
# Stop the SDR if it is running
self.stop_sdr()
# Start the radio
print(f"Starting freq: {self.freq}")
# Start the radio
print(f"Starting freq: {self.freq}")
# Set the settings in GQRX
self.GQRXHandler.set_all_settings(self.mode, self.squelch, self.freq)
# Set the settings in GQRX
self.GQRXHandler.set_all_settings(self.mode, self.squelch, self.freq)
# Set the started variable for later checks
self.sdr_started = True
# Set the started variable for later checks
self.sdr_started = True
# Check to see if the SDR is running
def stop_sdr(self):
if self.sdr_started:
# Wait for the running processes to close
# Need a way to 'close' GQRX
self.sdr_started = False
# Set the activity of the bot