File: py-phasevoc.c

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (320 lines) | stat: -rw-r--r-- 6,943 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "aubio-types.h"

static char Py_pvoc_doc[] = ""
"pvoc(win_s=512, hop_s=256)\n"
"\n"
"Phase vocoder.\n"
"\n"
"`pvoc` creates callable object implements a phase vocoder [1]_,\n"
"using the tricks detailed in [2]_.\n"
"\n"
"The call function takes one input of type `fvec` and of size\n"
"`hop_s`, and returns a `cvec` of length `win_s//2+1`.\n"
"\n"
"Parameters\n"
"----------\n"
"win_s : int\n"
"  number of channels in the phase-vocoder.\n"
"hop_s : int\n"
"  number of samples expected between each call\n"
"\n"
"Examples\n"
"--------\n"
">>> x = aubio.fvec(256)\n"
">>> pv = aubio.pvoc(512, 256)\n"
">>> pv(x)\n"
"aubio cvec of 257 elements\n"
"\n"
"Default values for hop_s and win_s are provided:\n"
"\n"
">>> pv = aubio.pvoc()\n"
">>> pv.win_s, pv.hop_s\n"
"512, 256\n"
"\n"
"A `cvec` can be resynthesised using `rdo()`:\n"
"\n"
">>> pv = aubio.pvoc(512, 256)\n"
">>> y = aubio.cvec(512)\n"
">>> x_reconstructed = pv.rdo(y)\n"
">>> x_reconstructed.shape\n"
"(256,)\n"
"\n"
"References\n"
"----------\n"
".. [1] James A. Moorer. The use of the phase vocoder in computer music\n"
"   applications. `Journal of the Audio Engineering Society`,\n"
"   26(1/2):42–45, 1978.\n"
".. [2] Amalia de Götzen, Nicolas Bernardini, and Daniel Arfib. Traditional\n"
"   (?) implementations of a phase vocoder: the tricks of the trade.\n"
"   In `Proceedings of the International Conference on Digital Audio\n"
"   Effects` (DAFx-00), pages 37–44, University of Verona, Italy, 2000.\n"
"   (`online version <"
"https://www.cs.princeton.edu/courses/archive/spr09/cos325/Bernardini.pdf"
">`_).\n"
"";


typedef struct
{
  PyObject_HEAD
  aubio_pvoc_t * o;
  uint_t win_s;
  uint_t hop_s;
  fvec_t vecin;
  cvec_t cvecin;
  PyObject *output;
  cvec_t c_output;
  PyObject *routput;
  fvec_t c_routput;
} Py_pvoc;


static PyObject *
Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
{
  int win_s = 0, hop_s = 0;
  Py_pvoc *self;
  static char *kwlist[] = { "win_s", "hop_s", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
          &win_s, &hop_s)) {
    return NULL;
  }

  self = (Py_pvoc *) type->tp_alloc (type, 0);

  if (self == NULL) {
    return NULL;
  }

  self->win_s = Py_default_vector_length;
  self->hop_s = Py_default_vector_length/2;

  if (win_s > 0) {
    self->win_s = win_s;
  } else if (win_s < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative window size");
    return NULL;
  }

  if (hop_s > 0) {
    self->hop_s = hop_s;
  } else if (hop_s < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative hop size");
    return NULL;
  }

  return (PyObject *) self;
}

static int
Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
{
  self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
  if (self->o == NULL) {
    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
    // AUBIO_ERR when failing
    return -1;
  }

  self->output = new_py_cvec(self->win_s);
  self->routput = new_py_fvec(self->hop_s);

  return 0;
}


static void
Py_pvoc_del (Py_pvoc *self, PyObject *unused)
{
  Py_XDECREF(self->output);
  Py_XDECREF(self->routput);
  if (self->o) {
    del_aubio_pvoc(self->o);
  }
  Py_TYPE(self)->tp_free((PyObject *) self);
}


