Skip to content

Commit

Permalink
PydubLoader: Add helper class for SoundFileLoader to avoid repetition
Browse files Browse the repository at this point in the history
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
keggsmurph21 committed Dec 31, 2019
1 parent 3bbcbf0 commit 109ad30
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 104 deletions.
31 changes: 5 additions & 26 deletions ultratrace2/model/files/loaders/flac.py
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
31 changes: 5 additions & 26 deletions ultratrace2/model/files/loaders/mp3.py
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
31 changes: 5 additions & 26 deletions ultratrace2/model/files/loaders/ogg.py
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
26 changes: 26 additions & 0 deletions ultratrace2/model/files/loaders/pydub.py
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
31 changes: 5 additions & 26 deletions ultratrace2/model/files/loaders/wav.py
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

0 comments on commit 109ad30

Please sign in to comment.