source: python/ext/py-phasevoc.c @ 70a304e

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

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

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include "aubio-types.h"
2
3static char Py_pvoc_doc[] = "pvoc object";
4
5typedef struct
6{
7  PyObject_HEAD
8  aubio_pvoc_t * o;
9  uint_t win_s;
10  uint_t hop_s;
11  fvec_t vecin;
12  cvec_t cvecin;
13  PyObject *output;
14  cvec_t c_output;
15  PyObject *routput;
16  fvec_t c_routput;
17} Py_pvoc;
18
19
20static PyObject *
21Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
22{
23  int win_s = 0, hop_s = 0;
24  Py_pvoc *self;
25  static char *kwlist[] = { "win_s", "hop_s", NULL };
26
27  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
28          &win_s, &hop_s)) {
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
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
75  self->output = new_py_cvec(self->win_s);
76  self->routput = new_py_fvec(self->hop_s);
77
78  return 0;
79}
80
81
82static void
83Py_pvoc_del (Py_pvoc *self, PyObject *unused)
84{
85  Py_XDECREF(self->output);
86  Py_XDECREF(self->routput);
87  del_aubio_pvoc(self->o);
88  Py_TYPE(self)->tp_free((PyObject *) self);
89}
90
91
92static PyObject *
93Py_pvoc_do(Py_pvoc * self, PyObject * args)
94{
95  PyObject *input;
96
97  if (!PyArg_ParseTuple (args, "O", &input)) {
98    return NULL;
99  }
100
101  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
102    return NULL;
103  }
104
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
112  Py_INCREF(self->output);
113  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
114    return NULL;
115  }
116  // compute the function
117  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
118  return self->output;
119}
120
121static PyMemberDef Py_pvoc_members[] = {
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"},
126  { NULL } // sentinel
127};
128
129static PyObject *
130Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
131{
132  PyObject *input;
133  if (!PyArg_ParseTuple (args, "O", &input)) {
134    return NULL;
135  }
136
137  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
138    return NULL;
139  }
140
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
148  Py_INCREF(self->routput);
149  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
150    return NULL;
151  }
152  // compute the function
153  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
154  return self->routput;
155}
156
157static PyMethodDef Py_pvoc_methods[] = {
158  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
159    "synthesis of spectral grain"},
160  {NULL}
161};
162
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.