source: python/ext/py-filter.c @ ee092a8

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

python/ext: simplify memory allocations, removed unneeded malloc/free calls

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[f826349]1#include "aubio-types.h"
2
3typedef struct
4{
5  PyObject_HEAD
6  aubio_filter_t * o;
7  uint_t order;
[569b363]8  fvec_t vec;
[7c785e6]9  fvec_t *out;
[f826349]10} Py_filter;
11
12static char Py_filter_doc[] = "filter object";
13
14static PyObject *
15Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
16{
[96fe713]17  int order= 0;
[f826349]18  Py_filter *self;
[96fe713]19  static char *kwlist[] = { "order", NULL };
[f826349]20
[a52d3ae]21  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
[96fe713]22          &order)) {
[f826349]23    return NULL;
24  }
25
26  self = (Py_filter *) type->tp_alloc (type, 0);
27
28  if (self == NULL) {
29    return NULL;
30  }
31
[615ac7d]32  self->order = 7;
[f826349]33
34  if (order > 0) {
35    self->order = order;
36  } else if (order < 0) {
37    PyErr_SetString (PyExc_ValueError,
38        "can not use negative order");
39    return NULL;
40  }
41
42  return (PyObject *) self;
43}
44
45static int
46Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds)
47{
[96fe713]48  self->o = new_aubio_filter (self->order);
[f826349]49  if (self->o == NULL) {
50    return -1;
51  }
[7c785e6]52  self->out = new_fvec(Py_default_vector_length);
[f826349]53  return 0;
54}
55
56static void
57Py_filter_del (Py_filter * self)
58{
[7c785e6]59  del_fvec(self->out);
[f826349]60  del_aubio_filter (self->o);
[770b9e7]61  Py_TYPE(self)->tp_free ((PyObject *) self);
[f826349]62}
63
64static PyObject * 
[a52d3ae]65Py_filter_do(Py_filter * self, PyObject * args)
[f826349]66{
67  PyObject *input;
68
69  if (!PyArg_ParseTuple (args, "O:digital_filter.do", &input)) {
70    return NULL;
71  }
72
73  if (input == NULL) {
74    return NULL;
75  }
76
[569b363]77  if (!PyAubio_ArrayToCFvec(input, &(self->vec))) {
[f826349]78    return NULL;
79  }
80
[7c785e6]81  // reallocate the output if needed
[569b363]82  if (self->vec.length != self->out->length) {
[7c785e6]83    del_fvec(self->out);
[569b363]84    self->out = new_fvec(self->vec.length);
[7c785e6]85  }
[f826349]86  // compute the function
[569b363]87  aubio_filter_do_outplace (self->o, &(self->vec), self->out);
[7c785e6]88  return PyAubio_CFvecToArray(self->out);
[f826349]89}
90
91static PyObject * 
[615ac7d]92Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
[f826349]93{
[615ac7d]94  uint_t err = 0;
95  uint_t samplerate;
96  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
97    return NULL;
98  }
99
100  err = aubio_filter_set_c_weighting (self->o, samplerate);
[f826349]101  if (err > 0) {
102    PyErr_SetString (PyExc_ValueError,
[7d50c5a]103        "error when setting filter to C-weighting");
[f826349]104    return NULL;
105  }
[9e6695d]106  Py_RETURN_NONE;
[f826349]107}
108
109static PyObject * 
[615ac7d]110Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
[f826349]111{
[615ac7d]112  uint_t err = 0;
113  uint_t samplerate;
114  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
115    return NULL;
116  }
117
118  err = aubio_filter_set_a_weighting (self->o, samplerate);
[f826349]119  if (err > 0) {
120    PyErr_SetString (PyExc_ValueError,
121        "error when setting filter to A-weighting");
122    return NULL;
123  }
[9e6695d]124  Py_RETURN_NONE;
[f826349]125}
126
[a81c8cd]127static PyObject *
128Py_filter_set_biquad(Py_filter * self, PyObject *args)
129{
130  uint_t err = 0;
131  lsmp_t b0, b1, b2, a1, a2;
132  if (!PyArg_ParseTuple (args, "ddddd", &b0, &b1, &b2, &a1, &a2)) {
133    return NULL;
134  }
135
136  err = aubio_filter_set_biquad (self->o, b0, b1, b2, a1, a2);
137  if (err > 0) {
138    PyErr_SetString (PyExc_ValueError,
139        "error when setting filter with biquad coefficients");
140    return NULL;
141  }
[9e6695d]142  Py_RETURN_NONE;
[a81c8cd]143}
144
[f826349]145static PyMemberDef Py_filter_members[] = {
146  // TODO remove READONLY flag and define getter/setter
147  {"order", T_INT, offsetof (Py_filter, order), READONLY,
148      "order of the filter"},
149  {NULL}                        /* Sentinel */
150};
151
152static PyMethodDef Py_filter_methods[] = {
[07bf04e6]153  {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_VARARGS,
[f826349]154      "set filter coefficients to C-weighting"},
[07bf04e6]155  {"set_a_weighting", (PyCFunction) Py_filter_set_a_weighting, METH_VARARGS,
[f826349]156      "set filter coefficients to A-weighting"},
[a81c8cd]157  {"set_biquad", (PyCFunction) Py_filter_set_biquad, METH_VARARGS,
158      "set b0, b1, b2, a1, a2 biquad coefficients"},
[f826349]159  {NULL}
160};
161
162PyTypeObject Py_filterType = {
[5c1200a]163  PyVarObject_HEAD_INIT(NULL, 0)
[f826349]164  "aubio.digital_filter",       /* tp_name           */
165  sizeof (Py_filter),           /* tp_basicsize      */
166  0,                            /* tp_itemsize       */
167  (destructor) Py_filter_del,   /* tp_dealloc        */
168  0,                            /* tp_print          */
169  0,                            /* tp_getattr        */
170  0,                            /* tp_setattr        */
171  0,                            /* tp_compare        */
172  0, //(reprfunc) Py_filter_repr,    /* tp_repr           */
173  0,                            /* tp_as_number      */
174  0,                            /* tp_as_sequence    */
175  0,                            /* tp_as_mapping     */
176  0,                            /* tp_hash           */
177  (ternaryfunc)Py_filter_do,    /* tp_call           */
178  0,                            /* tp_str            */
179  0,                            /* tp_getattro       */
180  0,                            /* tp_setattro       */
181  0,                            /* tp_as_buffer      */
182  Py_TPFLAGS_DEFAULT,           /* tp_flags          */
183  Py_filter_doc,                /* tp_doc            */
184  0,                            /* tp_traverse       */
185  0,                            /* tp_clear          */
186  0,                            /* tp_richcompare    */
187  0,                            /* tp_weaklistoffset */
188  0,                            /* tp_iter           */
189  0,                            /* tp_iternext       */
190  Py_filter_methods,            /* tp_methods        */
191  Py_filter_members,            /* tp_members        */
192  0,                            /* tp_getset         */
193  0,                            /* tp_base           */
194  0,                            /* tp_dict           */
195  0,                            /* tp_descr_get      */
196  0,                            /* tp_descr_set      */
197  0,                            /* tp_dictoffset     */
198  (initproc) Py_filter_init,    /* tp_init           */
199  0,                            /* tp_alloc          */
200  Py_filter_new,                /* tp_new            */
201};
Note: See TracBrowser for help on using the repository browser.