From: Paul Brossier Date: Sun, 12 Jan 2014 02:33:07 +0000 (-0400) Subject: python/lib/aubio/slicing.py: error checking, add more tests, use get_default_sound X-Git-Tag: 0.4.1~84 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=aee840b6e69427658b1c80540c08da514e92836c;p=aubio.git python/lib/aubio/slicing.py: error checking, add more tests, use get_default_sound --- diff --git a/python/lib/aubio/slicing.py b/python/lib/aubio/slicing.py index c60e674d..0e031515 100644 --- a/python/lib/aubio/slicing.py +++ b/python/lib/aubio/slicing.py @@ -6,6 +6,12 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end = None, samplerate = 0, hopsize = 256): + if timestamps == None or len(timestamps) == 0: + raise ValueError ("no timestamps given") + + if timestamps_end != None and len(timestamps_end) != len(timestamps): + raise ValueError ("len(timestamps_end) != len(timestamps)") + source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) if output_dir != None: if not os.path.isdir(output_dir): diff --git a/python/tests/sounds/chocolate_1min.wav b/python/tests/sounds/chocolate_1min.wav new file mode 100644 index 00000000..22c1b147 Binary files /dev/null and b/python/tests/sounds/chocolate_1min.wav differ diff --git a/python/tests/test_slicing.py b/python/tests/test_slicing.py index 3c0750e9..497c16ef 100755 --- a/python/tests/test_slicing.py +++ b/python/tests/test_slicing.py @@ -5,6 +5,7 @@ from numpy.testing import assert_equal, assert_almost_equal from aubio import slice_source_at_stamps from utils import count_samples_in_file, count_samples_in_directory +from utils import get_default_test_sound import tempfile import shutil @@ -12,7 +13,7 @@ import shutil class aubio_slicing_test_case(TestCase): def setUp(self): - self.source_file = 'chocolate_1min.wav' + self.source_file = get_default_test_sound(self) self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') def test_slice_start_only(self): @@ -36,6 +37,53 @@ class aubio_slicing_test_case(TestCase): "number samples written different from number of original samples") shutil.rmtree(self.output_dir) +class aubio_slicing_wrong_starts_test_case(TestCase): + + def setUp(self): + self.source_file = get_default_test_sound(self) + self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') + + def test_slice_start_empty(self): + regions_start = [] + self.assertRaises(ValueError, + slice_source_at_stamps, + self.source_file, regions_start, output_dir = self.output_dir) + + def test_slice_start_none(self): + regions_start = None + self.assertRaises(ValueError, + slice_source_at_stamps, + self.source_file, regions_start, output_dir = self.output_dir) + + def tearDown(self): + shutil.rmtree(self.output_dir) + +class aubio_slicing_wrong_ends_test_case(TestCase): + + def setUp(self): + self.source_file = get_default_test_sound(self) + self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') + + def test_slice_wrong_ends(self): + regions_start = [i*1000 for i in range(1, 100)] + regions_end = [] + self.assertRaises (ValueError, + slice_source_at_stamps, self.source_file, regions_start, regions_end, + output_dir = self.output_dir) + + def test_slice_no_ends(self): + regions_start = [i*1000 for i in range(1, 100)] + regions_end = None + slice_source_at_stamps (self.source_file, regions_start, regions_end, + output_dir = self.output_dir) + original_samples = count_samples_in_file(self.source_file) + written_samples = count_samples_in_directory(self.output_dir) + assert_equal(original_samples, written_samples, + "number samples written different from number of original samples") + + def tearDown(self): + shutil.rmtree(self.output_dir) + if __name__ == '__main__': from unittest import main main() diff --git a/python/tests/utils.py b/python/tests/utils.py index 9ada420b..c42c6080 100644 --- a/python/tests/utils.py +++ b/python/tests/utils.py @@ -12,6 +12,13 @@ def list_all_sounds(rel_dir): datadir = os.path.join(os.path.dirname(__file__), rel_dir) return glob.glob(os.path.join(datadir,'*.*')) +def get_default_test_sound(TestCase, rel_dir = 'sounds'): + all_sounds = list_all_sounds(rel_dir) + if len(all_sounds) == 0: + TestCase.skipTest("please add some sounds in \'python/tests/sounds\'") + else: + return all_sounds[0] + def array_from_yaml_file(filename): import yaml f = open(filename)