source: python/ext/py-source.c @ 5e394ecc

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 5e394ecc was 11b49d7, checked in by Paul Brossier <piem@piem.org>, 10 years ago

python/ext/py-source.c: add seek, thanks @davebrent for the heads up

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[d27634d]1#include "aubiowraphell.h"
2
3typedef 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
[a79ec76]13static char Py_source_doc[] = ""
14"   __new__(path, samplerate = 0, hop_size = 512, channels = 1)\n"
15"\n"
16"       Create a new source, opening the given path for reading.\n"
17"\n"
18"       Examples\n"
19"       --------\n"
20"\n"
21"       Create a new source, using the original samplerate, with hop_size = 512:\n"
22"\n"
23"       >>> source('/tmp/t.wav')\n"
24"\n"
25"       Create a new source, resampling the original to 8000Hz:\n"
26"\n"
27"       >>> source('/tmp/t.wav', samplerate = 8000)\n"
28"\n"
29"       Create a new source, resampling it at 32000Hz, hop_size = 32:\n"
30"\n"
31"       >>> source('/tmp/t.wav', samplerate = 32000, hop_size = 32)\n"
32"\n"
33"       Create a new source, using its original samplerate:\n"
34"\n"
35"       >>> source('/tmp/t.wav', samplerate = 0)\n"
36"\n"
37"   __call__()\n"
38"       vec, read = x() <==> vec, read = x.do()\n"
39"\n"
40"       Read vector from source.\n"
41"\n"
42"       See also\n"
43"       --------\n"
44"       aubio.source.do\n"
45"\n";
46
47static char Py_source_get_samplerate_doc[] = ""
48"x.get_samplerate() -> source samplerate\n"
49"\n"
50"Get samplerate of source.";
51
52static char Py_source_get_channels_doc[] = ""
53"x.get_channels() -> number of channels\n"
54"\n"
55"Get number of channels in source.";
56
57static char Py_source_do_doc[] = ""
58"vec, read = x.do() <==> vec, read = x()\n"
59"\n"
60"Read monophonic vector from source.";
61
62static char Py_source_do_multi_doc[] = ""
63"mat, read = x.do_multi()\n"
64"\n"
65"Read polyphonic vector from source.";
66
67static char Py_source_close_doc[] = ""
68"x.close()\n"
69"\n"
70"Close this source now.";
[d27634d]71
[11b49d7]72static char Py_source_seek_doc[] = ""
73"x.seek(position)\n"
74"\n"
75"Seek to resampled frame position.";
76
[d27634d]77static PyObject *
78Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
79{
80  Py_source *self;
81  char_t* uri = NULL;
82  uint_t samplerate = 0;
83  uint_t hop_size = 0;
[24931d5]84  uint_t channels = 0;
85  static char *kwlist[] = { "uri", "samplerate", "hop_size", "channels", NULL };
[d27634d]86
[24931d5]87  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sIII", kwlist,
88          &uri, &samplerate, &hop_size, &channels)) {
[d27634d]89    return NULL;
90  }
91
92  self = (Py_source *) pytype->tp_alloc (pytype, 0);
93
94  if (self == NULL) {
95    return NULL;
96  }
97
98  self->uri = "none";
99  if (uri != NULL) {
100    self->uri = uri;
101  }
102
[2f9af5d]103  self->samplerate = 0;
[18e22f9]104  if ((sint_t)samplerate > 0) {
[d27634d]105    self->samplerate = samplerate;
[18e22f9]106  } else if ((sint_t)samplerate < 0) {
107    PyErr_SetString (PyExc_ValueError,
108        "can not use negative value for samplerate");
109    return NULL;
[d27634d]110  }
111
112  self->hop_size = Py_default_vector_length / 2;
[18e22f9]113  if ((sint_t)hop_size > 0) {
[d27634d]114    self->hop_size = hop_size;
[18e22f9]115  } else if ((sint_t)hop_size < 0) {
116    PyErr_SetString (PyExc_ValueError,
117        "can not use negative value for hop_size");
118    return NULL;
[d27634d]119  }
120
[24931d5]121  self->channels = 1;
122  if ((sint_t)channels >= 0) {
123    self->channels = channels;
124  } else if ((sint_t)channels < 0) {
125    PyErr_SetString (PyExc_ValueError,
126        "can not use negative value for channels");
127    return NULL;
128  }
129
[d27634d]130  return (PyObject *) self;
131}
132
133static int
134Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
135{
136  self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
137  if (self->o == NULL) {
[24931d5]138    char_t errstr[30 + strlen(self->uri)];
139    sprintf(errstr, "error creating source with %s", self->uri);
140    PyErr_SetString (PyExc_StandardError, errstr);
[d27634d]141    return -1;
142  }
143  self->samplerate = aubio_source_get_samplerate ( self->o );
[24931d5]144  if (self->channels == 0) {
145    self->channels = aubio_source_get_channels ( self->o );
146  }
[d27634d]147
148  return 0;
149}
150
151AUBIO_DEL(source)
152
153/* function Py_source_do */
[1164bcdf]154static PyObject *
[d27634d]155Py_source_do(Py_source * self, PyObject * args)
156{
157
158
159  /* output vectors prototypes */
160  fvec_t* read_to;
161  uint_t read;
162
163
164
165
166
[1164bcdf]167
[d27634d]168  /* creating output read_to as a new_fvec of length self->hop_size */
169  read_to = new_fvec (self->hop_size);
170  read = 0;
171
172
173  /* compute _do function */
174  aubio_source_do (self->o, read_to, &read);
175
176  PyObject *outputs = PyList_New(0);
177  PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
178  //del_fvec (read_to);
179  PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
180  return outputs;
181}
182
[1164bcdf]183/* function Py_source_do_multi */
184static PyObject *
185Py_source_do_multi(Py_source * self, PyObject * args)
186{
187
188
189  /* output vectors prototypes */
190  fmat_t* read_to;
191  uint_t read;
192
193
194
195
196
197
198  /* creating output read_to as a new_fvec of length self->hop_size */
199  read_to = new_fmat (self->channels, self->hop_size);
200  read = 0;
201
202
203  /* compute _do function */
204  aubio_source_do_multi (self->o, read_to, &read);
205
206  PyObject *outputs = PyList_New(0);
207  PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to));
208  //del_fvec (read_to);
209  PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
210  return outputs;
211}
212
[d27634d]213AUBIO_MEMBERS_START(source)
[a79ec76]214  {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
215    "path at which the source was created"},
216  {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
217    "samplerate at which the source is viewed"},
218  {"channels", T_INT, offsetof (Py_source, channels), READONLY,
219    "number of channels found in the source"},
220  {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
221    "number of consecutive frames that will be read at each do or do_multi call"},
[d27634d]222AUBIO_MEMBERS_STOP(source)
223
224
225static PyObject *
226Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
227{
228  uint_t tmp = aubio_source_get_samplerate (self->o);
229  return (PyObject *)PyInt_FromLong (tmp);
230}
231
232static PyObject *
233Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
234{
235  uint_t tmp = aubio_source_get_channels (self->o);
236  return (PyObject *)PyInt_FromLong (tmp);
237}
238
[7b56229]239static PyObject *
240Pyaubio_source_close (Py_source *self, PyObject *unused)
241{
[3cc3fd8]242  aubio_source_close (self->o);
[7b56229]243  Py_RETURN_NONE;
244}
245
[11b49d7]246static PyObject *
247Pyaubio_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]265static 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
281AUBIO_TYPEOBJECT(source, "aubio.source")
Note: See TracBrowser for help on using the repository browser.