Changeset 615ac7d for interfaces/python


Ignore:
Timestamp:
Oct 19, 2009, 3:08:43 PM (15 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
ec1ce52
Parents:
3f99693
Message:

.

Location:
interfaces/python
Files:
3 added
7 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/aubio-types.h

    r3f99693 r615ac7d  
    1010#define Py_aubio_default_samplerate 44100
    1111
    12 #ifdef HAVE_AUBIO_DOUBLE
    13 #define AUBIO_FLOAT NPY_FLOAT
     12#if HAVE_AUBIO_DOUBLE
     13#error "Ouch! Python interface for aubio has not been much tested yet."
     14#define AUBIO_NPY_SMPL NPY_DOUBLE
    1415#else
    15 #define AUBIO_FLOAT NPY_LONG
     16#define AUBIO_NPY_SMPL NPY_FLOAT
    1617#endif
    1718
     
    3334} Py_fvec;
    3435extern PyTypeObject Py_fvecType;
     36extern PyObject *PyAubio_FvecToArray (Py_fvec * self);
     37extern Py_fvec *PyAubio_ArrayToFvec (PyObject * self);
    3538
     39typedef struct
     40{
     41  PyObject_HEAD
     42  cvec_t * o;
     43  uint_t length;
     44  uint_t channels;
     45} Py_cvec;
    3646extern PyTypeObject Py_cvecType;
     47extern PyObject *PyAubio_CvecToArray (Py_cvec * self);
     48extern Py_cvec *PyAubio_ArrayToCvec (PyObject * self);
    3749
    3850extern PyTypeObject Py_filterType;
    3951
    40 extern PyObject *PyAubio_FvecToArray (Py_fvec * self);
     52extern PyTypeObject Py_filterbankType;
    4153
    42 extern Py_fvec *PyAubio_ArrayToFvec (PyObject * self);
     54extern PyTypeObject Py_fftType;
     55
     56extern PyTypeObject Py_pvocType;
  • interfaces/python/aubiomodule.c

    r3f99693 r615ac7d  
    9494  // compute the function
    9595  fvec_min_removal (vec->o);
     96
    9697  // since this function does not return, we could return None
    9798  //return Py_None;
     
    119120  int err;
    120121
    121   if ((PyType_Ready (&Py_fvecType) < 0) ||
    122       (PyType_Ready (&Py_cvecType) < 0) ||
    123       (PyType_Ready (&Py_filterType) < 0)) {
     122  if ((PyType_Ready (&Py_fvecType) < 0)
     123      || (PyType_Ready (&Py_cvecType) < 0)
     124      || (PyType_Ready (&Py_filterType) < 0)
     125      || (PyType_Ready (&Py_filterbankType) < 0)
     126      || (PyType_Ready (&Py_fftType) < 0)
     127      || (PyType_Ready (&Py_pvocType) < 0)
     128  ) {
    124129    return;
    125130  }
     
    144149  Py_INCREF (&Py_filterType);
    145150  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
     151  Py_INCREF (&Py_filterbankType);
     152  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
     153  Py_INCREF (&Py_fftType);
     154  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
     155  Py_INCREF (&Py_pvocType);
     156  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
    146157}
  • interfaces/python/py-cvec.c

    r3f99693 r615ac7d  
    99        self.norm = array(length, channels)
    1010        self.phas = array(length, channels)
    11 
    1211*/
    1312
    14 typedef struct
    15 {
    16   PyObject_HEAD
    17   cvec_t * o;
    18   uint_t length;
    19   uint_t channels;
    20 } Py_cvec;
    2113
    2214static char Py_cvec_doc[] = "cvec object";
     
    9789    goto fail;
    9890  }
    99   cvec_print ( self->o );
     91  //cvec_print ( self->o );
    10092
    10193  result = PyString_Format (format, args);
     
    133125      goto fail;
    134126#if AUBIO_DO_CASTING
    135     } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
     127    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    136128      // input data type is not float32, casting
    137       array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT);
     129      array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);
    138130      if (array == NULL) {
    139131        PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
     
    141133      }
    142134#else
    143     } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
     135    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    144136      PyErr_SetString (PyExc_ValueError, "input array should be float32");
    145137      goto fail;
     
    153145    vec = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
    154146    if (PyArray_NDIM (array) == 1) {
     147      PyErr_SetString (PyExc_ValueError,
     148          "input array should be have at least two rows for norm and phas");
     149      goto fail;
     150    } else if (PyArray_NDIM (array) == 2) {
    155151      vec->channels = 1;
    156152      vec->length = PyArray_SIZE (array);
    157153    } else {
    158       vec->channels = PyArray_DIM (array, 0);
     154      vec->channels = PyArray_DIM (array, 0) / 2;
    159155      vec->length = PyArray_DIM (array, 1);
    160156    }
     
    167163    vec->o->phas = (smpl_t**)malloc(vec->o->channels * sizeof(smpl_t*));
    168164    // hat data[i] point to array line
    169     for (i = 0; i < vec->channels; i++) {
     165    for (i = 0; i < vec->channels; i+=2) {
    170166      vec->o->norm[i] = (smpl_t *) PyArray_GETPTR1 (array, i);
     167      vec->o->phas[i] = (smpl_t *) PyArray_GETPTR1 (array, i+1);
    171168    }
    172169
     
    186183{
    187184  PyObject *array = NULL;
    188   if (self->channels == 1) {
    189     npy_intp dims[] = { self->length, 1 };
    190     array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[0]);
    191   } else {
    192     uint_t i;
    193     npy_intp dims[] = { self->length, 1 };
    194     PyObject *concat = PyList_New (0), *tmp = NULL;
    195     for (i = 0; i < self->channels; i++) {
    196       tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);
    197       PyList_Append (concat, tmp);
    198       Py_DECREF (tmp);
    199     }
    200     array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
    201     Py_DECREF (concat);
    202   }
     185  uint_t i;
     186  npy_intp dims[] = { self->o->length, 1 };
     187  PyObject *concat = PyList_New (0), *tmp = NULL;
     188  for (i = 0; i < self->channels; i++) {
     189    tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);
     190    PyList_Append (concat, tmp);
     191    Py_DECREF (tmp);
     192    tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);
     193    PyList_Append (concat, tmp);
     194    Py_DECREF (tmp);
     195  }
     196  array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
     197  Py_DECREF (concat);
    203198  return array;
     199}
     200
     201PyObject *
     202PyAubio_CvecNormToArray (Py_cvec * self)
     203{
     204  PyObject *array = NULL;
     205  uint_t i;
     206  npy_intp dims[] = { self->o->length, 1 };
     207  PyObject *concat = PyList_New (0), *tmp = NULL;
     208  for (i = 0; i < self->channels; i++) {
     209    tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->norm[i]);
     210    PyList_Append (concat, tmp);
     211    Py_DECREF (tmp);
     212  }
     213  array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
     214  Py_DECREF (concat);
     215  return array;
     216}
     217
     218PyObject *
     219PyAubio_ArrayToCvecNorm (PyObject * self)
     220{
     221  return NULL;
     222}
     223
     224PyObject *
     225PyAubio_CvecPhasToArray (Py_cvec * self)
     226{
     227  PyObject *array = NULL;
     228  uint_t i;
     229  npy_intp dims[] = { self->o->length, 1 };
     230  PyObject *concat = PyList_New (0), *tmp = NULL;
     231  for (i = 0; i < self->channels; i++) {
     232    tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->phas[i]);
     233    PyList_Append (concat, tmp);
     234    Py_DECREF (tmp);
     235  }
     236  array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
     237  Py_DECREF (concat);
     238  return array;
     239}
     240
     241PyObject *
     242PyAubio_ArrayToCvecPhas (PyObject * self)
     243{
     244  return NULL;
     245}
     246
     247PyObject *
     248Py_cvec_get_norm (Py_cvec * self, void *closure)
     249{
     250  return PyAubio_CvecNormToArray(self);
     251}
     252
     253PyObject *
     254Py_cvec_get_phas (Py_cvec * self, void *closure)
     255{
     256  return PyAubio_CvecPhasToArray(self);
     257}
     258
     259static int
     260Py_cvec_set_norm (Py_cvec * self, PyObject *value, void * closure)
     261{
     262  return 0;
     263}
     264
     265static int
     266Py_cvec_set_phas (Py_cvec * self, PyObject *value, void * closure)
     267{
     268  return 0;
    204269}
    205270
     
    270335
    271336static PyMethodDef Py_cvec_methods[] = {
    272   {"__array__", (PyCFunction) PyAubio_FvecToArray, METH_NOARGS,
    273       "Returns the first channel as a numpy array."},
     337  {"__array__", (PyCFunction) PyAubio_CvecToArray, METH_NOARGS,
     338      "Returns the content of this cvec as a numpy array"},
     339/*
     340  {"norm", (PyCFunction) PyAubio_CvecNormToArray, METH_NOARGS,
     341      "Returns the content of the magnitude of this cvec as a numpy array."},
     342  {"phas", (PyCFunction) PyAubio_CvecPhasToArray, METH_NOARGS,
     343      "Returns the content of the phase of this cvec as a numpy array."},
     344*/
    274345  {NULL}
     346};
     347
     348static PyGetSetDef Py_cvec_getseters[] = {
     349  {"norm", (getter)Py_cvec_get_norm, (setter)Py_cvec_set_norm,
     350      "Content of the magnitude of this cvec",
     351      NULL},
     352  {"phas", (getter)Py_cvec_get_phas, (setter)Py_cvec_set_phas,
     353      "Content of the magnitude of this cvec",
     354      NULL},
     355  {NULL} /* sentinel */
    275356};
    276357
     
    320401  Py_cvec_methods,              /* tp_methods        */
    321402  Py_cvec_members,              /* tp_members        */
    322   0,                            /* tp_getset         */
     403  Py_cvec_getseters,            /* tp_getset         */
    323404  0,                            /* tp_base           */
    324405  0,                            /* tp_dict           */
  • interfaces/python/py-filter.c

    r3f99693 r615ac7d  
    55  PyObject_HEAD
    66  aubio_filter_t * o;
    7   uint_t samplerate;
    87  uint_t order;
    98  uint_t channels;
     
    1514Py_filter_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
    1615{
    17   int samplerate= 0, order= 0, channels = 0;
     16  int order= 0, channels = 0;
    1817  Py_filter *self;
    19   static char *kwlist[] = { "samplerate", "order", "channels", NULL };
     18  static char *kwlist[] = { "order", "channels", NULL };
    2019
    21   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|III", kwlist,
    22           &samplerate, &order, &channels)) {
     20  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
     21          &order, &channels)) {
    2322    return NULL;
    2423  }
     
    2625  self = (Py_filter *) type->tp_alloc (type, 0);
    2726
    28   self->samplerate = Py_aubio_default_samplerate;
    29   self->order = 7;
    30   self->channels = Py_default_vector_channels;
    31 
    3227  if (self == NULL) {
    3328    return NULL;
    3429  }
    3530
    36   if (samplerate > 0) {
    37     self->samplerate = samplerate;
    38   } else if (samplerate < 0) {
    39     PyErr_SetString (PyExc_ValueError,
    40         "can not use negative samplerate");
    41     return NULL;
    42   }
     31  self->order = 7;
     32  self->channels = Py_default_vector_channels;
    4333
    4434  if (order > 0) {
     
    6454Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds)
    6555{
    66   self->o = new_aubio_filter (self->samplerate, self->order, self->channels);
     56  self->o = new_aubio_filter (self->order, self->channels);
    6757  if (self->o == NULL) {
    6858    return -1;
     
    113103
    114104static PyObject *
    115 Py_filter_set_c_weighting (Py_filter * self, PyObject *unused)
     105Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
    116106{
    117   uint_t err = aubio_filter_set_c_weighting (((Py_filter *)self)->o);
     107  uint_t err = 0;
     108  uint_t samplerate;
     109  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
     110    return NULL;
     111  }
     112
     113  err = aubio_filter_set_c_weighting (self->o, samplerate);
    118114  if (err > 0) {
    119115    PyErr_SetString (PyExc_ValueError,
    120         "error when setting filter to C-weighting");
     116        "error when setting filter to A-weighting");
    121117    return NULL;
    122118  }
     
    125121
    126122static PyObject *
    127 Py_filter_set_a_weighting (Py_filter * self, PyObject *unused)
     123Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
    128124{
    129   uint_t err = aubio_filter_set_a_weighting (((Py_filter *)self)->o);
     125  uint_t err = 0;
     126  uint_t samplerate;
     127  if (!PyArg_ParseTuple (args, "I", &samplerate)) {
     128    return NULL;
     129  }
     130
     131  err = aubio_filter_set_a_weighting (self->o, samplerate);
    130132  if (err > 0) {
    131133    PyErr_SetString (PyExc_ValueError,
     
    138140static PyMemberDef Py_filter_members[] = {
    139141  // TODO remove READONLY flag and define getter/setter
    140   {"samplerate", T_INT, offsetof (Py_filter, samplerate), READONLY,
    141       "sampling rate"},
    142142  {"order", T_INT, offsetof (Py_filter, order), READONLY,
    143143      "order of the filter"},
     
    148148
    149149static PyMethodDef Py_filter_methods[] = {
    150   {"do", (PyCFunction) Py_filter_do, METH_VARARGS,
    151       "filter input vector"},
    152150  {"set_c_weighting", (PyCFunction) Py_filter_set_c_weighting, METH_NOARGS,
    153151      "set filter coefficients to C-weighting"},
  • interfaces/python/py-fvec.c

    r3f99693 r615ac7d  
    128128      goto fail;
    129129#if AUBIO_DO_CASTING
    130     } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
     130    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    131131      // input data type is not float32, casting
    132       array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_FLOAT);
     132      array = PyArray_Cast ( (PyArrayObject*) input, AUBIO_NPY_SMPL);
    133133      if (array == NULL) {
    134         PyErr_SetString (PyExc_IndexError, "failed converting to NPY_FLOAT");
     134        PyErr_SetString (PyExc_IndexError, "failed converting to AUBIO_NPY_SMPL");
    135135        goto fail;
    136136      }
    137137#else
    138     } else if (PyArray_TYPE (input) != AUBIO_FLOAT) {
     138    } else if (PyArray_TYPE (input) != AUBIO_NPY_SMPL) {
    139139      PyErr_SetString (PyExc_ValueError, "input array should be float32");
    140140      goto fail;
     
    182182  if (self->channels == 1) {
    183183    npy_intp dims[] = { self->length, 1 };
    184     array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[0]);
     184    array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[0]);
    185185  } else {
    186186    uint_t i;
     
    188188    PyObject *concat = PyList_New (0), *tmp = NULL;
    189189    for (i = 0; i < self->channels; i++) {
    190       tmp = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[i]);
     190      tmp = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[i]);
    191191      PyList_Append (concat, tmp);
    192192      Py_DECREF (tmp);
    193193    }
    194     array = PyArray_FromObject (concat, NPY_FLOAT, 2, 2);
     194    array = PyArray_FromObject (concat, AUBIO_NPY_SMPL, 2, 2);
    195195    Py_DECREF (concat);
    196196  }
     
    215215
    216216  npy_intp dims[] = { self->length, 1 };
    217   array = PyArray_SimpleNewFromData (1, dims, NPY_FLOAT, self->o->data[index]);
     217  array = PyArray_SimpleNewFromData (1, dims, AUBIO_NPY_SMPL, self->o->data[index]);
    218218  return array;
    219219}
     
    229229  }
    230230
    231   array = PyArray_FROM_OT (o, NPY_FLOAT);
     231  array = PyArray_FROM_OT (o, AUBIO_NPY_SMPL);
    232232  if (array == NULL) {
    233233    PyErr_SetString (PyExc_ValueError, "should be an array of float");
  • interfaces/python/setup.py

    r3f99693 r615ac7d  
    11from distutils.core import setup, Extension
     2
     3from os import listdir
     4generated_files = listdir('generated')
     5generated_files = ['generated/'+f for f in generated_files]
    26
    37setup(name="_aubio", version="1.0",
    48      ext_modules = [
    59        Extension("_aubio",
    6             ["aubiomodule.c", "py-fvec.c", "py-cvec.c", "py-filter.c"],
    7             include_dirs=['../../build/default/src', '../../src' ],
     10            ["aubiomodule.c",
     11            "py-fvec.c",
     12            "py-cvec.c",
     13            "py-filter.c",
     14            # macroised
     15            "py-filterbank.c",
     16            "py-fft.c",
     17            "py-phasevoc.c",
     18            # generated files
     19            ] + generated_files,
     20            include_dirs=['../../build/default/src', '../../src', '.' ],
    821            library_dirs=['../../build/default/src', '../../src/.libs' ],
    922            libraries=['aubio'])])
  • interfaces/python/test_filter.py

    r3f99693 r615ac7d  
    1212  def test_members(self):
    1313    f = digital_filter()
    14     assert_equal ([f.channels, f.order, f.samplerate], [1, 7, 44100])
    15     f = digital_filter(48000, 5, 2)
    16     assert_equal ([f.channels, f.order, f.samplerate], [2, 5, 48000])
     14    assert_equal ([f.channels, f.order], [1, 7])
     15    f = digital_filter(5, 2)
     16    assert_equal ([f.channels, f.order], [2, 5])
    1717    f(fvec())
    1818 
     
    3333  def test_c_weighting(self):
    3434    expected = array_from_text_file('c_weighting_test_simple.expected')
    35     f = digital_filter(44100, 5, 1)
    36     f.set_c_weighting()
     35    f = digital_filter(5, 1)
     36    f.set_c_weighting(44100)
    3737    v = fvec(32)
    3838    v[0][12] = .5
     
    4242  def test_a_weighting(self):
    4343    expected = array_from_text_file('a_weighting_test_simple.expected')
    44     f = digital_filter(44100, 7, 1)
    45     f.set_a_weighting()
     44    f = digital_filter(7, 1)
     45    f.set_a_weighting(44100)
    4646    v = fvec(32)
    4747    v[0][12] = .5
     
    5151  def test_a_weighting_parted(self):
    5252    expected = array_from_text_file('a_weighting_test_simple.expected')
    53     f = digital_filter(44100, 7, 1)
    54     f.set_a_weighting()
     53    f = digital_filter(7, 1)
     54    f.set_a_weighting(44100)
    5555    v = fvec(16)
    5656    v[0][12] = .5
Note: See TracChangeset for help on using the changeset viewer.