Implement noisegate into recording

- Should remove dead air
- Should save space as it's not recording dead air
This commit is contained in:
Logan Cusano
2023-07-01 18:24:19 -04:00
parent a58f314475
commit eec80f7239

View File

@@ -1,11 +1,11 @@
import pyaudio import pyaudio
import wave, logging, threading, time, queue, signal, argparse import wave, logging, threading, time, queue, signal, argparse, audioop
from os import path, makedirs from os import path, makedirs
logging.basicConfig(format="%(asctime)s: %(message)s", level=logging.INFO,datefmt="%H:%M:%S") logging.basicConfig(format="%(asctime)s: %(message)s", level=logging.INFO,datefmt="%H:%M:%S")
class DiscordRecorder: class DiscordRecorder:
def __init__(self, DEVICE_ID, CHUNK = 1024, FORMAT = pyaudio.paInt16, CHANNELS = 2, RATE = 48000, FILENAME = "./recs/radio.wav"): def __init__(self, DEVICE_ID, CHUNK = 960, FORMAT = pyaudio.paInt16, CHANNELS = 2, RATE = 48000, FILENAME = "./recs/radio.wav"):
self.pa_instance = pyaudio.PyAudio() self.pa_instance = pyaudio.PyAudio()
self.DEVICE_ID = DEVICE_ID self.DEVICE_ID = DEVICE_ID
@@ -13,6 +13,9 @@ class DiscordRecorder:
self.FORMAT = FORMAT self.FORMAT = FORMAT
self.CHANNELS = CHANNELS self.CHANNELS = CHANNELS
self.RATE = RATE self.RATE = RATE
self.NG_fadeout = 240/20 # Fadeout value used to hold the noisegate after de-triggering
self.NG_fadeout_count = 0 # A count set when the noisegate is triggered and was de-triggered
self.process_set_count = 0 # Counts how many processes have been made
self.FILENAME = FILENAME self.FILENAME = FILENAME
self._check_file_path_exists() self._check_file_path_exists()
@@ -35,8 +38,35 @@ class DiscordRecorder:
def _recorder(self): def _recorder(self):
logging.info("* Recording Thread Starting") logging.info("* Recording Thread Starting")
while True: while True:
data = self.running_stream.read(self.CHUNK) try:
self.queued_frames.put(data) curr_buffer = bytearray(self.stream.stream.read(self.CHUNK))
buffer_rms = audioop.rms(curr_buffer, 2)
if buffer_rms > 0:
buffer_decibel = 20 * math.log10(buffer_rms)
if self.process_set_count % 10 == 0:
if buffer_decibel >= self.stream.THRESHOLD:
LOGGER.debug(f"[Noisegate Open] {buffer_decibel} db")
else:
LOGGER.debug(f"[Noisegate Closed] {buffer_decibel} db")
if buffer_decibel >= self.stream.THRESHOLD:
self.NG_fadeout_count = self.NG_fadeout
self.process_set_count += 1
if curr_buffer:
return self.queued_frames.put(curr_buffer)
else:
if self.NG_fadeout_count > 0:
self.NG_fadeout_count -= 1
LOGGER.debug(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
self.process_set_count += 1
if curr_buffer:
return self.queued_frames.put(curr_buffer)
except OSError as e:
LOGGER.warning(e)
pass
# check for stop # check for stop
if self.stop_threads.is_set(): if self.stop_threads.is_set():