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

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since f5b8800 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
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    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
70    // AUBIO_ERR when failing
71    return -1;
72  }
73
74  self->output = new_py_cvec(self->win_s);
75  self->routput = new_py_fvec(self->hop_s);
76
77  return 0;
78}
79
80
81static void
82Py_pvoc_del (Py_pvoc *self, PyObject *unused)
83{
84  Py_XDECREF(self->output);
85  Py_XDECREF(self->routput);
86  if (self->o) {
87    del_aubio_pvoc(self->o);
88  }
89  Py_TYPE(self)->tp_free((PyObject *) self);
90}
91
92
93static PyObject *
94Py_pvoc_do(Py_pvoc * self, PyObject * args)
95{
96  PyObject *input;
97
98  if (!PyArg_ParseTuple (args, "O", &input)) {
99    return NULL;
100  }
101
102  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
103    return NULL;
104  }
105
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
113  Py_INCREF(self->output);
114  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
115    return NULL;
116  }
117  // compute the function
118  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
119  return self->output;
120}
121
122static PyMemberDef Py_pvoc_members[] = {
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"},
127  { NULL } // sentinel
128};
129
130static PyObject *
131Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
132{
133  PyObject *input;
134  if (!PyArg_ParseTuple (args, "O", &input)) {
135    return NULL;
136  }
137
138  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
139    return NULL;
140  }
141
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
149  Py_INCREF(self->routput);
150  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
151    return NULL;
152  }
153  // compute the function
154  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
155  return self->routput;
156}
157
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
176static PyMethodDef Py_pvoc_methods[] = {
177  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
178    "synthesis of spectral grain"},
179  {"set_window", (PyCFunction) Pyaubio_pvoc_set_window, METH_VARARGS, ""},
180  {NULL}
181};
182
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,
222  0,
223  0,
224  0,
225  0,
226  0,
227  0,
228  0,
229  0,
230  0,
231};
Note: See TracBrowser for help on using the repository browser.