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

Test sound files #122

Merged
merged 4 commits into from
Dec 31, 2019
Merged
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
4 changes: 3 additions & 1 deletion ultratrace2/model/files/loaders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ def get_width(self) -> int:


class SoundFileLoader(FileLoaderBase):
pass
@abstractmethod
def __len__(self) -> int:
"""Length of file in ms"""
18 changes: 15 additions & 3 deletions ultratrace2/model/files/loaders/flac.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .base import SoundFileLoader
import pydub # type: ignore

from .base import FileLoadError, SoundFileLoader


class FLACLoader(SoundFileLoader):
Expand All @@ -8,9 +10,19 @@ def get_path(self) -> str:
def set_path(self, path) -> None:
self._path = path

def __init__(self, path: str):
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":
raise NotImplementedError()
try:
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
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)}"
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
) from e
18 changes: 15 additions & 3 deletions ultratrace2/model/files/loaders/mp3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .base import SoundFileLoader
import pydub # type: ignore

from .base import FileLoadError, SoundFileLoader


class MP3Loader(SoundFileLoader):
Expand All @@ -8,9 +10,19 @@ def get_path(self) -> str:
def set_path(self, path) -> None:
self._path = path

def __init__(self, path: str):
def __init__(self, path: str, audio_segment: pydub.AudioSegment):
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
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":
raise NotImplementedError()
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
18 changes: 15 additions & 3 deletions ultratrace2/model/files/loaders/ogg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .base import SoundFileLoader
import pydub # type: ignore

from .base import FileLoadError, SoundFileLoader


class OggLoader(SoundFileLoader):
Expand All @@ -8,9 +10,19 @@ def get_path(self) -> str:
def set_path(self, path) -> None:
self._path = path

def __init__(self, path: str):
def __init__(self, path: str, audio_segment: pydub.AudioSegment):
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
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":
raise NotImplementedError()
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
27 changes: 27 additions & 0 deletions ultratrace2/model/files/loaders/tests/test_flac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from ..base import FileLoadError
from ..flac import FLACLoader


@pytest.mark.parametrize(
"path", ["", "/path/to/nowhere", "/dev/null", "/etc/sudoers"],
)
def test_loading_from_invalid_file(path) -> None:
with pytest.raises(FileLoadError):
FLACLoader.from_file(path)


@pytest.mark.parametrize(
"path,duration_ms",
[
("./test-data/example-audio/2L-125_04_stereo.mqa.flac", 97760),
("./test-data/example-audio/2L-125_stereo-176k-24b_04.flac", 97760),
("./test-data/example-audio/2L-125_stereo-44k-16b_04.flac", 97760),
("./test-data/example-audio/2L-125_stereo-88k-24b_04.flac", 97760),
("./test-data/ftyers/20150629171639.flac", 28200),
],
)
def test_loading_from_valid_file(path: str, duration_ms: float) -> None:
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
flac_file = FLACLoader.from_file(path)
assert len(flac_file) == duration_ms
27 changes: 27 additions & 0 deletions ultratrace2/model/files/loaders/tests/test_mp3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from ..base import FileLoadError
from ..mp3 import MP3Loader


@pytest.mark.parametrize(
"path", ["", "/path/to/nowhere", "/dev/null", "/etc/sudoers"],
)
def test_loading_from_invalid_file(path) -> None:
with pytest.raises(FileLoadError):
MP3Loader.from_file(path)


@pytest.mark.parametrize(
"path,duration_ms",
[
("./test-data/example-audio/bachfugue.mp3", 39552),
("./test-data/example-audio/cello82.mp3", 14160),
("./test-data/example-audio/gtr-nylon22.mp3", 5068),
("./test-data/example-audio/harpsi-cs.mp3", 18912),
("./test-data/example-audio/pno-cs.mp3", 20064),
],
)
def test_loading_from_valid_file(path: str, duration_ms: float) -> None:
mp3_file = MP3Loader.from_file(path)
assert len(mp3_file) == duration_ms
26 changes: 26 additions & 0 deletions ultratrace2/model/files/loaders/tests/test_ogg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from ..base import FileLoadError
from ..ogg import OggLoader


