Skip to content

Commit 9a83a2a

Browse files
felix920506pre-commit-ci[bot]Dorukyum
authored
feat: Add function to get elapsed time of playing audio on VoiceClient (#2587)
* add played_seconds function * Update CHANGELOG.md * style(pre-commit): auto fixes from pre-commit.com hooks * use datetime.timedelta instead of int * add comment * style(pre-commit): auto fixes from pre-commit.com hooks * rename function to reflect change * style(pre-commit): auto fixes from pre-commit.com hooks * don't use different counter * Update discord/voice_client.py Signed-off-by: Dorukyum <[email protected]> * Apply suggestions from code review Co-authored-by: Dorukyum <[email protected]> Signed-off-by: felix920506 <[email protected]> --------- Signed-off-by: felix920506 <[email protected]> Signed-off-by: Dorukyum <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dorukyum <[email protected]>
1 parent 1273941 commit 9a83a2a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ These changes are available on the `master` branch, but have not yet been releas
2222
`tags`. ([#2520](https://github.com/Pycord-Development/pycord/pull/2520))
2323
- Added `Member.guild_banner` and `Member.display_banner` properties.
2424
([#2556](https://github.com/Pycord-Development/pycord/pull/2556))
25+
- Added `elapsed` method to `VoiceClient`.
26+
([#2587](https://github.com/Pycord-Development/pycord/pull/2587/))
2527
- Added optional `filter` parameter to `utils.basic_autocomplete()`.
2628
([#2590](https://github.com/Pycord-Development/pycord/pull/2590))
2729

discord/player.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
724724
self._current_error: Exception | None = None
725725
self._connected: threading.Event = client._connected
726726
self._lock: threading.Lock = threading.Lock()
727+
self._played_frames_offset: int = 0
727728

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

757759
self.loops += 1
760+
758761
# Send the data read from the start of the function if it is not None
759762
if first_data is not None:
760763
data = first_data
@@ -809,6 +812,7 @@ def pause(self, *, update_speaking: bool = True) -> None:
809812
self._speak(False)
810813

811814
def resume(self, *, update_speaking: bool = True) -> None:
815+
self._played_frames_offset += self.loops
812816
self.loops = 0
813817
self._start = time.perf_counter()
814818
self._resumed.set()
@@ -834,3 +838,7 @@ def _speak(self, speaking: bool) -> None:
834838
)
835839
except Exception as e:
836840
_log.info("Speaking call in player failed: %s", e)
841+
842+
def played_frames(self) -> int:
843+
"""Gets the number of 20ms frames played since the start of the audio file."""
844+
return self._played_frames_offset + self.loops

discord/voice_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from __future__ import annotations
4141

4242
import asyncio
43+
import datetime
4344
import logging
4445
import select
4546
import socket
@@ -988,3 +989,9 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None:
988989
)
989990

990991
self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295)
992+
993+
def elapsed(self) -> datetime.timedelta:
994+
"""Returns the elapsed time of the playing audio."""
995+
if self._player:
996+
return datetime.timedelta(milliseconds=self._player.played_frames() * 20)
997+
return datetime.timedelta()

0 commit comments

Comments
 (0)