53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import threading
|
|
import time
|
|
|
|
import numpy as np
|
|
import discord
|
|
import sounddevice as sd
|
|
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
|
|
|
|
|
|
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):
|
|
global voice_connection, audio_stream
|
|
self.stream = sd.InputStream(device=num, callback=stream_callback)
|
|
voice_connection = _voice_connection
|
|
audio_stream = _audio_stream
|
|
|
|
def run(self) -> None:
|
|
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):
|
|
volume_normalization = np.linalg.norm(indata) * 10
|
|
if int(volume_normalization) >= noise_gate_trigger:
|
|
# Triggered noise-gate
|
|
if not voice_connection.is_playing():
|
|
voice_connection.play(discord.PCMAudio(audio_stream))
|
|
#print("|" * int(volume_normalization / 4))
|
|
print("Noise Gate was Triggered")
|
|
time.sleep(10)
|
|
else:
|
|
if voice_connection.is_playing():
|
|
print("Noise Gate stopped")
|
|
voice_connection.stop()
|
|
# try disconnecting and reconnecting |