source: python/tests/test_source.py @ 331249b

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

python/tests/test_source.py: improve tests, add double close and do_multi

  • Property mode set to 100755
File size: 3.3 KB
Line 
1#! /usr/bin/env python
2
3from numpy.testing import TestCase, assert_equal, assert_almost_equal
4from aubio import fvec, source
5from numpy import array
6from utils import list_all_sounds
7
8list_of_sounds = list_all_sounds('sounds')
9path = None
10
11class aubio_source_test_case_base(TestCase):
12
13    def setUp(self):
14        if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
15
16class aubio_source_test_case(aubio_source_test_case_base):
17
18    def test_close_file(self):
19        samplerate = 0 # use native samplerate
20        hop_size = 256
21        for p in list_of_sounds:
22            f = source(p, samplerate, hop_size)
23            f.close()
24
25    def test_close_file_twice(self):
26        samplerate = 0 # use native samplerate
27        hop_size = 256
28        for p in list_of_sounds:
29            f = source(p, samplerate, hop_size)
30            f.close()
31            f.close()
32
33class aubio_source_read_test_case(aubio_source_test_case_base):
34
35    def read_from_sink(self, f):
36        total_frames = 0
37        while True:
38            vec, read = f()
39            total_frames += read
40            if read < f.hop_size: break
41        print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
42        print "(", total_frames, "frames", "in",
43        print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
44        print "from", f.uri
45
46    def test_samplerate_hopsize(self):
47        for p in list_of_sounds:
48            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
49                f = source(p, samplerate, hop_size)
50                assert f.samplerate != 0
51                self.read_from_sink(f)
52
53    def test_samplerate_none(self):
54        for p in list_of_sounds:
55            f = source(p)
56            assert f.samplerate != 0
57            self.read_from_sink(f)
58
59    def test_samplerate_0(self):
60        for p in list_of_sounds:
61            f = source(p, 0)
62            assert f.samplerate != 0
63            self.read_from_sink(f)
64
65    def test_wrong_samplerate(self):
66        for p in list_of_sounds:
67            try:
68                f = source(p, -1)
69            except ValueError, e:
70                pass
71            else:
72                self.fail('negative samplerate does not raise ValueError')
73
74    def test_wrong_hop_size(self):
75        for p in list_of_sounds:
76            try:
77                f = source(p, 0, -1)
78            except ValueError, e:
79                pass
80            else:
81                self.fail('negative hop_size does not raise ValueError')
82
83    def test_zero_hop_size(self):
84        for p in list_of_sounds:
85            f = source(p, 0, 0)
86            assert f.samplerate != 0
87            assert f.hop_size != 0
88            self.read_from_sink(f)
89
90class aubio_source_readmulti_test_case(aubio_source_read_test_case):
91
92    def read_from_sink(self, f):
93        total_frames = 0
94        while True:
95            vec, read = f.do_multi()
96            total_frames += read
97            if read < f.hop_size: break
98        print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
99        print "(", total_frames, "frames", "in",
100        print f.channels, "channels and",
101        print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
102        print "from", f.uri
103
104if __name__ == '__main__':
105    from unittest import main
106    main()
Note: See TracBrowser for help on using the repository browser.