source: python/ext/py-fft.c @ 8147e03

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

python/ext/py-fft.c: improve error messages

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