Showing the changes to python (9 of 24 files). Show all
Unified · Split
| @@ -372,6 +372,10 @@ static PyMethodDef aubio_methods[] = { · 371 unmodified lines | ||
| 372 | 372 | {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc}, |
| 373 | 373 | {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc}, |
| 374 | 374 | {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc}, |
| 375 | + {"hztomel", Py_aubio_hztomel, METH_VARARGS|METH_KEYWORDS, Py_aubio_hztomel_doc}, | |
| 376 | + {"meltohz", Py_aubio_meltohz, METH_VARARGS|METH_KEYWORDS, Py_aubio_meltohz_doc}, | |
| 377 | + {"hztomel_htk", Py_aubio_hztomel_htk, METH_VARARGS, Py_aubio_hztomel_htk_doc}, | |
| 378 | + {"meltohz_htk", Py_aubio_meltohz_htk, METH_VARARGS, Py_aubio_meltohz_htk_doc}, | |
| 375 | 379 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 376 | 380 | }; |
| 377 | 381 | |
| @@ -138,8 +138,14 @@ Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) · 137 unmodified lines | ||
| 138 | 138 | err = aubio_filterbank_set_triangle_bands (self->o, |
| 139 | 139 | &(self->freqs), samplerate); |
| 140 | 140 | if (err > 0) { |
| 141 | − PyErr_SetString (PyExc_ValueError, | |
| 142 | − "error when running set_triangle_bands"); | |
| 141 | + if (PyErr_Occurred() == NULL) { | |
| 142 | + PyErr_SetString (PyExc_ValueError, "error running set_triangle_bands"); | |
| 143 | + } else { | |
| 144 | + // change the RuntimeError into ValueError | |
| 145 | + PyObject *type, *value, *traceback; | |
| 146 | + PyErr_Fetch(&type, &value, &traceback); | |
| 147 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 148 | + } | |
| 143 | 149 | return NULL; |
| 144 | 150 | } |
| 145 | 151 | Py_RETURN_NONE; |
| @@ -150,15 +156,79 @@ Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) · 4 unmodified lines | ||
| 150 | 156 | { |
| 151 | 157 | uint_t err = 0; |
| 152 | 158 | |
| 153 | − uint_t samplerate; | |
| 154 | − if (!PyArg_ParseTuple (args, "I", &samplerate)) { | |
| 159 | + smpl_t samplerate; | |
| 160 | + if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR, &samplerate)) { | |
| 155 | 161 | return NULL; |
| 156 | 162 | } |
| 157 | 163 | |
| 158 | 164 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
| 159 | 165 | if (err > 0) { |
| 160 | − PyErr_SetString (PyExc_ValueError, | |
| 161 | − "error when running set_mel_coeffs_slaney"); | |
| 166 | + if (PyErr_Occurred() == NULL) { | |
| 167 | + PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_slaney"); | |
| 168 | + } else { | |
| 169 | + // change the RuntimeError into ValueError | |
| 170 | + PyObject *type, *value, *traceback; | |
| 171 | + PyErr_Fetch(&type, &value, &traceback); | |
| 172 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 173 | + } | |
| 174 | + return NULL; | |
| 175 | + } | |
| 176 | + Py_RETURN_NONE; | |
| 177 | +} | |
| 178 | + | |
| 179 | +static PyObject * | |
| 180 | +Py_filterbank_set_mel_coeffs (Py_filterbank * self, PyObject *args) | |
| 181 | +{ | |
| 182 | + uint_t err = 0; | |
| 183 | + | |
| 184 | + smpl_t samplerate; | |
| 185 | + smpl_t freq_min; | |
| 186 | + smpl_t freq_max; | |
| 187 | + if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR | |
| 188 | + AUBIO_NPY_SMPL_CHR, &samplerate, &freq_min, &freq_max)) { | |
| 189 | + return NULL; | |
| 190 | + } | |
| 191 | + | |
| 192 | + err = aubio_filterbank_set_mel_coeffs (self->o, samplerate, | |
| 193 | + freq_min, freq_max); | |
| 194 | + if (err > 0) { | |
| 195 | + if (PyErr_Occurred() == NULL) { | |
| 196 | + PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs"); | |
| 197 | + } else { | |
| 198 | + // change the RuntimeError into ValueError | |
| 199 | + PyObject *type, *value, *traceback; | |
| 200 | + PyErr_Fetch(&type, &value, &traceback); | |
| 201 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 202 | + } | |
| 203 | + return NULL; | |
| 204 | + } | |
| 205 | + Py_RETURN_NONE; | |
| 206 | +} | |
| 207 | + | |
| 208 | +static PyObject * | |
| 209 | +Py_filterbank_set_mel_coeffs_htk (Py_filterbank * self, PyObject *args) | |
| 210 | +{ | |
| 211 | + uint_t err = 0; | |
| 212 | + | |
| 213 | + smpl_t samplerate; | |
| 214 | + smpl_t freq_min; | |
| 215 | + smpl_t freq_max; | |
| 216 | + if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR | |
| 217 | + AUBIO_NPY_SMPL_CHR, &samplerate, &freq_min, &freq_max)) { | |
| 218 | + return NULL; | |
| 219 | + } | |
| 220 | + | |
| 221 | + err = aubio_filterbank_set_mel_coeffs_htk (self->o, samplerate, | |
| 222 | + freq_min, freq_max); | |
| 223 | + if (err > 0) { | |
| 224 | + if (PyErr_Occurred() == NULL) { | |
| 225 | + PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_htk"); | |
| 226 | + } else { | |
| 227 | + // change the RuntimeError into ValueError | |
| 228 | + PyObject *type, *value, *traceback; | |
| 229 | + PyErr_Fetch(&type, &value, &traceback); | |
| 230 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 231 | + } | |
| 162 | 232 | return NULL; |
| 163 | 233 | } |
| 164 | 234 | Py_RETURN_NONE; |
| @@ -195,15 +265,69 @@ Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused) · 30 unmodified lines | ||
| 195 | 265 | aubio_filterbank_get_coeffs (self->o) ); |
| 196 | 266 | } |
| 197 | 267 | |
| 268 | +static PyObject * | |
| 269 | +Py_filterbank_set_power(Py_filterbank *self, PyObject *args) | |
| 270 | +{ | |
| 271 | + uint_t power; | |
| 272 | + | |
| 273 | + if (!PyArg_ParseTuple (args, "I", &power)) { | |
| 274 | + return NULL; | |
| 275 | + } | |
| 276 | + if(aubio_filterbank_set_power (self->o, power)) { | |
| 277 | + if (PyErr_Occurred() == NULL) { | |
| 278 | + PyErr_SetString (PyExc_ValueError, | |
| 279 | + "error running filterbank.set_power"); | |
| 280 | + } else { | |
| 281 | + // change the RuntimeError into ValueError | |
| 282 | + PyObject *type, *value, *traceback; | |
| 283 | + PyErr_Fetch(&type, &value, &traceback); | |
| 284 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 285 | + } | |
| 286 | + return NULL; | |
| 287 | + } | |
| 288 | + Py_RETURN_NONE; | |
| 289 | +} | |
| 290 | + | |
| 291 | +static PyObject * | |
| 292 | +Py_filterbank_set_norm(Py_filterbank *self, PyObject *args) | |
| 293 | +{ | |
| 294 | + uint_t playing; | |
| 295 | + | |
| 296 | + if (!PyArg_ParseTuple (args, "I", &playing)) { | |
| 297 | + return NULL; | |
| 298 | + } | |
| 299 | + if(aubio_filterbank_set_norm (self->o, playing)) { | |
| 300 | + if (PyErr_Occurred() == NULL) { | |
| 301 | + PyErr_SetString (PyExc_ValueError, | |
| 302 | + "error running filterbank.set_power"); | |
| 303 | + } else { | |
| 304 | + // change the RuntimeError into ValueError | |
| 305 | + PyObject *type, *value, *traceback; | |
| 306 | + PyErr_Fetch(&type, &value, &traceback); | |
| 307 | + PyErr_Restore(PyExc_ValueError, value, traceback); | |
| 308 | + } | |
| 309 | + return NULL; | |
| 310 | + } | |
| 311 | + Py_RETURN_NONE; | |
| 312 | +} | |
| 313 | + | |
| 198 | 314 | static PyMethodDef Py_filterbank_methods[] = { |
| 199 | 315 | {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands, |
| 200 | 316 | METH_VARARGS, "set coefficients of filterbanks"}, |
| 201 | 317 | {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, |
| 202 | 318 | METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"}, |
| 319 | + {"set_mel_coeffs", (PyCFunction) Py_filterbank_set_mel_coeffs, | |
| 320 | + METH_VARARGS, "set coefficients of filterbank to linearly spaced mel scale"}, | |
| 321 | + {"set_mel_coeffs_htk", (PyCFunction) Py_filterbank_set_mel_coeffs_htk, | |
| 322 | + METH_VARARGS, "set coefficients of filterbank to linearly spaced mel scale"}, | |
| 203 | 323 | {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, |
| 204 | 324 | METH_NOARGS, "get coefficients of filterbank"}, |
| 205 | 325 | {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, |
| 206 | 326 | METH_VARARGS, "set coefficients of filterbank"}, |
| 327 | + {"set_power", (PyCFunction) Py_filterbank_set_power, | |
| 328 | + METH_VARARGS, "set power applied to filterbank input spectrum"}, | |
| 329 | + {"set_norm", (PyCFunction) Py_filterbank_set_norm, | |
| 330 | + METH_VARARGS, "set norm applied to filterbank input spectrum"}, | |
| 207 | 331 | {NULL} |
| 208 | 332 | }; |
| 209 | 333 | |
| @@ -181,3 +181,57 @@ Py_aubio_ishift(PyObject *self, PyObject *args) · 180 unmodified lines | ||
| 181 | 181 | //Py_RETURN_NONE; |
| 182 | 182 | return (PyObject *) PyAubio_CFvecToArray(&vec); |
| 183 | 183 | } |
| 184 | + | |
| 185 | +PyObject* | |
| 186 | +Py_aubio_hztomel(PyObject *self, PyObject *args, PyObject *kwds) | |
| 187 | +{ | |
| 188 | + smpl_t v; | |
| 189 | + PyObject *htk = NULL; | |
| 190 | + static char *kwlist[] = {"f", "htk", NULL}; | |
| 191 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, AUBIO_NPY_SMPL_CHR "|O", | |
| 192 | + kwlist, &v, &htk)) | |
| 193 | + { | |
| 194 | + return NULL; | |
| 195 | + } | |
| 196 | + if (htk != NULL && PyObject_IsTrue(htk) == 1) | |
| 197 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_hztomel_htk(v)); | |
| 198 | + else | |
| 199 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_hztomel(v)); | |
| 200 | +} | |
| 201 | + | |
| 202 | +PyObject* | |
| 203 | +Py_aubio_meltohz(PyObject *self, PyObject *args, PyObject *kwds) | |
| 204 | +{ | |
| 205 | + smpl_t v; | |
| 206 | + PyObject *htk = NULL; | |
| 207 | + static char *kwlist[] = {"m", "htk", NULL}; | |
| 208 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, AUBIO_NPY_SMPL_CHR "|O", | |
| 209 | + kwlist, &v, &htk)) | |
| 210 | + { | |
| 211 | + return NULL; | |
| 212 | + } | |
| 213 | + if (htk != NULL && PyObject_IsTrue(htk) == 1) | |
| 214 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_meltohz_htk(v)); | |
| 215 | + else | |
| 216 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_meltohz(v)); | |
| 217 | +} | |
| 218 | + | |
| 219 | +PyObject* | |
| 220 | +Py_aubio_hztomel_htk(PyObject *self, PyObject *args) | |
| 221 | +{ | |
| 222 | + smpl_t v; | |
| 223 | + if (!PyArg_ParseTuple(args, AUBIO_NPY_SMPL_CHR, &v)) { | |
| 224 | + return NULL; | |
| 225 | + } | |
| 226 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_hztomel_htk(v)); | |
| 227 | +} | |
| 228 | + | |
| 229 | +PyObject* | |
| 230 | +Py_aubio_meltohz_htk(PyObject *self, PyObject *args) | |
| 231 | +{ | |
| 232 | + smpl_t v; | |
| 233 | + if (!PyArg_ParseTuple(args, AUBIO_NPY_SMPL_CHR, &v)) { | |
| 234 | + return NULL; | |
| 235 | + } | |
| 236 | + return Py_BuildValue(AUBIO_NPY_SMPL_CHR, aubio_meltohz_htk(v)); | |
| 237 | +} | |
| @@ -300,4 +300,72 @@ static char Py_aubio_ishift_doc[] = "" · 299 unmodified lines | ||
| 300 | 300 | ""; |
| 301 | 301 | PyObject * Py_aubio_ishift(PyObject *self, PyObject *args); |
| 302 | 302 | |
| 303 | +static char Py_aubio_hztomel_doc[] = "" | |
| 304 | +"hztomel(f, htk=False)\n" | |
| 305 | +"\n" | |
| 306 | +"Convert a scalar from frequency to mel scale.\n" | |
| 307 | +"\n" | |
| 308 | +"Parameters\n" | |
| 309 | +"----------\n" | |
| 310 | +"m : float\n" | |
| 311 | +" input frequency, in Hz\n" | |
| 312 | +"htk : bool\n" | |
| 313 | +" if `True`, use Htk mel scale instead of Slaney.\n" | |
| 314 | +"\n" | |
| 315 | +"Returns\n" | |
| 316 | +"-------\n" | |
| 317 | +"float\n" | |
| 318 | +" output mel\n" | |
| 319 | +"\n" | |
| 320 | +"See Also\n" | |
| 321 | +"--------\n" | |
| 322 | +"meltohz\n" | |
| 323 | +""; | |
| 324 | +PyObject * Py_aubio_hztomel(PyObject *self, PyObject *args); | |
| 325 | + | |
| 326 | +static char Py_aubio_meltohz_doc[] = "" | |
| 327 | +"meltohz(m, htk=False)\n" | |
| 328 | +"\n" | |
| 329 | +"Convert a scalar from mel scale to frequency.\n" | |
| 330 | +"\n" | |
| 331 | +"Parameters\n" | |
| 332 | +"----------\n" | |
| 333 | +"m : float\n" | |
| 334 | +" input mel\n" | |
| 335 | +"htk : bool\n" | |
| 336 | +" if `True`, use Htk mel scale instead of Slaney.\n" | |
| 337 | +"\n" | |
| 338 | +"Returns\n" | |
| 339 | +"-------\n" | |
| 340 | +"float\n" | |
| 341 | +" output frequency, in Hz\n" | |
| 342 | +"\n" | |
| 343 | +"See Also\n" | |
| 344 | +"--------\n" | |
| 345 | +"hztomel\n" | |
| 346 | +""; | |
| 347 | +PyObject * Py_aubio_meltohz(PyObject *self, PyObject *args); | |
| 348 | + | |
| 349 | +static char Py_aubio_hztomel_htk_doc[] = "" | |
| 350 | +"hztomel_htk(m)\n" | |
| 351 | +"\n" | |
| 352 | +"Same as `hztomel(m, htk=True)`\n" | |
| 353 | +"\n" | |
| 354 | +"See Also\n" | |
| 355 | +"--------\n" | |
| 356 | +"hztomel\n" | |
| 357 | +""; | |
| 358 | +PyObject * Py_aubio_hztomel_htk(PyObject *self, PyObject *args); | |
| 359 | + | |
| 360 | +static char Py_aubio_meltohz_htk_doc[] = "" | |
| 361 | +"meltohz_htk(m)\n" | |
| 362 | +"\n" | |
| 363 | +"Same as `meltohz(m, htk=True)`\n" | |
| 364 | +"\n" | |
| 365 | +"See Also\n" | |
| 366 | +"--------\n" | |
| 367 | +"meltohz\n" | |
| 368 | +""; | |
| 369 | +PyObject * Py_aubio_meltohz_htk(PyObject *self, PyObject *args); | |
| 370 | + | |
| 303 | 371 | #endif /* PY_AUBIO_MUSICUTILS_H */ |
| @@ -462,22 +462,35 @@ Pyaubio_{shortname}_{method} (Py_{shortname} * self, PyObject * args) · 461 unmodified lines | ||
| 462 | 462 | // {shortname} setters |
| 463 | 463 | """.format(**self.__dict__) |
| 464 | 464 | for set_param in self.prototypes['set']: |
| 465 | − params = get_params_types_names(set_param)[1] | |
| 466 | − paramtype = params['type'] | |
| 465 | + params = get_params_types_names(set_param)[1:] | |
| 466 | + param = self.shortname.split('_set_')[-1] | |
| 467 | + paramdecls = "".join([""" | |
| 468 | + {0} {1};""".format(p['type'], p['name']) for p in params]) | |
| 467 | 469 | method_name = get_name(set_param) |
| 468 | 470 | param = method_name.split('aubio_'+self.shortname+'_set_')[-1] |
| 469 | − pyparamtype = pyargparse_chars[paramtype] | |
| 471 | + refs = ", ".join(["&%s" % p['name'] for p in params]) | |
| 472 | + paramlist = ", ".join(["%s" % p['name'] for p in params]) | |
| 473 | + if len(params): | |
| 474 | + paramlist = "," + paramlist | |
| 475 | + pyparamtypes = ''.join([pyargparse_chars[p['type']] for p in params]) | |
| 470 | 476 | out += """ |
| 471 | 477 | static PyObject * |
| 472 | 478 | Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args) |
| 473 | 479 | {{ |
| 474 | 480 | uint_t err = 0; |
| 475 | − {paramtype} {param}; | |
| 481 | + {paramdecls} | |
| 482 | +""".format(param = param, paramdecls = paramdecls, **self.__dict__) | |
| 476 | 483 | |
| 477 | − if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{ | |
| 484 | + if len(refs) and len(pyparamtypes): | |
| 485 | + out += """ | |
| 486 | + | |
| 487 | + if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{ | |
| 478 | 488 | return NULL; |
| 479 | 489 | }} |
| 480 | − err = aubio_{shortname}_set_{param} (self->o, {param}); | |
| 490 | +""".format(pyparamtypes = pyparamtypes, refs = refs) | |
| 491 | + | |
| 492 | + out += """ | |
| 493 | + err = aubio_{shortname}_set_{param} (self->o {paramlist}); | |
| 481 | 494 | |
| 482 | 495 | if (err > 0) {{ |
| 483 | 496 | if (PyErr_Occurred() == NULL) {{ |
| @@ -492,7 +505,8 @@ Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args) · 8 unmodified lines | ||
| 492 | 505 | }} |
| 493 | 506 | Py_RETURN_NONE; |
| 494 | 507 | }} |
| 495 | −""".format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__) | |
| 508 | +""".format(param = param, refs = refs, paramdecls = paramdecls, | |
| 509 | + pyparamtypes = pyparamtypes, paramlist = paramlist, **self.__dict__) | |
| 496 | 510 | return out |
| 497 | 511 | |
| 498 | 512 | def gen_get(self): |
| @@ -141,6 +141,37 @@ class aubio_fft_test_case(TestCase): · 140 unmodified lines | ||
| 141 | 141 | assert_almost_equal ( r[0], impulse, decimal = 6) |
| 142 | 142 | assert_almost_equal ( r[1:], 0) |
| 143 | 143 | |
| 144 | +class aubio_fft_odd_sizes(TestCase): | |
| 145 | + | |
| 146 | + def test_reconstruct_with_odd_size(self): | |
| 147 | + win_s = 29 | |
| 148 | + self.recontruct(win_s, 'odd sizes not supported') | |
| 149 | + | |
| 150 | + def test_reconstruct_with_radix15(self): | |
| 151 | + win_s = 2 ** 4 * 15 | |
| 152 | + self.recontruct(win_s, 'radix 15 supported') | |
| 153 | + | |
| 154 | + def test_reconstruct_with_radix5(self): | |
| 155 | + win_s = 2 ** 4 * 5 | |
| 156 | + self.recontruct(win_s, 'radix 5 supported') | |
| 157 | + | |
| 158 | + def test_reconstruct_with_radix3(self): | |
| 159 | + win_s = 2 ** 4 * 3 | |
| 160 | + self.recontruct(win_s, 'radix 3 supported') | |
| 161 | + | |
| 162 | + def recontruct(self, win_s, skipMessage): | |
| 163 | + try: | |
| 164 | + f = fft(win_s) | |
| 165 | + except RuntimeError: | |
| 166 | + self.skipTest(skipMessage) | |
| 167 | + input_signal = fvec(win_s) | |
| 168 | + input_signal[win_s//2] = 1 | |
| 169 | + c = f(input_signal) | |
| 170 | + output_signal = f.rdo(c) | |
| 171 | + assert_almost_equal(input_signal, output_signal) | |
| 172 | + | |
| 173 | +class aubio_fft_wrong_params(TestCase): | |
| 174 | + | |
| 144 | 175 | def test_large_input_timegrain(self): |
| 145 | 176 | win_s = 1024 |
| 146 | 177 | f = fft(win_s) |
| @@ -169,22 +200,11 @@ class aubio_fft_test_case(TestCase): · 22 unmodified lines | ||
| 169 | 200 | with self.assertRaises(ValueError): |
| 170 | 201 | f.rdo(s) |
| 171 | 202 | |
| 172 | −class aubio_fft_wrong_params(TestCase): | |
| 173 | − | |
| 174 | 203 | def test_wrong_buf_size(self): |
| 175 | 204 | win_s = -1 |
| 176 | 205 | with self.assertRaises(ValueError): |
| 177 | 206 | fft(win_s) |
| 178 | 207 | |
| 179 | − def test_buf_size_not_power_of_two(self): | |
| 180 | − # when compiled with fftw3, aubio supports non power of two fft sizes | |
| 181 | − win_s = 320 | |
| 182 | − try: | |
| 183 | − with self.assertRaises(RuntimeError): | |
| 184 | − fft(win_s) | |
| 185 | − except AssertionError: | |
| 186 | − self.skipTest('creating aubio.fft with size %d did not fail' % win_s) | |
| 187 | − | |
| 188 | 208 | def test_buf_size_too_small(self): |
| 189 | 209 | win_s = 1 |
| 190 | 210 | with self.assertRaises(RuntimeError): |
| @@ -109,6 +109,63 @@ class aubio_filterbank_mel_test_case(TestCase): · 108 unmodified lines | ||
| 109 | 109 | f.set_triangle_bands(fvec(freq_list), samplerate) |
| 110 | 110 | |
| 111 | 111 | |
| 112 | + def test_triangle_freqs_without_norm(self): | |
| 113 | + """make sure set_triangle_bands works without """ | |
| 114 | + samplerate = 22050 | |
| 115 | + freq_list = fvec([0, 100, 1000, 10000]) | |
| 116 | + f = filterbank(len(freq_list) - 2, 1024) | |
| 117 | + f.set_norm(0) | |
| 118 | + f.set_triangle_bands(freq_list, samplerate) | |
| 119 | + expected = f.get_coeffs() | |
| 120 | + f.set_norm(1) | |
| 121 | + f.set_triangle_bands(fvec(freq_list), samplerate) | |
| 122 | + assert_almost_equal(f.get_coeffs().T, | |
| 123 | + expected.T * 2. / (freq_list[2:] - freq_list[:-2])) | |
| 124 | + | |
| 125 | + def test_triangle_freqs_wrong_norm(self): | |
| 126 | + f = filterbank(10, 1024) | |
| 127 | + with self.assertRaises(ValueError): | |
| 128 | + f.set_norm(-1) | |
| 129 | + | |
| 130 | + def test_triangle_freqs_with_power(self): | |
| 131 | + f = filterbank(9, 1024) | |
| 132 | + freqs = fvec([40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, | |
| 133 | + 24000]) | |
| 134 | + f.set_power(2) | |
| 135 | + f.set_triangle_bands(freqs, 48000) | |
| 136 | + spec = cvec(1024) | |
| 137 | + spec.norm[:] = .1 | |
| 138 | + expected = fvec([0.02070313, 0.02138672, 0.02127604, 0.02135417, | |
| 139 | + 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) | |
| 140 | + expected /= 100. | |
| 141 | + assert_almost_equal(f(spec), expected) | |
| 142 | + | |
| 143 | + def test_mel_coeffs(self): | |
| 144 | + f = filterbank(40, 1024) | |
| 145 | + f.set_mel_coeffs(44100, 0, 44100 / 2) | |
| 146 | + | |
| 147 | + def test_zero_fmax(self): | |
| 148 | + f = filterbank(40, 1024) | |
| 149 | + f.set_mel_coeffs(44100, 0, 0) | |
| 150 | + | |
| 151 | + def test_wrong_mel_coeffs(self): | |
| 152 | + f = filterbank(40, 1024) | |
| 153 | + with self.assertRaises(ValueError): | |
| 154 | + f.set_mel_coeffs_slaney(0) | |
| 155 | + with self.assertRaises(ValueError): | |
| 156 | + f.set_mel_coeffs(44100, 0, -44100 / 2) | |
| 157 | + with self.assertRaises(ValueError): | |
| 158 | + f.set_mel_coeffs(44100, -0.1, 44100 / 2) | |
| 159 | + with self.assertRaises(ValueError): | |
| 160 | + f.set_mel_coeffs(-44100, 0.1, 44100 / 2) | |
| 161 | + with self.assertRaises(ValueError): | |
| 162 | + f.set_mel_coeffs_htk(-1, 0, 0) | |
| 163 | + | |
| 164 | + def test_mel_coeffs_htk(self): | |
| 165 | + f = filterbank(40, 1024) | |
| 166 | + f.set_mel_coeffs_htk(44100, 0, 44100 / 2) | |
| 167 | + | |
| 168 | + | |
| 112 | 169 | if __name__ == '__main__': |
| 113 | 170 | from unittest import main |
| 114 | 171 | main() |
| @@ -0,0 +1,109 @@ | ||
| 1 | +#! /usr/bin/env python | |
| 2 | + | |
| 3 | +from unittest import main | |
| 4 | +from numpy.testing import TestCase | |
| 5 | +from numpy.testing import assert_equal, assert_almost_equal | |
| 6 | +import numpy as np | |
| 7 | +import aubio | |
| 8 | + | |
| 9 | +from aubio import hztomel, meltohz | |
| 10 | +from aubio import hztomel_htk, meltohz_htk | |
| 11 | + | |
| 12 | + | |
| 13 | +class aubio_hztomel_test_case(TestCase): | |
| 14 | + | |
| 15 | + def test_hztomel(self): | |
| 16 | + assert_equal(hztomel(0.), 0.) | |
| 17 | + assert_almost_equal(hztomel(400. / 3.), 2., decimal=5) | |
| 18 | + assert_almost_equal(hztomel(1000. / 3), 5.) | |
| 19 | + assert_equal(hztomel(200.), 3.) | |
| 20 | + assert_almost_equal(hztomel(1000.), 15) | |
| 21 | + assert_almost_equal(hztomel(6400), 42) | |
| 22 | + assert_almost_equal(hztomel(40960), 69) | |
| 23 | + | |
| 24 | + for m in np.linspace(0, 1000, 100): | |
| 25 | + assert_almost_equal(hztomel(meltohz(m)) - m, 0, decimal=3) | |
| 26 | + | |
| 27 | + def test_meltohz(self): | |
| 28 | + assert_equal(meltohz(0.), 0.) | |
| 29 | + assert_almost_equal(meltohz(2), 400. / 3., decimal=4) | |
| 30 | + assert_equal(meltohz(3.), 200.) | |
| 31 | + assert_almost_equal(meltohz(5), 1000. / 3., decimal=4) | |
| 32 | + assert_almost_equal(meltohz(15), 1000., decimal=4) | |
| 33 | + assert_almost_equal(meltohz(42), 6400., decimal=2) | |
| 34 | + assert_almost_equal(meltohz(69), 40960., decimal=1) | |
| 35 | + | |
| 36 | + for f in np.linspace(0, 20000, 1000): | |
| 37 | + assert_almost_equal(meltohz(hztomel(f)) - f, 0, decimal=1) | |
| 38 | + | |
| 39 | + def test_meltohz_negative(self): | |
| 40 | + # TODO add assert_warns | |
| 41 | + assert_equal(meltohz(-1), 0) | |
| 42 | + | |
| 43 | + def test_hztomel_negative(self): | |
| 44 | + # TODO add assert_warns | |
| 45 | + assert_equal(hztomel(-1), 0) | |
| 46 | + | |
| 47 | + | |
| 48 | +class aubio_hztomel_htk_test_case(TestCase): | |
| 49 | + | |
| 50 | + def test_meltohz(self): | |
| 51 | + assert_equal(meltohz(0, htk=True), 0) | |
| 52 | + assert_almost_equal(meltohz(2595, htk=True), 6300., decimal=1) | |
| 53 | + | |
| 54 | + def test_hztomel(self): | |
| 55 | + assert_equal(hztomel(0, htk=True), 0) | |
| 56 | + assert_almost_equal(hztomel(3428.7, htk=True), 2000., decimal=1) | |
| 57 | + assert_almost_equal(hztomel(6300, htk=True), 2595., decimal=1) | |
| 58 | + | |
| 59 | + def test_meltohz_negative(self): | |
| 60 | + # TODO add assert_warns | |
| 61 | + assert_equal(meltohz(-1, htk=True), 0) | |
| 62 | + assert_almost_equal(meltohz(2000, htk=True), 3428.7, decimal=1) | |
| 63 | + assert_almost_equal(meltohz(1000, htk=True), 1000., decimal=1) | |
| 64 | + | |
| 65 | + def test_hztomel_negative(self): | |
| 66 | + # TODO add assert_warns | |
| 67 | + assert_equal(hztomel(-1, htk=True), 0) | |
| 68 | + assert_almost_equal(hztomel(1000, htk=True), 1000., decimal=1) | |
| 69 | + | |
| 70 | + def test_hztomel_htk(self): | |
| 71 | + for f in np.linspace(0, 20000, 1000): | |
| 72 | + assert_almost_equal(meltohz_htk(hztomel_htk(f)) - f, 0, decimal=1) | |
| 73 | + for f in np.linspace(0, 20000, 1000): | |
| 74 | + assert_almost_equal(hztomel_htk(meltohz_htk(f)) - f, 0, decimal=1) | |
| 75 | + | |
| 76 | + | |
| 77 | +class aubio_hztomel_wrong_values(TestCase): | |
| 78 | + """ more tests to cover all branches """ | |
| 79 | + | |
| 80 | + def test_hztomel_wrong_values(self): | |
| 81 | + with self.assertRaises(TypeError): | |
| 82 | + hztomel('s') | |
| 83 | + | |
| 84 | + def test_meltohz_wrong_values(self): | |
| 85 | + with self.assertRaises(TypeError): | |
| 86 | + meltohz(bytes('ad')) | |
| 87 | + | |
| 88 | + def test_meltohz_no_arg(self): | |
| 89 | + with self.assertRaises(TypeError): | |
| 90 | + meltohz() | |
| 91 | + | |
| 92 | + def test_meltohz_htk_no_arg(self): | |
| 93 | + with self.assertRaises(TypeError): | |
| 94 | + meltohz_htk() | |
| 95 | + | |
| 96 | + def test_hztomel_htk_wrong_values(self): | |
| 97 | + with self.assertRaises(TypeError): | |
| 98 | + hztomel_htk('0') | |
| 99 | + | |
| 100 | + def test_hztomel_htk_false(self): | |
| 101 | + assert hztomel(120, htk=False) == hztomel(120) | |
| 102 | + | |
| 103 | + def test_meltohz_htk_false(self): | |
| 104 | + assert meltohz(12, htk=False) == meltohz(12) | |
| 105 | + | |
| 106 | + | |
| 107 | +if __name__ == '__main__': | |
| 108 | + from unittest import main | |
| 109 | + main() | |
| @@ -0,0 +1,93 @@ | ||
| 1 | +#! /usr/bin/env python | |
| 2 | + | |
| 3 | +"""A brute force test using `sink` to create and write samples to a stereo | |
| 4 | +file, then `source` to check the correct content is read from the files.""" | |
| 5 | + | |
| 6 | +import os.path | |
| 7 | +import unittest | |
| 8 | +import aubio | |
| 9 | +import numpy as np | |
| 10 | +from numpy.testing import assert_equal | |
| 11 | +from utils import get_tmp_sink_path | |
| 12 | + | |
| 13 | +class aubio_source_test_case(unittest.TestCase): | |
| 14 | + | |
| 15 | + def test_read_from_mono(self): | |
| 16 | + out = get_tmp_sink_path() | |
| 17 | + samplerate = 44100 | |
| 18 | + hop_size = 256 | |
| 19 | + blocks = 10 | |
| 20 | + channels = 1 | |
| 21 | + write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | |
| 22 | + write_samples *= .5 | |
| 23 | + self.check_write_and_read(samplerate, channels, hop_size, blocks, | |
| 24 | + write_samples) | |
| 25 | + | |
| 26 | + def test_read_from_stereo(self): | |
| 27 | + out = get_tmp_sink_path() | |
| 28 | + samplerate = 44100 | |
| 29 | + hop_size = 256 | |
| 30 | + blocks = 10 | |
| 31 | + channels = 1 | |
| 32 | + write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | |
| 33 | + write_samples *= .5 | |
| 34 | + self.check_write_and_read(samplerate, channels, hop_size, blocks, | |
| 35 | + write_samples) | |
| 36 | + | |
| 37 | + def test_read_from_half_stereo(self): | |
| 38 | + samplerate = 16000 | |
| 39 | + channels = 2 | |
| 40 | + hop_size = 512 | |
| 41 | + blocks = 10 | |
| 42 | + write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | |
| 43 | + write_samples *= .5 | |
| 44 | + write_samples[1, :] = 0 | |
| 45 | + self.check_write_and_read(samplerate, channels, hop_size, blocks, | |
| 46 | + write_samples) | |
| 47 | + | |
| 48 | + def test_read_from_cancelling_channels(self): | |
| 49 | + samplerate = 16000 | |
| 50 | + channels = 2 | |
| 51 | + hop_size = 512 | |
| 52 | + blocks = 10 | |
| 53 | + write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | |
| 54 | + write_samples *= .5 | |
| 55 | + write_samples[1] *= -1 | |
| 56 | + self.check_write_and_read(samplerate, channels, hop_size, blocks, | |
| 57 | + write_samples) | |
| 58 | + | |
| 59 | + def test_read_from_strange_three_channels(self): | |
| 60 | + samplerate = 8000 | |
| 61 | + channels = 3 | |
| 62 | + hop_size = 123 | |
| 63 | + blocks = 10 | |
| 64 | + write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | |
| 65 | + write_samples *= .5 | |
| 66 | + write_samples[1, :] = 0 | |
| 67 | + self.check_write_and_read(samplerate, channels, hop_size, blocks, | |
| 68 | + write_samples) | |
| 69 | + | |
| 70 | + def check_write_and_read(self, samplerate, channels, | |
| 71 | + hop_size, blocks, write_samples): | |
| 72 | + expected_mono = np.sum(write_samples, axis=0)/write_samples.shape[0] | |
| 73 | + out = get_tmp_sink_path() | |
| 74 | + snk = aubio.sink(out, samplerate, channels=channels) | |
| 75 | + for i in range(blocks): | |
| 76 | + snk.do_multi(write_samples, hop_size) | |
| 77 | + # close the sink before reading from it | |
| 78 | + snk.close() | |
| 79 | + | |
| 80 | + src = aubio.source(out, samplerate, hop_size) | |
| 81 | + for i in range(blocks): | |
| 82 | + read_samples, read = src.do_multi() | |
| 83 | + assert_equal (read_samples, write_samples) | |
| 84 | + assert_equal (read, hop_size) | |
| 85 | + | |
| 86 | + src.seek(0) | |
| 87 | + for i in range(blocks): | |
| 88 | + read_samples, read = src() | |
| 89 | + assert_equal (read, hop_size) | |
| 90 | + assert_equal (read_samples, expected_mono) | |
| 91 | + | |
| 92 | +if __name__ == '__main__': | |
| 93 | + unittest.main() | |