Changeset 31a09d2


Ignore:
Timestamp:
Jul 10, 2015, 2:26:27 AM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
9c8c8a6
Parents:
665b711
Message:

ext/py-musicutils.c: add silence_detection

Location:
python
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • python/ext/aubiomodule.c

    r665b711 r31a09d2  
    243243  {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc},
    244244  {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc},
     245  {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
    245246  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
    246247  {NULL, NULL} /* Sentinel */
  • python/ext/py-musicutils.c

    r665b711 r31a09d2  
    8181  return db_spl;
    8282}
     83
     84PyObject *
     85Py_aubio_silence_detection(PyObject *self, PyObject *args)
     86{
     87  PyObject *input;
     88  fvec_t *vec;
     89  PyObject *silence_detection;
     90  smpl_t threshold;
     91
     92  if (!PyArg_ParseTuple (args, "Of:silence_detection", &input, &threshold)) {
     93    PyErr_SetString (PyExc_ValueError, "failed parsing arguments");
     94    return NULL;
     95  }
     96
     97  if (input == NULL) {
     98    return NULL;
     99  }
     100
     101  vec = PyAubio_ArrayToCFvec (input);
     102  if (vec == NULL) {
     103    return NULL;
     104  }
     105
     106  silence_detection = Py_BuildValue("I", aubio_silence_detection(vec, threshold));
     107  if (silence_detection == NULL) {
     108    PyErr_SetString (PyExc_ValueError, "failed computing silence_detection");
     109    return NULL;
     110  }
     111
     112  return silence_detection;
     113}
  • python/ext/py-musicutils.h

    r665b711 r31a09d2  
    4646PyObject * Py_aubio_db_spl(PyObject *self, PyObject *args);
    4747
     48static char Py_aubio_silence_detection_doc[] = ""
     49"Check if buffer level in dB SPL is under a given threshold\n"
     50"\n"
     51"Return 0 if level is under the given threshold, 1 otherwise.\n"
     52"\n"
     53"Example\n"
     54"-------\n"
     55"\n"
     56">>> import numpy\n"""
     57">>> silence_detection(numpy.ones(1024, dtype=\"float32\"), -80)\n"
     58"0";
     59
     60PyObject * Py_aubio_silence_detection(PyObject *self, PyObject *args);
     61
    4862#endif /* _PY_AUBIO_MUSICUTILS_H_ */
  • python/tests/test_musicutils.py

    r665b711 r31a09d2  
    66from math import pi
    77
    8 from aubio import window, level_lin, db_spl
     8from aubio import window, level_lin, db_spl, silence_detection
    99
    1010from aubio import fvec
     
    7676        assert_equal(db_spl(-ones(1024, dtype="float32")), 0.)
    7777
     78class aubio_silence_detection(TestCase):
     79    def test_accept_fvec(self):
     80        silence_detection(fvec(1024), -70.)
     81
     82    def test_fail_not_fvec(self):
     83        try:
     84            silence_detection("default", -70)
     85        except ValueError, e:
     86            pass
     87        else:
     88            self.fail('non-number input phase does not raise a TypeError')
     89
     90    def test_zeros_is_one(self):
     91        from math import isinf
     92        assert silence_detection(fvec(1024), -70) == 1
     93
     94    def test_minus_ones_is_zero(self):
     95        from numpy import ones
     96        assert silence_detection(ones(1024, dtype="float32"), -70) == 0
     97
    7898if __name__ == '__main__':
    7999    from unittest import main
Note: See TracChangeset for help on using the changeset viewer.