ext/py-musicutils.c: add level_lin
authorPaul Brossier <piem@piem.org>
Thu, 9 Jul 2015 23:51:25 +0000 (01:51 +0200)
committerPaul Brossier <piem@piem.org>
Thu, 9 Jul 2015 23:51:25 +0000 (01:51 +0200)
python/ext/aubiomodule.c
python/ext/py-musicutils.c
python/tests/test_musicutils.py

index a54a0cb..51c13cb 100644 (file)
@@ -240,6 +240,7 @@ static PyMethodDef aubio_methods[] = {
   {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
   {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
   {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
+  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
   {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
   {NULL, NULL} /* Sentinel */
 };
index ca21fbe..44ef85e 100644 (file)
@@ -20,3 +20,33 @@ Py_aubio_window(PyObject *self, PyObject *args)
 
   return (PyObject *) PyAubio_CFvecToArray(window);
 }
+
+PyObject *
+Py_aubio_level_lin(PyObject *self, PyObject *args)
+{
+  PyObject *input;
+  fvec_t *vec;
+  PyObject *level_lin;
+
+  if (!PyArg_ParseTuple (args, "O:level_lin", &input)) {
+    PyErr_SetString (PyExc_ValueError, "failed parsing arguments");
+    return NULL;
+  }
+
+  if (input == NULL) {
+    return NULL;
+  }
+
+  vec = PyAubio_ArrayToCFvec (input);
+  if (vec == NULL) {
+    return NULL;
+  }
+
+  level_lin = Py_BuildValue("f", aubio_level_lin(vec));
+  if (level_lin == NULL) {
+    PyErr_SetString (PyExc_ValueError, "failed computing level_lin");
+    return NULL;
+  }
+
+  return level_lin;
+}
index ece079c..f252195 100755 (executable)
@@ -1,8 +1,13 @@
 #! /usr/bin/env python
 
 from numpy.testing import TestCase
-from numpy.testing.utils import assert_almost_equal
-from aubio import window
+from numpy.testing.utils import assert_equal, assert_almost_equal
+from numpy import cos, arange
+from math import pi
+
+from aubio import window, level_lin
+
+from aubio import fvec
 
 class aubio_window(TestCase):
 
@@ -26,13 +31,30 @@ class aubio_window(TestCase):
             self.fail('non-integer window length does not raise a ValueError')
 
     def test_compute_hanning_1024(self):
-        from numpy import cos, arange
-        from math import pi
         size = 1024
         aubio_window = window("hanning", size)
         numpy_window = .5 - .5 * cos(2. * pi * arange(size) / size)
         assert_almost_equal(aubio_window, numpy_window)
 
+class aubio_level_lin(TestCase):
+    def test_accept_fvec(self):
+        level_lin(fvec(1024))
+
+    def test_fail_not_fvec(self):
+        try:
+            level_lin("default")
+        except ValueError, e:
+            pass
+        else:
+            self.fail('non-number input phase does not raise a TypeError')
+
+    def test_zeros_is_zeros(self):
+        assert_equal(level_lin(fvec(1024)), 0.)
+
+    def test_minus_ones_is_one(self):
+        from numpy import ones
+        assert_equal(level_lin(-ones(1024, dtype="float32")), 1.)
+
 if __name__ == '__main__':
     from unittest import main
     main()