source: interfaces/python/py-filterbank.c @ 363ce7a

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

py-filterbank.c: use new proxy function, switch to mono and fmat

  • Property mode set to 100644
File size: 3.5 KB
Line 
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
52static PyObject * 
53Py_filterbank_do(Py_filterbank * self, PyObject * args)
54{
55  PyObject *input;
56  Py_cvec *vec;
57  fvec_t *out;
58
59  if (!PyArg_ParseTuple (args, "O", &input)) {
60    return NULL;
61  }
62
63  vec = PyAubio_ArrayToCvec (input);
64
65  if (vec == NULL) {
66    return NULL;
67  }
68
69  out = new_fvec (self->n_filters);
70
71  // compute the function
72  aubio_filterbank_do (self->o, vec->o, out);
73  return (PyObject *)PyAubio_CFvecToArray(out);
74}
75
76AUBIO_MEMBERS_START(filterbank) 
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
83static PyObject * 
84Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
85{
86  uint_t err = 0;
87
88  PyObject *input;
89  uint_t samplerate;
90  fvec_t *freqs;
91  if (!PyArg_ParseTuple (args, "OI", &input, &samplerate)) {
92    return NULL;
93  }
94
95  if (input == NULL) {
96    return NULL;
97  }
98
99  freqs = PyAubio_ArrayToCFvec (input);
100
101  if (freqs == NULL) {
102    return NULL;
103  }
104
105  err = aubio_filterbank_set_triangle_bands (self->o,
106      freqs, samplerate);
107  if (err > 0) {
108    PyErr_SetString (PyExc_ValueError,
109        "error when setting filter to A-weighting");
110    return NULL;
111  }
112  return Py_None;
113}
114
115static PyObject * 
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  }
131  return Py_None;
132}
133
134static PyObject * 
135Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
136{
137  return (PyObject *)PyAubio_CFmatToArray(
138      aubio_filterbank_get_coeffs (self->o) );
139}
140
141static PyMethodDef Py_filterbank_methods[] = {
142  {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands,
143    METH_VARARGS, "set coefficients of filterbanks"},
144  {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
145    METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"},
146  {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
147    METH_NOARGS, "get coefficients of filterbank"},
148  {NULL}
149};
150
151AUBIO_TYPEOBJECT(filterbank, "aubio.filterbank")
Note: See TracBrowser for help on using the repository browser.