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

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

ext/: no more hell, use plain c

  • Property mode set to 100644
File size: 2.7 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  cvec_t *out;
11  fvec_t *rout;
12} Py_fft;
13
14static PyObject *
15Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
16{
17  int win_s = 0;
18  Py_fft *self;
19  static char *kwlist[] = { "win_s", NULL };
20
21  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
22          &win_s)) {
23    return NULL;
24  }
25
26  self = (Py_fft *) type->tp_alloc (type, 0);
27
28  if (self == NULL) {
29    return NULL;
30  }
31
32  self->win_s = Py_default_vector_length;
33
34  if (win_s > 0) {
35    self->win_s = win_s;
36  } else if (win_s < 0) {
37    PyErr_SetString (PyExc_ValueError,
38        "can not use negative window size");
39    return NULL;
40  }
41
42  return (PyObject *) self;
43}
44
45static int
46Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
47{
48  self->o = new_aubio_fft (self->win_s);
49  if (self->o == NULL) {
50    char_t errstr[30];
51    sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
52    PyErr_SetString (PyExc_Exception, errstr);
53    return -1;
54  }
55  self->out = new_cvec(self->win_s);
56  self->rout = new_fvec(self->win_s);
57
58  return 0;
59}
60
61static void
62Py_fft_del (Py_fft *self, PyObject *unused)
63{
64  del_aubio_fft(self->o);
65  del_cvec(self->out);
66  del_fvec(self->rout);
67  Py_TYPE(self)->tp_free((PyObject *) self);
68}
69
70static PyObject * 
71Py_fft_do(Py_fft * self, PyObject * args)
72{
73  PyObject *input;
74  fvec_t *vec;
75
76  if (!PyArg_ParseTuple (args, "O", &input)) {
77    return NULL;
78  }
79
80  vec = PyAubio_ArrayToCFvec (input);
81
82  if (vec == NULL) {
83    return NULL;
84  }
85
86  // compute the function
87  aubio_fft_do (((Py_fft *)self)->o, vec, self->out);
88  return (PyObject *)PyAubio_CCvecToPyCvec(self->out);
89}
90
91static PyMemberDef Py_fft_members[] = {
92  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
93    "size of the window"},
94  {NULL}
95};
96
97static PyObject * 
98Py_fft_rdo(Py_fft * self, PyObject * args)
99{
100  PyObject *input;
101  cvec_t *vec;
102
103  if (!PyArg_ParseTuple (args, "O", &input)) {
104    return NULL;
105  }
106
107  vec = PyAubio_ArrayToCCvec (input);
108
109  if (vec == NULL) {
110    return NULL;
111  }
112
113  // compute the function
114  aubio_fft_rdo (((Py_fft *)self)->o, vec, self->rout);
115  return (PyObject *)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.