@pytest.mark.parametrize(
"path", ["", "/path/to/nowhere", "/dev/null", "/etc/sudoers"],
)
def test_loading_from_invalid_file(path) -> None:
with pytest.raises(FileLoadError):
OggLoader.from_file(path)


@pytest.mark.parametrize(
"path,duration_ms",
[
("./test-data/example-audio/20110815035350_example.ogg", 6519),
("./test-data/example-audio/20110925121709_example.ogg", 10392),
("./test-data/example-audio/example.ogg", 6120),
("./test-data/example-audio/massenet_le_cid.ogg", 261078),
],
)
def test_loading_from_valid_file(path: str, duration_ms: float) -> None:
ogg_file = OggLoader.from_file(path)
assert len(ogg_file) == duration_ms
172 changes: 172 additions & 0 deletions ultratrace2/model/files/loaders/tests/test_wav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import pytest

from ..base import FileLoadError
from ..wav import WAVLoader


@pytest.mark.parametrize(
"path", ["", "/path/to/nowhere", "/dev/null", "/etc/sudoers"],
)
def test_loading_from_invalid_file(path) -> None:
with pytest.raises(FileLoadError):
WAVLoader.from_file(path)


@pytest.mark.parametrize(
"path,duration_ms",
[
("./test-data/example-audio/BabyElephantWalk60.wav", 60000),
("./test-data/example-audio/CantinaBand3.wav", 3000),
("./test-data/example-audio/CantinaBand60.wav", 60000),
("./test-data/example-audio/Fanfare60.wav", 60000),
("./test-data/example-audio/gettysburg10.wav", 10003),
("./test-data/example-audio/gettysburg.wav", 17577),
("./test-data/example-audio/ImperialMarch60.wav", 60000),
("./test-data/example-audio/PinkPanther30.wav", 30000),
("./test-data/example-audio/PinkPanther60.wav", 60000),
("./test-data/example-audio/preamble10.wav", 9599),
("./test-data/example-audio/preamble.wav", 19098),
("./test-data/example-audio/StarWars3.wav", 3000),
("./test-data/example-audio/StarWars60.wav", 60000),
("./test-data/example-audio/taunt.wav", 4099),
("./test-data/qumuq/File001_Track0.wav", 6177),
("./test-data/qumuq/File001_Track1.wav", 6177),
("./test-data/qumuq/File002_Track0.wav", 3657),
("./test-data/qumuq/File002_Track1.wav", 3657),
("./test-data/qumuq/File003_Track0.wav", 4528),
("./test-data/qumuq/File003_Track1.wav", 4528),
("./test-data/qumuq/File004_Track0.wav", 4586),
("./test-data/qumuq/File004_Track1.wav", 4586),
("./test-data/qumuq/File005_Track0.wav", 3529),
("./test-data/qumuq/File005_Track1.wav", 3529),
("./test-data/qumuq/File006_Track0.wav", 3866),
("./test-data/qumuq/File006_Track1.wav", 3866),
("./test-data/qumuq/File007_Track0.wav", 3994),
("./test-data/qumuq/File007_Track1.wav", 3994),
("./test-data/qumuq/File008_Track0.wav", 3878),
("./test-data/qumuq/File008_Track1.wav", 3878),
("./test-data/qumuq/File009_Track0.wav", 3587),
("./test-data/qumuq/File009_Track1.wav", 3587),
("./test-data/qumuq/File010_Track0.wav", 4481),
("./test-data/qumuq/File010_Track1.wav", 4481),
("./test-data/qumuq/File011_Track0.wav", 3878),
("./test-data/qumuq/File011_Track1.wav", 3878),
("./test-data/qumuq/File012_Track0.wav", 3785),
("./test-data/qumuq/File012_Track1.wav", 3785),
("./test-data/qumuq/File013_Track0.wav", 4168),
("./test-data/qumuq/File013_Track1.wav", 4168),
("./test-data/qumuq/File014_Track0.wav", 4005),
("./test-data/qumuq/File014_Track1.wav", 4005),
("./test-data/qumuq/File015_Track0.wav", 3913),
("./test-data/qumuq/File015_Track1.wav", 3913),
("./test-data/qumuq/File016_Track0.wav", 4284),
("./test-data/qumuq/File016_Track1.wav", 4284),
("./test-data/qumuq/File017_Track0.wav", 4319),
("./test-data/qumuq/File017_Track1.wav", 4319),
("./test-data/qumuq/File018_Track0.wav", 4040),
("./test-data/qumuq/File018_Track1.wav", 4040),
("./test-data/qumuq/File019_Track0.wav", 4063),
("./test-data/qumuq/File019_Track1.wav", 4063),
("./test-data/qumuq/File020_Track0.wav", 4040),
("./test-data/qumuq/File020_Track1.wav", 4040),
("./test-data/qumuq/File021_Track0.wav", 3982),
("./test-data/qumuq/File021_Track1.wav", 3982),
("./test-data/qumuq/File022_Track0.wav", 4098),
("./test-data/qumuq/File022_Track1.wav", 4098),
("./test-data/qumuq/File023_Track0.wav", 4249),
("./test-data/qumuq/File023_Track1.wav", 4249),
("./test-data/qumuq/File024_Track0.wav", 4040),
("./test-data/qumuq/File024_Track1.wav", 4040),
("./test-data/qumuq/File025_Track0.wav", 4052),
("./test-data/qumuq/File025_Track1.wav", 4052),
("./test-data/qumuq/File026_Track0.wav", 4830),
("./test-data/qumuq/File026_Track1.wav", 4830),
("./test-data/qumuq/File027_Track0.wav", 4690),
("./test-data/qumuq/File027_Track1.wav", 4690),
("./test-data/qumuq/File028_Track0.wav", 4516),
("./test-data/qumuq/File028_Track1.wav", 4516),
("./test-data/qumuq/File029_Track0.wav", 4458),
("./test-data/qumuq/File029_Track1.wav", 4458),
("./test-data/qumuq/File030_Track0.wav", 3924),
("./test-data/qumuq/File030_Track1.wav", 3924),
("./test-data/qumuq/File031_Track0.wav", 4180),
("./test-data/qumuq/File031_Track1.wav", 4180),
("./test-data/qumuq/File032_Track0.wav", 4528),
("./test-data/qumuq/File032_Track1.wav", 4528),
("./test-data/qumuq/File033_Track0.wav", 4156),
("./test-data/qumuq/File033_Track1.wav", 4156),
("./test-data/qumuq/File034_Track0.wav", 4795),
("./test-data/qumuq/File034_Track1.wav", 4795),
("./test-data/qumuq/File035_Track0.wav", 2705),
("./test-data/qumuq/File035_Track1.wav", 2705),
("./test-data/qumuq/File036_Track0.wav", 2740),
("./test-data/qumuq/File036_Track1.wav", 2740),
("./test-data/qumuq/File037_Track0.wav", 3007),
("./test-data/qumuq/File037_Track1.wav", 3007),
("./test-data/qumuq/File038_Track0.wav", 2937),
("./test-data/qumuq/File038_Track1.wav", 2937),
("./test-data/qumuq/File039_Track0.wav", 3007),
("./test-data/qumuq/File039_Track1.wav", 3007),
("./test-data/qumuq/File040_Track0.wav", 3007),
("./test-data/qumuq/File040_Track1.wav", 3007),
("./test-data/qumuq/File041_Track0.wav", 3286),
("./test-data/qumuq/File041_Track1.wav", 3286),
("./test-data/qumuq/File042_Track0.wav", 3042),
("./test-data/qumuq/File042_Track1.wav", 3042),
("./test-data/qumuq/File043_Track0.wav", 2752),
("./test-data/qumuq/File043_Track1.wav", 2752),
("./test-data/qumuq/File044_Track0.wav", 3204),
("./test-data/qumuq/File044_Track1.wav", 3204),
("./test-data/qumuq/File045_Track0.wav", 2531),
("./test-data/qumuq/File045_Track1.wav", 2531),
("./test-data/qumuq/File046_Track0.wav", 3019),
("./test-data/qumuq/File046_Track1.wav", 3019),
("./test-data/qumuq/File047_Track0.wav", 2786),
("./test-data/qumuq/File047_Track1.wav", 2786),
("./test-data/qumuq/File048_Track0.wav", 3065),
("./test-data/qumuq/File048_Track1.wav", 3065),
("./test-data/qumuq/File049_Track0.wav", 3166),
("./test-data/qumuq/File049_Track1.wav", 3166),
("./test-data/qumuq/File050_Track0.wav", 2937),
("./test-data/qumuq/File050_Track1.wav", 2937),
("./test-data/qumuq/File051_Track0.wav", 3158),
("./test-data/qumuq/File051_Track1.wav", 3158),
("./test-data/qumuq/File052_Track0.wav", 3053),
("./test-data/qumuq/File052_Track1.wav", 3053),
("./test-data/qumuq/File053_Track0.wav", 3587),
("./test-data/qumuq/File053_Track1.wav", 3587),
("./test-data/qumuq/File054_Track0.wav", 3762),
("./test-data/qumuq/File054_Track1.wav", 3762),
("./test-data/qumuq/File055_Track0.wav", 2821),
("./test-data/qumuq/File055_Track1.wav", 2821),
("./test-data/qumuq/File056_Track0.wav", 3123),
("./test-data/qumuq/File056_Track1.wav", 3123),
("./test-data/qumuq/File057_Track0.wav", 2914),
("./test-data/qumuq/File057_Track1.wav", 2914),
("./test-data/qumuq/File058_Track0.wav", 2705),
("./test-data/qumuq/File058_Track1.wav", 2705),
("./test-data/qumuq/File059_Track0.wav", 2763),
("./test-data/qumuq/File059_Track1.wav", 2763),
("./test-data/qumuq/File060_Track0.wav", 3146),
("./test-data/qumuq/File060_Track1.wav", 3146),
("./test-data/qumuq/File061_Track0.wav", 3239),
("./test-data/qumuq/File061_Track1.wav", 3239),
("./test-data/qumuq/File062_Track0.wav", 3251),
("./test-data/qumuq/File062_Track1.wav", 3251),
("./test-data/qumuq/File063_Track0.wav", 3065),
("./test-data/qumuq/File063_Track1.wav", 3065),
("./test-data/qumuq/File064_Track0.wav", 3239),
("./test-data/qumuq/File064_Track1.wav", 3239),
("./test-data/qumuq/File065_Track0.wav", 3007),
("./test-data/qumuq/File065_Track1.wav", 3007),
("./test-data/qumuq/File066_Track0.wav", 3019),
("./test-data/qumuq/File066_Track1.wav", 3019),
("./test-data/qumuq/File067_Track0.wav", 3065),
("./test-data/qumuq/File067_Track1.wav", 3065),
("./test-data/qumuq/File068_Track0.wav", 3390),
("./test-data/qumuq/File068_Track1.wav", 3390),
],
)
def test_loading_from_valid_file(path: str, duration_ms: float) -> None:
wav_file = WAVLoader.from_file(path)
assert len(wav_file) == duration_ms
18 changes: 15 additions & 3 deletions ultratrace2/model/files/loaders/wav.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .base import SoundFileLoader
import pydub # type: ignore

from .base import FileLoadError, SoundFileLoader


class WAVLoader(SoundFileLoader):
Expand All @@ -8,9 +10,19 @@ def get_path(self) -> str:
def set_path(self, path) -> None:
self._path = path

def __init__(self, path: str):
def __init__(self, path: str, audio_segment: pydub.AudioSegment):
keggsmurph21 marked this conversation as resolved.
Show resolved Hide resolved
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":
raise NotImplementedError()
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