Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/ext/aubioproxy.c

    r3194dca1 r911c22f  
    11#include "aubio-types.h"
    22
    3 fvec_t *
    4 PyAubio_ArrayToCFvec (PyObject *input) {
    5   PyObject *array;
    6   fvec_t *vec;
    7   if (input == NULL) {
    8     PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    9     goto fail;
    10   }
    11   // parsing input object into a Py_fvec
    12   if (PyArray_Check(input)) {
     3PyObject *
     4new_py_fvec(uint_t length) {
     5    npy_intp dims[] = { length, 1 };
     6    return PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
     7}
    138
    14     // we got an array, convert it to an fvec
    15     if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    16       PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    17       goto fail;
    18     } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) {
    19       PyErr_SetString (PyExc_ValueError,
    20           "input array has more than one dimensions");
    21       goto fail;
    22     }
    23 
    24     if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    25       PyErr_SetString (PyExc_ValueError, "input array should be float");
    26       goto fail;
    27     } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    28       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    29       goto fail;
    30     } else {
    31       // input data type is float32, nothing else to do
    32       array = input;
    33     }
    34 
    35     // vec = new_fvec (vec->length);
    36     // no need to really allocate fvec, just its struct member
    37     vec = (fvec_t *)malloc(sizeof(fvec_t));
    38     long length = PyArray_SIZE ((PyArrayObject *)array);
    39     if (length > 0) {
    40       vec->length = (uint_t)length;
    41     } else {
    42       PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
    43       goto fail;
    44     }
    45     vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0);
    46 
    47   } else if (PyObject_TypeCheck (input, &PyList_Type)) {
    48     PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
    49     return NULL;
    50   } else {
    51     PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
    52     return NULL;
    53   }
    54 
    55   return vec;
    56 
    57 fail:
    58   return NULL;
     9PyObject *
     10new_py_fmat(uint_t height, uint_t length) {
     11    npy_intp dims[] = { height, length, 1 };
     12    return PyArray_ZEROS(2, dims, AUBIO_NPY_SMPL, 0);
    5913}
    6014
     
    6620}
    6721
    68 Py_cvec *
    69 PyAubio_CCvecToPyCvec (cvec_t * input) {
    70   Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
    71   vec->length = input->length;
    72   vec->o = input;
    73   Py_INCREF(vec);
    74   return vec;
     22int
     23PyAubio_IsValidVector (PyObject * input) {
     24  npy_intp length;
     25  if (input == NULL) {
     26    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
     27    return 0;
     28  }
     29  // parsing input object into a Py_fvec
     30  if (PyArray_Check(input)) {
     31
     32    // we got an array, convert it to an fvec
     33    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
     34      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
     35      return 0;
     36    } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) {
     37      PyErr_SetString (PyExc_ValueError,
     38          "input array has more than one dimensions");
     39      return 0;
     40    }
     41
     42    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
     43      PyErr_SetString (PyExc_ValueError, "input array should be float");
     44      return 0;
     45    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
     46      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
     47      return 0;
     48    }
     49
     50    length = PyArray_SIZE ((PyArrayObject *)input);
     51    if (length <= 0) {
     52      PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
     53      return 0;
     54    }
     55
     56  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
     57    PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
     58    return 0;
     59  } else {
     60    PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
     61    return 0;
     62  }
     63  return 1;
    7564}
    7665
    77 cvec_t *
    78 PyAubio_ArrayToCCvec (PyObject *input) {
    79   if (PyObject_TypeCheck (input, &Py_cvecType)) {
    80       return ((Py_cvec*)input)->o;
    81   } else {
    82       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    83       return NULL;
     66int
     67PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
     68
     69  if (!PyAubio_IsValidVector(input)){
     70    return 0;
    8471  }
     72
     73  out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
     74  out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
     75  return 1;
    8576}
    8677
     
    10293}
    10394
    104 fmat_t *
    105 PyAubio_ArrayToCFmat (PyObject *input) {
    106   PyObject *array;
    107   fmat_t *mat;
    108   uint_t i;
     95int
     96PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) {
     97  uint_t i, new_height;
     98  npy_intp length, height;
    10999  if (input == NULL) {
    110100    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
    111     goto fail;
     101    return 0;
    112102  }
    113103  // parsing input object into a Py_fvec
     
    117107    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
    118108      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
    119       goto fail;
     109      return 0;
    120110    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
    121111      PyErr_SetString (PyExc_ValueError,
    122112          "input array has more than two dimensions");
    123       goto fail;
     113      return 0;
    124114    }
    125115
    126116    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
    127117      PyErr_SetString (PyExc_ValueError, "input array should be float");
    128       goto fail;
     118      return 0;
    129119    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
    130       PyErr_SetString (PyExc_ValueError, "input array should be float32");
    131       goto fail;
    132     } else {
    133       // input data type is float32, nothing else to do
    134       array = input;
     120      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
     121      return 0;
    135122    }
    136123
    137124    // no need to really allocate fvec, just its struct member
    138     mat = (fmat_t *)malloc(sizeof(fmat_t));
    139     long length = PyArray_DIM ((PyArrayObject *)array, 1);
    140     if (length > 0) {
    141       mat->length = (uint_t)length;
    142     } else {
     125    length = PyArray_DIM ((PyArrayObject *)input, 1);
     126    if (length <= 0) {
    143127      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
    144       goto fail;
     128      return 0;
    145129    }
    146     long height = PyArray_DIM ((PyArrayObject *)array, 0);
    147     if (height > 0) {
    148       mat->height = (uint_t)height;
    149     } else {
     130    height = PyArray_DIM ((PyArrayObject *)input, 0);
     131    if (height <= 0) {
    150132      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
    151       goto fail;
    152     }
    153     mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height);
    154     for (i=0; i< mat->height; i++) {
    155       mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i);
     133      return 0;
    156134    }
    157135
    158136  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
    159137    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
    160     return NULL;
     138    return 0;
    161139  } else {
    162140    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
    163     return NULL;
     141    return 0;
    164142  }
    165143
    166   return mat;
     144  new_height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0);
     145  if (mat->height != new_height) {
     146    if (mat->data) {
     147      free(mat->data);
     148    }
     149    mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * new_height);
     150  }
    167151
    168 fail:
    169   return NULL;
     152  mat->height = new_height;
     153  mat->length = (uint_t)PyArray_DIM ((PyArrayObject *)input, 1);
     154  for (i=0; i< mat->height; i++) {
     155    mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)input, i);
     156  }
     157  return 1;
    170158}
    171 
Note: See TracChangeset for help on using the changeset viewer.