source: python/ext/py-filterbank.c @ f8340e1

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

python/: use Py_RETURN_NONE, fixing a memory bug triggered after opening many sinks

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[615ac7d]1#include "aubiowraphell.h"
2
3static char Py_filterbank_doc[] = "filterbank object";
4
5AUBIO_DECLARE(filterbank, uint_t n_filters; uint_t win_s)
6
7//AUBIO_NEW(filterbank)
8static PyObject *
9Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
10{
11  int win_s = 0, n_filters = 0;
12  Py_filterbank *self;
13  static char *kwlist[] = { "n_filters", "win_s", NULL };
14
15  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
16          &n_filters, &win_s)) {
17    return NULL;
18  }
19
20  self = (Py_filterbank *) type->tp_alloc (type, 0);
21
22  if (self == NULL) {
23    return NULL;
24  }
25
26  self->win_s = Py_default_vector_length;
27  if (win_s > 0) {
28    self->win_s = win_s;
29  } else if (win_s < 0) {
30    PyErr_SetString (PyExc_ValueError,
31        "can not use negative window size");
32    return NULL;
33  }
34
35  self->n_filters = 40;
36  if (n_filters > 0) {
37    self->n_filters = n_filters;
38  } else if (n_filters < 0) {
39    PyErr_SetString (PyExc_ValueError,
40        "can not use negative number of filters");
41    return NULL;
42  }
43
44  return (PyObject *) self;
45}
46
47
48AUBIO_INIT(filterbank, self->n_filters, self->win_s)
49
50AUBIO_DEL(filterbank)
51
[f6cbefe]52static PyObject *
[615ac7d]53Py_filterbank_do(Py_filterbank * self, PyObject * args)
54{
55  PyObject *input;
[3e882a8]56  cvec_t *vec;
[363ce7a]57  fvec_t *out;
[615ac7d]58
59  if (!PyArg_ParseTuple (args, "O", &input)) {
60    return NULL;
61  }
62
[3e882a8]63  vec = PyAubio_ArrayToCCvec (input);
[615ac7d]64
65  if (vec == NULL) {
66    return NULL;
67  }
68
[363ce7a]69  out = new_fvec (self->n_filters);
[615ac7d]70
71  // compute the function
[3e882a8]72  aubio_filterbank_do (self->o, vec, out);
[363ce7a]73  return (PyObject *)PyAubio_CFvecToArray(out);
[615ac7d]74}
75
[f6cbefe]76AUBIO_MEMBERS_START(filterbank)
[615ac7d]77  {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY,
78    "size of the window"},
79  {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY,
80    "number of filters"},
81AUBIO_MEMBERS_STOP(filterbank)
82
[f6cbefe]83static PyObject *
[615ac7d]84Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
85{
86  uint_t err = 0;
87
88  PyObject *input;
89  uint_t samplerate;
[363ce7a]90  fvec_t *freqs;
[615ac7d]91  if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
92    return NULL;
93  }
94
95  if (input == NULL) {
96    return NULL;
97  }
98
[363ce7a]99  freqs = PyAubio_ArrayToCFvec (input);
[615ac7d]100
101  if (freqs == NULL) {
102    return NULL;
103  }
104
105  err = aubio_filterbank_set_triangle_bands (self->o,
[363ce7a]106      freqs, samplerate);
[615ac7d]107  if (err > 0) {
108    PyErr_SetString (PyExc_ValueError,
109        "error when setting filter to A-weighting");
110    return NULL;
111  }
[9e6695d]112  Py_RETURN_NONE;
[615ac7d]113}
114
[f6cbefe]115static PyObject *
[615ac7d]116Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args)
117{
118  uint_t err = 0;
119
120  uint_t samplerate;
121  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
122    return NULL;
123  }
124
125  err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
126  if (err > 0) {
127    PyErr_SetString (PyExc_ValueError,
128        "error when setting filter to A-weighting");
129    return NULL;
130  }
[9e6695d]131  Py_RETURN_NONE;
[615ac7d]132}
133
[f6cbefe]134static PyObject *
135Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args)
136{
137  uint_t err = 0;
138
139  PyObject *input;
140  fmat_t *coeffs;
141
142  if (!PyArg_ParseTuple (args, "O", &input)) {
143    return NULL;
144  }
145
146  coeffs = PyAubio_ArrayToCFmat (input);
147
148  if (coeffs == NULL) {
149    PyErr_SetString (PyExc_ValueError,
150        "unable to parse input array");
151    return NULL;
152  }
153
154  err = aubio_filterbank_set_coeffs (self->o, coeffs);
155
156  if (err > 0) {
157    PyErr_SetString (PyExc_ValueError,
158        "error when setting filter coefficients");
159    return NULL;
160  }
[9e6695d]161  Py_RETURN_NONE;
[f6cbefe]162}
163
164static PyObject *
[615ac7d]165Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
166{
[363ce7a]167  return (PyObject *)PyAubio_CFmatToArray(
168      aubio_filterbank_get_coeffs (self->o) );
[615ac7d]169}
170
171static PyMethodDef Py_filterbank_methods[] = {
172  {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands,
173    METH_VARARGS, "set coefficients of filterbanks"},
174  {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
175    METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"},
176  {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
177    METH_NOARGS, "get coefficients of filterbank"},
[f6cbefe]178  {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
179    METH_VARARGS, "set coefficients of filterbank"},
[615ac7d]180  {NULL}
181};
182
183AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank")
Note: See TracBrowser for help on using the repository browser.