Update voice code to vws V4

- Update internals to be compatible with v4
- Adds multiple encryption mode support.  Previously only `xsalsa20_poly1305` was supported.  Now `xsalsa20_poly1305_suffix` is also supported.
  Note: There is no (nice) way to manually select a mode.  The user needn't worry about this however.
- Fixed speaking state bug.  When you disconnected from a voice channel while a bot was playing, upon reconnect you would be unable to hear the bot.  This was caused by bots not sending their speaking state while transmitting.  Bots will now set their speaking state properly when transmitting.  
  Note: This does not account for sending actual silence, the speaking indicator will still be active.
This commit is contained in:
Imayhaveborkedit
2019-01-16 01:35:31 -05:00
committed by Rapptz
parent 883ae8fe80
commit 9c5259afd7
4 changed files with 86 additions and 24 deletions

View File

@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
import threading
import subprocess
import audioop
import asyncio
import logging
import shlex
import time
@ -261,6 +262,7 @@ class AudioPlayer(threading.Thread):
# getattr lookup speed ups
play_audio = self.client.send_audio_packet
self._speak(True)
while not self._end.is_set():
# are we paused?
@ -309,14 +311,19 @@ class AudioPlayer(threading.Thread):
def stop(self):
self._end.set()
self._resumed.set()
self._speak(False)
def pause(self):
def pause(self, *, update_speaking=True):
self._resumed.clear()
if update_speaking:
self._speak(False)
def resume(self):
def resume(self, *, update_speaking=True):
self.loops = 0
self._start = time.time()
self._resumed.set()
if update_speaking:
self._speak(True)
def is_playing(self):
return self._resumed.is_set() and not self._end.is_set()
@ -326,6 +333,12 @@ class AudioPlayer(threading.Thread):
def _set_source(self, source):
with self._lock:
self.pause()
self.pause(update_speaking=False)
self.source = source
self.resume()
self.resume(update_speaking=False)
def _speak(self, speaking):
try:
asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.loop)
except Exception as e:
log.info("Speaking call in player failed: %s", e)