File: py-cvec.c

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (281 lines) | stat: -rw-r--r-- 7,634 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "aubio-types.h"

/* cvec type definition

class cvec():
    def __new__(self, length = 1024):
        self.length = length / 2 + 1
        self.norm = np.zeros(length / 2 + 1)
        self.phas = np.zeros(length / 2 + 1)

*/

// special python type for cvec
typedef struct
{
  PyObject_HEAD
  PyObject *norm;
  PyObject *phas;
  uint_t length;
} Py_cvec;

static char Py_cvec_doc[] = ""
"cvec(size)\n"
"\n"
"A container holding spectral data.\n"
"\n"
"Create one `cvec` to store the spectral information of a window\n"
"of `size` points. The data will be stored  in two vectors,\n"
":attr:`phas` and :attr:`norm`, each of shape (:attr:`length`,),\n"
"with `length = size // 2 + 1`.\n"
"\n"
"Parameters\n"
"----------\n"
"size: int\n"
"   Size of spectrum to create.\n"
"\n"
"Examples\n"
"--------\n"
">>> c = aubio.cvec(1024)\n"
">>> c\n"
"aubio cvec of 513 elements\n"
">>> c.length\n"
"513\n"
">>> c.norm.dtype, c.phas.dtype\n"
"(dtype('float32'), dtype('float32'))\n"
">>> c.norm.shape, c.phas.shape\n"
"((513,), (513,))\n"
"\n"
"See Also\n"
"--------\n"
"fvec, fft, pvoc\n"
"";


PyObject *
new_py_cvec(uint_t length) {
  Py_cvec* vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
  npy_intp dims[] = { length / 2 + 1, 1 };
  vec->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
  vec->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
  vec->length = length / 2 + 1;
  return (PyObject*)vec;
}

int
PyAubio_PyCvecToCCvec (PyObject *input, cvec_t *i) {
  if (PyObject_TypeCheck (input, &Py_cvecType)) {
      Py_cvec * in = (Py_cvec *)input;
      i->norm = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->norm), 0);
      i->phas = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)(in->phas), 0);
      i->length = ((Py_cvec*)input)->length;
      return 1;
  } else {
      PyErr_SetString (PyExc_ValueError, "input array should be aubio.cvec");
      return 0;
  }
}

static PyObject *
Py_cvec_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
{
  int length= 0;
  Py_cvec *self;
  static char *kwlist[] = { "length", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
          &length)) {
    return NULL;
  }

  self = (Py_cvec *) type->tp_alloc (type, 0);

  self->length = Py_default_vector_length / 2 + 1;

  if (self == NULL) {
    return NULL;
  }

  if (length > 0) {
    self->length = length / 2 + 1;
  } else if (length < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative number of elements");
    return NULL;
  }

  return (PyObject *) self;
}

static int
Py_cvec_init (Py_cvec * self, PyObject * args, PyObject * kwds)
{
  npy_intp dims[] = { self->length, 1 };
  self->phas = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
  self->norm = PyArray_ZEROS(1, dims, AUBIO_NPY_SMPL, 0);
  return 0;
}

static void
Py_cvec_del (Py_cvec * self)
{
  Py_DECREF(self->norm);
  Py_DECREF(self->phas);
  Py_TYPE(self)->tp_free ((PyObject *) self);
}

static PyObject *
Py_cvec_repr (Py_cvec * self, PyObject * unused)
{
  PyObject *format = NULL;
  PyObject *args = NULL;
  PyObject *result = NULL;

  format = PyUnicode_FromString ("aubio cvec of %d elements");
  if (format == NULL) {
    goto fail;
  }

  args = PyLong_FromLong(self->length);
  if (args == NULL) {
    goto fail;
  }
  // hide actual norm / phas content

  result = PyUnicode_Format (format, args);

fail:
  Py_XDECREF (format);
  Py_XDECREF (args);

  return result;
}

