source: python/ext/py-filterbank.c @ 92a8800

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

python/ext/py-cvec.c: rewrite and simplify aubio.cvec, safer and better memory usage (see #49)

  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include "aubio-types.h"
2
3static char Py_filterbank_doc[] = "filterbank object";
4
5typedef struct
6{
7  PyObject_HEAD
8  aubio_filterbank_t * o;
9  uint_t n_filters;
10  uint_t win_s;
11  cvec_t vec;
12  fvec_t freqs;
13  fmat_t coeffs;
14  fvec_t *out;
15} Py_filterbank;
16
17static PyObject *
18Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
19{
20  int win_s = 0, n_filters = 0;
21  Py_filterbank *self;
22  static char *kwlist[] = { "n_filters", "win_s", NULL };
23
24  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
25          &n_filters, &win_s)) {
26    return NULL;
27  }
28
29  self = (Py_filterbank *) type->tp_alloc (type, 0);
30
31  if (self == NULL) {
32    return NULL;
33  }
34
35  self->win_s = Py_default_vector_length;
36  if (win_s > 0) {
37    self->win_s = win_s;
38  } else if (win_s < 0) {
39    PyErr_SetString (PyExc_ValueError,
40        "can not use negative window size");
41    return NULL;
42  }
43
44  self->n_filters = 40;
45  if (n_filters > 0) {
46    self->n_filters = n_filters;
47  } else if (n_filters < 0) {
48    PyErr_SetString (PyExc_ValueError,
49        "can not use negative number of filters");
50    return NULL;
51  }
52
53  return (PyObject *) self;
54}
55
56static int
57Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds)
58{
59  self->o = new_aubio_filterbank (self->n_filters, self->win_s);
60  if (self->o == NULL) {
61    char_t errstr[30];
62    sprintf(errstr, "error creating filterbank with n_filters=%d, win_s=%d",
63        self->n_filters, self->win_s);
64    PyErr_SetString (PyExc_RuntimeError, errstr);
65    return -1;
66  }
67  self->out = new_fvec(self->n_filters);
68
69  return 0;
70}
71
72static void
73Py_filterbank_del (Py_filterbank *self, PyObject *unused)
74{
75  del_aubio_filterbank(self->o);
76  del_fvec(self->out);
77  free(self->coeffs.data);
78  Py_TYPE(self)->tp_free((PyObject *) self);
79}
80
81static PyObject *
82Py_filterbank_do(Py_filterbank * self, PyObject * args)
83{
84  PyObject *input;
85
86  if (!PyArg_ParseTuple (args, "O", &input)) {
87    return NULL;
88  }
89
90  if (!PyAubio_PyCvecToCCvec(input, &(self->vec) )) {
91    return NULL;
92  }
93
94  // compute the function
95  aubio_filterbank_do (self->o, &(self->vec), self->out);
96  return (PyObject *)PyAubio_CFvecToArray(self->out);
97}
98
99static PyMemberDef Py_filterbank_members[] = {
100  {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY,
101    "size of the window"},
102  {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY,
103    "number of filters"},
104  {NULL} /* sentinel */
105};
106
107static PyObject *
108Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
109{
110  uint_t err = 0;
111
112  PyObject *input;
113  uint_t samplerate;
114  if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
115    return NULL;
116  }
117
118  if (input == NULL) {
119    return NULL;
120  }
121
122  if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) {
123    return NULL;
124  }
125
126  err = aubio_filterbank_set_triangle_bands (self->o,
127      &(self->freqs), samplerate);
128  if (err > 0) {
129    PyErr_SetString (PyExc_ValueError,
130        "error when setting filter to A-weighting");
131    return NULL;
132  }
133  Py_RETURN_NONE;
134}
135
136static PyObject *
137Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args)
138{
139  uint_t err = 0;
140
141  uint_t samplerate;
142  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
143    return NULL;
144  }
145
146  err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
147  if (err > 0) {
148    PyErr_SetString (PyExc_ValueError,
149        "error when setting filter to A-weighting");
150    return NULL;
151  }
152  Py_RETURN_NONE;
153}
154
155static PyObject *
156Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args)
157{
158  uint_t err = 0;
159
160  PyObject *input;
161  if (!PyArg_ParseTuple (args, "O", &input)) {
162    return NULL;
163  }
164
165  if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) {
166    return NULL;
167  }
168
169  err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs));
170
171  if (err > 0) {
172    PyErr_SetString (PyExc_ValueError,
173        "error when setting filter coefficients");
174    return NULL;
175  }
176  Py_RETURN_NONE;
177}
178
179static PyObject *
180Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
181{
182  return (PyObject *)PyAubio_CFmatToArray(
183      aubio_filterbank_get_coeffs (self->o) );
184}
185
186static PyMethodDef Py_filterbank_methods[] = {
187  {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands,
188    METH_VARARGS, "set coefficients of filterbanks"},
189  {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
190    METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"},
191  {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
192    METH_NOARGS, "get coefficients of filterbank"},
193  {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
194    METH_VARARGS, "set coefficients of filterbank"},
195  {NULL}
196};
197
198PyTypeObject Py_filterbankType = {
199  PyVarObject_HEAD_INIT (NULL, 0)
200  "aubio.filterbank",
201  sizeof (Py_filterbank),
202  0,
203  (destructor) Py_filterbank_del,
204  0,
205  0,
206  0,
207  0,
208  0,
209  0,
210  0,
211  0,
212  0,
213  (ternaryfunc)Py_filterbank_do,
214  0,
215  0,
216  0,
217  0,
218  Py_TPFLAGS_DEFAULT,
219  Py_filterbank_doc,
220  0,
221  0,
222  0,
223  0,
224  0,
225  0,
226  Py_filterbank_methods,
227  Py_filterbank_members,
228  0,
229  0,
230  0,
231  0,
232  0,
233  0,
234  (initproc) Py_filterbank_init,
235  0,
236  Py_filterbank_new,
237};
Note: See TracBrowser for help on using the repository browser.