source: python/ext/aubioproxy.c @ fcef3fd

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since fcef3fd was ede5d38, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/ext/aubio-types.h: add new_py_ functions to create PyObjects? instead of fvec_t, apply to py-fft.c

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[7395ec5]1#include "aubio-types.h"
2
[bfe8256]3PyObject *
[ede5d38]4new_py_fvec(uint_t length) {
5    npy_intp dims[] = { length, 1 };
6    return PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
7}
8
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);
13}
14
15PyObject *
[bfe8256]16PyAubio_CFvecToArray (fvec_t * self)
17{
18  npy_intp dims[] = { self->length, 1 };
19  return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data);
20}
21
22int
23PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
[7395ec5]24  if (input == NULL) {
25    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
[bfe8256]26    return 0;
[7395ec5]27  }
28  // parsing input object into a Py_fvec
29  if (PyArray_Check(input)) {
30
[93004d5]31    // we got an array, convert it to an fvec
[1458de5]32    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
[7395ec5]33      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
[bfe8256]34      return 0;
[1458de5]35    } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) {
[7395ec5]36      PyErr_SetString (PyExc_ValueError,
37          "input array has more than one dimensions");
[bfe8256]38      return 0;
[7395ec5]39    }
40
[1458de5]41    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
[7395ec5]42      PyErr_SetString (PyExc_ValueError, "input array should be float");
[bfe8256]43      return 0;
[1458de5]44    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
[c6388f4]45      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
[bfe8256]46      return 0;
[7395ec5]47    }
48
49    // vec = new_fvec (vec->length);
[93004d5]50    // no need to really allocate fvec, just its struct member
[bfe8256]51    long length = PyArray_SIZE ((PyArrayObject *)input);
52    if (length <= 0) {
[3194dca1]53      PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
[bfe8256]54      return 0;
[3194dca1]55    }
[7395ec5]56
57  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
58    PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
[bfe8256]59    return 0;
[7395ec5]60  } else {
61    PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
[bfe8256]62    return 0;
[7395ec5]63  }
64
[bfe8256]65  out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
66  out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
67  return 1;
[7395ec5]68}
69
70PyObject *
71PyAubio_CFmatToArray (fmat_t * input)
72{
73  PyObject *array = NULL;
74  uint_t i;
75  npy_intp dims[] = { input->length, 1 };
76  PyObject *concat = PyList_New (0), *tmp = NULL;
77  for (i = 0; i < input->height; i++) {
78    tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]);
79    PyList_Append (concat, tmp);
80    Py_DECREF (tmp);
81  }
82  array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
83  Py_DECREF (concat);
84  return array;
85}
86
[bfe8256]87int
88PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) {
[93004d5]89  uint_t i;
90  if (input == NULL) {
91    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
[bfe8256]92    return 0;
[93004d5]93  }
94  // parsing input object into a Py_fvec
95  if (PyArray_Check(input)) {
96
97    // we got an array, convert it to an fvec
98    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
99      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
[bfe8256]100      return 0;
[93004d5]101    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
102      PyErr_SetString (PyExc_ValueError,
103          "input array has more than two dimensions");
[bfe8256]104      return 0;
[93004d5]105    }
106
107    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
108      PyErr_SetString (PyExc_ValueError, "input array should be float");
[bfe8256]109      return 0;
[93004d5]110    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
[c6388f4]111      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
[bfe8256]112      return 0;
[93004d5]113    }
114
115    // no need to really allocate fvec, just its struct member
[bfe8256]116    long length = PyArray_DIM ((PyArrayObject *)input, 1);
117    if (length <= 0) {
[3194dca1]118      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
[bfe8256]119      return 0;
[3194dca1]120    }
[bfe8256]121    long height = PyArray_DIM ((PyArrayObject *)input, 0);
122    if (height <= 0) {
[3194dca1]123      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
[bfe8256]124      return 0;
[93004d5]125    }
126
127  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
128    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
[bfe8256]129    return 0;
[93004d5]130  } else {
131    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
[bfe8256]132    return 0;
[93004d5]133  }
134
[569b363]135  uint_t new_height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0);
136  if (mat->height != new_height) {
137    if (mat->data) {
138      free(mat->data);
139    }
140    mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * new_height);
[bfe8256]141  }
[93004d5]142
[569b363]143  mat->height = new_height;
[bfe8256]144  mat->length = (uint_t)PyArray_DIM ((PyArrayObject *)input, 1);
145  for (i=0; i< mat->height; i++) {
146    mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)input, i);
147  }
148  return 1;
[7395ec5]149}
Note: See TracBrowser for help on using the repository browser.