From 9b23eb311431d5862a5920b69082cca1a93bcb8e Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 6 Oct 2009 02:49:53 +0200 Subject: [PATCH] interfaces/python: add cvec and filter types --- .../python/a_weighting_test_simple.expected | 2 + interfaces/python/aubio-types.h | 13 +++- interfaces/python/aubiomodule.c | 17 +++-- .../python/c_weighting_test_simple.expected | 2 + interfaces/python/py-fvec.c | 19 ++---- interfaces/python/setup.py | 2 +- interfaces/python/test_aubio.py | 5 +- interfaces/python/test_filter.py | 67 +++++++++++++++++++ 8 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 interfaces/python/a_weighting_test_simple.expected create mode 100644 interfaces/python/c_weighting_test_simple.expected create mode 100644 interfaces/python/test_filter.py diff --git a/interfaces/python/a_weighting_test_simple.expected b/interfaces/python/a_weighting_test_simple.expected new file mode 100644 index 00000000..6cd3ff3d --- /dev/null +++ b/interfaces/python/a_weighting_test_simple.expected @@ -0,0 +1,2 @@ + 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 5.00000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 + 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.27870563e-01 2.58244342e-01 1.18719361e-01 -4.02623805e-02 -5.61812129e-02 -5.24739734e-02 -4.72329276e-02 -4.23394349e-02 -3.79219586e-02 -3.39473148e-02 -3.03724479e-02 -2.71574847e-02 -2.42664433e-02 -2.16669285e-02 -1.93297810e-02 -1.72287543e-02 -1.53402241e-02 -1.36429261e-02 -1.21177207e-02 -1.07473802e-02 diff --git a/interfaces/python/aubio-types.h b/interfaces/python/aubio-types.h index f29020f5..d5609e7b 100644 --- a/interfaces/python/aubio-types.h +++ b/interfaces/python/aubio-types.h @@ -4,8 +4,10 @@ #include #include -#define Py_fvec_default_length 1024 -#define Py_fvec_default_channels 1 +#define Py_default_vector_length 1024 +#define Py_default_vector_channels 1 + +#define Py_aubio_default_samplerate 44100 #ifdef HAVE_AUBIO_DOUBLE #define AUBIO_FLOAT NPY_FLOAT @@ -24,12 +26,17 @@ it. typedef struct { - PyObject_HEAD fvec_t * o; + PyObject_HEAD + fvec_t * o; uint_t length; uint_t channels; } Py_fvec; extern PyTypeObject Py_fvecType; +extern PyTypeObject Py_cvecType; + +extern PyTypeObject Py_filterType; + extern PyObject *PyAubio_FvecToArray (Py_fvec * self); extern Py_fvec *PyAubio_ArrayToFvec (PyObject * self); diff --git a/interfaces/python/aubiomodule.c b/interfaces/python/aubiomodule.c index 89a0c9a1..4bd665f5 100644 --- a/interfaces/python/aubiomodule.c +++ b/interfaces/python/aubiomodule.c @@ -96,15 +96,12 @@ Py_min_removal(PyObject * self, PyObject * args) // since this function does not return, we could return None //return Py_None; // however it is convenient to return the modified vector - //return (PyObject *) PyAubio_FvecToArray(vec); + return (PyObject *) PyAubio_FvecToArray(vec); // or even without converting it back to an array - Py_INCREF(vec); - return (PyObject *)vec; + //Py_INCREF(vec); + //return (PyObject *)vec; } - - - static PyMethodDef aubio_methods[] = { {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, @@ -121,7 +118,9 @@ init_aubio (void) PyObject *m; int err; - if (PyType_Ready (&Py_fvecType) < 0) { + if ((PyType_Ready (&Py_fvecType) < 0) || + (PyType_Ready (&Py_cvecType) < 0) || + (PyType_Ready (&Py_filterType) < 0)) { return; } @@ -140,4 +139,8 @@ init_aubio (void) Py_INCREF (&Py_fvecType); PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType); + Py_INCREF (&Py_cvecType); + PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType); + Py_INCREF (&Py_filterType); + PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType); } diff --git a/interfaces/python/c_weighting_test_simple.expected b/interfaces/python/c_weighting_test_simple.expected new file mode 100644 index 00000000..70e2b159 --- /dev/null +++ b/interfaces/python/c_weighting_test_simple.expected @@ -0,0 +1,2 @@ + 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 5.00000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 + 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.08504281e-01 2.31621372e-01 1.38614617e-01 1.58040475e-02 -9.84900252e-04 -2.72686896e-03 -2.87772967e-03 -2.87932142e-03 -2.86783482e-03 -2.85529016e-03 -2.84270413e-03 -2.83016008e-03 -2.81766458e-03 -2.80521796e-03 -2.79282009e-03 -2.78047079e-03 -2.76816989e-03 -2.75591721e-03 -2.74371257e-03 -2.73155579e-03 diff --git a/interfaces/python/py-fvec.c b/interfaces/python/py-fvec.c index 4532bc33..88b131b7 100644 --- a/interfaces/python/py-fvec.c +++ b/interfaces/python/py-fvec.c @@ -27,8 +27,8 @@ Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) self = (Py_fvec *) type->tp_alloc (type, 0); - self->length = Py_fvec_default_length; - self->channels = Py_fvec_default_channels; + self->length = Py_default_vector_length; + self->channels = Py_default_vector_channels; if (self == NULL) { return NULL; @@ -50,7 +50,6 @@ Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds) return NULL; } - return (PyObject *) self; } @@ -88,6 +87,7 @@ Py_fvec_repr (Py_fvec * self, PyObject * unused) if (args == NULL) { goto fail; } + fvec_print ( self->o ); result = PyString_Format (format, args); @@ -98,18 +98,15 @@ fail: return result; } -static PyObject * -Py_fvec_print (Py_fvec * self, PyObject * unused) -{ - fvec_print (self->o); - return Py_None; -} - Py_fvec * PyAubio_ArrayToFvec (PyObject *input) { PyObject *array; Py_fvec *vec; uint_t i; + if (input == NULL) { + PyErr_SetString (PyExc_ValueError, "input array is not a python object"); + goto fail; + } // parsing input object into a Py_fvec if (PyObject_TypeCheck (input, &Py_fvecType)) { // input is an fvec, nothing else to do @@ -266,8 +263,6 @@ static PyMemberDef Py_fvec_members[] = { }; static PyMethodDef Py_fvec_methods[] = { - {"dump", (PyCFunction) Py_fvec_print, METH_NOARGS, - "Dumps the contents of the vector to stdout."}, {"__array__", (PyCFunction) PyAubio_FvecToArray, METH_NOARGS, "Returns the first channel as a numpy array."}, {NULL} diff --git a/interfaces/python/setup.py b/interfaces/python/setup.py index 17aa301a..6645bcf0 100644 --- a/interfaces/python/setup.py +++ b/interfaces/python/setup.py @@ -3,7 +3,7 @@ from distutils.core import setup, Extension setup(name="_aubio", version="1.0", ext_modules = [ Extension("_aubio", - ["aubiomodule.c", "py-fvec.c"], + ["aubiomodule.c", "py-fvec.c", "py-cvec.c", "py-filter.c"], include_dirs=['../../build/default/src', '../../src' ], library_dirs=['../../build/default/src', '../../src/.libs' ], libraries=['aubio'])]) diff --git a/interfaces/python/test_aubio.py b/interfaces/python/test_aubio.py index d4eb5692..6cb17120 100644 --- a/interfaces/python/test_aubio.py +++ b/interfaces/python/test_aubio.py @@ -1,13 +1,10 @@ from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal as numpy_assert_equal +from numpy.testing import assert_equal from _aubio import * from numpy import array AUBIO_DO_CASTING = 0 -def assert_equal(a, b): - numpy_assert_equal(array(a),array(b)) - class aubiomodule_test_case(TestCase): def setUp(self): diff --git a/interfaces/python/test_filter.py b/interfaces/python/test_filter.py new file mode 100644 index 00000000..23964c04 --- /dev/null +++ b/interfaces/python/test_filter.py @@ -0,0 +1,67 @@ +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from _aubio import * +from numpy import array + +def array_from_text_file(filename, dtype = 'float'): + return array([line.split() for line in open(filename).readlines()], + dtype = dtype) + +class aubio_filter_test_case(TestCase): + + def test_members(self): + f = digital_filter() + assert_equal ([f.channels, f.order, f.samplerate], [1, 7, 44100]) + f = digital_filter(48000, 5, 2) + assert_equal ([f.channels, f.order, f.samplerate], [2, 5, 48000]) + f(fvec()) + + def test_cweighting_error(self): + f = digital_filter (44100, 2, 1) + self.assertRaises ( ValueError, f.set_c_weighting ) + f = digital_filter (44100, 8, 1) + self.assertRaises ( ValueError, f.set_c_weighting ) + f = digital_filter (4000, 5, 1) + self.assertRaises ( ValueError, f.set_c_weighting ) + f = digital_filter (193000, 5, 1) + self.assertRaises ( ValueError, f.set_c_weighting ) + f = digital_filter (193000, 7, 1) + self.assertRaises ( ValueError, f.set_a_weighting ) + f = digital_filter (192000, 5, 1) + self.assertRaises ( ValueError, f.set_a_weighting ) + + def test_c_weighting(self): + expected = array_from_text_file('c_weighting_test_simple.expected') + f = digital_filter(44100, 5, 1) + f.set_c_weighting() + v = fvec(32) + v[0][12] = .5 + u = f(v) + assert_almost_equal (expected[1], u) + + def test_a_weighting(self): + expected = array_from_text_file('a_weighting_test_simple.expected') + f = digital_filter(44100, 7, 1) + f.set_a_weighting() + v = fvec(32) + v[0][12] = .5 + u = f(v) + assert_almost_equal (expected[1], u) + + def test_a_weighting_parted(self): + expected = array_from_text_file('a_weighting_test_simple.expected') + f = digital_filter(44100, 7, 1) + f.set_a_weighting() + v = fvec(16) + v[0][12] = .5 + u = f(v) + assert_almost_equal (expected[1][:16], u) + # one more time + v = fvec(16) + u = f(v) + assert_almost_equal (expected[1][16:], u) + +if __name__ == '__main__': + from unittest import main + main() + -- 2.26.2