Implement Noisegate function to start and stop playing the audio stream
This commit is contained in:
43
NoiseGate.py
Normal file
43
NoiseGate.py
Normal file
@@ -0,0 +1,43 @@
|
||||
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
|
||||
|
||||
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:
|
||||
self.stream.start()
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user