From 31a09d22e4c09793f206793e627b5c163df56019 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Fri, 10 Jul 2015 02:26:27 +0200 Subject: [PATCH] ext/py-musicutils.c: add silence_detection --- python/ext/aubiomodule.c | 1 + python/ext/py-musicutils.c | 31 +++++++++++++++++++++++++++++++ python/ext/py-musicutils.h | 14 ++++++++++++++ python/tests/test_musicutils.py | 22 +++++++++++++++++++++- 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index 9f058649..d36c27db 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -242,6 +242,7 @@ static PyMethodDef aubio_methods[] = { {"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}, + {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc}, {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, {NULL, NULL} /* Sentinel */ }; diff --git a/python/ext/py-musicutils.c b/python/ext/py-musicutils.c index 7e62e06e..4e5f6ed4 100644 --- a/python/ext/py-musicutils.c +++ b/python/ext/py-musicutils.c @@ -80,3 +80,34 @@ Py_aubio_db_spl(PyObject *self, PyObject *args) return db_spl; } + +PyObject * +Py_aubio_silence_detection(PyObject *self, PyObject *args) +{ + PyObject *input; + fvec_t *vec; + PyObject *silence_detection; + smpl_t threshold; + + if (!PyArg_ParseTuple (args, "Of:silence_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; + } + + silence_detection = Py_BuildValue("I", aubio_silence_detection(vec, threshold)); + if (silence_detection == NULL) { + PyErr_SetString (PyExc_ValueError, "failed computing silence_detection"); + return NULL; + } + + return silence_detection; +} diff --git a/python/ext/py-musicutils.h b/python/ext/py-musicutils.h index 45850041..8960db1d 100644 --- a/python/ext/py-musicutils.h +++ b/python/ext/py-musicutils.h @@ -45,4 +45,18 @@ static char Py_aubio_db_spl_doc[] = "" PyObject * Py_aubio_db_spl(PyObject *self, PyObject *args); +static char Py_aubio_silence_detection_doc[] = "" +"Check if buffer level in dB SPL is under a given threshold\n" +"\n" +"Return 0 if level is under the given threshold, 1 otherwise.\n" +"\n" +"Example\n" +"-------\n" +"\n" +">>> import numpy\n""" +">>> silence_detection(numpy.ones(1024, dtype=\"float32\"), -80)\n" +"0"; + +PyObject * Py_aubio_silence_detection(PyObject *self, PyObject *args); + #endif /* _PY_AUBIO_MUSICUTILS_H_ */ diff --git a/python/tests/test_musicutils.py b/python/tests/test_musicutils.py index 10702698..95f8e056 100755 --- a/python/tests/test_musicutils.py +++ b/python/tests/test_musicutils.py @@ -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 +from aubio import window, level_lin, db_spl, silence_detection from aubio import fvec @@ -75,6 +75,26 @@ class aubio_db_spl(TestCase): from numpy import ones assert_equal(db_spl(-ones(1024, dtype="float32")), 0.) +class aubio_silence_detection(TestCase): + def test_accept_fvec(self): + silence_detection(fvec(1024), -70.) + + def test_fail_not_fvec(self): + try: + silence_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 silence_detection(fvec(1024), -70) == 1 + + def test_minus_ones_is_zero(self): + from numpy import ones + assert silence_detection(ones(1024, dtype="float32"), -70) == 0 + if __name__ == '__main__': from unittest import main main() -- 2.11.0