source: python/ext/py-fft.c @ c7844d8

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

python/{ext/*.c,lib/gen_code.py}: complete PyTypeObject? definitions

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[5652a8c]1#include "aubio-types.h"
[615ac7d]2
3static char Py_fft_doc[] = "fft object";
4
[de81d2b]5typedef struct
6{
7  PyObject_HEAD
8  aubio_fft_t * o;
9  uint_t win_s;
[569b363]10  // do / rdo input vectors
11  fvec_t vecin;
12  cvec_t cvecin;
13  // do / rdo output results
[ede5d38]14  PyObject *doout;
15  PyObject *rdoout;
[de81d2b]16} Py_fft;
[615ac7d]17
18static PyObject *
19Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20{
[96fe713]21  int win_s = 0;
[615ac7d]22  Py_fft *self;
[96fe713]23  static char *kwlist[] = { "win_s", NULL };
[615ac7d]24
[96fe713]25  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
26          &win_s)) {
[615ac7d]27    return NULL;
28  }
29
30  self = (Py_fft *) type->tp_alloc (type, 0);
31
32  if (self == NULL) {
33    return NULL;
34  }
35
36  self->win_s = Py_default_vector_length;
37
38  if (win_s > 0) {
39    self->win_s = win_s;
40  } else if (win_s < 0) {
41    PyErr_SetString (PyExc_ValueError,
42        "can not use negative window size");
43    return NULL;
44  }
45
46  return (PyObject *) self;
47}
48
[de81d2b]49static int
50Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
51{
52  self->o = new_aubio_fft (self->win_s);
53  if (self->o == NULL) {
[23982aa]54    PyErr_Format(PyExc_RuntimeError,
55        "error creating fft with win_s=%d "
56        "(should be a power of 2 greater than 1; "
57        "try recompiling aubio with --enable-fftw3)",
58        self->win_s);
[de81d2b]59    return -1;
60  }
[b5bef11]61
[ede5d38]62  self->doout = new_py_cvec(self->win_s);
63  self->rdoout = new_py_fvec(self->win_s);
[615ac7d]64
[de81d2b]65  return 0;
66}
[615ac7d]67
[de81d2b]68static void
69Py_fft_del (Py_fft *self, PyObject *unused)
70{
[ede5d38]71  Py_XDECREF(self->doout);
72  Py_XDECREF(self->rdoout);
[23982aa]73  if (self->o) {
74    del_aubio_fft(self->o);
75  }
[5c1200a]76  Py_TYPE(self)->tp_free((PyObject *) self);
[de81d2b]77}
[615ac7d]78
[b5bef11]79static PyObject *
[de81d2b]80Py_fft_do(Py_fft * self, PyObject * args)
[615ac7d]81{
82  PyObject *input;
[a138975]83  cvec_t c_out;
[615ac7d]84
85  if (!PyArg_ParseTuple (args, "O", &input)) {
86    return NULL;
87  }
88
[569b363]89  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
[615ac7d]90    return NULL;
91  }
92
[6014dc0]93  if (self->vecin.length != self->win_s) {
94    PyErr_Format(PyExc_ValueError,
[8147e03]95                 "input array has length %d, but fft expects length %d",
[6014dc0]96                 self->vecin.length, self->win_s);
97    return NULL;
98  }
99
[ede5d38]100  Py_INCREF(self->doout);
101  if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
102    return NULL;
103  }
[615ac7d]104  // compute the function
[ede5d38]105  aubio_fft_do (self->o, &(self->vecin), &c_out);
106  return self->doout;
[615ac7d]107}
108
[5652a8c]109static PyMemberDef Py_fft_members[] = {
[615ac7d]110  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
111    "size of the window"},
[5652a8c]112  {NULL}
113};
[615ac7d]114
[b5bef11]115static PyObject *
[965b302]116Py_fft_rdo(Py_fft * self, PyObject * args)
[615ac7d]117{
118  PyObject *input;
[a138975]119  fvec_t out;
[615ac7d]120
121  if (!PyArg_ParseTuple (args, "O", &input)) {
122    return NULL;
123  }
124
[92a8800]125  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
[615ac7d]126    return NULL;
127  }
128
[1f87c1b]129  if (self->cvecin.length != self->win_s / 2 + 1) {
130    PyErr_Format(PyExc_ValueError,
[8147e03]131                 "input cvec has length %d, but fft expects length %d",
132                 self->cvecin.length, self->win_s / 2 + 1);
[1f87c1b]133    return NULL;
134  }
135
[ede5d38]136  Py_INCREF(self->rdoout);
137  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
138    return NULL;
139  }
[615ac7d]140  // compute the function
[ede5d38]141  aubio_fft_rdo (self->o, &(self->cvecin), &out);
142  return self->rdoout;
[615ac7d]143}
144
145static PyMethodDef Py_fft_methods[] = {
146  {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
147    "synthesis of spectral grain"},
148  {NULL}
149};
150
[5652a8c]151PyTypeObject Py_fftType = {
152  PyVarObject_HEAD_INIT (NULL, 0)
153  "aubio.fft",
154  sizeof (Py_fft),
155  0,
156  (destructor) Py_fft_del,
157  0,
158  0,
159  0,
160  0,
161  0,
162  0,
163  0,
164  0,
165  0,
166  (ternaryfunc)Py_fft_do,
167  0,
168  0,
169  0,
170  0,
171  Py_TPFLAGS_DEFAULT,
172  Py_fft_doc,
173  0,
174  0,
175  0,
176  0,
177  0,
178  0,
179  Py_fft_methods,
180  Py_fft_members,
181  0,
182  0,
183  0,
184  0,
185  0,
186  0,
187  (initproc) Py_fft_init,
188  0,
189  Py_fft_new,
[0e70ef9]190  0,
191  0,
192  0,
193  0,
194  0,
195  0,
196  0,
197  0,
198  0,
[5652a8c]199};
Note: See TracBrowser for help on using the repository browser.