Showing the changes to python (4 of 4 files). Show all
Unified · Split
| @@ -243,6 +243,7 @@ static PyMethodDef aubio_methods[] = { · 242 unmodified lines | |||
| 243 | {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc}, | 243 | {"level_lin", Py_aubio_level_lin, METH_VARARGS, Py_aubio_level_lin_doc}, |
| 244 | {"db_spl", Py_aubio_db_spl, METH_VARARGS, Py_aubio_db_spl_doc}, | 244 | {"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}, | 245 | {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc}, |
| 246 | {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc}, | ||
| 246 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, | 247 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, |
| 247 | {NULL, NULL} /* Sentinel */ | 248 | {NULL, NULL} /* Sentinel */ |
| 248 | }; | 249 | }; |
| @@ -111,3 +111,34 @@ Py_aubio_silence_detection(PyObject *self, PyObject *args) · 110 unmodified lines | |||
| 111 | 111 | ||
| 112 | return silence_detection; | 112 | return silence_detection; |
| 113 | } | 113 | } |
| 114 | |||
| 115 | PyObject * | ||
| 116 | Py_aubio_level_detection(PyObject *self, PyObject *args) | ||
| 117 | { | ||
| 118 | PyObject *input; | ||
| 119 | fvec_t *vec; | ||
| 120 | PyObject *level_detection; | ||
| 121 | smpl_t threshold; | ||
| 122 | |||
| 123 | if (!PyArg_ParseTuple (args, "Of:level_detection", &input, &threshold)) { | ||
| 124 | PyErr_SetString (PyExc_ValueError, "failed parsing arguments"); | ||
| 125 | return NULL; | ||
| 126 | } | ||
| 127 | |||
| 128 | if (input == NULL) { | ||
| 129 | return NULL; | ||
| 130 | } | ||
| 131 | |||
| 132 | vec = PyAubio_ArrayToCFvec (input); | ||
| 133 | if (vec == NULL) { | ||
| 134 | return NULL; | ||
| 135 | } | ||
| 136 | |||
| 137 | level_detection = Py_BuildValue("f", aubio_level_detection(vec, threshold)); | ||
| 138 | if (level_detection == NULL) { | ||
| 139 | PyErr_SetString (PyExc_ValueError, "failed computing level_detection"); | ||
| 140 | return NULL; | ||
| 141 | } | ||
| 142 | |||
| 143 | return level_detection; | ||
| 144 | } | ||
| @@ -59,4 +59,16 @@ static char Py_aubio_silence_detection_doc[] = "" · 58 unmodified lines | |||
| 59 | 59 | ||
| 60 | PyObject * Py_aubio_silence_detection(PyObject *self, PyObject *args); | 60 | PyObject * Py_aubio_silence_detection(PyObject *self, PyObject *args); |
| 61 | 61 | ||
| 62 | static char Py_aubio_level_detection_doc[] = "" | ||
| 63 | "Get buffer level in dB SPL if over a given threshold, 1. otherwise.\n" | ||
| 64 | "\n" | ||
| 65 | "Example\n" | ||
| 66 | "-------\n" | ||
| 67 | "\n" | ||
| 68 | ">>> import numpy\n""" | ||
| 69 | ">>> level_detection(0.7*numpy.ones(1024, dtype=\"float32\"), -80)\n" | ||
| 70 | "0"; | ||
| 71 | |||
| 72 | PyObject * Py_aubio_level_detection(PyObject *self, PyObject *args); | ||
| 73 | |||
| 62 | #endif /* _PY_AUBIO_MUSICUTILS_H_ */ | 74 | #endif /* _PY_AUBIO_MUSICUTILS_H_ */ |
| @@ -5,7 +5,7 @@ from numpy.testing.utils import assert_equal, assert_almost_equal · 4 unmodified lines | |||
| 5 | from numpy import cos, arange | 5 | from numpy import cos, arange |
| 6 | from math import pi | 6 | from math import pi |
| 7 | 7 | ||
| 8 | from aubio import window, level_lin, db_spl, silence_detection | 8 | from aubio import window, level_lin, db_spl, silence_detection, level_detection |
| 9 | 9 | ||
| 10 | from aubio import fvec | 10 | from aubio import fvec |
| 11 | 11 | ||
| @@ -95,6 +95,26 @@ class aubio_silence_detection(TestCase): · 83 unmodified lines | |||
| 95 | from numpy import ones | 95 | from numpy import ones |
| 96 | assert silence_detection(ones(1024, dtype="float32"), -70) == 0 | 96 | assert silence_detection(ones(1024, dtype="float32"), -70) == 0 |
| 97 | 97 | ||
| 98 | class aubio_level_detection(TestCase): | ||
| 99 | def test_accept_fvec(self): | ||
| 100 | level_detection(fvec(1024), -70.) | ||
| 101 | |||
| 102 | def test_fail_not_fvec(self): | ||
| 103 | try: | ||
| 104 | level_detection("default", -70) | ||
| 105 | except ValueError, e: | ||
| 106 | pass | ||
| 107 | else: | ||
| 108 | self.fail('non-number input phase does not raise a TypeError') | ||
| 109 | |||
| 110 | def test_zeros_is_one(self): | ||
| 111 | from math import isinf | ||
| 112 | assert level_detection(fvec(1024), -70) == 1 | ||
| 113 | |||
| 114 | def test_minus_ones_is_zero(self): | ||
| 115 | from numpy import ones | ||
| 116 | assert level_detection(ones(1024, dtype="float32"), -70) == 0 | ||
| 117 | |||
| 98 | if __name__ == '__main__': | 118 | if __name__ == '__main__': |
| 99 | from unittest import main | 119 | from unittest import main |
| 100 | main() | 120 | main() |