Changeset 93004d5


Ignore:
Timestamp:
Jul 12, 2012, 1:38:18 AM (12 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:
f6cbefe
Parents:
83b385f
Message:

aubioproxy.c: add PyAubio_ArrayToCFmat implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/aubioproxy.c

    r83b385f r93004d5  
    1212  if (PyArray_Check(input)) {
    1313
    14     // we got an array, convert it to an fvec 
     14    // we got an array, convert it to an fvec
    1515    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    1616      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     
    3434
    3535    // vec = new_fvec (vec->length);
    36     // no need to really allocate fvec, just its struct member 
     36    // no need to really allocate fvec, just its struct member
    3737    vec = (fvec_t *)malloc(sizeof(fvec_t));
    3838    vec->length = PyArray_SIZE ((PyArrayObject *)array);
     
    6262Py_cvec *
    6363PyAubio_CCvecToPyCvec (cvec_t * input) {
    64   Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 
     64  Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
    6565  vec->length = input->length;
    6666  vec->o = input;
     
    9898fmat_t *
    9999PyAubio_ArrayToCFmat (PyObject *input) {
     100  PyObject *array;
     101  fmat_t *mat;
     102  uint_t i;
     103  if (input == NULL) {
     104    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     105    goto fail;
     106  }
     107  // parsing input object into a Py_fvec
     108  if (PyArray_Check(input)) {
     109
     110    // we got an array, convert it to an fvec
     111    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
     112      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     113      goto fail;
     114    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
     115      PyErr_SetString (PyExc_ValueError,
     116          "input array has more than two dimensions");
     117      goto fail;
     118    }
     119
     120    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
     121      PyErr_SetString (PyExc_ValueError, "input array should be float");
     122      goto fail;
     123    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
     124      PyErr_SetString (PyExc_ValueError, "input array should be float32");
     125      goto fail;
     126    } else {
     127      // input data type is float32, nothing else to do
     128      array = input;
     129    }
     130
     131    // no need to really allocate fvec, just its struct member
     132    mat = (fmat_t *)malloc(sizeof(fmat_t));
     133    mat->length = PyArray_DIM ((PyArrayObject *)array, 1);
     134    mat->height = PyArray_DIM ((PyArrayObject *)array, 0);
     135    mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height);
     136    for (i=0; i< mat->height; i++) {
     137      mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i);
     138    }
     139
     140  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
     141    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
     142    return NULL;
     143  } else {
     144    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
     145    return NULL;
     146  }
     147
     148  return mat;
     149
     150fail:
    100151  return NULL;
    101152}
Note: See TracChangeset for help on using the changeset viewer.