ext/py-musicutils.c: add db_spl
authorPaul Brossier <piem@piem.org>
Fri, 10 Jul 2015 00:08:22 +0000 (02:08 +0200)
committerPaul Brossier <piem@piem.org>
Fri, 10 Jul 2015 00:08:22 +0000 (02:08 +0200)
python/ext/aubiomodule.c
python/ext/py-musicutils.c
python/tests/test_musicutils.py

index 51c13cb..9f05864 100644 (file)
@@ -241,6 +241,7 @@ static PyMethodDef aubio_methods[] = {
   {"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},
+  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
   {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
   {NULL, NULL} /* Sentinel */
 };
index 44ef85e..7e62e06 100644 (file)
@@ -50,3 +50,33 @@ Py_aubio_level_lin(PyObject *self, PyObject *args)
 
   return level_lin;
 }
+
+PyObject *
+Py_aubio_db_spl(PyObject *self, PyObject *args)
+{
+  PyObject *input;
+  fvec_t *vec;
+  PyObject *db_spl;
+
+  if (!PyArg_ParseTuple (args, "O:db_spl", &input)) {
+    PyErr_SetString (PyExc_ValueError, "failed parsing arguments");
+    return NULL;
+  }
+
+  if (input == NULL) {
+    return NULL;
+  }
+
+  vec = PyAubio_ArrayToCFvec (input);
+  if (vec == NULL) {
+    return NULL;
+  }
+
+  db_spl = Py_BuildValue("f", aubio_db_spl(vec));
+  if (db_spl == NULL) {
+    PyErr_SetString (PyExc_ValueError, "failed computing db_spl");
+    return NULL;
+  }
+
+  return db_spl;
+}
index f252195..1070269 100755 (executable)
@@ -5,7 +5,7 @@ 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 window, level_lin, db_spl
 
 from aubio import fvec
 
@@ -55,6 +55,26 @@ class aubio_level_lin(TestCase):
         from numpy import ones
         assert_equal(level_lin(-ones(1024, dtype="float32")), 1.)
 
+class aubio_db_spl(TestCase):
+    def test_accept_fvec(self):
+        db_spl(fvec(1024))
+
+    def test_fail_not_fvec(self):
+        try:
+            db_spl("default")
+        except ValueError, e:
+            pass
+        else:
+            self.fail('non-number input phase does not raise a TypeError')
+
+    def test_zeros_is_inf(self):
+        from math import isinf
+        assert isinf(db_spl(fvec(1024)))
+
+    def test_minus_ones_is_zero(self):
+        from numpy import ones
+        assert_equal(db_spl(-ones(1024, dtype="float32")), 0.)
+
 if __name__ == '__main__':
     from unittest import main
     main()