source: python/ext/aubioproxy.c @ 58eb250

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 58eb250 was 911c22f, checked in by Paul Brossier <piem@piem.org>, 9 years ago

ext/aubioproxy.c: fix windows c89 compilation

  • 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_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;
64}
65
66int
67PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
68
69  if (!PyAubio_IsValidVector(input)){
70    return 0;
71  }
72
73  out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
74  out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
75  return 1;
76}
77
78PyObject *
79PyAubio_CFmatToArray (fmat_t * input)
80{
81  PyObject *array = NULL;
82  uint_t i;
83  npy_intp dims[] = { input->length, 1 };
84  PyObject *concat = PyList_New (0), *tmp = NULL;
85  for (i = 0; i < input->height; i++) {
86    tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]);
87    PyList_Append (concat, tmp);
88    Py_DECREF (tmp);
89  }
90  array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
91  Py_DECREF (concat);
92  return array;
93}
94
95int
96PyAubio_ArrayToCFmat (PyObject *input, fmat_t *mat) {
97  uint_t i, new_height;
98  npy_intp length, height;
99  if (input == NULL) {
100    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
101    return 0;
102  }
103  // parsing input object into a Py_fvec
104  if (PyArray_Check(input)) {
105
106    // we got an array, convert it to an fvec
107    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
108      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
109      return 0;
110    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
111      PyErr_SetString (PyExc_ValueError,
112          "input array has more than two dimensions");
113      return 0;
114    }
115
116    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
117      PyErr_SetString (PyExc_ValueError, "input array should be float");
118      return 0;
119    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
120      PyErr_SetString (PyExc_ValueError, "input array should be " AUBIO_NPY_SMPL_STR);
121      return 0;
122    }
123
124    // no need to really allocate fvec, just its struct member
125    length = PyArray_DIM ((PyArrayObject *)input, 1);
126    if (length <= 0) {
127      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
128      return 0;
129    }
130    height = PyArray_DIM ((PyArrayObject *)input, 0);
131    if (height <= 0) {
132      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
133      return 0;
134    }
135
136  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
137    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
138    return 0;
139  } else {
140    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
141    return 0;
142  }
143
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  }
151
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;
158}
Note: See TracBrowser for help on using the repository browser.