Changeset 34d0c25 for python/ext


Ignore:
Timestamp:
May 11, 2016, 4:54:06 AM (9 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:
1ad9dc3
Parents:
ece990f
Message:

python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector

Location:
python/ext
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • python/ext/aubio-types.h

    rece990f r34d0c25  
    5959
    6060// defined in aubio-proxy.c
     61extern int PyAubio_IsValidVector (PyObject *input);
     62
    6163extern PyObject *PyAubio_CFvecToArray (fvec_t * self);
    6264extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out);
  • python/ext/aubioproxy.c

    rece990f r34d0c25  
    2121
    2222int
    23 PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
     23PyAubio_IsValidVector (PyObject * input) {
    2424  if (input == NULL) {
    2525    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     
    4747    }
    4848
    49     // vec = new_fvec (vec->length);
    50     // no need to really allocate fvec, just its struct member
    5149    long length = PyArray_SIZE ((PyArrayObject *)input);
    5250    if (length <= 0) {
     
    6058  } else {
    6159    PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
     60    return 0;
     61  }
     62  return 1;
     63}
     64
     65int
     66PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
     67
     68  if (!PyAubio_IsValidVector(input)){
    6269    return 0;
    6370  }
  • python/ext/py-cvec.c

    rece990f r34d0c25  
    155155Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
    156156{
    157   PyArrayObject * array;
    158   if (input == NULL) {
    159     PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    160     goto fail;
    161   }
    162   if (PyArray_Check(input)) {
    163     // we got an array, convert it to a cvec.norm
    164     if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    165       PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    166       goto fail;
    167     } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
    168       PyErr_SetString (PyExc_ValueError,
    169           "input array has more than two dimensions");
    170       goto fail;
    171     }
    172 
    173     if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    174       PyErr_SetString (PyExc_ValueError, "input array should be float");
    175       goto fail;
    176     } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    177       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    178       goto fail;
    179     }
    180     array = (PyArrayObject *)input;
    181 
    182     // check input array dimensions
    183     if (PyArray_NDIM (array) != 1) {
    184       PyErr_Format (PyExc_ValueError,
    185           "input array has %d dimensions, not 1",
    186           PyArray_NDIM (array));
    187       goto fail;
    188     } else {
    189       if (vec->length != PyArray_SIZE (array)) {
    190           PyErr_Format (PyExc_ValueError,
    191                   "input array has length %d, but cvec has length %d",
    192                   (int)PyArray_SIZE (array), vec->length);
    193           goto fail;
    194       }
    195     }
    196 
    197     Py_XDECREF(vec->norm);
    198     vec->norm = input;
    199     Py_INCREF(vec->norm);
    200 
    201   } else {
    202     PyErr_SetString (PyExc_ValueError, "can only accept array as input");
    203     return 1;
    204   }
    205 
     157  if (!PyAubio_IsValidVector(input)) {
     158    return 1;
     159  }
     160  long length = PyArray_SIZE ((PyArrayObject *)input);
     161  if (length != vec->length) {
     162    PyErr_Format (PyExc_ValueError,
     163        "input array has length %ld, but cvec has length %d", length,
     164        vec->length);
     165    return 1;
     166  }
     167
     168  Py_XDECREF(vec->norm);
     169  vec->norm = input;
     170  Py_INCREF(vec->norm);
    206171  return 0;
    207 
    208 fail:
    209   return 1;
    210172}
    211173
     
    213175Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
    214176{
    215   PyArrayObject * array;
    216   if (input == NULL) {
    217     PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    218     goto fail;
    219   }
    220   if (PyArray_Check(input)) {
    221 
    222     // we got an array, convert it to a cvec.phas
    223     if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    224       PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    225       goto fail;
    226     } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
    227       PyErr_SetString (PyExc_ValueError,
    228           "input array has more than two dimensions");
    229       goto fail;
    230     }
    231 
    232     if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    233       PyErr_SetString (PyExc_ValueError, "input array should be float");
    234       goto fail;
    235     } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    236       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    237       goto fail;
    238     }
    239     array = (PyArrayObject *)input;
    240 
    241     // check input array dimensions
    242     if (PyArray_NDIM (array) != 1) {
    243       PyErr_Format (PyExc_ValueError,
    244           "input array has %d dimensions, not 1",
    245           PyArray_NDIM (array));
    246       goto fail;
    247     } else {
    248       if (vec->length != PyArray_SIZE (array)) {
    249           PyErr_Format (PyExc_ValueError,
    250                   "input array has length %d, but cvec has length %d",
    251                   (int)PyArray_SIZE (array), vec->length);
    252           goto fail;
    253       }
    254     }
    255 
    256     Py_XDECREF(vec->phas);
    257     vec->phas = input;
    258     Py_INCREF(vec->phas);
    259 
    260   } else {
    261     PyErr_SetString (PyExc_ValueError, "can only accept array as input");
    262     return 1;
    263   }
    264 
     177  if (!PyAubio_IsValidVector(input)) {
     178    return 1;
     179  }
     180  long length = PyArray_SIZE ((PyArrayObject *)input);
     181  if (length != vec->length) {
     182    PyErr_Format (PyExc_ValueError,
     183        "input array has length %ld, but cvec has length %d", length,
     184        vec->length);
     185    return 1;
     186  }
     187
     188  Py_XDECREF(vec->phas);
     189  vec->phas = input;
     190  Py_INCREF(vec->phas);
    265191  return 0;
    266 
    267 fail:
    268   return 1;
    269192}
    270193
Note: See TracChangeset for help on using the changeset viewer.