source: python/ext/aubioproxy.c @ 135bc14

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 135bc14 was 911c22f, checked in by Paul Brossier <piem@piem.org>, 9 years ago

ext/aubioproxy.c: fix windows c89 compilation

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