Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sounds setters #395

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
:method:`Twitchio.ext.eventsub.EventSubWSClient.subscribe_channel_unban_request_create <EventSubWSClient.subscribe_channel_unban_request_create>`
- Added :method:`Twitchio.ext.eventsub.EventSubClient.subscribe_channel_unban_request_resolve <EventSubClient.subscribe_channel_unban_request_resolve>` /
:method:`Twitchio.ext.eventsub.EventSubWSClient.subscribe_channel_unban_request_resolve <EventSubWSClient.subscribe_channel_unban_request_resolve>`
- ext.sounds
- Additions
- Added TinyTag as a dependency to support retrieving audio metadata using TinyTag in `ext.sounds.__init__.py`.
- added :method:`Twitchio.ext.sounds.rate setter.
- added :method:`Twitchio.ext.sounds.channels setter.


2.9.2
=======
Expand Down Expand Up @@ -143,6 +149,7 @@
- Bumped ciso8601 from >=2.2,<2.3 to >=2.2,<3
- Bumped cchardet from >=2.1,<2.2 to >=2.1,<3


2.6.0
======
- TwitchIO
Expand Down
6 changes: 5 additions & 1 deletion docs/exts/sounds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ This bot will search YouTube for a relevant video and playback its audio.

**Sound with a Local File:**

This Sound will target a local file on your machine. Just pass the location to source.
This Sound will target a local file on your machine. Pass the location to source. You
may manually set the sample rate and number of channels if needed, however it should
be automatically detected.

.. code-block:: python3

sound = sounds.Sound(source='my_audio.mp3')
sound.channels = 1 # play mono channel
sound.rate = 24_000 # set sample


**Multiple Players:**
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ sphinxext-opengraph
Pygments
furo
pyaudio==0.2.11
yt-dlp>=2022.2.4
yt-dlp>=2022.2.4
tinytag>=1.9.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
sounds = [
"yt-dlp>=2022.2.4",
'pyaudio==0.2.11; platform_system!="Windows"',
'tinytag>=1.9.0',
]
speed = [
"ujson>=5.2,<6",
Expand Down
18 changes: 15 additions & 3 deletions twitchio/ext/sounds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import asyncio
import audioop
import dataclasses
Expand All @@ -33,6 +34,7 @@

import pyaudio
from yt_dlp import YoutubeDL
from tinytag import TinyTag


__all__ = ("Sound", "AudioPlayer")
Expand Down Expand Up @@ -173,6 +175,9 @@ def __init__(

elif isinstance(source, str):
self.title = source
tag = TinyTag.get(source)
self._rate = tag.samplerate
self._channels = tag.channels

self.proc = subprocess.Popen(
[
Expand All @@ -189,9 +194,6 @@ def __init__(
stdout=subprocess.PIPE,
)

self._channels = 2
self._rate = 48000

@classmethod
async def ytdl_search(cls, search: str, *, loop: Optional[asyncio.BaseEventLoop] = None):
"""|coro|
Expand All @@ -216,11 +218,21 @@ def channels(self):
"""The audio source channels."""
return self._channels

@channels.setter
def channels(self, channels: int):
"""Set audio source channels."""
self._channels = channels

@property
def rate(self):
"""The audio source sample rate."""
return self._rate

@rate.setter
def rate(self, rate: int):
"""Set audio source sample rate."""
self._rate = rate

@property
def source(self):
"""The raw audio source."""
Expand Down