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

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

ext/py-fft.c: continue fixing memory leak (#49)

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include "aubiowraphell.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
14//AUBIO_NEW(fft)
15static PyObject *
16Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
17{
18  int win_s = 0;
19  Py_fft *self;
20  static char *kwlist[] = { "win_s", NULL };
21
22  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
23          &win_s)) {
24    return NULL;
25  }
26
27  self = (Py_fft *) type->tp_alloc (type, 0);
28
29  if (self == NULL) {
30    return NULL;
31  }
32
33  self->win_s = Py_default_vector_length;
34
35  if (win_s > 0) {
36    self->win_s = win_s;
37  } else if (win_s < 0) {
38    PyErr_SetString (PyExc_ValueError,
39        "can not use negative window size");
40    return NULL;
41  }
42
43  return (PyObject *) self;
44}
45
46static int
47Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
48{
49  self->o = new_aubio_fft (self->win_s);
50  if (self->o == NULL) {
51    char_t errstr[30];
52    sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
53    PyErr_SetString (PyExc_StandardError, errstr);
54    return -1;
55  }
56  self->out = new_cvec(self->win_s);
57  self->rout = new_fvec(self->win_s);
58
59  return 0;
60}
61
62static void
63Py_fft_del (Py_fft *self, PyObject *unused)
64{
65  del_aubio_fft(self->o);
66  del_cvec(self->out);
67  del_fvec(self->rout);
68  self->ob_type->tp_free((PyObject *) self);
69}
70
71static PyObject * 
72Py_fft_do(Py_fft * self, PyObject * args)
73{
74  PyObject *input;
75  fvec_t *vec;
76
77  if (!PyArg_ParseTuple (args, "O", &input)) {
78    return NULL;
79  }
80
81  vec = PyAubio_ArrayToCFvec (input);
82
83  if (vec == NULL) {
84    return NULL;
85  }
86
87  // compute the function
88  aubio_fft_do (((Py_fft *)self)->o, vec, self->out);
89  return (PyObject *)PyAubio_CCvecToPyCvec(self->out);
90}
91
92AUBIO_MEMBERS_START(fft) 
93  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
94    "size of the window"},
95AUBIO_MEMBERS_STOP(fft)
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
124AUBIO_TYPEOBJECT(fft, "aubio.fft")
Note: See TracBrowser for help on using the repository browser.