source: python/ext/py-phasevoc.c @ 92a8800

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

python/ext/py-cvec.c: rewrite and simplify aubio.cvec, safer and better memory usage (see #49)

  • Property mode set to 100644
File size: 3.2 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 *output;
13  cvec_t cvecin;
14  fvec_t *routput;
15} Py_pvoc;
16
17
18static PyObject *
19Py_pvoc_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
20{
21  int win_s = 0, hop_s = 0;
22  Py_pvoc *self;
23  static char *kwlist[] = { "win_s", "hop_s", NULL };
24
25  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
26          &win_s, &hop_s)) {
27    return NULL;
28  }
29
30  self = (Py_pvoc *) type->tp_alloc (type, 0);
31
32  if (self == NULL) {
33    return NULL;
34  }
35
36  self->win_s = Py_default_vector_length;
37  self->hop_s = Py_default_vector_length/2;
38
39  if (self == NULL) {
40    return NULL;
41  }
42
43  if (win_s > 0) {
44    self->win_s = win_s;
45  } else if (win_s < 0) {
46    PyErr_SetString (PyExc_ValueError,
47        "can not use negative window size");
48    return NULL;
49  }
50
51  if (hop_s > 0) {
52    self->hop_s = hop_s;
53  } else if (hop_s < 0) {
54    PyErr_SetString (PyExc_ValueError,
55        "can not use negative hop size");
56    return NULL;
57  }
58
59  return (PyObject *) self;
60}
61
62static int
63Py_pvoc_init (Py_pvoc * self, PyObject * args, PyObject * kwds)
64{
65  self->o = new_aubio_pvoc ( self->win_s, self->hop_s);
66  if (self->o == NULL) {
67    char_t errstr[30];
68    sprintf(errstr, "error creating pvoc with %d, %d", self->win_s, self->hop_s);
69    PyErr_SetString (PyExc_RuntimeError, errstr);
70    return -1;
71  }
72
73  self->output = new_cvec(self->win_s);
74  self->routput = new_fvec(self->hop_s);
75
76  return 0;
77}
78
79
80static void
81Py_pvoc_del (Py_pvoc *self, PyObject *unused)
82{
83  del_aubio_pvoc(self->o);
84  del_cvec(self->output);
85  del_fvec(self->routput);
86  Py_TYPE(self)->tp_free((PyObject *) self);
87}
88
89
90static PyObject *
91Py_pvoc_do(Py_pvoc * self, PyObject * args)
92{
93  PyObject *input;
94
95  if (!PyArg_ParseTuple (args, "O", &input)) {
96    return NULL;
97  }
98
99  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
100    return NULL;
101  }
102
103  // compute the function
104  aubio_pvoc_do (self->o, &(self->vecin), self->output);
105  // convert cvec to py_cvec
106  return PyAubio_CCvecToPyCvec(self->output);
107}
108
109static PyMemberDef Py_pvoc_members[] = {
110  {"win_s", T_INT, offsetof (Py_pvoc, win_s), READONLY,
111    "size of the window"},
112  {"hop_s", T_INT, offsetof (Py_pvoc, hop_s), READONLY,
113    "size of the hop"},
114  { NULL } // sentinel
115};
116
117static PyObject * 
118Py_pvoc_rdo(Py_pvoc * self, PyObject * args)
119{
120  PyObject *input;
121  if (!PyArg_ParseTuple (args, "O", &input)) {
122    return NULL;
123  }
124
125  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin) )) {
126    return NULL;
127  }
128
129  // compute the function
130  aubio_pvoc_rdo (self->o, &(self->cvecin), self->routput);
131  return PyAubio_CFvecToArray(self->routput);
132}
133
134static PyMethodDef Py_pvoc_methods[] = {
135  {"rdo", (PyCFunction) Py_pvoc_rdo, METH_VARARGS,
136    "synthesis of spectral grain"},
137  {NULL}
138};
139
140PyTypeObject Py_pvocType = {
141  PyVarObject_HEAD_INIT (NULL, 0)
142  "aubio.pvoc",
143  sizeof (Py_pvoc),
144  0,
145  (destructor) Py_pvoc_del,
146  0,
147  0,
148  0,
149  0,
150  0,
151  0,
152  0,
153  0,
154  0,
155  (ternaryfunc)Py_pvoc_do,
156  0,
157  0,
158  0,
159  0,
160  Py_TPFLAGS_DEFAULT,
161  Py_pvoc_doc,
162  0,
163  0,
164  0,
165  0,
166  0,
167  0,
168  Py_pvoc_methods,
169  Py_pvoc_members,
170  0,
171  0,
172  0,
173  0,
174  0,
175  0,
176  (initproc) Py_pvoc_init,
177  0,
178  Py_pvoc_new,
179};
Note: See TracBrowser for help on using the repository browser.