1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from unittest import main |
---|
4 | from numpy.testing import TestCase, assert_equal |
---|
5 | from aubio import slice_source_at_stamps |
---|
6 | from .utils import count_files_in_directory, get_default_test_sound |
---|
7 | from .utils import count_samples_in_directory, count_samples_in_file |
---|
8 | |
---|
9 | import tempfile |
---|
10 | import shutil |
---|
11 | |
---|
12 | n_slices = 4 |
---|
13 | |
---|
14 | class aubio_slicing_test_case(TestCase): |
---|
15 | |
---|
16 | def setUp(self): |
---|
17 | self.source_file = get_default_test_sound(self) |
---|
18 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
19 | |
---|
20 | def test_slice_start_only(self): |
---|
21 | regions_start = [i*1000 for i in range(n_slices)] |
---|
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): |
---|
25 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
26 | slice_source_at_stamps(self.source_file, regions_start, |
---|
27 | output_dir = self.output_dir, create_first=True) |
---|
28 | |
---|
29 | def test_slice_start_beyond_end(self): |
---|
30 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
31 | regions_start += [count_samples_in_file(self.source_file) + 1000] |
---|
32 | slice_source_at_stamps(self.source_file, regions_start, |
---|
33 | output_dir = self.output_dir, create_first=True) |
---|
34 | |
---|
35 | def test_slice_start_every_blocksize(self): |
---|
36 | hopsize = 200 |
---|
37 | regions_start = [i*hopsize for i in range(0, n_slices)] |
---|
38 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
---|
39 | hopsize = 200) |
---|
40 | |
---|
41 | def test_slice_start_every_half_blocksize(self): |
---|
42 | hopsize = 200 |
---|
43 | regions_start = [i*hopsize//2 for i in range(0, n_slices)] |
---|
44 | slice_source_at_stamps(self.source_file, regions_start, |
---|
45 | output_dir = self.output_dir, hopsize = 200) |
---|
46 | |
---|
47 | def tearDown(self): |
---|
48 | original_samples = count_samples_in_file(self.source_file) |
---|
49 | written_samples = count_samples_in_directory(self.output_dir) |
---|
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") |
---|
53 | assert_equal(written_samples, original_samples, |
---|
54 | "number of samples written different from number of original samples") |
---|
55 | shutil.rmtree(self.output_dir) |
---|
56 | |
---|
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 | |
---|
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 | |
---|
115 | def tearDown(self): |
---|
116 | shutil.rmtree(self.output_dir) |
---|
117 | |
---|
118 | |
---|
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): |
---|
147 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
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): |
---|
154 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
155 | regions_end = None |
---|
156 | slice_source_at_stamps (self.source_file, regions_start, regions_end, |
---|
157 | output_dir = self.output_dir, create_first=True) |
---|
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") |
---|
161 | original_samples = count_samples_in_file(self.source_file) |
---|
162 | written_samples = count_samples_in_directory(self.output_dir) |
---|
163 | assert_equal(written_samples, original_samples, |
---|
164 | "number of samples written different from number of original samples") |
---|
165 | |
---|
166 | def tearDown(self): |
---|
167 | shutil.rmtree(self.output_dir) |
---|
168 | |
---|
169 | if __name__ == '__main__': |
---|
170 | main() |
---|