[d27634d] | 1 | #include "aubiowraphell.h" |
---|
| 2 | |
---|
| 3 | typedef struct |
---|
| 4 | { |
---|
| 5 | PyObject_HEAD |
---|
| 6 | aubio_source_t * o; |
---|
| 7 | char_t* uri; |
---|
| 8 | uint_t samplerate; |
---|
[1164bcdf] | 9 | uint_t channels; |
---|
[d27634d] | 10 | uint_t hop_size; |
---|
| 11 | } Py_source; |
---|
| 12 | |
---|
| 13 | static char Py_source_doc[] = "source object"; |
---|
| 14 | |
---|
| 15 | static PyObject * |
---|
| 16 | Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 17 | { |
---|
| 18 | Py_source *self; |
---|
| 19 | char_t* uri = NULL; |
---|
| 20 | uint_t samplerate = 0; |
---|
| 21 | uint_t hop_size = 0; |
---|
| 22 | static char *kwlist[] = { "uri", "samplerate", "hop_size", NULL }; |
---|
| 23 | |
---|
| 24 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist, |
---|
| 25 | &uri, &samplerate, &hop_size)) { |
---|
| 26 | return NULL; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | self = (Py_source *) pytype->tp_alloc (pytype, 0); |
---|
| 30 | |
---|
| 31 | if (self == NULL) { |
---|
| 32 | return NULL; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | self->uri = "none"; |
---|
| 36 | if (uri != NULL) { |
---|
| 37 | self->uri = uri; |
---|
| 38 | } |
---|
| 39 | |
---|
[2f9af5d] | 40 | self->samplerate = 0; |
---|
[18e22f9] | 41 | if ((sint_t)samplerate > 0) { |
---|
[d27634d] | 42 | self->samplerate = samplerate; |
---|
[18e22f9] | 43 | } else if ((sint_t)samplerate < 0) { |
---|
| 44 | PyErr_SetString (PyExc_ValueError, |
---|
| 45 | "can not use negative value for samplerate"); |
---|
| 46 | return NULL; |
---|
[d27634d] | 47 | } |
---|
| 48 | |
---|
| 49 | self->hop_size = Py_default_vector_length / 2; |
---|
[18e22f9] | 50 | if ((sint_t)hop_size > 0) { |
---|
[d27634d] | 51 | self->hop_size = hop_size; |
---|
[18e22f9] | 52 | } else if ((sint_t)hop_size < 0) { |
---|
| 53 | PyErr_SetString (PyExc_ValueError, |
---|
| 54 | "can not use negative value for hop_size"); |
---|
| 55 | return NULL; |
---|
[d27634d] | 56 | } |
---|
| 57 | |
---|
| 58 | return (PyObject *) self; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | static int |
---|
| 62 | Py_source_init (Py_source * self, PyObject * args, PyObject * kwds) |
---|
| 63 | { |
---|
| 64 | self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size ); |
---|
| 65 | if (self->o == NULL) { |
---|
| 66 | PyErr_SetString (PyExc_StandardError, "error creating object"); |
---|
| 67 | return -1; |
---|
| 68 | } |
---|
| 69 | self->samplerate = aubio_source_get_samplerate ( self->o ); |
---|
[1164bcdf] | 70 | self->channels = aubio_source_get_channels ( self->o ); |
---|
[d27634d] | 71 | |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | AUBIO_DEL(source) |
---|
| 76 | |
---|
| 77 | /* function Py_source_do */ |
---|
[1164bcdf] | 78 | static PyObject * |
---|
[d27634d] | 79 | Py_source_do(Py_source * self, PyObject * args) |
---|
| 80 | { |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | /* output vectors prototypes */ |
---|
| 84 | fvec_t* read_to; |
---|
| 85 | uint_t read; |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
[1164bcdf] | 91 | |
---|
[d27634d] | 92 | /* creating output read_to as a new_fvec of length self->hop_size */ |
---|
| 93 | read_to = new_fvec (self->hop_size); |
---|
| 94 | read = 0; |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /* compute _do function */ |
---|
| 98 | aubio_source_do (self->o, read_to, &read); |
---|
| 99 | |
---|
| 100 | PyObject *outputs = PyList_New(0); |
---|
| 101 | PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to)); |
---|
| 102 | //del_fvec (read_to); |
---|
| 103 | PyList_Append( outputs, (PyObject *)PyInt_FromLong (read)); |
---|
| 104 | return outputs; |
---|
| 105 | } |
---|
| 106 | |
---|
[1164bcdf] | 107 | /* function Py_source_do_multi */ |
---|
| 108 | static PyObject * |
---|
| 109 | Py_source_do_multi(Py_source * self, PyObject * args) |
---|
| 110 | { |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | /* output vectors prototypes */ |
---|
| 114 | fmat_t* read_to; |
---|
| 115 | uint_t read; |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | /* creating output read_to as a new_fvec of length self->hop_size */ |
---|
| 123 | read_to = new_fmat (self->channels, self->hop_size); |
---|
| 124 | read = 0; |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | /* compute _do function */ |
---|
| 128 | aubio_source_do_multi (self->o, read_to, &read); |
---|
| 129 | |
---|
| 130 | PyObject *outputs = PyList_New(0); |
---|
| 131 | PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to)); |
---|
| 132 | //del_fvec (read_to); |
---|
| 133 | PyList_Append( outputs, (PyObject *)PyInt_FromLong (read)); |
---|
| 134 | return outputs; |
---|
| 135 | } |
---|
| 136 | |
---|
[d27634d] | 137 | AUBIO_MEMBERS_START(source) |
---|
| 138 | {"uri", T_STRING, offsetof (Py_source, uri), READONLY, ""}, |
---|
| 139 | {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY, ""}, |
---|
[1164bcdf] | 140 | {"channels", T_INT, offsetof (Py_source, channels), READONLY, ""}, |
---|
[d27634d] | 141 | {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY, ""}, |
---|
| 142 | AUBIO_MEMBERS_STOP(source) |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | static PyObject * |
---|
| 146 | Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused) |
---|
| 147 | { |
---|
| 148 | uint_t tmp = aubio_source_get_samplerate (self->o); |
---|
| 149 | return (PyObject *)PyInt_FromLong (tmp); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static PyObject * |
---|
| 153 | Pyaubio_source_get_channels (Py_source *self, PyObject *unused) |
---|
| 154 | { |
---|
| 155 | uint_t tmp = aubio_source_get_channels (self->o); |
---|
| 156 | return (PyObject *)PyInt_FromLong (tmp); |
---|
| 157 | } |
---|
| 158 | |
---|
[7b56229] | 159 | static PyObject * |
---|
| 160 | Pyaubio_source_close (Py_source *self, PyObject *unused) |
---|
| 161 | { |
---|
[3cc3fd8] | 162 | aubio_source_close (self->o); |
---|
[7b56229] | 163 | Py_RETURN_NONE; |
---|
| 164 | } |
---|
| 165 | |
---|
[d27634d] | 166 | static PyMethodDef Py_source_methods[] = { |
---|
| 167 | {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate, |
---|
| 168 | METH_NOARGS, ""}, |
---|
| 169 | {"get_channels", (PyCFunction) Pyaubio_source_get_channels, |
---|
| 170 | METH_NOARGS, ""}, |
---|
[1164bcdf] | 171 | {"do_multi", (PyCFunction) Py_source_do_multi, |
---|
| 172 | METH_NOARGS, ""}, |
---|
[7b56229] | 173 | {"close", (PyCFunction) Pyaubio_source_close, |
---|
| 174 | METH_NOARGS, ""}, |
---|
[d27634d] | 175 | {NULL} /* sentinel */ |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | AUBIO_TYPEOBJECT(source, "aubio.source") |
---|