1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from unittest import main |
---|
4 | from numpy.testing import TestCase, assert_equal |
---|
5 | from numpy import array, arange, isnan, isinf |
---|
6 | from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq |
---|
7 | from aubio import unwrap2pi |
---|
8 | from aubio import fvec |
---|
9 | from math import pi |
---|
10 | |
---|
11 | class aubio_mathutils(TestCase): |
---|
12 | |
---|
13 | def test_unwrap2pi(self): |
---|
14 | unwrap2pi(int(23)) |
---|
15 | unwrap2pi(float(23.)) |
---|
16 | unwrap2pi(int(23.)) |
---|
17 | unwrap2pi(arange(10)) |
---|
18 | unwrap2pi(arange(10).astype("int")) |
---|
19 | unwrap2pi(arange(10).astype("float")) |
---|
20 | unwrap2pi(arange(10).astype("float32")) |
---|
21 | unwrap2pi([1,3,5]) |
---|
22 | unwrap2pi([23.,24.,25.]) |
---|
23 | a = fvec(10) |
---|
24 | a[:] = 4. |
---|
25 | unwrap2pi(a) |
---|
26 | a = pi/100. * arange(-600,600).astype("float") |
---|
27 | unwrap2pi(a) |
---|
28 | #print zip(a, b) |
---|
29 | |
---|
30 | def test_unwrap2pi_fails_on_list(self): |
---|
31 | with self.assertRaises((TypeError, NotImplementedError)): |
---|
32 | unwrap2pi(["23.","24.",25.]) |
---|
33 | |
---|
34 | def test_unwrap2pi_takes_fvec(self): |
---|
35 | a = fvec(10) |
---|
36 | b = unwrap2pi(a) |
---|
37 | #print zip(a, b) |
---|
38 | assert ( b > -pi ).all() |
---|
39 | assert ( b <= pi ).all() |
---|
40 | |
---|
41 | def test_unwrap2pi_takes_array_of_float(self): |
---|
42 | a = arange(-10., 10.).astype("float") |
---|
43 | b = unwrap2pi(a) |
---|
44 | #print zip(a, b) |
---|
45 | assert ( b > -pi ).all() |
---|
46 | assert ( b <= pi ).all() |
---|
47 | |
---|
48 | def test_unwrap2pi_takes_array_of_float32(self): |
---|
49 | a = arange(-10, 10).astype("float32") |
---|
50 | b = unwrap2pi(a) |
---|
51 | #print zip(a, b) |
---|
52 | assert ( b > -pi ).all() |
---|
53 | assert ( b <= pi ).all() |
---|
54 | |
---|
55 | def test_freqtomidi(self): |
---|
56 | a = array(list(range(-20, 50000, 100)) + [ -1e32, 1e32 ]) |
---|
57 | b = freqtomidi(a) |
---|
58 | #print zip(a, b) |
---|
59 | assert_equal ( isnan(array(b)), False ) |
---|
60 | assert_equal ( isinf(array(b)), False ) |
---|
61 | assert_equal ( array(b) < 0, False ) |
---|
62 | |
---|
63 | def test_miditofreq(self): |
---|
64 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
65 | b = miditofreq(a) |
---|
66 | #print zip(a, b) |
---|
67 | assert_equal ( isnan(b), False ) |
---|
68 | assert_equal ( isinf(b), False ) |
---|
69 | assert_equal ( b < 0, False ) |
---|
70 | |
---|
71 | def test_miditobin(self): |
---|
72 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
73 | b = [ miditobin(x, 44100, 512) for x in a ] |
---|
74 | #print zip(a, b) |
---|
75 | assert_equal ( isnan(array(b)), False ) |
---|
76 | assert_equal ( isinf(array(b)), False ) |
---|
77 | assert_equal ( array(b) < 0, False ) |
---|
78 | |
---|
79 | def test_bintomidi(self): |
---|
80 | a = list(range(-100, 512)) |
---|
81 | b = [ bintomidi(x, 44100, 512) for x in a ] |
---|
82 | #print zip(a, b) |
---|
83 | assert_equal ( isnan(array(b)), False ) |
---|
84 | assert_equal ( isinf(array(b)), False ) |
---|
85 | assert_equal ( array(b) < 0, False ) |
---|
86 | |
---|
87 | def test_freqtobin(self): |
---|
88 | a = list(range(-20, 50000, 100)) + [ -1e32, 1e32 ] |
---|
89 | b = [ freqtobin(x, 44100, 512) for x in a ] |
---|
90 | #print zip(a, b) |
---|
91 | assert_equal ( isnan(array(b)), False ) |
---|
92 | assert_equal ( isinf(array(b)), False ) |
---|
93 | assert_equal ( array(b) < 0, False ) |
---|
94 | |
---|
95 | def test_bintofreq(self): |
---|
96 | a = list(range(-20, 148)) |
---|
97 | b = [ bintofreq(x, 44100, 512) for x in a ] |
---|
98 | #print zip(a, b) |
---|
99 | assert_equal ( isnan(array(b)), False ) |
---|
100 | assert_equal ( isinf(array(b)), False ) |
---|
101 | assert_equal ( array(b) < 0, False ) |
---|
102 | |
---|
103 | if __name__ == '__main__': |
---|
104 | main() |
---|