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 | b = unwrap2pi (a) |
---|
27 | #print zip(a, b) |
---|
28 | |
---|
29 | try: |
---|
30 | print (unwrap2pi(["23.","24.",25.])) |
---|
31 | except Exception as e: |
---|
32 | pass |
---|
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 = [ bintomidi(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 | from unittest import main |
---|
105 | main() |
---|