[5652a8c] | 1 | #include "aubio-types.h" |
---|
[d27634d] | 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; |
---|
[cfa46b9] | 11 | uint_t duration; |
---|
[1ee5e21] | 12 | PyObject *read_to; |
---|
| 13 | fvec_t c_read_to; |
---|
| 14 | PyObject *mread_to; |
---|
| 15 | fmat_t c_mread_to; |
---|
[d27634d] | 16 | } Py_source; |
---|
| 17 | |
---|
[a79ec76] | 18 | static char Py_source_doc[] = "" |
---|
| 19 | " __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n" |
---|
| 20 | "\n" |
---|
| 21 | " Create a new source, opening the given path for reading.\n" |
---|
| 22 | "\n" |
---|
| 23 | " Examples\n" |
---|
| 24 | " --------\n" |
---|
| 25 | "\n" |
---|
| 26 | " Create a new source, using the original samplerate, with hop_size = 512:\n" |
---|
| 27 | "\n" |
---|
| 28 | " >>> source('/tmp/t.wav')\n" |
---|
| 29 | "\n" |
---|
| 30 | " Create a new source, resampling the original to 8000Hz:\n" |
---|
| 31 | "\n" |
---|
| 32 | " >>> source('/tmp/t.wav', samplerate = 8000)\n" |
---|
| 33 | "\n" |
---|
| 34 | " Create a new source, resampling it at 32000Hz, hop_size = 32:\n" |
---|
| 35 | "\n" |
---|
| 36 | " >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n" |
---|
| 37 | "\n" |
---|
| 38 | " Create a new source, using its original samplerate:\n" |
---|
| 39 | "\n" |
---|
| 40 | " >>> source('/tmp/t.wav', samplerate = 0)\n" |
---|
| 41 | "\n" |
---|
| 42 | " __call__()\n" |
---|
| 43 | " vec, read = x() <==> vec, read = x.do()\n" |
---|
| 44 | "\n" |
---|
| 45 | " Read vector from source.\n" |
---|
| 46 | "\n" |
---|
| 47 | " See also\n" |
---|
| 48 | " --------\n" |
---|
| 49 | " aubio.source.do\n" |
---|
| 50 | "\n"; |
---|
| 51 | |
---|
| 52 | static char Py_source_get_samplerate_doc[] = "" |
---|
| 53 | "x.get_samplerate() -> source samplerate\n" |
---|
| 54 | "\n" |
---|
| 55 | "Get samplerate of source."; |
---|
| 56 | |
---|
| 57 | static char Py_source_get_channels_doc[] = "" |
---|
| 58 | "x.get_channels() -> number of channels\n" |
---|
| 59 | "\n" |
---|
| 60 | "Get number of channels in source."; |
---|
| 61 | |
---|
| 62 | static char Py_source_do_doc[] = "" |
---|
| 63 | "vec, read = x.do() <==> vec, read = x()\n" |
---|
| 64 | "\n" |
---|
| 65 | "Read monophonic vector from source."; |
---|
| 66 | |
---|
| 67 | static char Py_source_do_multi_doc[] = "" |
---|
| 68 | "mat, read = x.do_multi()\n" |
---|
| 69 | "\n" |
---|
| 70 | "Read polyphonic vector from source."; |
---|
| 71 | |
---|
| 72 | static char Py_source_close_doc[] = "" |
---|
| 73 | "x.close()\n" |
---|
| 74 | "\n" |
---|
| 75 | "Close this source now."; |
---|
[d27634d] | 76 | |
---|
[11b49d7] | 77 | static char Py_source_seek_doc[] = "" |
---|
| 78 | "x.seek(position)\n" |
---|
| 79 | "\n" |
---|
| 80 | "Seek to resampled frame position."; |
---|
| 81 | |
---|
[d27634d] | 82 | static PyObject * |
---|
| 83 | Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 84 | { |
---|
| 85 | Py_source *self; |
---|
| 86 | char_t* uri = NULL; |
---|
| 87 | uint_t samplerate = 0; |
---|
| 88 | uint_t hop_size = 0; |
---|
[24931d5] | 89 | uint_t channels = 0; |
---|
| 90 | static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL }; |
---|
[d27634d] | 91 | |
---|
[24931d5] | 92 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist, |
---|
| 93 | &uri, &samplerate, &hop_size, &channels)) { |
---|
[d27634d] | 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | self = (Py_source *) pytype->tp_alloc (pytype, 0); |
---|
| 98 | |
---|
| 99 | if (self == NULL) { |
---|
| 100 | return NULL; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | self->uri = "none"; |
---|
| 104 | if (uri != NULL) { |
---|
| 105 | self->uri = uri; |
---|
| 106 | } |
---|
| 107 | |
---|
[2f9af5d] | 108 | self->samplerate = 0; |
---|
[18e22f9] | 109 | if ((sint_t)samplerate > 0) { |
---|
[d27634d] | 110 | self->samplerate = samplerate; |
---|
[18e22f9] | 111 | } else if ((sint_t)samplerate < 0) { |
---|
| 112 | PyErr_SetString (PyExc_ValueError, |
---|
| 113 | "can not use negative value for samplerate"); |
---|
| 114 | return NULL; |
---|
[d27634d] | 115 | } |
---|
| 116 | |
---|
| 117 | self->hop_size = Py_default_vector_length / 2; |
---|
[18e22f9] | 118 | if ((sint_t)hop_size > 0) { |
---|
[d27634d] | 119 | self->hop_size = hop_size; |
---|
[18e22f9] | 120 | } else if ((sint_t)hop_size < 0) { |
---|
| 121 | PyErr_SetString (PyExc_ValueError, |
---|
| 122 | "can not use negative value for hop_size"); |
---|
| 123 | return NULL; |
---|
[d27634d] | 124 | } |
---|
| 125 | |
---|
[24931d5] | 126 | self->channels = 1; |
---|
| 127 | if ((sint_t)channels >= 0) { |
---|
| 128 | self->channels = channels; |
---|
| 129 | } else if ((sint_t)channels < 0) { |
---|
| 130 | PyErr_SetString (PyExc_ValueError, |
---|
| 131 | "can not use negative value for channels"); |
---|
| 132 | return NULL; |
---|
| 133 | } |
---|
| 134 | |
---|
[d27634d] | 135 | return (PyObject *) self; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | static int |
---|
| 139 | Py_source_init (Py_source * self, PyObject * args, PyObject * kwds) |
---|
| 140 | { |
---|
| 141 | self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size ); |
---|
| 142 | if (self->o == NULL) { |
---|
[24931d5] | 143 | char_t errstr[30 + strlen(self->uri)]; |
---|
| 144 | sprintf(errstr, "error creating source with %s", self->uri); |
---|
[ac7e49b] | 145 | PyErr_SetString (PyExc_RuntimeError, errstr); |
---|
[d27634d] | 146 | return -1; |
---|
| 147 | } |
---|
| 148 | self->samplerate = aubio_source_get_samplerate ( self->o ); |
---|
[24931d5] | 149 | if (self->channels == 0) { |
---|
| 150 | self->channels = aubio_source_get_channels ( self->o ); |
---|
| 151 | } |
---|
[cfa46b9] | 152 | self->duration = aubio_source_get_duration ( self->o ); |
---|
[d27634d] | 153 | |
---|
[1ee5e21] | 154 | self->read_to = new_py_fvec(self->hop_size); |
---|
| 155 | self->mread_to = new_py_fmat(self->channels, self->hop_size); |
---|
[a28dab6] | 156 | |
---|
[d27634d] | 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
[a28dab6] | 160 | static void |
---|
| 161 | Py_source_del (Py_source *self, PyObject *unused) |
---|
| 162 | { |
---|
| 163 | del_aubio_source(self->o); |
---|
[1ee5e21] | 164 | //del_fvec(self->read_to); |
---|
| 165 | Py_XDECREF(self->read_to); |
---|
| 166 | Py_XDECREF(self->mread_to); |
---|
[5c1200a] | 167 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[a28dab6] | 168 | } |
---|
| 169 | |
---|
[d27634d] | 170 | |
---|
| 171 | /* function Py_source_do */ |
---|
[1164bcdf] | 172 | static PyObject * |
---|
[d27634d] | 173 | Py_source_do(Py_source * self, PyObject * args) |
---|
| 174 | { |
---|
| 175 | uint_t read; |
---|
| 176 | read = 0; |
---|
| 177 | |
---|
[1ee5e21] | 178 | Py_INCREF(self->read_to); |
---|
| 179 | if (!PyAubio_ArrayToCFvec(self->read_to, &(self->c_read_to))) { |
---|
| 180 | return NULL; |
---|
| 181 | } |
---|
[d27634d] | 182 | /* compute _do function */ |
---|
[1ee5e21] | 183 | aubio_source_do (self->o, &(self->c_read_to), &read); |
---|
[d27634d] | 184 | |
---|
[26eb6d0] | 185 | PyObject *outputs = PyTuple_New(2); |
---|
[1ee5e21] | 186 | PyTuple_SetItem( outputs, 0, self->read_to ); |
---|
[26eb6d0] | 187 | PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read)); |
---|
[d27634d] | 188 | return outputs; |
---|
| 189 | } |
---|
| 190 | |
---|
[1164bcdf] | 191 | /* function Py_source_do_multi */ |
---|
| 192 | static PyObject * |
---|
| 193 | Py_source_do_multi(Py_source * self, PyObject * args) |
---|
| 194 | { |
---|
| 195 | uint_t read; |
---|
| 196 | read = 0; |
---|
| 197 | |
---|
[1ee5e21] | 198 | Py_INCREF(self->mread_to); |
---|
| 199 | if (!PyAubio_ArrayToCFmat(self->mread_to, &(self->c_mread_to))) { |
---|
| 200 | return NULL; |
---|
| 201 | } |
---|
[1164bcdf] | 202 | /* compute _do function */ |
---|
[1ee5e21] | 203 | aubio_source_do_multi (self->o, &(self->c_mread_to), &read); |
---|
[1164bcdf] | 204 | |
---|
[26eb6d0] | 205 | PyObject *outputs = PyTuple_New(2); |
---|
[1ee5e21] | 206 | PyTuple_SetItem( outputs, 0, self->mread_to); |
---|
[26eb6d0] | 207 | PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read)); |
---|
[1164bcdf] | 208 | return outputs; |
---|
| 209 | } |
---|
| 210 | |
---|
[5652a8c] | 211 | static PyMemberDef Py_source_members[] = { |
---|
[a79ec76] | 212 | {"uri", T_STRING, offsetof (Py_source, uri), READONLY, |
---|
| 213 | "path at which the source was created"}, |
---|
| 214 | {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY, |
---|
| 215 | "samplerate at which the source is viewed"}, |
---|
| 216 | {"channels", T_INT, offsetof (Py_source, channels), READONLY, |
---|
| 217 | "number of channels found in the source"}, |
---|
| 218 | {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY, |
---|
| 219 | "number of consecutive frames that will be read at each do or do_multi call"}, |
---|
[cfa46b9] | 220 | {"duration", T_INT, offsetof (Py_source, duration), READONLY, |
---|
| 221 | "total number of frames in the source (estimated)"}, |
---|
[5652a8c] | 222 | { NULL } // sentinel |
---|
| 223 | }; |
---|
[d27634d] | 224 | |
---|
| 225 | static PyObject * |
---|
| 226 | Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused) |
---|
| 227 | { |
---|
| 228 | uint_t tmp = aubio_source_get_samplerate (self->o); |
---|
[5c1200a] | 229 | return (PyObject *)PyLong_FromLong (tmp); |
---|
[d27634d] | 230 | } |
---|
| 231 | |
---|
| 232 | static PyObject * |
---|
| 233 | Pyaubio_source_get_channels (Py_source *self, PyObject *unused) |
---|
| 234 | { |
---|
| 235 | uint_t tmp = aubio_source_get_channels (self->o); |
---|
[5c1200a] | 236 | return (PyObject *)PyLong_FromLong (tmp); |
---|
[d27634d] | 237 | } |
---|
| 238 | |
---|
[7b56229] | 239 | static PyObject * |
---|
| 240 | Pyaubio_source_close (Py_source *self, PyObject *unused) |
---|
| 241 | { |
---|
[3cc3fd8] | 242 | aubio_source_close (self->o); |
---|
[7b56229] | 243 | Py_RETURN_NONE; |
---|
| 244 | } |
---|
| 245 | |
---|
[11b49d7] | 246 | static PyObject * |
---|
| 247 | Pyaubio_source_seek (Py_source *self, PyObject *args) |
---|
| 248 | { |
---|
| 249 | uint_t err = 0; |
---|
| 250 | |
---|
| 251 | uint_t position; |
---|
| 252 | if (!PyArg_ParseTuple (args, "I", &position)) { |
---|
| 253 | return NULL; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | err = aubio_source_seek(self->o, position); |
---|
| 257 | if (err != 0) { |
---|
| 258 | PyErr_SetString (PyExc_ValueError, |
---|
| 259 | "error when seeking in source"); |
---|
| 260 | return NULL; |
---|
| 261 | } |
---|
| 262 | Py_RETURN_NONE; |
---|
| 263 | } |
---|
| 264 | |
---|
[d27634d] | 265 | static PyMethodDef Py_source_methods[] = { |
---|
| 266 | {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate, |
---|
[a79ec76] | 267 | METH_NOARGS, Py_source_get_samplerate_doc}, |
---|
[d27634d] | 268 | {"get_channels", (PyCFunction) Pyaubio_source_get_channels, |
---|
[a79ec76] | 269 | METH_NOARGS, Py_source_get_channels_doc}, |
---|
| 270 | {"do", (PyCFunction) Py_source_do, |
---|
| 271 | METH_NOARGS, Py_source_do_doc}, |
---|
[1164bcdf] | 272 | {"do_multi", (PyCFunction) Py_source_do_multi, |
---|
[a79ec76] | 273 | METH_NOARGS, Py_source_do_multi_doc}, |
---|
[7b56229] | 274 | {"close", (PyCFunction) Pyaubio_source_close, |
---|
[a79ec76] | 275 | METH_NOARGS, Py_source_close_doc}, |
---|
[11b49d7] | 276 | {"seek", (PyCFunction) Pyaubio_source_seek, |
---|
| 277 | METH_VARARGS, Py_source_seek_doc}, |
---|
[d27634d] | 278 | {NULL} /* sentinel */ |
---|
| 279 | }; |
---|
| 280 | |
---|
[5652a8c] | 281 | PyTypeObject Py_sourceType = { |
---|
| 282 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 283 | "aubio.source", |
---|
| 284 | sizeof (Py_source), |
---|
| 285 | 0, |
---|
| 286 | (destructor) Py_source_del, |
---|
| 287 | 0, |
---|
| 288 | 0, |
---|
| 289 | 0, |
---|
| 290 | 0, |
---|
| 291 | 0, |
---|
| 292 | 0, |
---|
| 293 | 0, |
---|
| 294 | 0, |
---|
| 295 | 0, |
---|
| 296 | (ternaryfunc)Py_source_do, |
---|
| 297 | 0, |
---|
| 298 | 0, |
---|
| 299 | 0, |
---|
| 300 | 0, |
---|
| 301 | Py_TPFLAGS_DEFAULT, |
---|
| 302 | Py_source_doc, |
---|
| 303 | 0, |
---|
| 304 | 0, |
---|
| 305 | 0, |
---|
| 306 | 0, |
---|
| 307 | 0, |
---|
| 308 | 0, |
---|
| 309 | Py_source_methods, |
---|
| 310 | Py_source_members, |
---|
| 311 | 0, |
---|
| 312 | 0, |
---|
| 313 | 0, |
---|
| 314 | 0, |
---|
| 315 | 0, |
---|
| 316 | 0, |
---|
| 317 | (initproc) Py_source_init, |
---|
| 318 | 0, |
---|
| 319 | Py_source_new, |
---|
| 320 | }; |
---|