[88432a9] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[0b6d23d] | 3 | from unittest import main |
---|
| 4 | from numpy.testing import TestCase, assert_equal |
---|
[88432a9] | 5 | from aubio import slice_source_at_stamps |
---|
[a1bf01d] | 6 | from .utils import count_files_in_directory, get_default_test_sound |
---|
| 7 | from .utils import count_samples_in_directory, count_samples_in_file |
---|
[88432a9] | 8 | |
---|
| 9 | import tempfile |
---|
| 10 | import shutil |
---|
| 11 | |
---|
[4320679] | 12 | n_slices = 4 |
---|
[6f27719] | 13 | |
---|
[88432a9] | 14 | class aubio_slicing_test_case(TestCase): |
---|
| 15 | |
---|
| 16 | def setUp(self): |
---|
[aee840b] | 17 | self.source_file = get_default_test_sound(self) |
---|
[88432a9] | 18 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
| 19 | |
---|
| 20 | def test_slice_start_only(self): |
---|
[6f27719] | 21 | regions_start = [i*1000 for i in range(n_slices)] |
---|
[88432a9] | 22 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir) |
---|
| 23 | |
---|
| 24 | def test_slice_start_only_no_zero(self): |
---|
[6f27719] | 25 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
[d194d8c] | 26 | slice_source_at_stamps(self.source_file, regions_start, |
---|
| 27 | output_dir = self.output_dir, create_first=True) |
---|
[88432a9] | 28 | |
---|
| 29 | def test_slice_start_beyond_end(self): |
---|
[6f27719] | 30 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
[88432a9] | 31 | regions_start += [count_samples_in_file(self.source_file) + 1000] |
---|
[d194d8c] | 32 | slice_source_at_stamps(self.source_file, regions_start, |
---|
| 33 | output_dir = self.output_dir, create_first=True) |
---|
[88432a9] | 34 | |
---|
[f36ecea] | 35 | def test_slice_start_every_blocksize(self): |
---|
| 36 | hopsize = 200 |
---|
[ab24edb] | 37 | regions_start = [i*hopsize for i in range(0, n_slices)] |
---|
[f36ecea] | 38 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
---|
| 39 | hopsize = 200) |
---|
| 40 | |
---|
[58a82d4] | 41 | def test_slice_start_every_half_blocksize(self): |
---|
| 42 | hopsize = 200 |
---|
[ab24edb] | 43 | regions_start = [i*hopsize//2 for i in range(0, n_slices)] |
---|
[58a82d4] | 44 | slice_source_at_stamps(self.source_file, regions_start, |
---|
| 45 | output_dir = self.output_dir, hopsize = 200) |
---|
| 46 | |
---|
[88432a9] | 47 | def tearDown(self): |
---|
| 48 | original_samples = count_samples_in_file(self.source_file) |
---|
| 49 | written_samples = count_samples_in_directory(self.output_dir) |
---|
[f36ecea] | 50 | total_files = count_files_in_directory(self.output_dir) |
---|
| 51 | assert_equal(n_slices, total_files, |
---|
| 52 | "number of slices created different from expected") |
---|
[6f27719] | 53 | assert_equal(written_samples, original_samples, |
---|
| 54 | "number of samples written different from number of original samples") |
---|
[88432a9] | 55 | shutil.rmtree(self.output_dir) |
---|
| 56 | |
---|
[4320679] | 57 | class aubio_slicing_with_ends_test_case(TestCase): |
---|
| 58 | |
---|
| 59 | def setUp(self): |
---|
| 60 | self.source_file = get_default_test_sound(self) |
---|
| 61 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
| 62 | |
---|
| 63 | def test_slice_start_and_ends_no_gap(self): |
---|
| 64 | regions_start = [i*1000 for i in range(n_slices)] |
---|
| 65 | regions_ends = [start - 1 for start in regions_start[1:]] + [1e120] |
---|
| 66 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
| 67 | output_dir = self.output_dir) |
---|
| 68 | original_samples = count_samples_in_file(self.source_file) |
---|
| 69 | written_samples = count_samples_in_directory(self.output_dir) |
---|
| 70 | total_files = count_files_in_directory(self.output_dir) |
---|
| 71 | assert_equal(n_slices, total_files, |
---|
| 72 | "number of slices created different from expected") |
---|
| 73 | assert_equal(written_samples, original_samples, |
---|
| 74 | "number of samples written different from number of original samples") |
---|
| 75 | |
---|
| 76 | def test_slice_start_and_ends_200_gap(self): |
---|
| 77 | regions_start = [i*1000 for i in range(n_slices)] |
---|
| 78 | regions_ends = [start + 199 for start in regions_start] |
---|
| 79 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
| 80 | output_dir = self.output_dir) |
---|
| 81 | expected_samples = 200 * n_slices |
---|
| 82 | written_samples = count_samples_in_directory(self.output_dir) |
---|
| 83 | total_files = count_files_in_directory(self.output_dir) |
---|
| 84 | assert_equal(n_slices, total_files, |
---|
| 85 | "number of slices created different from expected") |
---|
| 86 | assert_equal(written_samples, expected_samples, |
---|
| 87 | "number of samples written different from number of original samples") |
---|
| 88 | |
---|
| 89 | def test_slice_start_and_ends_overlaping(self): |
---|
| 90 | regions_start = [i*1000 for i in range(n_slices)] |
---|
| 91 | regions_ends = [start + 1199 for start in regions_start] |
---|
| 92 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
| 93 | output_dir = self.output_dir) |
---|
| 94 | expected_samples = 1200 * n_slices |
---|
| 95 | written_samples = count_samples_in_directory(self.output_dir) |
---|
| 96 | total_files = count_files_in_directory(self.output_dir) |
---|
| 97 | assert_equal(n_slices, total_files, |
---|
| 98 | "number of slices created different from expected") |
---|
| 99 | assert_equal(written_samples, expected_samples, |
---|
| 100 | "number of samples written different from number of original samples") |
---|
| 101 | |
---|
[f0ce8aa3] | 102 | def test_slice_start_and_ends_with_missing_end(self): |
---|
| 103 | regions_start = [i*1000 for i in range(n_slices)] |
---|
| 104 | regions_ends = [r-1 for r in regions_start[1:]] |
---|
| 105 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
| 106 | output_dir = self.output_dir) |
---|
| 107 | written_samples = count_samples_in_directory(self.output_dir) |
---|
| 108 | original_samples = count_samples_in_file(self.source_file) |
---|
| 109 | total_files = count_files_in_directory(self.output_dir) |
---|
| 110 | assert_equal(n_slices, total_files, |
---|
| 111 | "number of slices created different from expected") |
---|
| 112 | assert_equal(written_samples, original_samples, |
---|
| 113 | "number of samples written different from number of original samples") |
---|
| 114 | |
---|
[4320679] | 115 | def tearDown(self): |
---|
| 116 | shutil.rmtree(self.output_dir) |
---|
| 117 | |
---|
| 118 | |
---|
[aee840b] | 119 | class aubio_slicing_wrong_starts_test_case(TestCase): |
---|
| 120 | |
---|
| 121 | def setUp(self): |
---|
| 122 | self.source_file = get_default_test_sound(self) |
---|
| 123 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
| 124 | |
---|
| 125 | def test_slice_start_empty(self): |
---|
| 126 | regions_start = [] |
---|
| 127 | self.assertRaises(ValueError, |
---|
| 128 | slice_source_at_stamps, |
---|
| 129 | self.source_file, regions_start, output_dir = self.output_dir) |
---|
| 130 | |
---|
| 131 | def test_slice_start_none(self): |
---|
| 132 | regions_start = None |
---|
| 133 | self.assertRaises(ValueError, |
---|
| 134 | slice_source_at_stamps, |
---|
| 135 | self.source_file, regions_start, output_dir = self.output_dir) |
---|
| 136 | |
---|
| 137 | def tearDown(self): |
---|
| 138 | shutil.rmtree(self.output_dir) |
---|
| 139 | |
---|
| 140 | class aubio_slicing_wrong_ends_test_case(TestCase): |
---|
| 141 | |
---|
| 142 | def setUp(self): |
---|
| 143 | self.source_file = get_default_test_sound(self) |
---|
| 144 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
| 145 | |
---|
| 146 | def test_slice_wrong_ends(self): |
---|
[f36ecea] | 147 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
[aee840b] | 148 | regions_end = [] |
---|
| 149 | self.assertRaises (ValueError, |
---|
| 150 | slice_source_at_stamps, self.source_file, regions_start, regions_end, |
---|
| 151 | output_dir = self.output_dir) |
---|
| 152 | |
---|
| 153 | def test_slice_no_ends(self): |
---|
[f36ecea] | 154 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
[aee840b] | 155 | regions_end = None |
---|
| 156 | slice_source_at_stamps (self.source_file, regions_start, regions_end, |
---|
[d194d8c] | 157 | output_dir = self.output_dir, create_first=True) |
---|
[4320679] | 158 | total_files = count_files_in_directory(self.output_dir) |
---|
| 159 | assert_equal(n_slices, total_files, |
---|
| 160 | "number of slices created different from expected") |
---|
[aee840b] | 161 | original_samples = count_samples_in_file(self.source_file) |
---|
| 162 | written_samples = count_samples_in_directory(self.output_dir) |
---|
[6f27719] | 163 | assert_equal(written_samples, original_samples, |
---|
| 164 | "number of samples written different from number of original samples") |
---|
[aee840b] | 165 | |
---|
| 166 | def tearDown(self): |
---|
| 167 | shutil.rmtree(self.output_dir) |
---|
| 168 | |
---|
[88432a9] | 169 | if __name__ == '__main__': |
---|
| 170 | main() |
---|