2f8ed6e4d74717b704a8539a8e24e6537f91999b
[aubio.git] / python / ext / py-source.c
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;
9   uint_t channels;
10   uint_t hop_size;
11 } Py_source;
12
13 static 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
47 static char Py_source_get_samplerate_doc[] = ""
48 "x.get_samplerate() -> source samplerate\n"
49 "\n"
50 "Get samplerate of source.";
51
52 static char Py_source_get_channels_doc[] = ""
53 "x.get_channels() -> number of channels\n"
54 "\n"
55 "Get number of channels in source.";
56
57 static char Py_source_do_doc[] = ""
58 "vec, read = x.do() <==> vec, read = x()\n"
59 "\n"
60 "Read monophonic vector from source.";
61
62 static char Py_source_do_multi_doc[] = ""
63 "mat, read = x.do_multi()\n"
64 "\n"
65 "Read polyphonic vector from source.";
66
67 static char Py_source_close_doc[] = ""
68 "x.close()\n"
69 "\n"
70 "Close this source now.";
71
72 static PyObject *
73 Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
74 {
75   Py_source *self;
76   char_t* uri = NULL;
77   uint_t samplerate = 0;
78   uint_t hop_size = 0;
79   static char *kwlist[] = { "uri", "samplerate", "hop_size", NULL };
80
81   if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
82           &uri, &samplerate, &hop_size)) {
83     return NULL;
84   }
85
86   self = (Py_source *) pytype->tp_alloc (pytype, 0);
87
88   if (self == NULL) {
89     return NULL;
90   }
91
92   self->uri = "none";
93   if (uri != NULL) {
94     self->uri = uri;
95   }
96
97   self->samplerate = 0;
98   if ((sint_t)samplerate > 0) {
99     self->samplerate = samplerate;
100   } else if ((sint_t)samplerate < 0) {
101     PyErr_SetString (PyExc_ValueError,
102         "can not use negative value for samplerate");
103     return NULL;
104   }
105
106   self->hop_size = Py_default_vector_length / 2;
107   if ((sint_t)hop_size > 0) {
108     self->hop_size = hop_size;
109   } else if ((sint_t)hop_size < 0) {
110     PyErr_SetString (PyExc_ValueError,
111         "can not use negative value for hop_size");
112     return NULL;
113   }
114
115   return (PyObject *) self;
116 }
117
118 static int
119 Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
120 {
121   self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
122   if (self->o == NULL) {
123     PyErr_SetString (PyExc_StandardError, "error creating object");
124     return -1;
125   }
126   self->samplerate = aubio_source_get_samplerate ( self->o );
127   self->channels = aubio_source_get_channels ( self->o );
128
129   return 0;
130 }
131
132 AUBIO_DEL(source)
133
134 /* function Py_source_do */
135 static PyObject *
136 Py_source_do(Py_source * self, PyObject * args)
137 {
138
139
140   /* output vectors prototypes */
141   fvec_t* read_to;
142   uint_t read;
143
144
145
146
147
148
149   /* creating output read_to as a new_fvec of length self->hop_size */
150   read_to = new_fvec (self->hop_size);
151   read = 0;
152
153
154   /* compute _do function */
155   aubio_source_do (self->o, read_to, &read);
156
157   PyObject *outputs = PyList_New(0);
158   PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
159   //del_fvec (read_to);
160   PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
161   return outputs;
162 }
163
164 /* function Py_source_do_multi */
165 static PyObject *
166 Py_source_do_multi(Py_source * self, PyObject * args)
167 {
168
169
170   /* output vectors prototypes */
171   fmat_t* read_to;
172   uint_t read;
173
174
175
176
177
178
179   /* creating output read_to as a new_fvec of length self->hop_size */
180   read_to = new_fmat (self->channels, self->hop_size);
181   read = 0;
182
183
184   /* compute _do function */
185   aubio_source_do_multi (self->o, read_to, &read);
186
187   PyObject *outputs = PyList_New(0);
188   PyList_Append( outputs, (PyObject *)PyAubio_CFmatToArray (read_to));
189   //del_fvec (read_to);
190   PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
191   return outputs;
192 }
193
194 AUBIO_MEMBERS_START(source)
195   {"uri", T_STRING, offsetof (Py_source, uri), READONLY,
196     "path at which the source was created"},
197   {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY,
198     "samplerate at which the source is viewed"},
199   {"channels", T_INT, offsetof (Py_source, channels), READONLY,
200     "number of channels found in the source"},
201   {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
202     "number of consecutive frames that will be read at each do or do_multi call"},
203 AUBIO_MEMBERS_STOP(source)
204
205
206 static PyObject *
207 Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
208 {
209   uint_t tmp = aubio_source_get_samplerate (self->o);
210   return (PyObject *)PyInt_FromLong (tmp);
211 }
212
213 static PyObject *
214 Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
215 {
216   uint_t tmp = aubio_source_get_channels (self->o);
217   return (PyObject *)PyInt_FromLong (tmp);
218 }
219
220 static PyObject *
221 Pyaubio_source_close (Py_source *self, PyObject *unused)
222 {
223   aubio_source_close (self->o);
224   Py_RETURN_NONE;
225 }
226
227 static PyMethodDef Py_source_methods[] = {
228   {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
229     METH_NOARGS, Py_source_get_samplerate_doc},
230   {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
231     METH_NOARGS, Py_source_get_channels_doc},
232   {"do", (PyCFunction) Py_source_do,
233     METH_NOARGS, Py_source_do_doc},
234   {"do_multi", (PyCFunction) Py_source_do_multi,
235     METH_NOARGS, Py_source_do_multi_doc},
236   {"close", (PyCFunction) Pyaubio_source_close,
237     METH_NOARGS, Py_source_close_doc},
238   {NULL} /* sentinel */
239 };
240
241 AUBIO_TYPEOBJECT(source, "aubio.source")