diff --git a/NoiseGate.py b/NoiseGate.py index 4a82358..a7eae09 100644 --- a/NoiseGate.py +++ b/NoiseGate.py @@ -1,3 +1,4 @@ +import threading import time import numpy as np @@ -9,12 +10,14 @@ noise_gate_trigger = 0 # Set this value for the trigger on the noise-gate voice_connection = None audio_stream = None + class NoiseGate(Thread): def __init__(self, trigger_value: int = 1000): global noise_gate_trigger super(NoiseGate, self).__init__() self.stream = None - + self.stop_NG = threading.Event() + self.NG_Started = threading.Event() noise_gate_trigger = trigger_value def init_stream(self, num, _voice_connection, _audio_stream): @@ -24,7 +27,14 @@ class NoiseGate(Thread): audio_stream = _audio_stream def run(self) -> None: - self.stream.start() + while not self.stop_NG.is_set(): + self.NG_Started.set() + self.stream.start() + + self.stream.stop() + self.stream.close() + self.NG_Started.clear() + print('Thread #%s stopped' % self.ident) def stream_callback(indata, *args): @@ -40,4 +50,4 @@ def stream_callback(indata, *args): if voice_connection.is_playing(): print("Noise Gate stopped") voice_connection.stop() - # try disconnecting and reconnecting + # try disconnecting and reconnecting \ No newline at end of file diff --git a/bot.py b/bot.py index d09bac1..051cb14 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,5 @@ import os +import platform import discord import sound import configparser @@ -23,6 +24,9 @@ class Bot(commands.Bot): self.Default_Mention_Group = kwargs['Mention_Group'] self.Handler = kwargs['Handler'] + # Init Variable forsound + self.streamHandler = None + # Init the audio devices list self.Devices_List = sound.query_devices().items() @@ -82,9 +86,9 @@ class Bot(commands.Bot): # Create an audio stream from selected device - stream = sound.PCMStream(voice_connection) + self.streamHandler = sound.PCMStream(voice_connection) # Ensure the selected device is available and start the audio stream - stream.change_device(self.DEVICE_ID) + self.streamHandler.change_device(self.DEVICE_ID) # Start the SDR and begin playing to the audio stream self.start_sdr() @@ -99,6 +103,8 @@ class Bot(commands.Bot): @self.command(help="Use this command to have the bot leave your channel", brief="Leaves the current voice channel") async def leave(ctx): + # Stop the sound handlers + self.streamHandler.cleanup() # Disconnect the client from the voice channel await ctx.voice_client.disconnect() # Change the presence to away and '@ me' @@ -187,8 +193,10 @@ class Bot(commands.Bot): def load_opus(self): # Check the system type and load the correct library - if self.system_os_type == 'Linux': + if self.system_os_type == 'Linux_32': discord.opus.load_opus('./opus/libopus.so') + elif self.system_os_type == 'Linux_64': + discord.opus.load_opus('./opus/libopus_aarcch64.so') elif self.system_os_type == 'Windows': discord.opus.load_opus('./opus/libopus.dll') @@ -233,7 +241,11 @@ class Bot(commands.Bot): if os.name == 'nt': self.system_os_type = 'Windows' else: - self.system_os_type = 'Linux' + processor = platform.architecture()[0] + if processor == "64bit": + self.system_os_type = 'Linux_64' + elif processor == "32bit": + self.system_os_type = 'Linux_32' # Check to see if there is only one frequency def start_sdr(self): diff --git a/sound.py b/sound.py index 39e2d46..447945b 100644 --- a/sound.py +++ b/sound.py @@ -26,9 +26,7 @@ class PCMStream: return bytes(data) def change_device(self, num): - if self.stream is not None: - self.stream.stop() - self.stream.close() + self.clean_up() self.stream = sd.RawInputStream(device=num) noisegate_obj.init_stream(num, self.voice_connection, self) @@ -36,6 +34,19 @@ class PCMStream: self.stream.start() noisegate_obj.start() + def clean_up(self): + global noisegate_obj + if self.stream is not None: + self.stream.stop() + self.stream.close() + + if noisegate_obj.NG_Started.is_set(): + print("Closing the noisegate") + noisegate_obj.stop_NG.set() + noisegate_obj.join() + noisegate_obj = NoiseGate() + print("Started the noisegate") + class DeviceNotFoundError(Exception): def __init__(self):