source: python/ext/aubioproxy.c @ 074b41b

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

python/: move source files to ext/

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