PyObject *
Py_cvec_get_norm (Py_cvec * self, void *closure)
{
  // we want self->norm to still exist after our caller return it
  Py_INCREF(self->norm);
  return (PyObject*)(self->norm);
}

PyObject *
Py_cvec_get_phas (Py_cvec * self, void *closure)
{
  // we want self->phas to still exist after our caller return it
  Py_INCREF(self->phas);
  return (PyObject *)(self->phas);
}

static int
Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
{
  npy_intp length;
  if (!PyAubio_IsValidVector(input)) {
    return -1;
  }
  length = PyArray_SIZE ((PyArrayObject *)input);
  if (length != vec->length) {
    PyErr_Format (PyExc_ValueError,
        "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length,
        vec->length);
    return -1;
  }

  Py_XDECREF(vec->norm);
  vec->norm = input;
  Py_INCREF(vec->norm);
  return 0;
}

static int
Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
{
  npy_intp length;
  if (!PyAubio_IsValidVector(input)) {
    return -1;
  }
  length = PyArray_SIZE ((PyArrayObject *)input);
  if (length != vec->length) {
    PyErr_Format (PyExc_ValueError,
        "input array has length %" NPY_INTP_FMT ", but cvec has length %d", length,
        vec->length);
    return -1;
  }

  Py_XDECREF(vec->phas);
  vec->phas = input;
  Py_INCREF(vec->phas);
  return 0;
}

static PyMemberDef Py_cvec_members[] = {
  // TODO remove READONLY flag and define getter/setter
  {"length", T_INT, offsetof (Py_cvec, length), READONLY,
      "int: Length of `norm` and `phas` vectors."},
  {NULL}                        /* Sentinel */
};

static PyMethodDef Py_cvec_methods[] = {
  {NULL}
};

static PyGetSetDef Py_cvec_getseters[] = {
  {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm,
      "numpy.ndarray: Vector of shape `(length,)` containing the magnitude.",
      NULL},
  {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas,
      "numpy.ndarray: Vector of shape `(length,)` containing the phase.",
      NULL},
  {NULL} /* sentinel */
};

PyTypeObject Py_cvecType = {
  PyVarObject_HEAD_INIT(NULL, 0)
  "aubio.cvec",                 /* tp_name           */
  sizeof (Py_cvec),             /* tp_basicsize      */
  0,                            /* tp_itemsize       */
  (destructor) Py_cvec_del,     /* tp_dealloc        */
  0,                            /* tp_print          */
  0,                            /* tp_getattr        */
  0,                            /* tp_setattr        */
  0,                            /* tp_compare        */
  (reprfunc) Py_cvec_repr,      /* tp_repr           */
  0,                            /* tp_as_number      */
  0, //&Py_cvec_tp_as_sequence, /* tp_as_sequence    */
  0,                            /* tp_as_mapping     */
  0,                            /* tp_hash           */
  0,                            /* tp_call           */
  0,                            /* tp_str            */
  0,                            /* tp_getattro       */
  0,                            /* tp_setattro       */
  0,                            /* tp_as_buffer      */
  Py_TPFLAGS_DEFAULT,           /* tp_flags          */
  Py_cvec_doc,                  /* tp_doc            */
  0,                            /* tp_traverse       */
  0,                            /* tp_clear          */
  0,                            /* tp_richcompare    */
  0,                            /* tp_weaklistoffset */
  0,                            /* tp_iter           */
  0,                            /* tp_iternext       */
  Py_cvec_methods,              /* tp_methods        */
  Py_cvec_members,              /* tp_members        */
  Py_cvec_getseters,            /* tp_getset         */
  0,                            /* tp_base           */
  0,                            /* tp_dict           */
  0,                            /* tp_descr_get      */
  0,                            /* tp_descr_set      */
  0,                            /* tp_dictoffset     */
  (initproc) Py_cvec_init,      /* tp_init           */
  0,                            /* tp_alloc          */
  Py_cvec_new,                  /* tp_new            */
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
};