python/tests/test_source.py: skip tests if no sounds were found
[aubio.git] / python / tests / test_musicutils.py
1 #! /usr/bin/env python
2
3 from numpy.testing import TestCase
4 from numpy.testing.utils import assert_equal, assert_almost_equal
5 from numpy import cos, arange
6 from math import pi
7
8 from aubio import window, level_lin, db_spl, silence_detection, level_detection
9
10 from aubio import fvec, float_type
11
12 class aubio_window(TestCase):
13
14     def test_accept_name_and_size(self):
15         window("default", 1024)
16
17     def test_fail_name_not_string(self):
18         with self.assertRaises(TypeError):
19             window(10, 1024)
20
21     def test_fail_size_not_int(self):
22         with self.assertRaises(TypeError):
23             window("default", "default")
24
25     def test_compute_hanning_1024(self):
26         size = 1024
27         aubio_window = window("hanning", size)
28         numpy_window = .5 - .5 * cos(2. * pi * arange(size) / size)
29         assert_almost_equal(aubio_window, numpy_window)
30
31 class aubio_level_lin(TestCase):
32     def test_accept_fvec(self):
33         level_lin(fvec(1024))
34
35     def test_fail_not_fvec(self):
36         try:
37             level_lin("default")
38         except ValueError as e:
39             pass
40         else:
41             self.fail('non-number input phase does not raise a TypeError')
42
43     def test_zeros_is_zeros(self):
44         assert_equal(level_lin(fvec(1024)), 0.)
45
46     def test_minus_ones_is_one(self):
47         from numpy import ones
48         assert_equal(level_lin(-ones(1024, dtype = float_type)), 1.)
49
50 class aubio_db_spl(TestCase):
51     def test_accept_fvec(self):
52         db_spl(fvec(1024))
53
54     def test_fail_not_fvec(self):
55         try:
56             db_spl("default")
57         except ValueError as e:
58             pass
59         else:
60             self.fail('non-number input phase does not raise a TypeError')
61
62     def test_zeros_is_inf(self):
63         from math import isinf
64         assert isinf(db_spl(fvec(1024)))
65
66     def test_minus_ones_is_zero(self):
67         from numpy import ones
68         assert_equal(db_spl(-ones(1024, dtype = float_type)), 0.)
69
70 class aubio_silence_detection(TestCase):
71     def test_accept_fvec(self):
72         silence_detection(fvec(1024), -70.)
73
74     def test_fail_not_fvec(self):
75         try:
76             silence_detection("default", -70)
77         except ValueError as e:
78             pass
79         else:
80             self.fail('non-number input phase does not raise a TypeError')
81
82     def test_zeros_is_one(self):
83         from math import isinf
84         assert silence_detection(fvec(1024), -70) == 1
85
86     def test_minus_ones_is_zero(self):
87         from numpy import ones
88         assert silence_detection(ones(1024, dtype = float_type), -70) == 0
89
90 class aubio_level_detection(TestCase):
91     def test_accept_fvec(self):
92         level_detection(fvec(1024), -70.)
93
94     def test_fail_not_fvec(self):
95         try:
96             level_detection("default", -70)
97         except ValueError as e:
98             pass
99         else:
100             self.fail('non-number input phase does not raise a TypeError')
101
102     def test_zeros_is_one(self):
103         from math import isinf
104         assert level_detection(fvec(1024), -70) == 1
105
106     def test_minus_ones_is_zero(self):
107         from numpy import ones
108         assert level_detection(ones(1024, dtype = float_type), -70) == 0
109
110 if __name__ == '__main__':
111     from unittest import main
112     main()