Ignore:
Timestamp:
Jun 22, 2016, 1:00:10 PM (8 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:
4b9443c4
Parents:
60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into notes

File:
1 edited

Legend:

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

    r60fc05b rf264b17  
    11#include "aubio-types.h"
    22
    3 /* cvec type definition 
     3/* cvec type definition
    44
    55class cvec():
    6     def __init__(self, length = 1024):
    7         self.length = length
    8         self.norm = array(length)
    9         self.phas = array(length)
     6    def __new__(self, length = 1024):
     7        self.length = length / 2 + 1
     8        self.norm = np.zeros(length / 2 + 1)
     9        self.phas = np.zeros(length / 2 + 1)
    1010
    1111*/
    1212
     13// special python type for cvec
     14typedef struct
     15{
     16  PyObject_HEAD
     17  PyObject *norm;
     18  PyObject *phas;
     19  uint_t length;
     20} Py_cvec;
     21
    1322static char Py_cvec_doc[] = "cvec object";
     23
     24
     25PyObject *
     26new_py_cvec(uint_t length) {
     27  Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     28  npy_intp dims[] = { length / 2 + 1, 1 };
     29  vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
     30  vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
     31  vec->length = length / 2 + 1;
     32  return (PyObject*)vec;
     33}
     34
     35int
     36PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) {
     37  if (PyObject_TypeCheck (input, &Py_cvecType)) {
     38      Py_cvec * in = (Py_cvec *)input;
     39      i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0);
     40      i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0);
     41      i->length = ((Py_cvec*)input)->length;
     42      return 1;
     43  } else {
     44      PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec");
     45      return 0;
     46  }
     47}
    1448
    1549static PyObject *
     
    2458    return NULL;
    2559  }
    26 
    2760
    2861  self = (Py_cvec *) type->tp_alloc (type, 0);
     
    4881Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
    4982{
    50   self->o = new_cvec ((self->length - 1) * 2);
    51   if (self->o == NULL) {
    52     return -1;
    53   }
    54 
     83  npy_intp dims[] = { self->length, 1 };
     84  self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
     85  self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
    5586  return 0;
    5687}
     
    5990Py_cvec_del (Py_cvec * self)
    6091{
    61   del_cvec (self->o);
    62   self->ob_type->tp_free ((PyObject *) self);
     92  Py_DECREF(self->norm);
     93  Py_DECREF(self->phas);
     94  Py_TYPE(self)->tp_free ((PyObject *) self);
    6395}
    6496
     
    70102  PyObject *result = NULL;
    71103
    72   format = PyString_FromString ("aubio cvec of %d elements");
     104  format = PyUnicode_FromString ("aubio cvec of %d elements");
    73105  if (format == NULL) {
    74106    goto fail;
     
    79111    goto fail;
    80112  }
    81   cvec_print ( self->o );
    82 
    83   result = PyString_Format (format, args);
     113  // hide actual norm / phas content
     114
     115  result = PyUnicode_Format (format, args);
    84116
    85117fail:
     
    91123
    92124PyObject *
    93 PyAubio_CvecNormToArray (Py_cvec * self)
    94 {
    95   npy_intp dims[] = { self->o->length, 1 };
    96   return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm);
    97 }
    98 
    99 
    100 PyObject *
    101 PyAubio_CvecPhasToArray (Py_cvec * self)
    102 {
    103   npy_intp dims[] = { self->o->length, 1 };
    104   return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas);
    105 }
    106 
    107 PyObject *
    108 PyAubio_ArrayToCvecPhas (PyObject * self)
    109 {
    110   return NULL;
    111 }
    112 
    113 PyObject *
    114125Py_cvec_get_norm (Py_cvec * self, void *closure)
    115126{
    116   return PyAubio_CvecNormToArray(self);
     127  // we want self->norm to still exist after our caller return it
     128  Py_INCREF(self->norm);
     129  return (PyObject*)(self->norm);
    117130}
    118131
     
    120133Py_cvec_get_phas (Py_cvec * self, void *closure)
    121134{
    122   return PyAubio_CvecPhasToArray(self);
     135  // we want self->phas to still exist after our caller return it
     136  Py_INCREF(self->phas);
     137  return (PyObject *)(self->phas);
    123138}
    124139
     
    126141Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
    127142{
    128   PyArrayObject * array;
    129   if (input == NULL) {
    130     PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    131     goto fail;
    132   }
    133   if (PyArray_Check(input)) {
    134 
    135     // we got an array, convert it to a cvec.norm
    136     if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    137       PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    138       goto fail;
    139     } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
    140       PyErr_SetString (PyExc_ValueError,
    141           "input array has more than two dimensions");
    142       goto fail;
    143     }
    144 
    145     if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    146       PyErr_SetString (PyExc_ValueError, "input array should be float");
    147       goto fail;
    148     } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    149       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    150       goto fail;
    151     }
    152     array = (PyArrayObject *)input;
    153 
    154     // check input array dimensions
    155     if (PyArray_NDIM (array) != 1) {
    156       PyErr_Format (PyExc_ValueError,
    157           "input array has %d dimensions, not 1",
    158           PyArray_NDIM (array));
    159       goto fail;
    160     } else {
    161       if (vec->o->length != PyArray_SIZE (array)) {
    162           PyErr_Format (PyExc_ValueError,
    163                   "input array has length %d, but cvec has length %d",
    164                   (int)PyArray_SIZE (array), vec->o->length);
    165           goto fail;
    166       }
    167     }
    168 
    169     vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0);
    170 
    171   } else {
    172     PyErr_SetString (PyExc_ValueError, "can only accept array as input");
    173     return 1;
    174   }
    175 
    176   Py_INCREF(array);
     143  npy_intp length;
     144  if (!PyAubio_IsValidVector(input)) {
     145    return 1;
     146  }
     147  length = PyArray_SIZE ((PyArrayObject *)input);
     148  if (length != vec->length) {
     149    PyErr_Format (PyExc_ValueError,
     150        "input array has length %ld, but cvec has length %d", length,
     151        vec->length);
     152    return 1;
     153  }
     154
     155  Py_XDECREF(vec->norm);
     156  vec->norm = input;
     157  Py_INCREF(vec->norm);
    177158  return 0;
    178 
    179 fail:
    180   return 1;
    181159}
    182160
     
    184162Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
    185163{
    186   PyArrayObject * array;
    187   if (input == NULL) {
    188     PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    189     goto fail;
    190   }
    191   if (PyArray_Check(input)) {
    192 
    193     // we got an array, convert it to a cvec.phas
    194     if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    195       PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    196       goto fail;
    197     } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
    198       PyErr_SetString (PyExc_ValueError,
    199           "input array has more than two dimensions");
    200       goto fail;
    201     }
    202 
    203     if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    204       PyErr_SetString (PyExc_ValueError, "input array should be float");
    205       goto fail;
    206     } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    207       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    208       goto fail;
    209     }
    210     array = (PyArrayObject *)input;
    211 
    212     // check input array dimensions
    213     if (PyArray_NDIM (array) != 1) {
    214       PyErr_Format (PyExc_ValueError,
    215           "input array has %d dimensions, not 1",
    216           PyArray_NDIM (array));
    217       goto fail;
    218     } else {
    219       if (vec->o->length != PyArray_SIZE (array)) {
    220           PyErr_Format (PyExc_ValueError,
    221                   "input array has length %d, but cvec has length %d",
    222                   (int)PyArray_SIZE (array), vec->o->length);
    223           goto fail;
    224       }
    225     }
    226 
    227     vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0);
    228 
    229   } else {
    230     PyErr_SetString (PyExc_ValueError, "can only accept array as input");
    231     return 1;
    232   }
    233 
    234   Py_INCREF(array);
     164  npy_intp length;
     165  if (!PyAubio_IsValidVector(input)) {
     166    return 1;
     167  }
     168  length = PyArray_SIZE ((PyArrayObject *)input);
     169  if (length != vec->length) {
     170    PyErr_Format (PyExc_ValueError,
     171        "input array has length %ld, but cvec has length %d", length,
     172        vec->length);
     173    return 1;
     174  }
     175
     176  Py_XDECREF(vec->phas);
     177  vec->phas = input;
     178  Py_INCREF(vec->phas);
    235179  return 0;
    236 
    237 fail:
    238   return 1;
    239180}
    240181
     
    261202
    262203PyTypeObject Py_cvecType = {
    263   PyObject_HEAD_INIT (NULL)
    264   0,                            /* ob_size           */
     204  PyVarObject_HEAD_INIT(NULL, 0)
    265205  "aubio.cvec",                 /* tp_name           */
    266206  sizeof (Py_cvec),             /* tp_basicsize      */
     
    273213  (reprfunc) Py_cvec_repr,      /* tp_repr           */
    274214  0,                            /* tp_as_number      */
    275   0, //&Py_cvec_tp_as_sequence,      /* tp_as_sequence    */
     215  0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence    */
    276216  0,                            /* tp_as_mapping     */
    277217  0,                            /* tp_hash           */
     
    300240  0,                            /* tp_alloc          */
    301241  Py_cvec_new,                  /* tp_new            */
    302 };
     242  0,
     243  0,
     244  0,
     245  0,
     246  0,
     247  0,
     248  0,
     249  0,
     250  0,
     251};
Note: See TracChangeset for help on using the changeset viewer.