source: python/ext/py-fft.c @ 8bffcff

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

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

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#include "aubio-types.h"
2
3static char Py_fft_doc[] = "fft object";
4
5typedef struct
6{
7  PyObject_HEAD
8  aubio_fft_t * o;
9  uint_t win_s;
10  // do / rdo input vectors
11  fvec_t vecin;
12  cvec_t cvecin;
13  // do / rdo output results
14  cvec_t *out;
15  fvec_t *rout;
16  // bridge for cvec output
17  Py_cvec *py_out;
18} Py_fft;
19
20static PyObject *
21Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
22{
23  int win_s = 0;
24  Py_fft *self;
25  static char *kwlist[] = { "win_s", NULL };
26
27  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
28          &win_s)) {
29    return NULL;
30  }
31
32  self = (Py_fft *) type->tp_alloc (type, 0);
33
34  if (self == NULL) {
35    return NULL;
36  }
37
38  self->win_s = Py_default_vector_length;
39
40  if (win_s > 0) {
41    self->win_s = win_s;
42  } else if (win_s < 0) {
43    PyErr_SetString (PyExc_ValueError,
44        "can not use negative window size");
45    return NULL;
46  }
47
48  return (PyObject *) self;
49}
50
51static int
52Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
53{
54  self->o = new_aubio_fft (self->win_s);
55  if (self->o == NULL) {
56    char_t errstr[30];
57    sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
58    PyErr_SetString (PyExc_Exception, errstr);
59    return -1;
60  }
61
62  self->out = new_cvec(self->win_s);
63  self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
64  Py_XINCREF(self->py_out);
65  self->rout = new_fvec(self->win_s);
66
67  return 0;
68}
69
70static void
71Py_fft_del (Py_fft *self, PyObject *unused)
72{
73  Py_XDECREF((PyObject*)(self->py_out));
74  del_aubio_fft(self->o);
75  del_cvec(self->out);
76  del_fvec(self->rout);
77  Py_TYPE(self)->tp_free((PyObject *) self);
78}
79
80static PyObject *
81Py_fft_do(Py_fft * self, PyObject * args)
82{
83  PyObject *input;
84
85  if (!PyArg_ParseTuple (args, "O", &input)) {
86    return NULL;
87  }
88
89  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
90    return NULL;
91  }
92
93  // compute the function
94  aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out);
95#if 0
96  Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
97  PyObject* output = PyAubio_CCvecToPyCvec(self->out, py_out);
98  return output;
99#else
100  // convert cvec to py_cvec, incrementing refcount to keep a copy
101  return PyAubio_CCvecToPyCvec(self->out, self->py_out);
102#endif
103}
104
105static PyMemberDef Py_fft_members[] = {
106  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
107    "size of the window"},
108  {NULL}
109};
110
111static PyObject *
112Py_fft_rdo(Py_fft * self, PyObject * args)
113{
114  PyObject *input;
115
116  if (!PyArg_ParseTuple (args, "O", &input)) {
117    return NULL;
118  }
119
120  if (!PyAubio_ArrayToCCvec (input, &(self->cvecin)) ) {
121    return NULL;
122  }
123
124  // compute the function
125  aubio_fft_rdo (self->o, &(self->cvecin), self->rout);
126  return PyAubio_CFvecToArray(self->rout);
127}
128
129static PyMethodDef Py_fft_methods[] = {
130  {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
131    "synthesis of spectral grain"},
132  {NULL}
133};
134
135PyTypeObject Py_fftType = {
136  PyVarObject_HEAD_INIT (NULL, 0)
137  "aubio.fft",
138  sizeof (Py_fft),
139  0,
140  (destructor) Py_fft_del,
141  0,
142  0,
143  0,
144  0,
145  0,
146  0,
147  0,
148  0,
149  0,
150  (ternaryfunc)Py_fft_do,
151  0,
152  0,
153  0,
154  0,
155  Py_TPFLAGS_DEFAULT,
156  Py_fft_doc,
157  0,
158  0,
159  0,
160  0,
161  0,
162  0,
163  Py_fft_methods,
164  Py_fft_members,
165  0,
166  0,
167  0,
168  0,
169  0,
170  0,
171  (initproc) Py_fft_init,
172  0,
173  Py_fft_new,
174};
Note: See TracBrowser for help on using the repository browser.