Debian Codegraph @

src/objects/distomodule.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 <math.h>
24 #include "pyomodule.h"
25 #include "streammodule.h"
26 #include "servermodule.h"
27 #include "dummymodule.h"
28
29 typedef struct
30 {
31 pyo_audio_HEAD
32 PyObject *input;
33 Stream *input_stream;
34 PyObject *drive;
35 Stream *drive_stream;
36 PyObject *slope;
37 Stream *slope_stream;
38 int init;
39 int modebuffer[4];
40 MYFLT y1; // sample memory
41 } Disto;
42
43 /* old version (prior to 0.8.0) with atan2
44 static MYFLT _clip(MYFLT x) {
45 if (x < 0)
46 return 0;
47 else if (x > 1)
48 return 1;
49 else
50 return x;
51 }
52
53 static void
54 Disto_transform_ii(Disto *self) {
55 MYFLT val, coeff;
56 int i;
57 MYFLT *in = Stream_getData((Stream *)self->input_stream);
58
59 MYFLT drv = .4 - _clip(PyFloat_AS_DOUBLE(self->drive)) * .3999;
60 MYFLT slp = _clip(PyFloat_AS_DOUBLE(self->slope));
61
62 for (i=0; i<self->bufsize; i++) {
63 val = MYATAN2(in[i], drv);
64 self->data[i] = val;
65 }
66 coeff = 1.0 - slp;
67 for (i=0; i<self->bufsize; i++) {
68 val = self->data[i] * coeff + self->y1 * slp;
69 self->y1 = val;
70 self->data[i] = val;
71 }
72 }
73 */
74 static void
75 Disto_transform_ii(Disto *self)
76 {
77 int i;
78 MYFLT val;
79 MYFLT *in = Stream_getData((Stream *)self->input_stream);
80 MYFLT drv = PyFloat_AS_DOUBLE(self->drive);
81 MYFLT slp = PyFloat_AS_DOUBLE(self->slope);
82
83 drv = (drv < 0.0) ? 0.0 : (drv > 0.998) ? 0.998 : drv;
84 drv = (2.0 * drv) / (1 - drv);
85 slp = (slp < 0.0) ? 0.0 : (slp > 0.999) ? 0.999 : slp;
86
87 for (i = 0; i < self->bufsize; i++)
88 {
89 val = (1 + drv) * in[i] / (1 + drv * MYFABS(in[i]));
90 self->data[i] = self->y1 = val + (self->y1 - val) * slp;
91 }
92 }
93
94 static void
95 Disto_transform_ai(Disto *self)
96 {
97 int i;
98 MYFLT val, drv;
99 MYFLT *in = Stream_getData((Stream *)self->input_stream);
100 MYFLT *drive = Stream_getData((Stream *)self->drive_stream);
101 MYFLT slp = PyFloat_AS_DOUBLE(self->slope);
102 slp = (slp < 0.0) ? 0.0 : (slp > 0.999) ? 0.999 : slp;
103
104 for (i = 0; i < self->bufsize; i++)
105 {
106 drv = drive[i];
107 drv = (drv < 0.0) ? 0.0 : (drv > 0.998) ? 0.998 : drv;
108 drv = (2.0 * drv) / (1 - drv);
109 val = (1 + drv) * in[i] / (1 + drv * MYFABS(in[i]));
110 self->data[i] = self->y1 = val + (self->y1 - val) * slp;
111 }
112 }
113
114 static void
115 Disto_transform_ia(Disto *self)
116 {
117 int i;
118 MYFLT val, slp;
119 MYFLT *in = Stream_getData((Stream *)self->input_stream);
120 MYFLT drv = PyFloat_AS_DOUBLE(self->drive);
121 MYFLT *slope = Stream_getData((Stream *)self->slope_stream);
122
123 drv = (drv < 0.0) ? 0.0 : (drv > 0.998) ? 0.998 : drv;
124 drv = (2.0 * drv) / (1 - drv);
125
126 for (i = 0; i < self->bufsize; i++)
127 {
128 slp = slope[i];
129 slp = (slp < 0.0) ? 0.0 : (slp > 0.999) ? 0.999 : slp;
130 val = (1 + drv) * in[i] / (1 + drv * MYFABS(in[i]));
131 self->data[i] = self->y1 = val + (self->y1 - val) * slp;
132 }
133 }
134
135 static void
136 Disto_transform_aa(Disto *self)
137 {
138 int i;
139 MYFLT val, drv, slp;
140 MYFLT *in = Stream_getData((Stream *)self->input_stream);
141 MYFLT *drive = Stream_getData((Stream *)self->drive_stream);
142 MYFLT *slope = Stream_getData((Stream *)self->slope_stream);
143
144 for (i = 0; i < self->bufsize; i++)
145 {
146 drv = drive[i];
147 drv = (drv < 0.0) ? 0.0 : (drv > 0.998) ? 0.998 : drv;
148 slp = slope[i];
149 slp = (slp < 0.0) ? 0.0 : (slp > 0.999) ? 0.999 : slp;
150 drv = (2.0 * drv) / (1 - drv);
151 val = (1 + drv) * in[i] / (1 + drv * MYFABS(in[i]));
152 self->data[i] = self->y1 = val + (self->y1 - val) * slp;
153 }
154 }
155
156 static void Disto_postprocessing_ii(Disto *self) { POST_PROCESSING_II };
157 static void Disto_postprocessing_ai(Disto *self) { POST_PROCESSING_AI };
158 static void Disto_postprocessing_ia(Disto *self) { POST_PROCESSING_IA };
159 static void Disto_postprocessing_aa(Disto *self) { POST_PROCESSING_AA };
160 static void Disto_postprocessing_ireva(Disto *self) { POST_PROCESSING_IREVA };
161 static void Disto_postprocessing_areva(Disto *self) { POST_PROCESSING_AREVA };
162 static void Disto_postprocessing_revai(Disto *self) { POST_PROCESSING_REVAI };
163 static void Disto_postprocessing_revaa(Disto *self) { POST_PROCESSING_REVAA };
164 static void Disto_postprocessing_revareva(Disto *self) { POST_PROCESSING_REVAREVA };
165
166 static void
167 Disto_setProcMode(Disto *self)
168 {
169 int procmode, muladdmode;
170 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
171 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
172
173 switch (procmode)
174 {
175 case 0:
176 self->proc_func_ptr = Disto_transform_ii;
177 break;
178
179 case 1:
180 self->proc_func_ptr = Disto_transform_ai;
181 break;
182
183 case 10:
184 self->proc_func_ptr = Disto_transform_ia;
185 break;
186
187 case 11:
188 self->proc_func_ptr = Disto_transform_aa;
189 break;
190 }
191
192 switch (muladdmode)
193 {
194 case 0:
195 self->muladd_func_ptr = Disto_postprocessing_ii;
196 break;
197
198 case 1:
199 self->muladd_func_ptr = Disto_postprocessing_ai;
200 break;
201
202 case 2:
203 self->muladd_func_ptr = Disto_postprocessing_revai;
204 break;
205
206 case 10:
207 self->muladd_func_ptr = Disto_postprocessing_ia;
208 break;
209
210 case 11:
211 self->muladd_func_ptr = Disto_postprocessing_aa;
212 break;
213
214 case 12:
215 self->muladd_func_ptr = Disto_postprocessing_revaa;
216 break;
217
218 case 20:
219 self->muladd_func_ptr = Disto_postprocessing_ireva;
220 break;
221
222 case 21:
223 self->muladd_func_ptr = Disto_postprocessing_areva;
224 break;
225
226 case 22:
227 self->muladd_func_ptr = Disto_postprocessing_revareva;
228 break;
229 }
230 }
231
232 static void
233 Disto_compute_next_data_frame(Disto *self)
234 {
235 (*self->proc_func_ptr)(self);
236 (*self->muladd_func_ptr)(self);
237 }
238
239 static int
240 Disto_traverse(Disto *self, visitproc visit, void *arg)
241 {
242 pyo_VISIT
243 Py_VISIT(self->input);
244 Py_VISIT(self->drive);
245 Py_VISIT(self->slope);
246 return 0;
247 }
248
249 static int
250 Disto_clear(Disto *self)
251 {
252 pyo_CLEAR
253 Py_CLEAR(self->input);
254 Py_CLEAR(self->drive);
255 Py_CLEAR(self->slope);
256 return 0;
257 }
258
259 static void
260 Disto_dealloc(Disto* self)
261 {
262 pyo_DEALLOC
263 Disto_clear(self);
264 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
265 Py_TYPE(self)->tp_free((PyObject*)self);
266 }
267
268 static PyObject *
269 Disto_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
270 {
271 int i;
272 PyObject *inputtmp, *input_streamtmp, *drivetmp = NULL, *slopetmp = NULL, *multmp = NULL, *addtmp = NULL;
273 Disto *self;
274 self = (Disto *)type->tp_alloc(type, 0);
275
276 self->drive = PyFloat_FromDouble(.75);
277 self->slope = PyFloat_FromDouble(.5);
278 self->modebuffer[0] = 0;
279 self->modebuffer[1] = 0;
280 self->modebuffer[2] = 0;
281 self->modebuffer[3] = 0;
282 self->y1 = 0;
283
284 INIT_OBJECT_COMMON
285 Stream_setFunctionPtr(self->stream, Disto_compute_next_data_frame);
286 self->mode_func_ptr = Disto_setProcMode;
287
288 static char *kwlist[] = {"input", "drive", "slope", "mul", "add", NULL};
289
290 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &drivetmp, &slopetmp, &multmp, &addtmp))
291 Py_RETURN_NONE;
292
293 INIT_INPUT_STREAM
294
295 if (drivetmp)
296 {
297 PyObject_CallMethod((PyObject *)self, "setDrive", "O", drivetmp);
298 }
299
300 if (slopetmp)
301 {
302 PyObject_CallMethod((PyObject *)self, "setSlope", "O", slopetmp);
303 }
304
305 if (multmp)
306 {
307 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
308 }
309
310 if (addtmp)
311 {
312 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
313 }
314
315 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
316
317 (*self->mode_func_ptr)(self);
318
319 return (PyObject *)self;
320 }
321
322 static PyObject * Disto_getServer(Disto* self) { GET_SERVER };
323 static PyObject * Disto_getStream(Disto* self) { GET_STREAM };
324 static PyObject * Disto_setMul(Disto *self, PyObject *arg) { SET_MUL };
325 static PyObject * Disto_setAdd(Disto *self, PyObject *arg) { SET_ADD };
326 static PyObject * Disto_setSub(Disto *self, PyObject *arg) { SET_SUB };
327 static PyObject * Disto_setDiv(Disto *self, PyObject *arg) { SET_DIV };
328
329 static PyObject * Disto_play(Disto *self, PyObject *args, PyObject *kwds) { PLAY };
330 static PyObject * Disto_out(Disto *self, PyObject *args, PyObject *kwds) { OUT };
331 static PyObject * Disto_stop(Disto *self, PyObject *args, PyObject *kwds) { STOP };
332
333 static PyObject * Disto_multiply(Disto *self, PyObject *arg) { MULTIPLY };
334 static PyObject * Disto_inplace_multiply(Disto *self, PyObject *arg) { INPLACE_MULTIPLY };
335 static PyObject * Disto_add(Disto *self, PyObject *arg) { ADD };
336 static PyObject * Disto_inplace_add(Disto *self, PyObject *arg) { INPLACE_ADD };
337 static PyObject * Disto_sub(Disto *self, PyObject *arg) { SUB };
338 static PyObject * Disto_inplace_sub(Disto *self, PyObject *arg) { INPLACE_SUB };
339 static PyObject * Disto_div(Disto *self, PyObject *arg) { DIV };
340 static PyObject * Disto_inplace_div(Disto *self, PyObject *arg) { INPLACE_DIV };
341
342 static PyObject * Disto_setDrive(Disto *self, PyObject *arg) { SET_PARAM(self->drive, self->drive_stream, 2); }
343 static PyObject * Disto_setSlope(Disto *self, PyObject *arg) { SET_PARAM(self->slope, self->slope_stream, 3); }
344
345 static PyMemberDef Disto_members[] =
346 {
347 {"server", T_OBJECT_EX, offsetof(Disto, server), 0, "Pyo server."},
348 {"stream", T_OBJECT_EX, offsetof(Disto, stream), 0, "Stream object."},
349 {"input", T_OBJECT_EX, offsetof(Disto, input), 0, "Input sound object."},
350 {"drive", T_OBJECT_EX, offsetof(Disto, drive), 0, "Cutoff driveuency in cycle per second."},
351 {"slope", T_OBJECT_EX, offsetof(Disto, slope), 0, "Lowpass filter slope factor."},
352 {"mul", T_OBJECT_EX, offsetof(Disto, mul), 0, "Mul factor."},
353 {"add", T_OBJECT_EX, offsetof(Disto, add), 0, "Add factor."},
354 {NULL} /* Sentinel */
355 };
356
357 static PyMethodDef Disto_methods[] =
358 {
359 {"getServer", (PyCFunction)Disto_getServer, METH_NOARGS, "Returns server object."},
360 {"_getStream", (PyCFunction)Disto_getStream, METH_NOARGS, "Returns stream object."},
361 {"play", (PyCFunction)Disto_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
362 {"out", (PyCFunction)Disto_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
363 {"stop", (PyCFunction)Disto_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
364 {"setDrive", (PyCFunction)Disto_setDrive, METH_O, "Sets distortion drive factor (0 -> 1)."},
365 {"setSlope", (PyCFunction)Disto_setSlope, METH_O, "Sets lowpass filter slope factor."},
366 {"setMul", (PyCFunction)Disto_setMul, METH_O, "Sets oscillator mul factor."},
367 {"setAdd", (PyCFunction)Disto_setAdd, METH_O, "Sets oscillator add factor."},
368 {"setSub", (PyCFunction)Disto_setSub, METH_O, "Sets inverse add factor."},
369 {"setDiv", (PyCFunction)Disto_setDiv, METH_O, "Sets inverse mul factor."},
370 {NULL} /* Sentinel */
371 };
372
373 static PyNumberMethods Disto_as_number =
374 {
375 (binaryfunc)Disto_add, /*nb_add*/
376 (binaryfunc)Disto_sub, /*nb_subtract*/
377 (binaryfunc)Disto_multiply, /*nb_multiply*/
378 0, /*nb_remainder*/
379 0, /*nb_divmod*/
380 0, /*nb_power*/
381 0, /*nb_neg*/
382 0, /*nb_pos*/
383 0, /*(unaryfunc)array_abs,*/
384 0, /*nb_nonzero*/
385 0, /*nb_invert*/
386 0, /*nb_lshift*/
387 0, /*nb_rshift*/
388 0, /*nb_and*/
389 0, /*nb_xor*/
390 0, /*nb_or*/
391 0, /*nb_int*/
392 0, /*nb_long*/
393 0, /*nb_float*/
394 (binaryfunc)Disto_inplace_add, /*inplace_add*/
395 (binaryfunc)Disto_inplace_sub, /*inplace_subtract*/
396 (binaryfunc)Disto_inplace_multiply, /*inplace_multiply*/
397 0, /*inplace_remainder*/
398 0, /*inplace_power*/
399 0, /*inplace_lshift*/
400 0, /*inplace_rshift*/
401 0, /*inplace_and*/
402 0, /*inplace_xor*/
403 0, /*inplace_or*/
404 0, /*nb_floor_divide*/
405 (binaryfunc)Disto_div, /*nb_true_divide*/
406 0, /*nb_inplace_floor_divide*/
407 (binaryfunc)Disto_inplace_div, /*nb_inplace_true_divide*/
408 0, /* nb_index */
409 };
410
411 PyTypeObject DistoType =
412 {
413 PyVarObject_HEAD_INIT(NULL, 0)
414 "_pyo.Disto_base", /*tp_name*/
415 sizeof(Disto), /*tp_basicsize*/
416 0, /*tp_itemsize*/
417 (destructor)Disto_dealloc, /*tp_dealloc*/
418 0, /*tp_print*/
419 0, /*tp_getattr*/
420 0, /*tp_setattr*/
421 0, /*tp_as_async (tp_compare in Python 2)*/
422 0, /*tp_repr*/
423 &Disto_as_number, /*tp_as_number*/
424 0, /*tp_as_sequence*/
425 0, /*tp_as_mapping*/
426 0, /*tp_hash */
427 0, /*tp_call*/
428 0, /*tp_str*/
429 0, /*tp_getattro*/
430 0, /*tp_setattro*/
431 0, /*tp_as_buffer*/
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
433 "Disto objects. Arctan distortion.", /* tp_doc */
434 (traverseproc)Disto_traverse, /* tp_traverse */
435 (inquiry)Disto_clear, /* tp_clear */
436 0, /* tp_richcompare */
437 0, /* tp_weaklistoffset */
438 0, /* tp_iter */
439 0, /* tp_iternext */
440 Disto_methods, /* tp_methods */
441 Disto_members, /* tp_members */
442 0, /* tp_getset */
443 0, /* tp_base */
444 0, /* tp_dict */
445 0, /* tp_descr_get */
446 0, /* tp_descr_set */
447 0, /* tp_dictoffset */
448 0, /* tp_init */
449 0, /* tp_alloc */
450 Disto_new, /* tp_new */
451 };
452
453 /*****************/
454 /** Clip object **/
455 /*****************/
456
457 typedef struct
458 {
459 pyo_audio_HEAD
460 PyObject *input;
461 Stream *input_stream;
462 PyObject *min;
463 Stream *min_stream;
464 PyObject *max;
465 Stream *max_stream;
466 int modebuffer[4];
467 } Clip;
468
469 static void
470 Clip_transform_ii(Clip *self)
471 {
472 MYFLT val;
473 int i;
474 MYFLT *in = Stream_getData((Stream *)self->input_stream);
475 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
476 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
477
478 for (i = 0; i < self->bufsize; i++)
479 {
480 val = in[i];
481
482 if(val < mi)
483 self->data[i] = mi;
484 else if(val > ma)
485 self->data[i] = ma;
486 else
487 self->data[i] = val;
488 }
489 }
490
491 static void
492 Clip_transform_ai(Clip *self)
493 {
494 MYFLT val, mini;
495 int i;
496 MYFLT *in = Stream_getData((Stream *)self->input_stream);
497 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
498 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
499
500 for (i = 0; i < self->bufsize; i++)
501 {
502 val = in[i];
503 mini = mi[i];
504
505 if(val < mini)
506 self->data[i] = mini;
507 else if(val > ma)
508 self->data[i] = ma;
509 else
510 self->data[i] = val;
511 }
512 }
513
514 static void
515 Clip_transform_ia(Clip *self)
516 {
517 MYFLT val, maxi;
518 int i;
519 MYFLT *in = Stream_getData((Stream *)self->input_stream);
520 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
521 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
522
523 for (i = 0; i < self->bufsize; i++)
524 {
525 val = in[i];
526 maxi = ma[i];
527
528 if(val < mi)
529 self->data[i] = mi;
530 else if(val > maxi)
531 self->data[i] = maxi;
532 else
533 self->data[i] = val;
534 }
535 }
536
537 static void
538 Clip_transform_aa(Clip *self)
539 {
540 MYFLT val, mini, maxi;
541 int i;
542 MYFLT *in = Stream_getData((Stream *)self->input_stream);
543 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
544 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
545
546 for (i = 0; i < self->bufsize; i++)
547 {
548 val = in[i];
549 mini = mi[i];
550 maxi = ma[i];
551
552 if(val < mini)
553 self->data[i] = mini;
554 else if(val > maxi)
555 self->data[i] = maxi;
556 else
557 self->data[i] = val;
558 }
559 }
560
561 static void Clip_postprocessing_ii(Clip *self) { POST_PROCESSING_II };
562 static void Clip_postprocessing_ai(Clip *self) { POST_PROCESSING_AI };
563 static void Clip_postprocessing_ia(Clip *self) { POST_PROCESSING_IA };
564 static void Clip_postprocessing_aa(Clip *self) { POST_PROCESSING_AA };
565 static void Clip_postprocessing_ireva(Clip *self) { POST_PROCESSING_IREVA };
566 static void Clip_postprocessing_areva(Clip *self) { POST_PROCESSING_AREVA };
567 static void Clip_postprocessing_revai(Clip *self) { POST_PROCESSING_REVAI };
568 static void Clip_postprocessing_revaa(Clip *self) { POST_PROCESSING_REVAA };
569 static void Clip_postprocessing_revareva(Clip *self) { POST_PROCESSING_REVAREVA };
570
571 static void
572 Clip_setProcMode(Clip *self)
573 {
574 int procmode, muladdmode;
575 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
576 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
577
578 switch (procmode)
579 {
580 case 0:
581 self->proc_func_ptr = Clip_transform_ii;
582 break;
583
584 case 1:
585 self->proc_func_ptr = Clip_transform_ai;
586 break;
587
588 case 10:
589 self->proc_func_ptr = Clip_transform_ia;
590 break;
591
592 case 11:
593 self->proc_func_ptr = Clip_transform_aa;
594 break;
595 }
596
597 switch (muladdmode)
598 {
599 case 0:
600 self->muladd_func_ptr = Clip_postprocessing_ii;
601 break;
602
603 case 1:
604 self->muladd_func_ptr = Clip_postprocessing_ai;
605 break;
606
607 case 2:
608 self->muladd_func_ptr = Clip_postprocessing_revai;
609 break;
610
611 case 10:
612 self->muladd_func_ptr = Clip_postprocessing_ia;
613 break;
614
615 case 11:
616 self->muladd_func_ptr = Clip_postprocessing_aa;
617 break;
618
619 case 12:
620 self->muladd_func_ptr = Clip_postprocessing_revaa;
621 break;
622
623 case 20:
624 self->muladd_func_ptr = Clip_postprocessing_ireva;
625 break;
626
627 case 21:
628 self->muladd_func_ptr = Clip_postprocessing_areva;
629 break;
630
631 case 22:
632 self->muladd_func_ptr = Clip_postprocessing_revareva;
633 break;
634 }
635 }
636
637 static void
638 Clip_compute_next_data_frame(Clip *self)
639 {
640 (*self->proc_func_ptr)(self);
641 (*self->muladd_func_ptr)(self);
642 }
643
644 static int
645 Clip_traverse(Clip *self, visitproc visit, void *arg)
646 {
647 pyo_VISIT
648 Py_VISIT(self->input);
649 Py_VISIT(self->min);
650 Py_VISIT(self->max);
651 return 0;
652 }
653
654 static int
655 Clip_clear(Clip *self)
656 {
657 pyo_CLEAR
658 Py_CLEAR(self->input);
659 Py_CLEAR(self->min);
660 Py_CLEAR(self->max);
661 return 0;
662 }
663
664 static void
665 Clip_dealloc(Clip* self)
666 {
667 pyo_DEALLOC
668 Clip_clear(self);
669 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
670 Py_TYPE(self)->tp_free((PyObject*)self);
671 }
672
673 static PyObject *
674 Clip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
675 {
676 int i;
677 PyObject *inputtmp, *input_streamtmp, *mintmp = NULL, *maxtmp = NULL, *multmp = NULL, *addtmp = NULL;
678 Clip *self;
679 self = (Clip *)type->tp_alloc(type, 0);
680
681 self->min = PyFloat_FromDouble(-1.0);
682 self->max = PyFloat_FromDouble(1.0);
683 self->modebuffer[0] = 0;
684 self->modebuffer[1] = 0;
685 self->modebuffer[2] = 0;
686 self->modebuffer[3] = 0;
687
688 INIT_OBJECT_COMMON
689 Stream_setFunctionPtr(self->stream, Clip_compute_next_data_frame);
690 self->mode_func_ptr = Clip_setProcMode;
691
692 static char *kwlist[] = {"input", "min", "max", "mul", "add", NULL};
693
694 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &mintmp, &maxtmp, &multmp, &addtmp))
695 Py_RETURN_NONE;
696
697 INIT_INPUT_STREAM
698
699 if (mintmp)
700 {
701 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
702 }
703
704 if (maxtmp)
705 {
706 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
707 }
708
709 if (multmp)
710 {
711 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
712 }
713
714 if (addtmp)
715 {
716 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
717 }
718
719 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
720
721 (*self->mode_func_ptr)(self);
722
723 return (PyObject *)self;
724 }
725
726 static PyObject * Clip_getServer(Clip* self) { GET_SERVER };
727 static PyObject * Clip_getStream(Clip* self) { GET_STREAM };
728 static PyObject * Clip_setMul(Clip *self, PyObject *arg) { SET_MUL };
729 static PyObject * Clip_setAdd(Clip *self, PyObject *arg) { SET_ADD };
730 static PyObject * Clip_setSub(Clip *self, PyObject *arg) { SET_SUB };
731 static PyObject * Clip_setDiv(Clip *self, PyObject *arg) { SET_DIV };
732
733 static PyObject * Clip_play(Clip *self, PyObject *args, PyObject *kwds) { PLAY };
734 static PyObject * Clip_out(Clip *self, PyObject *args, PyObject *kwds) { OUT };
735 static PyObject * Clip_stop(Clip *self, PyObject *args, PyObject *kwds) { STOP };
736
737 static PyObject * Clip_multiply(Clip *self, PyObject *arg) { MULTIPLY };
738 static PyObject * Clip_inplace_multiply(Clip *self, PyObject *arg) { INPLACE_MULTIPLY };
739 static PyObject * Clip_add(Clip *self, PyObject *arg) { ADD };
740 static PyObject * Clip_inplace_add(Clip *self, PyObject *arg) { INPLACE_ADD };
741 static PyObject * Clip_sub(Clip *self, PyObject *arg) { SUB };
742 static PyObject * Clip_inplace_sub(Clip *self, PyObject *arg) { INPLACE_SUB };
743 static PyObject * Clip_div(Clip *self, PyObject *arg) { DIV };
744 static PyObject * Clip_inplace_div(Clip *self, PyObject *arg) { INPLACE_DIV };
745
746 static PyObject * Clip_setMin(Clip *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
747 static PyObject * Clip_setMax(Clip *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
748
749 static PyMemberDef Clip_members[] =
750 {
751 {"server", T_OBJECT_EX, offsetof(Clip, server), 0, "Pyo server."},
752 {"stream", T_OBJECT_EX, offsetof(Clip, stream), 0, "Stream object."},
753 {"input", T_OBJECT_EX, offsetof(Clip, input), 0, "Input sound object."},
754 {"min", T_OBJECT_EX, offsetof(Clip, min), 0, "Minimum possible value."},
755 {"max", T_OBJECT_EX, offsetof(Clip, max), 0, "Maximum possible value."},
756 {"mul", T_OBJECT_EX, offsetof(Clip, mul), 0, "Mul factor."},
757 {"add", T_OBJECT_EX, offsetof(Clip, add), 0, "Add factor."},
758 {NULL} /* Sentinel */
759 };
760
761 static PyMethodDef Clip_methods[] =
762 {
763 {"getServer", (PyCFunction)Clip_getServer, METH_NOARGS, "Returns server object."},
764 {"_getStream", (PyCFunction)Clip_getStream, METH_NOARGS, "Returns stream object."},
765 {"play", (PyCFunction)Clip_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
766 {"out", (PyCFunction)Clip_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
767 {"stop", (PyCFunction)Clip_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
768 {"setMin", (PyCFunction)Clip_setMin, METH_O, "Sets the minimum value."},
769 {"setMax", (PyCFunction)Clip_setMax, METH_O, "Sets the maximum value."},
770 {"setMul", (PyCFunction)Clip_setMul, METH_O, "Sets oscillator mul factor."},
771 {"setAdd", (PyCFunction)Clip_setAdd, METH_O, "Sets oscillator add factor."},
772 {"setSub", (PyCFunction)Clip_setSub, METH_O, "Sets inverse add factor."},
773 {"setDiv", (PyCFunction)Clip_setDiv, METH_O, "Sets inverse mul factor."},
774 {NULL} /* Sentinel */
775 };
776
777 static PyNumberMethods Clip_as_number =
778 {
779 (binaryfunc)Clip_add, /*nb_add*/
780 (binaryfunc)Clip_sub, /*nb_subtract*/
781 (binaryfunc)Clip_multiply, /*nb_multiply*/
782 0, /*nb_remainder*/
783 0, /*nb_divmod*/
784 0, /*nb_power*/
785 0, /*nb_neg*/
786 0, /*nb_pos*/
787 0, /*(unaryfunc)array_abs,*/
788 0, /*nb_nonzero*/
789 0, /*nb_invert*/
790 0, /*nb_lshift*/
791 0, /*nb_rshift*/
792 0, /*nb_and*/
793 0, /*nb_xor*/
794 0, /*nb_or*/
795 0, /*nb_int*/
796 0, /*nb_long*/
797 0, /*nb_float*/
798 (binaryfunc)Clip_inplace_add, /*inplace_add*/
799 (binaryfunc)Clip_inplace_sub, /*inplace_subtract*/
800 (binaryfunc)Clip_inplace_multiply, /*inplace_multiply*/
801 0, /*inplace_remainder*/
802 0, /*inplace_power*/
803 0, /*inplace_lshift*/
804 0, /*inplace_rshift*/
805 0, /*inplace_and*/
806 0, /*inplace_xor*/
807 0, /*inplace_or*/
808 0, /*nb_floor_divide*/
809 (binaryfunc)Clip_div, /*nb_true_divide*/
810 0, /*nb_inplace_floor_divide*/
811 (binaryfunc)Clip_inplace_div, /*nb_inplace_true_divide*/
812 0, /* nb_index */
813 };
814
815 PyTypeObject ClipType =
816 {
817 PyVarObject_HEAD_INIT(NULL, 0)
818 "_pyo.Clip_base", /*tp_name*/
819 sizeof(Clip), /*tp_basicsize*/
820 0, /*tp_itemsize*/
821 (destructor)Clip_dealloc, /*tp_dealloc*/
822 0, /*tp_print*/
823 0, /*tp_getattr*/
824 0, /*tp_setattr*/
825 0, /*tp_as_async (tp_compare in Python 2)*/
826 0, /*tp_repr*/
827 &Clip_as_number, /*tp_as_number*/
828 0, /*tp_as_sequence*/
829 0, /*tp_as_mapping*/
830 0, /*tp_hash */
831 0, /*tp_call*/
832 0, /*tp_str*/
833 0, /*tp_getattro*/
834 0, /*tp_setattro*/
835 0, /*tp_as_buffer*/
836 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
837 "Clip objects. Clips a signal to a predefined limit.", /* tp_doc */
838 (traverseproc)Clip_traverse, /* tp_traverse */
839 (inquiry)Clip_clear, /* tp_clear */
840 0, /* tp_richcompare */
841 0, /* tp_weaklistoffset */
842 0, /* tp_iter */
843 0, /* tp_iternext */
844 Clip_methods, /* tp_methods */
845 Clip_members, /* tp_members */
846 0, /* tp_getset */
847 0, /* tp_base */
848 0, /* tp_dict */
849 0, /* tp_descr_get */
850 0, /* tp_descr_set */
851 0, /* tp_dictoffset */
852 0, /* tp_init */
853 0, /* tp_alloc */
854 Clip_new, /* tp_new */
855 };
856
857 /*****************/
858 /* Mirror object */
859 /*****************/
860
861 typedef struct
862 {
863 pyo_audio_HEAD
864 PyObject *input;
865 Stream *input_stream;
866 PyObject *min;
867 Stream *min_stream;
868 PyObject *max;
869 Stream *max_stream;
870 int modebuffer[4];
871 } Mirror;
872
873 static void
874 Mirror_transform_ii(Mirror *self)
875 {
876 MYFLT val, avg;
877 int i;
878 MYFLT *in = Stream_getData((Stream *)self->input_stream);
879 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
880 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
881
882 if (mi >= ma)
883 {
884 avg = (mi + ma) * 0.5;
885
886 for (i = 0; i < self->bufsize; i++)
887 {
888 self->data[i] = avg;
889 }
890 }
891 else
892 {
893 for (i = 0; i < self->bufsize; i++)
894 {
895 val = in[i];
896
897 while ((val > ma) || (val < mi))
898 {
899 if (val > ma)
900 val = ma + ma - val;
901 else
902 val = mi + mi - val;
903 }
904
905 self->data[i] = val;
906 }
907 }
908 }
909
910 static void
911 Mirror_transform_ai(Mirror *self)
912 {
913 MYFLT val, avg, mi;
914 int i;
915 MYFLT *in = Stream_getData((Stream *)self->input_stream);
916 MYFLT *mini = Stream_getData((Stream *)self->min_stream);
917 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
918
919 for (i = 0; i < self->bufsize; i++)
920 {
921 val = in[i];
922 mi = mini[i];
923
924 if (mi >= ma)
925 {
926 avg = (mi + ma) * 0.5;
927 self->data[i] = avg;
928 }
929 else
930 {
931 while ((val > ma) || (val < mi))
932 {
933 if (val > ma)
934 val = ma + ma - val;
935 else
936 val = mi + mi - val;
937 }
938
939 self->data[i] = val;
940 }
941 }
942 }
943
944 static void
945 Mirror_transform_ia(Mirror *self)
946 {
947 MYFLT val, avg, ma;
948 int i;
949 MYFLT *in = Stream_getData((Stream *)self->input_stream);
950 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
951 MYFLT *maxi = Stream_getData((Stream *)self->max_stream);
952
953 for (i = 0; i < self->bufsize; i++)
954 {
955 val = in[i];
956 ma = maxi[i];
957
958 if (mi >= ma)
959 {
960 avg = (mi + ma) * 0.5;
961 self->data[i] = avg;
962 }
963 else
964 {
965 while ((val > ma) || (val < mi))
966 {
967 if (val > ma)
968 val = ma + ma - val;
969 else
970 val = mi + mi - val;
971 }
972
973 self->data[i] = val;
974 }
975 }
976 }
977
978 static void
979 Mirror_transform_aa(Mirror *self)
980 {
981 MYFLT val, avg, mi, ma;
982 int i;
983 MYFLT *in = Stream_getData((Stream *)self->input_stream);
984 MYFLT *mini = Stream_getData((Stream *)self->min_stream);
985 MYFLT *maxi = Stream_getData((Stream *)self->max_stream);
986
987 for (i = 0; i < self->bufsize; i++)
988 {
989 val = in[i];
990 mi = mini[i];
991 ma = maxi[i];
992
993 if (mi >= ma)
994 {
995 avg = (mi + ma) * 0.5;
996 self->data[i] = avg;
997 }
998 else
999 {
1000 while ((val > ma) || (val < mi))
1001 {
1002 if (val > ma)
1003 val = ma + ma - val;
1004 else
1005 val = mi + mi - val;
1006 }
1007
1008 self->data[i] = val;
1009 }
1010 }
1011 }
1012
1013 static void Mirror_postprocessing_ii(Mirror *self) { POST_PROCESSING_II };
1014 static void Mirror_postprocessing_ai(Mirror *self) { POST_PROCESSING_AI };
1015 static void Mirror_postprocessing_ia(Mirror *self) { POST_PROCESSING_IA };
1016 static void Mirror_postprocessing_aa(Mirror *self) { POST_PROCESSING_AA };
1017 static void Mirror_postprocessing_ireva(Mirror *self) { POST_PROCESSING_IREVA };
1018 static void Mirror_postprocessing_areva(Mirror *self) { POST_PROCESSING_AREVA };
1019 static void Mirror_postprocessing_revai(Mirror *self) { POST_PROCESSING_REVAI };
1020 static void Mirror_postprocessing_revaa(Mirror *self) { POST_PROCESSING_REVAA };
1021 static void Mirror_postprocessing_revareva(Mirror *self) { POST_PROCESSING_REVAREVA };
1022
1023 static void
1024 Mirror_setProcMode(Mirror *self)
1025 {
1026 int procmode, muladdmode;
1027 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
1028 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1029
1030 switch (procmode)
1031 {
1032 case 0:
1033 self->proc_func_ptr = Mirror_transform_ii;
1034 break;
1035
1036 case 1:
1037 self->proc_func_ptr = Mirror_transform_ai;
1038 break;
1039
1040 case 10:
1041 self->proc_func_ptr = Mirror_transform_ia;
1042 break;
1043
1044 case 11:
1045 self->proc_func_ptr = Mirror_transform_aa;
1046 break;
1047 }
1048
1049 switch (muladdmode)
1050 {
1051 case 0:
1052 self->muladd_func_ptr = Mirror_postprocessing_ii;
1053 break;
1054
1055 case 1:
1056 self->muladd_func_ptr = Mirror_postprocessing_ai;
1057 break;
1058
1059 case 2:
1060 self->muladd_func_ptr = Mirror_postprocessing_revai;
1061 break;
1062
1063 case 10:
1064 self->muladd_func_ptr = Mirror_postprocessing_ia;
1065 break;
1066
1067 case 11:
1068 self->muladd_func_ptr = Mirror_postprocessing_aa;
1069 break;
1070
1071 case 12:
1072 self->muladd_func_ptr = Mirror_postprocessing_revaa;
1073 break;
1074
1075 case 20:
1076 self->muladd_func_ptr = Mirror_postprocessing_ireva;
1077 break;
1078
1079 case 21:
1080 self->muladd_func_ptr = Mirror_postprocessing_areva;
1081 break;
1082
1083 case 22:
1084 self->muladd_func_ptr = Mirror_postprocessing_revareva;
1085 break;
1086 }
1087 }
1088
1089 static void
1090 Mirror_compute_next_data_frame(Mirror *self)
1091 {
1092 (*self->proc_func_ptr)(self);
1093 (*self->muladd_func_ptr)(self);
1094 }
1095
1096 static int
1097 Mirror_traverse(Mirror *self, visitproc visit, void *arg)
1098 {
1099 pyo_VISIT
1100 Py_VISIT(self->input);
1101 Py_VISIT(self->min);
1102 Py_VISIT(self->max);
1103 return 0;
1104 }
1105
1106 static int
1107 Mirror_clear(Mirror *self)
1108 {
1109 pyo_CLEAR
1110 Py_CLEAR(self->input);
1111 Py_CLEAR(self->min);
1112 Py_CLEAR(self->max);
1113 return 0;
1114 }
1115
1116 static void
1117 Mirror_dealloc(Mirror* self)
1118 {
1119 pyo_DEALLOC
1120 Mirror_clear(self);
1121 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1122 Py_TYPE(self)->tp_free((PyObject*)self);
1123 }
1124
1125 static PyObject *
1126 Mirror_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1127 {
1128 int i;
1129 PyObject *inputtmp, *input_streamtmp, *mintmp = NULL, *maxtmp = NULL, *multmp = NULL, *addtmp = NULL;
1130 Mirror *self;
1131 self = (Mirror *)type->tp_alloc(type, 0);
1132
1133 self->min = PyFloat_FromDouble(0.0);
1134 self->max = PyFloat_FromDouble(1.0);
1135 self->modebuffer[0] = 0;
1136 self->modebuffer[1] = 0;
1137 self->modebuffer[2] = 0;
1138 self->modebuffer[3] = 0;
1139
1140 INIT_OBJECT_COMMON
1141 Stream_setFunctionPtr(self->stream, Mirror_compute_next_data_frame);
1142 self->mode_func_ptr = Mirror_setProcMode;
1143
1144 static char *kwlist[] = {"input", "min", "max", "mul", "add", NULL};
1145
1146 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &mintmp, &maxtmp, &multmp, &addtmp))
1147 Py_RETURN_NONE;
1148
1149 INIT_INPUT_STREAM
1150
1151 if (mintmp)
1152 {
1153 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
1154 }
1155
1156 if (maxtmp)
1157 {
1158 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
1159 }
1160
1161 if (multmp)
1162 {
1163 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1164 }
1165
1166 if (addtmp)
1167 {
1168 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1169 }
1170
1171 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1172
1173 (*self->mode_func_ptr)(self);
1174
1175 return (PyObject *)self;
1176 }
1177
1178 static PyObject * Mirror_getServer(Mirror* self) { GET_SERVER };
1179 static PyObject * Mirror_getStream(Mirror* self) { GET_STREAM };
1180 static PyObject * Mirror_setMul(Mirror *self, PyObject *arg) { SET_MUL };
1181 static PyObject * Mirror_setAdd(Mirror *self, PyObject *arg) { SET_ADD };
1182 static PyObject * Mirror_setSub(Mirror *self, PyObject *arg) { SET_SUB };
1183 static PyObject * Mirror_setDiv(Mirror *self, PyObject *arg) { SET_DIV };
1184
1185 static PyObject * Mirror_play(Mirror *self, PyObject *args, PyObject *kwds) { PLAY };
1186 static PyObject * Mirror_out(Mirror *self, PyObject *args, PyObject *kwds) { OUT };
1187 static PyObject * Mirror_stop(Mirror *self, PyObject *args, PyObject *kwds) { STOP };
1188
1189 static PyObject * Mirror_multiply(Mirror *self, PyObject *arg) { MULTIPLY };
1190 static PyObject * Mirror_inplace_multiply(Mirror *self, PyObject *arg) { INPLACE_MULTIPLY };
1191 static PyObject * Mirror_add(Mirror *self, PyObject *arg) { ADD };
1192 static PyObject * Mirror_inplace_add(Mirror *self, PyObject *arg) { INPLACE_ADD };
1193 static PyObject * Mirror_sub(Mirror *self, PyObject *arg) { SUB };
1194 static PyObject * Mirror_inplace_sub(Mirror *self, PyObject *arg) { INPLACE_SUB };
1195 static PyObject * Mirror_div(Mirror *self, PyObject *arg) { DIV };
1196 static PyObject * Mirror_inplace_div(Mirror *self, PyObject *arg) { INPLACE_DIV };
1197
1198 static PyObject * Mirror_setMin(Mirror *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
1199 static PyObject * Mirror_setMax(Mirror *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
1200
1201 static PyMemberDef Mirror_members[] =
1202 {
1203 {"server", T_OBJECT_EX, offsetof(Mirror, server), 0, "Pyo server."},
1204 {"stream", T_OBJECT_EX, offsetof(Mirror, stream), 0, "Stream object."},
1205 {"input", T_OBJECT_EX, offsetof(Mirror, input), 0, "Input sound object."},
1206 {"min", T_OBJECT_EX, offsetof(Mirror, min), 0, "Minimum possible value."},
1207 {"max", T_OBJECT_EX, offsetof(Mirror, max), 0, "Maximum possible value."},
1208 {"mul", T_OBJECT_EX, offsetof(Mirror, mul), 0, "Mul factor."},
1209 {"add", T_OBJECT_EX, offsetof(Mirror, add), 0, "Add factor."},
1210 {NULL} /* Sentinel */
1211 };
1212
1213 static PyMethodDef Mirror_methods[] =
1214 {
1215 {"getServer", (PyCFunction)Mirror_getServer, METH_NOARGS, "Returns server object."},
1216 {"_getStream", (PyCFunction)Mirror_getStream, METH_NOARGS, "Returns stream object."},
1217 {"play", (PyCFunction)Mirror_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1218 {"out", (PyCFunction)Mirror_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1219 {"stop", (PyCFunction)Mirror_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1220 {"setMin", (PyCFunction)Mirror_setMin, METH_O, "Sets the minimum value."},
1221 {"setMax", (PyCFunction)Mirror_setMax, METH_O, "Sets the maximum value."},
1222 {"setMul", (PyCFunction)Mirror_setMul, METH_O, "Sets oscillator mul factor."},
1223 {"setAdd", (PyCFunction)Mirror_setAdd, METH_O, "Sets oscillator add factor."},
1224 {"setSub", (PyCFunction)Mirror_setSub, METH_O, "Sets inverse add factor."},
1225 {"setDiv", (PyCFunction)Mirror_setDiv, METH_O, "Sets inverse mul factor."},
1226 {NULL} /* Sentinel */
1227 };
1228
1229 static PyNumberMethods Mirror_as_number =
1230 {
1231 (binaryfunc)Mirror_add, /*nb_add*/
1232 (binaryfunc)Mirror_sub, /*nb_subtract*/
1233 (binaryfunc)Mirror_multiply, /*nb_multiply*/
1234 0, /*nb_remainder*/
1235 0, /*nb_divmod*/
1236 0, /*nb_power*/
1237 0, /*nb_neg*/
1238 0, /*nb_pos*/
1239 0, /*(unaryfunc)array_abs,*/
1240 0, /*nb_nonzero*/
1241 0, /*nb_invert*/
1242 0, /*nb_lshift*/
1243 0, /*nb_rshift*/
1244 0, /*nb_and*/
1245 0, /*nb_xor*/
1246 0, /*nb_or*/
1247 0, /*nb_int*/
1248 0, /*nb_long*/
1249 0, /*nb_float*/
1250 (binaryfunc)Mirror_inplace_add, /*inplace_add*/
1251 (binaryfunc)Mirror_inplace_sub, /*inplace_subtract*/
1252 (binaryfunc)Mirror_inplace_multiply, /*inplace_multiply*/
1253 0, /*inplace_remainder*/
1254 0, /*inplace_power*/
1255 0, /*inplace_lshift*/
1256 0, /*inplace_rshift*/
1257 0, /*inplace_and*/
1258 0, /*inplace_xor*/
1259 0, /*inplace_or*/
1260 0, /*nb_floor_divide*/
1261 (binaryfunc)Mirror_div, /*nb_true_divide*/
1262 0, /*nb_inplace_floor_divide*/
1263 (binaryfunc)Mirror_inplace_div, /*nb_inplace_true_divide*/
1264 0, /* nb_index */
1265 };
1266
1267 PyTypeObject MirrorType =
1268 {
1269 PyVarObject_HEAD_INIT(NULL, 0)
1270 "_pyo.Mirror_base", /*tp_name*/
1271 sizeof(Mirror), /*tp_basicsize*/
1272 0, /*tp_itemsize*/
1273 (destructor)Mirror_dealloc, /*tp_dealloc*/
1274 0, /*tp_print*/
1275 0, /*tp_getattr*/
1276 0, /*tp_setattr*/
1277 0, /*tp_as_async (tp_compare in Python 2)*/
1278 0, /*tp_repr*/
1279 &Mirror_as_number, /*tp_as_number*/
1280 0, /*tp_as_sequence*/
1281 0, /*tp_as_mapping*/
1282 0, /*tp_hash */
1283 0, /*tp_call*/
1284 0, /*tp_str*/
1285 0, /*tp_getattro*/
1286 0, /*tp_setattro*/
1287 0, /*tp_as_buffer*/
1288 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1289 "Mirror objects. Reflects the signal that exceeds the min and max thresholds.", /* tp_doc */
1290 (traverseproc)Mirror_traverse, /* tp_traverse */
1291 (inquiry)Mirror_clear, /* tp_clear */
1292 0, /* tp_richcompare */
1293 0, /* tp_weaklistoffset */
1294 0, /* tp_iter */
1295 0, /* tp_iternext */
1296 Mirror_methods, /* tp_methods */
1297 Mirror_members, /* tp_members */
1298 0, /* tp_getset */
1299 0, /* tp_base */
1300 0, /* tp_dict */
1301 0, /* tp_descr_get */
1302 0, /* tp_descr_set */
1303 0, /* tp_dictoffset */
1304 0, /* tp_init */
1305 0, /* tp_alloc */
1306 Mirror_new, /* tp_new */
1307 };
1308
1309 /*****************/
1310 /** Wrap object **/
1311 /*****************/
1312
1313 typedef struct
1314 {
1315 pyo_audio_HEAD
1316 PyObject *input;
1317 Stream *input_stream;
1318 PyObject *min;
1319 Stream *min_stream;
1320 PyObject *max;
1321 Stream *max_stream;
1322 int modebuffer[4];
1323 } Wrap;
1324
1325 static void
1326 Wrap_transform_ii(Wrap *self)
1327 {
1328 MYFLT val, avg, rng, tmp;
1329 int i;
1330 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1331 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
1332 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
1333
1334 if (mi >= ma)
1335 {
1336 avg = (mi + ma) * 0.5;
1337
1338 for (i = 0; i < self->bufsize; i++)
1339 {
1340 self->data[i] = avg;
1341 }
1342 }
1343 else
1344 {
1345 rng = ma - mi;
1346
1347 for (i = 0; i < self->bufsize; i++)
1348 {
1349 val = in[i];
1350 tmp = (val - mi) / rng;
1351
1352 if (tmp >= 1.0)
1353 {
1354 tmp -= (int)tmp;
1355 val = tmp * rng + mi;
1356 }
1357 else if (tmp < 0)
1358 {
1359 tmp += (int)(-tmp) + 1;
1360 val = tmp * rng + mi;
1361
1362 if (val == ma)
1363 val = mi;
1364 }
1365
1366 self->data[i] = val;
1367 }
1368 }
1369 }
1370
1371 static void
1372 Wrap_transform_ai(Wrap *self)
1373 {
1374 MYFLT val, avg, rng, tmp, mi;
1375 int i;
1376 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1377 MYFLT *mini = Stream_getData((Stream *)self->min_stream);
1378 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
1379
1380 for (i = 0; i < self->bufsize; i++)
1381 {
1382 val = in[i];
1383 mi = mini[i];
1384
1385 if (mi >= ma)
1386 {
1387 avg = (mi + ma) * 0.5;
1388 self->data[i] = avg;
1389 }
1390 else
1391 {
1392 rng = ma - mi;
1393 tmp = (val - mi) / rng;
1394
1395 if (tmp >= 1.0)
1396 {
1397 tmp -= (int)tmp;
1398 val = tmp * rng + mi;
1399 }
1400 else if (tmp < 0)
1401 {
1402 tmp += (int)(-tmp) + 1;
1403 val = tmp * rng + mi;
1404
1405 if (val == ma)
1406 val = mi;
1407 }
1408
1409 self->data[i] = val;
1410 }
1411 }
1412 }
1413
1414 static void
1415 Wrap_transform_ia(Wrap *self)
1416 {
1417 MYFLT val, avg, rng, tmp, ma;
1418 int i;
1419 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1420 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
1421 MYFLT *maxi = Stream_getData((Stream *)self->max_stream);
1422
1423 for (i = 0; i < self->bufsize; i++)
1424 {
1425 val = in[i];
1426 ma = maxi[i];
1427
1428 if (mi >= ma)
1429 {
1430 avg = (mi + ma) * 0.5;
1431 self->data[i] = avg;
1432 }
1433 else
1434 {
1435 rng = ma - mi;
1436 tmp = (val - mi) / rng;
1437
1438 if (tmp >= 1.0)
1439 {
1440 tmp -= (int)tmp;
1441 val = tmp * rng + mi;
1442 }
1443 else if (tmp < 0)
1444 {
1445 tmp += (int)(-tmp) + 1;
1446 val = tmp * rng + mi;
1447
1448 if (val == ma)
1449 val = mi;
1450 }
1451
1452 self->data[i] = val;
1453 }
1454 }
1455 }
1456
1457 static void
1458 Wrap_transform_aa(Wrap *self)
1459 {
1460 MYFLT val, avg, rng, tmp, mi, ma;
1461 int i;
1462 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1463 MYFLT *mini = Stream_getData((Stream *)self->min_stream);
1464 MYFLT *maxi = Stream_getData((Stream *)self->max_stream);
1465
1466 for (i = 0; i < self->bufsize; i++)
1467 {
1468 val = in[i];
1469 mi = mini[i];
1470 ma = maxi[i];
1471
1472 if (mi >= ma)
1473 {
1474 avg = (mi + ma) * 0.5;
1475 self->data[i] = avg;
1476 }
1477 else
1478 {
1479 rng = ma - mi;
1480 tmp = (val - mi) / rng;
1481
1482 if (tmp >= 1.0)
1483 {
1484 tmp -= (int)tmp;
1485 val = tmp * rng + mi;
1486 }
1487 else if (tmp < 0)
1488 {
1489 tmp += (int)(-tmp) + 1;
1490 val = tmp * rng + mi;
1491
1492 if (val == ma)
1493 val = mi;
1494 }
1495
1496 self->data[i] = val;
1497 }
1498 }
1499 }
1500
1501 static void Wrap_postprocessing_ii(Wrap *self) { POST_PROCESSING_II };
1502 static void Wrap_postprocessing_ai(Wrap *self) { POST_PROCESSING_AI };
1503 static void Wrap_postprocessing_ia(Wrap *self) { POST_PROCESSING_IA };
1504 static void Wrap_postprocessing_aa(Wrap *self) { POST_PROCESSING_AA };
1505 static void Wrap_postprocessing_ireva(Wrap *self) { POST_PROCESSING_IREVA };
1506 static void Wrap_postprocessing_areva(Wrap *self) { POST_PROCESSING_AREVA };
1507 static void Wrap_postprocessing_revai(Wrap *self) { POST_PROCESSING_REVAI };
1508 static void Wrap_postprocessing_revaa(Wrap *self) { POST_PROCESSING_REVAA };
1509 static void Wrap_postprocessing_revareva(Wrap *self) { POST_PROCESSING_REVAREVA };
1510
1511 static void
1512 Wrap_setProcMode(Wrap *self)
1513 {
1514 int procmode, muladdmode;
1515 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
1516 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1517
1518 switch (procmode)
1519 {
1520 case 0:
1521 self->proc_func_ptr = Wrap_transform_ii;
1522 break;
1523
1524 case 1:
1525 self->proc_func_ptr = Wrap_transform_ai;
1526 break;
1527
1528 case 10:
1529 self->proc_func_ptr = Wrap_transform_ia;
1530 break;
1531
1532 case 11:
1533 self->proc_func_ptr = Wrap_transform_aa;
1534 break;
1535 }
1536
1537 switch (muladdmode)
1538 {
1539 case 0:
1540 self->muladd_func_ptr = Wrap_postprocessing_ii;
1541 break;
1542
1543 case 1:
1544 self->muladd_func_ptr = Wrap_postprocessing_ai;
1545 break;
1546
1547 case 2:
1548 self->muladd_func_ptr = Wrap_postprocessing_revai;
1549 break;
1550
1551 case 10:
1552 self->muladd_func_ptr = Wrap_postprocessing_ia;
1553 break;
1554
1555 case 11:
1556 self->muladd_func_ptr = Wrap_postprocessing_aa;
1557 break;
1558
1559 case 12:
1560 self->muladd_func_ptr = Wrap_postprocessing_revaa;
1561 break;
1562
1563 case 20:
1564 self->muladd_func_ptr = Wrap_postprocessing_ireva;
1565 break;
1566
1567 case 21:
1568 self->muladd_func_ptr = Wrap_postprocessing_areva;
1569 break;
1570
1571 case 22:
1572 self->muladd_func_ptr = Wrap_postprocessing_revareva;
1573 break;
1574 }
1575 }
1576
1577 static void
1578 Wrap_compute_next_data_frame(Wrap *self)
1579 {
1580 (*self->proc_func_ptr)(self);
1581 (*self->muladd_func_ptr)(self);
1582 }
1583
1584 static int
1585 Wrap_traverse(Wrap *self, visitproc visit, void *arg)
1586 {
1587 pyo_VISIT
1588 Py_VISIT(self->input);
1589 Py_VISIT(self->min);
1590 Py_VISIT(self->max);
1591 return 0;
1592 }
1593
1594 static int
1595 Wrap_clear(Wrap *self)
1596 {
1597 pyo_CLEAR
1598 Py_CLEAR(self->input);
1599 Py_CLEAR(self->min);
1600 Py_CLEAR(self->max);
1601 return 0;
1602 }
1603
1604 static void
1605 Wrap_dealloc(Wrap* self)
1606 {
1607 pyo_DEALLOC
1608 Wrap_clear(self);
1609 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1610 Py_TYPE(self)->tp_free((PyObject*)self);
1611 }
1612
1613 static PyObject *
1614 Wrap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1615 {
1616 int i;
1617 PyObject *inputtmp, *input_streamtmp, *mintmp = NULL, *maxtmp = NULL, *multmp = NULL, *addtmp = NULL;
1618 Wrap *self;
1619 self = (Wrap *)type->tp_alloc(type, 0);
1620
1621 self->min = PyFloat_FromDouble(0.0);
1622 self->max = PyFloat_FromDouble(1.0);
1623 self->modebuffer[0] = 0;
1624 self->modebuffer[1] = 0;
1625 self->modebuffer[2] = 0;
1626 self->modebuffer[3] = 0;
1627
1628 INIT_OBJECT_COMMON
1629 Stream_setFunctionPtr(self->stream, Wrap_compute_next_data_frame);
1630 self->mode_func_ptr = Wrap_setProcMode;
1631
1632 static char *kwlist[] = {"input", "min", "max", "mul", "add", NULL};
1633
1634 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &mintmp, &maxtmp, &multmp, &addtmp))
1635 Py_RETURN_NONE;
1636
1637 INIT_INPUT_STREAM
1638
1639 if (mintmp)
1640 {
1641 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
1642 }
1643
1644 if (maxtmp)
1645 {
1646 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
1647 }
1648
1649 if (multmp)
1650 {
1651 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1652 }
1653
1654 if (addtmp)
1655 {
1656 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1657 }
1658
1659 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1660
1661 (*self->mode_func_ptr)(self);
1662
1663 return (PyObject *)self;
1664 }
1665
1666 static PyObject * Wrap_getServer(Wrap* self) { GET_SERVER };
1667 static PyObject * Wrap_getStream(Wrap* self) { GET_STREAM };
1668 static PyObject * Wrap_setMul(Wrap *self, PyObject *arg) { SET_MUL };
1669 static PyObject * Wrap_setAdd(Wrap *self, PyObject *arg) { SET_ADD };
1670 static PyObject * Wrap_setSub(Wrap *self, PyObject *arg) { SET_SUB };
1671 static PyObject * Wrap_setDiv(Wrap *self, PyObject *arg) { SET_DIV };
1672
1673 static PyObject * Wrap_play(Wrap *self, PyObject *args, PyObject *kwds) { PLAY };
1674 static PyObject * Wrap_out(Wrap *self, PyObject *args, PyObject *kwds) { OUT };
1675 static PyObject * Wrap_stop(Wrap *self, PyObject *args, PyObject *kwds) { STOP };
1676
1677 static PyObject * Wrap_multiply(Wrap *self, PyObject *arg) { MULTIPLY };
1678 static PyObject * Wrap_inplace_multiply(Wrap *self, PyObject *arg) { INPLACE_MULTIPLY };
1679 static PyObject * Wrap_add(Wrap *self, PyObject *arg) { ADD };
1680 static PyObject * Wrap_inplace_add(Wrap *self, PyObject *arg) { INPLACE_ADD };
1681 static PyObject * Wrap_sub(Wrap *self, PyObject *arg) { SUB };
1682 static PyObject * Wrap_inplace_sub(Wrap *self, PyObject *arg) { INPLACE_SUB };
1683 static PyObject * Wrap_div(Wrap *self, PyObject *arg) { DIV };
1684 static PyObject * Wrap_inplace_div(Wrap *self, PyObject *arg) { INPLACE_DIV };
1685
1686 static PyObject * Wrap_setMin(Wrap *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
1687 static PyObject * Wrap_setMax(Wrap *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
1688
1689 static PyMemberDef Wrap_members[] =
1690 {
1691 {"server", T_OBJECT_EX, offsetof(Wrap, server), 0, "Pyo server."},
1692 {"stream", T_OBJECT_EX, offsetof(Wrap, stream), 0, "Stream object."},
1693 {"input", T_OBJECT_EX, offsetof(Wrap, input), 0, "Input sound object."},
1694 {"min", T_OBJECT_EX, offsetof(Wrap, min), 0, "Minimum possible value."},
1695 {"max", T_OBJECT_EX, offsetof(Wrap, max), 0, "Maximum possible value."},
1696 {"mul", T_OBJECT_EX, offsetof(Wrap, mul), 0, "Mul factor."},
1697 {"add", T_OBJECT_EX, offsetof(Wrap, add), 0, "Add factor."},
1698 {NULL} /* Sentinel */
1699 };
1700
1701 static PyMethodDef Wrap_methods[] =
1702 {
1703 {"getServer", (PyCFunction)Wrap_getServer, METH_NOARGS, "Returns server object."},
1704 {"_getStream", (PyCFunction)Wrap_getStream, METH_NOARGS, "Returns stream object."},
1705 {"play", (PyCFunction)Wrap_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1706 {"out", (PyCFunction)Wrap_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1707 {"stop", (PyCFunction)Wrap_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1708 {"setMin", (PyCFunction)Wrap_setMin, METH_O, "Sets the minimum value."},
1709 {"setMax", (PyCFunction)Wrap_setMax, METH_O, "Sets the maximum value."},
1710 {"setMul", (PyCFunction)Wrap_setMul, METH_O, "Sets oscillator mul factor."},
1711 {"setAdd", (PyCFunction)Wrap_setAdd, METH_O, "Sets oscillator add factor."},
1712 {"setSub", (PyCFunction)Wrap_setSub, METH_O, "Sets inverse add factor."},
1713 {"setDiv", (PyCFunction)Wrap_setDiv, METH_O, "Sets inverse mul factor."},
1714 {NULL} /* Sentinel */
1715 };
1716
1717 static PyNumberMethods Wrap_as_number =
1718 {
1719 (binaryfunc)Wrap_add, /*nb_add*/
1720 (binaryfunc)Wrap_sub, /*nb_subtract*/
1721 (binaryfunc)Wrap_multiply, /*nb_multiply*/
1722 0, /*nb_remainder*/
1723 0, /*nb_divmod*/
1724 0, /*nb_power*/
1725 0, /*nb_neg*/
1726 0, /*nb_pos*/
1727 0, /*(unaryfunc)array_abs,*/
1728 0, /*nb_nonzero*/
1729 0, /*nb_invert*/
1730 0, /*nb_lshift*/
1731 0, /*nb_rshift*/
1732 0, /*nb_and*/
1733 0, /*nb_xor*/
1734 0, /*nb_or*/
1735 0, /*nb_int*/
1736 0, /*nb_long*/
1737 0, /*nb_float*/
1738 (binaryfunc)Wrap_inplace_add, /*inplace_add*/
1739 (binaryfunc)Wrap_inplace_sub, /*inplace_subtract*/
1740 (binaryfunc)Wrap_inplace_multiply, /*inplace_multiply*/
1741 0, /*inplace_remainder*/
1742 0, /*inplace_power*/
1743 0, /*inplace_lshift*/
1744 0, /*inplace_rshift*/
1745 0, /*inplace_and*/
1746 0, /*inplace_xor*/
1747 0, /*inplace_or*/
1748 0, /*nb_floor_divide*/
1749 (binaryfunc)Wrap_div, /*nb_true_divide*/
1750 0, /*nb_inplace_floor_divide*/
1751 (binaryfunc)Wrap_inplace_div, /*nb_inplace_true_divide*/
1752 0, /* nb_index */
1753 };
1754
1755 PyTypeObject WrapType =
1756 {
1757 PyVarObject_HEAD_INIT(NULL, 0)
1758 "_pyo.Wrap_base", /*tp_name*/
1759 sizeof(Wrap), /*tp_basicsize*/
1760 0, /*tp_itemsize*/
1761 (destructor)Wrap_dealloc, /*tp_dealloc*/
1762 0, /*tp_print*/
1763 0, /*tp_getattr*/
1764 0, /*tp_setattr*/
1765 0, /*tp_as_async (tp_compare in Python 2)*/
1766 0, /*tp_repr*/
1767 &Wrap_as_number, /*tp_as_number*/
1768 0, /*tp_as_sequence*/
1769 0, /*tp_as_mapping*/
1770 0, /*tp_hash */
1771 0, /*tp_call*/
1772 0, /*tp_str*/
1773 0, /*tp_getattro*/
1774 0, /*tp_setattro*/
1775 0, /*tp_as_buffer*/
1776 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1777 "Wrap objects. Wraps-around the signal that exceeds the min and max thresholds.", /* tp_doc */
1778 (traverseproc)Wrap_traverse, /* tp_traverse */
1779 (inquiry)Wrap_clear, /* tp_clear */
1780 0, /* tp_richcompare */
1781 0, /* tp_weaklistoffset */
1782 0, /* tp_iter */
1783 0, /* tp_iternext */
1784 Wrap_methods, /* tp_methods */
1785 Wrap_members, /* tp_members */
1786 0, /* tp_getset */
1787 0, /* tp_base */
1788 0, /* tp_dict */
1789 0, /* tp_descr_get */
1790 0, /* tp_descr_set */
1791 0, /* tp_dictoffset */
1792 0, /* tp_init */
1793 0, /* tp_alloc */
1794 Wrap_new, /* tp_new */
1795 };
1796
1797 /*****************/
1798 /** Degrade object **/
1799 /*****************/
1800
1801 typedef struct
1802 {
1803 pyo_audio_HEAD
1804 PyObject *input;
1805 Stream *input_stream;
1806 PyObject *bitdepth;
1807 Stream *bitdepth_stream;
1808 PyObject *srscale;
1809 Stream *srscale_stream;
1810 MYFLT value;
1811 int sampsCount;
1812 int modebuffer[4];
1813 } Degrade;
1814
1815 static MYFLT
1816 _bit_clip(MYFLT x)
1817 {
1818 if (x < 1.0)
1819 return 1.0;
1820 else if (x > 32.0)
1821 return 32.0;
1822 else
1823 return x;
1824 }
1825
1826 static MYFLT
1827 _sr_clip(MYFLT x)
1828 {
1829 // half sr ten times
1830 if (x <= 0.0009765625)
1831 return 0.0009765625;
1832 else if (x > 1.0)
1833 return 1.0;
1834 else
1835 return x;
1836 }
1837
1838 static void
1839 Degrade_transform_ii(Degrade *self)
1840 {
1841 MYFLT bitscl, ibitscl, newsr;
1842 int i, nsamps, tmp;
1843
1844 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1845 MYFLT bitdepth = _bit_clip(PyFloat_AS_DOUBLE(self->bitdepth));
1846 MYFLT srscale = _sr_clip(PyFloat_AS_DOUBLE(self->srscale));
1847
1848 bitscl = MYPOW(2.0, bitdepth - 1);
1849 ibitscl = 1.0 / bitscl;
1850
1851 newsr = self->sr * srscale;
1852 nsamps = (int)(self->sr / newsr);
1853
1854 for (i = 0; i < self->bufsize; i++)
1855 {
1856 self->sampsCount++;
1857
1858 if (self->sampsCount >= nsamps)
1859 {
1860 self->sampsCount = 0;
1861 tmp = (int)(in[i] * bitscl + 0.5);
1862 self->value = tmp * ibitscl;
1863 }
1864
1865 self->data[i] = self->value;
1866 }
1867 }
1868
1869 static void
1870 Degrade_transform_ai(Degrade *self)
1871 {
1872 MYFLT bitscl, ibitscl, newsr;
1873 int i, nsamps, tmp;
1874
1875 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1876 MYFLT *bitdepth = Stream_getData((Stream *)self->bitdepth_stream);
1877 MYFLT srscale = _sr_clip(PyFloat_AS_DOUBLE(self->srscale));
1878
1879 newsr = self->sr * srscale;
1880 nsamps = (int)(self->sr / newsr);
1881
1882 for (i = 0; i < self->bufsize; i++)
1883 {
1884 self->sampsCount++;
1885
1886 if (self->sampsCount >= nsamps)
1887 {
1888 self->sampsCount = 0;
1889 bitscl = MYPOW(2.0, _bit_clip(bitdepth[i]) - 1);
1890 ibitscl = 1.0 / bitscl;
1891 tmp = (int)(in[i] * bitscl + 0.5);
1892 self->value = tmp * ibitscl;
1893 }
1894
1895 self->data[i] = self->value;
1896 }
1897 }
1898
1899 static void
1900 Degrade_transform_ia(Degrade *self)
1901 {
1902 MYFLT bitscl, ibitscl, newsr;
1903 int i, nsamps, tmp;
1904
1905 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1906 MYFLT bitdepth = _bit_clip(PyFloat_AS_DOUBLE(self->bitdepth));
1907 MYFLT *srscale = Stream_getData((Stream *)self->srscale_stream);
1908
1909 bitscl = MYPOW(2.0, bitdepth - 1);
1910 ibitscl = 1.0 / bitscl;
1911
1912 for (i = 0; i < self->bufsize; i++)
1913 {
1914 newsr = self->sr * _sr_clip(srscale[i]);
1915 nsamps = (int)(self->sr / newsr);
1916 self->sampsCount++;
1917
1918 if (self->sampsCount >= nsamps)
1919 {
1920 self->sampsCount = 0;
1921 tmp = (int)(in[i] * bitscl + 0.5);
1922 self->value = tmp * ibitscl;
1923 }
1924
1925 self->data[i] = self->value;
1926 }
1927 }
1928
1929 static void
1930 Degrade_transform_aa(Degrade *self)
1931 {
1932 MYFLT bitscl, ibitscl, newsr;
1933 int i, nsamps, tmp;
1934
1935 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1936 MYFLT *bitdepth = Stream_getData((Stream *)self->bitdepth_stream);
1937 MYFLT *srscale = Stream_getData((Stream *)self->srscale_stream);
1938
1939 for (i = 0; i < self->bufsize; i++)
1940 {
1941 newsr = self->sr * _sr_clip(srscale[i]);
1942 nsamps = (int)(self->sr / newsr);
1943 self->sampsCount++;
1944
1945 if (self->sampsCount >= nsamps)
1946 {
1947 self->sampsCount = 0;
1948 bitscl = MYPOW(2.0, _bit_clip(bitdepth[i]) - 1);
1949 ibitscl = 1.0 / bitscl;
1950 tmp = (int)(in[i] * bitscl + 0.5);
1951 self->value = tmp * ibitscl;
1952 }
1953
1954 self->data[i] = self->value;
1955 }
1956 }
1957
1958 static void Degrade_postprocessing_ii(Degrade *self) { POST_PROCESSING_II };
1959 static void Degrade_postprocessing_ai(Degrade *self) { POST_PROCESSING_AI };
1960 static void Degrade_postprocessing_ia(Degrade *self) { POST_PROCESSING_IA };
1961 static void Degrade_postprocessing_aa(Degrade *self) { POST_PROCESSING_AA };
1962 static void Degrade_postprocessing_ireva(Degrade *self) { POST_PROCESSING_IREVA };
1963 static void Degrade_postprocessing_areva(Degrade *self) { POST_PROCESSING_AREVA };
1964 static void Degrade_postprocessing_revai(Degrade *self) { POST_PROCESSING_REVAI };
1965 static void Degrade_postprocessing_revaa(Degrade *self) { POST_PROCESSING_REVAA };
1966 static void Degrade_postprocessing_revareva(Degrade *self) { POST_PROCESSING_REVAREVA };
1967
1968 static void
1969 Degrade_setProcMode(Degrade *self)
1970 {
1971 int procmode, muladdmode;
1972 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
1973 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1974
1975 switch (procmode)
1976 {
1977 case 0:
1978 self->proc_func_ptr = Degrade_transform_ii;
1979 break;
1980
1981 case 1:
1982 self->proc_func_ptr = Degrade_transform_ai;
1983 break;
1984
1985 case 10:
1986 self->proc_func_ptr = Degrade_transform_ia;
1987 break;
1988
1989 case 11:
1990 self->proc_func_ptr = Degrade_transform_aa;
1991 break;
1992 }
1993
1994 switch (muladdmode)
1995 {
1996 case 0:
1997 self->muladd_func_ptr = Degrade_postprocessing_ii;
1998 break;
1999
2000 case 1:
2001 self->muladd_func_ptr = Degrade_postprocessing_ai;
2002 break;
2003
2004 case 2:
2005 self->muladd_func_ptr = Degrade_postprocessing_revai;
2006 break;
2007
2008 case 10:
2009 self->muladd_func_ptr = Degrade_postprocessing_ia;
2010 break;
2011
2012 case 11:
2013 self->muladd_func_ptr = Degrade_postprocessing_aa;
2014 break;
2015
2016 case 12:
2017 self->muladd_func_ptr = Degrade_postprocessing_revaa;
2018 break;
2019
2020 case 20:
2021 self->muladd_func_ptr = Degrade_postprocessing_ireva;
2022 break;
2023
2024 case 21:
2025 self->muladd_func_ptr = Degrade_postprocessing_areva;
2026 break;
2027
2028 case 22:
2029 self->muladd_func_ptr = Degrade_postprocessing_revareva;
2030 break;
2031 }
2032 }
2033
2034 static void
2035 Degrade_compute_next_data_frame(Degrade *self)
2036 {
2037 (*self->proc_func_ptr)(self);
2038 (*self->muladd_func_ptr)(self);
2039 }
2040
2041 static int
2042 Degrade_traverse(Degrade *self, visitproc visit, void *arg)
2043 {
2044 pyo_VISIT
2045 Py_VISIT(self->input);
2046 Py_VISIT(self->bitdepth);
2047 Py_VISIT(self->srscale);
2048 return 0;
2049 }
2050
2051 static int
2052 Degrade_clear(Degrade *self)
2053 {
2054 pyo_CLEAR
2055 Py_CLEAR(self->input);
2056 Py_CLEAR(self->bitdepth);
2057 Py_CLEAR(self->srscale);
2058 return 0;
2059 }
2060
2061 static void
2062 Degrade_dealloc(Degrade* self)
2063 {
2064 pyo_DEALLOC
2065 Degrade_clear(self);
2066 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2067 Py_TYPE(self)->tp_free((PyObject*)self);
2068 }
2069
2070 static PyObject *
2071 Degrade_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2072 {
2073 int i;
2074 PyObject *inputtmp, *input_streamtmp, *bitdepthtmp = NULL, *srscaletmp = NULL, *multmp = NULL, *addtmp = NULL;
2075 Degrade *self;
2076 self = (Degrade *)type->tp_alloc(type, 0);
2077
2078 self->bitdepth = PyFloat_FromDouble(16);
2079 self->srscale = PyFloat_FromDouble(1.0);
2080 self->value = 0.0;
2081 self->sampsCount = 0;
2082 self->modebuffer[0] = 0;
2083 self->modebuffer[1] = 0;
2084 self->modebuffer[2] = 0;
2085 self->modebuffer[3] = 0;
2086
2087 INIT_OBJECT_COMMON
2088 Stream_setFunctionPtr(self->stream, Degrade_compute_next_data_frame);
2089 self->mode_func_ptr = Degrade_setProcMode;
2090
2091 static char *kwlist[] = {"input", "bitdepth", "srscale", "mul", "add", NULL};
2092
2093 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &bitdepthtmp, &srscaletmp, &multmp, &addtmp))
2094 Py_RETURN_NONE;
2095
2096 INIT_INPUT_STREAM
2097
2098 if (bitdepthtmp)
2099 {
2100 PyObject_CallMethod((PyObject *)self, "setBitdepth", "O", bitdepthtmp);
2101 }
2102
2103 if (srscaletmp)
2104 {
2105 PyObject_CallMethod((PyObject *)self, "setSrscale", "O", srscaletmp);
2106 }
2107
2108 if (multmp)
2109 {
2110 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2111 }
2112
2113 if (addtmp)
2114 {
2115 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2116 }
2117
2118 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2119
2120 (*self->mode_func_ptr)(self);
2121
2122 return (PyObject *)self;
2123 }
2124
2125 static PyObject * Degrade_getServer(Degrade* self) { GET_SERVER };
2126 static PyObject * Degrade_getStream(Degrade* self) { GET_STREAM };
2127 static PyObject * Degrade_setMul(Degrade *self, PyObject *arg) { SET_MUL };
2128 static PyObject * Degrade_setAdd(Degrade *self, PyObject *arg) { SET_ADD };
2129 static PyObject * Degrade_setSub(Degrade *self, PyObject *arg) { SET_SUB };
2130 static PyObject * Degrade_setDiv(Degrade *self, PyObject *arg) { SET_DIV };
2131
2132 static PyObject * Degrade_play(Degrade *self, PyObject *args, PyObject *kwds) { PLAY };
2133 static PyObject * Degrade_out(Degrade *self, PyObject *args, PyObject *kwds) { OUT };
2134 static PyObject * Degrade_stop(Degrade *self, PyObject *args, PyObject *kwds) { STOP };
2135
2136 static PyObject * Degrade_multiply(Degrade *self, PyObject *arg) { MULTIPLY };
2137 static PyObject * Degrade_inplace_multiply(Degrade *self, PyObject *arg) { INPLACE_MULTIPLY };
2138 static PyObject * Degrade_add(Degrade *self, PyObject *arg) { ADD };
2139 static PyObject * Degrade_inplace_add(Degrade *self, PyObject *arg) { INPLACE_ADD };
2140 static PyObject * Degrade_sub(Degrade *self, PyObject *arg) { SUB };
2141 static PyObject * Degrade_inplace_sub(Degrade *self, PyObject *arg) { INPLACE_SUB };
2142 static PyObject * Degrade_div(Degrade *self, PyObject *arg) { DIV };
2143 static PyObject * Degrade_inplace_div(Degrade *self, PyObject *arg) { INPLACE_DIV };
2144
2145 static PyObject * Degrade_setBitdepth(Degrade *self, PyObject *arg) { SET_PARAM(self->bitdepth, self->bitdepth_stream, 2); }
2146 static PyObject * Degrade_setSrscale(Degrade *self, PyObject *arg) { SET_PARAM(self->srscale, self->srscale_stream, 3); }
2147
2148 static PyMemberDef Degrade_members[] =
2149 {
2150 {"server", T_OBJECT_EX, offsetof(Degrade, server), 0, "Pyo server."},
2151 {"stream", T_OBJECT_EX, offsetof(Degrade, stream), 0, "Stream object."},
2152 {"input", T_OBJECT_EX, offsetof(Degrade, input), 0, "Input sound object."},
2153 {"bitdepth", T_OBJECT_EX, offsetof(Degrade, bitdepth), 0, "Number of bits for amplitude values."},
2154 {"srscale", T_OBJECT_EX, offsetof(Degrade, srscale), 0, "Sampling depth factor."},
2155 {"mul", T_OBJECT_EX, offsetof(Degrade, mul), 0, "Mul factor."},
2156 {"add", T_OBJECT_EX, offsetof(Degrade, add), 0, "Add factor."},
2157 {NULL} /* Sentinel */
2158 };
2159
2160 static PyMethodDef Degrade_methods[] =
2161 {
2162 {"getServer", (PyCFunction)Degrade_getServer, METH_NOARGS, "Returns server object."},
2163 {"_getStream", (PyCFunction)Degrade_getStream, METH_NOARGS, "Returns stream object."},
2164 {"play", (PyCFunction)Degrade_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2165 {"out", (PyCFunction)Degrade_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2166 {"stop", (PyCFunction)Degrade_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2167 {"setBitdepth", (PyCFunction)Degrade_setBitdepth, METH_O, "Sets the bitdepth value."},
2168 {"setSrscale", (PyCFunction)Degrade_setSrscale, METH_O, "Sets the srscale value."},
2169 {"setMul", (PyCFunction)Degrade_setMul, METH_O, "Sets oscillator mul factor."},
2170 {"setAdd", (PyCFunction)Degrade_setAdd, METH_O, "Sets oscillator add factor."},
2171 {"setSub", (PyCFunction)Degrade_setSub, METH_O, "Sets inverse add factor."},
2172 {"setDiv", (PyCFunction)Degrade_setDiv, METH_O, "Sets inverse mul factor."},
2173 {NULL} /* Sentinel */
2174 };
2175
2176 static PyNumberMethods Degrade_as_number =
2177 {
2178 (binaryfunc)Degrade_add, /*nb_add*/
2179 (binaryfunc)Degrade_sub, /*nb_subtract*/
2180 (binaryfunc)Degrade_multiply, /*nb_multiply*/
2181 0, /*nb_remainder*/
2182 0, /*nb_divmod*/
2183 0, /*nb_power*/
2184 0, /*nb_neg*/
2185 0, /*nb_pos*/
2186 0, /*(unaryfunc)array_abs,*/
2187 0, /*nb_nonzero*/
2188 0, /*nb_invert*/
2189 0, /*nb_lshift*/
2190 0, /*nb_rshift*/
2191 0, /*nb_and*/
2192 0, /*nb_xor*/
2193 0, /*nb_or*/
2194 0, /*nb_int*/
2195 0, /*nb_long*/
2196 0, /*nb_float*/
2197 (binaryfunc)Degrade_inplace_add, /*inplace_add*/
2198 (binaryfunc)Degrade_inplace_sub, /*inplace_subtract*/
2199 (binaryfunc)Degrade_inplace_multiply, /*inplace_multiply*/
2200 0, /*inplace_remainder*/
2201 0, /*inplace_power*/
2202 0, /*inplace_lshift*/
2203 0, /*inplace_rshift*/
2204 0, /*inplace_and*/
2205 0, /*inplace_xor*/
2206 0, /*inplace_or*/
2207 0, /*nb_floor_divide*/
2208 (binaryfunc)Degrade_div, /*nb_true_divide*/
2209 0, /*nb_inplace_floor_divide*/
2210 (binaryfunc)Degrade_inplace_div, /*nb_inplace_true_divide*/
2211 0, /* nb_index */
2212 };
2213
2214 PyTypeObject DegradeType =
2215 {
2216 PyVarObject_HEAD_INIT(NULL, 0)
2217 "_pyo.Degrade_base", /*tp_name*/
2218 sizeof(Degrade), /*tp_basicsize*/
2219 0, /*tp_itemsize*/
2220 (destructor)Degrade_dealloc, /*tp_dealloc*/
2221 0, /*tp_print*/
2222 0, /*tp_getattr*/
2223 0, /*tp_setattr*/
2224 0, /*tp_as_async (tp_compare in Python 2)*/
2225 0, /*tp_repr*/
2226 &Degrade_as_number, /*tp_as_number*/
2227 0, /*tp_as_sequence*/
2228 0, /*tp_as_mapping*/
2229 0, /*tp_hash */
2230 0, /*tp_call*/
2231 0, /*tp_str*/
2232 0, /*tp_getattro*/
2233 0, /*tp_setattro*/
2234 0, /*tp_as_buffer*/
2235 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2236 "Degrade objects. Applies different bitdepth and sr on a signal.", /* tp_doc */
2237 (traverseproc)Degrade_traverse, /* tp_traverse */
2238 (inquiry)Degrade_clear, /* tp_clear */
2239 0, /* tp_richcompare */
2240 0, /* tp_weaklistoffset */
2241 0, /* tp_iter */
2242 0, /* tp_iternext */
2243 Degrade_methods, /* tp_methods */
2244 Degrade_members, /* tp_members */
2245 0, /* tp_getset */
2246 0, /* tp_base */
2247 0, /* tp_dict */
2248 0, /* tp_descr_get */
2249 0, /* tp_descr_set */
2250 0, /* tp_dictoffset */
2251 0, /* tp_init */
2252 0, /* tp_alloc */
2253 Degrade_new, /* tp_new */
2254 };
2255
2256 /************/
2257 /* Min */
2258 /************/
2259 typedef struct
2260 {
2261 pyo_audio_HEAD
2262 PyObject *input;
2263 Stream *input_stream;
2264 PyObject *comp;
2265 Stream *comp_stream;
2266 int modebuffer[3]; // need at least 2 slots for mul & add
2267 } Min;
2268
2269 static void
2270 Min_process_i(Min *self)
2271 {
2272 int i;
2273 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2274 MYFLT cp = PyFloat_AS_DOUBLE(self->comp);
2275
2276 for (i = 0; i < self->bufsize; i++)
2277 {
2278 self->data[i] = in[i] < cp ? in[i] : cp;
2279 }
2280 }
2281
2282 static void
2283 Min_process_a(Min *self)
2284 {
2285 int i;
2286 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2287 MYFLT *cp = Stream_getData((Stream *)self->comp_stream);
2288
2289 for (i = 0; i < self->bufsize; i++)
2290 {
2291 self->data[i] = in[i] < cp[i] ? in[i] : cp[i];
2292 }
2293 }
2294
2295 static void Min_postprocessing_ii(Min *self) { POST_PROCESSING_II };
2296 static void Min_postprocessing_ai(Min *self) { POST_PROCESSING_AI };
2297 static void Min_postprocessing_ia(Min *self) { POST_PROCESSING_IA };
2298 static void Min_postprocessing_aa(Min *self) { POST_PROCESSING_AA };
2299 static void Min_postprocessing_ireva(Min *self) { POST_PROCESSING_IREVA };
2300 static void Min_postprocessing_areva(Min *self) { POST_PROCESSING_AREVA };
2301 static void Min_postprocessing_revai(Min *self) { POST_PROCESSING_REVAI };
2302 static void Min_postprocessing_revaa(Min *self) { POST_PROCESSING_REVAA };
2303 static void Min_postprocessing_revareva(Min *self) { POST_PROCESSING_REVAREVA };
2304
2305 static void
2306 Min_setProcMode(Min *self)
2307 {
2308 int procmode, muladdmode;
2309 procmode = self->modebuffer[2];
2310 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2311
2312 switch (procmode)
2313 {
2314 case 0:
2315 self->proc_func_ptr = Min_process_i;
2316 break;
2317
2318 case 1:
2319 self->proc_func_ptr = Min_process_a;
2320 break;
2321 }
2322
2323 switch (muladdmode)
2324 {
2325 case 0:
2326 self->muladd_func_ptr = Min_postprocessing_ii;
2327 break;
2328
2329 case 1:
2330 self->muladd_func_ptr = Min_postprocessing_ai;
2331 break;
2332
2333 case 2:
2334 self->muladd_func_ptr = Min_postprocessing_revai;
2335 break;
2336
2337 case 10:
2338 self->muladd_func_ptr = Min_postprocessing_ia;
2339 break;
2340
2341 case 11:
2342 self->muladd_func_ptr = Min_postprocessing_aa;
2343 break;
2344
2345 case 12:
2346 self->muladd_func_ptr = Min_postprocessing_revaa;
2347 break;
2348
2349 case 20:
2350 self->muladd_func_ptr = Min_postprocessing_ireva;
2351 break;
2352
2353 case 21:
2354 self->muladd_func_ptr = Min_postprocessing_areva;
2355 break;
2356
2357 case 22:
2358 self->muladd_func_ptr = Min_postprocessing_revareva;
2359 break;
2360 }
2361 }
2362
2363 static void
2364 Min_compute_next_data_frame(Min *self)
2365 {
2366 (*self->proc_func_ptr)(self);
2367 (*self->muladd_func_ptr)(self);
2368 }
2369
2370 static int
2371 Min_traverse(Min *self, visitproc visit, void *arg)
2372 {
2373 pyo_VISIT
2374 Py_VISIT(self->input);
2375 Py_VISIT(self->comp);
2376 return 0;
2377 }
2378
2379 static int
2380 Min_clear(Min *self)
2381 {
2382 pyo_CLEAR
2383 Py_CLEAR(self->input);
2384 Py_CLEAR(self->comp);
2385 return 0;
2386 }
2387
2388 static void
2389 Min_dealloc(Min* self)
2390 {
2391 pyo_DEALLOC
2392 Min_clear(self);
2393 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2394 Py_TYPE(self)->tp_free((PyObject*)self);
2395 }
2396
2397 static PyObject *
2398 Min_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2399 {
2400 int i;
2401 PyObject *inputtmp, *input_streamtmp, *comptmp = NULL, *multmp = NULL, *addtmp = NULL;
2402 Min *self;
2403 self = (Min *)type->tp_alloc(type, 0);
2404
2405 self->comp = PyFloat_FromDouble(0.5);
2406 self->modebuffer[0] = 0;
2407 self->modebuffer[1] = 0;
2408 self->modebuffer[2] = 0;
2409
2410 INIT_OBJECT_COMMON
2411
2412 Stream_setFunctionPtr(self->stream, Min_compute_next_data_frame);
2413 self->mode_func_ptr = Min_setProcMode;
2414
2415 static char *kwlist[] = {"input", "comp", "mul", "add", NULL};
2416
2417 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &comptmp, &multmp, &addtmp))
2418 Py_RETURN_NONE;
2419
2420 INIT_INPUT_STREAM
2421
2422 if (comptmp)
2423 {
2424 PyObject_CallMethod((PyObject *)self, "setComp", "O", comptmp);
2425 }
2426
2427 if (multmp)
2428 {
2429 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2430 }
2431
2432 if (addtmp)
2433 {
2434 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2435 }
2436
2437 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2438
2439 (*self->mode_func_ptr)(self);
2440
2441 return (PyObject *)self;
2442 }
2443
2444 static PyObject * Min_getServer(Min* self) { GET_SERVER };
2445 static PyObject * Min_getStream(Min* self) { GET_STREAM };
2446 static PyObject * Min_setMul(Min *self, PyObject *arg) { SET_MUL };
2447 static PyObject * Min_setAdd(Min *self, PyObject *arg) { SET_ADD };
2448 static PyObject * Min_setSub(Min *self, PyObject *arg) { SET_SUB };
2449 static PyObject * Min_setDiv(Min *self, PyObject *arg) { SET_DIV };
2450
2451 static PyObject * Min_play(Min *self, PyObject *args, PyObject *kwds) { PLAY };
2452 static PyObject * Min_out(Min *self, PyObject *args, PyObject *kwds) { OUT };
2453 static PyObject * Min_stop(Min *self, PyObject *args, PyObject *kwds) { STOP };
2454
2455 static PyObject * Min_multiply(Min *self, PyObject *arg) { MULTIPLY };
2456 static PyObject * Min_inplace_multiply(Min *self, PyObject *arg) { INPLACE_MULTIPLY };
2457 static PyObject * Min_add(Min *self, PyObject *arg) { ADD };
2458 static PyObject * Min_inplace_add(Min *self, PyObject *arg) { INPLACE_ADD };
2459 static PyObject * Min_sub(Min *self, PyObject *arg) { SUB };
2460 static PyObject * Min_inplace_sub(Min *self, PyObject *arg) { INPLACE_SUB };
2461 static PyObject * Min_div(Min *self, PyObject *arg) { DIV };
2462 static PyObject * Min_inplace_div(Min *self, PyObject *arg) { INPLACE_DIV };
2463
2464 static PyObject * Min_setComp(Min *self, PyObject *arg) { SET_PARAM(self->comp, self->comp_stream, 2); }
2465
2466 static PyMemberDef Min_members[] =
2467 {
2468 {"server", T_OBJECT_EX, offsetof(Min, server), 0, "Pyo server."},
2469 {"stream", T_OBJECT_EX, offsetof(Min, stream), 0, "Stream object."},
2470 {"input", T_OBJECT_EX, offsetof(Min, input), 0, "Input sound object."},
2471 {"comp", T_OBJECT_EX, offsetof(Min, comp), 0, "Comparator."},
2472 {"mul", T_OBJECT_EX, offsetof(Min, mul), 0, "Mul factor."},
2473 {"add", T_OBJECT_EX, offsetof(Min, add), 0, "Add factor."},
2474 {NULL} /* Sentinel */
2475 };
2476
2477 static PyMethodDef Min_methods[] =
2478 {
2479 {"getServer", (PyCFunction)Min_getServer, METH_NOARGS, "Returns server object."},
2480 {"_getStream", (PyCFunction)Min_getStream, METH_NOARGS, "Returns stream object."},
2481 {"play", (PyCFunction)Min_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2482 {"out", (PyCFunction)Min_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2483 {"stop", (PyCFunction)Min_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2484 {"setComp", (PyCFunction)Min_setComp, METH_O, "Sets comparator."},
2485 {"setMul", (PyCFunction)Min_setMul, METH_O, "Sets oscillator mul factor."},
2486 {"setAdd", (PyCFunction)Min_setAdd, METH_O, "Sets oscillator add factor."},
2487 {"setSub", (PyCFunction)Min_setSub, METH_O, "Sets inverse add factor."},
2488 {"setDiv", (PyCFunction)Min_setDiv, METH_O, "Sets inverse mul factor."},
2489 {NULL} /* Sentinel */
2490 };
2491
2492 static PyNumberMethods Min_as_number =
2493 {
2494 (binaryfunc)Min_add, /*nb_add*/
2495 (binaryfunc)Min_sub, /*nb_subtract*/
2496 (binaryfunc)Min_multiply, /*nb_multiply*/
2497 0, /*nb_remainder*/
2498 0, /*nb_divmod*/
2499 0, /*nb_power*/
2500 0, /*nb_neg*/
2501 0, /*nb_pos*/
2502 0, /*(unaryfunc)array_abs,*/
2503 0, /*nb_nonzero*/
2504 0, /*nb_invert*/
2505 0, /*nb_lshift*/
2506 0, /*nb_rshift*/
2507 0, /*nb_and*/
2508 0, /*nb_xor*/
2509 0, /*nb_or*/
2510 0, /*nb_int*/
2511 0, /*nb_long*/
2512 0, /*nb_float*/
2513 (binaryfunc)Min_inplace_add, /*inplace_add*/
2514 (binaryfunc)Min_inplace_sub, /*inplace_subtract*/
2515 (binaryfunc)Min_inplace_multiply, /*inplace_multiply*/
2516 0, /*inplace_remainder*/
2517 0, /*inplace_power*/
2518 0, /*inplace_lshift*/
2519 0, /*inplace_rshift*/
2520 0, /*inplace_and*/
2521 0, /*inplace_xor*/
2522 0, /*inplace_or*/
2523 0, /*nb_floor_divide*/
2524 (binaryfunc)Min_div, /*nb_true_divide*/
2525 0, /*nb_inplace_floor_divide*/
2526 (binaryfunc)Min_inplace_div, /*nb_inplace_true_divide*/
2527 0, /* nb_index */
2528 };
2529
2530 PyTypeObject MinType =
2531 {
2532 PyVarObject_HEAD_INIT(NULL, 0)
2533 "_pyo.Min_base", /*tp_name*/
2534 sizeof(Min), /*tp_basicsize*/
2535 0, /*tp_itemsize*/
2536 (destructor)Min_dealloc, /*tp_dealloc*/
2537 0, /*tp_print*/
2538 0, /*tp_getattr*/
2539 0, /*tp_setattr*/
2540 0, /*tp_as_async (tp_compare in Python 2)*/
2541 0, /*tp_repr*/
2542 &Min_as_number, /*tp_as_number*/
2543 0, /*tp_as_sequence*/
2544 0, /*tp_as_mapping*/
2545 0, /*tp_hash */
2546 0, /*tp_call*/
2547 0, /*tp_str*/
2548 0, /*tp_getattro*/
2549 0, /*tp_setattro*/
2550 0, /*tp_as_buffer*/
2551 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2552 "Min objects. Outputs the minimum of two values.", /* tp_doc */
2553 (traverseproc)Min_traverse, /* tp_traverse */
2554 (inquiry)Min_clear, /* tp_clear */
2555 0, /* tp_richcompare */
2556 0, /* tp_weaklistoffset */
2557 0, /* tp_iter */
2558 0, /* tp_iternext */
2559 Min_methods, /* tp_methods */
2560 Min_members, /* tp_members */
2561 0, /* tp_getset */
2562 0, /* tp_base */
2563 0, /* tp_dict */
2564 0, /* tp_descr_get */
2565 0, /* tp_descr_set */
2566 0, /* tp_dictoffset */
2567 0, /* tp_init */
2568 0, /* tp_alloc */
2569 Min_new, /* tp_new */
2570 };
2571
2572 /************/
2573 /* Max */
2574 /************/
2575 typedef struct
2576 {
2577 pyo_audio_HEAD
2578 PyObject *input;
2579 Stream *input_stream;
2580 PyObject *comp;
2581 Stream *comp_stream;
2582 int modebuffer[3]; // need at least 2 slots for mul & add
2583 } Max;
2584
2585 static void
2586 Max_process_i(Max *self)
2587 {
2588 int i;
2589 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2590 MYFLT cp = PyFloat_AS_DOUBLE(self->comp);
2591
2592 for (i = 0; i < self->bufsize; i++)
2593 {
2594 self->data[i] = in[i] > cp ? in[i] : cp;
2595 }
2596 }
2597
2598 static void
2599 Max_process_a(Max *self)
2600 {
2601 int i;
2602 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2603 MYFLT *cp = Stream_getData((Stream *)self->comp_stream);
2604
2605 for (i = 0; i < self->bufsize; i++)
2606 {
2607 self->data[i] = in[i] > cp[i] ? in[i] : cp[i];
2608 }
2609 }
2610
2611 static void Max_postprocessing_ii(Max *self) { POST_PROCESSING_II };
2612 static void Max_postprocessing_ai(Max *self) { POST_PROCESSING_AI };
2613 static void Max_postprocessing_ia(Max *self) { POST_PROCESSING_IA };
2614 static void Max_postprocessing_aa(Max *self) { POST_PROCESSING_AA };
2615 static void Max_postprocessing_ireva(Max *self) { POST_PROCESSING_IREVA };
2616 static void Max_postprocessing_areva(Max *self) { POST_PROCESSING_AREVA };
2617 static void Max_postprocessing_revai(Max *self) { POST_PROCESSING_REVAI };
2618 static void Max_postprocessing_revaa(Max *self) { POST_PROCESSING_REVAA };
2619 static void Max_postprocessing_revareva(Max *self) { POST_PROCESSING_REVAREVA };
2620
2621 static void
2622 Max_setProcMode(Max *self)
2623 {
2624 int procmode, muladdmode;
2625 procmode = self->modebuffer[2];
2626 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2627
2628 switch (procmode)
2629 {
2630 case 0:
2631 self->proc_func_ptr = Max_process_i;
2632 break;
2633
2634 case 1:
2635 self->proc_func_ptr = Max_process_a;
2636 break;
2637 }
2638
2639 switch (muladdmode)
2640 {
2641 case 0:
2642 self->muladd_func_ptr = Max_postprocessing_ii;
2643 break;
2644
2645 case 1:
2646 self->muladd_func_ptr = Max_postprocessing_ai;
2647 break;
2648
2649 case 2:
2650 self->muladd_func_ptr = Max_postprocessing_revai;
2651 break;
2652
2653 case 10:
2654 self->muladd_func_ptr = Max_postprocessing_ia;
2655 break;
2656
2657 case 11:
2658 self->muladd_func_ptr = Max_postprocessing_aa;
2659 break;
2660
2661 case 12:
2662 self->muladd_func_ptr = Max_postprocessing_revaa;
2663 break;
2664
2665 case 20:
2666 self->muladd_func_ptr = Max_postprocessing_ireva;
2667 break;
2668
2669 case 21:
2670 self->muladd_func_ptr = Max_postprocessing_areva;
2671 break;
2672
2673 case 22:
2674 self->muladd_func_ptr = Max_postprocessing_revareva;
2675 break;
2676 }
2677 }
2678
2679 static void
2680 Max_compute_next_data_frame(Max *self)
2681 {
2682 (*self->proc_func_ptr)(self);
2683 (*self->muladd_func_ptr)(self);
2684 }
2685
2686 static int
2687 Max_traverse(Max *self, visitproc visit, void *arg)
2688 {
2689 pyo_VISIT
2690 Py_VISIT(self->input);
2691 Py_VISIT(self->comp);
2692 return 0;
2693 }
2694
2695 static int
2696 Max_clear(Max *self)
2697 {
2698 pyo_CLEAR
2699 Py_CLEAR(self->input);
2700 Py_CLEAR(self->comp);
2701 return 0;
2702 }
2703
2704 static void
2705 Max_dealloc(Max* self)
2706 {
2707 pyo_DEALLOC
2708 Max_clear(self);
2709 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2710 Py_TYPE(self)->tp_free((PyObject*)self);
2711 }
2712
2713 static PyObject *
2714 Max_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2715 {
2716 int i;
2717 PyObject *inputtmp, *input_streamtmp, *comptmp = NULL, *multmp = NULL, *addtmp = NULL;
2718 Max *self;
2719 self = (Max *)type->tp_alloc(type, 0);
2720
2721 self->comp = PyFloat_FromDouble(0.5);
2722 self->modebuffer[0] = 0;
2723 self->modebuffer[1] = 0;
2724 self->modebuffer[2] = 0;
2725
2726 INIT_OBJECT_COMMON
2727
2728 Stream_setFunctionPtr(self->stream, Max_compute_next_data_frame);
2729 self->mode_func_ptr = Max_setProcMode;
2730
2731 static char *kwlist[] = {"input", "comp", "mul", "add", NULL};
2732
2733 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &comptmp, &multmp, &addtmp))
2734 Py_RETURN_NONE;
2735
2736 INIT_INPUT_STREAM
2737
2738 if (comptmp)
2739 {
2740 PyObject_CallMethod((PyObject *)self, "setComp", "O", comptmp);
2741 }
2742
2743 if (multmp)
2744 {
2745 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2746 }
2747
2748 if (addtmp)
2749 {
2750 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2751 }
2752
2753 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2754
2755 (*self->mode_func_ptr)(self);
2756
2757 return (PyObject *)self;
2758 }
2759
2760 static PyObject * Max_getServer(Max* self) { GET_SERVER };
2761 static PyObject * Max_getStream(Max* self) { GET_STREAM };
2762 static PyObject * Max_setMul(Max *self, PyObject *arg) { SET_MUL };
2763 static PyObject * Max_setAdd(Max *self, PyObject *arg) { SET_ADD };
2764 static PyObject * Max_setSub(Max *self, PyObject *arg) { SET_SUB };
2765 static PyObject * Max_setDiv(Max *self, PyObject *arg) { SET_DIV };
2766
2767 static PyObject * Max_play(Max *self, PyObject *args, PyObject *kwds) { PLAY };
2768 static PyObject * Max_out(Max *self, PyObject *args, PyObject *kwds) { OUT };
2769 static PyObject * Max_stop(Max *self, PyObject *args, PyObject *kwds) { STOP };
2770
2771 static PyObject * Max_multiply(Max *self, PyObject *arg) { MULTIPLY };
2772 static PyObject * Max_inplace_multiply(Max *self, PyObject *arg) { INPLACE_MULTIPLY };
2773 static PyObject * Max_add(Max *self, PyObject *arg) { ADD };
2774 static PyObject * Max_inplace_add(Max *self, PyObject *arg) { INPLACE_ADD };
2775 static PyObject * Max_sub(Max *self, PyObject *arg) { SUB };
2776 static PyObject * Max_inplace_sub(Max *self, PyObject *arg) { INPLACE_SUB };
2777 static PyObject * Max_div(Max *self, PyObject *arg) { DIV };
2778 static PyObject * Max_inplace_div(Max *self, PyObject *arg) { INPLACE_DIV };
2779
2780 static PyObject * Max_setComp(Max *self, PyObject *arg) { SET_PARAM(self->comp, self->comp_stream, 2); }
2781
2782 static PyMemberDef Max_members[] =
2783 {
2784 {"server", T_OBJECT_EX, offsetof(Max, server), 0, "Pyo server."},
2785 {"stream", T_OBJECT_EX, offsetof(Max, stream), 0, "Stream object."},
2786 {"input", T_OBJECT_EX, offsetof(Max, input), 0, "Input sound object."},
2787 {"comp", T_OBJECT_EX, offsetof(Max, comp), 0, "Comparator."},
2788 {"mul", T_OBJECT_EX, offsetof(Max, mul), 0, "Mul factor."},
2789 {"add", T_OBJECT_EX, offsetof(Max, add), 0, "Add factor."},
2790 {NULL} /* Sentinel */
2791 };
2792
2793 static PyMethodDef Max_methods[] =
2794 {
2795 {"getServer", (PyCFunction)Max_getServer, METH_NOARGS, "Returns server object."},
2796 {"_getStream", (PyCFunction)Max_getStream, METH_NOARGS, "Returns stream object."},
2797 {"play", (PyCFunction)Max_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2798 {"out", (PyCFunction)Max_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2799 {"stop", (PyCFunction)Max_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2800 {"setComp", (PyCFunction)Max_setComp, METH_O, "Sets comparator."},
2801 {"setMul", (PyCFunction)Max_setMul, METH_O, "Sets oscillator mul factor."},
2802 {"setAdd", (PyCFunction)Max_setAdd, METH_O, "Sets oscillator add factor."},
2803 {"setSub", (PyCFunction)Max_setSub, METH_O, "Sets inverse add factor."},
2804 {"setDiv", (PyCFunction)Max_setDiv, METH_O, "Sets inverse mul factor."},
2805 {NULL} /* Sentinel */
2806 };
2807
2808 static PyNumberMethods Max_as_number =
2809 {
2810 (binaryfunc)Max_add, /*nb_add*/
2811 (binaryfunc)Max_sub, /*nb_subtract*/
2812 (binaryfunc)Max_multiply, /*nb_multiply*/
2813 0, /*nb_remainder*/
2814 0, /*nb_divmod*/
2815 0, /*nb_power*/
2816 0, /*nb_neg*/
2817 0, /*nb_pos*/
2818 0, /*(unaryfunc)array_abs,*/
2819 0, /*nb_nonzero*/
2820 0, /*nb_invert*/
2821 0, /*nb_lshift*/
2822 0, /*nb_rshift*/
2823 0, /*nb_and*/
2824 0, /*nb_xor*/
2825 0, /*nb_or*/
2826 0, /*nb_int*/
2827 0, /*nb_long*/
2828 0, /*nb_float*/
2829 (binaryfunc)Max_inplace_add, /*inplace_add*/
2830 (binaryfunc)Max_inplace_sub, /*inplace_subtract*/
2831 (binaryfunc)Max_inplace_multiply, /*inplace_multiply*/
2832 0, /*inplace_remainder*/
2833 0, /*inplace_power*/
2834 0, /*inplace_lshift*/
2835 0, /*inplace_rshift*/
2836 0, /*inplace_and*/
2837 0, /*inplace_xor*/
2838 0, /*inplace_or*/
2839 0, /*nb_floor_divide*/
2840 (binaryfunc)Max_div, /*nb_true_divide*/
2841 0, /*nb_inplace_floor_divide*/
2842 (binaryfunc)Max_inplace_div, /*nb_inplace_true_divide*/
2843 0, /* nb_index */
2844 };
2845
2846 PyTypeObject MaxType =
2847 {
2848 PyVarObject_HEAD_INIT(NULL, 0)
2849 "_pyo.Max_base", /*tp_name*/
2850 sizeof(Max), /*tp_basicsize*/
2851 0, /*tp_itemsize*/
2852 (destructor)Max_dealloc, /*tp_dealloc*/
2853 0, /*tp_print*/
2854 0, /*tp_getattr*/
2855 0, /*tp_setattr*/
2856 0, /*tp_as_async (tp_compare in Python 2)*/
2857 0, /*tp_repr*/
2858 &Max_as_number, /*tp_as_number*/
2859 0, /*tp_as_sequence*/
2860 0, /*tp_as_mapping*/
2861 0, /*tp_hash */
2862 0, /*tp_call*/
2863 0, /*tp_str*/
2864 0, /*tp_getattro*/
2865 0, /*tp_setattro*/
2866 0, /*tp_as_buffer*/
2867 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2868 "Max objects. Outputs the Maximum of two values.", /* tp_doc */
2869 (traverseproc)Max_traverse, /* tp_traverse */
2870 (inquiry)Max_clear, /* tp_clear */
2871 0, /* tp_richcompare */
2872 0, /* tp_weaklistoffset */
2873 0, /* tp_iter */
2874 0, /* tp_iternext */
2875 Max_methods, /* tp_methods */
2876 Max_members, /* tp_members */
2877 0, /* tp_getset */
2878 0, /* tp_base */
2879 0, /* tp_dict */
2880 0, /* tp_descr_get */
2881 0, /* tp_descr_set */
2882 0, /* tp_dictoffset */
2883 0, /* tp_init */
2884 0, /* tp_alloc */
2885 Max_new, /* tp_new */
2886 };