New logging
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import audioop
|
||||
import logging
|
||||
import math
|
||||
import pyaudio
|
||||
import discord
|
||||
@@ -6,6 +7,7 @@ import numpy
|
||||
|
||||
voice_connection = None
|
||||
|
||||
LOGGER = logging.getLogger("Discord_Radio_Bot.NoiseGateV2")
|
||||
|
||||
class AudioStream:
|
||||
def __init__(self, _channels: int = 2, _sample_rate: int = 48000, _frames_per_buffer: int = 1024,
|
||||
@@ -24,19 +26,19 @@ class AudioStream:
|
||||
if _input:
|
||||
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
||||
else:
|
||||
print(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
f" Reinitialize with '_input=True'")
|
||||
|
||||
if _output_device_index:
|
||||
if _output:
|
||||
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
||||
else:
|
||||
print(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
if _init_on_startup:
|
||||
# Init PyAudio instance
|
||||
print("Creating PyAudio instance")
|
||||
LOGGER.info("Creating PyAudio instance")
|
||||
self.paInstance = pyaudio.PyAudio()
|
||||
|
||||
# Define and initialize stream object if we have been passed a device ID (pyaudio.open)
|
||||
@@ -44,7 +46,7 @@ class AudioStream:
|
||||
|
||||
if _output_device_index or _input_device_index:
|
||||
if _init_on_startup:
|
||||
print("Init stream")
|
||||
LOGGER.info("Init stream")
|
||||
self.init_stream()
|
||||
|
||||
def init_stream(self, _new_output_device_index: int = None, _new_input_device_index: int = None):
|
||||
@@ -53,14 +55,14 @@ class AudioStream:
|
||||
if self.paInstance_kwargs['input']:
|
||||
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
||||
else:
|
||||
print(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
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:
|
||||
print(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
self.close_if_open()
|
||||
@@ -74,7 +76,7 @@ class AudioStream:
|
||||
if self.stream.is_active():
|
||||
self.stream.stop_stream()
|
||||
self.stream.close()
|
||||
print(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
LOGGER.debug(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
|
||||
def list_devices(self, _display_input_devices: bool = True, _display_output_devices: bool = True):
|
||||
info = self.paInstance.get_host_api_info_by_index(0)
|
||||
@@ -89,13 +91,13 @@ class AudioStream:
|
||||
input_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||
devices['Input'][i] = input_device
|
||||
if _display_input_devices:
|
||||
print("Input Device id ", i, " - ", input_device)
|
||||
LOGGER.debug("Input Device id ", i, " - ", input_device)
|
||||
|
||||
if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0:
|
||||
output_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||
devices['Output'][i] = output_device
|
||||
if _display_output_devices:
|
||||
print("Output Device id ", i, " - ", output_device)
|
||||
LOGGER.debug("Output Device id ", i, " - ", output_device)
|
||||
|
||||
return devices
|
||||
|
||||
@@ -125,6 +127,7 @@ class NoiseGate(AudioStream):
|
||||
if self.stream.is_active:
|
||||
self.stream.stop_stream()
|
||||
|
||||
|
||||
class NoiseGateStream(discord.AudioSource):
|
||||
def __init__(self, _stream):
|
||||
super(NoiseGateStream, self).__init__()
|
||||
@@ -142,7 +145,7 @@ class NoiseGateStream(discord.AudioSource):
|
||||
buffer_decibel = 20 * math.log10(buffer_rms)
|
||||
|
||||
if self.process_set_count % 10 == 0:
|
||||
print(f"{buffer_decibel} db")
|
||||
LOGGER.debug(f"{buffer_decibel} db")
|
||||
|
||||
if buffer_decibel >= self.stream.THRESHOLD:
|
||||
self.NG_fadeout_count = self.NG_fadeout
|
||||
@@ -153,7 +156,7 @@ class NoiseGateStream(discord.AudioSource):
|
||||
else:
|
||||
if self.NG_fadeout_count > 0:
|
||||
self.NG_fadeout_count -= 1
|
||||
print(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
||||
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:
|
||||
@@ -161,6 +164,7 @@ class NoiseGateStream(discord.AudioSource):
|
||||
return bytes(curr_buffer)
|
||||
|
||||
except OSError as e:
|
||||
LOGGER.warning(e)
|
||||
pass
|
||||
|
||||
def audio_datalist_set_volume(self, datalist, volume):
|
||||
|
||||
Reference in New Issue
Block a user