1 | #include "aubio-types.h" |
---|
2 | |
---|
3 | typedef struct |
---|
4 | { |
---|
5 | PyObject_HEAD |
---|
6 | aubio_sink_t * o; |
---|
7 | char_t* uri; |
---|
8 | uint_t samplerate; |
---|
9 | uint_t channels; |
---|
10 | fvec_t *write_data; |
---|
11 | fmat_t *mwrite_data; |
---|
12 | } Py_sink; |
---|
13 | |
---|
14 | static char Py_sink_doc[] = "" |
---|
15 | " __new__(path, samplerate = 44100, channels = 1)\n" |
---|
16 | "\n" |
---|
17 | " Create a new sink, opening the given path for writing.\n" |
---|
18 | "\n" |
---|
19 | " Examples\n" |
---|
20 | " --------\n" |
---|
21 | "\n" |
---|
22 | " Create a new sink at 44100Hz, mono:\n" |
---|
23 | "\n" |
---|
24 | " >>> sink('/tmp/t.wav')\n" |
---|
25 | "\n" |
---|
26 | " Create a new sink at 8000Hz, mono:\n" |
---|
27 | "\n" |
---|
28 | " >>> sink('/tmp/t.wav', samplerate = 8000)\n" |
---|
29 | "\n" |
---|
30 | " Create a new sink at 32000Hz, stereo:\n" |
---|
31 | "\n" |
---|
32 | " >>> sink('/tmp/t.wav', samplerate = 32000, channels = 2)\n" |
---|
33 | "\n" |
---|
34 | " Create a new sink at 32000Hz, 5 channels:\n" |
---|
35 | "\n" |
---|
36 | " >>> sink('/tmp/t.wav', channels = 5, samplerate = 32000)\n" |
---|
37 | "\n" |
---|
38 | " __call__(vec, write)\n" |
---|
39 | " x(vec,write) <==> x.do(vec, write)\n" |
---|
40 | "\n" |
---|
41 | " Write vector to sink.\n" |
---|
42 | "\n" |
---|
43 | " See also\n" |
---|
44 | " --------\n" |
---|
45 | " aubio.sink.do\n" |
---|
46 | "\n"; |
---|
47 | |
---|
48 | static char Py_sink_do_doc[] = "" |
---|
49 | "x.do(vec, write) <==> x(vec, write)\n" |
---|
50 | "\n" |
---|
51 | "write monophonic vector to sink"; |
---|
52 | |
---|
53 | static char Py_sink_do_multi_doc[] = "" |
---|
54 | "x.do_multi(mat, write)\n" |
---|
55 | "\n" |
---|
56 | "write polyphonic vector to sink"; |
---|
57 | |
---|
58 | static char Py_sink_close_doc[] = "" |
---|
59 | "x.close()\n" |
---|
60 | "\n" |
---|
61 | "close this sink now"; |
---|
62 | |
---|
63 | static PyObject * |
---|
64 | Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
65 | { |
---|
66 | Py_sink *self; |
---|
67 | char_t* uri = NULL; |
---|
68 | uint_t samplerate = 0; |
---|
69 | uint_t channels = 0; |
---|
70 | static char *kwlist[] = { "uri", "samplerate", "channels", NULL }; |
---|
71 | |
---|
72 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist, |
---|
73 | &uri, &samplerate, &channels)) { |
---|
74 | return NULL; |
---|
75 | } |
---|
76 | |
---|
77 | self = (Py_sink *) pytype->tp_alloc (pytype, 0); |
---|
78 | |
---|
79 | if (self == NULL) { |
---|
80 | return NULL; |
---|
81 | } |
---|
82 | |
---|
83 | self->uri = "none"; |
---|
84 | if (uri != NULL) { |
---|
85 | self->uri = uri; |
---|
86 | } |
---|
87 | |
---|
88 | self->samplerate = Py_aubio_default_samplerate; |
---|
89 | if ((sint_t)samplerate > 0) { |
---|
90 | self->samplerate = samplerate; |
---|
91 | } else if ((sint_t)samplerate < 0) { |
---|
92 | PyErr_SetString (PyExc_ValueError, |
---|
93 | "can not use negative value for samplerate"); |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | self->channels = 1; |
---|
98 | if ((sint_t)channels > 0) { |
---|
99 | self->channels = channels; |
---|
100 | } else if ((sint_t)channels < 0) { |
---|
101 | PyErr_SetString (PyExc_ValueError, |
---|
102 | "can not use negative or null value for channels"); |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | return (PyObject *) self; |
---|
107 | } |
---|
108 | |
---|
109 | static int |
---|
110 | Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds) |
---|
111 | { |
---|
112 | if (self->channels == 1) { |
---|
113 | self->o = new_aubio_sink ( self->uri, self->samplerate ); |
---|
114 | } else { |
---|
115 | self->o = new_aubio_sink ( self->uri, 0 ); |
---|
116 | aubio_sink_preset_channels ( self->o, self->channels ); |
---|
117 | aubio_sink_preset_samplerate ( self->o, self->samplerate ); |
---|
118 | } |
---|
119 | if (self->o == NULL) { |
---|
120 | PyErr_SetString (PyExc_RuntimeError, "error creating sink with this uri"); |
---|
121 | return -1; |
---|
122 | } |
---|
123 | self->samplerate = aubio_sink_get_samplerate ( self->o ); |
---|
124 | self->channels = aubio_sink_get_channels ( self->o ); |
---|
125 | |
---|
126 | self->write_data = (fvec_t *)malloc(sizeof(fvec_t)); |
---|
127 | self->mwrite_data = (fmat_t *)malloc(sizeof(fmat_t)); |
---|
128 | self->mwrite_data->height = self->channels; |
---|
129 | self->mwrite_data->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->channels); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | static void |
---|
134 | Py_sink_del (Py_sink *self, PyObject *unused) |
---|
135 | { |
---|
136 | del_aubio_sink(self->o); |
---|
137 | free(self->write_data); |
---|
138 | free(self->mwrite_data->data); |
---|
139 | free(self->mwrite_data); |
---|
140 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
141 | } |
---|
142 | |
---|
143 | /* function Py_sink_do */ |
---|
144 | static PyObject * |
---|
145 | Py_sink_do(Py_sink * self, PyObject * args) |
---|
146 | { |
---|
147 | /* input vectors python prototypes */ |
---|
148 | PyObject * write_data_obj; |
---|
149 | |
---|
150 | /* input vectors prototypes */ |
---|
151 | uint_t write; |
---|
152 | |
---|
153 | |
---|
154 | if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) { |
---|
155 | return NULL; |
---|
156 | } |
---|
157 | |
---|
158 | /* input vectors parsing */ |
---|
159 | if (!PyAubio_ArrayToCFvec(write_data_obj, self->write_data)) { |
---|
160 | return NULL; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | /* compute _do function */ |
---|
165 | aubio_sink_do (self->o, self->write_data, write); |
---|
166 | |
---|
167 | Py_RETURN_NONE; |
---|
168 | } |
---|
169 | |
---|
170 | /* function Py_sink_do_multi */ |
---|
171 | static PyObject * |
---|
172 | Py_sink_do_multi(Py_sink * self, PyObject * args) |
---|
173 | { |
---|
174 | /* input vectors python prototypes */ |
---|
175 | PyObject * write_data_obj; |
---|
176 | |
---|
177 | /* input vectors prototypes */ |
---|
178 | uint_t write; |
---|
179 | |
---|
180 | |
---|
181 | if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) { |
---|
182 | return NULL; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /* input vectors parsing */ |
---|
187 | if (!PyAubio_ArrayToCFmat(write_data_obj, self->mwrite_data)) { |
---|
188 | return NULL; |
---|
189 | } |
---|
190 | |
---|
191 | /* compute _do function */ |
---|
192 | aubio_sink_do_multi (self->o, self->mwrite_data, write); |
---|
193 | Py_RETURN_NONE; |
---|
194 | } |
---|
195 | |
---|
196 | static PyMemberDef Py_sink_members[] = { |
---|
197 | {"uri", T_STRING, offsetof (Py_sink, uri), READONLY, |
---|
198 | "path at which the sink was created"}, |
---|
199 | {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY, |
---|
200 | "samplerate at which the sink was created"}, |
---|
201 | {"channels", T_INT, offsetof (Py_sink, channels), READONLY, |
---|
202 | "number of channels with which the sink was created"}, |
---|
203 | { NULL } // sentinel |
---|
204 | }; |
---|
205 | |
---|
206 | static PyObject * |
---|
207 | Pyaubio_sink_close (Py_sink *self, PyObject *unused) |
---|
208 | { |
---|
209 | aubio_sink_close (self->o); |
---|
210 | Py_RETURN_NONE; |
---|
211 | } |
---|
212 | |
---|
213 | static PyMethodDef Py_sink_methods[] = { |
---|
214 | {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc}, |
---|
215 | {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc}, |
---|
216 | {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc}, |
---|
217 | {NULL} /* sentinel */ |
---|
218 | }; |
---|
219 | |
---|
220 | PyTypeObject Py_sinkType = { |
---|
221 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
222 | "aubio.sink", |
---|
223 | sizeof (Py_sink), |
---|
224 | 0, |
---|
225 | (destructor) Py_sink_del, |
---|
226 | 0, |
---|
227 | 0, |
---|
228 | 0, |
---|
229 | 0, |
---|
230 | 0, |
---|
231 | 0, |
---|
232 | 0, |
---|
233 | 0, |
---|
234 | 0, |
---|
235 | (ternaryfunc)Py_sink_do, |
---|
236 | 0, |
---|
237 | 0, |
---|
238 | 0, |
---|
239 | 0, |
---|
240 | Py_TPFLAGS_DEFAULT, |
---|
241 | Py_sink_doc, |
---|
242 | 0, |
---|
243 | 0, |
---|
244 | 0, |
---|
245 | 0, |
---|
246 | 0, |
---|
247 | 0, |
---|
248 | Py_sink_methods, |
---|
249 | Py_sink_members, |
---|
250 | 0, |
---|
251 | 0, |
---|
252 | 0, |
---|
253 | 0, |
---|
254 | 0, |
---|
255 | 0, |
---|
256 | (initproc) Py_sink_init, |
---|
257 | 0, |
---|
258 | Py_sink_new, |
---|
259 | }; |
---|