source: python/tests/test_slicing.py @ 58a82d4

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since 58a82d4 was 58a82d4, checked in by Paul Brossier <piem@piem.org>, 16 months ago

[py] add test with slices shorter than hop_size

  • Property mode set to 100755
File size: 6.6 KB
Line 
1#! /usr/bin/env python
2
3from unittest import main
4from numpy.testing import TestCase, assert_equal
5from aubio import slice_source_at_stamps
6from .utils import count_files_in_directory, get_default_test_sound
7from .utils import count_samples_in_directory, count_samples_in_file
8
9import tempfile
10import shutil
11
12n_slices = 4
13
14class 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 test_slice_start_every_half_blocksize(self):
40        hopsize = 200
41        regions_start = [i*hopsize//2 for i in range(1, n_slices)]
42        slice_source_at_stamps(self.source_file, regions_start,
43                output_dir = self.output_dir, hopsize = 200)
44
45    def tearDown(self):
46        original_samples = count_samples_in_file(self.source_file)
47        written_samples = count_samples_in_directory(self.output_dir)
48        total_files = count_files_in_directory(self.output_dir)
49        assert_equal(n_slices, total_files,
50            "number of slices created different from expected")
51        assert_equal(written_samples, original_samples,
52            "number of samples written different from number of original samples")
53        shutil.rmtree(self.output_dir)
54
55class aubio_slicing_with_ends_test_case(TestCase):
56
57    def setUp(self):
58        self.source_file = get_default_test_sound(self)
59        self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')
60
61    def test_slice_start_and_ends_no_gap(self):
62        regions_start = [i*1000 for i in range(n_slices)]
63        regions_ends = [start - 1 for start in regions_start[1:]] + [1e120]
64        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
65                output_dir = self.output_dir)
66        original_samples = count_samples_in_file(self.source_file)
67        written_samples = count_samples_in_directory(self.output_dir)
68        total_files = count_files_in_directory(self.output_dir)
69        assert_equal(n_slices, total_files,
70            "number of slices created different from expected")
71        assert_equal(written_samples, original_samples,
72            "number of samples written different from number of original samples")
73
74    def test_slice_start_and_ends_200_gap(self):
75        regions_start = [i*1000 for i in range(n_slices)]
76        regions_ends = [start + 199 for start in regions_start]
77        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
78                output_dir = self.output_dir)
79        expected_samples = 200 * n_slices
80        written_samples = count_samples_in_directory(self.output_dir)
81        total_files = count_files_in_directory(self.output_dir)
82        assert_equal(n_slices, total_files,
83            "number of slices created different from expected")
84        assert_equal(written_samples, expected_samples,
85            "number of samples written different from number of original samples")
86
87    def test_slice_start_and_ends_overlaping(self):
88        regions_start = [i*1000 for i in range(n_slices)]
89        regions_ends = [start + 1199 for start in regions_start]
90        slice_source_at_stamps(self.source_file, regions_start, regions_ends,
91                output_dir = self.output_dir)
92        expected_samples = 1200 * n_slices
93        written_samples = count_samples_in_directory(self.output_dir)
94        total_files = count_files_in_directory(self.output_dir)
95        assert_equal(n_slices, total_files,
96            "number of slices created different from expected")
97        assert_equal(written_samples, expected_samples,
98            "number of samples written different from number of original samples")
99
100    def tearDown(self):
101        shutil.rmtree(self.output_dir)
102
103
104class aubio_slicing_wrong_starts_test_case(TestCase):
105
106    def setUp(self):
107        self.source_file = get_default_test_sound(self)
108        self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')
109
110    def test_slice_start_empty(self):
111        regions_start = []
112        self.assertRaises(ValueError,
113                slice_source_at_stamps,
114                self.source_file, regions_start, output_dir = self.output_dir)
115
116    def test_slice_start_none(self):
117        regions_start = None
118        self.assertRaises(ValueError,
119                slice_source_at_stamps,
120                self.source_file, regions_start, output_dir = self.output_dir)
121
122    def tearDown(self):
123        shutil.rmtree(self.output_dir)
124
125class aubio_slicing_wrong_ends_test_case(TestCase):
126
127    def setUp(self):
128        self.source_file = get_default_test_sound(self)
129        self.output_dir = tempfile.mkdtemp(suffix = 'aubio_slicing_test_case')
130
131    def test_slice_wrong_ends(self):
132        regions_start = [i*1000 for i in range(1, n_slices)]
133        regions_end = []
134        self.assertRaises (ValueError,
135            slice_source_at_stamps, self.source_file, regions_start, regions_end,
136                output_dir = self.output_dir)
137
138    def test_slice_no_ends(self):
139        regions_start = [i*1000 for i in range(1, n_slices)]
140        regions_end = None
141        slice_source_at_stamps (self.source_file, regions_start, regions_end,
142                output_dir = self.output_dir)
143        total_files = count_files_in_directory(self.output_dir)
144        assert_equal(n_slices, total_files,
145            "number of slices created different from expected")
146        original_samples = count_samples_in_file(self.source_file)
147        written_samples = count_samples_in_directory(self.output_dir)
148        assert_equal(written_samples, original_samples,
149            "number of samples written different from number of original samples")
150
151    def tearDown(self):
152        shutil.rmtree(self.output_dir)
153
154if __name__ == '__main__':
155    main()
Note: See TracBrowser for help on using the repository browser.