Changeset a8aaef3 for interfaces


Ignore:
Timestamp:
Oct 21, 2009, 4:12:42 PM (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:
c85da04
Parents:
2d1d5ce
Message:

interfaces/python/py-cvec.c: write setters for norm and phas

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/py-cvec.c

    r2d1d5ce ra8aaef3  
    216216}
    217217
    218 PyObject *
    219 PyAubio_ArrayToCvecNorm (PyObject * self)
    220 {
    221   return NULL;
    222 }
    223218
    224219PyObject *
     
    258253
    259254static int
    260 Py_cvec_set_norm (Py_cvec * self, PyObject *value, void * closure)
    261 {
     255Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
     256{
     257  uint_t i;
     258  PyObject * array;
     259  if (input == NULL) {
     260    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     261    goto fail;
     262  }
     263  if (PyArray_Check(input)) {
     264
     265    // we got an array, convert it to a cvec.norm
     266    if (PyArray_NDIM (input) == 0) {
     267      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     268      goto fail;
     269    } else if (PyArray_NDIM (input) > 2) {
     270      PyErr_SetString (PyExc_ValueError,
     271          "input array has more than two dimensions");
     272      goto fail;
     273    }
     274
     275    if (!PyArray_ISFLOAT (input)) {
     276      PyErr_SetString (PyExc_ValueError, "input array should be float");
     277      goto fail;
     278    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
     279      PyErr_SetString (PyExc_ValueError, "input array should be float32");
     280      goto fail;
     281    }
     282    array = input;
     283
     284    // check input array dimensions
     285    if (PyArray_NDIM (array) == 1) {
     286      if (vec->channels != 1) {
     287          PyErr_SetString (PyExc_ValueError,
     288                  "input array should have more than one channel");
     289          goto fail;
     290      }
     291      if (vec->o->length != PyArray_SIZE (array)) {
     292          PyErr_Format (PyExc_ValueError,
     293                  "input array has length %d, but cvec has length %d",
     294                  PyArray_SIZE (array), vec->o->length);
     295          goto fail;
     296      }
     297    } else {
     298      if (vec->channels != PyArray_DIM (array, 0)) {
     299          PyErr_Format (PyExc_ValueError,
     300                  "input array has %d channels, but vector has %d channels",
     301                  PyArray_DIM (array, 0), vec->channels);
     302          goto fail;
     303      }
     304      if (vec->o->length != PyArray_DIM (array, 1)) {
     305          PyErr_Format (PyExc_ValueError,
     306                  "input array has length %d, but vector has length %d",
     307                  PyArray_DIM (array, 1), vec->o->length);
     308          goto fail;
     309      }
     310    }
     311
     312    for (i = 0; i < vec->channels; i++) {
     313      vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
     314    }
     315
     316  } else {
     317    PyErr_SetString (PyExc_ValueError, "can only accept array as input");
     318    return 1;
     319  }
     320
     321  Py_INCREF(array);
    262322  return 0;
     323
     324fail:
     325  return 1;
    263326}
    264327
    265328static int
    266 Py_cvec_set_phas (Py_cvec * self, PyObject *value, void * closure)
    267 {
     329Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
     330{
     331  uint_t i;
     332  PyObject * array;
     333  if (input == NULL) {
     334    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     335    goto fail;
     336  }
     337  if (PyArray_Check(input)) {
     338
     339    // we got an array, convert it to a cvec.phas
     340    if (PyArray_NDIM (input) == 0) {
     341      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     342      goto fail;
     343    } else if (PyArray_NDIM (input) > 2) {
     344      PyErr_SetString (PyExc_ValueError,
     345          "input array has more than two dimensions");
     346      goto fail;
     347    }
     348
     349    if (!PyArray_ISFLOAT (input)) {
     350      PyErr_SetString (PyExc_ValueError, "input array should be float");
     351      goto fail;
     352    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
     353      PyErr_SetString (PyExc_ValueError, "input array should be float32");
     354      goto fail;
     355    }
     356    array = input;
     357
     358    // check input array dimensions
     359    if (PyArray_NDIM (array) == 1) {
     360      if (vec->channels != 1) {
     361          PyErr_SetString (PyExc_ValueError,
     362                  "input array should have more than one channel");
     363          goto fail;
     364      }
     365      if (vec->o->length != PyArray_SIZE (array)) {
     366          PyErr_Format (PyExc_ValueError,
     367                  "input array has length %d, but cvec has length %d",
     368                  PyArray_SIZE (array), vec->o->length);
     369          goto fail;
     370      }
     371    } else {
     372      if (vec->channels != PyArray_DIM (array, 0)) {
     373          PyErr_Format (PyExc_ValueError,
     374                  "input array has %d channels, but vector has %d channels",
     375                  PyArray_DIM (array, 0), vec->channels);
     376          goto fail;
     377      }
     378      if (vec->o->length != PyArray_DIM (array, 1)) {
     379          PyErr_Format (PyExc_ValueError,
     380                  "input array has length %d, but vector has length %d",
     381                  PyArray_DIM (array, 1), vec->o->length);
     382          goto fail;
     383      }
     384    }
     385
     386    for (i = 0; i < vec->channels; i++) {
     387      vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
     388    }
     389
     390  } else {
     391    PyErr_SetString (PyExc_ValueError, "can only accept array as input");
     392    return 1;
     393  }
     394
     395  Py_INCREF(array);
    268396  return 0;
     397
     398fail:
     399  return 1;
    269400}
    270401
     
    337468  {"__array__", (PyCFunction) PyAubio_CvecToArray, METH_NOARGS,
    338469      "Returns the content of this cvec as a numpy array"},
    339 /*
    340   {"norm", (PyCFunction) PyAubio_CvecNormToArray, METH_NOARGS,
    341       "Returns the content of the magnitude of this cvec as a numpy array."},
    342   {"phas", (PyCFunction) PyAubio_CvecPhasToArray, METH_NOARGS,
    343       "Returns the content of the phase of this cvec as a numpy array."},
    344 */
    345470  {NULL}
    346471};
Note: See TracChangeset for help on using the changeset viewer.