source: python/tests/test_source.py @ d45f527

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

python/tests/test_source.py: filter user warnings to avoid spamming the console

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