source: python/ext/py-phasevoc.c @ e458af4

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since e458af4 was 965adee, checked in by Paul Brossier <piem@piem.org>, 7 years ago

python/ext/py-phasevoc.c: add _set_window

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[5652a8c]1#include "aubio-types.h"
[615ac7d]2
[51b9c83]3static char Py_pvoc_doc[] = "pvoc object";
4
[202697a]5typedef struct
6{
7  PyObject_HEAD
8  aubio_pvoc_t * o;
9  uint_t win_s;
10  uint_t hop_s;
[569b363]11  fvec_t vecin;
12  cvec_t cvecin;
[307fdfc]13  PyObject *output;
14  cvec_t c_output;
15  PyObject *routput;
16  fvec_t c_routput;
[202697a]17} Py_pvoc;
18
[615ac7d]19
20static PyObject *
21Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
22{
[0f045b2]23  int win_s = 0, hop_s = 0;
[615ac7d]24  Py_pvoc *self;
[0f045b2]25  static char *kwlist[] = { "win_s", "hop_s", NULL };
[615ac7d]26
[0f045b2]27  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
28          &win_s, &hop_s)) {
[615ac7d]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
[5652a8c]61  return (PyObject *) self;
62}
63
64static int
65Py_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) {
[3528079]69    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
70    // AUBIO_ERR when failing
[5652a8c]71    return -1;
72  }
73
[307fdfc]74  self->output = new_py_cvec(self->win_s);
75  self->routput = new_py_fvec(self->hop_s);
[202697a]76
[5652a8c]77  return 0;
[615ac7d]78}
79
80
[202697a]81static void
82Py_pvoc_del (Py_pvoc *self, PyObject *unused)
83{
[307fdfc]84  Py_XDECREF(self->output);
85  Py_XDECREF(self->routput);
[8cf51c4]86  if (self->o) {
87    del_aubio_pvoc(self->o);
88  }
[5c1200a]89  Py_TYPE(self)->tp_free((PyObject *) self);
[202697a]90}
91
[615ac7d]92
[b5bef11]93static PyObject *
[c04d250]94Py_pvoc_do(Py_pvoc * self, PyObject * args)
[615ac7d]95{
96  PyObject *input;
97
98  if (!PyArg_ParseTuple (args, "O", &input)) {
99    return NULL;
100  }
101
[569b363]102  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
[615ac7d]103    return NULL;
104  }
105
[99aa353]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
[307fdfc]113  Py_INCREF(self->output);
114  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
115    return NULL;
116  }
[615ac7d]117  // compute the function
[307fdfc]118  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
119  return self->output;
[615ac7d]120}
121
[5652a8c]122static PyMemberDef Py_pvoc_members[] = {
[615ac7d]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"},
[5652a8c]127  { NULL } // sentinel
128};
[615ac7d]129
[307fdfc]130static PyObject *
[c04d250]131Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
[615ac7d]132{
133  PyObject *input;
134  if (!PyArg_ParseTuple (args, "O", &input)) {
135    return NULL;
136  }
137
[92a8800]138  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
[615ac7d]139    return NULL;
140  }
141
[99aa353]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
[307fdfc]149  Py_INCREF(self->routput);
150  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
151    return NULL;
152  }
[615ac7d]153  // compute the function
[307fdfc]154  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
155  return self->routput;
[615ac7d]156}
157
[965adee]158static PyObject *
159Pyaubio_pvoc_set_window (Py_pvoc *self, PyObject *args)
160{
161  uint_t err = 0;
162  char_t *window = NULL;
163
164  if (!PyArg_ParseTuple (args, "s", &window)) {
165    return NULL;
166  }
167  err = aubio_pvoc_set_window (self->o, window);
168
169  if (err > 0) {
170    PyErr_SetString (PyExc_ValueError, "error running aubio_pvoc_set_window");
171    return NULL;
172  }
173  Py_RETURN_NONE;
174}
175
[615ac7d]176static PyMethodDef Py_pvoc_methods[] = {
177  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
178    "synthesis of spectral grain"},
[965adee]179  {"set_window", (PyCFunction) Pyaubio_pvoc_set_window, METH_VARARGS, ""},
[615ac7d]180  {NULL}
181};
182
[5652a8c]183PyTypeObject Py_pvocType = {
184  PyVarObject_HEAD_INIT (NULL, 0)
185  "aubio.pvoc",
186  sizeof (Py_pvoc),
187  0,
188  (destructor) Py_pvoc_del,
189  0,
190  0,
191  0,
192  0,
193  0,
194  0,
195  0,
196  0,
197  0,
198  (ternaryfunc)Py_pvoc_do,
199  0,
200  0,
201  0,
202  0,
203  Py_TPFLAGS_DEFAULT,
204  Py_pvoc_doc,
205  0,
206  0,
207  0,
208  0,
209  0,
210  0,
211  Py_pvoc_methods,
212  Py_pvoc_members,
213  0,
214  0,
215  0,
216  0,
217  0,
218  0,
219  (initproc) Py_pvoc_init,
220  0,
221  Py_pvoc_new,
[0e70ef9]222  0,
223  0,
224  0,
225  0,
226  0,
227  0,
228  0,
229  0,
230  0,
[5652a8c]231};
Note: See TracBrowser for help on using the repository browser.