python/ext/py-source.c: add seek, thanks @davebrent for the heads up
[aubio.git] / python / tests / test_source.py
1 #! /usr/bin/env python
2
3 from numpy.testing import TestCase, assert_equal, assert_almost_equal
4 from aubio import fvec, source
5 from numpy import array
6 from utils import list_all_sounds
7
8 list_of_sounds = list_all_sounds('sounds')
9 path = None
10
11 class 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
16 class 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
33 class 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, 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, 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
103 class 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
118 if __name__ == '__main__':
119     from unittest import main
120     main()