1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, run_module_suite |
---|
4 | from numpy.testing import assert_equal, assert_almost_equal |
---|
5 | |
---|
6 | from aubio import slice_source_at_stamps |
---|
7 | from utils import * |
---|
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, output_dir = self.output_dir) |
---|
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, output_dir = self.output_dir) |
---|
32 | |
---|
33 | def test_slice_start_every_blocksize(self): |
---|
34 | hopsize = 200 |
---|
35 | regions_start = [i*hopsize for i in range(1, n_slices)] |
---|
36 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
---|
37 | hopsize = 200) |
---|
38 | |
---|
39 | def tearDown(self): |
---|
40 | original_samples = count_samples_in_file(self.source_file) |
---|
41 | written_samples = count_samples_in_directory(self.output_dir) |
---|
42 | total_files = count_files_in_directory(self.output_dir) |
---|
43 | assert_equal(n_slices, total_files, |
---|
44 | "number of slices created different from expected") |
---|
45 | assert_equal(written_samples, original_samples, |
---|
46 | "number of samples written different from number of original samples") |
---|
47 | shutil.rmtree(self.output_dir) |
---|
48 | |
---|
49 | class aubio_slicing_with_ends_test_case(TestCase): |
---|
50 | |
---|
51 | def setUp(self): |
---|
52 | self.source_file = get_default_test_sound(self) |
---|
53 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
54 | |
---|
55 | def test_slice_start_and_ends_no_gap(self): |
---|
56 | regions_start = [i*1000 for i in range(n_slices)] |
---|
57 | regions_ends = [start - 1 for start in regions_start[1:]] + [1e120] |
---|
58 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
59 | output_dir = self.output_dir) |
---|
60 | original_samples = count_samples_in_file(self.source_file) |
---|
61 | written_samples = count_samples_in_directory(self.output_dir) |
---|
62 | total_files = count_files_in_directory(self.output_dir) |
---|
63 | assert_equal(n_slices, total_files, |
---|
64 | "number of slices created different from expected") |
---|
65 | assert_equal(written_samples, original_samples, |
---|
66 | "number of samples written different from number of original samples") |
---|
67 | |
---|
68 | def test_slice_start_and_ends_200_gap(self): |
---|
69 | regions_start = [i*1000 for i in range(n_slices)] |
---|
70 | regions_ends = [start + 199 for start in regions_start] |
---|
71 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
72 | output_dir = self.output_dir) |
---|
73 | expected_samples = 200 * n_slices |
---|
74 | written_samples = count_samples_in_directory(self.output_dir) |
---|
75 | total_files = count_files_in_directory(self.output_dir) |
---|
76 | assert_equal(n_slices, total_files, |
---|
77 | "number of slices created different from expected") |
---|
78 | assert_equal(written_samples, expected_samples, |
---|
79 | "number of samples written different from number of original samples") |
---|
80 | |
---|
81 | def test_slice_start_and_ends_overlaping(self): |
---|
82 | regions_start = [i*1000 for i in range(n_slices)] |
---|
83 | regions_ends = [start + 1199 for start in regions_start] |
---|
84 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, |
---|
85 | output_dir = self.output_dir) |
---|
86 | expected_samples = 1200 * n_slices |
---|
87 | written_samples = count_samples_in_directory(self.output_dir) |
---|
88 | total_files = count_files_in_directory(self.output_dir) |
---|
89 | assert_equal(n_slices, total_files, |
---|
90 | "number of slices created different from expected") |
---|
91 | assert_equal(written_samples, expected_samples, |
---|
92 | "number of samples written different from number of original samples") |
---|
93 | |
---|
94 | def tearDown(self): |
---|
95 | shutil.rmtree(self.output_dir) |
---|
96 | |
---|
97 | |
---|
98 | class aubio_slicing_wrong_starts_test_case(TestCase): |
---|
99 | |
---|
100 | def setUp(self): |
---|
101 | self.source_file = get_default_test_sound(self) |
---|
102 | self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case') |
---|
103 | |
---|
104 | def test_slice_start_empty(self): |
---|
105 | regions_start = [] |
---|
106 | self.assertRaises(ValueError, |
---|
107 | slice_source_at_stamps, |
---|
108 | self.source_file, regions_start, output_dir = self.output_dir) |
---|
109 | |
---|
110 | def test_slice_start_none(self): |
---|
111 | regions_start = None |
---|
112 | self.assertRaises(ValueError, |
---|
113 | slice_source_at_stamps, |
---|
114 | self.source_file, regions_start, output_dir = self.output_dir) |
---|
115 | |
---|
116 | def tearDown(self): |
---|
117 | shutil.rmtree(self.output_dir) |
---|
118 | |
---|
119 | class aubio_slicing_wrong_ends_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_wrong_ends(self): |
---|
126 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
127 | regions_end = [] |
---|
128 | self.assertRaises (ValueError, |
---|
129 | slice_source_at_stamps, self.source_file, regions_start, regions_end, |
---|
130 | output_dir = self.output_dir) |
---|
131 | |
---|
132 | def test_slice_no_ends(self): |
---|
133 | regions_start = [i*1000 for i in range(1, n_slices)] |
---|
134 | regions_end = None |
---|
135 | slice_source_at_stamps (self.source_file, regions_start, regions_end, |
---|
136 | output_dir = self.output_dir) |
---|
137 | total_files = count_files_in_directory(self.output_dir) |
---|
138 | assert_equal(n_slices, total_files, |
---|
139 | "number of slices created different from expected") |
---|
140 | original_samples = count_samples_in_file(self.source_file) |
---|
141 | written_samples = count_samples_in_directory(self.output_dir) |
---|
142 | assert_equal(written_samples, original_samples, |
---|
143 | "number of samples written different from number of original samples") |
---|
144 | |
---|
145 | def tearDown(self): |
---|
146 | shutil.rmtree(self.output_dir) |
---|
147 | |
---|
148 | if __name__ == '__main__': |
---|
149 | from unittest import main |
---|
150 | main() |
---|