Debian Codegraph @

src/objects/sigmodule.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
28 typedef struct
29 {
30 pyo_audio_HEAD
31 PyObject *value;
32 Stream *value_stream;
33 int modebuffer[3];
34 } Sig;
35
36 static void Sig_postprocessing_ii(Sig *self) { POST_PROCESSING_II };
37 static void Sig_postprocessing_ai(Sig *self) { POST_PROCESSING_AI };
38 static void Sig_postprocessing_ia(Sig *self) { POST_PROCESSING_IA };
39 static void Sig_postprocessing_aa(Sig *self) { POST_PROCESSING_AA };
40 static void Sig_postprocessing_ireva(Sig *self) { POST_PROCESSING_IREVA };
41 static void Sig_postprocessing_areva(Sig *self) { POST_PROCESSING_AREVA };
42 static void Sig_postprocessing_revai(Sig *self) { POST_PROCESSING_REVAI };
43 static void Sig_postprocessing_revaa(Sig *self) { POST_PROCESSING_REVAA };
44 static void Sig_postprocessing_revareva(Sig *self) { POST_PROCESSING_REVAREVA };
45
46 static void
47 Sig_setProcMode(Sig *self)
48 {
49 int muladdmode;
50 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
51
52 switch (muladdmode)
53 {
54 case 0:
55 self->muladd_func_ptr = Sig_postprocessing_ii;
56 break;
57
58 case 1:
59 self->muladd_func_ptr = Sig_postprocessing_ai;
60 break;
61
62 case 2:
63 self->muladd_func_ptr = Sig_postprocessing_revai;
64 break;
65
66 case 10:
67 self->muladd_func_ptr = Sig_postprocessing_ia;
68 break;
69
70 case 11:
71 self->muladd_func_ptr = Sig_postprocessing_aa;
72 break;
73
74 case 12:
75 self->muladd_func_ptr = Sig_postprocessing_revaa;
76 break;
77
78 case 20:
79 self->muladd_func_ptr = Sig_postprocessing_ireva;
80 break;
81
82 case 21:
83 self->muladd_func_ptr = Sig_postprocessing_areva;
84 break;
85
86 case 22:
87 self->muladd_func_ptr = Sig_postprocessing_revareva;
88 break;
89 }
90 }
91
92 static void
93 Sig_compute_next_data_frame(Sig *self)
94 {
95 int i;
96
97 if (self->modebuffer[2] == 0)
98 {
99 MYFLT val = PyFloat_AS_DOUBLE(self->value);
100
101 for (i = 0; i < self->bufsize; i++)
102 {
103 self->data[i] = val;
104 }
105 }
106 else
107 {
108 MYFLT *vals = Stream_getData((Stream *)self->value_stream);
109
110 for (i = 0; i < self->bufsize; i++)
111 {
112 self->data[i] = vals[i];
113 }
114 }
115
116 (*self->muladd_func_ptr)(self);
117 }
118
119 static int
120 Sig_traverse(Sig *self, visitproc visit, void *arg)
121 {
122 pyo_VISIT
123 Py_VISIT(self->value);
124 return 0;
125 }
126
127 static int
128 Sig_clear(Sig *self)
129 {
130 pyo_CLEAR
131 Py_CLEAR(self->value);
132 return 0;
133 }
134
135 static void
136 Sig_dealloc(Sig* self)
137 {
138 pyo_DEALLOC
139 Sig_clear(self);
140 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
141 Py_TYPE(self)->tp_free((PyObject*)self);
142 }
143
144 static PyObject *
145 Sig_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
146 {
147 int i;
148 PyObject *valuetmp = NULL, *multmp = NULL, *addtmp = NULL;
149 Sig *self;
150 self = (Sig *)type->tp_alloc(type, 0);
151
152 self->value = PyFloat_FromDouble(0.0);
153 self->modebuffer[0] = 0;
154 self->modebuffer[1] = 0;
155 self->modebuffer[2] = 0;
156
157 INIT_OBJECT_COMMON
158 Stream_setFunctionPtr(self->stream, Sig_compute_next_data_frame);
159 self->mode_func_ptr = Sig_setProcMode;
160
161 static char *kwlist[] = {"value", "mul", "add", NULL};
162
163 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &valuetmp, &multmp, &addtmp))
164 Py_RETURN_NONE;
165
166 if (valuetmp)
167 {
168 PyObject_CallMethod((PyObject *)self, "setValue", "O", valuetmp);
169 }
170
171 if (multmp)
172 {
173 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
174 }
175
176 if (addtmp)
177 {
178 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
179 }
180
181 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
182
183 (*self->mode_func_ptr)(self);
184
185 Sig_compute_next_data_frame((Sig *)self);
186
187 return (PyObject *)self;
188 }
189
190 static PyObject * Sig_setValue(Sig *self, PyObject *arg) { SET_PARAM(self->value, self->value_stream, 2) }
191
192 static PyObject * Sig_getServer(Sig* self) { GET_SERVER };
193 static PyObject * Sig_getStream(Sig* self) { GET_STREAM };
194 static PyObject * Sig_setMul(Sig *self, PyObject *arg) { SET_MUL };
195 static PyObject * Sig_setAdd(Sig *self, PyObject *arg) { SET_ADD };
196 static PyObject * Sig_setSub(Sig *self, PyObject *arg) { SET_SUB };
197 static PyObject * Sig_setDiv(Sig *self, PyObject *arg) { SET_DIV };
198
199 static PyObject * Sig_play(Sig *self, PyObject *args, PyObject *kwds) { PLAY };
200 static PyObject * Sig_out(Sig *self, PyObject *args, PyObject *kwds) { OUT };
201 static PyObject * Sig_stop(Sig *self, PyObject *args, PyObject *kwds) { STOP };
202
203 static PyObject * Sig_multiply(Sig *self, PyObject *arg) { MULTIPLY };
204 static PyObject * Sig_inplace_multiply(Sig *self, PyObject *arg) { INPLACE_MULTIPLY };
205 static PyObject * Sig_add(Sig *self, PyObject *arg) { ADD };
206 static PyObject * Sig_inplace_add(Sig *self, PyObject *arg) { INPLACE_ADD };
207 static PyObject * Sig_sub(Sig *self, PyObject *arg) { SUB };
208 static PyObject * Sig_inplace_sub(Sig *self, PyObject *arg) { INPLACE_SUB };
209 static PyObject * Sig_div(Sig *self, PyObject *arg) { DIV };
210 static PyObject * Sig_inplace_div(Sig *self, PyObject *arg) { INPLACE_DIV };
211
212 static PyMemberDef Sig_members[] =
213 {
214 {"server", T_OBJECT_EX, offsetof(Sig, server), 0, "Pyo server."},
215 {"stream", T_OBJECT_EX, offsetof(Sig, stream), 0, "Stream object."},
216 {"value", T_OBJECT_EX, offsetof(Sig, value), 0, "Target value."},
217 {"mul", T_OBJECT_EX, offsetof(Sig, mul), 0, "Mul factor."},
218 {"add", T_OBJECT_EX, offsetof(Sig, add), 0, "Add factor."},
219 {NULL} /* Sentinel */
220 };
221
222 static PyMethodDef Sig_methods[] =
223 {
224 {"getServer", (PyCFunction)Sig_getServer, METH_NOARGS, "Returns server object."},
225 {"_getStream", (PyCFunction)Sig_getStream, METH_NOARGS, "Returns stream object."},
226 {"play", (PyCFunction)Sig_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
227 {"out", (PyCFunction)Sig_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
228 {"stop", (PyCFunction)Sig_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
229 {"setValue", (PyCFunction)Sig_setValue, METH_O, "Sets Sig value."},
230 {"setMul", (PyCFunction)Sig_setMul, METH_O, "Sets Sig mul factor."},
231 {"setAdd", (PyCFunction)Sig_setAdd, METH_O, "Sets Sig add factor."},
232 {"setSub", (PyCFunction)Sig_setSub, METH_O, "Sets inverse add factor."},
233 {"setDiv", (PyCFunction)Sig_setDiv, METH_O, "Sets inverse mul factor."},
234 {NULL} /* Sentinel */
235 };
236
237 static PyNumberMethods Sig_as_number =
238 {
239 (binaryfunc)Sig_add, /*nb_add*/
240 (binaryfunc)Sig_sub, /*nb_subtract*/
241 (binaryfunc)Sig_multiply, /*nb_multiply*/
242 0, /*nb_remainder*/
243 0, /*nb_divmod*/
244 0, /*nb_power*/
245 0, /*nb_neg*/
246 0, /*nb_pos*/
247 0, /*(unaryfunc)array_abs,*/
248 0, /*nb_nonzero*/
249 0, /*nb_invert*/
250 0, /*nb_lshift*/
251 0, /*nb_rshift*/
252 0, /*nb_and*/
253 0, /*nb_xor*/
254 0, /*nb_or*/
255 0, /*nb_int*/
256 0, /*nb_long*/
257 0, /*nb_float*/
258 (binaryfunc)Sig_inplace_add, /*inplace_add*/
259 (binaryfunc)Sig_inplace_sub, /*inplace_subtract*/
260 (binaryfunc)Sig_inplace_multiply, /*inplace_multiply*/
261 0, /*inplace_remainder*/
262 0, /*inplace_power*/
263 0, /*inplace_lshift*/
264 0, /*inplace_rshift*/
265 0, /*inplace_and*/
266 0, /*inplace_xor*/
267 0, /*inplace_or*/
268 0, /*nb_floor_divide*/
269 (binaryfunc)Sig_div, /*nb_true_divide*/
270 0, /*nb_inplace_floor_divide*/
271 (binaryfunc)Sig_inplace_div, /*nb_inplace_true_divide*/
272 0, /* nb_index */
273 };
274
275 PyTypeObject SigType =
276 {
277 PyVarObject_HEAD_INIT(NULL, 0)
278 "_pyo.Sig_base", /*tp_name*/
279 sizeof(Sig), /*tp_basicsize*/
280 0, /*tp_itemsize*/
281 (destructor)Sig_dealloc, /*tp_dealloc*/
282 0, /*tp_print*/
283 0, /*tp_getattr*/
284 0, /*tp_setattr*/
285 0, /*tp_as_async (tp_compare in Python 2)*/
286 0, /*tp_repr*/
287 &Sig_as_number, /*tp_as_number*/
288 0, /*tp_as_sequence*/
289 0, /*tp_as_mapping*/
290 0, /*tp_hash */
291 0, /*tp_call*/
292 0, /*tp_str*/
293 0, /*tp_getattro*/
294 0, /*tp_setattro*/
295 0, /*tp_as_buffer*/
296 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
297 "Sig objects. Converts number into a signal stream.", /* tp_doc */
298 (traverseproc)Sig_traverse, /* tp_traverse */
299 (inquiry)Sig_clear, /* tp_clear */
300 0, /* tp_richcompare */
301 0, /* tp_weaklistoffset */
302 0, /* tp_iter */
303 0, /* tp_iternext */
304 Sig_methods, /* tp_methods */
305 Sig_members, /* tp_members */
306 0, /* tp_getset */
307 0, /* tp_base */
308 0, /* tp_dict */
309 0, /* tp_descr_get */
310 0, /* tp_descr_set */
311 0, /* tp_dictoffset */
312 0, /* tp_init */
313 0, /* tp_alloc */
314 Sig_new, /* tp_new */
315 };
316
317 /***************************/
318 /* SigTo - Sig + ramp time */
319 /***************************/
320 typedef struct
321 {
322 pyo_audio_HEAD
323 PyObject *value;
324 Stream *value_stream;
325 PyObject *time;
326 Stream *time_stream;
327 MYFLT lastValue;
328 MYFLT currentValue;
329 long timeStep;
330 MYFLT stepVal;
331 long timeCount;
332 int modebuffer[4];
333 } SigTo;
334
335 static void
336 SigTo_generates_i(SigTo *self)
337 {
338 int i;
339 MYFLT value, time;
340
341 if (self->modebuffer[2] == 0)
342 {
343 value = PyFloat_AS_DOUBLE(self->value);
344
345 if (value != self->lastValue)
346 {
347 if (self->modebuffer[3] == 0)
348 time = PyFloat_AS_DOUBLE(self->time);
349 else
350 time = Stream_getData((Stream *)self->time_stream)[0];
351
352 self->timeCount = 0;
353 self->lastValue = value;
354 self->timeStep = (long)(time * self->sr);
355
356 if (self->timeStep > 0)
357 self->stepVal = (value - self->currentValue) / self->timeStep;
358 }
359
360 if (self->timeStep <= 0)
361 {
362 for (i = 0; i < self->bufsize; i++)
363 self->data[i] = self->currentValue = self->lastValue = value;
364 }
365 else
366 {
367 for (i = 0; i < self->bufsize; i++)
368 {
369 if (self->timeCount == (self->timeStep - 1))
370 {
371 self->currentValue = value;
372 self->timeCount++;
373 }
374 else if (self->timeCount < self->timeStep)
375 {
376 self->currentValue += self->stepVal;
377 self->timeCount++;
378 }
379
380 self->data[i] = self->currentValue;
381 }
382 }
383 }
384 else
385 {
386 MYFLT *vals = Stream_getData((Stream *)self->value_stream);
387
388 for (i = 0; i < self->bufsize; i++)
389 {
390 value = vals[i];
391
392 if (value != self->lastValue)
393 {
394 if (self->modebuffer[3] == 0)
395 time = PyFloat_AS_DOUBLE(self->time);
396 else
397 time = Stream_getData((Stream *)self->time_stream)[i];
398
399 self->timeCount = 0;
400 self->lastValue = value;
401 self->timeStep = (long)(time * self->sr);
402
403 if (self->timeStep > 0)
404 self->stepVal = (value - self->currentValue) / self->timeStep;
405 }
406
407 if (self->timeStep <= 0)
408 {
409 self->data[i] = self->currentValue = self->lastValue = value;
410 }
411 else
412 {
413 if (self->timeCount == (self->timeStep - 1))
414 {
415 self->currentValue = value;
416 self->timeCount++;
417 }
418 else if (self->timeCount < self->timeStep)
419 {
420 self->currentValue += self->stepVal;
421 self->timeCount++;
422 }
423
424 self->data[i] = self->currentValue;
425 }
426 }
427 }
428 }
429
430 static void SigTo_postprocessing_ii(SigTo *self) { POST_PROCESSING_II };
431 static void SigTo_postprocessing_ai(SigTo *self) { POST_PROCESSING_AI };
432 static void SigTo_postprocessing_ia(SigTo *self) { POST_PROCESSING_IA };
433 static void SigTo_postprocessing_aa(SigTo *self) { POST_PROCESSING_AA };
434 static void SigTo_postprocessing_ireva(SigTo *self) { POST_PROCESSING_IREVA };
435 static void SigTo_postprocessing_areva(SigTo *self) { POST_PROCESSING_AREVA };
436 static void SigTo_postprocessing_revai(SigTo *self) { POST_PROCESSING_REVAI };
437 static void SigTo_postprocessing_revaa(SigTo *self) { POST_PROCESSING_REVAA };
438 static void SigTo_postprocessing_revareva(SigTo *self) { POST_PROCESSING_REVAREVA };
439
440 static void
441 SigTo_setProcMode(SigTo *self)
442 {
443 int muladdmode;
444 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
445
446 self->proc_func_ptr = SigTo_generates_i;
447
448 switch (muladdmode)
449 {
450 case 0:
451 self->muladd_func_ptr = SigTo_postprocessing_ii;
452 break;
453
454 case 1:
455 self->muladd_func_ptr = SigTo_postprocessing_ai;
456 break;
457
458 case 2:
459 self->muladd_func_ptr = SigTo_postprocessing_revai;
460 break;
461
462 case 10:
463 self->muladd_func_ptr = SigTo_postprocessing_ia;
464 break;
465
466 case 11:
467 self->muladd_func_ptr = SigTo_postprocessing_aa;
468 break;
469
470 case 12:
471 self->muladd_func_ptr = SigTo_postprocessing_revaa;
472 break;
473
474 case 20:
475 self->muladd_func_ptr = SigTo_postprocessing_ireva;
476 break;
477
478 case 21:
479 self->muladd_func_ptr = SigTo_postprocessing_areva;
480 break;
481
482 case 22:
483 self->muladd_func_ptr = SigTo_postprocessing_revareva;
484 break;
485 }
486 }
487
488 static void
489 SigTo_compute_next_data_frame(SigTo *self)
490 {
491 (*self->proc_func_ptr)(self);
492 (*self->muladd_func_ptr)(self);
493 }
494
495 static int
496 SigTo_traverse(SigTo *self, visitproc visit, void *arg)
497 {
498 pyo_VISIT
499 Py_VISIT(self->value);
500 Py_VISIT(self->time);
501 return 0;
502 }
503
504 static int
505 SigTo_clear(SigTo *self)
506 {
507 pyo_CLEAR
508 Py_CLEAR(self->value);
509 Py_CLEAR(self->time);
510 return 0;
511 }
512
513 static void
514 SigTo_dealloc(SigTo* self)
515 {
516 pyo_DEALLOC
517 SigTo_clear(self);
518 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
519 Py_TYPE(self)->tp_free((PyObject*)self);
520 }
521
522 static PyObject *
523 SigTo_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
524 {
525 int i;
526 MYFLT inittmp = 0.0;
527 PyObject *valuetmp = NULL, *timetmp = NULL, *multmp = NULL, *addtmp = NULL;
528 SigTo *self;
529 self = (SigTo *)type->tp_alloc(type, 0);
530
531 self->value = PyFloat_FromDouble(0.0);
532 self->time = PyFloat_FromDouble(0.025);
533 self->timeCount = 0;
534 self->stepVal = 0.0;
535 self->timeStep = 0;
536 self->modebuffer[0] = 0;
537 self->modebuffer[1] = 0;
538 self->modebuffer[2] = 0;
539 self->modebuffer[3] = 0;
540
541 INIT_OBJECT_COMMON
542 Stream_setFunctionPtr(self->stream, SigTo_compute_next_data_frame);
543 self->mode_func_ptr = SigTo_setProcMode;
544
545 static char *kwlist[] = {"value", "time", "init", "mul", "add", NULL};
546
547 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_OFOO, kwlist, &valuetmp, &timetmp, &inittmp, &multmp, &addtmp))
548 Py_RETURN_NONE;
549
550 if (valuetmp)
551 {
552 PyObject_CallMethod((PyObject *)self, "setValue", "O", valuetmp);
553 }
554
555 if (timetmp)
556 {
557 PyObject_CallMethod((PyObject *)self, "setTime", "O", timetmp);
558 }
559
560 if (multmp)
561 {
562 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
563 }
564
565 if (addtmp)
566 {
567 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
568 }
569
570 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
571
572 self->lastValue = self->currentValue = inittmp;
573
574 (*self->mode_func_ptr)(self);
575
576 for (i = 0; i < self->bufsize; i++)
577 {
578 self->data[i] = self->currentValue;
579 }
580
581 return (PyObject *)self;
582 }
583
584 static PyObject * SigTo_setValue(SigTo *self, PyObject *arg) { SET_PARAM(self->value, self->value_stream, 2); }
585 static PyObject * SigTo_setTime(SigTo *self, PyObject *arg) { SET_PARAM(self->time, self->time_stream, 3); }
586
587 static PyObject * SigTo_getServer(SigTo* self) { GET_SERVER };
588 static PyObject * SigTo_getStream(SigTo* self) { GET_STREAM };
589 static PyObject * SigTo_setMul(SigTo *self, PyObject *arg) { SET_MUL };
590 static PyObject * SigTo_setAdd(SigTo *self, PyObject *arg) { SET_ADD };
591 static PyObject * SigTo_setSub(SigTo *self, PyObject *arg) { SET_SUB };
592 static PyObject * SigTo_setDiv(SigTo *self, PyObject *arg) { SET_DIV };
593
594 static PyObject * SigTo_play(SigTo *self, PyObject *args, PyObject *kwds) { PLAY };
595 static PyObject * SigTo_stop(SigTo *self, PyObject *args, PyObject *kwds) { STOP };
596
597 static PyObject * SigTo_multiply(SigTo *self, PyObject *arg) { MULTIPLY };
598 static PyObject * SigTo_inplace_multiply(SigTo *self, PyObject *arg) { INPLACE_MULTIPLY };
599 static PyObject * SigTo_add(SigTo *self, PyObject *arg) { ADD };
600 static PyObject * SigTo_inplace_add(SigTo *self, PyObject *arg) { INPLACE_ADD };
601 static PyObject * SigTo_sub(SigTo *self, PyObject *arg) { SUB };
602 static PyObject * SigTo_inplace_sub(SigTo *self, PyObject *arg) { INPLACE_SUB };
603 static PyObject * SigTo_div(SigTo *self, PyObject *arg) { DIV };
604 static PyObject * SigTo_inplace_div(SigTo *self, PyObject *arg) { INPLACE_DIV };
605
606 static PyMemberDef SigTo_members[] =
607 {
608 {"server", T_OBJECT_EX, offsetof(SigTo, server), 0, "Pyo server."},
609 {"stream", T_OBJECT_EX, offsetof(SigTo, stream), 0, "Stream object."},
610 {"value", T_OBJECT_EX, offsetof(SigTo, value), 0, "Target value."},
611 {"time", T_OBJECT_EX, offsetof(SigTo, time), 0, "Ramp time."},
612 {"mul", T_OBJECT_EX, offsetof(SigTo, mul), 0, "Mul factor."},
613 {"add", T_OBJECT_EX, offsetof(SigTo, add), 0, "Add factor."},
614 {NULL} /* Sentinel */
615 };
616
617 static PyMethodDef SigTo_methods[] =
618 {
619 {"getServer", (PyCFunction)SigTo_getServer, METH_NOARGS, "Returns server object."},
620 {"_getStream", (PyCFunction)SigTo_getStream, METH_NOARGS, "Returns stream object."},
621 {"play", (PyCFunction)SigTo_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
622 {"stop", (PyCFunction)SigTo_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
623 {"setValue", (PyCFunction)SigTo_setValue, METH_O, "Sets SigTo value."},
624 {"setTime", (PyCFunction)SigTo_setTime, METH_O, "Sets ramp time in seconds."},
625 {"setMul", (PyCFunction)SigTo_setMul, METH_O, "Sets SigTo mul factor."},
626 {"setAdd", (PyCFunction)SigTo_setAdd, METH_O, "Sets SigTo add factor."},
627 {"setSub", (PyCFunction)SigTo_setSub, METH_O, "Sets inverse add factor."},
628 {"setDiv", (PyCFunction)SigTo_setDiv, METH_O, "Sets inverse mul factor."},
629 {NULL} /* Sentinel */
630 };
631
632 static PyNumberMethods SigTo_as_number =
633 {
634 (binaryfunc)SigTo_add, /*nb_add*/
635 (binaryfunc)SigTo_sub, /*nb_subtract*/
636 (binaryfunc)SigTo_multiply, /*nb_multiply*/
637 0, /*nb_remainder*/
638 0, /*nb_divmod*/
639 0, /*nb_power*/
640 0, /*nb_neg*/
641 0, /*nb_pos*/
642 0, /*(unaryfunc)array_abs,*/
643 0, /*nb_nonzero*/
644 0, /*nb_invert*/
645 0, /*nb_lshift*/
646 0, /*nb_rshift*/
647 0, /*nb_and*/
648 0, /*nb_xor*/
649 0, /*nb_or*/
650 0, /*nb_int*/
651 0, /*nb_long*/
652 0, /*nb_float*/
653 (binaryfunc)SigTo_inplace_add, /*inplace_add*/
654 (binaryfunc)SigTo_inplace_sub, /*inplace_subtract*/
655 (binaryfunc)SigTo_inplace_multiply, /*inplace_multiply*/
656 0, /*inplace_remainder*/
657 0, /*inplace_power*/
658 0, /*inplace_lshift*/
659 0, /*inplace_rshift*/
660 0, /*inplace_and*/
661 0, /*inplace_xor*/
662 0, /*inplace_or*/
663 0, /*nb_floor_divide*/
664 (binaryfunc)SigTo_div, /*nb_true_divide*/
665 0, /*nb_inplace_floor_divide*/
666 (binaryfunc)SigTo_inplace_div, /*nb_inplace_true_divide*/
667 0, /* nb_index */
668 };
669
670 PyTypeObject SigToType =
671 {
672 PyVarObject_HEAD_INIT(NULL, 0)
673 "_pyo.SigTo_base", /*tp_name*/
674 sizeof(SigTo), /*tp_basicsize*/
675 0, /*tp_itemsize*/
676 (destructor)SigTo_dealloc, /*tp_dealloc*/
677 0, /*tp_print*/
678 0, /*tp_getattr*/
679 0, /*tp_setattr*/
680 0, /*tp_as_async (tp_compare in Python 2)*/
681 0, /*tp_repr*/
682 &SigTo_as_number, /*tp_as_number*/
683 0, /*tp_as_sequence*/
684 0, /*tp_as_mapping*/
685 0, /*tp_hash */
686 0, /*tp_call*/
687 0, /*tp_str*/
688 0, /*tp_getattro*/
689 0, /*tp_setattro*/
690 0, /*tp_as_buffer*/
691 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
692 "SigTo objects. Converts number into a signal stream and apply a ramp from last value.", /* tp_doc */
693 (traverseproc)SigTo_traverse, /* tp_traverse */
694 (inquiry)SigTo_clear, /* tp_clear */
695 0, /* tp_richcompare */
696 0, /* tp_weaklistoffset */
697 0, /* tp_iter */
698 0, /* tp_iternext */
699 SigTo_methods, /* tp_methods */
700 SigTo_members, /* tp_members */
701 0, /* tp_getset */
702 0, /* tp_base */
703 0, /* tp_dict */
704 0, /* tp_descr_get */
705 0, /* tp_descr_set */
706 0, /* tp_dictoffset */
707 0, /* tp_init */
708 0, /* tp_alloc */
709 SigTo_new, /* tp_new */
710 };
711
712 /***************************/
713 /* VarPort - Sig + ramp time + portamento + callback */
714 /***************************/
715 typedef struct
716 {
717 pyo_audio_HEAD
718 PyObject *callable;
719 PyObject *arg;
720 MYFLT value;
721 MYFLT time;
722 MYFLT lastTime;
723 MYFLT lastValue;
724 MYFLT currentValue;
725 long timeStep;
726 long timeout;
727 MYFLT stepVal;
728 long timeCount;
729 int modebuffer[2];
730 int flag;
731 } VarPort;
732
733 static void
734 VarPort_generates_i(VarPort *self)
735 {
736 int i;
737 PyObject *tuple, *result;
738
739 if (self->value != self->lastValue || self->time != self->lastTime)
740 {
741 self->flag = 1;
742 self->timeCount = 0;
743 self->stepVal = (self->value - self->currentValue) / (self->timeStep + 1);
744 self->lastValue = self->value;
745 self->lastTime = self->time;
746 }
747
748 if (self->flag == 1)
749 {
750 for (i = 0; i < self->bufsize; i++)
751 {
752 if (self->timeCount >= self->timeStep)
753 self->currentValue = self->value;
754 else if (self->timeCount < self->timeStep)
755 {
756 self->currentValue += self->stepVal;
757 }
758
759 self->timeCount++;
760 self->data[i] = self->currentValue;
761 }
762 }
763 else
764 {
765 for (i = 0; i < self->bufsize; i++)
766 {
767 self->data[i] = self->currentValue;
768 }
769 }
770
771 if (self->timeCount >= self->timeout && self->flag == 1)
772 {
773 self->flag = 0;
774
775 if (self->callable != Py_None)
776 {
777 if (self->arg != Py_None)
778 {
779 tuple = PyTuple_New(1);
780 PyTuple_SET_ITEM(tuple, 0, self->arg);
781 }
782 else
783 {
784 tuple = PyTuple_New(0);
785 }
786
787 result = PyObject_Call(self->callable, tuple, NULL);
788
789 if (result == NULL)
790 {
791 PyErr_Print();
792 return;
793 }
794 }
795 }
796 }
797
798 static void VarPort_postprocessing_ii(VarPort *self) { POST_PROCESSING_II };
799 static void VarPort_postprocessing_ai(VarPort *self) { POST_PROCESSING_AI };
800 static void VarPort_postprocessing_ia(VarPort *self) { POST_PROCESSING_IA };
801 static void VarPort_postprocessing_aa(VarPort *self) { POST_PROCESSING_AA };
802 static void VarPort_postprocessing_ireva(VarPort *self) { POST_PROCESSING_IREVA };
803 static void VarPort_postprocessing_areva(VarPort *self) { POST_PROCESSING_AREVA };
804 static void VarPort_postprocessing_revai(VarPort *self) { POST_PROCESSING_REVAI };
805 static void VarPort_postprocessing_revaa(VarPort *self) { POST_PROCESSING_REVAA };
806 static void VarPort_postprocessing_revareva(VarPort *self) { POST_PROCESSING_REVAREVA };
807
808 static void
809 VarPort_setProcMode(VarPort *self)
810 {
811 int muladdmode;
812 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
813
814 self->proc_func_ptr = VarPort_generates_i;
815
816 switch (muladdmode)
817 {
818 case 0:
819 self->muladd_func_ptr = VarPort_postprocessing_ii;
820 break;
821
822 case 1:
823 self->muladd_func_ptr = VarPort_postprocessing_ai;
824 break;
825
826 case 2:
827 self->muladd_func_ptr = VarPort_postprocessing_revai;
828 break;
829
830 case 10:
831 self->muladd_func_ptr = VarPort_postprocessing_ia;
832 break;
833
834 case 11:
835 self->muladd_func_ptr = VarPort_postprocessing_aa;
836 break;
837
838 case 12:
839 self->muladd_func_ptr = VarPort_postprocessing_revaa;
840 break;
841
842 case 20:
843 self->muladd_func_ptr = VarPort_postprocessing_ireva;
844 break;
845
846 case 21:
847 self->muladd_func_ptr = VarPort_postprocessing_areva;
848 break;
849
850 case 22:
851 self->muladd_func_ptr = VarPort_postprocessing_revareva;
852 break;
853 }
854 }
855
856 static void
857 VarPort_compute_next_data_frame(VarPort *self)
858 {
859 (*self->proc_func_ptr)(self);
860 (*self->muladd_func_ptr)(self);
861 }
862
863 static int
864 VarPort_traverse(VarPort *self, visitproc visit, void *arg)
865 {
866 pyo_VISIT
867 Py_VISIT(self->callable);
868 Py_VISIT(self->arg);
869 return 0;
870 }
871
872 static int
873 VarPort_clear(VarPort *self)
874 {
875 pyo_CLEAR
876 Py_CLEAR(self->callable);
877 Py_CLEAR(self->arg);
878 return 0;
879 }
880
881 static void
882 VarPort_dealloc(VarPort* self)
883 {
884 pyo_DEALLOC
885 VarPort_clear(self);
886 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
887 Py_TYPE(self)->tp_free((PyObject*)self);
888 }
889
890 static PyObject *
891 VarPort_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
892 {
893 int i;
894 MYFLT inittmp = 0.0;
895 PyObject *valuetmp = NULL, *timetmp = NULL, *calltmp = NULL, *argtmp = NULL, *multmp = NULL, *addtmp = NULL;
896 VarPort *self;
897 self = (VarPort *)type->tp_alloc(type, 0);
898
899 self->flag = 1;
900 self->time = self->lastTime = 0.025;
901 self->timeStep = (long)(self->time * self->sr);
902 self->timeout = (long)((self->time + 0.1) * self->sr);
903 self->timeCount = 0;
904 self->stepVal = 0.0;
905 self->modebuffer[0] = 0;
906 self->modebuffer[1] = 0;
907
908 self->callable = Py_None;
909 self->arg = Py_None;
910
911 INIT_OBJECT_COMMON
912 Stream_setFunctionPtr(self->stream, VarPort_compute_next_data_frame);
913 self->mode_func_ptr = VarPort_setProcMode;
914
915 static char *kwlist[] = {"value", "time", "init", "callable", "arg", "mul", "add", NULL};
916
917 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_OFOOOO, kwlist, &valuetmp, &timetmp, &inittmp, &calltmp, &argtmp, &multmp, &addtmp))
918 Py_RETURN_NONE;
919
920 if (valuetmp)
921 {
922 PyObject_CallMethod((PyObject *)self, "setValue", "O", valuetmp);
923 }
924
925 if (timetmp)
926 {
927 PyObject_CallMethod((PyObject *)self, "setTime", "O", timetmp);
928 }
929
930 if (multmp)
931 {
932 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
933 }
934
935 if (addtmp)
936 {
937 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
938 }
939
940 if (calltmp)
941 {
942 Py_DECREF(self->callable);
943 Py_INCREF(calltmp);
944 self->callable = calltmp;
945 }
946
947 if (argtmp)
948 {
949 Py_DECREF(self->arg);
950 Py_INCREF(argtmp);
951 self->arg = argtmp;
952 }
953
954 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
955
956 self->lastValue = self->currentValue = inittmp;
957
958 (*self->mode_func_ptr)(self);
959
960 for (i = 0; i < self->bufsize; i++)
961 {
962 self->data[i] = self->currentValue;
963 }
964
965 return (PyObject *)self;
966 }
967
968 static PyObject *
969 VarPort_setValue(VarPort *self, PyObject *arg)
970 {
971 ASSERT_ARG_NOT_NULL
972
973 if (PyNumber_Check(arg))
974 self->value = PyFloat_AsDouble(arg);
975 else
976 self->value = self->lastValue;
977
978 Py_RETURN_NONE;
979 }
980
981 static PyObject *
982 VarPort_setTime(VarPort *self, PyObject *arg)
983 {
984 ASSERT_ARG_NOT_NULL
985
986 if (PyNumber_Check(arg))
987 {
988 self->time = PyFloat_AsDouble(arg);
989 self->timeStep = (long)(self->time * self->sr);
990 self->timeout = (long)((self->time + 0.1) * self->sr);
991 }
992
993 Py_RETURN_NONE;
994 }
995
996 static PyObject *
997 VarPort_setFunction(VarPort *self, PyObject *arg)
998 {
999 if (! PyCallable_Check(arg))
1000 {
1001 PyErr_SetString(PyExc_TypeError, "The function attribute must be callable.");
1002 Py_RETURN_NONE;
1003 }
1004
1005 Py_XDECREF(self->callable);
1006 self->callable = arg;
1007 Py_INCREF(self->callable);
1008
1009 Py_RETURN_NONE;
1010 }
1011
1012 static PyObject * VarPort_getServer(VarPort* self) { GET_SERVER };
1013 static PyObject * VarPort_getStream(VarPort* self) { GET_STREAM };
1014 static PyObject * VarPort_setMul(VarPort *self, PyObject *arg) { SET_MUL };
1015 static PyObject * VarPort_setAdd(VarPort *self, PyObject *arg) { SET_ADD };
1016 static PyObject * VarPort_setSub(VarPort *self, PyObject *arg) { SET_SUB };
1017 static PyObject * VarPort_setDiv(VarPort *self, PyObject *arg) { SET_DIV };
1018
1019 static PyObject * VarPort_play(VarPort *self, PyObject *args, PyObject *kwds) { PLAY };
1020 static PyObject * VarPort_stop(VarPort *self, PyObject *args, PyObject *kwds) { STOP };
1021
1022 static PyObject * VarPort_multiply(VarPort *self, PyObject *arg) { MULTIPLY };
1023 static PyObject * VarPort_inplace_multiply(VarPort *self, PyObject *arg) { INPLACE_MULTIPLY };
1024 static PyObject * VarPort_add(VarPort *self, PyObject *arg) { ADD };
1025 static PyObject * VarPort_inplace_add(VarPort *self, PyObject *arg) { INPLACE_ADD };
1026 static PyObject * VarPort_sub(VarPort *self, PyObject *arg) { SUB };
1027 static PyObject * VarPort_inplace_sub(VarPort *self, PyObject *arg) { INPLACE_SUB };
1028 static PyObject * VarPort_div(VarPort *self, PyObject *arg) { DIV };
1029 static PyObject * VarPort_inplace_div(VarPort *self, PyObject *arg) { INPLACE_DIV };
1030
1031 static PyMemberDef VarPort_members[] =
1032 {
1033 {"server", T_OBJECT_EX, offsetof(VarPort, server), 0, "Pyo server."},
1034 {"stream", T_OBJECT_EX, offsetof(VarPort, stream), 0, "Stream object."},
1035 {"mul", T_OBJECT_EX, offsetof(VarPort, mul), 0, "Mul factor."},
1036 {"add", T_OBJECT_EX, offsetof(VarPort, add), 0, "Add factor."},
1037 {NULL} /* Sentinel */
1038 };
1039
1040 static PyMethodDef VarPort_methods[] =
1041 {
1042 {"getServer", (PyCFunction)VarPort_getServer, METH_NOARGS, "Returns server object."},
1043 {"_getStream", (PyCFunction)VarPort_getStream, METH_NOARGS, "Returns stream object."},
1044 {"play", (PyCFunction)VarPort_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1045 {"stop", (PyCFunction)VarPort_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1046 {"setValue", (PyCFunction)VarPort_setValue, METH_O, "Sets VarPort value."},
1047 {"setTime", (PyCFunction)VarPort_setTime, METH_O, "Sets ramp time in seconds."},
1048 {"setFunction", (PyCFunction)VarPort_setFunction, METH_O, "Sets function to be called."},
1049 {"setMul", (PyCFunction)VarPort_setMul, METH_O, "Sets VarPort mul factor."},
1050 {"setAdd", (PyCFunction)VarPort_setAdd, METH_O, "Sets VarPort add factor."},
1051 {"setSub", (PyCFunction)VarPort_setSub, METH_O, "Sets inverse add factor."},
1052 {"setDiv", (PyCFunction)VarPort_setDiv, METH_O, "Sets inverse mul factor."},
1053 {NULL} /* Sentinel */
1054 };
1055
1056 static PyNumberMethods VarPort_as_number =
1057 {
1058 (binaryfunc)VarPort_add, /*nb_add*/
1059 (binaryfunc)VarPort_sub, /*nb_subtract*/
1060 (binaryfunc)VarPort_multiply, /*nb_multiply*/
1061 0, /*nb_remainder*/
1062 0, /*nb_divmod*/
1063 0, /*nb_power*/
1064 0, /*nb_neg*/
1065 0, /*nb_pos*/
1066 0, /*(unaryfunc)array_abs,*/
1067 0, /*nb_nonzero*/
1068 0, /*nb_invert*/
1069 0, /*nb_lshift*/
1070 0, /*nb_rshift*/
1071 0, /*nb_and*/
1072 0, /*nb_xor*/
1073 0, /*nb_or*/
1074 0, /*nb_int*/
1075 0, /*nb_long*/
1076 0, /*nb_float*/
1077 (binaryfunc)VarPort_inplace_add, /*inplace_add*/
1078 (binaryfunc)VarPort_inplace_sub, /*inplace_subtract*/
1079 (binaryfunc)VarPort_inplace_multiply, /*inplace_multiply*/
1080 0, /*inplace_remainder*/
1081 0, /*inplace_power*/
1082 0, /*inplace_lshift*/
1083 0, /*inplace_rshift*/
1084 0, /*inplace_and*/
1085 0, /*inplace_xor*/
1086 0, /*inplace_or*/
1087 0, /*nb_floor_divide*/
1088 (binaryfunc)VarPort_div, /*nb_true_divide*/
1089 0, /*nb_inplace_floor_divide*/
1090 (binaryfunc)VarPort_inplace_div, /*nb_inplace_true_divide*/
1091 0, /* nb_index */
1092 };
1093
1094 PyTypeObject VarPortType =
1095 {
1096 PyVarObject_HEAD_INIT(NULL, 0)
1097 "_pyo.VarPort_base", /*tp_name*/
1098 sizeof(VarPort), /*tp_basicsize*/
1099 0, /*tp_itemsize*/
1100 (destructor)VarPort_dealloc, /*tp_dealloc*/
1101 0, /*tp_print*/
1102 0, /*tp_getattr*/
1103 0, /*tp_setattr*/
1104 0, /*tp_as_async (tp_compare in Python 2)*/
1105 0, /*tp_repr*/
1106 &VarPort_as_number, /*tp_as_number*/
1107 0, /*tp_as_sequence*/
1108 0, /*tp_as_mapping*/
1109 0, /*tp_hash */
1110 0, /*tp_call*/
1111 0, /*tp_str*/
1112 0, /*tp_getattro*/
1113 0, /*tp_setattro*/
1114 0, /*tp_as_buffer*/
1115 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1116 "VarPort objects. Converts number into a signal stream and apply a ramp from last value.", /* tp_doc */
1117 (traverseproc)VarPort_traverse, /* tp_traverse */
1118 (inquiry)VarPort_clear, /* tp_clear */
1119 0, /* tp_richcompare */
1120 0, /* tp_weaklistoffset */
1121 0, /* tp_iter */
1122 0, /* tp_iternext */
1123 VarPort_methods, /* tp_methods */
1124 VarPort_members, /* tp_members */
1125 0, /* tp_getset */
1126 0, /* tp_base */
1127 0, /* tp_dict */
1128 0, /* tp_descr_get */
1129 0, /* tp_descr_set */
1130 0, /* tp_dictoffset */
1131 0, /* tp_init */
1132 0, /* tp_alloc */
1133 VarPort_new, /* tp_new */
1134 };