Improve noise gate

This commit is contained in:
Logan Cusano
2022-02-12 19:59:38 -05:00
parent f4cf8715e9
commit 18ceb0074e

View File

@@ -9,10 +9,11 @@ from threading import Thread, Event
noise_gate_trigger = 0 # Set this value for the trigger on the noise-gate
voice_connection = None
audio_stream = None
last_callback_value = 0
class NoiseGate(Thread):
def __init__(self, trigger_value: int = 1000):
def __init__(self, trigger_value: int = 700):
global noise_gate_trigger
super(NoiseGate, self).__init__()
self.stream = None
@@ -27,27 +28,40 @@ class NoiseGate(Thread):
audio_stream = _audio_stream
def run(self) -> None:
self.NG_Started.set()
self.stream.start()
while not self.stop_NG.is_set():
self.NG_Started.set()
self.stream.start()
#print("Main loop start")
if last_callback_value >= noise_gate_trigger:
trigger_noise_gate(True)
while last_callback_value >= noise_gate_trigger:
time.sleep(6)
trigger_noise_gate(False)
#print("Main loop end")
self.stream.stop()
self.stream.close()
self.NG_Started.clear()
voice_connection.stop()
print('Thread #%s stopped' % self.ident)
def stream_callback(indata, *args):
global last_callback_value
volume_normalization = np.linalg.norm(indata) * 10
if int(volume_normalization) >= noise_gate_trigger:
# Triggered noise-gate
last_callback_value = int(volume_normalization)
#print(f"Callback Value: {last_callback_value}")
def trigger_noise_gate(triggered: bool):
if triggered:
if not voice_connection.is_playing():
voice_connection.play(discord.PCMAudio(audio_stream))
#print("|" * int(volume_normalization / 4))
# print("|" * int(volume_normalization / 4))
print("Noise Gate was Triggered")
time.sleep(10)
#time.sleep(10)
else:
if voice_connection.is_playing():
print("Noise Gate stopped")
voice_connection.stop()
# try disconnecting and reconnecting
voice_connection.pause()
# try disconnecting and reconnecting