source: python/ext/py-phasevoc.c @ 4077fa1

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 4077fa1 was 3528079, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/ext/py-phasevoc.c: use error string set in src/spectral/phasevoc.c

  • 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) {
[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
158static PyMethodDef Py_pvoc_methods[] = {
159  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
160    "synthesis of spectral grain"},
161  {NULL}
162};
163
[5652a8c]164PyTypeObject Py_pvocType = {
165  PyVarObject_HEAD_INIT (NULL, 0)
166  "aubio.pvoc",
167  sizeof (Py_pvoc),
168  0,
169  (destructor) Py_pvoc_del,
170  0,
171  0,
172  0,
173  0,
174  0,
175  0,
176  0,
177  0,
178  0,
179  (ternaryfunc)Py_pvoc_do,
180  0,
181  0,
182  0,
183  0,
184  Py_TPFLAGS_DEFAULT,
185  Py_pvoc_doc,
186  0,
187  0,
188  0,
189  0,
190  0,
191  0,
192  Py_pvoc_methods,
193  Py_pvoc_members,
194  0,
195  0,
196  0,
197  0,
198  0,
199  0,
200  (initproc) Py_pvoc_init,
201  0,
202  Py_pvoc_new,
[0e70ef9]203  0,
204  0,
205  0,
206  0,
207  0,
208  0,
209  0,
210  0,
211  0,
[5652a8c]212};
Note: See TracBrowser for help on using the repository browser.