ext/py-musicutils.c: add level_detection (closes #21)
authorPaul Brossier <piem@piem.org>
Fri, 10 Jul 2015 00:34:48 +0000 (02:34 +0200)
committerPaul Brossier <piem@piem.org>
Fri, 10 Jul 2015 00:34:48 +0000 (02:34 +0200)
python/ext/aubiomodule.c
python/ext/py-musicutils.c
python/ext/py-musicutils.h
python/tests/test_musicutils.py

index d36c27d..8278db2 100644 (file)
@@ -243,6 +243,7 @@ static PyMethodDef aubio_methods[] = {
   {"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},
   {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
+  {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
   {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
   {NULL, NULL} /* Sentinel */
 };
index 4e5f6ed..d1e3d01 100644 (file)
@@ -111,3 +111,34 @@ Py_aubio_silence_detection(PyObject *self, PyObject *args)
 
   return silence_detection;
 }
+
+PyObject *
+Py_aubio_level_detection(PyObject *self, PyObject *args)
+{
+  PyObject *input;
+  fvec_t *vec;
+  PyObject *level_detection;
+  smpl_t threshold;
+
+  if (!PyArg_ParseTuple (args, "Of:level_detection", &input, &threshold)) {
+    PyErr_SetString (PyExc_ValueError, "failed parsing arguments");
+    return NULL;
+  }
+
+  if (input == NULL) {
+    return NULL;
+  }
+
+  vec = PyAubio_ArrayToCFvec (input);
+  if (vec == NULL) {
+    return NULL;
+  }
+
+  level_detection = Py_BuildValue("f", aubio_level_detection(vec, threshold));
+  if (level_detection == NULL) {
+    PyErr_SetString (PyExc_ValueError, "failed computing level_detection");
+    return NULL;
+  }
+
+  return level_detection;
+}
index 8960db1..f6d509f 100644 (file)
@@ -59,4 +59,16 @@ static char Py_aubio_silence_detection_doc[] = ""
 
 PyObject * Py_aubio_silence_detection(PyObject *self, PyObject *args);
 
+static char Py_aubio_level_detection_doc[] = ""
+"Get buffer level in dB SPL if over a given threshold, 1. otherwise.\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> import numpy\n"""
+">>> level_detection(0.7*numpy.ones(1024, dtype=\"float32\"), -80)\n"
+"0";
+
+PyObject * Py_aubio_level_detection(PyObject *self, PyObject *args);
+
 #endif /* _PY_AUBIO_MUSICUTILS_H_ */
index 95f8e05..3513289 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, db_spl, silence_detection
+from aubio import window, level_lin, db_spl, silence_detection, level_detection
 
 from aubio import fvec
 
@@ -95,6 +95,26 @@ class aubio_silence_detection(TestCase):
         from numpy import ones
         assert silence_detection(ones(1024, dtype="float32"), -70) == 0
 
+class aubio_level_detection(TestCase):
+    def test_accept_fvec(self):
+        level_detection(fvec(1024), -70.)
+
+    def test_fail_not_fvec(self):
+        try:
+            level_detection("default", -70)
+        except ValueError, e:
+            pass
+        else:
+            self.fail('non-number input phase does not raise a TypeError')
+
+    def test_zeros_is_one(self):
+        from math import isinf
+        assert level_detection(fvec(1024), -70) == 1
+
+    def test_minus_ones_is_zero(self):
+        from numpy import ones
+        assert level_detection(ones(1024, dtype="float32"), -70) == 0
+
 if __name__ == '__main__':
     from unittest import main
     main()