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;
99 } else if (win_s < 0) {
100 PyErr_SetString (PyExc_ValueError,
101 "can not use negative window size");
107 } else if (hop_s < 0) {
108 PyErr_SetString (PyExc_ValueError,
109 "can not use negative hop size");
113 return (PyObject *) self;
117 Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
119 self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
120 if (self->o == NULL) {
121 // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
122 // AUBIO_ERR when failing
126 self->output = new_py_cvec(self->win_s);
127 self->routput = new_py_fvec(self->hop_s);
134 Py_pvoc_del (Py_pvoc *self, PyObject *unused)
136 Py_XDECREF(self->output);
137 Py_XDECREF(self->routput);
139 del_aubio_pvoc(self->o);
141 Py_TYPE(self)->tp_free((PyObject *) self);
146 Py_pvoc_do(Py_pvoc * self, PyObject * args)
150 if (!PyArg_ParseTuple (args, "O", &input)) {
154 if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
158 if (self->vecin.length != self->hop_s) {
159 PyErr_Format(PyExc_ValueError,
160 "input fvec has length %d, but pvoc expects length %d",
161 self->vecin.length, self->hop_s);
165 Py_INCREF(self->output);
166 if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
169 // compute the function
170 aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
174 static PyMemberDef Py_pvoc_members[] = {
175 {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
176 "int: Size of phase vocoder analysis windows, in samples.\n"
178 {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
179 "int: Interval between two analysis, in samples.\n"
185 Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
188 if (!PyArg_ParseTuple (args, "O", &input)) {
192 if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
196 if (self->cvecin.length != self->win_s / 2 + 1) {
197 PyErr_Format(PyExc_ValueError,
198 "input cvec has length %d, but pvoc expects length %d",
199 self->cvecin.length, self->win_s / 2 + 1);
203 Py_INCREF(self->routput);
204 if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
207 // compute the function
208 aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
209 return self->routput;
213 Pyaubio_pvoc_set_window (Py_pvoc *self, PyObject *args)
216 char_t *window = NULL;
218 if (!PyArg_ParseTuple (args, "s", &window)) {
221 err = aubio_pvoc_set_window (self->o, window);
224 PyErr_SetString (PyExc_ValueError, "error running aubio_pvoc_set_window");
230 static PyMethodDef Py_pvoc_methods[] = {
231 {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
234 "Read a new spectral grain and resynthesise the next `hop_s`\n"
240 " new input `cvec` to synthesize from, should be of size `win_s/2+1`\n"
245 " re-synthesised output of shape `(hop_s,)`\n"
249 ">>> pv = aubio.pvoc(2048, 512)\n"
250 ">>> out = pv.rdo(aubio.cvec(2048))\n"
254 {"set_window", (PyCFunction) Pyaubio_pvoc_set_window, METH_VARARGS,
255 "set_window(window_type)\n"
257 "Set window function\n"
261 "window_type : str\n"
262 " the window type to use for this phase vocoder\n"
267 " If an unknown window type was given.\n"
271 "window : create a window.\n"
276 PyTypeObject Py_pvocType = {
277 PyVarObject_HEAD_INIT (NULL, 0)
281 (destructor) Py_pvoc_del,
291 (ternaryfunc)Py_pvoc_do,
312 (initproc) Py_pvoc_init,