static PyObject *
Py_pvoc_do(Py_pvoc * self, PyObject * args)
{
  PyObject *input;

  if (!PyArg_ParseTuple (args, "O", &input)) {
    return NULL;
  }

  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
    return NULL;
  }

  if (self->vecin.length != self->hop_s) {
    PyErr_Format(PyExc_ValueError,
                 "input fvec has length %d, but pvoc expects length %d",
                 self->vecin.length, self->hop_s);
    return NULL;
  }

  Py_INCREF(self->output);
  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
    return NULL;
  }
  // compute the function
  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
  return self->output;
}

static PyMemberDef Py_pvoc_members[] = {
  {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
    "int: Size of phase vocoder analysis windows, in samples.\n"
    ""},
  {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
    "int: Interval between two analysis, in samples.\n"
    ""},
  { NULL } // sentinel
};

static PyObject *
Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
{
  PyObject *input;
  if (!PyArg_ParseTuple (args, "O", &input)) {
    return NULL;
  }

  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
    return NULL;
  }

  if (self->cvecin.length != self->win_s / 2 + 1) {
    PyErr_Format(PyExc_ValueError,
                 "input cvec has length %d, but pvoc expects length %d",
                 self->cvecin.length, self->win_s / 2 + 1);
    return NULL;
  }

  Py_INCREF(self->routput);
  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
    return NULL;
  }
  // compute the function
  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
  return self->routput;
}

static PyObject *
Pyaubio_pvoc_set_window (Py_pvoc *self, PyObject *args)
{
  uint_t err = 0;
  char_t *window = NULL;

  if (!PyArg_ParseTuple (args, "s", &window)) {
    return NULL;
  }
  err = aubio_pvoc_set_window (self->o, window);

  if (err > 0) {
    PyErr_SetString (PyExc_ValueError, "error running aubio_pvoc_set_window");
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyMethodDef Py_pvoc_methods[] = {
  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
    "rdo(fftgrain)\n"
    "\n"
    "Read a new spectral grain and resynthesise the next `hop_s`\n"
    "output samples.\n"
    "\n"
    "Parameters\n"
    "----------\n"
    "fftgrain : cvec\n"
    "    new input `cvec` to synthesize from, should be of size `win_s/2+1`\n"
    "\n"
    "Returns\n"
    "-------\n"
    "fvec\n"
    "    re-synthesised output of shape `(hop_s,)`\n"
    "\n"
    "Example\n"
    "-------\n"
    ">>> pv = aubio.pvoc(2048, 512)\n"
    ">>> out = pv.rdo(aubio.cvec(2048))\n"
    ">>> out.shape\n"
    "(512,)\n"
    ""},
  {"set_window", (PyCFunction) Pyaubio_pvoc_set_window, METH_VARARGS,
    "set_window(window_type)\n"
    "\n"
    "Set window function\n"
    "\n"
    "Parameters\n"
    "----------\n"
    "window_type : str\n"
    "    the window type to use for this phase vocoder\n"
    "\n"
    "Raises\n"
    "------\n"
    "ValueError\n"
    "    If an unknown window type was given.\n"
    "\n"
    "See Also\n"
    "--------\n"
    "window : create a window.\n"
    ""},
  {NULL}
};

PyTypeObject Py_pvocType = {
  PyVarObject_HEAD_INIT (NULL, 0)
  "aubio.pvoc",
  sizeof (Py_pvoc),
  0,
  (destructor) Py_pvoc_del,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  (ternaryfunc)Py_pvoc_do,
  0,
  0,
  0,
  0,
  Py_TPFLAGS_DEFAULT,
  Py_pvoc_doc,
  0,
  0,
  0,
  0,
  0,
  0,
  Py_pvoc_methods,
  Py_pvoc_members,
  0,
  0,
  0,
  0,
  0,
  0,
  (initproc) Py_pvoc_init,
  0,
  Py_pvoc_new,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
};