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