[6a50b9e] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[afceccd] | 3 | from numpy.testing import TestCase, assert_equal |
---|
[143cc43] | 4 | from numpy import array, arange, isnan, isinf |
---|
[6a50b9e] | 5 | from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq |
---|
[6514bb6] | 6 | from aubio import unwrap2pi |
---|
[143cc43] | 7 | from aubio import fvec |
---|
| 8 | from math import pi |
---|
[6a50b9e] | 9 | |
---|
| 10 | class aubio_mathutils(TestCase): |
---|
| 11 | |
---|
[6514bb6] | 12 | def test_unwrap2pi(self): |
---|
[143cc43] | 13 | unwrap2pi(int(23)) |
---|
| 14 | unwrap2pi(float(23.)) |
---|
[689106e] | 15 | unwrap2pi(int(23.)) |
---|
[143cc43] | 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) |
---|
[6514bb6] | 28 | |
---|
[143cc43] | 29 | try: |
---|
[689106e] | 30 | print (unwrap2pi(["23.","24.",25.])) |
---|
| 31 | except Exception as e: |
---|
[143cc43] | 32 | pass |
---|
[6a50b9e] | 33 | |
---|
[143cc43] | 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() |
---|
[6a50b9e] | 40 | |
---|
[143cc43] | 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() |
---|
[6a50b9e] | 47 | |
---|
[143cc43] | 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() |
---|
[6a50b9e] | 54 | |
---|
| 55 | def test_freqtomidi(self): |
---|
[689106e] | 56 | a = array(list(range(-20, 50000, 100)) + [ -1e32, 1e32 ]) |
---|
[143cc43] | 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 ) |
---|
[6a50b9e] | 62 | |
---|
| 63 | def test_miditofreq(self): |
---|
[689106e] | 64 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
[143cc43] | 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): |
---|
[689106e] | 72 | a = list(range(-30, 200)) + [-100000, 10000] |
---|
[ddfa6be] | 73 | b = [ miditobin(x, 44100, 512) for x in a ] |
---|
[143cc43] | 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): |
---|
[689106e] | 80 | a = list(range(-100, 512)) |
---|
[143cc43] | 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): |
---|
[689106e] | 88 | a = list(range(-20, 50000, 100)) + [ -1e32, 1e32 ] |
---|
[143cc43] | 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): |
---|
[689106e] | 96 | a = list(range(-20, 148)) |
---|
[143cc43] | 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 ) |
---|
[6a50b9e] | 102 | |
---|
| 103 | if __name__ == '__main__': |
---|
| 104 | from unittest import main |
---|
| 105 | main() |
---|