From abd78c83d216841c43e2347755d728f579774d97 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Mon, 14 Jul 2025 21:17:09 -0400 Subject: [PATCH] stream blank when no sound --- app/internal/NoiseGatev2.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/app/internal/NoiseGatev2.py b/app/internal/NoiseGatev2.py index bc4142d..c745c15 100644 --- a/app/internal/NoiseGatev2.py +++ b/app/internal/NoiseGatev2.py @@ -6,12 +6,18 @@ from internal.logger import create_logger LOGGER = create_logger(__name__) +# The size of a 20ms, 48kHz, stereo, 16-bit PCM audio frame. +# (960 frames * 2 channels * 2 bytes/sample) +DISCORD_FRAME_SIZE = 3840 +SILENT_FRAME = b'\x00' * DISCORD_FRAME_SIZE + + # noinspection PyUnresolvedReferences class AudioStream: def __init__(self, _channels: int = 2, _sample_rate: int = 48000, _frames_per_buffer: int = 960, _input_device_index: int = None, _output_device_index: int = None, _input: bool = True, _output: bool = True, _init_on_startup: bool = True): - # NOTE: frames_per_buffer changed to 960 to match Discord's 20ms frame size + # Corrected frames_per_buffer to 960 to match Discord's 20ms frame size self.paInstance_kwargs = { 'format': pyaudio.paInt16, 'channels': _channels, @@ -106,19 +112,18 @@ class NoiseGate(AudioStream): def core(self, error=None): if error: LOGGER.warning(f"Audio stream stopped unexpectedly with error: {error}") - return # Avoid recursion on error + return if self.voice_connection.is_connected() and not self.voice_connection.is_playing(): LOGGER.debug("Playing stream to discord") - # The 'after' callback can be prone to loops, simplified here self.voice_connection.play(self.NGStream, after=lambda e: self.core(e)) async def close(self): LOGGER.debug("Closing NoiseGate resources...") if self.voice_connection and self.voice_connection.is_connected(): - self.voice_connection.stop() # Stop sending audio + self.voice_connection.stop() - self.close_if_open() # Close PyAudio stream + self.close_if_open() if self.paInstance: self.paInstance.terminate() @@ -130,21 +135,19 @@ class NoiseGateStream(discord.AudioSource): def __init__(self, noise_gate_instance: NoiseGate): super(NoiseGateStream, self).__init__() self.noise_gate = noise_gate_instance - self.NG_fadeout = 12 # Equivalent to 240ms of audio frames (240 / 20ms) + self.NG_fadeout = 12 self.NG_fadeout_count = 0 self.process_set_count = 0 def read(self): try: - # Check connection status via the parent NoiseGate instance if not self.noise_gate.voice_connection.is_connected(): - return b'' + return SILENT_FRAME - # Read from the PyAudio stream, also via the parent instance curr_buffer = self.noise_gate.stream.read(960, exception_on_overflow=False) - if not curr_buffer: - return b'' + if len(curr_buffer) != DISCORD_FRAME_SIZE: + return SILENT_FRAME buffer_rms = audioop.rms(curr_buffer, 2) @@ -165,11 +168,11 @@ class NoiseGateStream(discord.AudioSource): self.process_set_count += 1 return bytes(curr_buffer) - return b'' # Return silence if below threshold and not in fadeout + return SILENT_FRAME except IOError as e: LOGGER.error(f"PyAudio IOError in read(): {e}") - return b'' # Return silence to not break the Discord audio pump + return SILENT_FRAME except Exception as e: LOGGER.error(f"Unhandled exception in NoiseGateStream.read: {e}", exc_info=True) - return b'' \ No newline at end of file + return SILENT_FRAME \ No newline at end of file