From d27634db13fcc48b84ec2dd08018af7c105e9cba Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 10 Dec 2013 16:13:49 -0500 Subject: [PATCH] python/ext/py-source.c: not generated, modified init to get samplerate from aubio_source_t Signed-off-by: Paul Brossier --- python/ext/aubio-types.h | 4 +- python/ext/aubiomodule.c | 3 ++ python/ext/py-source.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++ python/lib/generator.py | 1 + python/setup.py | 1 + 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 python/ext/py-source.c diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h index 8c5546b0..e8afc8d7 100644 --- a/python/ext/aubio-types.h +++ b/python/ext/aubio-types.h @@ -30,7 +30,7 @@ #define Py_default_vector_length 1024 -#define Py_aubio_default_samplerate 44100 +#define Py_aubio_default_samplerate 0 #if HAVE_AUBIO_DOUBLE #error "Ouch! Python interface for aubio has not been much tested yet." @@ -68,3 +68,5 @@ extern PyTypeObject Py_fftType; extern PyTypeObject Py_pvocType; +extern PyTypeObject Py_sourceType; + diff --git a/python/ext/aubiomodule.c b/python/ext/aubiomodule.c index e311d886..29610697 100644 --- a/python/ext/aubiomodule.c +++ b/python/ext/aubiomodule.c @@ -198,6 +198,7 @@ init_aubio (void) || (PyType_Ready (&Py_filterbankType) < 0) || (PyType_Ready (&Py_fftType) < 0) || (PyType_Ready (&Py_pvocType) < 0) + || (PyType_Ready (&Py_sourceType) < 0) // generated objects || (generated_types_ready() < 0 ) ) { @@ -226,6 +227,8 @@ init_aubio (void) PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType); Py_INCREF (&Py_pvocType); PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType); + Py_INCREF (&Py_sourceType); + PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType); // add generated objects add_generated_objects(m); diff --git a/python/ext/py-source.c b/python/ext/py-source.c new file mode 100644 index 00000000..33f6a16b --- /dev/null +++ b/python/ext/py-source.c @@ -0,0 +1,137 @@ +// WARNING: this file is generated, DO NOT EDIT + +// WARNING: if you haven't read the first line yet, please do so +#include "aubiowraphell.h" + +typedef struct +{ + PyObject_HEAD + aubio_source_t * o; + char_t* uri; + uint_t samplerate; + uint_t hop_size; +} Py_source; + +static char Py_source_doc[] = "source object"; + +static PyObject * +Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) +{ + Py_source *self; + char_t* uri = NULL; + uint_t samplerate = 0; + uint_t hop_size = 0; + static char *kwlist[] = { "uri", "samplerate", "hop_size", NULL }; + + if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist, + &uri, &samplerate, &hop_size)) { + return NULL; + } + + self = (Py_source *) pytype->tp_alloc (pytype, 0); + + if (self == NULL) { + return NULL; + } + + self->uri = "none"; + if (uri != NULL) { + self->uri = uri; + } + + self->samplerate = Py_aubio_default_samplerate; + if (samplerate > 0) { + self->samplerate = samplerate; + //} else if (samplerate < 0) { + // PyErr_SetString (PyExc_ValueError, + // "can not use negative value for samplerate"); + // return NULL; + } + + self->hop_size = Py_default_vector_length / 2; + if (hop_size > 0) { + self->hop_size = hop_size; + //} else if (hop_size < 0) { + // PyErr_SetString (PyExc_ValueError, + // "can not use negative value for hop_size"); + // return NULL; + } + + return (PyObject *) self; +} + +static int +Py_source_init (Py_source * self, PyObject * args, PyObject * kwds) +{ + self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size ); + if (self->o == NULL) { + PyErr_SetString (PyExc_StandardError, "error creating object"); + return -1; + } + self->samplerate = aubio_source_get_samplerate ( self->o ); + + return 0; +} + +AUBIO_DEL(source) + +/* function Py_source_do */ +static PyObject * +Py_source_do(Py_source * self, PyObject * args) +{ + + + /* output vectors prototypes */ + fvec_t* read_to; + uint_t read; + + + + + + + /* creating output read_to as a new_fvec of length self->hop_size */ + read_to = new_fvec (self->hop_size); + read = 0; + + + /* compute _do function */ + aubio_source_do (self->o, read_to, &read); + + PyObject *outputs = PyList_New(0); + PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to)); + //del_fvec (read_to); + PyList_Append( outputs, (PyObject *)PyInt_FromLong (read)); + return outputs; +} + +AUBIO_MEMBERS_START(source) + {"uri", T_STRING, offsetof (Py_source, uri), READONLY, ""}, + {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY, ""}, + {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY, ""}, +AUBIO_MEMBERS_STOP(source) + + +static PyObject * +Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused) +{ + uint_t tmp = aubio_source_get_samplerate (self->o); + return (PyObject *)PyInt_FromLong (tmp); +} + +static PyObject * +Pyaubio_source_get_channels (Py_source *self, PyObject *unused) +{ + uint_t tmp = aubio_source_get_channels (self->o); + return (PyObject *)PyInt_FromLong (tmp); +} + +static PyMethodDef Py_source_methods[] = { + {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate, + METH_NOARGS, ""}, + {"get_channels", (PyCFunction) Pyaubio_source_get_channels, + METH_NOARGS, ""}, + {NULL} /* sentinel */ +}; + +AUBIO_TYPEOBJECT(source, "aubio.source") diff --git a/python/lib/generator.py b/python/lib/generator.py index efb42a0f..cac7d9aa 100755 --- a/python/lib/generator.py +++ b/python/lib/generator.py @@ -56,6 +56,7 @@ def generate_object_files(output_path): 'pitchyinfft', 'sink_apple_audio', 'sink_sndfile', + 'source', 'source_apple_audio', 'source_sndfile', 'source_avcodec', diff --git a/python/setup.py b/python/setup.py index abf73ba9..44380afb 100755 --- a/python/setup.py +++ b/python/setup.py @@ -53,6 +53,7 @@ aubio_extension = Extension("aubio._aubio", [ "ext/py-filterbank.c", "ext/py-fft.c", "ext/py-phasevoc.c", + "ext/py-source.c", # generated files ] + generated_object_files, include_dirs = include_dirs, -- 2.11.0