From f94a900c440af28d61a507b4cd4231890b5162ce Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 27 Mar 2022 14:43:33 -0400 Subject: [PATCH] Added profile commands for the bot --- BotResources.py | 2 ++ NoiseGatev2.py | 3 ++- bot.py | 36 ++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/BotResources.py b/BotResources.py index 5761c8c..17939b4 100644 --- a/BotResources.py +++ b/BotResources.py @@ -10,6 +10,8 @@ PDB_ACCEPTABLE_HANDLERS = {'gqrx': { }} PDB_KNOWN_BOT_IDS = {756327271597473863: "Greada", 915064996994633729: "Jorn", 943742040255115304: "Brent"} +DEFAULT_NOISEGATE_THRESHOLD = 50 + def check_if_config_exists(): if exists('./config.ini'): diff --git a/NoiseGatev2.py b/NoiseGatev2.py index 32975cf..c6f5407 100644 --- a/NoiseGatev2.py +++ b/NoiseGatev2.py @@ -3,6 +3,7 @@ import math import pyaudio import discord import numpy +from BotResources import DEFAULT_NOISEGATE_THRESHOLD voice_connection = None @@ -107,7 +108,7 @@ class AudioStream: class NoiseGate(AudioStream): - def __init__(self, _voice_connection, _noise_gate_threshold: int = 50, **kwargs): + def __init__(self, _voice_connection, _noise_gate_threshold: int = DEFAULT_NOISEGATE_THRESHOLD, **kwargs): super(NoiseGate, self).__init__(_init_on_startup=True, **kwargs) global voice_connection voice_connection = _voice_connection diff --git a/bot.py b/bot.py index dd74724..9cff56c 100644 --- a/bot.py +++ b/bot.py @@ -42,6 +42,7 @@ class Bot(commands.Bot): self.freq = "104700000" self.mode = "wfm" self.squelch = 0 + self.noisegate_sensitivity = BotResources.DEFAULT_NOISEGATE_THRESHOLD # Init SDR Variables self.system_os_type = None @@ -102,7 +103,8 @@ class Bot(commands.Bot): # Create an audio stream from selected device print("Starting noisegate/stream handler") self.streamHandler = NoiseGatev2.NoiseGate(_input_device_index=self.DEVICE_ID, - _voice_connection=voice_connection) + _voice_connection=voice_connection, + _noise_gate_threshold=self.noisegate_sensitivity) # Start the audio stream self.streamHandler.run() @@ -165,6 +167,8 @@ class Bot(commands.Bot): await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the threshold from " f"{self.streamHandler.THRESHOLD} to {_threshold}") self.streamHandler.THRESHOLD = _threshold + if self.sdr_started: + await self.set_activity() # Add commands for GQRX and OP25 if self.Handler in BotResources.PDB_ACCEPTABLE_HANDLERS.keys(): @@ -461,13 +465,14 @@ class Bot(commands.Bot): await self.change_presence(activity=discord.Game(name=f"@ me"), status=discord.Status.idle) # Save the current radio settings as a profile - async def save_radio_config(self, profile_name: str): + async def save_radio_config(self, _profile_name: str): + print(f"Saving profile {_profile_name}") config = configparser.SafeConfigParser() if os.path.exists('./profiles.ini'): config.read('./profiles.ini') - profile_name = str(profile_name).upper() + profile_name = str(_profile_name).upper() if not config.has_section(str(profile_name)): config.add_section(str(profile_name)) @@ -475,6 +480,7 @@ class Bot(commands.Bot): config[str(profile_name)]['Frequency'] = self.freq config[str(profile_name)]['Mode'] = self.mode config[str(profile_name)]['Squelch'] = str(self.squelch) + config[str(profile_name)]['Noisegate Sensitivity'] = str(self.noisegate_sensitivity) with open('./profiles.ini', 'w+') as config_file: config.write(config_file) @@ -487,6 +493,7 @@ class Bot(commands.Bot): # Load a saved profile into the current settings async def load_radio_config(self, profile_name): + print(f"Loading profile {profile_name}") config = configparser.ConfigParser() if os.path.exists('./profiles.ini'): config.read('./profiles.ini') @@ -495,6 +502,13 @@ class Bot(commands.Bot): self.freq = config[self.profile_name]['Frequency'] self.mode = config[self.profile_name]['Mode'] self.squelch = float(config[self.profile_name]['Squelch']) + try: + self.noisegate_sensitivity = int(config[self.profile_name]['Noisegate Sensitivity']) + except NameError as err: + print(f"Config does not contain a 'noisegate sensitivity' value, " + f"creating one now with the default value: {BotResources.DEFAULT_NOISEGATE_THRESHOLD}") + self.noisegate_sensitivity = BotResources.DEFAULT_NOISEGATE_THRESHOLD + await self.save_radio_config(self.profile_name) if self.sdr_started: self.start_sdr() @@ -510,10 +524,11 @@ class Bot(commands.Bot): message_body = "" if self.profile_name: message_body += f"Profile Name: {str(self.profile_name).upper()}\n" - message_body += f"Frequency: {self.freq}\n" \ - f"Mode: {str(self.mode).upper()}\n" + message_body += f"\tMode:\t\t\t\t\t{self.mode}\n" \ + f"\tFrequency:\t\t\t{self.freq}\n" \ + f"\tNoisegate Sensitivity:\t{self.noisegate_sensitivity}" if self.squelch: - message_body += f"Squelch: {self.squelch}" + message_body += f"\tSquelch:\t\t\t\t{self.squelch}" return message_body @@ -523,9 +538,10 @@ class Bot(commands.Bot): if os.path.exists('./profiles.ini'): config.read('./profiles.ini') for section in config.sections(): - message_body += f"\n{section}:\n" \ - f"\tMode:\t\t\t\t{config[section]['Mode']}\n" \ - f"\tFrequency:\t\t{config[section]['Frequency']}\n" \ - f"\tSquelch:\t\t\t{config[section]['Squelch']}\n" + message_body += f"\nProfile Name: {section}:\n" \ + f"\tMode:\t\t\t\t\t{config[section]['Mode']}\n" \ + f"\tFrequency:\t\t\t{config[section]['Frequency']}\n" \ + f"\tNoisegate Sensitivity:\t{config[section]['Noisegate Sensitivity']}" \ + f"\tSquelch:\t\t\t\t{config[section]['Squelch']}\n" return message_body