mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 12:18:59 +00:00
Add silence padding after transmission breaks
Prevents unwanted interpolation/distortion of audio by sending silence packets after pausing or ending the audio stream.
This commit is contained in:
parent
3f92f35bb1
commit
48b4ea84c9
@ -72,6 +72,8 @@ __all__ = (
|
|||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
OPUS_SILENCE = b'\xF8\xFF\xFE'
|
||||||
|
|
||||||
c_int_ptr = ctypes.POINTER(ctypes.c_int)
|
c_int_ptr = ctypes.POINTER(ctypes.c_int)
|
||||||
c_int16_ptr = ctypes.POINTER(ctypes.c_int16)
|
c_int16_ptr = ctypes.POINTER(ctypes.c_int16)
|
||||||
c_float_ptr = ctypes.POINTER(ctypes.c_float)
|
c_float_ptr = ctypes.POINTER(ctypes.c_float)
|
||||||
|
@ -40,7 +40,7 @@ from typing import Any, Callable, Generic, IO, Optional, TYPE_CHECKING, Tuple, T
|
|||||||
|
|
||||||
from .enums import SpeakingState
|
from .enums import SpeakingState
|
||||||
from .errors import ClientException
|
from .errors import ClientException
|
||||||
from .opus import Encoder as OpusEncoder
|
from .opus import Encoder as OpusEncoder, OPUS_SILENCE
|
||||||
from .oggparse import OggStream
|
from .oggparse import OggStream
|
||||||
from .utils import MISSING
|
from .utils import MISSING
|
||||||
|
|
||||||
@ -720,6 +720,7 @@ class AudioPlayer(threading.Thread):
|
|||||||
while not self._end.is_set():
|
while not self._end.is_set():
|
||||||
# are we paused?
|
# are we paused?
|
||||||
if not self._resumed.is_set():
|
if not self._resumed.is_set():
|
||||||
|
self.send_silence()
|
||||||
# wait until we aren't
|
# wait until we aren't
|
||||||
self._resumed.wait()
|
self._resumed.wait()
|
||||||
continue
|
continue
|
||||||
@ -744,6 +745,8 @@ class AudioPlayer(threading.Thread):
|
|||||||
delay = max(0, self.DELAY + (next_time - time.perf_counter()))
|
delay = max(0, self.DELAY + (next_time - time.perf_counter()))
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
|
self.send_silence()
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
try:
|
try:
|
||||||
self._do_run()
|
self._do_run()
|
||||||
@ -800,3 +803,11 @@ class AudioPlayer(threading.Thread):
|
|||||||
asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.client.loop)
|
asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.client.loop)
|
||||||
except Exception:
|
except Exception:
|
||||||
_log.exception("Speaking call in player failed")
|
_log.exception("Speaking call in player failed")
|
||||||
|
|
||||||
|
def send_silence(self, count: int = 5) -> None:
|
||||||
|
try:
|
||||||
|
for n in range(count):
|
||||||
|
self.client.send_audio_packet(OPUS_SILENCE, encode=False)
|
||||||
|
except Exception:
|
||||||
|
# Any possible error (probably a socket error) is so inconsequential it's not even worth logging
|
||||||
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user