source: interfaces/python/aubioproxy.c @ 0178c65

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

aubioproxy.c: added conversion functions from C to numpy

  • Property mode set to 100644
File size: 2.6 KB
Line 
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
14    // we got an array, convert it to an fvec
15    if (PyArray_NDIM (input) == 0) {
16      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
17      goto fail;
18    } else if (PyArray_NDIM (input) > 1) {
19      PyErr_SetString (PyExc_ValueError,
20          "input array has more than one dimensions");
21      goto fail;
22    }
23
24    if (!PyArray_ISFLOAT (input)) {
25      PyErr_SetString (PyExc_ValueError, "input array should be float");
26      goto fail;
27    } else if (PyArray_TYPE (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    vec->length = PyArray_SIZE (array);
39    vec->data = (smpl_t *) PyArray_GETPTR1 (array, 0);
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) {
64  Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType); 
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) {
100  return NULL;
101}
102
Note: See TracBrowser for help on using the repository browser.