python/ext: simplify memory allocations, removed unneeded malloc/free calls
[aubio.git] / python / ext / py-phasevoc.c
1 #include "aubio-types.h"
2
3 typedef struct
4 {
5   PyObject_HEAD
6   aubio_pvoc_t * o;
7   uint_t win_s;
8   uint_t hop_s;
9   fvec_t vecin;
10   cvec_t *output;
11   Py_cvec *py_out;
12   cvec_t cvecin;
13   fvec_t *routput;
14 } Py_pvoc;
15
16
17 static PyObject *
18 Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
19 {
20   int win_s = 0, hop_s = 0;
21   Py_pvoc *self;
22   static char *kwlist[] = { "win_s", "hop_s", NULL };
23
24   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
25           &win_s, &hop_s)) {
26     return NULL;
27   }
28
29   self = (Py_pvoc *) type->tp_alloc (type, 0);
30
31   if (self == NULL) {
32     return NULL;
33   }
34
35   self->win_s = Py_default_vector_length;
36   self->hop_s = Py_default_vector_length/2;
37
38   if (self == NULL) {
39     return NULL;
40   }
41
42   if (win_s > 0) {
43     self->win_s = win_s;
44   } else if (win_s < 0) {
45     PyErr_SetString (PyExc_ValueError,
46         "can not use negative window size");
47     return NULL;
48   }
49
50   if (hop_s > 0) {
51     self->hop_s = hop_s;
52   } else if (hop_s < 0) {
53     PyErr_SetString (PyExc_ValueError,
54         "can not use negative hop size");
55     return NULL;
56   }
57
58   return (PyObject *) self;
59 }
60
61 static int
62 Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
63 {
64   self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
65   if (self->o == NULL) {
66     char_t errstr[30];
67     sprintf(errstr, "error creating pvoc with %d, %d", self->win_s, self->hop_s);
68     PyErr_SetString (PyExc_RuntimeError, errstr);
69     return -1;
70   }
71
72   self->output = new_cvec(self->win_s);
73   self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
74   self->routput = new_fvec(self->hop_s);
75
76   return 0;
77 }
78
79
80 static void
81 Py_pvoc_del (Py_pvoc *self, PyObject *unused)
82 {
83   del_aubio_pvoc(self->o);
84   del_cvec(self->output);
85   del_fvec(self->routput);
86   Py_TYPE(self)->tp_free((PyObject *) self);
87 }
88
89
90 static PyObject *
91 Py_pvoc_do(Py_pvoc * self, PyObject * args)
92 {
93   PyObject *input;
94
95   if (!PyArg_ParseTuple (args, "O", &input)) {
96     return NULL;
97   }
98
99   if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
100     return NULL;
101   }
102
103   // compute the function
104   aubio_pvoc_do (self->o, &(self->vecin), self->output);
105 #if 0
106   Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
107   PyObject* output = PyAubio_CCvecToPyCvec(self->output, py_out);
108   return output;
109 #else
110   // convert cvec to py_cvec, incrementing refcount to keep a copy
111   return PyAubio_CCvecToPyCvec(self->output, self->py_out);
112 #endif
113 }
114
115 static PyMemberDef Py_pvoc_members[] = {
116   {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
117     "size of the window"},
118   {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
119     "size of the hop"},
120   { NULL } // sentinel
121 };
122
123 static PyObject * 
124 Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
125 {
126   PyObject *input;
127   if (!PyArg_ParseTuple (args, "O", &input)) {
128     return NULL;
129   }
130
131   if (!PyAubio_ArrayToCCvec (input, &(self->cvecin) )) {
132     return NULL;
133   }
134
135   // compute the function
136   aubio_pvoc_rdo (self->o, &(self->cvecin), self->routput);
137   return PyAubio_CFvecToArray(self->routput);
138 }
139
140 static PyMethodDef Py_pvoc_methods[] = {
141   {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
142     "synthesis of spectral grain"},
143   {NULL}
144 };
145
146 PyTypeObject Py_pvocType = {
147   PyVarObject_HEAD_INIT (NULL, 0)
148   "aubio.pvoc",
149   sizeof (Py_pvoc),
150   0,
151   (destructor) Py_pvoc_del,
152   0,
153   0,
154   0,
155   0,
156   0,
157   0,
158   0,
159   0,
160   0,
161   (ternaryfunc)Py_pvoc_do,
162   0,
163   0,
164   0,
165   0,
166   Py_TPFLAGS_DEFAULT,
167   Py_pvoc_doc,
168   0,
169   0,
170   0,
171   0,
172   0,
173   0,
174   Py_pvoc_methods,
175   Py_pvoc_members,
176   0,
177   0,
178   0,
179   0,
180   0,
181   0,
182   (initproc) Py_pvoc_init,
183   0,
184   Py_pvoc_new,
185 };