Skip to content

Commit

Permalink
Merge branch 'master' into voice_messages
Browse files Browse the repository at this point in the history
  • Loading branch information
plun1331 authored Sep 26, 2024
2 parents 5bec4a8 + a2117ad commit d5551cb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ These changes are available on the `master` branch, but have not yet been releas

### Fixed

- Fix `Enum` options not setting the correct type when only one choice is available.
- Fixed `Enum` options not setting the correct type when only one choice is available.
([#2577](https://github.com/Pycord-Development/pycord/pull/2577))
- Fixed `codec` option for `FFmpegOpusAudio` class to make it in line with
documentation. ([#2581](https://github.com/Pycord-Development/pycord/pull/2581))
- Fixed a possible bug where audio would play too fast at the beginning of audio files.
([#2584](https://github.com/Pycord-Development/pycord/pull/2584))

### Changed

Expand Down
41 changes: 31 additions & 10 deletions discord/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ class FFmpegOpusAudio(FFmpegAudio):
The codec to use to encode the audio data. Normally this would be
just ``libopus``, but is used by :meth:`FFmpegOpusAudio.from_probe` to
opportunistically skip pointlessly re-encoding Opus audio data by passing
``copy`` as the codec value. Any values other than ``copy``, ``opus``, or
``libopus`` will be considered ``libopus``. Defaults to ``libopus``.
``copy`` as the codec value. Any values other than ``copy``, or
``libopus`` will be considered ``libopus``. ``opus`` will also be considered
``libopus`` since the ``opus`` encoder is still in development. Defaults to ``libopus``.
.. warning::
Expand Down Expand Up @@ -407,7 +408,9 @@ def __init__(
args.append("-i")
args.append("-" if pipe else source)

codec = "copy" if codec in ("opus", "libopus") else "libopus"
# use "libopus" when "opus" is specified since the "opus" encoder is incomplete
# link to ffmpeg docs: https://www.ffmpeg.org/ffmpeg-codecs.html#opus
codec = "copy" if codec == "copy" else "libopus"

args.extend(
(
Expand All @@ -417,17 +420,24 @@ def __init__(
"opus",
"-c:a",
codec,
"-ar",
"48000",
"-ac",
"2",
"-b:a",
f"{bitrate}k",
"-loglevel",
"warning",
)
)

# only pass in bitrate, sample rate, channels arguments when actually encoding to avoid ffmpeg warnings
if codec != "copy":
args.extend(
(
"-ar",
"48000",
"-ac",
"2",
"-b:a",
f"{bitrate}k",
)
)

if isinstance(options, str):
args.extend(shlex.split(options))

Expand Down Expand Up @@ -501,6 +511,8 @@ def custom_probe(source, executable):

executable = kwargs.get("executable")
codec, bitrate = await cls.probe(source, method=method, executable=executable)
# only re-encode if the source isn't already opus, else directly copy the source audio stream
codec = "copy" if codec in ("opus", "libopus") else "libopus"
return cls(source, bitrate=bitrate, codec=codec, **kwargs) # type: ignore

@classmethod
Expand Down Expand Up @@ -717,6 +729,9 @@ def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
raise TypeError('Expected a callable for the "after" parameter.')

def _do_run(self) -> None:
# attempt to read first audio segment from source before starting
# some sources can take a few seconds and may cause problems
first_data = self.source.read()
self.loops = 0
self._start = time.perf_counter()

Expand All @@ -740,7 +755,13 @@ def _do_run(self) -> None:
self._start = time.perf_counter()

self.loops += 1
data = self.source.read()
# Send the data read from the start of the function if it is not None
if first_data is not None:
data = first_data
first_data = None
# Else read the next bit from the source
else:
data = self.source.read()

if not data:
self.stop()
Expand Down
4 changes: 2 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-r _.txt
pylint~=3.2.7
pylint~=3.3.1
pytest~=8.3.3
pytest-asyncio~=0.23.8
# pytest-order~=1.0.1
mypy~=1.11.2
coverage~=7.6
pre-commit==3.8.0
codespell==2.3.0
bandit==1.7.9
bandit==1.7.10
flake8==7.1.1
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ furo==2024.8.6
sphinx-autodoc-typehints==2.2.3
sphinx-intl==2.2.0
typing_extensions==4.12.2
levenshtein==0.25.1
levenshtein==0.26.0

0 comments on commit d5551cb

Please sign in to comment.