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