-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PydubLoader: Add helper class for SoundFileLoader to avoid repetition
There needs to be some class sitting between SoundFileLoader and the concrete children since - each child shares a lot of code with siblings - each child needs the `pydub` external library, which may not exist on all systems See #122 for more context.
- Loading branch information
1 parent
3bbcbf0
commit 109ad30
Showing
5 changed files
with
46 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import pydub # type: ignore | ||
from .pydub import PydubLoader | ||
|
||
from .base import FileLoadError, SoundFileLoader | ||
|
||
|
||
class FLACLoader(SoundFileLoader): | ||
def get_path(self) -> str: | ||
return self._path | ||
|
||
def set_path(self, path) -> None: | ||
self._path = path | ||
|
||
def __init__(self, path: str, audio_segment: pydub.AudioSegment): | ||
self.set_path(path) | ||
self.audio_segment = audio_segment | ||
|
||
def __len__(self) -> int: | ||
return len(self.audio_segment) | ||
|
||
@classmethod | ||
def from_file(cls, path: str) -> "FLACLoader": | ||
try: | ||
audio_segment = pydub.AudioSegment.from_file(path) | ||
return FLACLoader(path, audio_segment) | ||
except Exception as e: | ||
raise FileLoadError( | ||
f"Invalid FLAC ({path}), unable to read: {str(e)}" | ||
) from e | ||
class FLACLoader(PydubLoader): | ||
@staticmethod | ||
def get_priority() -> int: | ||
return 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import pydub # type: ignore | ||
from .pydub import PydubLoader | ||
|
||
from .base import FileLoadError, SoundFileLoader | ||
|
||
|
||
class MP3Loader(SoundFileLoader): | ||
def get_path(self) -> str: | ||
return self._path | ||
|
||
def set_path(self, path) -> None: | ||
self._path = path | ||
|
||
def __init__(self, path: str, audio_segment: pydub.AudioSegment): | ||
self.set_path(path) | ||
self.audio_segment = audio_segment | ||
|
||
def __len__(self) -> int: | ||
return len(self.audio_segment) | ||
|
||
@classmethod | ||
def from_file(cls, path: str) -> "MP3Loader": | ||
try: | ||
audio_segment = pydub.AudioSegment.from_file(path) | ||
return MP3Loader(path, audio_segment) | ||
except Exception as e: | ||
raise FileLoadError( | ||
f"Invalid MP3 ({path}), unable to read: {str(e)}" | ||
) from e | ||
class MP3Loader(PydubLoader): | ||
@staticmethod | ||
def get_priority() -> int: | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import pydub # type: ignore | ||
from .pydub import PydubLoader | ||
|
||
from .base import FileLoadError, SoundFileLoader | ||
|
||
|
||
class OggLoader(SoundFileLoader): | ||
def get_path(self) -> str: | ||
return self._path | ||
|
||
def set_path(self, path) -> None: | ||
self._path = path | ||
|
||
def __init__(self, path: str, audio_segment: pydub.AudioSegment): | ||
self.set_path(path) | ||
self.audio_segment = audio_segment | ||
|
||
def __len__(self) -> int: | ||
return len(self.audio_segment) | ||
|
||
@classmethod | ||
def from_file(cls, path: str) -> "OggLoader": | ||
try: | ||
audio_segment = pydub.AudioSegment.from_file(path) | ||
return OggLoader(path, audio_segment) | ||
except Exception as e: | ||
raise FileLoadError( | ||
f"Invalid Ogg ({path}), unable to read: {str(e)}" | ||
) from e | ||
class OggLoader(PydubLoader): | ||
@staticmethod | ||
def get_priority() -> int: | ||
return 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pydub # type: ignore | ||
|
||
from .base import FileLoadError, SoundFileLoader | ||
|
||
|
||
class PydubLoader(SoundFileLoader): | ||
def get_path(self) -> str: | ||
return self._path | ||
|
||
def set_path(self, path) -> None: | ||
self._path = path | ||
|
||
def __init__(self, path: str, audio_segment: pydub.AudioSegment): | ||
self.set_path(path) | ||
self.audio_segment = audio_segment | ||
|
||
def __len__(self) -> int: | ||
return len(self.audio_segment) | ||
|
||
@classmethod | ||
def from_file(cls, path: str) -> "PydubLoader": | ||
try: | ||
audio_segment = pydub.AudioSegment.from_file(path) | ||
return PydubLoader(path, audio_segment) | ||
except Exception as e: | ||
raise FileLoadError(f"Invalid AudioSegment ({path}), unable to read") from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import pydub # type: ignore | ||
from .pydub import PydubLoader | ||
|
||
from .base import FileLoadError, SoundFileLoader | ||
|
||
|
||
class WAVLoader(SoundFileLoader): | ||
def get_path(self) -> str: | ||
return self._path | ||
|
||
def set_path(self, path) -> None: | ||
self._path = path | ||
|
||
def __init__(self, path: str, audio_segment: pydub.AudioSegment): | ||
self.set_path(path) | ||
self.audio_segment = audio_segment | ||
|
||
def __len__(self) -> int: | ||
return len(self.audio_segment) | ||
|
||
@classmethod | ||
def from_file(cls, path: str) -> "WAVLoader": | ||
try: | ||
audio_segment = pydub.AudioSegment.from_file(path) | ||
return WAVLoader(path, audio_segment) | ||
except Exception as e: | ||
raise FileLoadError( | ||
f"Invalid WAV ({path}), unable to read: {str(e)}" | ||
) from e | ||
class WAVLoader(PydubLoader): | ||
@staticmethod | ||
def get_priority() -> int: | ||
return 3 |