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