Add OpusNotLoaded exception and opus.is_loaded utility function.

This commit is contained in:
Rapptz
2015-12-08 19:37:34 -05:00
parent 9e36047ffb
commit 72f355bb64
3 changed files with 36 additions and 3 deletions

View File

@ -111,13 +111,37 @@ def load_opus(name):
global _lib
_lib = libopus_loader(name)
def is_loaded():
"""Function to check if opus lib is successfully loaded either
via the ``ctypes.util.find_library`` call of :func:`load_opus`.
This must return ``True`` for voice to work.
Returns
-------
bool
Indicates if the opus library has been loaded.
"""
global _lib
return _lib is not None
class OpusError(DiscordException):
"""An exception that is thrown for libopus related errors."""
"""An exception that is thrown for libopus related errors.
Attributes
----------
code : int
The error code returned.
"""
def __init__(self, code):
self.code = code
msg = _lib.opus_strerror(self.code).decode('utf-8')
log.info('"{}" has happened'.format(msg))
super(DiscordException, self).__init__(msg)
super().__init__(msg)
class OpusNotLoaded(DiscordException):
"""An exception that is thrown for when libopus is not loaded."""
pass
# Some constants...
@ -137,6 +161,9 @@ class Encoder:
self.samples_per_frame = int(self.sampling_rate / 1000 * self.frame_length)
self.frame_size = self.samples_per_frame * self.sample_size
if not is_loaded():
raise OpusNotLoaded()
self._state = self._create_state()
def __del__(self):