source: python/ext/py-fft.c @ 61a1e5d

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

python/ext/py-fft.c: use error string set in src/spectral/fft.c

  • Property mode set to 100644
File size: 3.5 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) {
[dab4a4c]54    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
55    // AUBIO_ERR when failing
[de81d2b]56    return -1;
57  }
[b5bef11]58
[ede5d38]59  self->doout = new_py_cvec(self->win_s);
60  self->rdoout = new_py_fvec(self->win_s);
[615ac7d]61
[de81d2b]62  return 0;
63}
[615ac7d]64
[de81d2b]65static void
66Py_fft_del (Py_fft *self, PyObject *unused)
67{
[ede5d38]68  Py_XDECREF(self->doout);
69  Py_XDECREF(self->rdoout);
[23982aa]70  if (self->o) {
71    del_aubio_fft(self->o);
72  }
[5c1200a]73  Py_TYPE(self)->tp_free((PyObject *) self);
[de81d2b]74}
[615ac7d]75
[b5bef11]76static PyObject *
[de81d2b]77Py_fft_do(Py_fft * self, PyObject * args)
[615ac7d]78{
79  PyObject *input;
[a138975]80  cvec_t c_out;
[615ac7d]81
82  if (!PyArg_ParseTuple (args, "O", &input)) {
83    return NULL;
84  }
85
[569b363]86  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
[615ac7d]87    return NULL;
88  }
89
[6014dc0]90  if (self->vecin.length != self->win_s) {
91    PyErr_Format(PyExc_ValueError,
[8147e03]92                 "input array has length %d, but fft expects length %d",
[6014dc0]93                 self->vecin.length, self->win_s);
94    return NULL;
95  }
96
[ede5d38]97  Py_INCREF(self->doout);
98  if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
99    return NULL;
100  }
[615ac7d]101  // compute the function
[ede5d38]102  aubio_fft_do (self->o, &(self->vecin), &c_out);
103  return self->doout;
[615ac7d]104}
105
[5652a8c]106static PyMemberDef Py_fft_members[] = {
[615ac7d]107  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
108    "size of the window"},
[5652a8c]109  {NULL}
110};
[615ac7d]111
[b5bef11]112static PyObject *
[965b302]113Py_fft_rdo(Py_fft * self, PyObject * args)
[615ac7d]114{
115  PyObject *input;
[a138975]116  fvec_t out;
[615ac7d]117
118  if (!PyArg_ParseTuple (args, "O", &input)) {
119    return NULL;
120  }
121
[92a8800]122  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
[615ac7d]123    return NULL;
124  }
125
[1f87c1b]126  if (self->cvecin.length != self->win_s / 2 + 1) {
127    PyErr_Format(PyExc_ValueError,
[8147e03]128                 "input cvec has length %d, but fft expects length %d",
129                 self->cvecin.length, self->win_s / 2 + 1);
[1f87c1b]130    return NULL;
131  }
132
[ede5d38]133  Py_INCREF(self->rdoout);
134  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
135    return NULL;
136  }
[615ac7d]137  // compute the function
[ede5d38]138  aubio_fft_rdo (self->o, &(self->cvecin), &out);
139  return self->rdoout;
[615ac7d]140}
141
142static PyMethodDef Py_fft_methods[] = {
143  {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
144    "synthesis of spectral grain"},
145  {NULL}
146};
147
[5652a8c]148PyTypeObject Py_fftType = {
149  PyVarObject_HEAD_INIT (NULL, 0)
150  "aubio.fft",
151  sizeof (Py_fft),
152  0,
153  (destructor) Py_fft_del,
154  0,
155  0,
156  0,
157  0,
158  0,
159  0,
160  0,
161  0,
162  0,
163  (ternaryfunc)Py_fft_do,
164  0,
165  0,
166  0,
167  0,
168  Py_TPFLAGS_DEFAULT,
169  Py_fft_doc,
170  0,
171  0,
172  0,
173  0,
174  0,
175  0,
176  Py_fft_methods,
177  Py_fft_members,
178  0,
179  0,
180  0,
181  0,
182  0,
183  0,
184  (initproc) Py_fft_init,
185  0,
186  Py_fft_new,
[0e70ef9]187  0,
188  0,
189  0,
190  0,
191  0,
192  0,
193  0,
194  0,
195  0,
[5652a8c]196};
Note: See TracBrowser for help on using the repository browser.