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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.