diff --git a/pytalk/bot.py b/pytalk/bot.py index 64920f8..83abf85 100644 --- a/pytalk/bot.py +++ b/pytalk/bot.py @@ -58,6 +58,7 @@ async def add_server( server: TeamTalkServerInfo | dict[str, Any], reconnect: bool = True, backoff_config: dict[str, Any] | None = None, + enable_muxed_audio: bool = True, ) -> None: """Add a server to the bot. @@ -73,12 +74,18 @@ async def add_server( Can contain keys: `base`, `exponent`, `max_value`, `max_tries`. These settings govern the retry behavior for both the initial connection sequence and for reconnections after a connection loss. + enable_muxed_audio (bool): If `True`, the instance will process and dispatch + `muxed_audio` events. If `False`, these events will be ignored, + reducing CPU overhead for bots that do not need to process mixed audio. + Defaults to `True`. """ if isinstance(server, dict): server = TeamTalkServerInfo.from_dict(server) _log.debug("Adding server: %s, %s", self, server) - tt = TeamTalkInstance(self, server, reconnect, backoff_config) + tt = TeamTalkInstance( + self, server, reconnect, backoff_config, enable_muxed_audio + ) successful_initial_connection = await tt.initial_connect_loop() if not successful_initial_connection: _log.error( diff --git a/pytalk/instance.py b/pytalk/instance.py index 291fbd2..4455bb4 100644 --- a/pytalk/instance.py +++ b/pytalk/instance.py @@ -78,6 +78,7 @@ def __init__( server_info: TeamTalkServerInfo, reconnect: bool = True, backoff_config: dict[str, Any] | None = None, + enable_muxed_audio: bool = True, ) -> None: """Initialize a pytalk.TeamTalkInstance instance. @@ -91,6 +92,10 @@ def __init__( These settings govern the retry behavior for both the initial connection sequence and for reconnections after a connection loss. Defaults to `None` (using default Backoff settings). + enable_muxed_audio (bool): If `True`, the instance will process and dispatch + `muxed_audio` events. If `False`, these events will be ignored, + reducing CPU overhead for bots that do not need to process mixed audio. + Defaults to `True`. """ super().__init__() @@ -106,6 +111,7 @@ def __init__( self._current_input_device_id: int | None = -1 self._audio_sdk_lock = threading.Lock() self.reconnect_enabled = reconnect + self._enable_muxed_audio = enable_muxed_audio if backoff_config: self._backoff = Backoff(**backoff_config) else: @@ -173,6 +179,20 @@ def login(self, join_channel_on_login: bool = True) -> bool: self.bot.dispatch("my_login", self.server) self.logged_in = True + if not self._enable_muxed_audio: + # Disable muxed audio events at the SDK level + sdk._EnableAudioBlockEventEx( + self._tt, + sdk.TT_MUXED_USERID, + sdk.StreamType.STREAMTYPE_VOICE, + None, + False, + ) + _log.info( + "Muxed audio events disabled at SDK level for %s.", + self.server_info.host, + ) + if join_channel_on_login: channel_id_to_join = self.server_info.join_channel_id if channel_id_to_join > 0: # Only join if channel_id is strictly positive @@ -1419,7 +1439,7 @@ async def _process_events(self) -> None: # noqa: C901, PLR0911, PLR0912, PLR091 return if event == sdk.ClientEvent.CLIENTEVENT_CMD_USER_JOINED: user_joined = TeamTalkUser(self, msg.user) - if user_joined.id == super().getMyUserID(): + if user_joined.id == super().getMyUserID() and self._enable_muxed_audio: sdk._EnableAudioBlockEventEx( self._tt, sdk.TT_MUXED_USERID, @@ -1432,7 +1452,7 @@ async def _process_events(self) -> None: # noqa: C901, PLR0911, PLR0912, PLR091 if event == sdk.ClientEvent.CLIENTEVENT_CMD_USER_LEFT: user_left = TeamTalkUser(self, msg.user) channel_left_from = TeamTalkChannel(self, msg.nSource) - if user_left.id == super().getMyUserID(): + if user_left.id == super().getMyUserID() and self._enable_muxed_audio: sdk._EnableAudioBlockEventEx( self._tt, sdk.TT_MUXED_USERID,