source: python/ext/aubioproxy.c @ 307fdfc

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 307fdfc 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
Line 
1#include "aubio-types.h"
2
3PyObject *
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 *
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) {
24  if (input == NULL) {
25    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
26    return 0;
27  }
28  // parsing input object into a Py_fvec
29  if (PyArray_Check(input)) {
30
31    // we got an array, convert it to an fvec
32    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
33      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
34      return 0;
35    } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) {
36      PyErr_SetString (PyExc_ValueError,
37          "input array has more than one dimensions");
38      return 0;
39    }
40
41    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
42      PyErr_SetString (PyExc_ValueError, "input array should be float");
43      return 0;
44    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
45      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
46      return 0;
47    }
48
49    // vec = new_fvec (vec->length);
50    // no need to really allocate fvec, just its struct member
51    long length = PyArray_SIZE ((PyArrayObject *)input);
52    if (length <= 0) {
53      PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
54      return 0;
55    }
56
57  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
58    PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
59    return 0;
60  } else {
61    PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
62    return 0;
63  }
64
65  out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
66  out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
67  return 1;
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
87int
88PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) {
89  uint_t i;
90  if (input == NULL) {
91    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
92    return 0;
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");
100      return 0;
101    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
102      PyErr_SetString (PyExc_ValueError,
103          "input array has more than two dimensions");
104      return 0;
105    }
106
107    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
108      PyErr_SetString (PyExc_ValueError, "input array should be float");
109      return 0;
110    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
111      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
112      return 0;
113    }
114
115    // no need to really allocate fvec, just its struct member
116    long length = PyArray_DIM ((PyArrayObject *)input, 1);
117    if (length <= 0) {
118      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
119      return 0;
120    }
121    long height = PyArray_DIM ((PyArrayObject *)input, 0);
122    if (height <= 0) {
123      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
124      return 0;
125    }
126
127  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
128    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
129    return 0;
130  } else {
131    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
132    return 0;
133  }
134
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);
141  }
142
143  mat->height = new_height;
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;
149}
Note: See TracBrowser for help on using the repository browser.