Merge branch 'master' into feature/pytest
[aubio.git] / python / tests / test_musicutils.py
1 #! /usr/bin/env python
2
3 import numpy as np
4 from numpy.testing import TestCase
5 from numpy.testing import assert_equal, assert_almost_equal
6 from aubio import window, level_lin, db_spl, silence_detection, level_detection
7 from aubio import fvec, float_type
8
9 class aubio_window(TestCase):
10
11     def test_accept_name_and_size(self):
12         window("default", 1024)
13
14     def test_fail_name_not_string(self):
15         with self.assertRaises(TypeError):
16             window(10, 1024)
17
18     def test_fail_size_not_int(self):
19         with self.assertRaises(TypeError):
20             window("default", "default")
21
22     def test_compute_hanning_1024(self):
23         size = 1024
24         aubio_window = window("hanning", size)
25         numpy_window = .5 - .5 * np.cos(2. * np.pi * np.arange(size) / size)
26         assert_almost_equal(aubio_window, numpy_window)
27
28 class aubio_level_lin(TestCase):
29     def test_accept_fvec(self):
30         level_lin(fvec(1024))
31
32     def test_fail_not_fvec(self):
33         with self.assertRaises(ValueError):
34             level_lin("default")
35
36     def test_zeros_is_zeros(self):
37         assert_equal(level_lin(fvec(1024)), 0.)
38
39     def test_minus_ones_is_one(self):
40         assert_equal(level_lin(-np.ones(1024, dtype = float_type)), 1.)
41
42 class aubio_db_spl(TestCase):
43     def test_accept_fvec(self):
44         db_spl(fvec(1024))
45
46     def test_fail_not_fvec(self):
47         with self.assertRaises(ValueError):
48             db_spl("default")
49
50     def test_zeros_is_inf(self):
51         assert np.isinf(db_spl(fvec(1024)))
52
53     def test_minus_ones_is_zero(self):
54         assert_equal(db_spl(-np.ones(1024, dtype = float_type)), 0.)
55
56 class aubio_silence_detection(TestCase):
57     def test_accept_fvec(self):
58         silence_detection(fvec(1024), -70.)
59
60     def test_fail_not_fvec(self):
61         with self.assertRaises(ValueError):
62             silence_detection("default", -70)
63
64     def test_zeros_is_one(self):
65         assert silence_detection(fvec(1024), -70) == 1
66
67     def test_minus_ones_is_zero(self):
68         from numpy import ones
69         assert silence_detection(ones(1024, dtype = float_type), -70) == 0
70
71 class aubio_level_detection(TestCase):
72     def test_accept_fvec(self):
73         level_detection(fvec(1024), -70.)
74
75     def test_fail_not_fvec(self):
76         with self.assertRaises(ValueError):
77             level_detection("default", -70)
78
79     def test_zeros_is_one(self):
80         assert level_detection(fvec(1024), -70) == 1
81
82     def test_minus_ones_is_zero(self):
83         from numpy import ones
84         assert level_detection(ones(1024, dtype = float_type), -70) == 0
85
86 if __name__ == '__main__':
87     from unittest import main
88     main()