python/ext/py-source.c: added duration
[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         result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
42         params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri
43         print (result_str.format(*params))
44         return total_frames
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_source(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_source(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_source(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 as 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 as 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_source(f)
89
90     def test_seek_to_half(self):
91         from random import randint
92         for p in list_of_sounds:
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     def test_duration(self):
103         for p in list_of_sounds:
104             total_frames = 0
105             f = source(p)
106             duration = f.duration
107             while True:
108                 vec, read = f()
109                 total_frames += read
110                 if read < f.hop_size: break
111             self.assertEqual(duration, total_frames)
112
113 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
114
115     def read_from_source(self, f):
116         total_frames = 0
117         while True:
118             vec, read = f.do_multi()
119             total_frames += read
120             if read < f.hop_size: break
121         result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
122         params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
123         print (result_str.format(*params))
124         return total_frames
125
126 if __name__ == '__main__':
127     from unittest import main
128     main()