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