Merge branch 'master' into awhitening
[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 cvecin;
13   PyObject *output;
14   cvec_t c_output;
15   PyObject *routput;
16   fvec_t c_routput;
17 } Py_pvoc;
18
19
20 static PyObject *
21 Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
22 {
23   int win_s = 0, hop_s = 0;
24   Py_pvoc *self;
25   static char *kwlist[] = { "win_s", "hop_s", NULL };
26
27   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
28           &win_s, &hop_s)) {
29     return NULL;
30   }
31
32   self = (Py_pvoc *) type->tp_alloc (type, 0);
33
34   if (self == NULL) {
35     return NULL;
36   }
37
38   self->win_s = Py_default_vector_length;
39   self->hop_s = Py_default_vector_length/2;
40
41   if (self == NULL) {
42     return NULL;
43   }
44
45   if (win_s > 0) {
46     self->win_s = win_s;
47   } else if (win_s < 0) {
48     PyErr_SetString (PyExc_ValueError,
49         "can not use negative window size");
50     return NULL;
51   }
52
53   if (hop_s > 0) {
54     self->hop_s = hop_s;
55   } else if (hop_s < 0) {
56     PyErr_SetString (PyExc_ValueError,
57         "can not use negative hop size");
58     return NULL;
59   }
60
61   return (PyObject *) self;
62 }
63
64 static int
65 Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
66 {
67   self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
68   if (self->o == NULL) {
69     // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
70     // AUBIO_ERR when failing
71     return -1;
72   }
73
74   self->output = new_py_cvec(self->win_s);
75   self->routput = new_py_fvec(self->hop_s);
76
77   return 0;
78 }
79
80
81 static void
82 Py_pvoc_del (Py_pvoc *self, PyObject *unused)
83 {
84   Py_XDECREF(self->output);
85   Py_XDECREF(self->routput);
86   if (self->o) {
87     del_aubio_pvoc(self->o);
88   }
89   Py_TYPE(self)->tp_free((PyObject *) self);
90 }
91
92
93 static PyObject *
94 Py_pvoc_do(Py_pvoc * self, PyObject * args)
95 {
96   PyObject *input;
97
98   if (!PyArg_ParseTuple (args, "O", &input)) {
99     return NULL;
100   }
101
102   if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
103     return NULL;
104   }
105
106   if (self->vecin.length != self->hop_s) {
107     PyErr_Format(PyExc_ValueError,
108                  "input fvec has length %d, but pvoc expects length %d",
109                  self->vecin.length, self->hop_s);
110     return NULL;
111   }
112
113   Py_INCREF(self->output);
114   if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
115     return NULL;
116   }
117   // compute the function
118   aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
119   return self->output;
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_PyCvecToCCvec (input, &(self->cvecin) )) {
139     return NULL;
140   }
141
142   if (self->cvecin.length != self->win_s / 2 + 1) {
143     PyErr_Format(PyExc_ValueError,
144                  "input cvec has length %d, but pvoc expects length %d",
145                  self->cvecin.length, self->win_s / 2 + 1);
146     return NULL;
147   }
148
149   Py_INCREF(self->routput);
150   if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
151     return NULL;
152   }
153   // compute the function
154   aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
155   return self->routput;
156 }
157
158 static PyMethodDef Py_pvoc_methods[] = {
159   {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
160     "synthesis of spectral grain"},
161   {NULL}
162 };
163
164 PyTypeObject Py_pvocType = {
165   PyVarObject_HEAD_INIT (NULL, 0)
166   "aubio.pvoc",
167   sizeof (Py_pvoc),
168   0,
169   (destructor) Py_pvoc_del,
170   0,
171   0,
172   0,
173   0,
174   0,
175   0,
176   0,
177   0,
178   0,
179   (ternaryfunc)Py_pvoc_do,
180   0,
181   0,
182   0,
183   0,
184   Py_TPFLAGS_DEFAULT,
185   Py_pvoc_doc,
186   0,
187   0,
188   0,
189   0,
190   0,
191   0,
192   Py_pvoc_methods,
193   Py_pvoc_members,
194   0,
195   0,
196   0,
197   0,
198   0,
199   0,
200   (initproc) Py_pvoc_init,
201   0,
202   Py_pvoc_new,
203   0,
204   0,
205   0,
206   0,
207   0,
208   0,
209   0,
210   0,
211   0,
212 };