source: python/ext/py-phasevoc.c @ 21e8408

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

python/ext/py-phasevoc.c: use new_py_fvec, new_py_cvec

  • Property mode set to 100644
File size: 3.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    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  Py_INCREF(self->output);
106  if (!PyAubio_PyCvecToCCvec (self->output, &(self->c_output))) {
107    return NULL;
108  }
109  // compute the function
110  aubio_pvoc_do (self->o, &(self->vecin), &(self->c_output));
111  return self->output;
112}
113
114static PyMemberDef Py_pvoc_members[] = {
115  {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
116    "size of the window"},
117  {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
118    "size of the hop"},
119  { NULL } // sentinel
120};
121
122static PyObject *
123Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
124{
125  PyObject *input;
126  if (!PyArg_ParseTuple (args, "O", &input)) {
127    return NULL;
128  }
129
130  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
131    return NULL;
132  }
133
134  Py_INCREF(self->routput);
135  if (!PyAubio_ArrayToCFvec(self->routput, &(self->c_routput)) ) {
136    return NULL;
137  }
138  // compute the function
139  aubio_pvoc_rdo (self->o, &(self->cvecin), &(self->c_routput));
140  return self->routput;
141}
142
143static PyMethodDef Py_pvoc_methods[] = {
144  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
145    "synthesis of spectral grain"},
146  {NULL}
147};
148
149PyTypeObject Py_pvocType = {
150  PyVarObject_HEAD_INIT (NULL, 0)
151  "aubio.pvoc",
152  sizeof (Py_pvoc),
153  0,
154  (destructor) Py_pvoc_del,
155  0,
156  0,
157  0,
158  0,
159  0,
160  0,
161  0,
162  0,
163  0,
164  (ternaryfunc)Py_pvoc_do,
165  0,
166  0,
167  0,
168  0,
169  Py_TPFLAGS_DEFAULT,
170  Py_pvoc_doc,
171  0,
172  0,
173  0,
174  0,
175  0,
176  0,
177  Py_pvoc_methods,
178  Py_pvoc_members,
179  0,
180  0,
181  0,
182  0,
183  0,
184  0,
185  (initproc) Py_pvoc_init,
186  0,
187  Py_pvoc_new,
188};
Note: See TracBrowser for help on using the repository browser.