Changeset 7a7b00f


Ignore:
Timestamp:
Dec 5, 2009, 1:42:46 AM (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:
96fe713
Parents:
f7b05d4
Message:

py-fvec.c, py-cvec.c: towards mono

Location:
interfaces/python
Files:
2 edited

Legend:

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

    rf7b05d4 r7a7b00f  
    44
    55class cvec():
    6     def __init__(self, length = 1024, channels = 1):
     6    def __init__(self, length = 1024):
    77        self.length = length
    8         self.channels = channels
    9         self.norm = array(length, channels)
    10         self.phas = array(length, channels)
     8        self.norm = array(length)
     9        self.phas = array(length)
     10
    1111*/
    12 
    1312
    1413static char Py_cvec_doc[] = "cvec object";
     
    1716Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
    1817{
    19   int length= 0, channels = 0;
     18  int length= 0;
    2019  Py_cvec *self;
    21   static char *kwlist[] = { "length", "channels", NULL };
    22 
    23   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
    24           &length, &channels)) {
     20  static char *kwlist[] = { "length", NULL };
     21
     22  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
     23          &length)) {
    2524    return NULL;
    2625  }
     
    3029
    3130  self->length = Py_default_vector_length / 2 + 1;
    32   self->channels = Py_default_vector_channels;
    3331
    3432  if (self == NULL) {
     
    4442  }
    4543
    46   if (channels > 0) {
    47     self->channels = channels;
    48   } else if (channels < 0) {
    49     PyErr_SetString (PyExc_ValueError,
    50         "can not use negative number of channels");
    51     return NULL;
    52   }
    53 
    5444  return (PyObject *) self;
    5545}
     
    5848Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
    5949{
    60   self->o = new_cvec ((self->length - 1) * 2, self->channels);
     50  self->o = new_cvec ((self->length - 1) * 2);
    6151  if (self->o == NULL) {
    6252    return -1;
     
    8070  PyObject *result = NULL;
    8171
    82   format = PyString_FromString ("aubio cvec of %d elements with %d channels");
     72  format = PyString_FromString ("aubio cvec of %d elements");
    8373  if (format == NULL) {
    8474    goto fail;
    8575  }
    8676
    87   args = Py_BuildValue ("II", self->length, self->channels);
     77  args = Py_BuildValue ("I", self->length);
    8878  if (args == NULL) {
    8979    goto fail;
    9080  }
    91   //cvec_print ( self->o );
     81  cvec_print ( self->o );
    9282
    9383  result = PyString_Format (format, args);
     
    10494  PyObject *array;
    10595  Py_cvec *vec;
    106   uint_t i;
     96  if (input == NULL) {
     97    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     98    goto fail;
     99  }
    107100  // parsing input object into a Py_cvec
    108101  if (PyObject_TypeCheck (input, &Py_cvecType)) {
     
    115108      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    116109      goto fail;
     110    } else if (PyArray_NDIM (input) > 1) {
     111      PyErr_SetString (PyExc_ValueError,
     112          "input array has more than one dimensions");
     113      goto fail;
     114    }
     115
     116    if (!PyArray_ISFLOAT (input)) {
     117      PyErr_SetString (PyExc_ValueError, "input array should be float");
     118      goto fail;
     119    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
     120      PyErr_SetString (PyExc_ValueError, "input array should be float32");
     121      goto fail;
     122    } else {
     123      // input data type is float32, nothing else to do
     124      array = input;
     125    }
     126
     127    // create a new cvec object
     128    vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     129    if (PyArray_NDIM (array) != 2) {
     130      PyErr_SetString (PyExc_ValueError,
     131          "input array should be have exactly two rows for norm and phas");
     132      goto fail;
     133    } else {
     134      vec->length = PyArray_SIZE (array);
     135    }
     136
     137    // no need to really allocate cvec, just its struct member
     138    // vec->o = new_cvec (vec->length);
     139    vec->o = (cvec_t *)malloc(sizeof(cvec_t));
     140    vec->o->length = vec->length;
     141    // have norm and phas point to array rows
     142    vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0);
     143    vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 1);
     144
     145  } else {
     146    PyErr_SetString (PyExc_ValueError, "can only accept array or cvec as input");
     147    return NULL;
     148  }
     149
     150  return vec;
     151
     152fail:
     153  return NULL;
     154}
     155
     156PyObject *
     157PyAubio_CvecToArray (Py_cvec * self)
     158{
     159  PyObject *array = NULL;
     160  npy_intp dims[] = { self->o->length, 1 };
     161  PyObject *concat = PyList_New (0), *tmp = NULL;
     162  tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm);
     163  PyList_Append (concat, tmp);
     164  Py_DECREF (tmp);
     165  tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas);
     166  PyList_Append (concat, tmp);
     167  Py_DECREF (tmp);
     168  array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
     169  Py_DECREF (concat);
     170  return array;
     171}
     172
     173PyObject *
     174PyAubio_CvecNormToArray (Py_cvec * self)
     175{
     176  npy_intp dims[] = { self->o->length, 1 };
     177  return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm);
     178}
     179
     180
     181PyObject *
     182PyAubio_CvecPhasToArray (Py_cvec * self)
     183{
     184  npy_intp dims[] = { self->o->length, 1 };
     185  return PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas);
     186}
     187
     188PyObject *
     189PyAubio_ArrayToCvecPhas (PyObject * self)
     190{
     191  return NULL;
     192}
     193
     194PyObject *
     195Py_cvec_get_norm (Py_cvec * self, void *closure)
     196{
     197  return PyAubio_CvecNormToArray(self);
     198}
     199
     200PyObject *
     201Py_cvec_get_phas (Py_cvec * self, void *closure)
     202{
     203  return PyAubio_CvecPhasToArray(self);
     204}
     205
     206static int
     207Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
     208{
     209  PyObject * array;
     210  if (input == NULL) {
     211    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     212    goto fail;
     213  }
     214  if (PyArray_Check(input)) {
     215
     216    // we got an array, convert it to a cvec.norm
     217    if (PyArray_NDIM (input) == 0) {
     218      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     219      goto fail;
    117220    } else if (PyArray_NDIM (input) > 2) {
    118221      PyErr_SetString (PyExc_ValueError,
     
    124227      PyErr_SetString (PyExc_ValueError, "input array should be float");
    125228      goto fail;
    126 #if AUBIO_DO_CASTING
    127     } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    128       // input data type is not float32, casting
    129       array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);
    130       if (array == NULL) {
    131         PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
    132         goto fail;
    133       }
    134 #else
    135229    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    136230      PyErr_SetString (PyExc_ValueError, "input array should be float32");
    137231      goto fail;
    138 #endif
     232    }
     233    array = input;
     234
     235    // check input array dimensions
     236    if (PyArray_NDIM (array) != 1) {
     237      PyErr_Format (PyExc_ValueError,
     238          "input array has %d dimensions, not 1",
     239          PyArray_NDIM (array));
     240      goto fail;
    139241    } else {
    140       // input data type is float32, nothing else to do
    141       array = input;
    142     }
    143 
    144     // create a new cvec object
    145     vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
    146     if (PyArray_NDIM (array) == 1) {
    147       PyErr_SetString (PyExc_ValueError,
    148           "input array should be have at least two rows for norm and phas");
    149       goto fail;
    150     } else if (PyArray_NDIM (array) == 2) {
    151       vec->channels = 1;
    152       vec->length = PyArray_SIZE (array);
    153     } else {
    154       vec->channels = PyArray_DIM (array, 0) / 2;
    155       vec->length = PyArray_DIM (array, 1);
    156     }
    157 
    158     // no need to really allocate cvec, just its struct member
    159     // vec->o = new_cvec (vec->length, vec->channels);
    160     vec->o = (cvec_t *)malloc(sizeof(cvec_t));
    161     vec->o->length = vec->length; vec->o->channels = vec->channels;
    162     vec->o->norm = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
    163     vec->o->phas = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
    164     // hat data[i] point to array line
    165     for (i = 0; i < vec->channels; i+=2) {
    166       vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
    167       vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i+1);
    168     }
    169 
    170   } else {
    171     PyErr_SetString (PyExc_ValueError, "can only accept array or cvec as input");
    172     return NULL;
    173   }
    174 
    175   return vec;
    176 
    177 fail:
    178   return NULL;
    179 }
    180 
    181 PyObject *
    182 PyAubio_CvecToArray (Py_cvec * self)
    183 {
    184   PyObject *array = NULL;
    185   uint_t i;
    186   npy_intp dims[] = { self->o->length, 1 };
    187   PyObject *concat = PyList_New (0), *tmp = NULL;
    188   for (i = 0; i < self->channels; i++) {
    189     tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);
    190     PyList_Append (concat, tmp);
    191     Py_DECREF (tmp);
    192     tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);
    193     PyList_Append (concat, tmp);
    194     Py_DECREF (tmp);
    195   }
    196   array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
    197   Py_DECREF (concat);
    198   return array;
    199 }
    200 
    201 PyObject *
    202 PyAubio_CvecNormToArray (Py_cvec * self)
    203 {
    204   PyObject *array = NULL;
    205   uint_t i;
    206   npy_intp dims[] = { self->o->length, 1 };
    207   PyObject *concat = PyList_New (0), *tmp = NULL;
    208   for (i = 0; i < self->channels; i++) {
    209     tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);
    210     PyList_Append (concat, tmp);
    211     Py_DECREF (tmp);
    212   }
    213   array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
    214   Py_DECREF (concat);
    215   return array;
    216 }
    217 
    218 
    219 PyObject *
    220 PyAubio_CvecPhasToArray (Py_cvec * self)
    221 {
    222   PyObject *array = NULL;
    223   uint_t i;
    224   npy_intp dims[] = { self->o->length, 1 };
    225   PyObject *concat = PyList_New (0), *tmp = NULL;
    226   for (i = 0; i < self->channels; i++) {
    227     tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);
    228     PyList_Append (concat, tmp);
    229     Py_DECREF (tmp);
    230   }
    231   array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
    232   Py_DECREF (concat);
    233   return array;
    234 }
    235 
    236 PyObject *
    237 PyAubio_ArrayToCvecPhas (PyObject * self)
    238 {
    239   return NULL;
    240 }
    241 
    242 PyObject *
    243 Py_cvec_get_norm (Py_cvec * self, void *closure)
    244 {
    245   return PyAubio_CvecNormToArray(self);
    246 }
    247 
    248 PyObject *
    249 Py_cvec_get_phas (Py_cvec * self, void *closure)
    250 {
    251   return PyAubio_CvecPhasToArray(self);
    252 }
    253 
    254 static int
    255 Py_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       }
    291242      if (vec->o->length != PyArray_SIZE (array)) {
    292243          PyErr_Format (PyExc_ValueError,
     
    295246          goto fail;
    296247      }
    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     }
     248    }
     249
     250    vec->o->norm = (smpl_t *) PyArray_GETPTR1 (array, 0);
    315251
    316252  } else {
     
    329265Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
    330266{
    331   uint_t i;
    332267  PyObject * array;
    333268  if (input == NULL) {
     
    357292
    358293    // 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       }
     294    if (PyArray_NDIM (array) != 1) {
     295      PyErr_Format (PyExc_ValueError,
     296          "input array has %d dimensions, not 1",
     297          PyArray_NDIM (array));
     298      goto fail;
     299    } else {
    365300      if (vec->o->length != PyArray_SIZE (array)) {
    366301          PyErr_Format (PyExc_ValueError,
     
    369304          goto fail;
    370305      }
    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     }
     306    }
     307
     308    vec->o->phas = (smpl_t *) PyArray_GETPTR1 (array, 0);
    389309
    390310  } else {
     
    398318fail:
    399319  return 1;
    400 }
    401 
    402 static Py_ssize_t
    403 Py_cvec_getchannels (Py_cvec * self)
    404 {
    405   return self->channels;
    406 }
    407 
    408 static PyObject *
    409 Py_cvec_getitem (Py_cvec * self, Py_ssize_t index)
    410 {
    411   PyObject *array;
    412 
    413   if (index < 0 || index >= self->channels) {
    414     PyErr_SetString (PyExc_IndexError, "no such channel");
    415     return NULL;
    416   }
    417 
    418   npy_intp dims[] = { self->length, 1 };
    419   array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[index]);
    420   return array;
    421 }
    422 
    423 static int
    424 Py_cvec_setitem (Py_cvec * self, Py_ssize_t index, PyObject * o)
    425 {
    426   PyObject *array;
    427 
    428   if (index < 0 || index >= self->channels) {
    429     PyErr_SetString (PyExc_IndexError, "no such channel");
    430     return -1;
    431   }
    432 
    433   array = PyArray_FROM_OT (o, NPY_FLOAT);
    434   if (array == NULL) {
    435     PyErr_SetString (PyExc_ValueError, "should be an array of float");
    436     goto fail;
    437   }
    438 
    439   if (PyArray_NDIM (array) != 1) {
    440     PyErr_SetString (PyExc_ValueError, "should be a one-dimensional array");
    441     goto fail;
    442   }
    443 
    444   if (PyArray_SIZE (array) != self->length) {
    445     PyErr_SetString (PyExc_ValueError,
    446         "should be an array of same length as target cvec");
    447     goto fail;
    448   }
    449 
    450   self->o->norm[index] = (smpl_t *) PyArray_GETPTR1 (array, 0);
    451 
    452   return 0;
    453 
    454 fail:
    455   return -1;
    456320}
    457321
     
    460324  {"length", T_INT, offsetof (Py_cvec, length), READONLY,
    461325      "length attribute"},
    462   {"channels", T_INT, offsetof (Py_cvec, channels), READONLY,
    463       "channels attribute"},
    464326  {NULL}                        /* Sentinel */
    465327};
     
    480342  {NULL} /* sentinel */
    481343};
    482 
    483 static PySequenceMethods Py_cvec_tp_as_sequence = {
    484   (lenfunc) Py_cvec_getchannels,        /* sq_length         */
    485   0,                                    /* sq_concat         */
    486   0,                                    /* sq_repeat         */
    487   (ssizeargfunc) Py_cvec_getitem,       /* sq_item           */
    488   0,                                    /* sq_slice          */
    489   (ssizeobjargproc) Py_cvec_setitem,    /* sq_ass_item       */
    490   0,                                    /* sq_ass_slice      */
    491   0,                                    /* sq_contains       */
    492   0,                                    /* sq_inplace_concat */
    493   0,                                    /* sq_inplace_repeat */
    494 };
    495 
    496344
    497345PyTypeObject Py_cvecType = {
     
    508356  (reprfunc) Py_cvec_repr,      /* tp_repr           */
    509357  0,                            /* tp_as_number      */
    510   &Py_cvec_tp_as_sequence,      /* tp_as_sequence    */
     358  0, //&Py_cvec_tp_as_sequence,      /* tp_as_sequence    */
    511359  0,                            /* tp_as_mapping     */
    512360  0,                            /* tp_hash           */
  • interfaces/python/py-fvec.c

    rf7b05d4 r7a7b00f  
    44
    55class fvec():
    6     def __init__(self, length = 1024, channels = 1):
     6    def __init__(self, length = 1024):
    77        self.length = length
    8         self.channels = channels
    9         self.data = array(length, channels)
     8        self.data = array(length)
    109
    1110*/
     
    1615Py_fvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
    1716{
    18   int length= 0, channels = 0;
     17  int length= 0;
    1918  Py_fvec *self;
    20   static char *kwlist[] = { "length", "channels", NULL };
    21 
    22   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
    23           &length, &channels)) {
    24     return NULL;
    25   }
    26 
     19  static char *kwlist[] = { "length", NULL };
     20
     21  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
     22          &length)) {
     23    return NULL;
     24  }
    2725
    2826  self = (Py_fvec *) type->tp_alloc (type, 0);
    2927
    3028  self->length = Py_default_vector_length;
    31   self->channels = Py_default_vector_channels;
    3229
    3330  if (self == NULL) {
     
    4340  }
    4441
    45   if (channels > 0) {
    46     self->channels = channels;
    47   } else if (channels < 0) {
    48     PyErr_SetString (PyExc_ValueError,
    49         "can not use negative number of channels");
    50     return NULL;
    51   }
    52 
    5342  return (PyObject *) self;
    5443}
     
    5746Py_fvec_init (Py_fvec * self, PyObject * args, PyObject * kwds)
    5847{
    59   self->o = new_fvec (self->length, self->channels);
     48  self->o = new_fvec (self->length);
    6049  if (self->o == NULL) {
    6150    return -1;
     
    7564Py_fvec_repr (Py_fvec * self, PyObject * unused)
    7665{
     66#if 0
    7767  PyObject *format = NULL;
    7868  PyObject *args = NULL;
    7969  PyObject *result = NULL;
    8070
    81   format = PyString_FromString ("aubio fvec of %d elements with %d channels");
     71  format = PyString_FromString ("aubio fvec of %d elements");
    8272  if (format == NULL) {
    8373    goto fail;
    8474  }
    8575
    86   args = Py_BuildValue ("II", self->length, self->channels);
     76  args = Py_BuildValue ("I", self->length);
    8777  if (args == NULL) {
    8878    goto fail;
     
    9282  result = PyString_Format (format, args);
    9383
     84fail:
     85  Py_XDECREF (format);
     86  Py_XDECREF (args);
     87
     88  return result;
     89#endif
     90  PyObject *format = NULL;
     91  PyObject *args = NULL;
     92  PyObject *result = NULL;
     93
     94  format = PyString_FromString ("%s");
     95  if (format == NULL) {
     96    goto fail;
     97  }
     98
     99  args = Py_BuildValue ("O", PyAubio_FvecToArray (self));
     100  if (args == NULL) {
     101    goto fail;
     102  }
     103
     104  result = PyString_Format (format, args);
    94105fail:
    95106  Py_XDECREF (format);
     
    103114  PyObject *array;
    104115  Py_fvec *vec;
    105   uint_t i;
    106116  if (input == NULL) {
    107117    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     
    118128      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    119129      goto fail;
    120     } else if (PyArray_NDIM (input) > 2) {
     130    } else if (PyArray_NDIM (input) > 1) {
    121131      PyErr_SetString (PyExc_ValueError,
    122           "input array has more than two dimensions");
     132          "input array has more than one dimensions");
    123133      goto fail;
    124134    }
     
    127137      PyErr_SetString (PyExc_ValueError, "input array should be float");
    128138      goto fail;
    129 #if AUBIO_DO_CASTING
    130     } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    131       // input data type is not float32, casting
    132       array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);
    133       if (array == NULL) {
    134         PyErr_SetString (PyExc_IndexError, "failed converting to AUBIO_NPY_SMPL");
    135         goto fail;
    136       }
    137 #else
    138139    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    139140      PyErr_SetString (PyExc_ValueError, "input array should be float32");
    140141      goto fail;
    141 #endif
    142142    } else {
    143143      // input data type is float32, nothing else to do
     
    147147    // create a new fvec object
    148148    vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType);
    149     if (PyArray_NDIM (array) == 1) {
    150       vec->channels = 1;
    151       vec->length = PyArray_SIZE (array);
    152     } else {
    153       vec->channels = PyArray_DIM (array, 0);
    154       vec->length = PyArray_DIM (array, 1);
    155     }
     149    vec->length = PyArray_SIZE (array);
    156150
    157151    // no need to really allocate fvec, just its struct member
    158     // vec->o = new_fvec (vec->length, vec->channels);
     152    // vec->o = new_fvec (vec->length);
    159153    vec->o = (fvec_t *)malloc(sizeof(fvec_t));
    160     vec->o->length = vec->length; vec->o->channels = vec->channels;
    161     vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
    162     // hat data[i] point to array line
    163     for (i = 0; i < vec->channels; i++) {
    164       vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
    165     }
    166 
     154    vec->o->length = vec->length;
     155    vec->o->data = (smpl_t *) PyArray_GETPTR1 (array, 0);
     156
     157  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
     158    PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
     159    return NULL;
    167160  } else {
    168     PyErr_SetString (PyExc_ValueError, "can only accept array or fvec as input");
     161    PyErr_SetString (PyExc_ValueError, "can only accept vector or fvec as input");
    169162    return NULL;
    170163  }
     
    179172PyAubio_CFvecToArray (fvec_t * self)
    180173{
    181   PyObject *array = NULL;
    182   uint_t i;
    183174  npy_intp dims[] = { self->length, 1 };
    184   PyObject *concat = PyList_New (0), *tmp = NULL;
    185   for (i = 0; i < self->channels; i++) {
    186     tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data[i]);
    187     PyList_Append (concat, tmp);
    188     Py_DECREF (tmp);
    189   }
    190   array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
    191   Py_DECREF (concat);
    192   return array;
     175  return  PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data);
    193176}
    194177
     
    197180{
    198181  PyObject *array = NULL;
    199   if (self->channels == 1) {
    200     npy_intp dims[] = { self->length, 1 };
    201     array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[0]);
    202   } else {
    203     uint_t i;
    204     npy_intp dims[] = { self->length, 1 };
    205     PyObject *concat = PyList_New (0), *tmp = NULL;
    206     for (i = 0; i < self->channels; i++) {
    207       tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[i]);
    208       PyList_Append (concat, tmp);
    209       Py_DECREF (tmp);
    210     }
    211     array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
    212     Py_DECREF (concat);
    213   }
     182  npy_intp dims[] = { self->length, 1 };
     183  array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data);
    214184  return array;
    215 }
    216 
    217 static Py_ssize_t
    218 Py_fvec_getchannels (Py_fvec * self)
    219 {
    220   return self->channels;
    221185}
    222186
     
    224188Py_fvec_getitem (Py_fvec * self, Py_ssize_t index)
    225189{
    226   PyObject *array;
    227 
    228   if (index < 0 || index >= self->channels) {
    229     PyErr_SetString (PyExc_IndexError, "no such channel");
    230     return NULL;
    231   }
    232 
    233   npy_intp dims[] = { self->length, 1 };
    234   array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[index]);
    235   return array;
     190  if (index < 0 || index >= self->length) {
     191    PyErr_SetString (PyExc_IndexError, "no such element");
     192    return NULL;
     193  }
     194
     195  return PyFloat_FromDouble (self->o->data[index]);
    236196}
    237197
     
    239199Py_fvec_setitem (Py_fvec * self, Py_ssize_t index, PyObject * o)
    240200{
    241   PyObject *array;
    242 
    243   if (index < 0 || index >= self->channels) {
    244     PyErr_SetString (PyExc_IndexError, "no such channel");
    245     return -1;
    246   }
    247 
    248   array = PyArray_FROM_OT (o, AUBIO_NPY_SMPL);
    249   if (array == NULL) {
    250     PyErr_SetString (PyExc_ValueError, "should be an array of float");
    251     goto fail;
    252   }
    253 
    254   if (PyArray_NDIM (array) != 1) {
    255     PyErr_SetString (PyExc_ValueError, "should be a one-dimensional array");
    256     goto fail;
    257   }
    258 
    259   if (PyArray_SIZE (array) != self->length) {
    260     PyErr_SetString (PyExc_ValueError,
    261         "should be an array of same length as target fvec");
    262     goto fail;
    263   }
    264 
    265   self->o->data[index] = (smpl_t *) PyArray_GETPTR1 (array, 0);
     201
     202  if (index < 0 || index >= self->length) {
     203    PyErr_SetString (PyExc_IndexError, "no such element");
     204    goto fail;
     205  }
     206
     207  if (PyFloat_Check (o)) {
     208    PyErr_SetString (PyExc_ValueError, "should be a float");
     209    goto fail;
     210  }
     211
     212  self->o->data[index] = (smpl_t) PyFloat_AsDouble(o);
    266213
    267214  return 0;
     
    269216fail:
    270217  return -1;
     218}
     219
     220int
     221Py_fvec_get_length (Py_fvec * self)                                                                                                                                     
     222{                                                                                                                                                                       
     223  return self->length;                                                                                                                                                 
    271224}
    272225
     
    275228  {"length", T_INT, offsetof (Py_fvec, length), READONLY,
    276229      "length attribute"},
    277   {"channels", T_INT, offsetof (Py_fvec, channels), READONLY,
    278       "channels attribute"},
    279230  {NULL}                        /* Sentinel */
    280231};
     
    287238
    288239static PySequenceMethods Py_fvec_tp_as_sequence = {
    289   (lenfunc) Py_fvec_getchannels,        /* sq_length         */
     240  (lenfunc) Py_fvec_get_length,        /* sq_length         */
    290241  0,                                    /* sq_concat         */
    291242  0,                                    /* sq_repeat         */
Note: See TracChangeset for help on using the changeset viewer.