diff --git a/ultratrace2/model/files/loaders/flac.py b/ultratrace2/model/files/loaders/flac.py index d307ac9..d41ba5f 100644 --- a/ultratrace2/model/files/loaders/flac.py +++ b/ultratrace2/model/files/loaders/flac.py @@ -1,32 +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 diff --git a/ultratrace2/model/files/loaders/mp3.py b/ultratrace2/model/files/loaders/mp3.py index 3187dd2..8e6a537 100644 --- a/ultratrace2/model/files/loaders/mp3.py +++ b/ultratrace2/model/files/loaders/mp3.py @@ -1,32 +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 diff --git a/ultratrace2/model/files/loaders/ogg.py b/ultratrace2/model/files/loaders/ogg.py index 15d57ac..80bcdb6 100644 --- a/ultratrace2/model/files/loaders/ogg.py +++ b/ultratrace2/model/files/loaders/ogg.py @@ -1,32 +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 diff --git a/ultratrace2/model/files/loaders/pydub.py b/ultratrace2/model/files/loaders/pydub.py new file mode 100644 index 0000000..470878c --- /dev/null +++ b/ultratrace2/model/files/loaders/pydub.py @@ -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 diff --git a/ultratrace2/model/files/loaders/wav.py b/ultratrace2/model/files/loaders/wav.py index 74ea52a..1dcdce6 100644 --- a/ultratrace2/model/files/loaders/wav.py +++ b/ultratrace2/model/files/loaders/wav.py @@ -1,32 +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