source: python/ext/py-phasevoc.c @ 1e4d90f

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 1e4d90f was 99aa353, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/py-phasevoc.c: add input size checks

  • Property mode set to 100644
File size: 3.9 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) {
69    char_t errstr[30];
70    sprintf(errstr, "error creating pvoc with %d, %d", self->win_s, self->hop_s);
71    PyErr_SetString (PyExc_RuntimeError, errstr);
72    return -1;
73  }
74
[307fdfc]75  self->output = new_py_cvec(self->win_s);
76  self->routput = new_py_fvec(self->hop_s);
[202697a]77
[5652a8c]78  return 0;
[615ac7d]79}
80
81
[202697a]82static void
83Py_pvoc_del (Py_pvoc *self, PyObject *unused)
84{
[307fdfc]85  Py_XDECREF(self->output);
86  Py_XDECREF(self->routput);
[202697a]87  del_aubio_pvoc(self->o);
[5c1200a]88  Py_TYPE(self)->tp_free((PyObject *) self);
[202697a]89}
90
[615ac7d]91
[b5bef11]92static PyObject *
[c04d250]93Py_pvoc_do(Py_pvoc * self, PyObject * args)
[615ac7d]94{
95  PyObject *input;
96
97  if (!PyArg_ParseTuple (args, "O", &input)) {
98    return NULL;
99  }
100
[569b363]101  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
[615ac7d]102    return NULL;
103  }
104
[99aa353]105  if (self->vecin.length != self->hop_s) {
106    PyErr_Format(PyExc_ValueError,
107                 "input fvec has length %d, but pvoc expects length %d",
108                 self->vecin.length, self->hop_s);
109    return NULL;
110  }
111
[307fdfc]112  Py_INCREF(self->output);
113  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
114    return NULL;
115  }
[615ac7d]116  // compute the function
[307fdfc]117  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
118  return self->output;
[615ac7d]119}
120
[5652a8c]121static PyMemberDef Py_pvoc_members[] = {
[615ac7d]122  {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
123    "size of the window"},
124  {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
125    "size of the hop"},
[5652a8c]126  { NULL } // sentinel
127};
[615ac7d]128
[307fdfc]129static PyObject *
[c04d250]130Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
[615ac7d]131{
132  PyObject *input;
133  if (!PyArg_ParseTuple (args, "O", &input)) {
134    return NULL;
135  }
136
[92a8800]137  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
[615ac7d]138    return NULL;
139  }
140
[99aa353]141  if (self->cvecin.length != self->win_s / 2 + 1) {
142    PyErr_Format(PyExc_ValueError,
143                 "input cvec has length %d, but pvoc expects length %d",
144                 self->cvecin.length, self->win_s / 2 + 1);
145    return NULL;
146  }
147
[307fdfc]148  Py_INCREF(self->routput);
149  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
150    return NULL;
151  }
[615ac7d]152  // compute the function
[307fdfc]153  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
154  return self->routput;
[615ac7d]155}
156
157static PyMethodDef Py_pvoc_methods[] = {
158  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
159    "synthesis of spectral grain"},
160  {NULL}
161};
162
[5652a8c]163PyTypeObject Py_pvocType = {
164  PyVarObject_HEAD_INIT (NULL, 0)
165  "aubio.pvoc",
166  sizeof (Py_pvoc),
167  0,
168  (destructor) Py_pvoc_del,
169  0,
170  0,
171  0,
172  0,
173  0,
174  0,
175  0,
176  0,
177  0,
178  (ternaryfunc)Py_pvoc_do,
179  0,
180  0,
181  0,
182  0,
183  Py_TPFLAGS_DEFAULT,
184  Py_pvoc_doc,
185  0,
186  0,
187  0,
188  0,
189  0,
190  0,
191  Py_pvoc_methods,
192  Py_pvoc_members,
193  0,
194  0,
195  0,
196  0,
197  0,
198  0,
199  (initproc) Py_pvoc_init,
200  0,
201  Py_pvoc_new,
202};
Note: See TracBrowser for help on using the repository browser.