python/lib/aubio/slicing.py: error checking, add more tests, use get_default_sound
authorPaul Brossier <piem@piem.org>
Sun, 12 Jan 2014 02:33:07 +0000 (22:33 -0400)
committerPaul Brossier <piem@piem.org>
Sun, 12 Jan 2014 02:33:07 +0000 (22:33 -0400)
python/lib/aubio/slicing.py
python/tests/sounds/chocolate_1min.wav [new file with mode: 0644]
python/tests/test_slicing.py
python/tests/utils.py

index c60e674..0e03151 100644 (file)
@@ -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 (file)
index 0000000..22c1b14
Binary files /dev/null and b/python/tests/sounds/chocolate_1min.wav differ
index 3c0750e..497c16e 100755 (executable)
@@ -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()
index 9ada420..c42c608 100644 (file)
@@ -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)