source: python/tests/test_source.py @ 50a8260

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 50a8260 was 50a8260, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests/test_source.py: break long line

  • Property mode set to 100755
File size: 5.0 KB
Line 
1#! /usr/bin/env python
2
3from nose2 import main
4from nose2.tools import params
5from numpy.testing import TestCase
6from aubio import source
7from utils import list_all_sounds
8
9import warnings
10warnings.filterwarnings('ignore', category=UserWarning, append=True)
11
12list_of_sounds = list_all_sounds('sounds')
13samplerates = [0, 44100, 8000, 32000]
14hop_sizes = [512, 1024, 64]
15
16path = None
17
18all_params = []
19for soundfile in list_of_sounds:
20    for hop_size in hop_sizes:
21        for samplerate in samplerates:
22            all_params.append((hop_size, samplerate, soundfile))
23
24
25class aubio_source_test_case_base(TestCase):
26
27    def setUp(self):
28        if not len(list_of_sounds):
29            self.skipTest('add some sound files in \'python/tests/sounds\'')
30        self.default_test_sound = list_of_sounds[0]
31
32class aubio_source_test_case(aubio_source_test_case_base):
33
34    @params(*list_of_sounds)
35    def test_close_file(self, filename):
36        samplerate = 0 # use native samplerate
37        hop_size = 256
38        f = source(filename, samplerate, hop_size)
39        f.close()
40
41    @params(*list_of_sounds)
42    def test_close_file_twice(self, filename):
43        samplerate = 0 # use native samplerate
44        hop_size = 256
45        f = source(filename, samplerate, hop_size)
46        f.close()
47        f.close()
48
49class aubio_source_read_test_case(aubio_source_test_case_base):
50
51    def read_from_source(self, f):
52        total_frames = 0
53        while True:
54            _ , read = f()
55            total_frames += read
56            if read < f.hop_size: break
57        #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
58        #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
59        #print (result_str.format(*result_params))
60        return total_frames
61
62    @params(*all_params)
63    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
64        try:
65            f = source(soundfile, samplerate, hop_size)
66        except RuntimeError as e:
67            self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
68        assert f.samplerate != 0
69        self.read_from_source(f)
70
71    @params(*list_of_sounds)
72    def test_samplerate_none(self, p):
73        f = source(p)
74        assert f.samplerate != 0
75        self.read_from_source(f)
76
77    @params(*list_of_sounds)
78    def test_samplerate_0(self, p):
79        f = source(p, 0)
80        assert f.samplerate != 0
81        self.read_from_source(f)
82
83    @params(*list_of_sounds)
84    def test_zero_hop_size(self, p):
85        f = source(p, 0, 0)
86        assert f.samplerate != 0
87        assert f.hop_size != 0
88        self.read_from_source(f)
89
90    @params(*list_of_sounds)
91    def test_seek_to_half(self, p):
92        from random import randint
93        f = source(p, 0, 0)
94        assert f.samplerate != 0
95        assert f.hop_size != 0
96        a = self.read_from_source(f)
97        c = randint(0, a)
98        f.seek(c)
99        b = self.read_from_source(f)
100        assert a == b + c
101
102    @params(*list_of_sounds)
103    def test_duration(self, p):
104        total_frames = 0
105        f = source(p)
106        duration = f.duration
107        while True:
108            _, read = f()
109            total_frames += read
110            if read < f.hop_size: break
111        self.assertEqual(duration, total_frames)
112
113
114class aubio_source_test_wrong_params(TestCase):
115
116    def test_wrong_file(self):
117        with self.assertRaises(RuntimeError):
118            source('path_to/unexisting file.mp3')
119
120class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
121
122    def test_wrong_samplerate(self):
123        with self.assertRaises(ValueError):
124            source(self.default_test_sound, -1)
125
126    def test_wrong_hop_size(self):
127        with self.assertRaises(ValueError):
128            source(self.default_test_sound, 0, -1)
129
130    def test_wrong_channels(self):
131        with self.assertRaises(ValueError):
132            source(self.default_test_sound, 0, 0, -1)
133
134    def test_wrong_seek(self):
135        f = source(self.default_test_sound)
136        with self.assertRaises(ValueError):
137            f.seek(-1)
138
139    def test_wrong_seek_too_large(self):
140        f = source(self.default_test_sound)
141        try:
142            with self.assertRaises(ValueError):
143                f.seek(f.duration + f.samplerate * 10)
144        except AssertionError:
145            self.skipTest('seeking after end of stream failed raising ValueError')
146
147class aubio_source_readmulti_test_case(aubio_source_read_test_case):
148
149    def read_from_source(self, f):
150        total_frames = 0
151        while True:
152            _, read = f.do_multi()
153            total_frames += read
154            if read < f.hop_size: break
155        #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
156        #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
157        #print (result_str.format(*result_params))
158        return total_frames
159
160if __name__ == '__main__':
161    main()
Note: See TracBrowser for help on using the repository browser.