Changeset 6b1aafc for interfaces


Ignore:
Timestamp:
Oct 2, 2009, 11:20:31 AM (15 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
207ed19
Parents:
352fd5f
Message:

python/aubiomodule.c: PyAubio_ArrayToFvec to convert numpy array to a Py_fvec, no copy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/aubiomodule.c

    r352fd5f r6b1aafc  
    55#include "aubio-types.h"
    66
    7 static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
    8 
    9 static PyObject *
    10 Py_alpha_norm (PyObject * self, PyObject * args)
    11 {
    12   PyObject *input;
     7Py_fvec *
     8PyAubio_ArrayToFvec (PyObject *input) {
     9  PyObject *array;
    1310  Py_fvec *vec;
    14   smpl_t alpha;
    15   PyObject *result;
    16   PyObject *array;
    1711  uint_t i;
    18 
    19   if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
    20     return NULL;
    21   }
    22 
    23   if (input == NULL) {
    24     return NULL;
    25   }
    26 
    2712  // parsing input object into a Py_fvec
    2813  if (PyObject_TypeCheck (input, &Py_fvecType)) {
     
    3621      goto fail;
    3722    } else if (PyArray_NDIM (input) > 2) {
    38       PyErr_SetString (PyExc_ValueError, "input array has more than two dimensions");
     23      PyErr_SetString (PyExc_ValueError,
     24          "input array has more than two dimensions");
    3925      goto fail;
    4026    }
     
    4329      PyErr_SetString (PyExc_ValueError, "input array should be float");
    4430      goto fail;
    45     } else if (PyArray_TYPE (input) != NPY_FLOAT) {
     31#if AUBIO_DO_CASTING
     32    } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
    4633      // input data type is not float32, casting
    47       array = PyArray_Cast ( (PyArrayObject*) input, NPY_FLOAT);
     34      array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT);
    4835      if (array == NULL) {
    4936        PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
    5037        goto fail;
    5138      }
     39#else
     40    } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
     41      PyErr_SetString (PyExc_ValueError, "input array should be float32");
     42      goto fail;
     43#endif
    5244    } else {
    5345      // input data type is float32, nothing else to do
     
    6557    }
    6658
    67     // FIXME should not need to allocate fvec
    68     vec->o = new_fvec (vec->length, vec->channels);
     59    // no need to really allocate fvec, just its struct member
     60    // vec->o = new_fvec (vec->length, vec->channels);
     61    vec->o = (fvec_t *)malloc(sizeof(fvec_t));
     62    vec->o->length = vec->length; vec->o->channels = vec->channels;
     63    vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
     64    // hat data[i] point to array line
    6965    for (i = 0; i < vec->channels; i++) {
    7066      vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
     
    7672  }
    7773
     74  return vec;
     75
     76fail:
     77  return NULL;
     78}
     79
     80
     81
     82static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
     83
     84static PyObject *
     85Py_alpha_norm (PyObject * self, PyObject * args)
     86{
     87  PyObject *input;
     88  Py_fvec *vec;
     89  smpl_t alpha;
     90  PyObject *result;
     91
     92  if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
     93    return NULL;
     94  }
     95
     96  if (input == NULL) {
     97    return NULL;
     98  }
     99
     100  vec = PyAubio_ArrayToFvec (input);
     101
     102  if (vec == NULL) {
     103    return NULL;
     104  }
     105
    78106  // compute the function
    79   result = Py_BuildValue ("f", vec_alpha_norm (vec->o, alpha));
     107  result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha));
    80108  if (result == NULL) {
    81109    return NULL;
     
    83111
    84112  return result;
    85 
    86 fail:
    87     return NULL;
    88113}
    89114
Note: See TracChangeset for help on using the changeset viewer.