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

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

interfaces/python: towards mono

  • Property mode set to 100644
File size: 3.8 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#include "generated/aubio-generated.h"
7
8static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
9
10static PyObject *
11Py_alpha_norm (PyObject * self, PyObject * args)
12{
13  PyObject *input;
14  Py_fvec *vec;
15  smpl_t alpha;
16  PyObject *result;
17
18  if (!PyArg_ParseTuple (args, "Of:alpha_norm", &input, &alpha)) {
19    return NULL;
20  }
21
22  if (input == NULL) {
23    return NULL;
24  }
25
26  vec = PyAubio_ArrayToFvec (input);
27
28  if (vec == NULL) {
29    return NULL;
30  }
31
32  // compute the function
33  result = Py_BuildValue ("f", fvec_alpha_norm (vec->o, alpha));
34  if (result == NULL) {
35    return NULL;
36  }
37
38  return result;
39}
40
41static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
42
43static PyObject *
44Py_zero_crossing_rate (PyObject * self, PyObject * args)
45{
46  PyObject *input;
47  Py_fvec *vec;
48  PyObject *result;
49
50  if (!PyArg_ParseTuple (args, "O:zero_crossing_rate", &input)) {
51    return NULL;
52  }
53
54  if (input == NULL) {
55    return NULL;
56  }
57
58  vec = PyAubio_ArrayToFvec (input);
59
60  if (vec == NULL) {
61    return NULL;
62  }
63
64  // compute the function
65  result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec->o));
66  if (result == NULL) {
67    return NULL;
68  }
69
70  return result;
71}
72
73static char Py_min_removal_doc[] = "compute zero crossing rate";
74
75static PyObject * 
76Py_min_removal(PyObject * self, PyObject * args)
77{
78  PyObject *input;
79  Py_fvec *vec;
80
81  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
82    return NULL;
83  }
84
85  if (input == NULL) {
86    return NULL;
87  }
88
89  vec = PyAubio_ArrayToFvec (input);
90
91  if (vec == NULL) {
92    return NULL;
93  }
94
95  // compute the function
96  fvec_min_removal (vec->o);
97
98  // since this function does not return, we could return None
99  //return Py_None;
100  // however it is convenient to return the modified vector
101  return (PyObject *) PyAubio_FvecToArray(vec);
102  // or even without converting it back to an array
103  //Py_INCREF(vec);
104  //return (PyObject *)vec;
105}
106
107static PyMethodDef aubio_methods[] = {
108  {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
109  {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, 
110    Py_zero_crossing_rate_doc},
111  {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
112  {NULL, NULL} /* Sentinel */
113};
114
115static char aubio_module_doc[] = "Python module for the aubio library";
116
117PyMODINIT_FUNC
118init_aubio (void)
119{
120  PyObject *m;
121  int err;
122
123  if ((PyType_Ready (&Py_fvecType) < 0) 
124      || (PyType_Ready (&Py_fmatType) < 0) 
125      || (PyType_Ready (&Py_cvecType) < 0) 
126      || (PyType_Ready (&Py_filterType) < 0) 
127      || (PyType_Ready (&Py_filterbankType) < 0) 
128      || (PyType_Ready (&Py_fftType) < 0) 
129      || (PyType_Ready (&Py_pvocType) < 0) 
130      // generated objects
131      || (generated_types_ready() < 0 )
132  ) {
133    return;
134  }
135
136  err = _import_array ();
137
138  if (err != 0) {
139    fprintf (stderr,
140        "Unable to import Numpy C API from aubio module (error %d)\n", err);
141  }
142
143  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
144
145  if (m == NULL) {
146    return;
147  }
148
149  Py_INCREF (&Py_fvecType);
150  PyModule_AddObject (m, "fvec", (PyObject *) & Py_fvecType);
151  Py_INCREF (&Py_fmatType);
152  PyModule_AddObject (m, "fmat", (PyObject *) & Py_fmatType);
153  Py_INCREF (&Py_cvecType);
154  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
155  Py_INCREF (&Py_filterType);
156  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
157  Py_INCREF (&Py_filterbankType);
158  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
159  Py_INCREF (&Py_fftType);
160  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
161  Py_INCREF (&Py_pvocType);
162  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
163
164  // generated objects
165  add_generated_objects(m);
166}
Note: See TracBrowser for help on using the repository browser.