source: interfaces/python/aubiomodule.c @ e6c11e3

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

interfaces/python: improve draft wrapper alpha_norm example, add some unit tests

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