File: py-filter.c

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (267 lines) | stat: -rw-r--r-- 7,137 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "aubio-types.h"

typedef struct
{
  PyObject_HEAD
  aubio_filter_t * o;
  uint_t order;
  fvec_t vec;
  PyObject *out;
  fvec_t c_out;
} Py_filter;

static char Py_filter_doc[] = ""
"digital_filter(order=7)\n"
"\n"
"Create a digital filter.\n"
"";

static char Py_filter_set_c_weighting_doc[] = ""
"set_c_weighting(samplerate)\n"
"\n"
"Set filter coefficients to C-weighting.\n"
"\n"
"`samplerate` should be one of 8000, 11025, 16000, 22050, 24000, 32000,\n"
"44100, 48000, 88200, 96000, or 192000. `order` of the filter should be 5.\n"
"\n"
"Parameters\n"
"----------\n"
"samplerate : int\n"
"    Sampling-rate of the input signal, in Hz.\n"
"";

static char Py_filter_set_a_weighting_doc[] = ""
"set_a_weighting(samplerate)\n"
"\n"
"Set filter coefficients to A-weighting.\n"
"\n"
"`samplerate` should be one of 8000, 11025, 16000, 22050, 24000, 32000,\n"
"44100, 48000, 88200, 96000, or 192000. `order` of the filter should be 7.\n"
"\n"
"Parameters\n"
"----------\n"
"samplerate : int\n"
"    Sampling-rate of the input signal.\n"
"";

static char Py_filter_set_biquad_doc[] = ""
"set_biquad(b0, b1, b2, a1, a2)\n"
"\n"
"Set biquad coefficients. `order` of the filter should be 3.\n"
"\n"
"Parameters\n"
"----------\n"
"b0 : float\n"
"    Forward filter coefficient.\n"
"b1 : float\n"
"    Forward filter coefficient.\n"
"b2 : float\n"
"    Forward filter coefficient.\n"
"a1 : float\n"
"    Feedback filter coefficient.\n"
"a2 : float\n"
"    Feedback filter coefficient.\n"
"";

static PyObject *
Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
{
  int order= 0;
  Py_filter *self;
  static char *kwlist[] = { "order", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
          &order)) {
    return NULL;
  }

  self = (Py_filter *) type->tp_alloc (type, 0);

  if (self == NULL) {
    return NULL;
  }

  self->order = 7;

  if (order > 0) {
    self->order = order;
  } else if (order < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative order");
    return NULL;
  }

  return (PyObject *) self;
}

static int
Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds)
{
  self->o = new_aubio_filter (self->order);
  if (self->o == NULL) {
    return -1;
  }
  self->out = NULL;
  return 0;
}

static void
Py_filter_del (Py_filter * self)
{
  Py_XDECREF(self->out);
  if (self->o)
    del_aubio_filter (self->o);
  Py_TYPE(self)->tp_free ((PyObject *) self);
}

static PyObject *
Py_filter_do(Py_filter * self, PyObject * args)
{
  PyObject *input;

  if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) {
    return NULL;
  }

  if (input == NULL) {
    return NULL;
  }

  if (!PyAubio_ArrayToCFvec(input, &(self->vec))) {
    return NULL;
  }

  // initialize output now
  if (self->out == NULL) {
    self->out = new_py_fvec(self->vec.length);
  }

  Py_INCREF(self->out);
  if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) {
    return NULL;
  }
  // compute the function
  aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out));
  return self->out;
}

static PyObject *
Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
{
  uint_t err = 0;
  uint_t samplerate;
  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
    return NULL;
  }

  err = aubio_filter_set_c_weighting (self->o, samplerate);
  if (err > 0) {
    PyErr_SetString (PyExc_ValueError,
        "error when setting filter to C-weighting");
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
{
  uint_t err = 0;
  uint_t samplerate;
  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
    return NULL;
  }

  err = aubio_filter_set_a_weighting (self->o, samplerate);
  if (err > 0) {
    PyErr_SetString (PyExc_ValueError,
        "error when setting filter to A-weighting");
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filter_set_biquad(Py_filter * self, PyObject *args)
{
  uint_t err = 0;
  lsmp_t b0, b1, b2, a1, a2;
  if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) {
    return NULL;
  }

  err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2);
  if (err > 0) {
    PyErr_SetString (PyExc_ValueError,
        "error when setting filter with biquad coefficients");
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyMemberDef Py_filter_members[] = {
  // TODO remove READONLY flag and define getter/setter
  {"order", T_INT, offsetof (Py_filter, order), READONLY,
      "order of the filter"},
  {NULL}                        /* Sentinel */
};

static PyMethodDef Py_filter_methods[] = {
  {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS,
      Py_filter_set_c_weighting_doc},
  {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS,
      Py_filter_set_a_weighting_doc},
  {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS,
      Py_filter_set_biquad_doc},
  {NULL}
};

PyTypeObject Py_filterType = {
  PyVarObject_HEAD_INIT(NULL, 0)
  "aubio.digital_filter",       /* tp_name           */
  sizeof (Py_filter),           /* tp_basicsize      */
  0,                            /* tp_itemsize       */
  (destructor) Py_filter_del,   /* tp_dealloc        */
  0,                            /* tp_print          */
  0,                            /* tp_getattr        */
  0,                            /* tp_setattr        */
  0,                            /* tp_compare        */
  0, //(reprfunc) Py_filter_repr,    /* tp_repr           */
  0,                            /* tp_as_number      */
  0,                            /* tp_as_sequence    */
  0,                            /* tp_as_mapping     */
  0,                            /* tp_hash           */
  (ternaryfunc)Py_filter_do,    /* tp_call           */
  0,                            /* tp_str            */
  0,                            /* tp_getattro       */
  0,                            /* tp_setattro       */
  0,                            /* tp_as_buffer      */
  Py_TPFLAGS_DEFAULT,           /* tp_flags          */
  Py_filter_doc,                /* tp_doc            */
  0,                            /* tp_traverse       */
  0,                            /* tp_clear          */
  0,                            /* tp_richcompare    */
  0,                            /* tp_weaklistoffset */
  0,                            /* tp_iter           */
  0,                            /* tp_iternext       */
  Py_filter_methods,            /* tp_methods        */
  Py_filter_members,            /* tp_members        */
  0,                            /* tp_getset         */
  0,                            /* tp_base           */
  0,                            /* tp_dict           */
  0,                            /* tp_descr_get      */
  0,                            /* tp_descr_set      */
  0,                            /* tp_dictoffset     */
  (initproc) Py_filter_init,    /* tp_init           */
  0,                            /* tp_alloc          */
  Py_filter_new,                /* tp_new            */
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
};