diff options
| author | Rapptz <[email protected]> | 2015-12-08 19:37:34 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2015-12-08 19:37:34 -0500 |
| commit | 72f355bb64e05f6e9cc848445c026bfbbafca3ae (patch) | |
| tree | 98852912e58047f2b086c4f5276f4906a3bd5abe /discord/opus.py | |
| parent | Explicitly close responses that don't get read. (diff) | |
| download | discord.py-72f355bb64e05f6e9cc848445c026bfbbafca3ae.tar.xz discord.py-72f355bb64e05f6e9cc848445c026bfbbafca3ae.zip | |
Add OpusNotLoaded exception and opus.is_loaded utility function.
Diffstat (limited to 'discord/opus.py')
| -rw-r--r-- | discord/opus.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/discord/opus.py b/discord/opus.py index 68c6a3ec..2f1e2d1d 100644 --- a/discord/opus.py +++ b/discord/opus.py @@ -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): |