From 498e35c7783589ff21b74559f44b8ab5d4193510 Mon Sep 17 00:00:00 2001 From: David Hozic Date: Sun, 23 Jul 2023 12:43:48 +0200 Subject: [PATCH 01/11] Replace audioop --- discord/player.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/discord/player.py b/discord/player.py index 87b0f5718a..aae3db78bd 100644 --- a/discord/player.py +++ b/discord/player.py @@ -25,7 +25,6 @@ from __future__ import annotations import asyncio -import audioop import io import json import logging @@ -36,6 +35,8 @@ import threading import time import traceback +from math import floor +from itertools import chain from typing import IO, TYPE_CHECKING, Any, Callable, Generic, TypeVar from .errors import ClientException @@ -691,8 +692,16 @@ def cleanup(self) -> None: self.original.cleanup() def read(self) -> bytes: + maxval = 0x7FFF + minval = -0x8000 + ret = self.original.read() - return audioop.mul(ret, 2, min(self._volume, 2.0)) + samples = bytes(chain.from_iterable( + int(floor(min(maxval, max(int.from_bytes(ret[i * 2: (i + 1) * 2], "little", signed=True) * min(self._volume, 2.0), minval)))) + .to_bytes(2, "little", signed=True) + for i in range(len(ret) // 2) + )) + return samples class AudioPlayer(threading.Thread): From d82d0d3073f80655c4c01b906558cd3d9bec2a59 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Jul 2023 10:49:03 +0000 Subject: [PATCH 02/11] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/player.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/discord/player.py b/discord/player.py index 5d57c07025..996559b097 100644 --- a/discord/player.py +++ b/discord/player.py @@ -35,8 +35,8 @@ import threading import time import traceback -from math import floor from itertools import chain +from math import floor from typing import IO, TYPE_CHECKING, Any, Callable, Generic, TypeVar from .errors import ClientException @@ -701,11 +701,25 @@ def read(self) -> bytes: minval = -0x8000 ret = self.original.read() - samples = bytes(chain.from_iterable( - int(floor(min(maxval, max(int.from_bytes(ret[i * 2: (i + 1) * 2], "little", signed=True) * min(self._volume, 2.0), minval)))) - .to_bytes(2, "little", signed=True) - for i in range(len(ret) // 2) - )) + samples = bytes( + chain.from_iterable( + int( + floor( + min( + maxval, + max( + int.from_bytes( + ret[i * 2 : (i + 1) * 2], "little", signed=True + ) + * min(self._volume, 2.0), + minval, + ), + ) + ) + ).to_bytes(2, "little", signed=True) + for i in range(len(ret) // 2) + ) + ) return samples From 9d07e175a97d1c67328e81f4a10b85cae980b597 Mon Sep 17 00:00:00 2001 From: David Hozic Date: Sun, 23 Jul 2023 12:49:12 +0200 Subject: [PATCH 03/11] versionchanged --- discord/player.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/discord/player.py b/discord/player.py index aae3db78bd..5d57c07025 100644 --- a/discord/player.py +++ b/discord/player.py @@ -653,6 +653,11 @@ class PCMVolumeTransformer(AudioSource, Generic[AT]): This does not work on audio sources that have :meth:`AudioSource.is_opus` set to ``True``. + .. versionchanged:: 2.5 + + Replaced audioop implementation of :py:meth:`discord.PCMVolumeTransformer.read` + method with a pure Python equivalent. + Parameters ---------- original: :class:`AudioSource` From 11bfdbcfc63e033d9cc27d27681dd50d27c24ef3 Mon Sep 17 00:00:00 2001 From: David Hozic Date: Sun, 23 Jul 2023 12:51:48 +0200 Subject: [PATCH 04/11] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a22982689d..54a6b5be6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,7 +74,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2138](https://github.com/Pycord-Development/pycord/pull/2138)) ### Changed - +- Replaced audioop (deprecated module) implementation of `PCMVolumeTransformer.read` + method with a pure Python equivalent. ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) - Suppressed FFMPEG output when recording voice channels. ([#1993](https://github.com/Pycord-Development/pycord/pull/1993)) - Changed file-upload size limit from 8 MB to 25 MB accordingly. From fb785c2142a2b9fb0c92d47602f0cc1dad12dbc5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Jul 2023 10:51:38 +0000 Subject: [PATCH 05/11] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54a6b5be6c..5f30450f25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,8 +74,10 @@ These changes are available on the `master` branch, but have not yet been releas ([#2138](https://github.com/Pycord-Development/pycord/pull/2138)) ### Changed + - Replaced audioop (deprecated module) implementation of `PCMVolumeTransformer.read` - method with a pure Python equivalent. ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) + method with a pure Python equivalent. + ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) - Suppressed FFMPEG output when recording voice channels. ([#1993](https://github.com/Pycord-Development/pycord/pull/1993)) - Changed file-upload size limit from 8 MB to 25 MB accordingly. From 6a1a901748c890a325984a55da4ff3fd6ad293fd Mon Sep 17 00:00:00 2001 From: David Hozic Date: Thu, 27 Jul 2023 19:24:43 +0200 Subject: [PATCH 06/11] speed --- discord/player.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/discord/player.py b/discord/player.py index 996559b097..931c2382a2 100644 --- a/discord/player.py +++ b/discord/player.py @@ -35,7 +35,8 @@ import threading import time import traceback -from itertools import chain +import array + from math import floor from typing import IO, TYPE_CHECKING, Any, Callable, Generic, TypeVar @@ -701,25 +702,9 @@ def read(self) -> bytes: minval = -0x8000 ret = self.original.read() - samples = bytes( - chain.from_iterable( - int( - floor( - min( - maxval, - max( - int.from_bytes( - ret[i * 2 : (i + 1) * 2], "little", signed=True - ) - * min(self._volume, 2.0), - minval, - ), - ) - ) - ).to_bytes(2, "little", signed=True) - for i in range(len(ret) // 2) - ) - ) + samples = array.array("h") + samples.frombytes(ret) + samples = array.array('h', map(lambda sample: int(floor(min(maxval, max(sample * min(self._volume, 2.0), minval)))), samples)).tobytes() return samples From 9f77d87d35fd24bf9c74d74ef5bc9e1939363989 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Jul 2023 17:25:18 +0000 Subject: [PATCH 07/11] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/player.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/discord/player.py b/discord/player.py index 931c2382a2..cabbe59cbf 100644 --- a/discord/player.py +++ b/discord/player.py @@ -24,6 +24,7 @@ """ from __future__ import annotations +import array import asyncio import io import json @@ -35,8 +36,6 @@ import threading import time import traceback -import array - from math import floor from typing import IO, TYPE_CHECKING, Any, Callable, Generic, TypeVar @@ -704,7 +703,15 @@ def read(self) -> bytes: ret = self.original.read() samples = array.array("h") samples.frombytes(ret) - samples = array.array('h', map(lambda sample: int(floor(min(maxval, max(sample * min(self._volume, 2.0), minval)))), samples)).tobytes() + samples = array.array( + "h", + map( + lambda sample: int( + floor(min(maxval, max(sample * min(self._volume, 2.0), minval))) + ), + samples, + ), + ).tobytes() return samples From 2568ff9ee762dd51a47a49699ce9e1a90ab4616a Mon Sep 17 00:00:00 2001 From: David Hozic Date: Thu, 27 Jul 2023 22:48:51 +0200 Subject: [PATCH 08/11] changelog --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f30450f25..dc5cda8b6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,9 +75,6 @@ These changes are available on the `master` branch, but have not yet been releas ### Changed -- Replaced audioop (deprecated module) implementation of `PCMVolumeTransformer.read` - method with a pure Python equivalent. - ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) - Suppressed FFMPEG output when recording voice channels. ([#1993](https://github.com/Pycord-Development/pycord/pull/1993)) - Changed file-upload size limit from 8 MB to 25 MB accordingly. @@ -99,6 +96,9 @@ These changes are available on the `master` branch, but have not yet been releas ([#2099](https://github.com/Pycord-Development/pycord/pull/2099)) - Replace `orjson` support with `msgspec` support. ([#2170](https://github.com/Pycord-Development/pycord/pull/2170)) +- Replaced audioop (deprecated module) implementation of `PCMVolumeTransformer.read` + method with a pure Python equivalent. + ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) ### Removed From 7527bcd272f9e39fcddc64f3749e7729b78af6e2 Mon Sep 17 00:00:00 2001 From: David Hozic Date: Fri, 28 Jul 2023 10:01:38 +0200 Subject: [PATCH 09/11] Optimization (1 ms) --- discord/player.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/discord/player.py b/discord/player.py index cabbe59cbf..cc42496868 100644 --- a/discord/player.py +++ b/discord/player.py @@ -700,19 +700,14 @@ def read(self) -> bytes: maxval = 0x7FFF minval = -0x8000 + volume = min(self._volume, 2.0) ret = self.original.read() samples = array.array("h") samples.frombytes(ret) - samples = array.array( - "h", - map( - lambda sample: int( - floor(min(maxval, max(sample * min(self._volume, 2.0), minval))) - ), - samples, - ), - ).tobytes() - return samples + for i in range(len(samples)): + samples[i] = int(floor(min(maxval, max(samples[i] * volume, minval)))) + + return samples.tobytes() class AudioPlayer(threading.Thread): From aefca17d0d68bfc10c898d50e7302e96a872760d Mon Sep 17 00:00:00 2001 From: David Hozic Date: Sun, 31 Dec 2023 14:51:29 +0100 Subject: [PATCH 10/11] Update discord/player.py Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: David Hozic --- discord/player.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/discord/player.py b/discord/player.py index cc42496868..30094fb5a3 100644 --- a/discord/player.py +++ b/discord/player.py @@ -653,11 +653,6 @@ class PCMVolumeTransformer(AudioSource, Generic[AT]): This does not work on audio sources that have :meth:`AudioSource.is_opus` set to ``True``. - .. versionchanged:: 2.5 - - Replaced audioop implementation of :py:meth:`discord.PCMVolumeTransformer.read` - method with a pure Python equivalent. - Parameters ---------- original: :class:`AudioSource` From 7e91341769adb01918206c5ee550d9bab90daa5a Mon Sep 17 00:00:00 2001 From: David Hozic Date: Wed, 16 Oct 2024 19:30:01 +0200 Subject: [PATCH 11/11] Update CHANGELOG.md Co-authored-by: Lala Sabathil Signed-off-by: David Hozic --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 732a99a48e..3405068a18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,8 +52,6 @@ These changes are available on the `master` branch, but have not yet been releas ([#2496](https://github.com/Pycord-Development/pycord/pull/2496)) - ⚠️ **Removed support for Python 3.8.** ([#2521](https://github.com/Pycord-Development/pycord/pull/2521)) -- Changed the error message that appears when attempting to add a subcommand group to a - subcommand group. ([#2275](https://github.com/Pycord-Development/pycord/pull/2275)) - Replaced audioop (deprecated module) implementation of `PCMVolumeTransformer.read` method with a pure Python equivalent. ([#2176](https://github.com/Pycord-Development/pycord/pull/2176))