4242from .sinks import RawData
4343
4444if TYPE_CHECKING :
45+ from discord .voice .client import VoiceClient
46+
4547 T = TypeVar ("T" )
4648 APPLICATION_CTL = Literal ["audio" , "voip" , "lowdelay" ]
4749 BAND_CTL = Literal ["narrow" , "medium" , "wide" , "superwide" , "full" ]
@@ -346,9 +348,9 @@ class OpusError(DiscordException):
346348 The error code returned.
347349 """
348350
349- def __init__ (self , code : int ):
351+ def __init__ (self , code : int = 0 , message : str | None = None ):
350352 self .code : int = code
351- msg = _lib .opus_strerror (self .code ).decode ("utf-8" )
353+ msg = message or _lib .opus_strerror (self .code ).decode ("utf-8" )
352354 _log .info ('"%s" has happened' , msg )
353355 super ().__init__ (msg )
354356
@@ -523,7 +525,7 @@ def _get_last_packet_duration(self):
523525
524526 def decode (self , data , * , fec = False ):
525527 if data is None and fec :
526- raise OpusError ("Invalid arguments: FEC cannot be used with null data" )
528+ raise OpusError (message = "Invalid arguments: FEC cannot be used with null data" )
527529
528530 if data is None :
529531 frame_size = self ._get_last_packet_duration () or self .SAMPLES_PER_FRAME
@@ -545,62 +547,3 @@ def decode(self, data, *, fec=False):
545547 )
546548
547549 return array .array ("h" , pcm [: ret * channel_count ]).tobytes ()
548-
549-
550- class DecodeManager (threading .Thread , _OpusStruct ):
551- def __init__ (self , client : VoiceRecorderClient ):
552- super ().__init__ (daemon = True , name = "DecodeManager" )
553-
554- self .client : VoiceRecorderClient = client
555- self .decode_queue : list [RawData ] = []
556-
557- self .decoder : dict [int , Decoder ] = {}
558-
559- self ._end_thread = threading .Event ()
560-
561- def decode (self , opus_frame : RawData ):
562- if not isinstance (opus_frame , RawData ):
563- raise TypeError ("opus_frame should be a RawData object." )
564- self .decode_queue .append (opus_frame )
565-
566- def run (self ):
567- while not self ._end_thread .is_set ():
568- try :
569- data = self .decode_queue .pop (0 )
570- except IndexError :
571- time .sleep (0.001 )
572- continue
573-
574- try :
575- if data .decrypted_data is None :
576- continue
577- else :
578- data .decoded_data = self .get_decoder (data .ssrc ).decode (
579- data .decrypted_data
580- )
581- except OpusError :
582- _log .exception (
583- "Error occurred while decoding opus frame." , exc_info = True
584- )
585- continue
586-
587- self .client .receive_audio (data )
588-
589- def stop (self ) -> None :
590- while self .decoding :
591- time .sleep (0.1 )
592- self .decoder = {}
593- gc .collect ()
594- _log .debug ("Decoder Process Killed" )
595- self ._end_thread .set ()
596-
597- def get_decoder (self , ssrc : int ) -> Decoder :
598- d = self .decoder .get (ssrc )
599- if d is not None :
600- return d
601- self .decoder [ssrc ] = Decoder ()
602- return self .decoder [ssrc ]
603-
604- @property
605- def decoding (self ) -> bool :
606- return bool (self .decode_queue )
0 commit comments