source: python/ext/py-fft.c @ 92a8800

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

python/ext/py-cvec.c: rewrite and simplify aubio.cvec, safer and better memory usage (see #49)

  • Property mode set to 100644
File size: 2.8 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} Py_fft;
17
18static PyObject *
19Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20{
21  int win_s = 0;
22  Py_fft *self;
23  static char *kwlist[] = { "win_s", NULL };
24
25  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
26          &win_s)) {
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
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);
56    PyErr_SetString (PyExc_Exception, errstr);
57    return -1;
58  }
59
60  self->out = new_cvec(self->win_s);
61  self->rout = new_fvec(self->win_s);
62
63  return 0;
64}
65
66static void
67Py_fft_del (Py_fft *self, PyObject *unused)
68{
69  del_aubio_fft(self->o);
70  del_cvec(self->out);
71  del_fvec(self->rout);
72  Py_TYPE(self)->tp_free((PyObject *) self);
73}
74
75static PyObject *
76Py_fft_do(Py_fft * self, PyObject * args)
77{
78  PyObject *input;
79
80  if (!PyArg_ParseTuple (args, "O", &input)) {
81    return NULL;
82  }
83
84  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
85    return NULL;
86  }
87
88  // compute the function
89  aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out);
90  // convert cvec to py_cvec
91  return PyAubio_CCvecToPyCvec(self->out);
92}
93
94static PyMemberDef Py_fft_members[] = {
95  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
96    "size of the window"},
97  {NULL}
98};
99
100static PyObject *
101Py_fft_rdo(Py_fft * self, PyObject * args)
102{
103  PyObject *input;
104
105  if (!PyArg_ParseTuple (args, "O", &input)) {
106    return NULL;
107  }
108
109  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
110    return NULL;
111  }
112
113  // compute the function
114  aubio_fft_rdo (self->o, &(self->cvecin), self->rout);
115  return PyAubio_CFvecToArray(self->rout);
116}
117
118static PyMethodDef Py_fft_methods[] = {
119  {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
120    "synthesis of spectral grain"},
121  {NULL}
122};
123
124PyTypeObject Py_fftType = {
125  PyVarObject_HEAD_INIT (NULL, 0)
126  "aubio.fft",
127  sizeof (Py_fft),
128  0,
129  (destructor) Py_fft_del,
130  0,
131  0,
132  0,
133  0,
134  0,
135  0,
136  0,
137  0,
138  0,
139  (ternaryfunc)Py_fft_do,
140  0,
141  0,
142  0,
143  0,
144  Py_TPFLAGS_DEFAULT,
145  Py_fft_doc,
146  0,
147  0,
148  0,
149  0,
150  0,
151  0,
152  Py_fft_methods,
153  Py_fft_members,
154  0,
155  0,
156  0,
157  0,
158  0,
159  0,
160  (initproc) Py_fft_init,
161  0,
162  Py_fft_new,
163};
Note: See TracBrowser for help on using the repository browser.