source: interfaces/python/aubiomodule.c @ 6b1aafc

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

python/aubiomodule.c: PyAubio_ArrayToFvec to convert numpy array to a Py_fvec, no copy

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[0a6c211]1#include <Python.h>
[ae6e15c]2#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
[0a6c211]3#include <numpy/arrayobject.h>
4
[ae6e15c]5#include "aubio-types.h"
[0a6c211]6
[6b1aafc]7Py_fvec *
8PyAubio_ArrayToFvec (PyObject *input) {
[ae6e15c]9  PyObject *array;
[6b1aafc]10  Py_fvec *vec;
[ae6e15c]11  uint_t i;
12  // parsing input object into a Py_fvec
13  if (PyObject_TypeCheck (input, &Py_fvecType)) {
14    // input is an fvec, nothing else to do
15    vec = (Py_fvec *) input;
16  } else if (PyArray_Check(input)) {
[0a6c211]17
[ae6e15c]18    // we got an array, convert it to an fvec
19    if (PyArray_NDIM (input) == 0) {
20      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
21      goto fail;
22    } else if (PyArray_NDIM (input) > 2) {
[6b1aafc]23      PyErr_SetString (PyExc_ValueError,
24          "input array has more than two dimensions");
[ae6e15c]25      goto fail;
[0a6c211]26    }
27
[ae6e15c]28    if (!PyArray_ISFLOAT (input)) {
29      PyErr_SetString (PyExc_ValueError, "input array should be float");
30      goto fail;
[6b1aafc]31#if AUBIO_DO_CASTING
32    } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
[ae6e15c]33      // input data type is not float32, casting
[6b1aafc]34      array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT);
[ae6e15c]35      if (array == NULL) {
36        PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
37        goto fail;
38      }
[6b1aafc]39#else
40    } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
41      PyErr_SetString (PyExc_ValueError, "input array should be float32");
42      goto fail;
43#endif
[ae6e15c]44    } else {
45      // input data type is float32, nothing else to do
46      array = input;
47    }
[0a6c211]48
[ae6e15c]49    // create a new fvec object
50    vec = (Py_fvec*) PyObject_New (Py_fvec, &Py_fvecType); 
51    if (PyArray_NDIM (array) == 1) {
52      vec->channels = 1;
53      vec->length = PyArray_SIZE (array);
54    } else {
55      vec->channels = PyArray_DIM (array, 0);
56      vec->length = PyArray_DIM (array, 1);
57    }
[0a6c211]58
[6b1aafc]59    // no need to really allocate fvec, just its struct member
60    // vec->o = new_fvec (vec->length, vec->channels);
61    vec->o = (fvec_t *)malloc(sizeof(fvec_t));
62    vec->o->length = vec->length; vec->o->channels = vec->channels;
63    vec->o->data = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
64    // hat data[i] point to array line
[ae6e15c]65    for (i = 0; i < vec->channels; i++) {
66      vec->o->data[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
67    }
[0a6c211]68
[ae6e15c]69  } else {
70    PyErr_SetString (PyExc_ValueError, "can only accept array or fvec as input");
[0a6c211]71    return NULL;
72  }
73
[6b1aafc]74  return vec;
75
76fail:
77  return NULL;
78}
79
80
81
82static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
83
84static PyObject *
85Py_alpha_norm (PyObject * self, PyObject * args)
86{
87  PyObject *input;
88  Py_fvec *vec;
89  smpl_t alpha;
90  PyObject *result;
91
92  if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
93    return NULL;
94  }
95
96  if (input == NULL) {
97    return NULL;
98  }
99
100  vec = PyAubio_ArrayToFvec (input);
101
102  if (vec == NULL) {
103    return NULL;
104  }
105
[ae6e15c]106  // compute the function
[6b1aafc]107  result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha));
[0a6c211]108  if (result == NULL) {
109    return NULL;
110  }
111
112  return result;
113}
114
115static PyMethodDef aubio_methods[] = {
116  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
117  {NULL, NULL}                  /* Sentinel */
118};
119
[ae6e15c]120static char aubio_module_doc[] = "Python module for the aubio library";
121
[0a6c211]122PyMODINIT_FUNC
123init_aubio (void)
124{
125  PyObject *m;
126  int err;
127
128  if (PyType_Ready (&Py_fvecType) < 0) {
129    return;
130  }
131
132  err = _import_array ();
133
134  if (err != 0) {
135    fprintf (stderr,
136        "Unable to import Numpy C API from aubio module (error %d)\n", err);
137  }
138
139  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
140
141  if (m == NULL) {
142    return;
143  }
144
145  Py_INCREF (&Py_fvecType);
146  PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType);
147}
Note: See TracBrowser for help on using the repository browser.