Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add function to get elapsed time of playing audio on VoiceClient #2587

Merged
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ These changes are available on the `master` branch, but have not yet been releas
`tags`. ([#2520](https://github.com/Pycord-Development/pycord/pull/2520))
- Added `Member.guild_banner` and `Member.display_banner` properties.
([#2556](https://github.com/Pycord-Development/pycord/pull/2556))
- Added `elapsed` method to `VoiceClient`.
([#2587](https://github.com/Pycord-Development/pycord/pull/2587/))
- Added optional `filter` parameter to `utils.basic_autocomplete()`.
([#2590](https://github.com/Pycord-Development/pycord/pull/2590))

Expand Down
8 changes: 8 additions & 0 deletions discord/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
self._current_error: Exception | None = None
self._connected: threading.Event = client._connected
self._lock: threading.Lock = threading.Lock()
self._played_frames_offset: int = 0

if after is not None and not callable(after):
raise TypeError('Expected a callable for the "after" parameter.')
Expand Down Expand Up @@ -751,10 +752,12 @@ def _do_run(self) -> None:
# wait until we are connected
self._connected.wait()
# reset our internal data
self._played_frames_offset += self.loops
self.loops = 0
self._start = time.perf_counter()

self.loops += 1

# Send the data read from the start of the function if it is not None
if first_data is not None:
data = first_data
Expand Down Expand Up @@ -809,6 +812,7 @@ def pause(self, *, update_speaking: bool = True) -> None:
self._speak(False)

def resume(self, *, update_speaking: bool = True) -> None:
self._played_frames_offset += self.loops
self.loops = 0
self._start = time.perf_counter()
self._resumed.set()
Expand All @@ -834,3 +838,7 @@ def _speak(self, speaking: bool) -> None:
)
except Exception as e:
_log.info("Speaking call in player failed: %s", e)

def played_frames(self) -> int:
"""Gets the number of 20ms frames played since the start of the audio file."""
return self._played_frames_offset + self.loops
7 changes: 7 additions & 0 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from __future__ import annotations

import asyncio
import datetime
import logging
import select
import socket
Expand Down Expand Up @@ -988,3 +989,9 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None:
)

self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295)

def elapsed(self) -> datetime.timedelta:
"""Returns the elapsed time of the playing audio."""
if self._player:
return datetime.timedelta(milliseconds=self._player.played_frames() * 20)
return datetime.timedelta()