From 3e18edf3f8313baec804460ba7bff435c768af68 Mon Sep 17 00:00:00 2001 From: felix920506 Date: Sun, 22 Sep 2024 22:32:42 -0400 Subject: [PATCH 01/11] add played_seconds function --- discord/player.py | 6 ++++++ discord/voice_client.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/discord/player.py b/discord/player.py index 508bf906ae..4585152fed 100644 --- a/discord/player.py +++ b/discord/player.py @@ -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: int = 0 if after is not None and not callable(after): raise TypeError('Expected a callable for the "after" parameter.') @@ -755,6 +756,8 @@ def _do_run(self) -> None: self._start = time.perf_counter() self.loops += 1 + self._played_frames += 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 @@ -834,3 +837,6 @@ def _speak(self, speaking: bool) -> None: ) except Exception as e: _log.info("Speaking call in player failed: %s", e) + + def played_seconds(self) -> int: + return self._played_frames // 50 diff --git a/discord/voice_client.py b/discord/voice_client.py index b9d4766883..9fd5431bfd 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -988,3 +988,10 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: ) self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295) + + def played_seconds(self) -> int: + """Gets the elapsed time of the playing audio in seconds. Returns 0 if not playing anything.""" + if self._player: + return self._player.played_seconds() + else: + return 0 From 271cf989d531fb2bef550ca7dc7ecaf4ab74a13c Mon Sep 17 00:00:00 2001 From: felix920506 Date: Sun, 22 Sep 2024 23:00:17 -0400 Subject: [PATCH 02/11] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f74299ba1..2878e4d4dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `played_seconds` function to `VoiceClient` to get elapsed time of the + playing audio file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) ### Fixed From 5c995f3c8a1bf4a3ab0033216609ead3fe398a3c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 03:00:41 +0000 Subject: [PATCH 03/11] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2878e4d4dc..c2b00de0c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +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 `played_seconds` function to `VoiceClient` to get elapsed time of the - playing audio file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) +- Added `played_seconds` function to `VoiceClient` to get elapsed time of the playing + audio file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) ### Fixed From 7cfe137ab9869e963b625a788c1256c714659482 Mon Sep 17 00:00:00 2001 From: felix920506 Date: Sun, 22 Sep 2024 23:52:26 -0400 Subject: [PATCH 04/11] use datetime.timedelta instead of int --- discord/player.py | 4 ++-- discord/voice_client.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/discord/player.py b/discord/player.py index 4585152fed..d1f76f36a3 100644 --- a/discord/player.py +++ b/discord/player.py @@ -838,5 +838,5 @@ def _speak(self, speaking: bool) -> None: except Exception as e: _log.info("Speaking call in player failed: %s", e) - def played_seconds(self) -> int: - return self._played_frames // 50 + def played_frames(self) -> int: + return self._played_frames diff --git a/discord/voice_client.py b/discord/voice_client.py index 9fd5431bfd..7c3572a40d 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -46,6 +46,7 @@ import struct import threading import time +import datetime from typing import TYPE_CHECKING, Any, Callable, Literal, overload from . import opus, utils @@ -989,9 +990,9 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295) - def played_seconds(self) -> int: - """Gets the elapsed time of the playing audio in seconds. Returns 0 if not playing anything.""" + def played_seconds(self) -> datetime.timedelta: + """Gets the elapsed time of the playing audio. Returns 0 if not playing anything.""" if self._player: - return self._player.played_seconds() + return datetime.timedelta(milliseconds=self._player.played_frames()*20) else: - return 0 + return datetime.timedelta() From 23a0438d0d4383c6599c1ce215f7d7193c3d98b3 Mon Sep 17 00:00:00 2001 From: felix920506 Date: Sun, 22 Sep 2024 23:54:07 -0400 Subject: [PATCH 05/11] add comment --- discord/player.py | 1 + 1 file changed, 1 insertion(+) diff --git a/discord/player.py b/discord/player.py index d1f76f36a3..e51d87dd90 100644 --- a/discord/player.py +++ b/discord/player.py @@ -839,4 +839,5 @@ def _speak(self, speaking: bool) -> None: _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 From 9bbb9167ec6446c88586ccb9036ce538dcc44737 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 03:54:30 +0000 Subject: [PATCH 06/11] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/voice_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/discord/voice_client.py b/discord/voice_client.py index 7c3572a40d..26189f3197 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -40,13 +40,13 @@ from __future__ import annotations import asyncio +import datetime import logging import select import socket import struct import threading import time -import datetime from typing import TYPE_CHECKING, Any, Callable, Literal, overload from . import opus, utils @@ -993,6 +993,6 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: def played_seconds(self) -> datetime.timedelta: """Gets the elapsed time of the playing audio. Returns 0 if not playing anything.""" if self._player: - return datetime.timedelta(milliseconds=self._player.played_frames()*20) + return datetime.timedelta(milliseconds=self._player.played_frames() * 20) else: return datetime.timedelta() From 27aaac1ee1d6e5e3df75c7d589b3686382a6b4ca Mon Sep 17 00:00:00 2001 From: felix920506 Date: Mon, 23 Sep 2024 00:02:57 -0400 Subject: [PATCH 07/11] rename function to reflect change --- CHANGELOG.md | 2 +- discord/voice_client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2b00de0c2..211572924b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ 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 `played_seconds` function to `VoiceClient` to get elapsed time of the playing +- Added `played_time` function to `VoiceClient` to get elapsed time of the playing audio file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) ### Fixed diff --git a/discord/voice_client.py b/discord/voice_client.py index 7c3572a40d..6f4119ec5b 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -990,7 +990,7 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295) - def played_seconds(self) -> datetime.timedelta: + def played_time(self) -> datetime.timedelta: """Gets the elapsed time of the playing audio. Returns 0 if not playing anything.""" if self._player: return datetime.timedelta(milliseconds=self._player.played_frames()*20) From dba7f493b20cdbc56ecc9c7f5d84ad7f23b62d22 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 04:03:31 +0000 Subject: [PATCH 08/11] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 211572924b..cc6ebff3b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +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 `played_time` function to `VoiceClient` to get elapsed time of the playing - audio file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) +- Added `played_time` function to `VoiceClient` to get elapsed time of the playing audio + file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) ### Fixed From 526cb95e351659619a3cb10170884b107dd4fd75 Mon Sep 17 00:00:00 2001 From: felix920506 Date: Fri, 4 Oct 2024 12:06:43 -0400 Subject: [PATCH 09/11] don't use different counter --- discord/player.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/discord/player.py b/discord/player.py index e51d87dd90..9868aabe70 100644 --- a/discord/player.py +++ b/discord/player.py @@ -724,7 +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: int = 0 + self._played_frames_offset: int = 0 if after is not None and not callable(after): raise TypeError('Expected a callable for the "after" parameter.') @@ -752,11 +752,11 @@ 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 - self._played_frames += 1 # Send the data read from the start of the function if it is not None if first_data is not None: @@ -812,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() @@ -840,4 +841,4 @@ def _speak(self, speaking: bool) -> None: def played_frames(self) -> int: """Gets the number of 20ms frames played since the start of the audio file.""" - return self._played_frames + return self._played_frames_offset + self.loops From 605a50342c27343bd98071d8a5977dd8289812dd Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 11 Oct 2024 08:23:38 +0300 Subject: [PATCH 10/11] Update discord/voice_client.py Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- discord/voice_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/voice_client.py b/discord/voice_client.py index b38a4bb1c4..8673949f0b 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -991,7 +991,7 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295) def played_time(self) -> datetime.timedelta: - """Gets the elapsed time of the playing audio. Returns 0 if not playing anything.""" + """Returns the elapsed time of the playing audio.""" if self._player: return datetime.timedelta(milliseconds=self._player.played_frames() * 20) else: From ca5b54e72e4e1186dd905f0ed06db4c9e9fbdc80 Mon Sep 17 00:00:00 2001 From: felix920506 Date: Fri, 11 Oct 2024 01:40:23 -0400 Subject: [PATCH 11/11] Apply suggestions from code review Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: felix920506 --- CHANGELOG.md | 4 ++-- discord/voice_client.py | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b253d13756..4906fd851e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +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 `played_time` function to `VoiceClient` to get elapsed time of the playing audio - file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/)) +- 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)) diff --git a/discord/voice_client.py b/discord/voice_client.py index 8673949f0b..4ba571c9a7 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -990,9 +990,8 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None: self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295) - def played_time(self) -> datetime.timedelta: + 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) - else: - return datetime.timedelta() + return datetime.timedelta()