Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pytalk/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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(
Expand Down
24 changes: 22 additions & 2 deletions pytalk/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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__()
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down