ref: 6b1aafc24e21090c4fe820017b442445e1f6ea39
parent: 352fd5f7b20761a99dd66181d8c61e4acbceb5df
author: Paul Brossier <piem@piem.org>
date: Fri Oct 2 07:20:31 EDT 2009
python/aubiomodule.c: PyAubio_ArrayToFvec to convert numpy array to a Py_fvec, no copy
--- a/interfaces/python/aubiomodule.c
+++ b/interfaces/python/aubiomodule.c
@@ -4,26 +4,11 @@
#include "aubio-types.h"
-static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
-
-static PyObject *
-Py_alpha_norm (PyObject * self, PyObject * args)
-{
- PyObject *input;
- Py_fvec *vec;
- smpl_t alpha;
- PyObject *result;
+Py_fvec *
+PyAubio_ArrayToFvec (PyObject *input) {
PyObject *array;
+ Py_fvec *vec;
uint_t i;
-
- if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
- return NULL;
- }
-
- if (input == NULL) {
- return NULL;
- }
-
// parsing input object into a Py_fvec
if (PyObject_TypeCheck (input, &Py_fvecType)) {
// input is an fvec, nothing else to do
@@ -35,7 +20,8 @@
PyErr_SetString (PyExc_ValueError, "input array is a scalar");
goto fail;
} else if (PyArray_NDIM (input) > 2) {
- PyErr_SetString (PyExc_ValueError, "input array has more than two dimensions");
+ PyErr_SetString (PyExc_ValueError,
+ "input array has more than two dimensions");
goto fail;
}
@@ -42,13 +28,19 @@
if (!PyArray_ISFLOAT (input)) {
PyErr_SetString (PyExc_ValueError, "input array should be float");
goto fail;
- } else if (PyArray_TYPE (input) != NPY_FLOAT) {
+#if AUBIO_DO_CASTING
+ } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
// input data type is not float32, casting
- array = PyArray_Cast ( (PyArrayObject*) input, NPY_FLOAT);
+ array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT);
if (array == NULL) {
PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
goto fail;
}
+#else
+ } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
+ PyErr_SetString (PyExc_ValueError, "input array should be float32");
+ goto fail;
+#endif
} else {
// input data type is float32, nothing else to do
array = input;
@@ -64,8 +56,12 @@
vec->length = PyArray_DIM (array, 1);
}
- // FIXME should not need to allocate fvec
- vec->o = new_fvec (vec->length, vec->channels);
+ // no need to really allocate fvec, just its struct member
+ // vec->o = new_fvec (vec->length, vec->channels);
+ vec->o = (fvec_t *)malloc(sizeof(fvec_t));
+ vec->o->length = vec->length; vec->o->channels = vec->channels;
+ vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
+ // hat data[i] point to array line
for (i = 0; i < vec->channels; i++) {
vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
}
@@ -75,16 +71,45 @@
return NULL;
}
+ return vec;
+
+fail:
+ return NULL;
+}
+
+
+
+static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
+
+static PyObject *
+Py_alpha_norm (PyObject * self, PyObject * args)
+{
+ PyObject *input;
+ Py_fvec *vec;
+ smpl_t alpha;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
+ return NULL;
+ }
+
+ if (input == NULL) {
+ return NULL;
+ }
+
+ vec = PyAubio_ArrayToFvec (input);
+
+ if (vec == NULL) {
+ return NULL;
+ }
+
// compute the function
- result = Py_BuildValue ("f", vec_alpha_norm (vec->o, alpha));
+ result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha));
if (result == NULL) {
return NULL;
}
return result;
-
-fail:
- return NULL;
}
static PyMethodDef aubio_methods[] = {