Implement noisegate into recording
- Should remove dead air - Should save space as it's not recording dead air
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import pyaudio
|
||||
import wave, logging, threading, time, queue, signal, argparse
|
||||
import wave, logging, threading, time, queue, signal, argparse, audioop
|
||||
from os import path, makedirs
|
||||
|
||||
logging.basicConfig(format="%(asctime)s: %(message)s", level=logging.INFO,datefmt="%H:%M:%S")
|
||||
|
||||
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.DEVICE_ID = DEVICE_ID
|
||||
@@ -13,6 +13,9 @@ class DiscordRecorder:
|
||||
self.FORMAT = FORMAT
|
||||
self.CHANNELS = CHANNELS
|
||||
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._check_file_path_exists()
|
||||
@@ -35,8 +38,35 @@ class DiscordRecorder:
|
||||
def _recorder(self):
|
||||
logging.info("* Recording Thread Starting")
|
||||
while True:
|
||||
data = self.running_stream.read(self.CHUNK)
|
||||
self.queued_frames.put(data)
|
||||
try:
|
||||
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
|
||||
if self.stop_threads.is_set():
|
||||
|
||||
Reference in New Issue
Block a user