Skip to content

Commit fe39e05

Browse files
authored
Hotfix 0.3.3 (#5)
- Fixed properties in audio source classes - Fixed the lack of context manager methods for `URLAudioSource` - Fixed missing `close` method for `URLAudioSource` - Updated attributes of the 'AudioSourceBase`
2 parents ff72354 + e7a9d19 commit fe39e05

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

seaplayer_audio/_types.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,6 @@ def __str__(self) -> str:
7474
def __repr__(self) -> str:
7575
return self.__str__()
7676

77-
"""
78-
class URLOpenRetFile:
79-
url: str
80-
code: int
81-
status: int
82-
closed: bool
83-
chunked: bool
84-
will_close: bool
85-
length: Optional[int]
86-
def read(self, n: int=-1) -> bytes: ...
87-
def read1(self, n: int=-1) -> bytes: ...
88-
def readinto(self, b) -> int: ...
89-
def readinto1(self, b) -> int: ...
90-
def isclosed(self) -> bool: ...
91-
def close(self) -> None: ...
92-
"""
93-
9477
# ! URL Open Types
9578

9679
class URLOpenRetType:

seaplayer_audio/audiosources/fileaudiosource.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# ^ File Audio Source (sync)
1717

1818
class FileAudioSource(AudioSourceBase):
19+
"""A class for reading an audio stream in array format from a file."""
1920
__repr_attrs__ = ('name', ('metadata', True), 'samplerate', 'channels', 'subtype', 'endian', 'format', 'bitrate')
2021

2122
def __init__(
@@ -55,31 +56,41 @@ def __del__(self) -> None:
5556

5657
@property
5758
def samplerate(self) -> AudioSamplerate:
59+
"""The sampling rate of the audio source."""
5860
return self.sfio.samplerate
5961

6062
@property
6163
def channels(self) -> AudioChannels:
64+
"""The number of channels of the audio source."""
6265
return self.sfio.channels
6366

6467
@property
6568
def subtype(self) -> AudioSubType:
69+
"""The type of audio stream packaging."""
6670
return self.sfio.subtype
6771

6872
@property
6973
def endian(self) -> AudioEndians:
74+
"""The type of byte sequence."""
7075
return self.sfio.endian
7176

7277
@property
7378
def format(self) -> AudioFormat:
79+
"""Audio format for storing an audio stream."""
7480
return self.sfio.format
7581

7682
@property
7783
def bitrate(self) -> Optional[int]:
78-
try: return self.minfo.info.bitrate
79-
except: return None
84+
"""The speed of the audio stream in the format of bits per second."""
85+
try:
86+
if self.minfo.info.bitrate is not None:
87+
return self.minfo.info.bitrate
88+
except:
89+
pass
8090

8191
@property
8292
def closed(self) -> bool:
93+
"""Whether the IO will be closed after the context manager is closed."""
8394
return self.sfio.closed
8495

8596
# ^ IO Methods Tests

seaplayer_audio/audiosources/urlaudiosource.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from numpy import ndarray
22
from soundfile import SoundFile
3-
from typing_extensions import Optional
3+
from typing_extensions import Optional, Self
44
from .._types import AudioSamplerate, AudioChannels, AudioSubType, AudioFormat, AudioEndians, AudioDType
55
from ..base import AudioSourceBase
66
from ..functions import get_mutagen_info, get_audio_metadata
77
from .urlio import URLIO
88

99
# ! URL Audio Source Class
1010
class URLAudioSource(AudioSourceBase):
11+
"""A class for reading an audio stream in array format by direct/indirect reference."""
1112
__repr_attrs__ = ('name', ('metadata', True), 'samplerate', 'channels', 'subtype', 'endian', 'format', 'bitrate')
1213

1314
def __init__(
@@ -26,36 +27,60 @@ def __init__(
2627
self.sfio = SoundFile(self.urlio, 'r', samplerate, channels, subtype, endian, format, closefd=closefd)
2728
self.minfo = get_mutagen_info(self.name)
2829
self.metadata = get_audio_metadata(self.sfio, self.minfo)
30+
self.closefd = closefd
31+
32+
# ^ Dander Methods
33+
34+
def __del__(self) -> None:
35+
if self.closefd and (not self.closed):
36+
self.close()
37+
38+
def __enter__(self) -> Self:
39+
return self
40+
41+
def __exit__(self, *args: object) -> None:
42+
if self.closefd and (not self.closed):
43+
self.close()
2944

3045
# ^ Propertyes
3146

3247
@property
3348
def samplerate(self) -> AudioSamplerate:
49+
"""The sampling rate of the audio source."""
3450
return self.sfio.samplerate
3551

3652
@property
3753
def channels(self) -> AudioChannels:
54+
"""The number of channels of the audio source."""
3855
return self.sfio.channels
3956

4057
@property
4158
def subtype(self) -> AudioSubType:
59+
"""The type of audio stream packaging."""
4260
return self.sfio.subtype
4361

4462
@property
4563
def endian(self) -> AudioEndians:
64+
"""The type of byte sequence."""
4665
return self.sfio.endian
4766

4867
@property
4968
def format(self) -> AudioFormat:
69+
"""Audio format for storing an audio stream."""
5070
return self.sfio.format
5171

5272
@property
5373
def bitrate(self) -> Optional[int]:
54-
try: return self.minfo.info.bitrate
55-
except: return None
74+
"""The speed of the audio stream in the format of bits per second."""
75+
try:
76+
if self.minfo.info.bitrate is not None:
77+
return self.minfo.info.bitrate
78+
except:
79+
pass
5680

5781
@property
5882
def closed(self) -> bool:
83+
"""Whether the IO will be closed after the context manager is closed."""
5984
return self.sfio.closed
6085

6186
# ^ IO Check Methods
@@ -126,4 +151,5 @@ def tell(self) -> int:
126151

127152
def close(self) -> None:
128153
"""Close the file. Can be called multiple times."""
129-
return self.sfio.close()
154+
self.urlio.close()
155+
self.sfio.close()

seaplayer_audio/audiosources/urlio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __next__(self) -> NoReturn:
107107
def __enter__(self) -> Self:
108108
return self
109109

110-
def __exit__(self, *args) -> None:
110+
def __exit__(self, *args: object) -> None:
111111
if self.closefd and (not self.closed):
112112
self.close()
113113

seaplayer_audio/base/audiosource.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from io import IOBase
44
from dataclasses import dataclass
55
from typing_extensions import Any, Iterable, Optional, NoReturn, deprecated
6-
from .._types import Reprable
6+
from .._types import (
7+
AudioSamplerate, AudioChannels, AudioSubType, AudioEndians, AudioFormat,
8+
Reprable
9+
)
710

811
# ! Audio Source Types
912

@@ -24,6 +27,18 @@ class AudioSourceMetadata:
2427
class AudioSourceBase(IOBase, Reprable):
2528
"""Base class for working with audio sources (sync)."""
2629

30+
__slots__ = ('samplerate', 'channels', 'subtype', 'endian', 'format')
31+
32+
# ^ Variables
33+
34+
samplerate: AudioSamplerate
35+
channels: AudioChannels
36+
subtype: AudioSubType
37+
endian: AudioEndians
38+
format: AudioFormat
39+
40+
# ^ Methods
41+
2742
@deprecated('!!! NOT IMPLEMENTED !!!')
2843
def __iter__(self) -> NoReturn:
2944
"""!!! NOT IMPLEMENTED !!!"""

0 commit comments

Comments
 (0)