source: python/ext/py-phasevoc.c @ 023aa32

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

python/{ext/*.c,lib/gen_code.py}: complete PyTypeObject? definitions

  • 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    PyErr_Format(PyExc_RuntimeError,
70        "failed creating pvoc with win_s=%d, hop_s=%d",
71        self->win_s, self->hop_s);
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  if (self->o) {
88    del_aubio_pvoc(self->o);
89  }
90  Py_TYPE(self)->tp_free((PyObject *) self);
91}
92
93
94static PyObject *
95Py_pvoc_do(Py_pvoc * self, PyObject * args)
96{
97  PyObject *input;
98
99  if (!PyArg_ParseTuple (args, "O", &input)) {
100    return NULL;
101  }
102
103  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
104    return NULL;
105  }
106
107  if (self->vecin.length != self->hop_s) {
108    PyErr_Format(PyExc_ValueError,
109                 "input fvec has length %d, but pvoc expects length %d",
110                 self->vecin.length, self->hop_s);
111    return NULL;
112  }
113
114  Py_INCREF(self->output);
115  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
116    return NULL;
117  }
118  // compute the function
119  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
120  return self->output;
121}
122
123static PyMemberDef Py_pvoc_members[] = {
124  {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
125    "size of the window"},
126  {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
127    "size of the hop"},
128  { NULL } // sentinel
129};
130
131static PyObject *
132Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
133{
134  PyObject *input;
135  if (!PyArg_ParseTuple (args, "O", &input)) {
136    return NULL;
137  }
138
139  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
140    return NULL;
141  }
142
143  if (self->cvecin.length != self->win_s / 2 + 1) {
144    PyErr_Format(PyExc_ValueError,
145                 "input cvec has length %d, but pvoc expects length %d",
146                 self->cvecin.length, self->win_s / 2 + 1);
147    return NULL;
148  }
149
150  Py_INCREF(self->routput);
151  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
152    return NULL;
153  }
154  // compute the function
155  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
156  return self->routput;
157}
158
159static PyMethodDef Py_pvoc_methods[] = {
160  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
161    "synthesis of spectral grain"},
162  {NULL}
163};
164
165PyTypeObject Py_pvocType = {
166  PyVarObject_HEAD_INIT (NULL, 0)
167  "aubio.pvoc",
168  sizeof (Py_pvoc),
169  0,
170  (destructor) Py_pvoc_del,
171  0,
172  0,
173  0,
174  0,
175  0,
176  0,
177  0,
178  0,
179  0,
180  (ternaryfunc)Py_pvoc_do,
181  0,
182  0,
183  0,
184  0,
185  Py_TPFLAGS_DEFAULT,
186  Py_pvoc_doc,
187  0,
188  0,
189  0,
190  0,
191  0,
192  0,
193  Py_pvoc_methods,
194  Py_pvoc_members,
195  0,
196  0,
197  0,
198  0,
199  0,
200  0,
201  (initproc) Py_pvoc_init,
202  0,
203  Py_pvoc_new,
204  0,
205  0,
206  0,
207  0,
208  0,
209  0,
210  0,
211  0,
212  0,
213};
Note: See TracBrowser for help on using the repository browser.