Debian Codegraph @

src/objects/matrixprocessmodule.c

raw
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #include <Python.h>
22 #include "structmember.h"
23 #include "pyomodule.h"
24 #include "streammodule.h"
25 #include "servermodule.h"
26 #include "dummymodule.h"
27 #include "matrixmodule.h"
28
29 /**************/
30 /* MatrixPointer object */
31 /**************/
32 typedef struct
33 {
34 pyo_audio_HEAD
35 PyObject *matrix;
36 PyObject *x;
37 Stream *x_stream;
38 PyObject *y;
39 Stream *y_stream;
40 int modebuffer[2];
41 } MatrixPointer;
42
43 static void
44 MatrixPointer_readframes(MatrixPointer *self)
45 {
46 int i;
47
48 MYFLT *x = Stream_getData((Stream *)self->x_stream);
49 MYFLT *y = Stream_getData((Stream *)self->y_stream);
50
51 for (i = 0; i < self->bufsize; i++)
52 {
53 self->data[i] = MatrixStream_getInterpPointFromPos((MatrixStream *)self->matrix, x[i], y[i]);
54 }
55 }
56
57 static void MatrixPointer_postprocessing_ii(MatrixPointer *self) { POST_PROCESSING_II };
58 static void MatrixPointer_postprocessing_ai(MatrixPointer *self) { POST_PROCESSING_AI };
59 static void MatrixPointer_postprocessing_ia(MatrixPointer *self) { POST_PROCESSING_IA };
60 static void MatrixPointer_postprocessing_aa(MatrixPointer *self) { POST_PROCESSING_AA };
61 static void MatrixPointer_postprocessing_ireva(MatrixPointer *self) { POST_PROCESSING_IREVA };
62 static void MatrixPointer_postprocessing_areva(MatrixPointer *self) { POST_PROCESSING_AREVA };
63 static void MatrixPointer_postprocessing_revai(MatrixPointer *self) { POST_PROCESSING_REVAI };
64 static void MatrixPointer_postprocessing_revaa(MatrixPointer *self) { POST_PROCESSING_REVAA };
65 static void MatrixPointer_postprocessing_revareva(MatrixPointer *self) { POST_PROCESSING_REVAREVA };
66
67 static void
68 MatrixPointer_setProcMode(MatrixPointer *self)
69 {
70 int muladdmode;
71 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
72
73 self->proc_func_ptr = MatrixPointer_readframes;
74
75 switch (muladdmode)
76 {
77 case 0:
78 self->muladd_func_ptr = MatrixPointer_postprocessing_ii;
79 break;
80
81 case 1:
82 self->muladd_func_ptr = MatrixPointer_postprocessing_ai;
83 break;
84
85 case 2:
86 self->muladd_func_ptr = MatrixPointer_postprocessing_revai;
87 break;
88
89 case 10:
90 self->muladd_func_ptr = MatrixPointer_postprocessing_ia;
91 break;
92
93 case 11:
94 self->muladd_func_ptr = MatrixPointer_postprocessing_aa;
95 break;
96
97 case 12:
98 self->muladd_func_ptr = MatrixPointer_postprocessing_revaa;
99 break;
100
101 case 20:
102 self->muladd_func_ptr = MatrixPointer_postprocessing_ireva;
103 break;
104
105 case 21:
106 self->muladd_func_ptr = MatrixPointer_postprocessing_areva;
107 break;
108
109 case 22:
110 self->muladd_func_ptr = MatrixPointer_postprocessing_revareva;
111 break;
112 }
113 }
114
115 static void
116 MatrixPointer_compute_next_data_frame(MatrixPointer *self)
117 {
118 (*self->proc_func_ptr)(self);
119 (*self->muladd_func_ptr)(self);
120 }
121
122 static int
123 MatrixPointer_traverse(MatrixPointer *self, visitproc visit, void *arg)
124 {
125 pyo_VISIT
126 Py_VISIT(self->matrix);
127 Py_VISIT(self->x);
128 Py_VISIT(self->y);
129 return 0;
130 }
131
132 static int
133 MatrixPointer_clear(MatrixPointer *self)
134 {
135 pyo_CLEAR
136 Py_CLEAR(self->matrix);
137 Py_CLEAR(self->x);
138 Py_CLEAR(self->y);
139 return 0;
140 }
141
142 static void
143 MatrixPointer_dealloc(MatrixPointer* self)
144 {
145 pyo_DEALLOC
146 MatrixPointer_clear(self);
147 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
148 Py_TYPE(self)->tp_free((PyObject*)self);
149 }
150
151 static PyObject *
152 MatrixPointer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
153 {
154 int i;
155 PyObject *matrixtmp, *xtmp, *ytmp, *multmp = NULL, *addtmp = NULL;
156 MatrixPointer *self;
157 self = (MatrixPointer *)type->tp_alloc(type, 0);
158
159 self->x = PyFloat_FromDouble(0.0);
160 self->y = PyFloat_FromDouble(0.0);
161
162 self->modebuffer[0] = 0;
163 self->modebuffer[1] = 0;
164
165 INIT_OBJECT_COMMON
166 Stream_setFunctionPtr(self->stream, MatrixPointer_compute_next_data_frame);
167 self->mode_func_ptr = MatrixPointer_setProcMode;
168
169 static char *kwlist[] = {"matrix", "x", "y", "mul", "add", NULL};
170
171 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO|OO", kwlist, &matrixtmp, &xtmp, &ytmp, &multmp, &addtmp))
172 Py_RETURN_NONE;
173
174 if ( PyObject_HasAttrString((PyObject *)matrixtmp, "getMatrixStream") == 0 )
175 {
176 PyErr_SetString(PyExc_TypeError, "\"matrix\" argument of MatrixPointer must be a PyoMatrixObject.\n");
177 Py_RETURN_NONE;
178 }
179
180 self->matrix = PyObject_CallMethod((PyObject *)matrixtmp, "getMatrixStream", "");
181
182 if (xtmp)
183 {
184 PyObject_CallMethod((PyObject *)self, "setX", "O", xtmp);
185 }
186
187 if (ytmp)
188 {
189 PyObject_CallMethod((PyObject *)self, "setY", "O", ytmp);
190 }
191
192 if (multmp)
193 {
194 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
195 }
196
197 if (addtmp)
198 {
199 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
200 }
201
202 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
203
204 return (PyObject *)self;
205 }
206
207 static PyObject * MatrixPointer_getServer(MatrixPointer* self) { GET_SERVER };
208 static PyObject * MatrixPointer_getStream(MatrixPointer* self) { GET_STREAM };
209 static PyObject * MatrixPointer_setMul(MatrixPointer *self, PyObject *arg) { SET_MUL };
210 static PyObject * MatrixPointer_setAdd(MatrixPointer *self, PyObject *arg) { SET_ADD };
211 static PyObject * MatrixPointer_setSub(MatrixPointer *self, PyObject *arg) { SET_SUB };
212 static PyObject * MatrixPointer_setDiv(MatrixPointer *self, PyObject *arg) { SET_DIV };
213
214 static PyObject * MatrixPointer_play(MatrixPointer *self, PyObject *args, PyObject *kwds) { PLAY };
215 static PyObject * MatrixPointer_out(MatrixPointer *self, PyObject *args, PyObject *kwds) { OUT };
216 static PyObject * MatrixPointer_stop(MatrixPointer *self, PyObject *args, PyObject *kwds) { STOP };
217
218 static PyObject * MatrixPointer_multiply(MatrixPointer *self, PyObject *arg) { MULTIPLY };
219 static PyObject * MatrixPointer_inplace_multiply(MatrixPointer *self, PyObject *arg) { INPLACE_MULTIPLY };
220 static PyObject * MatrixPointer_add(MatrixPointer *self, PyObject *arg) { ADD };
221 static PyObject * MatrixPointer_inplace_add(MatrixPointer *self, PyObject *arg) { INPLACE_ADD };
222 static PyObject * MatrixPointer_sub(MatrixPointer *self, PyObject *arg) { SUB };
223 static PyObject * MatrixPointer_inplace_sub(MatrixPointer *self, PyObject *arg) { INPLACE_SUB };
224 static PyObject * MatrixPointer_div(MatrixPointer *self, PyObject *arg) { DIV };
225 static PyObject * MatrixPointer_inplace_div(MatrixPointer *self, PyObject *arg) { INPLACE_DIV };
226
227 static PyObject *
228 MatrixPointer_getMatrix(MatrixPointer* self)
229 {
230 Py_INCREF(self->matrix);
231 return self->matrix;
232 };
233
234 static PyObject *
235 MatrixPointer_setMatrix(MatrixPointer *self, PyObject *arg)
236 {
237 ASSERT_ARG_NOT_NULL
238
239 if ( PyObject_HasAttrString((PyObject *)arg, "getMatrixStream") == 0 )
240 {
241 PyErr_SetString(PyExc_TypeError, "\"matrix\" argument of MatrixPointer must be a PyoMatrixObject.\n");
242 Py_RETURN_NONE;
243 }
244
245 Py_DECREF(self->matrix);
246 self->matrix = PyObject_CallMethod((PyObject *)arg, "getMatrixStream", "");
247
248 Py_RETURN_NONE;
249 }
250
251 static PyObject *
252 MatrixPointer_setX(MatrixPointer *self, PyObject *arg)
253 {
254 ASSERT_ARG_NOT_NULL
255
256 if (PyObject_HasAttrString((PyObject *)arg, "server") == 0)
257 {
258 PyErr_SetString(PyExc_TypeError, "\"x\" attribute of MatrixPointer must be a PyoObject.\n");
259 Py_RETURN_NONE;
260 }
261
262 Py_DECREF(self->x);
263
264 self->x = arg;
265 Py_INCREF(self->x);
266 PyObject *streamtmp = PyObject_CallMethod((PyObject *)self->x, "_getStream", NULL);
267 self->x_stream = (Stream *)streamtmp;
268 Py_INCREF(self->x_stream);
269
270 Py_RETURN_NONE;
271 }
272
273 static PyObject *
274 MatrixPointer_setY(MatrixPointer *self, PyObject *arg)
275 {
276 ASSERT_ARG_NOT_NULL
277
278 if (PyObject_HasAttrString((PyObject *)arg, "server") == 0)
279 {
280 PyErr_SetString(PyExc_TypeError, "\"y\" attribute of MatrixPointer must be a PyoObject.\n");
281 Py_RETURN_NONE;
282 }
283
284 Py_DECREF(self->y);
285
286 self->y = arg;
287 Py_INCREF(self->y);
288 PyObject *streamtmp = PyObject_CallMethod((PyObject *)self->y, "_getStream", NULL);
289 self->y_stream = (Stream *)streamtmp;
290 Py_INCREF(self->y_stream);
291
292 Py_RETURN_NONE;
293 }
294
295 static PyMemberDef MatrixPointer_members[] =
296 {
297 {"server", T_OBJECT_EX, offsetof(MatrixPointer, server), 0, "Pyo server."},
298 {"stream", T_OBJECT_EX, offsetof(MatrixPointer, stream), 0, "Stream object."},
299 {"matrix", T_OBJECT_EX, offsetof(MatrixPointer, matrix), 0, "Waveform matrix."},
300 {"x", T_OBJECT_EX, offsetof(MatrixPointer, x), 0, "Reader x."},
301 {"y", T_OBJECT_EX, offsetof(MatrixPointer, y), 0, "Reader y."},
302 {"mul", T_OBJECT_EX, offsetof(MatrixPointer, mul), 0, "Mul factor."},
303 {"add", T_OBJECT_EX, offsetof(MatrixPointer, add), 0, "Add factor."},
304 {NULL} /* Sentinel */
305 };
306
307 static PyMethodDef MatrixPointer_methods[] =
308 {
309 {"getMatrix", (PyCFunction)MatrixPointer_getMatrix, METH_NOARGS, "Returns waveform matrix object."},
310 {"getServer", (PyCFunction)MatrixPointer_getServer, METH_NOARGS, "Returns server object."},
311 {"_getStream", (PyCFunction)MatrixPointer_getStream, METH_NOARGS, "Returns stream object."},
312 {"play", (PyCFunction)MatrixPointer_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
313 {"out", (PyCFunction)MatrixPointer_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
314 {"stop", (PyCFunction)MatrixPointer_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
315 {"setMatrix", (PyCFunction)MatrixPointer_setMatrix, METH_O, "Sets oscillator matrix."},
316 {"setX", (PyCFunction)MatrixPointer_setX, METH_O, "Sets reader x."},
317 {"setY", (PyCFunction)MatrixPointer_setY, METH_O, "Sets reader y."},
318 {"setMul", (PyCFunction)MatrixPointer_setMul, METH_O, "Sets oscillator mul factor."},
319 {"setAdd", (PyCFunction)MatrixPointer_setAdd, METH_O, "Sets oscillator add factor."},
320 {"setSub", (PyCFunction)MatrixPointer_setSub, METH_O, "Sets oscillator inverse add factor."},
321 {"setDiv", (PyCFunction)MatrixPointer_setDiv, METH_O, "Sets inverse mul factor."},
322 {NULL} /* Sentinel */
323 };
324
325 static PyNumberMethods MatrixPointer_as_number =
326 {
327 (binaryfunc)MatrixPointer_add, /*nb_add*/
328 (binaryfunc)MatrixPointer_sub, /*nb_subtract*/
329 (binaryfunc)MatrixPointer_multiply, /*nb_multiply*/
330 0, /*nb_remainder*/
331 0, /*nb_divmod*/
332 0, /*nb_power*/
333 0, /*nb_neg*/
334 0, /*nb_pos*/
335 0, /*(unaryfunc)array_abs,*/
336 0, /*nb_nonzero*/
337 0, /*nb_invert*/
338 0, /*nb_lshift*/
339 0, /*nb_rshift*/
340 0, /*nb_and*/
341 0, /*nb_xor*/
342 0, /*nb_or*/
343 0, /*nb_int*/
344 0, /*nb_long*/
345 0, /*nb_float*/
346 (binaryfunc)MatrixPointer_inplace_add, /*inplace_add*/
347 (binaryfunc)MatrixPointer_inplace_sub, /*inplace_subtract*/
348 (binaryfunc)MatrixPointer_inplace_multiply, /*inplace_multiply*/
349 0, /*inplace_remainder*/
350 0, /*inplace_power*/
351 0, /*inplace_lshift*/
352 0, /*inplace_rshift*/
353 0, /*inplace_and*/
354 0, /*inplace_xor*/
355 0, /*inplace_or*/
356 0, /*nb_floor_divide*/
357 (binaryfunc)MatrixPointer_div, /*nb_true_divide*/
358 0, /*nb_inplace_floor_divide*/
359 (binaryfunc)MatrixPointer_inplace_div, /*nb_inplace_true_divide*/
360 0, /* nb_x */
361 };
362
363 PyTypeObject MatrixPointerType =
364 {
365 PyVarObject_HEAD_INIT(NULL, 0)
366 "_pyo.MatrixPointer_base", /*tp_name*/
367 sizeof(MatrixPointer), /*tp_basicsize*/
368 0, /*tp_itemsize*/
369 (destructor)MatrixPointer_dealloc, /*tp_dealloc*/
370 0, /*tp_print*/
371 0, /*tp_getattr*/
372 0, /*tp_setattr*/
373 0, /*tp_as_async (tp_compare in Python 2)*/
374 0, /*tp_repr*/
375 &MatrixPointer_as_number, /*tp_as_number*/
376 0, /*tp_as_sequence*/
377 0, /*tp_as_mapping*/
378 0, /*tp_hash */
379 0, /*tp_call*/
380 0, /*tp_str*/
381 0, /*tp_getattro*/
382 0, /*tp_setattro*/
383 0, /*tp_as_buffer*/
384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
385 "MatrixPointer objects. Read a waveform matrix with a pointer x.", /* tp_doc */
386 (traverseproc)MatrixPointer_traverse, /* tp_traverse */
387 (inquiry)MatrixPointer_clear, /* tp_clear */
388 0, /* tp_richcompare */
389 0, /* tp_weaklistoffset */
390 0, /* tp_iter */
391 0, /* tp_iternext */
392 MatrixPointer_methods, /* tp_methods */
393 MatrixPointer_members, /* tp_members */
394 0, /* tp_getset */
395 0, /* tp_base */
396 0, /* tp_dict */
397 0, /* tp_descr_get */
398 0, /* tp_descr_set */
399 0, /* tp_dictoffset */
400 0, /* tp_init */
401 0, /* tp_alloc */
402 MatrixPointer_new, /* tp_new */
403 };