source: python/ext/aubioproxy.c @ 0e362b5

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

ext/aubioproxy.c: improve sizes checks, cast to uint_t

Signed-off-by: Paul Brossier <piem@piem.org>

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[7395ec5]1#include "aubio-types.h"
2
3fvec_t *
4PyAubio_ArrayToCFvec (PyObject *input) {
5  PyObject *array;
6  fvec_t *vec;
7  if (input == NULL) {
8    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
9    goto fail;
10  }
11  // parsing input object into a Py_fvec
12  if (PyArray_Check(input)) {
13
[93004d5]14    // we got an array, convert it to an fvec
[1458de5]15    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
[7395ec5]16      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
17      goto fail;
[1458de5]18    } else if (PyArray_NDIM ((PyArrayObject *)input) > 1) {
[7395ec5]19      PyErr_SetString (PyExc_ValueError,
20          "input array has more than one dimensions");
21      goto fail;
22    }
23
[1458de5]24    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
[7395ec5]25      PyErr_SetString (PyExc_ValueError, "input array should be float");
26      goto fail;
[1458de5]27    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
[7395ec5]28      PyErr_SetString (PyExc_ValueError, "input array should be float32");
29      goto fail;
30    } else {
31      // input data type is float32, nothing else to do
32      array = input;
33    }
34
35    // vec = new_fvec (vec->length);
[93004d5]36    // no need to really allocate fvec, just its struct member
[7395ec5]37    vec = (fvec_t *)malloc(sizeof(fvec_t));
[3194dca1]38    long length = PyArray_SIZE ((PyArrayObject *)array);
39    if (length > 0) {
40      vec->length = (uint_t)length;
41    } else {
42      PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
43      goto fail;
44    }
[1458de5]45    vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0);
[7395ec5]46
47  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
48    PyErr_SetString (PyExc_ValueError, "does not convert from list yet");
49    return NULL;
50  } else {
51    PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
52    return NULL;
53  }
54
55  return vec;
56
57fail:
58  return NULL;
59}
60
61PyObject *
62PyAubio_CFvecToArray (fvec_t * self)
63{
64  npy_intp dims[] = { self->length, 1 };
65  return PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->data);
66}
67
68Py_cvec *
69PyAubio_CCvecToPyCvec (cvec_t * input) {
[93004d5]70  Py_cvec *vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
[7395ec5]71  vec->length = input->length;
72  vec->o = input;
73  Py_INCREF(vec);
74  return vec;
75}
76
77cvec_t *
78PyAubio_ArrayToCCvec (PyObject *input) {
79  if (PyObject_TypeCheck (input, &Py_cvecType)) {
80      return ((Py_cvec*)input)->o;
81  } else {
82      PyErr_SetString (PyExc_ValueError, "input array should be float32");
83      return NULL;
84  }
85}
86
87PyObject *
88PyAubio_CFmatToArray (fmat_t * input)
89{
90  PyObject *array = NULL;
91  uint_t i;
92  npy_intp dims[] = { input->length, 1 };
93  PyObject *concat = PyList_New (0), *tmp = NULL;
94  for (i = 0; i < input->height; i++) {
95    tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, input->data[i]);
96    PyList_Append (concat, tmp);
97    Py_DECREF (tmp);
98  }
99  array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
100  Py_DECREF (concat);
101  return array;
102}
103
104fmat_t *
105PyAubio_ArrayToCFmat (PyObject *input) {
[93004d5]106  PyObject *array;
107  fmat_t *mat;
108  uint_t i;
109  if (input == NULL) {
110    PyErr_SetString (PyExc_ValueError, "input array is not a python object");
111    goto fail;
112  }
113  // parsing input object into a Py_fvec
114  if (PyArray_Check(input)) {
115
116    // we got an array, convert it to an fvec
117    if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
118      PyErr_SetString (PyExc_ValueError, "input array is a scalar");
119      goto fail;
120    } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
121      PyErr_SetString (PyExc_ValueError,
122          "input array has more than two dimensions");
123      goto fail;
124    }
125
126    if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
127      PyErr_SetString (PyExc_ValueError, "input array should be float");
128      goto fail;
129    } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
130      PyErr_SetString (PyExc_ValueError, "input array should be float32");
131      goto fail;
132    } else {
133      // input data type is float32, nothing else to do
134      array = input;
135    }
136
137    // no need to really allocate fvec, just its struct member
138    mat = (fmat_t *)malloc(sizeof(fmat_t));
[3194dca1]139    long length = PyArray_DIM ((PyArrayObject *)array, 1);
140    if (length > 0) {
141      mat->length = (uint_t)length;
142    } else {
143      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
144      goto fail;
145    }
146    long height = PyArray_DIM ((PyArrayObject *)array, 0);
147    if (height > 0) {
148      mat->height = (uint_t)height;
149    } else {
150      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
151      goto fail;
152    }
[93004d5]153    mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height);
154    for (i=0; i< mat->height; i++) {
155      mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i);
156    }
157
158  } else if (PyObject_TypeCheck (input, &PyList_Type)) {
159    PyErr_SetString (PyExc_ValueError, "can not convert list to fmat");
160    return NULL;
161  } else {
162    PyErr_SetString (PyExc_ValueError, "can only accept matrix of float as input");
163    return NULL;
164  }
165
166  return mat;
167
168fail:
[7395ec5]169  return NULL;
170}
171
Note: See TracBrowser for help on using the repository browser.