source: python/tests/test_source.py @ 0229e51

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

python/tests/: prepare for python3 (see #33)

  • Property mode set to 100755
File size: 3.7 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_source(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        return total_frames
46
47    def test_samplerate_hopsize(self):
48        for p in list_of_sounds:
49            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
50                f = source(p, samplerate, hop_size)
51                assert f.samplerate != 0
52                self.read_from_source(f)
53
54    def test_samplerate_none(self):
55        for p in list_of_sounds:
56            f = source(p)
57            assert f.samplerate != 0
58            self.read_from_source(f)
59
60    def test_samplerate_0(self):
61        for p in list_of_sounds:
62            f = source(p, 0)
63            assert f.samplerate != 0
64            self.read_from_source(f)
65
66    def test_wrong_samplerate(self):
67        for p in list_of_sounds:
68            try:
69                f = source(p, -1)
70            except ValueError as e:
71                pass
72            else:
73                self.fail('negative samplerate does not raise ValueError')
74
75    def test_wrong_hop_size(self):
76        for p in list_of_sounds:
77            try:
78                f = source(p, 0, -1)
79            except ValueError as e:
80                pass
81            else:
82                self.fail('negative hop_size does not raise ValueError')
83
84    def test_zero_hop_size(self):
85        for p in list_of_sounds:
86            f = source(p, 0, 0)
87            assert f.samplerate != 0
88            assert f.hop_size != 0
89            self.read_from_source(f)
90
91    def test_seek_to_half(self):
92        from random import randint
93        for p in list_of_sounds:
94            f = source(p, 0, 0)
95            assert f.samplerate != 0
96            assert f.hop_size != 0
97            a = self.read_from_source(f)
98            c = randint(0, a)
99            f.seek(c)
100            b = self.read_from_source(f)
101            assert a == b + c
102
103class aubio_source_readmulti_test_case(aubio_source_read_test_case):
104
105    def read_from_source(self, f):
106        total_frames = 0
107        while True:
108            vec, read = f.do_multi()
109            total_frames += read
110            if read < f.hop_size: break
111        print ("read", "%.2fs" % (total_frames / float(f.samplerate) ) ),
112        print ("(", total_frames, "frames", "in" ),
113        print (f.channels, "channels and" ),
114        print (total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")" ),
115        print ("from", f.uri)
116        return total_frames
117
118if __name__ == '__main__':
119    from unittest import main
120    main()
Note: See TracBrowser for help on using the repository browser.