1 #include "aubio-types.h"
3 static char Py_pvoc_doc[] = ""
4 "pvoc(win_s=512, hop_s=256)\n"
8 "`pvoc` creates callable object implements a phase vocoder [1]_,\n"
9 "using the tricks detailed in [2]_.\n"
11 "The call function takes one input of type `fvec` and of size\n"
12 "`hop_s`, and returns a `cvec` of length `win_s//2+1`.\n"
17 " number of channels in the phase-vocoder.\n"
19 " number of samples expected between each call\n"
23 ">>> x = aubio.fvec(256)\n"
24 ">>> pv = aubio.pvoc(512, 256)\n"
26 "aubio cvec of 257 elements\n"
28 "Default values for hop_s and win_s are provided:\n"
30 ">>> pv = aubio.pvoc()\n"
31 ">>> pv.win_s, pv.hop_s\n"
34 "A `cvec` can be resynthesised using `rdo()`:\n"
36 ">>> pv = aubio.pvoc(512, 256)\n"
37 ">>> y = aubio.cvec(512)\n"
38 ">>> x_reconstructed = pv.rdo(y)\n"
39 ">>> x_reconstructed.shape\n"
44 ".. [1] James A. Moorer. The use of the phase vocoder in computer music\n"
45 " applications. `Journal of the Audio Engineering Society`,\n"
46 " 26(1/2):42–45, 1978.\n"
47 ".. [2] Amalia de Götzen, Nicolas Bernardini, and Daniel Arfib. Traditional\n"
48 " (?) implementations of a phase vocoder: the tricks of the trade.\n"
49 " In `Proceedings of the International Conference on Digital Audio\n"
50 " Effects` (DAFx-00), pages 37–44, University of Verona, Italy, 2000.\n"
52 "https://www.cs.princeton.edu/courses/archive/spr09/cos325/Bernardini.pdf"
73 Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
75 int win_s = 0, hop_s = 0;
77 static char *kwlist[] = { "win_s", "hop_s", NULL };
79 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
84 self = (Py_pvoc *) type->tp_alloc (type, 0);
90 self->win_s = Py_default_vector_length;
91 self->hop_s = Py_default_vector_length/2;
95 } else if (win_s < 0) {
96 PyErr_SetString (PyExc_ValueError,
97 "can not use negative window size");
103 } else if (hop_s < 0) {
104 PyErr_SetString (PyExc_ValueError,
105 "can not use negative hop size");
109 return (PyObject *) self;
113 Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
115 self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
116 if (self->o == NULL) {
117 // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
118 // AUBIO_ERR when failing
122 self->output = new_py_cvec(self->win_s);
123 self->routput = new_py_fvec(self->hop_s);
130 Py_pvoc_del (Py_pvoc *self, PyObject *unused)
132 Py_XDECREF(self->output);
133 Py_XDECREF(self->routput);
135 del_aubio_pvoc(self->o);
137 Py_TYPE(self)->tp_free((PyObject *) self);
142 Py_pvoc_do(Py_pvoc * self, PyObject * args)
146 if (!PyArg_ParseTuple (args, "O", &input)) {
150 if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
154 if (self->vecin.length != self->hop_s) {
155 PyErr_Format(PyExc_ValueError,
156 "input fvec has length %d, but pvoc expects length %d",
157 self->vecin.length, self->hop_s);
161 Py_INCREF(self->output);
162 if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
165 // compute the function
166 aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
170 static PyMemberDef Py_pvoc_members[] = {
171 {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
172 "int: Size of phase vocoder analysis windows, in samples.\n"
174 {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
175 "int: Interval between two analysis, in samples.\n"
181 Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
184 if (!PyArg_ParseTuple (args, "O", &input)) {
188 if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
192 if (self->cvecin.length != self->win_s / 2 + 1) {
193 PyErr_Format(PyExc_ValueError,
194 "input cvec has length %d, but pvoc expects length %d",
195 self->cvecin.length, self->win_s / 2 + 1);
199 Py_INCREF(self->routput);
200 if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
203 // compute the function
204 aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
205 return self->routput;
209 Pyaubio_pvoc_set_window (Py_pvoc *self, PyObject *args)
212 char_t *window = NULL;
214 if (!PyArg_ParseTuple (args, "s", &window)) {
217 err = aubio_pvoc_set_window (self->o, window);
220 PyErr_SetString (PyExc_ValueError, "error running aubio_pvoc_set_window");
226 static PyMethodDef Py_pvoc_methods[] = {
227 {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
230 "Read a new spectral grain and resynthesise the next `hop_s`\n"
236 " new input `cvec` to synthesize from, should be of size `win_s/2+1`\n"
241 " re-synthesised output of shape `(hop_s,)`\n"
245 ">>> pv = aubio.pvoc(2048, 512)\n"
246 ">>> out = pv.rdo(aubio.cvec(2048))\n"
250 {"set_window", (PyCFunction) Pyaubio_pvoc_set_window, METH_VARARGS,
251 "set_window(window_type)\n"
253 "Set window function\n"
257 "window_type : str\n"
258 " the window type to use for this phase vocoder\n"
263 " If an unknown window type was given.\n"
267 "window : create a window.\n"
272 PyTypeObject Py_pvocType = {
273 PyVarObject_HEAD_INIT (NULL, 0)
277 (destructor) Py_pvoc_del,
287 (ternaryfunc)Py_pvoc_do,
308 (initproc) Py_pvoc_init,