Skip to content

Commit

Permalink
feat: Add function to get elapsed time of playing audio on VoiceClient (
Browse files Browse the repository at this point in the history
#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 <53639936+Dorukyum@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Signed-off-by: felix920506 <felix920506@gmail.com>

---------

Signed-off-by: felix920506 <felix920506@gmail.com>
Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent 1273941 commit 9a83a2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
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()

0 comments on commit 9a83a2a

Please sign in to comment.