Merge branch 'V3_Better_Logging' of git.vpn.cusano.net:Discord_Bot_Gang/Discord-Radio-Bot into V3_Better_Logging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import configparser
|
||||
import logging
|
||||
import os
|
||||
from datetime import date
|
||||
from os.path import exists
|
||||
from NoiseGatev2 import AudioStream
|
||||
@@ -221,7 +222,9 @@ def init_global_logger(_verbose_level: str = "WARNING"):
|
||||
init_logger.setLevel(logging.DEBUG)
|
||||
|
||||
# create file handler which logs even debug messages
|
||||
fh = logging.FileHandler(f'DRB-{date.today()}.log')
|
||||
if not exists("./logs/"):
|
||||
os.mkdir("./logs/")
|
||||
fh = logging.FileHandler(f'./logs/DRB-{date.today()}.log')
|
||||
fh.setLevel(logging.DEBUG)
|
||||
|
||||
# create console handler with a higher log level
|
||||
|
||||
@@ -9,6 +9,8 @@ voice_connection = None
|
||||
|
||||
LOGGER = logging.getLogger("Discord_Radio_Bot.NoiseGateV2")
|
||||
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
class AudioStream:
|
||||
def __init__(self, _channels: int = 2, _sample_rate: int = 48000, _frames_per_buffer: int = 1024,
|
||||
_input_device_index: int = None, _output_device_index: int = None, _input: bool = True,
|
||||
@@ -27,14 +29,14 @@ class AudioStream:
|
||||
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
f" Reinitialize with '_input=True'")
|
||||
f" Reinitialize with '_input=True'")
|
||||
|
||||
if _output_device_index:
|
||||
if _output:
|
||||
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
f" Reinitialize with '_output=True'")
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
if _init_on_startup:
|
||||
# Init PyAudio instance
|
||||
@@ -56,14 +58,14 @@ class AudioStream:
|
||||
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
f" Reinitialize with '_input=True'")
|
||||
f" Reinitialize with '_input=True'")
|
||||
|
||||
if _new_output_device_index:
|
||||
if self.paInstance_kwargs['output']:
|
||||
self.paInstance_kwargs['output_device_index'] = _new_output_device_index
|
||||
else:
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
f" Reinitialize with '_output=True'")
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
self.close_if_open()
|
||||
|
||||
@@ -108,6 +110,7 @@ class AudioStream:
|
||||
self.paInstance.terminate()
|
||||
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
class NoiseGate(AudioStream):
|
||||
def __init__(self, _voice_connection, _noise_gate_threshold: int, **kwargs):
|
||||
super(NoiseGate, self).__init__(_init_on_startup=True, **kwargs)
|
||||
@@ -119,22 +122,27 @@ class NoiseGate(AudioStream):
|
||||
def run(self) -> None:
|
||||
global voice_connection
|
||||
# Start the audio stream
|
||||
LOGGER.debug(f"Starting stream")
|
||||
self.stream.start_stream()
|
||||
LOGGER.debug(f"Playing stream to discord")
|
||||
voice_connection.play(self.NGStream)
|
||||
|
||||
async def close(self):
|
||||
LOGGER.debug(f"Closing")
|
||||
await voice_connection.disconnect()
|
||||
if self.stream.is_active:
|
||||
self.stream.stop_stream()
|
||||
LOGGER.debug(f"Stopping stream")
|
||||
|
||||
|
||||
# noinspection PyUnresolvedReferences
|
||||
class NoiseGateStream(discord.AudioSource):
|
||||
def __init__(self, _stream):
|
||||
super(NoiseGateStream, self).__init__()
|
||||
self.stream = _stream # The actual audio stream object
|
||||
self.NG_fadeout = 480/20 # Fadeout value used to hold the noisegate after de-triggering
|
||||
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 in order to limit the prints
|
||||
self.process_set_count = 0 # Counts how many processes have been made
|
||||
|
||||
def read(self):
|
||||
try:
|
||||
@@ -159,6 +167,9 @@ class NoiseGateStream(discord.AudioSource):
|
||||
LOGGER.debug(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
||||
self.process_set_count += 1
|
||||
if curr_buffer:
|
||||
if self.NG_fadeout_count == 0:
|
||||
voice_connection.stop()
|
||||
LOGGER.debug(f"Fadeout concluded, stopping voice")
|
||||
return bytes(curr_buffer)
|
||||
|
||||
except OSError as e:
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import logging
|
||||
import shutil
|
||||
import threading
|
||||
import subprocess
|
||||
import asyncio
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
@@ -39,7 +37,8 @@ class OP25Handler(threading.Thread):
|
||||
|
||||
time.sleep(.5)
|
||||
|
||||
def set_op25_parameters(self, _frequency: str = False, _http_enabled: bool = True, _start: bool = False, _stop: bool = False):
|
||||
def set_op25_parameters(self, _frequency: str = False, _http_enabled: bool = True, _start: bool = False,
|
||||
_stop: bool = False):
|
||||
if _frequency:
|
||||
self.Frequency = _frequency
|
||||
|
||||
@@ -62,7 +61,7 @@ class OP25Handler(threading.Thread):
|
||||
self.logger.info(f"Starting OP25")
|
||||
# Change the interpreter's working directory (idr why)
|
||||
if self.HTTP_ENABLED:
|
||||
p25_kwargs.extend(["-l", "http:0.0.0.0:8080"])
|
||||
p25_kwargs.extend(["-v", "5", "-l", "http:0.0.0.0:8080"])
|
||||
|
||||
self.logger.debug(f"OP25 Keyword Args: {p25_kwargs}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user