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