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