Debian Codegraph @

src/objects/panmodule.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 *pan;
35 Stream *pan_stream;
36 PyObject *spread;
37 Stream *spread_stream;
38 int chnls;
39 int modebuffer[2];
40 MYFLT *buffer_streams;
41 } Panner;
42
43 static MYFLT
44 P_clip(MYFLT p)
45 {
46 if (p < 0.0)
47 return 0.0;
48 else if (p > 1.0)
49 return 1.0;
50 else
51 return p;
52 }
53
54 static void
55 Panner_splitter_thru(Panner *self)
56 {
57 int i;
58 MYFLT *in = Stream_getData((Stream *)self->input_stream);
59
60 for (i = 0; i < self->bufsize; i++)
61 {
62 self->buffer_streams[i] = in[i];
63 }
64 }
65
66 static void
67 Panner_splitter_st_i(Panner *self)
68 {
69 int i;
70 MYFLT inval, pi_over_two = PI / 2.0;
71 MYFLT *in = Stream_getData((Stream *)self->input_stream);
72
73 MYFLT pan = PyFloat_AS_DOUBLE(self->pan);
74 pan = P_clip(pan) * pi_over_two;
75
76 for (i = 0; i < self->bufsize; i++)
77 {
78 inval = in[i];
79 self->buffer_streams[i] = inval * MYCOS(pan);
80 self->buffer_streams[i + self->bufsize] = inval * MYSIN(pan);
81 }
82 }
83
84 static void
85 Panner_splitter_st_a(Panner *self)
86 {
87 int i;
88 MYFLT inval, panval, pi_over_two = PI / 2.0;
89 MYFLT *in = Stream_getData((Stream *)self->input_stream);
90
91 MYFLT *pan = Stream_getData((Stream *)self->pan_stream);
92
93 for (i = 0; i < self->bufsize; i++)
94 {
95 inval = in[i];
96 panval = P_clip(pan[i]) * pi_over_two;
97 self->buffer_streams[i] = inval * MYCOS(panval);
98 self->buffer_streams[i + self->bufsize] = inval * MYSIN(panval);
99 }
100 }
101
102 static void
103 Panner_splitter_ii(Panner *self)
104 {
105 MYFLT val, inval, phase, sprd;
106 int j, i;
107 MYFLT *in = Stream_getData((Stream *)self->input_stream);
108
109 MYFLT pan = PyFloat_AS_DOUBLE(self->pan);
110 MYFLT spd = PyFloat_AS_DOUBLE(self->spread);
111
112 pan = P_clip(pan);
113 spd = P_clip(spd);
114
115 sprd = 20.0 - (MYSQRT(spd) * 20.0) + 0.1;
116
117 for (i = 0; i < self->bufsize; i++)
118 {
119 inval = in[i];
120
121 for (j = 0; j < self->chnls; j++)
122 {
123 phase = j / (MYFLT)self->chnls;
124 val = inval * MYPOW(MYCOS((pan - phase) * TWOPI) * 0.5 + 0.5, sprd);
125 self->buffer_streams[i + j * self->bufsize] = val;
126 }
127 }
128 }
129
130 static void
131 Panner_splitter_ai(Panner *self)
132 {
133 MYFLT val, inval, phase, sprd;
134 int j, i;
135 MYFLT *in = Stream_getData((Stream *)self->input_stream);
136
137 MYFLT *pan = Stream_getData((Stream *)self->pan_stream);
138 MYFLT spd = PyFloat_AS_DOUBLE(self->spread);
139
140 spd = P_clip(spd);
141
142 sprd = 20.0 - (MYSQRT(spd) * 20.0) + 0.1;
143
144 for (i = 0; i < self->bufsize; i++)
145 {
146 inval = in[i];
147
148 for (j = 0; j < self->chnls; j++)
149 {
150 phase = j / (MYFLT)self->chnls;
151 val = inval * MYPOW(MYCOS((P_clip(pan[i]) - phase) * TWOPI) * 0.5 + 0.5, sprd);
152 self->buffer_streams[i + j * self->bufsize] = val;
153 }
154 }
155 }
156
157 static void
158 Panner_splitter_ia(Panner *self)
159 {
160 MYFLT val, inval, phase, spdval, sprd;
161 int j, i;
162 MYFLT *in = Stream_getData((Stream *)self->input_stream);
163
164 MYFLT pan = PyFloat_AS_DOUBLE(self->pan);
165 MYFLT *spd = Stream_getData((Stream *)self->spread_stream);
166
167 pan = P_clip(pan);
168
169 for (i = 0; i < self->bufsize; i++)
170 {
171 inval = in[i];
172 spdval = P_clip(spd[i]);
173 sprd = 20.0 - (MYSQRT(spdval) * 20.0) + 0.1;
174
175 for (j = 0; j < self->chnls; j++)
176 {
177 phase = j / (MYFLT)self->chnls;
178 val = inval * MYPOW(MYCOS((pan - phase) * TWOPI) * 0.5 + 0.5, sprd);
179 self->buffer_streams[i + j * self->bufsize] = val;
180 }
181 }
182 }
183
184 static void
185 Panner_splitter_aa(Panner *self)
186 {
187 MYFLT val, inval, phase, spdval, sprd;
188 int j, i;
189 MYFLT *in = Stream_getData((Stream *)self->input_stream);
190
191 MYFLT *pan = Stream_getData((Stream *)self->pan_stream);
192 MYFLT *spd = Stream_getData((Stream *)self->spread_stream);
193
194
195 for (i = 0; i < self->bufsize; i++)
196 {
197 inval = in[i];
198 spdval = P_clip(spd[i]);
199 sprd = 20.0 - (MYSQRT(spdval) * 20.0) + 0.1;
200
201 for (j = 0; j < self->chnls; j++)
202 {
203 phase = j / (MYFLT)self->chnls;
204 val = inval * MYPOW(MYCOS((P_clip(pan[i]) - phase) * TWOPI) * 0.5 + 0.5, sprd);
205 self->buffer_streams[i + j * self->bufsize] = val;
206 }
207 }
208 }
209
210 MYFLT *
211 Panner_getSamplesBuffer(Panner *self)
212 {
213 return (MYFLT *)self->buffer_streams;
214 }
215
216 static void
217 Panner_setProcMode(Panner *self)
218 {
219 int procmode;
220 procmode = self->modebuffer[0] + self->modebuffer[1] * 10;
221
222 if (self->chnls > 2)
223 {
224 switch (procmode)
225 {
226 case 0:
227 self->proc_func_ptr = Panner_splitter_ii;
228 break;
229
230 case 1:
231 self->proc_func_ptr = Panner_splitter_ai;
232 break;
233
234 case 10:
235 self->proc_func_ptr = Panner_splitter_ia;
236 break;
237
238 case 11:
239 self->proc_func_ptr = Panner_splitter_aa;
240 break;
241 }
242 }
243 else if (self->chnls == 2)
244 {
245 switch (self->modebuffer[0])
246 {
247 case 0:
248 self->proc_func_ptr = Panner_splitter_st_i;
249 break;
250
251 case 1:
252 self->proc_func_ptr = Panner_splitter_st_a;
253 break;
254 }
255 }
256 else if (self->chnls == 1)
257 {
258 self->proc_func_ptr = Panner_splitter_thru;
259 }
260 }
261
262 static void
263 Panner_compute_next_data_frame(Panner *self)
264 {
265 (*self->proc_func_ptr)(self);
266 }
267
268 static int
269 Panner_traverse(Panner *self, visitproc visit, void *arg)
270 {
271 pyo_VISIT
272 Py_VISIT(self->input);
273 Py_VISIT(self->pan);
274 Py_VISIT(self->spread);
275 return 0;
276 }
277
278 static int
279 Panner_clear(Panner *self)
280 {
281 pyo_CLEAR
282 Py_CLEAR(self->input);
283 Py_CLEAR(self->pan);
284 Py_CLEAR(self->spread);
285 return 0;
286 }
287
288 static void
289 Panner_dealloc(Panner* self)
290 {
291 pyo_DEALLOC
292 PyMem_RawFree(self->buffer_streams);
293 Panner_clear(self);
294 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
295 Py_TYPE(self)->tp_free((PyObject*)self);
296 }
297
298 static PyObject *
299 Panner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
300 {
301 int i;
302 PyObject *inputtmp, *input_streamtmp, *pantmp = NULL, *spreadtmp = NULL;
303 Panner *self;
304 self = (Panner *)type->tp_alloc(type, 0);
305
306 INIT_OBJECT_COMMON
307 Stream_setFunctionPtr(self->stream, Panner_compute_next_data_frame);
308 self->mode_func_ptr = Panner_setProcMode;
309
310 self->pan = PyFloat_FromDouble(0.5);
311 self->spread = PyFloat_FromDouble(0.5);
312 self->chnls = 2;
313 self->modebuffer[0] = 0;
314 self->modebuffer[1] = 0;
315
316 static char *kwlist[] = {"input", "outs", "pan", "spread", NULL};
317
318 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &inputtmp, &self->chnls, &pantmp, &spreadtmp))
319 Py_RETURN_NONE;
320
321 INIT_INPUT_STREAM
322
323 if (pantmp)
324 {
325 PyObject_CallMethod((PyObject *)self, "setPan", "O", pantmp);
326 }
327
328 if (spreadtmp)
329 {
330 PyObject_CallMethod((PyObject *)self, "setSpread", "O", spreadtmp);
331 }
332
333 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
334
335 if (self->chnls < 1)
336 self->chnls = 1;
337
338 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->chnls * self->bufsize * sizeof(MYFLT));
339
340 (*self->mode_func_ptr)(self);
341
342 return (PyObject *)self;
343 }
344
345 static PyObject * Panner_getServer(Panner* self) { GET_SERVER };
346 static PyObject * Panner_getStream(Panner* self) { GET_STREAM };
347
348 static PyObject * Panner_play(Panner *self, PyObject *args, PyObject *kwds) { PLAY };
349 static PyObject * Panner_stop(Panner *self, PyObject *args, PyObject *kwds) { STOP };
350
351 static PyObject * Panner_setPan(Panner *self, PyObject *arg) { SET_PARAM(self->pan, self->pan_stream, 0); }
352 static PyObject * Panner_setSpread(Panner *self, PyObject *arg) { SET_PARAM(self->spread, self->spread_stream, 1); }
353
354 static PyMemberDef Panner_members[] =
355 {
356 {"server", T_OBJECT_EX, offsetof(Panner, server), 0, "Pyo server."},
357 {"stream", T_OBJECT_EX, offsetof(Panner, stream), 0, "Stream object."},
358 {"input", T_OBJECT_EX, offsetof(Panner, input), 0, "Input sound object."},
359 {"pan", T_OBJECT_EX, offsetof(Panner, pan), 0, "Pan object."},
360 {"spread", T_OBJECT_EX, offsetof(Panner, spread), 0, "Spread object."},
361 {NULL} /* Sentinel */
362 };
363
364 static PyMethodDef Panner_methods[] =
365 {
366 {"getServer", (PyCFunction)Panner_getServer, METH_NOARGS, "Returns server object."},
367 {"_getStream", (PyCFunction)Panner_getStream, METH_NOARGS, "Returns stream object."},
368 {"play", (PyCFunction)Panner_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
369 {"stop", (PyCFunction)Panner_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
370 {"setPan", (PyCFunction)Panner_setPan, METH_O, "Sets panning value between 0 and 1."},
371 {"setSpread", (PyCFunction)Panner_setSpread, METH_O, "Sets spreading value between 0 and 1."},
372 {NULL} /* Sentinel */
373 };
374
375 PyTypeObject PannerType =
376 {
377 PyVarObject_HEAD_INIT(NULL, 0)
378 "_pyo.Panner_base", /*tp_name*/
379 sizeof(Panner), /*tp_basicsize*/
380 0, /*tp_itemsize*/
381 (destructor)Panner_dealloc, /*tp_dealloc*/
382 0, /*tp_print*/
383 0, /*tp_getattr*/
384 0, /*tp_setattr*/
385 0, /*tp_as_async (tp_compare in Python 2)*/
386 0, /*tp_repr*/
387 0, /*tp_as_number*/
388 0, /*tp_as_sequence*/
389 0, /*tp_as_mapping*/
390 0, /*tp_hash */
391 0, /*tp_call*/
392 0, /*tp_str*/
393 0, /*tp_getattro*/
394 0, /*tp_setattro*/
395 0, /*tp_as_buffer*/
396 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
397 "Panner main objects.", /* tp_doc */
398 (traverseproc)Panner_traverse, /* tp_traverse */
399 (inquiry)Panner_clear, /* tp_clear */
400 0, /* tp_richcompare */
401 0, /* tp_weaklistoffset */
402 0, /* tp_iter */
403 0, /* tp_iternext */
404 Panner_methods, /* tp_methods */
405 Panner_members, /* tp_members */
406 0, /* tp_getset */
407 0, /* tp_base */
408 0, /* tp_dict */
409 0, /* tp_descr_get */
410 0, /* tp_descr_set */
411 0, /* tp_dictoffset */
412 0, /* tp_init */
413 0, /* tp_alloc */
414 Panner_new, /* tp_new */
415 };
416
417 /************************************************************************************************/
418 /* Pan streamer object */
419 /************************************************************************************************/
420 typedef struct
421 {
422 pyo_audio_HEAD
423 Panner *mainSplitter;
424 int modebuffer[2];
425 int chnl; // panning order
426 } Pan;
427
428 static void Pan_postprocessing_ii(Pan *self) { POST_PROCESSING_II };
429 static void Pan_postprocessing_ai(Pan *self) { POST_PROCESSING_AI };
430 static void Pan_postprocessing_ia(Pan *self) { POST_PROCESSING_IA };
431 static void Pan_postprocessing_aa(Pan *self) { POST_PROCESSING_AA };
432 static void Pan_postprocessing_ireva(Pan *self) { POST_PROCESSING_IREVA };
433 static void Pan_postprocessing_areva(Pan *self) { POST_PROCESSING_AREVA };
434 static void Pan_postprocessing_revai(Pan *self) { POST_PROCESSING_REVAI };
435 static void Pan_postprocessing_revaa(Pan *self) { POST_PROCESSING_REVAA };
436 static void Pan_postprocessing_revareva(Pan *self) { POST_PROCESSING_REVAREVA };
437
438 static void
439 Pan_setProcMode(Pan *self)
440 {
441 int muladdmode;
442 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
443
444 switch (muladdmode)
445 {
446 case 0:
447 self->muladd_func_ptr = Pan_postprocessing_ii;
448 break;
449
450 case 1:
451 self->muladd_func_ptr = Pan_postprocessing_ai;
452 break;
453
454 case 2:
455 self->muladd_func_ptr = Pan_postprocessing_revai;
456 break;
457
458 case 10:
459 self->muladd_func_ptr = Pan_postprocessing_ia;
460 break;
461
462 case 11:
463 self->muladd_func_ptr = Pan_postprocessing_aa;
464 break;
465
466 case 12:
467 self->muladd_func_ptr = Pan_postprocessing_revaa;
468 break;
469
470 case 20:
471 self->muladd_func_ptr = Pan_postprocessing_ireva;
472 break;
473
474 case 21:
475 self->muladd_func_ptr = Pan_postprocessing_areva;
476 break;
477
478 case 22:
479 self->muladd_func_ptr = Pan_postprocessing_revareva;
480 break;
481 }
482 }
483
484 static void
485 Pan_compute_next_data_frame(Pan *self)
486 {
487 int i;
488 MYFLT *tmp;
489 int offset = self->chnl * self->bufsize;
490 tmp = Panner_getSamplesBuffer((Panner *)self->mainSplitter);
491
492 for (i = 0; i < self->bufsize; i++)
493 {
494 self->data[i] = tmp[i + offset];
495 }
496
497 (*self->muladd_func_ptr)(self);
498 }
499
500 static int
501 Pan_traverse(Pan *self, visitproc visit, void *arg)
502 {
503 pyo_VISIT
504 Py_VISIT(self->mainSplitter);
505 return 0;
506 }
507
508 static int
509 Pan_clear(Pan *self)
510 {
511 pyo_CLEAR
512 Py_CLEAR(self->mainSplitter);
513 return 0;
514 }
515
516 static void
517 Pan_dealloc(Pan* self)
518 {
519 pyo_DEALLOC
520 Pan_clear(self);
521 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
522 Py_TYPE(self)->tp_free((PyObject*)self);
523 }
524
525 static PyObject *
526 Pan_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
527 {
528 int i;
529 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
530 Pan *self;
531 self = (Pan *)type->tp_alloc(type, 0);
532
533 self->modebuffer[0] = 0;
534 self->modebuffer[1] = 0;
535
536 INIT_OBJECT_COMMON
537 Stream_setFunctionPtr(self->stream, Pan_compute_next_data_frame);
538 self->mode_func_ptr = Pan_setProcMode;
539
540 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
541
542 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
543 Py_RETURN_NONE;
544
545 self->mainSplitter = (Panner *)maintmp;
546 Py_INCREF(self->mainSplitter);
547
548 if (multmp)
549 {
550 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
551 }
552
553 if (addtmp)
554 {
555 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
556 }
557
558 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
559
560 (*self->mode_func_ptr)(self);
561
562 return (PyObject *)self;
563 }
564
565 static PyObject * Pan_getServer(Pan* self) { GET_SERVER };
566 static PyObject * Pan_getStream(Pan* self) { GET_STREAM };
567 static PyObject * Pan_setMul(Pan *self, PyObject *arg) { SET_MUL };
568 static PyObject * Pan_setAdd(Pan *self, PyObject *arg) { SET_ADD };
569 static PyObject * Pan_setSub(Pan *self, PyObject *arg) { SET_SUB };
570 static PyObject * Pan_setDiv(Pan *self, PyObject *arg) { SET_DIV };
571
572 static PyObject * Pan_play(Pan *self, PyObject *args, PyObject *kwds) { PLAY };
573 static PyObject * Pan_out(Pan *self, PyObject *args, PyObject *kwds) { OUT };
574 static PyObject * Pan_stop(Pan *self, PyObject *args, PyObject *kwds) { STOP };
575
576 static PyObject * Pan_multiply(Pan *self, PyObject *arg) { MULTIPLY };
577 static PyObject * Pan_inplace_multiply(Pan *self, PyObject *arg) { INPLACE_MULTIPLY };
578 static PyObject * Pan_add(Pan *self, PyObject *arg) { ADD };
579 static PyObject * Pan_inplace_add(Pan *self, PyObject *arg) { INPLACE_ADD };
580 static PyObject * Pan_sub(Pan *self, PyObject *arg) { SUB };
581 static PyObject * Pan_inplace_sub(Pan *self, PyObject *arg) { INPLACE_SUB };
582 static PyObject * Pan_div(Pan *self, PyObject *arg) { DIV };
583 static PyObject * Pan_inplace_div(Pan *self, PyObject *arg) { INPLACE_DIV };
584
585 static PyMemberDef Pan_members[] =
586 {
587 {"server", T_OBJECT_EX, offsetof(Pan, server), 0, "Pyo server."},
588 {"stream", T_OBJECT_EX, offsetof(Pan, stream), 0, "Stream object."},
589 {"mul", T_OBJECT_EX, offsetof(Pan, mul), 0, "Mul factor."},
590 {"add", T_OBJECT_EX, offsetof(Pan, add), 0, "Add factor."},
591 {NULL} /* Sentinel */
592 };
593
594 static PyMethodDef Pan_methods[] =
595 {
596 {"getServer", (PyCFunction)Pan_getServer, METH_NOARGS, "Returns server object."},
597 {"_getStream", (PyCFunction)Pan_getStream, METH_NOARGS, "Returns stream object."},
598 {"play", (PyCFunction)Pan_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
599 {"out", (PyCFunction)Pan_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
600 {"stop", (PyCFunction)Pan_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
601 {"setMul", (PyCFunction)Pan_setMul, METH_O, "Sets Pan mul factor."},
602 {"setAdd", (PyCFunction)Pan_setAdd, METH_O, "Sets Pan add factor."},
603 {"setSub", (PyCFunction)Pan_setSub, METH_O, "Sets inverse add factor."},
604 {"setDiv", (PyCFunction)Pan_setDiv, METH_O, "Sets inverse mul factor."},
605 {NULL} /* Sentinel */
606 };
607
608 static PyNumberMethods Pan_as_number =
609 {
610 (binaryfunc)Pan_add, /*nb_add*/
611 (binaryfunc)Pan_sub, /*nb_subtract*/
612 (binaryfunc)Pan_multiply, /*nb_multiply*/
613 0, /*nb_remainder*/
614 0, /*nb_divmod*/
615 0, /*nb_power*/
616 0, /*nb_neg*/
617 0, /*nb_pos*/
618 0, /*(unaryfunc)array_abs,*/
619 0, /*nb_nonzero*/
620 0, /*nb_invert*/
621 0, /*nb_lshift*/
622 0, /*nb_rshift*/
623 0, /*nb_and*/
624 0, /*nb_xor*/
625 0, /*nb_or*/
626 0, /*nb_int*/
627 0, /*nb_long*/
628 0, /*nb_float*/
629 (binaryfunc)Pan_inplace_add, /*inplace_add*/
630 (binaryfunc)Pan_inplace_sub, /*inplace_subtract*/
631 (binaryfunc)Pan_inplace_multiply, /*inplace_multiply*/
632 0, /*inplace_remainder*/
633 0, /*inplace_power*/
634 0, /*inplace_lshift*/
635 0, /*inplace_rshift*/
636 0, /*inplace_and*/
637 0, /*inplace_xor*/
638 0, /*inplace_or*/
639 0, /*nb_floor_divide*/
640 (binaryfunc)Pan_div, /*nb_true_divide*/
641 0, /*nb_inplace_floor_divide*/
642 (binaryfunc)Pan_inplace_div, /*nb_inplace_true_divide*/
643 0, /* nb_index */
644 };
645
646 PyTypeObject PanType =
647 {
648 PyVarObject_HEAD_INIT(NULL, 0)
649 "_pyo.Pan_base", /*tp_name*/
650 sizeof(Pan), /*tp_basicsize*/
651 0, /*tp_itemsize*/
652 (destructor)Pan_dealloc, /*tp_dealloc*/
653 0, /*tp_print*/
654 0, /*tp_getattr*/
655 0, /*tp_setattr*/
656 0, /*tp_as_async (tp_compare in Python 2)*/
657 0, /*tp_repr*/
658 &Pan_as_number, /*tp_as_number*/
659 0, /*tp_as_sequence*/
660 0, /*tp_as_mapping*/
661 0, /*tp_hash */
662 0, /*tp_call*/
663 0, /*tp_str*/
664 0, /*tp_getattro*/
665 0, /*tp_setattro*/
666 0, /*tp_as_buffer*/
667 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
668 "Pan objects. Reads one band from a Panner.", /* tp_doc */
669 (traverseproc)Pan_traverse, /* tp_traverse */
670 (inquiry)Pan_clear, /* tp_clear */
671 0, /* tp_richcompare */
672 0, /* tp_weaklistoffset */
673 0, /* tp_iter */
674 0, /* tp_iternext */
675 Pan_methods, /* tp_methods */
676 Pan_members, /* tp_members */
677 0, /* tp_getset */
678 0, /* tp_base */
679 0, /* tp_dict */
680 0, /* tp_descr_get */
681 0, /* tp_descr_set */
682 0, /* tp_dictoffset */
683 0, /* tp_init */
684 0, /* tp_alloc */
685 Pan_new, /* tp_new */
686 };
687
688 /*********************/
689 /*** Simple panner ***/
690 /*********************/
691 typedef struct
692 {
693 pyo_audio_HEAD
694 PyObject *input;
695 Stream *input_stream;
696 PyObject *pan;
697 Stream *pan_stream;
698 int chnls;
699 int k1;
700 int k2;
701 int modebuffer[1];
702 MYFLT *buffer_streams;
703 } SPanner;
704
705 static void
706 SPanner_splitter_thru(SPanner *self)
707 {
708 int i;
709 MYFLT *in = Stream_getData((Stream *)self->input_stream);
710
711 for (i = 0; i < self->bufsize; i++)
712 {
713 self->buffer_streams[i] = in[i];
714 }
715 }
716
717 static void
718 SPanner_splitter_st_i(SPanner *self)
719 {
720 int i;
721 MYFLT inval;
722 MYFLT *in = Stream_getData((Stream *)self->input_stream);
723
724 MYFLT pan = P_clip(PyFloat_AS_DOUBLE(self->pan));
725
726 for (i = 0; i < self->bufsize; i++)
727 {
728 inval = in[i];
729 self->buffer_streams[i] = inval * MYSQRT(1.0 - pan);
730 self->buffer_streams[i + self->bufsize] = inval * MYSQRT(pan);
731 }
732 }
733
734 static void
735 SPanner_splitter_st_a(SPanner *self)
736 {
737 int i;
738 MYFLT inval, panval;
739 MYFLT *in = Stream_getData((Stream *)self->input_stream);
740
741 MYFLT *pan = Stream_getData((Stream *)self->pan_stream);
742
743 for (i = 0; i < self->bufsize; i++)
744 {
745 inval = in[i];
746 panval = P_clip(pan[i]);
747 self->buffer_streams[i] = inval * MYSQRT(1.0 - panval);
748 self->buffer_streams[i + self->bufsize] = inval * MYSQRT(panval);
749 }
750 }
751
752 static void
753 SPanner_splitter_i(SPanner *self)
754 {
755 MYFLT inval, min, pan1, pan2;
756 int j, i;
757 MYFLT *in = Stream_getData((Stream *)self->input_stream);
758 MYFLT pan = PyFloat_AS_DOUBLE(self->pan);
759 MYFLT fchnls = (MYFLT)self->chnls;
760
761 for (i = 0; i < self->bufsize; i++)
762 {
763 self->buffer_streams[i + self->k1] = 0.0;
764 self->buffer_streams[i + self->k2] = 0.0;
765 }
766
767 min = 0;
768 self->k1 = 0;
769 self->k2 = self->bufsize;
770
771 for (j = self->chnls; j > 0; j--)
772 {
773 int j1 = j - 1;
774 min = j1 / fchnls;
775
776 if (pan > min)
777 {
778 self->k1 = j1 * self->bufsize;
779
780 if (j == self->chnls)
781 self->k2 = 0;
782 else
783 self->k2 = j * self->bufsize;
784
785 break;
786 }
787 }
788
789 pan = P_clip((pan - min) * self->chnls);
790 pan1 = MYSQRT(1.0 - pan);
791 pan2 = MYSQRT(pan);
792
793 for (i = 0; i < self->bufsize; i++)
794 {
795 inval = in[i];
796 self->buffer_streams[i + self->k1] = inval * pan1;
797 self->buffer_streams[i + self->k2] = inval * pan2;
798 }
799 }
800
801 static void
802 SPanner_splitter_a(SPanner *self)
803 {
804 MYFLT inval, min, pan;
805 int i, j, j1, len;
806 MYFLT *in = Stream_getData((Stream *)self->input_stream);
807 MYFLT *apan = Stream_getData((Stream *)self->pan_stream);
808 MYFLT fchnls = (MYFLT)self->chnls;
809
810 len = self->chnls * self->bufsize;
811
812 for (i = 0; i < len; i++)
813 {
814 self->buffer_streams[i] = 0.0;
815 }
816
817 for (i = 0; i < self->bufsize; i++)
818 {
819 pan = apan[i];
820 inval = in[i];
821 min = 0;
822 self->k1 = 0;
823 self->k2 = self->bufsize;
824
825 for (j = self->chnls; j > 0; j--)
826 {
827 j1 = j - 1;
828 min = j1 / fchnls;
829
830 if (pan > min)
831 {
832 self->k1 = j1 * self->bufsize;
833
834 if (j == self->chnls)
835 self->k2 = 0;
836 else
837 self->k2 = j * self->bufsize;
838
839 break;
840 }
841 }
842
843 pan = P_clip((pan - min) * self->chnls);
844 self->buffer_streams[i + self->k1] = inval * MYSQRT(1.0 - pan);
845 self->buffer_streams[i + self->k2] = inval * MYSQRT(pan);
846 }
847 }
848
849 MYFLT *
850 SPanner_getSamplesBuffer(SPanner *self)
851 {
852 return (MYFLT *)self->buffer_streams;
853 }
854
855 static void
856 SPanner_setProcMode(SPanner *self)
857 {
858 int procmode;
859 procmode = self->modebuffer[0];
860
861 if (self->chnls > 2)
862 {
863 switch (procmode)
864 {
865 case 0:
866 self->proc_func_ptr = SPanner_splitter_i;
867 break;
868
869 case 1:
870 self->proc_func_ptr = SPanner_splitter_a;
871 break;
872 }
873 }
874 else if (self->chnls == 2)
875 {
876 switch (self->modebuffer[0])
877 {
878 case 0:
879 self->proc_func_ptr = SPanner_splitter_st_i;
880 break;
881
882 case 1:
883 self->proc_func_ptr = SPanner_splitter_st_a;
884 break;
885 }
886 }
887 else if (self->chnls == 1)
888 {
889 self->proc_func_ptr = SPanner_splitter_thru;
890 }
891 }
892
893 static void
894 SPanner_compute_next_data_frame(SPanner *self)
895 {
896 (*self->proc_func_ptr)(self);
897 }
898
899 static int
900 SPanner_traverse(SPanner *self, visitproc visit, void *arg)
901 {
902 pyo_VISIT
903 Py_VISIT(self->input);
904 Py_VISIT(self->pan);
905 return 0;
906 }
907
908 static int
909 SPanner_clear(SPanner *self)
910 {
911 pyo_CLEAR
912 Py_CLEAR(self->input);
913 Py_CLEAR(self->pan);
914 return 0;
915 }
916
917 static void
918 SPanner_dealloc(SPanner* self)
919 {
920 pyo_DEALLOC
921 PyMem_RawFree(self->buffer_streams);
922 SPanner_clear(self);
923 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
924 Py_TYPE(self)->tp_free((PyObject*)self);
925 }
926
927 static PyObject *
928 SPanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
929 {
930 int i;
931 PyObject *inputtmp, *input_streamtmp, *pantmp = NULL;
932 SPanner *self;
933 self = (SPanner *)type->tp_alloc(type, 0);
934
935 INIT_OBJECT_COMMON
936 Stream_setFunctionPtr(self->stream, SPanner_compute_next_data_frame);
937 self->mode_func_ptr = SPanner_setProcMode;
938
939 self->pan = PyFloat_FromDouble(0.5);
940 self->chnls = 2;
941 self->k1 = 0;
942 self->k2 = self->bufsize;
943 self->modebuffer[0] = 0;
944
945 static char *kwlist[] = {"input", "outs", "pan", NULL};
946
947 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iO", kwlist, &inputtmp, &self->chnls, &pantmp))
948 Py_RETURN_NONE;
949
950 INIT_INPUT_STREAM
951
952 if (pantmp)
953 {
954 PyObject_CallMethod((PyObject *)self, "setPan", "O", pantmp);
955 }
956
957 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
958
959 if (self->chnls < 1)
960 self->chnls = 1;
961
962 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->chnls * self->bufsize * sizeof(MYFLT));
963
964 (*self->mode_func_ptr)(self);
965
966 int len = self->chnls * self->bufsize;
967
968 for (i = 0; i < len; i++)
969 {
970 self->buffer_streams[i] = 0.0;
971 }
972
973 return (PyObject *)self;
974 }
975
976 static PyObject * SPanner_getServer(SPanner* self) { GET_SERVER };
977 static PyObject * SPanner_getStream(SPanner* self) { GET_STREAM };
978
979 static PyObject * SPanner_play(SPanner *self, PyObject *args, PyObject *kwds) { PLAY };
980 static PyObject * SPanner_stop(SPanner *self, PyObject *args, PyObject *kwds) { STOP };
981
982 static PyObject * SPanner_setPan(SPanner *self, PyObject *arg) { SET_PARAM(self->pan, self->pan_stream, 0); }
983
984 static PyMemberDef SPanner_members[] =
985 {
986 {"server", T_OBJECT_EX, offsetof(SPanner, server), 0, "Pyo server."},
987 {"stream", T_OBJECT_EX, offsetof(SPanner, stream), 0, "Stream object."},
988 {"input", T_OBJECT_EX, offsetof(SPanner, input), 0, "Input sound object."},
989 {"pan", T_OBJECT_EX, offsetof(SPanner, pan), 0, "Pan object."},
990 {NULL} /* Sentinel */
991 };
992
993 static PyMethodDef SPanner_methods[] =
994 {
995 {"getServer", (PyCFunction)SPanner_getServer, METH_NOARGS, "Returns server object."},
996 {"_getStream", (PyCFunction)SPanner_getStream, METH_NOARGS, "Returns stream object."},
997 {"play", (PyCFunction)SPanner_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
998 {"stop", (PyCFunction)SPanner_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
999 {"setPan", (PyCFunction)SPanner_setPan, METH_O, "Sets panning value between 0 and 1."},
1000 {NULL} /* Sentinel */
1001 };
1002
1003 PyTypeObject SPannerType =
1004 {
1005 PyVarObject_HEAD_INIT(NULL, 0)
1006 "_pyo.SPanner_base", /*tp_name*/
1007 sizeof(SPanner), /*tp_basicsize*/
1008 0, /*tp_itemsize*/
1009 (destructor)SPanner_dealloc, /*tp_dealloc*/
1010 0, /*tp_print*/
1011 0, /*tp_getattr*/
1012 0, /*tp_setattr*/
1013 0, /*tp_as_async (tp_compare in Python 2)*/
1014 0, /*tp_repr*/
1015 0, /*tp_as_number*/
1016 0, /*tp_as_sequence*/
1017 0, /*tp_as_mapping*/
1018 0, /*tp_hash */
1019 0, /*tp_call*/
1020 0, /*tp_str*/
1021 0, /*tp_getattro*/
1022 0, /*tp_setattro*/
1023 0, /*tp_as_buffer*/
1024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1025 "SPanner main objects. Simple equal power panner", /* tp_doc */
1026 (traverseproc)SPanner_traverse, /* tp_traverse */
1027 (inquiry)SPanner_clear, /* tp_clear */
1028 0, /* tp_richcompare */
1029 0, /* tp_weaklistoffset */
1030 0, /* tp_iter */
1031 0, /* tp_iternext */
1032 SPanner_methods, /* tp_methods */
1033 SPanner_members, /* tp_members */
1034 0, /* tp_getset */
1035 0, /* tp_base */
1036 0, /* tp_dict */
1037 0, /* tp_descr_get */
1038 0, /* tp_descr_set */
1039 0, /* tp_dictoffset */
1040 0, /* tp_init */
1041 0, /* tp_alloc */
1042 SPanner_new, /* tp_new */
1043 };
1044
1045 /************************************************************************************************/
1046 /* SSPan streamer object */
1047 /************************************************************************************************/
1048 typedef struct
1049 {
1050 pyo_audio_HEAD
1051 SPanner *mainSplitter;
1052 int modebuffer[2];
1053 int chnl; // panning order
1054 } SPan;
1055
1056 static void SPan_postprocessing_ii(SPan *self) { POST_PROCESSING_II };
1057 static void SPan_postprocessing_ai(SPan *self) { POST_PROCESSING_AI };
1058 static void SPan_postprocessing_ia(SPan *self) { POST_PROCESSING_IA };
1059 static void SPan_postprocessing_aa(SPan *self) { POST_PROCESSING_AA };
1060 static void SPan_postprocessing_ireva(SPan *self) { POST_PROCESSING_IREVA };
1061 static void SPan_postprocessing_areva(SPan *self) { POST_PROCESSING_AREVA };
1062 static void SPan_postprocessing_revai(SPan *self) { POST_PROCESSING_REVAI };
1063 static void SPan_postprocessing_revaa(SPan *self) { POST_PROCESSING_REVAA };
1064 static void SPan_postprocessing_revareva(SPan *self) { POST_PROCESSING_REVAREVA };
1065
1066 static void
1067 SPan_setProcMode(SPan *self)
1068 {
1069 int muladdmode;
1070 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1071
1072 switch (muladdmode)
1073 {
1074 case 0:
1075 self->muladd_func_ptr = SPan_postprocessing_ii;
1076 break;
1077
1078 case 1:
1079 self->muladd_func_ptr = SPan_postprocessing_ai;
1080 break;
1081
1082 case 2:
1083 self->muladd_func_ptr = SPan_postprocessing_revai;
1084 break;
1085
1086 case 10:
1087 self->muladd_func_ptr = SPan_postprocessing_ia;
1088 break;
1089
1090 case 11:
1091 self->muladd_func_ptr = SPan_postprocessing_aa;
1092 break;
1093
1094 case 12:
1095 self->muladd_func_ptr = SPan_postprocessing_revaa;
1096 break;
1097
1098 case 20:
1099 self->muladd_func_ptr = SPan_postprocessing_ireva;
1100 break;
1101
1102 case 21:
1103 self->muladd_func_ptr = SPan_postprocessing_areva;
1104 break;
1105
1106 case 22:
1107 self->muladd_func_ptr = SPan_postprocessing_revareva;
1108 break;
1109 }
1110 }
1111
1112 static void
1113 SPan_compute_next_data_frame(SPan *self)
1114 {
1115 int i;
1116 MYFLT *tmp;
1117 int offset = self->chnl * self->bufsize;
1118 tmp = SPanner_getSamplesBuffer((SPanner *)self->mainSplitter);
1119
1120 for (i = 0; i < self->bufsize; i++)
1121 {
1122 self->data[i] = tmp[i + offset];
1123 }
1124
1125 (*self->muladd_func_ptr)(self);
1126 }
1127
1128 static int
1129 SPan_traverse(SPan *self, visitproc visit, void *arg)
1130 {
1131 pyo_VISIT
1132 Py_VISIT(self->mainSplitter);
1133 return 0;
1134 }
1135
1136 static int
1137 SPan_clear(SPan *self)
1138 {
1139 pyo_CLEAR
1140 Py_CLEAR(self->mainSplitter);
1141 return 0;
1142 }
1143
1144 static void
1145 SPan_dealloc(SPan* self)
1146 {
1147 pyo_DEALLOC
1148 SPan_clear(self);
1149 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1150 Py_TYPE(self)->tp_free((PyObject*)self);
1151 }
1152
1153 static PyObject *
1154 SPan_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1155 {
1156 int i;
1157 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1158 SPan *self;
1159 self = (SPan *)type->tp_alloc(type, 0);
1160
1161 self->modebuffer[0] = 0;
1162 self->modebuffer[1] = 0;
1163
1164 INIT_OBJECT_COMMON
1165 Stream_setFunctionPtr(self->stream, SPan_compute_next_data_frame);
1166 self->mode_func_ptr = SPan_setProcMode;
1167
1168 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1169
1170 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1171 Py_RETURN_NONE;
1172
1173 self->mainSplitter = (SPanner *)maintmp;
1174 Py_INCREF(self->mainSplitter);
1175
1176 if (multmp)
1177 {
1178 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1179 }
1180
1181 if (addtmp)
1182 {
1183 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1184 }
1185
1186 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1187
1188 (*self->mode_func_ptr)(self);
1189
1190 return (PyObject *)self;
1191 }
1192
1193 static PyObject * SPan_getServer(SPan* self) { GET_SERVER };
1194 static PyObject * SPan_getStream(SPan* self) { GET_STREAM };
1195 static PyObject * SPan_setMul(SPan *self, PyObject *arg) { SET_MUL };
1196 static PyObject * SPan_setAdd(SPan *self, PyObject *arg) { SET_ADD };
1197 static PyObject * SPan_setSub(SPan *self, PyObject *arg) { SET_SUB };
1198 static PyObject * SPan_setDiv(SPan *self, PyObject *arg) { SET_DIV };
1199
1200 static PyObject * SPan_play(SPan *self, PyObject *args, PyObject *kwds) { PLAY };
1201 static PyObject * SPan_out(SPan *self, PyObject *args, PyObject *kwds) { OUT };
1202 static PyObject * SPan_stop(SPan *self, PyObject *args, PyObject *kwds) { STOP };
1203
1204 static PyObject * SPan_multiply(SPan *self, PyObject *arg) { MULTIPLY };
1205 static PyObject * SPan_inplace_multiply(SPan *self, PyObject *arg) { INPLACE_MULTIPLY };
1206 static PyObject * SPan_add(SPan *self, PyObject *arg) { ADD };
1207 static PyObject * SPan_inplace_add(SPan *self, PyObject *arg) { INPLACE_ADD };
1208 static PyObject * SPan_sub(SPan *self, PyObject *arg) { SUB };
1209 static PyObject * SPan_inplace_sub(SPan *self, PyObject *arg) { INPLACE_SUB };
1210 static PyObject * SPan_div(SPan *self, PyObject *arg) { DIV };
1211 static PyObject * SPan_inplace_div(SPan *self, PyObject *arg) { INPLACE_DIV };
1212
1213 static PyMemberDef SPan_members[] =
1214 {
1215 {"server", T_OBJECT_EX, offsetof(SPan, server), 0, "Pyo server."},
1216 {"stream", T_OBJECT_EX, offsetof(SPan, stream), 0, "Stream object."},
1217 {"mul", T_OBJECT_EX, offsetof(SPan, mul), 0, "Mul factor."},
1218 {"add", T_OBJECT_EX, offsetof(SPan, add), 0, "Add factor."},
1219 {NULL} /* Sentinel */
1220 };
1221
1222 static PyMethodDef SPan_methods[] =
1223 {
1224 {"getServer", (PyCFunction)SPan_getServer, METH_NOARGS, "Returns server object."},
1225 {"_getStream", (PyCFunction)SPan_getStream, METH_NOARGS, "Returns stream object."},
1226 {"play", (PyCFunction)SPan_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1227 {"out", (PyCFunction)SPan_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1228 {"stop", (PyCFunction)SPan_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1229 {"setMul", (PyCFunction)SPan_setMul, METH_O, "Sets SPan mul factor."},
1230 {"setAdd", (PyCFunction)SPan_setAdd, METH_O, "Sets SPan add factor."},
1231 {"setSub", (PyCFunction)SPan_setSub, METH_O, "Sets inverse add factor."},
1232 {"setDiv", (PyCFunction)SPan_setDiv, METH_O, "Sets inverse mul factor."},
1233 {NULL} /* Sentinel */
1234 };
1235
1236 static PyNumberMethods SPan_as_number =
1237 {
1238 (binaryfunc)SPan_add, /*nb_add*/
1239 (binaryfunc)SPan_sub, /*nb_subtract*/
1240 (binaryfunc)SPan_multiply, /*nb_multiply*/
1241 0, /*nb_remainder*/
1242 0, /*nb_divmod*/
1243 0, /*nb_power*/
1244 0, /*nb_neg*/
1245 0, /*nb_pos*/
1246 0, /*(unaryfunc)array_abs,*/
1247 0, /*nb_nonzero*/
1248 0, /*nb_invert*/
1249 0, /*nb_lshift*/
1250 0, /*nb_rshift*/
1251 0, /*nb_and*/
1252 0, /*nb_xor*/
1253 0, /*nb_or*/
1254 0, /*nb_int*/
1255 0, /*nb_long*/
1256 0, /*nb_float*/
1257 (binaryfunc)SPan_inplace_add, /*inplace_add*/
1258 (binaryfunc)SPan_inplace_sub, /*inplace_subtract*/
1259 (binaryfunc)SPan_inplace_multiply, /*inplace_multiply*/
1260 0, /*inplace_remainder*/
1261 0, /*inplace_power*/
1262 0, /*inplace_lshift*/
1263 0, /*inplace_rshift*/
1264 0, /*inplace_and*/
1265 0, /*inplace_xor*/
1266 0, /*inplace_or*/
1267 0, /*nb_floor_divide*/
1268 (binaryfunc)SPan_div, /*nb_true_divide*/
1269 0, /*nb_inplace_floor_divide*/
1270 (binaryfunc)SPan_inplace_div, /*nb_inplace_true_divide*/
1271 0, /* nb_index */
1272 };
1273
1274 PyTypeObject SPanType =
1275 {
1276 PyVarObject_HEAD_INIT(NULL, 0)
1277 "_pyo.SPan_base", /*tp_name*/
1278 sizeof(SPan), /*tp_basicsize*/
1279 0, /*tp_itemsize*/
1280 (destructor)SPan_dealloc, /*tp_dealloc*/
1281 0, /*tp_print*/
1282 0, /*tp_getattr*/
1283 0, /*tp_setattr*/
1284 0, /*tp_as_async (tp_compare in Python 2)*/
1285 0, /*tp_repr*/
1286 &SPan_as_number, /*tp_as_number*/
1287 0, /*tp_as_sequence*/
1288 0, /*tp_as_mapping*/
1289 0, /*tp_hash */
1290 0, /*tp_call*/
1291 0, /*tp_str*/
1292 0, /*tp_getattro*/
1293 0, /*tp_setattro*/
1294 0, /*tp_as_buffer*/
1295 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1296 "SPan objects. Reads one band from a SPanner.", /* tp_doc */
1297 (traverseproc)SPan_traverse, /* tp_traverse */
1298 (inquiry)SPan_clear, /* tp_clear */
1299 0, /* tp_richcompare */
1300 0, /* tp_weaklistoffset */
1301 0, /* tp_iter */
1302 0, /* tp_iternext */
1303 SPan_methods, /* tp_methods */
1304 SPan_members, /* tp_members */
1305 0, /* tp_getset */
1306 0, /* tp_base */
1307 0, /* tp_dict */
1308 0, /* tp_descr_get */
1309 0, /* tp_descr_set */
1310 0, /* tp_dictoffset */
1311 0, /* tp_init */
1312 0, /* tp_alloc */
1313 SPan_new, /* tp_new */
1314 };
1315
1316 /*********************/
1317 /***** Switcher ******/
1318 /*********************/
1319 typedef struct
1320 {
1321 pyo_audio_HEAD
1322 PyObject *input;
1323 Stream *input_stream;
1324 PyObject *voice;
1325 Stream *voice_stream;
1326 int chnls;
1327 int k1;
1328 int k2;
1329 int modebuffer[1];
1330 MYFLT *buffer_streams;
1331 } Switcher;
1332
1333 static MYFLT
1334 Switcher_clip_voice(Switcher *self, MYFLT v)
1335 {
1336 int chnls = self->chnls - 1;
1337
1338 if (v < 0.0)
1339 return 0.0;
1340 else if (v > chnls)
1341 return chnls;
1342 else
1343 return v;
1344 }
1345
1346 static void
1347 Switcher_splitter_i(Switcher *self)
1348 {
1349 MYFLT inval, voice1, voice2;
1350 int j1, j, i;
1351 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1352 MYFLT voice = Switcher_clip_voice(self, PyFloat_AS_DOUBLE(self->voice));
1353
1354 for (i = 0; i < self->bufsize; i++)
1355 {
1356 self->buffer_streams[i + self->k1] = 0.0;
1357 self->buffer_streams[i + self->k2] = 0.0;
1358 }
1359
1360 j1 = (int)voice;
1361 j = j1 + 1;
1362
1363 if (j1 >= (self->chnls - 1))
1364 {
1365 j1--;
1366 j--;
1367 }
1368
1369 self->k1 = j1 * self->bufsize;
1370 self->k2 = j * self->bufsize;
1371
1372 voice = P_clip(voice - j1);
1373 voice1 = MYSQRT(1.0 - voice);
1374 voice2 = MYSQRT(voice);
1375
1376 for (i = 0; i < self->bufsize; i++)
1377 {
1378 inval = in[i];
1379 self->buffer_streams[i + self->k1] = inval * voice1;
1380 self->buffer_streams[i + self->k2] = inval * voice2;
1381 }
1382 }
1383
1384 static void
1385 Switcher_splitter_a(Switcher *self)
1386 {
1387 MYFLT inval, voice;
1388 int i, j, j1, len;
1389 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1390 MYFLT *avoice = Stream_getData((Stream *)self->voice_stream);
1391
1392 len = self->chnls * self->bufsize;
1393
1394 for (i = 0; i < len; i++)
1395 {
1396 self->buffer_streams[i] = 0.0;
1397 }
1398
1399 for (i = 0; i < self->bufsize; i++)
1400 {
1401 voice = Switcher_clip_voice(self, avoice[i]);
1402 inval = in[i];
1403
1404 j1 = (int)voice;
1405 j = j1 + 1;
1406
1407 if (j1 >= (self->chnls - 1))
1408 {
1409 j1--;
1410 j--;
1411 }
1412
1413 self->k1 = j1 * self->bufsize;
1414 self->k2 = j * self->bufsize;
1415
1416 voice = P_clip(voice - j1);
1417
1418 self->buffer_streams[i + self->k1] = inval * MYSQRT(1.0 - voice);
1419 self->buffer_streams[i + self->k2] = inval * MYSQRT(voice);
1420 }
1421 }
1422
1423 MYFLT *
1424 Switcher_getSamplesBuffer(Switcher *self)
1425 {
1426 return (MYFLT *)self->buffer_streams;
1427 }
1428
1429 static void
1430 Switcher_setProcMode(Switcher *self)
1431 {
1432 switch (self->modebuffer[0])
1433 {
1434 case 0:
1435 self->proc_func_ptr = Switcher_splitter_i;
1436 break;
1437
1438 case 1:
1439 self->proc_func_ptr = Switcher_splitter_a;
1440 break;
1441 }
1442 }
1443
1444 static void
1445 Switcher_compute_next_data_frame(Switcher *self)
1446 {
1447 (*self->proc_func_ptr)(self);
1448 }
1449
1450 static int
1451 Switcher_traverse(Switcher *self, visitproc visit, void *arg)
1452 {
1453 pyo_VISIT
1454 Py_VISIT(self->input);
1455 Py_VISIT(self->voice);
1456 return 0;
1457 }
1458
1459 static int
1460 Switcher_clear(Switcher *self)
1461 {
1462 pyo_CLEAR
1463 Py_CLEAR(self->input);
1464 Py_CLEAR(self->voice);
1465 return 0;
1466 }
1467
1468 static void
1469 Switcher_dealloc(Switcher* self)
1470 {
1471 pyo_DEALLOC
1472 PyMem_RawFree(self->buffer_streams);
1473 Switcher_clear(self);
1474 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1475 Py_TYPE(self)->tp_free((PyObject*)self);
1476 }
1477
1478 static PyObject *
1479 Switcher_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1480 {
1481 int i;
1482 PyObject *inputtmp, *input_streamtmp, *voicetmp = NULL;
1483 Switcher *self;
1484 self = (Switcher *)type->tp_alloc(type, 0);
1485
1486 INIT_OBJECT_COMMON
1487 Stream_setFunctionPtr(self->stream, Switcher_compute_next_data_frame);
1488 self->mode_func_ptr = Switcher_setProcMode;
1489
1490 self->voice = PyFloat_FromDouble(0.0);
1491 self->chnls = 2;
1492 self->k1 = 0;
1493 self->k2 = self->bufsize;
1494 self->modebuffer[0] = 0;
1495
1496 static char *kwlist[] = {"input", "outs", "voice", NULL};
1497
1498 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iO", kwlist, &inputtmp, &self->chnls, &voicetmp))
1499 Py_RETURN_NONE;
1500
1501 INIT_INPUT_STREAM
1502
1503 if (voicetmp)
1504 {
1505 PyObject_CallMethod((PyObject *)self, "setVoice", "O", voicetmp);
1506 }
1507
1508 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1509
1510 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->chnls * self->bufsize * sizeof(MYFLT));
1511
1512 (*self->mode_func_ptr)(self);
1513
1514 int len = self->chnls * self->bufsize;
1515
1516 for (i = 0; i < len; i++)
1517 {
1518 self->buffer_streams[i] = 0.0;
1519 }
1520
1521 return (PyObject *)self;
1522 }
1523
1524 static PyObject * Switcher_getServer(Switcher* self) { GET_SERVER };
1525 static PyObject * Switcher_getStream(Switcher* self) { GET_STREAM };
1526
1527 static PyObject * Switcher_play(Switcher *self, PyObject *args, PyObject *kwds) { PLAY };
1528 static PyObject * Switcher_stop(Switcher *self, PyObject *args, PyObject *kwds) { STOP };
1529
1530 static PyObject * Switcher_setVoice(Switcher *self, PyObject *arg) { SET_PARAM(self->voice, self->voice_stream, 0); }
1531
1532 static PyMemberDef Switcher_members[] =
1533 {
1534 {"server", T_OBJECT_EX, offsetof(Switcher, server), 0, "Pyo server."},
1535 {"stream", T_OBJECT_EX, offsetof(Switcher, stream), 0, "Stream object."},
1536 {"input", T_OBJECT_EX, offsetof(Switcher, input), 0, "Input sound object."},
1537 {"voice", T_OBJECT_EX, offsetof(Switcher, voice), 0, "voice object."},
1538 {NULL} /* Sentinel */
1539 };
1540
1541 static PyMethodDef Switcher_methods[] =
1542 {
1543 {"getServer", (PyCFunction)Switcher_getServer, METH_NOARGS, "Returns server object."},
1544 {"_getStream", (PyCFunction)Switcher_getStream, METH_NOARGS, "Returns stream object."},
1545 {"play", (PyCFunction)Switcher_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1546 {"stop", (PyCFunction)Switcher_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1547 {"setVoice", (PyCFunction)Switcher_setVoice, METH_O, "Sets voice value between 0 and outs-1."},
1548 {NULL} /* Sentinel */
1549 };
1550
1551 PyTypeObject SwitcherType =
1552 {
1553 PyVarObject_HEAD_INIT(NULL, 0)
1554 "_pyo.Switcher_base", /*tp_name*/
1555 sizeof(Switcher), /*tp_basicsize*/
1556 0, /*tp_itemsize*/
1557 (destructor)Switcher_dealloc, /*tp_dealloc*/
1558 0, /*tp_print*/
1559 0, /*tp_getattr*/
1560 0, /*tp_setattr*/
1561 0, /*tp_as_async (tp_compare in Python 2)*/
1562 0, /*tp_repr*/
1563 0, /*tp_as_number*/
1564 0, /*tp_as_sequence*/
1565 0, /*tp_as_mapping*/
1566 0, /*tp_hash */
1567 0, /*tp_call*/
1568 0, /*tp_str*/
1569 0, /*tp_getattro*/
1570 0, /*tp_setattro*/
1571 0, /*tp_as_buffer*/
1572 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1573 "Switcher main objects. Simple equal power voicener", /* tp_doc */
1574 (traverseproc)Switcher_traverse, /* tp_traverse */
1575 (inquiry)Switcher_clear, /* tp_clear */
1576 0, /* tp_richcompare */
1577 0, /* tp_weaklistoffset */
1578 0, /* tp_iter */
1579 0, /* tp_iternext */
1580 Switcher_methods, /* tp_methods */
1581 Switcher_members, /* tp_members */
1582 0, /* tp_getset */
1583 0, /* tp_base */
1584 0, /* tp_dict */
1585 0, /* tp_descr_get */
1586 0, /* tp_descr_set */
1587 0, /* tp_dictoffset */
1588 0, /* tp_init */
1589 0, /* tp_alloc */
1590 Switcher_new, /* tp_new */
1591 };
1592
1593 /************************************************************************************************/
1594 /* Switch streamer object */
1595 /************************************************************************************************/
1596 typedef struct
1597 {
1598 pyo_audio_HEAD
1599 Switcher *mainSplitter;
1600 int modebuffer[2];
1601 int chnl; // switch order
1602 } Switch;
1603
1604 static void Switch_postprocessing_ii(Switch *self) { POST_PROCESSING_II };
1605 static void Switch_postprocessing_ai(Switch *self) { POST_PROCESSING_AI };
1606 static void Switch_postprocessing_ia(Switch *self) { POST_PROCESSING_IA };
1607 static void Switch_postprocessing_aa(Switch *self) { POST_PROCESSING_AA };
1608 static void Switch_postprocessing_ireva(Switch *self) { POST_PROCESSING_IREVA };
1609 static void Switch_postprocessing_areva(Switch *self) { POST_PROCESSING_AREVA };
1610 static void Switch_postprocessing_revai(Switch *self) { POST_PROCESSING_REVAI };
1611 static void Switch_postprocessing_revaa(Switch *self) { POST_PROCESSING_REVAA };
1612 static void Switch_postprocessing_revareva(Switch *self) { POST_PROCESSING_REVAREVA };
1613
1614 static void
1615 Switch_setProcMode(Switch *self)
1616 {
1617 int muladdmode;
1618 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1619
1620 switch (muladdmode)
1621 {
1622 case 0:
1623 self->muladd_func_ptr = Switch_postprocessing_ii;
1624 break;
1625
1626 case 1:
1627 self->muladd_func_ptr = Switch_postprocessing_ai;
1628 break;
1629
1630 case 2:
1631 self->muladd_func_ptr = Switch_postprocessing_revai;
1632 break;
1633
1634 case 10:
1635 self->muladd_func_ptr = Switch_postprocessing_ia;
1636 break;
1637
1638 case 11:
1639 self->muladd_func_ptr = Switch_postprocessing_aa;
1640 break;
1641
1642 case 12:
1643 self->muladd_func_ptr = Switch_postprocessing_revaa;
1644 break;
1645
1646 case 20:
1647 self->muladd_func_ptr = Switch_postprocessing_ireva;
1648 break;
1649
1650 case 21:
1651 self->muladd_func_ptr = Switch_postprocessing_areva;
1652 break;
1653
1654 case 22:
1655 self->muladd_func_ptr = Switch_postprocessing_revareva;
1656 break;
1657 }
1658 }
1659
1660 static void
1661 Switch_compute_next_data_frame(Switch *self)
1662 {
1663 int i;
1664 MYFLT *tmp;
1665 int offset = self->chnl * self->bufsize;
1666 tmp = Switcher_getSamplesBuffer((Switcher *)self->mainSplitter);
1667
1668 for (i = 0; i < self->bufsize; i++)
1669 {
1670 self->data[i] = tmp[i + offset];
1671 }
1672
1673 (*self->muladd_func_ptr)(self);
1674 }
1675
1676 static int
1677 Switch_traverse(Switch *self, visitproc visit, void *arg)
1678 {
1679 pyo_VISIT
1680 Py_VISIT(self->mainSplitter);
1681 return 0;
1682 }
1683
1684 static int
1685 Switch_clear(Switch *self)
1686 {
1687 pyo_CLEAR
1688 Py_CLEAR(self->mainSplitter);
1689 return 0;
1690 }
1691
1692 static void
1693 Switch_dealloc(Switch* self)
1694 {
1695 pyo_DEALLOC
1696 Switch_clear(self);
1697 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1698 Py_TYPE(self)->tp_free((PyObject*)self);
1699 }
1700
1701 static PyObject *
1702 Switch_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1703 {
1704 int i;
1705 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1706 Switch *self;
1707 self = (Switch *)type->tp_alloc(type, 0);
1708
1709 self->modebuffer[0] = 0;
1710 self->modebuffer[1] = 0;
1711
1712 INIT_OBJECT_COMMON
1713 Stream_setFunctionPtr(self->stream, Switch_compute_next_data_frame);
1714 self->mode_func_ptr = Switch_setProcMode;
1715
1716 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1717
1718 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1719 Py_RETURN_NONE;
1720
1721 self->mainSplitter = (Switcher *)maintmp;
1722 Py_INCREF(self->mainSplitter);
1723
1724 if (multmp)
1725 {
1726 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1727 }
1728
1729 if (addtmp)
1730 {
1731 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1732 }
1733
1734 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1735
1736 (*self->mode_func_ptr)(self);
1737
1738 return (PyObject *)self;
1739 }
1740
1741 static PyObject * Switch_getServer(Switch* self) { GET_SERVER };
1742 static PyObject * Switch_getStream(Switch* self) { GET_STREAM };
1743 static PyObject * Switch_setMul(Switch *self, PyObject *arg) { SET_MUL };
1744 static PyObject * Switch_setAdd(Switch *self, PyObject *arg) { SET_ADD };
1745 static PyObject * Switch_setSub(Switch *self, PyObject *arg) { SET_SUB };
1746 static PyObject * Switch_setDiv(Switch *self, PyObject *arg) { SET_DIV };
1747
1748 static PyObject * Switch_play(Switch *self, PyObject *args, PyObject *kwds) { PLAY };
1749 static PyObject * Switch_out(Switch *self, PyObject *args, PyObject *kwds) { OUT };
1750 static PyObject * Switch_stop(Switch *self, PyObject *args, PyObject *kwds) { STOP };
1751
1752 static PyObject * Switch_multiply(Switch *self, PyObject *arg) { MULTIPLY };
1753 static PyObject * Switch_inplace_multiply(Switch *self, PyObject *arg) { INPLACE_MULTIPLY };
1754 static PyObject * Switch_add(Switch *self, PyObject *arg) { ADD };
1755 static PyObject * Switch_inplace_add(Switch *self, PyObject *arg) { INPLACE_ADD };
1756 static PyObject * Switch_sub(Switch *self, PyObject *arg) { SUB };
1757 static PyObject * Switch_inplace_sub(Switch *self, PyObject *arg) { INPLACE_SUB };
1758 static PyObject * Switch_div(Switch *self, PyObject *arg) { DIV };
1759 static PyObject * Switch_inplace_div(Switch *self, PyObject *arg) { INPLACE_DIV };
1760
1761 static PyMemberDef Switch_members[] =
1762 {
1763 {"server", T_OBJECT_EX, offsetof(Switch, server), 0, "Pyo server."},
1764 {"stream", T_OBJECT_EX, offsetof(Switch, stream), 0, "Stream object."},
1765 {"mul", T_OBJECT_EX, offsetof(Switch, mul), 0, "Mul factor."},
1766 {"add", T_OBJECT_EX, offsetof(Switch, add), 0, "Add factor."},
1767 {NULL} /* Sentinel */
1768 };
1769
1770 static PyMethodDef Switch_methods[] =
1771 {
1772 {"getServer", (PyCFunction)Switch_getServer, METH_NOARGS, "Returns server object."},
1773 {"_getStream", (PyCFunction)Switch_getStream, METH_NOARGS, "Returns stream object."},
1774 {"play", (PyCFunction)Switch_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1775 {"out", (PyCFunction)Switch_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1776 {"stop", (PyCFunction)Switch_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1777 {"setMul", (PyCFunction)Switch_setMul, METH_O, "Sets Switch mul factor."},
1778 {"setAdd", (PyCFunction)Switch_setAdd, METH_O, "Sets Switch add factor."},
1779 {"setSub", (PyCFunction)Switch_setSub, METH_O, "Sets inverse add factor."},
1780 {"setDiv", (PyCFunction)Switch_setDiv, METH_O, "Sets inverse mul factor."},
1781 {NULL} /* Sentinel */
1782 };
1783
1784 static PyNumberMethods Switch_as_number =
1785 {
1786 (binaryfunc)Switch_add, /*nb_add*/
1787 (binaryfunc)Switch_sub, /*nb_subtract*/
1788 (binaryfunc)Switch_multiply, /*nb_multiply*/
1789 0, /*nb_remainder*/
1790 0, /*nb_divmod*/
1791 0, /*nb_power*/
1792 0, /*nb_neg*/
1793 0, /*nb_pos*/
1794 0, /*(unaryfunc)array_abs,*/
1795 0, /*nb_nonzero*/
1796 0, /*nb_invert*/
1797 0, /*nb_lshift*/
1798 0, /*nb_rshift*/
1799 0, /*nb_and*/
1800 0, /*nb_xor*/
1801 0, /*nb_or*/
1802 0, /*nb_int*/
1803 0, /*nb_long*/
1804 0, /*nb_float*/
1805 (binaryfunc)Switch_inplace_add, /*inplace_add*/
1806 (binaryfunc)Switch_inplace_sub, /*inplace_subtract*/
1807 (binaryfunc)Switch_inplace_multiply, /*inplace_multiply*/
1808 0, /*inplace_remainder*/
1809 0, /*inplace_power*/
1810 0, /*inplace_lshift*/
1811 0, /*inplace_rshift*/
1812 0, /*inplace_and*/
1813 0, /*inplace_xor*/
1814 0, /*inplace_or*/
1815 0, /*nb_floor_divide*/
1816 (binaryfunc)Switch_div, /*nb_true_divide*/
1817 0, /*nb_inplace_floor_divide*/
1818 (binaryfunc)Switch_inplace_div, /*nb_inplace_true_divide*/
1819 0, /* nb_index */
1820 };
1821
1822 PyTypeObject SwitchType =
1823 {
1824 PyVarObject_HEAD_INIT(NULL, 0)
1825 "_pyo.Switch_base", /*tp_name*/
1826 sizeof(Switch), /*tp_basicsize*/
1827 0, /*tp_itemsize*/
1828 (destructor)Switch_dealloc, /*tp_dealloc*/
1829 0, /*tp_print*/
1830 0, /*tp_getattr*/
1831 0, /*tp_setattr*/
1832 0, /*tp_as_async (tp_compare in Python 2)*/
1833 0, /*tp_repr*/
1834 &Switch_as_number, /*tp_as_number*/
1835 0, /*tp_as_sequence*/
1836 0, /*tp_as_mapping*/
1837 0, /*tp_hash */
1838 0, /*tp_call*/
1839 0, /*tp_str*/
1840 0, /*tp_getattro*/
1841 0, /*tp_setattro*/
1842 0, /*tp_as_buffer*/
1843 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1844 "Switch objects. Reads one band from a Switchner.", /* tp_doc */
1845 (traverseproc)Switch_traverse, /* tp_traverse */
1846 (inquiry)Switch_clear, /* tp_clear */
1847 0, /* tp_richcompare */
1848 0, /* tp_weaklistoffset */
1849 0, /* tp_iter */
1850 0, /* tp_iternext */
1851 Switch_methods, /* tp_methods */
1852 Switch_members, /* tp_members */
1853 0, /* tp_getset */
1854 0, /* tp_base */
1855 0, /* tp_dict */
1856 0, /* tp_descr_get */
1857 0, /* tp_descr_set */
1858 0, /* tp_dictoffset */
1859 0, /* tp_init */
1860 0, /* tp_alloc */
1861 Switch_new, /* tp_new */
1862 };
1863
1864 /****************/
1865 /**** VoiceManager *****/
1866 /****************/
1867 typedef struct
1868 {
1869 pyo_audio_HEAD
1870 PyObject *input;
1871 Stream *input_stream;
1872 PyObject *trigger_streams;
1873 int maxVoices;
1874 int *voices;
1875 int modebuffer[2]; // need at least 2 slots for mul & add
1876 } VoiceManager;
1877
1878 static void
1879 VoiceManager_generate(VoiceManager *self)
1880 {
1881 int j, i;
1882 Stream *trig_stream;
1883
1884 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1885
1886 for (i = 0; i < self->bufsize; i++)
1887 self->data[i] = -1.0;
1888
1889 if (self->maxVoices > 0)
1890 {
1891 for (i = 0; i < self->bufsize; i++)
1892 {
1893 for (j = 0; j < self->maxVoices; j++)
1894 {
1895 trig_stream = (Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->trigger_streams, j), "_getStream", NULL);
1896 if (Stream_getData(trig_stream)[i] == 1.0)
1897 self->voices[j] = 0;
1898 }
1899
1900 if (in[i] == 1.0)
1901 {
1902 for (j = 0; j < self->maxVoices; j++)
1903 {
1904 if (self->voices[j] == 0)
1905 {
1906 self->data[i] = (MYFLT)j;
1907 self->voices[j] = 1;
1908 break;
1909 }
1910 }
1911 }
1912 }
1913 }
1914 }
1915
1916 static void VoiceManager_postprocessing_ii(VoiceManager *self) { POST_PROCESSING_II };
1917 static void VoiceManager_postprocessing_ai(VoiceManager *self) { POST_PROCESSING_AI };
1918 static void VoiceManager_postprocessing_ia(VoiceManager *self) { POST_PROCESSING_IA };
1919 static void VoiceManager_postprocessing_aa(VoiceManager *self) { POST_PROCESSING_AA };
1920 static void VoiceManager_postprocessing_ireva(VoiceManager *self) { POST_PROCESSING_IREVA };
1921 static void VoiceManager_postprocessing_areva(VoiceManager *self) { POST_PROCESSING_AREVA };
1922 static void VoiceManager_postprocessing_revai(VoiceManager *self) { POST_PROCESSING_REVAI };
1923 static void VoiceManager_postprocessing_revaa(VoiceManager *self) { POST_PROCESSING_REVAA };
1924 static void VoiceManager_postprocessing_revareva(VoiceManager *self) { POST_PROCESSING_REVAREVA };
1925
1926 static void
1927 VoiceManager_setProcMode(VoiceManager *self)
1928 {
1929 int muladdmode;
1930 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1931
1932 self->proc_func_ptr = VoiceManager_generate;
1933
1934 switch (muladdmode)
1935 {
1936 case 0:
1937 self->muladd_func_ptr = VoiceManager_postprocessing_ii;
1938 break;
1939
1940 case 1:
1941 self->muladd_func_ptr = VoiceManager_postprocessing_ai;
1942 break;
1943
1944 case 2:
1945 self->muladd_func_ptr = VoiceManager_postprocessing_revai;
1946 break;
1947
1948 case 10:
1949 self->muladd_func_ptr = VoiceManager_postprocessing_ia;
1950 break;
1951
1952 case 11:
1953 self->muladd_func_ptr = VoiceManager_postprocessing_aa;
1954 break;
1955
1956 case 12:
1957 self->muladd_func_ptr = VoiceManager_postprocessing_revaa;
1958 break;
1959
1960 case 20:
1961 self->muladd_func_ptr = VoiceManager_postprocessing_ireva;
1962 break;
1963
1964 case 21:
1965 self->muladd_func_ptr = VoiceManager_postprocessing_areva;
1966 break;
1967
1968 case 22:
1969 self->muladd_func_ptr = VoiceManager_postprocessing_revareva;
1970 break;
1971 }
1972 }
1973
1974 static void
1975 VoiceManager_compute_next_data_frame(VoiceManager *self)
1976 {
1977 (*self->proc_func_ptr)(self);
1978 (*self->muladd_func_ptr)(self);
1979 }
1980
1981 static int
1982 VoiceManager_traverse(VoiceManager *self, visitproc visit, void *arg)
1983 {
1984 pyo_VISIT
1985 Py_VISIT(self->input);
1986 return 0;
1987 }
1988
1989 static int
1990 VoiceManager_clear(VoiceManager *self)
1991 {
1992 pyo_CLEAR
1993 Py_CLEAR(self->input);
1994 return 0;
1995 }
1996
1997 static void
1998 VoiceManager_dealloc(VoiceManager* self)
1999 {
2000 pyo_DEALLOC
2001 VoiceManager_clear(self);
2002
2003 if (self->voices != NULL)
2004 {
2005 PyMem_RawFree(self->voices);
2006 }
2007
2008 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2009 Py_TYPE(self)->tp_free((PyObject*)self);
2010 }
2011
2012 static PyObject *
2013 VoiceManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2014 {
2015 int i;
2016 PyObject *inputtmp, *input_streamtmp, *triggerstmp = NULL, *multmp = NULL, *addtmp = NULL;
2017 VoiceManager *self;
2018 self = (VoiceManager *)type->tp_alloc(type, 0);
2019
2020 self->voices = NULL;
2021 self->maxVoices = 0;
2022 self->modebuffer[0] = 0;
2023 self->modebuffer[1] = 0;
2024
2025 INIT_OBJECT_COMMON
2026 Stream_setFunctionPtr(self->stream, VoiceManager_compute_next_data_frame);
2027 self->mode_func_ptr = VoiceManager_setProcMode;
2028
2029 static char *kwlist[] = {"input", "triggers", "mul", "add", NULL};
2030
2031 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &triggerstmp, &multmp, &addtmp))
2032 Py_RETURN_NONE;
2033
2034 INIT_INPUT_STREAM
2035
2036 if (triggerstmp && triggerstmp != Py_None)
2037 {
2038 PyObject_CallMethod((PyObject *)self, "setTriggers", "O", triggerstmp);
2039 }
2040
2041 if (multmp)
2042 {
2043 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2044 }
2045
2046 if (addtmp)
2047 {
2048 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2049 }
2050
2051 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2052
2053 (*self->mode_func_ptr)(self);
2054
2055 return (PyObject *)self;
2056 }
2057
2058 static PyObject * VoiceManager_getServer(VoiceManager* self) { GET_SERVER };
2059 static PyObject * VoiceManager_getStream(VoiceManager* self) { GET_STREAM };
2060 static PyObject * VoiceManager_setMul(VoiceManager *self, PyObject *arg) { SET_MUL };
2061 static PyObject * VoiceManager_setAdd(VoiceManager *self, PyObject *arg) { SET_ADD };
2062 static PyObject * VoiceManager_setSub(VoiceManager *self, PyObject *arg) { SET_SUB };
2063 static PyObject * VoiceManager_setDiv(VoiceManager *self, PyObject *arg) { SET_DIV };
2064
2065 static PyObject * VoiceManager_play(VoiceManager *self, PyObject *args, PyObject *kwds) { PLAY };
2066 static PyObject * VoiceManager_stop(VoiceManager *self, PyObject *args, PyObject *kwds) { STOP };
2067
2068 static PyObject * VoiceManager_multiply(VoiceManager *self, PyObject *arg) { MULTIPLY };
2069 static PyObject * VoiceManager_inplace_multiply(VoiceManager *self, PyObject *arg) { INPLACE_MULTIPLY };
2070 static PyObject * VoiceManager_add(VoiceManager *self, PyObject *arg) { ADD };
2071 static PyObject * VoiceManager_inplace_add(VoiceManager *self, PyObject *arg) { INPLACE_ADD };
2072 static PyObject * VoiceManager_sub(VoiceManager *self, PyObject *arg) { SUB };
2073 static PyObject * VoiceManager_inplace_sub(VoiceManager *self, PyObject *arg) { INPLACE_SUB };
2074 static PyObject * VoiceManager_div(VoiceManager *self, PyObject *arg) { DIV };
2075 static PyObject * VoiceManager_inplace_div(VoiceManager *self, PyObject *arg) { INPLACE_DIV };
2076
2077 static PyObject *
2078 VoiceManager_setTriggers(VoiceManager *self, PyObject *arg)
2079 {
2080 int i;
2081
2082 if (! PyList_Check(arg))
2083 {
2084 PyErr_SetString(PyExc_TypeError, "The triggers attribute must be a list.");
2085 Py_RETURN_NONE;
2086 }
2087
2088 Py_INCREF(arg);
2089 Py_XDECREF(self->trigger_streams);
2090 self->trigger_streams = arg;
2091
2092 self->maxVoices = PyList_Size(arg);
2093 self->voices = (int *)PyMem_RawRealloc(self->voices, self->maxVoices * sizeof(int));
2094
2095 for (i = 0; i < self->maxVoices; i++)
2096 {
2097 self->voices[i] = 0;
2098 }
2099
2100 Py_RETURN_NONE;
2101 }
2102
2103 static PyMemberDef VoiceManager_members[] =
2104 {
2105 {"server", T_OBJECT_EX, offsetof(VoiceManager, server), 0, "Pyo server."},
2106 {"stream", T_OBJECT_EX, offsetof(VoiceManager, stream), 0, "Stream object."},
2107 {"input", T_OBJECT_EX, offsetof(VoiceManager, input), 0, "Input sound object."},
2108 {"trigger_streams", T_OBJECT_EX, offsetof(VoiceManager, trigger_streams), 0, "Array of trigger streams."},
2109 {"mul", T_OBJECT_EX, offsetof(VoiceManager, mul), 0, "Mul factor."},
2110 {"add", T_OBJECT_EX, offsetof(VoiceManager, add), 0, "Add factor."},
2111 {NULL} /* Sentinel */
2112 };
2113
2114 static PyMethodDef VoiceManager_methods[] =
2115 {
2116 {"getServer", (PyCFunction)VoiceManager_getServer, METH_NOARGS, "Returns server object."},
2117 {"_getStream", (PyCFunction)VoiceManager_getStream, METH_NOARGS, "Returns stream object."},
2118 {"play", (PyCFunction)VoiceManager_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2119 {"stop", (PyCFunction)VoiceManager_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2120 {"setTriggers", (PyCFunction)VoiceManager_setTriggers, METH_O, "Sets list of trigger streams."},
2121 {"setMul", (PyCFunction)VoiceManager_setMul, METH_O, "Sets mul factor."},
2122 {"setAdd", (PyCFunction)VoiceManager_setAdd, METH_O, "Sets add factor."},
2123 {"setSub", (PyCFunction)VoiceManager_setSub, METH_O, "Sets inverse add factor."},
2124 {"setDiv", (PyCFunction)VoiceManager_setDiv, METH_O, "Sets inverse mul factor."},
2125 {NULL} /* Sentinel */
2126 };
2127
2128 static PyNumberMethods VoiceManager_as_number =
2129 {
2130 (binaryfunc)VoiceManager_add, /*nb_add*/
2131 (binaryfunc)VoiceManager_sub, /*nb_subtract*/
2132 (binaryfunc)VoiceManager_multiply, /*nb_multiply*/
2133 0, /*nb_remainder*/
2134 0, /*nb_divmod*/
2135 0, /*nb_power*/
2136 0, /*nb_neg*/
2137 0, /*nb_pos*/
2138 0, /*(unaryfunc)array_abs,*/
2139 0, /*nb_nonzero*/
2140 0, /*nb_invert*/
2141 0, /*nb_lshift*/
2142 0, /*nb_rshift*/
2143 0, /*nb_and*/
2144 0, /*nb_xor*/
2145 0, /*nb_or*/
2146 0, /*nb_int*/
2147 0, /*nb_long*/
2148 0, /*nb_float*/
2149 (binaryfunc)VoiceManager_inplace_add, /*inplace_add*/
2150 (binaryfunc)VoiceManager_inplace_sub, /*inplace_subtract*/
2151 (binaryfunc)VoiceManager_inplace_multiply, /*inplace_multiply*/
2152 0, /*inplace_remainder*/
2153 0, /*inplace_power*/
2154 0, /*inplace_lshift*/
2155 0, /*inplace_rshift*/
2156 0, /*inplace_and*/
2157 0, /*inplace_xor*/
2158 0, /*inplace_or*/
2159 0, /*nb_floor_divide*/
2160 (binaryfunc)VoiceManager_div, /*nb_true_divide*/
2161 0, /*nb_inplace_floor_divide*/
2162 (binaryfunc)VoiceManager_inplace_div, /*nb_inplace_true_divide*/
2163 0, /* nb_index */
2164 };
2165
2166 PyTypeObject VoiceManagerType =
2167 {
2168 PyVarObject_HEAD_INIT(NULL, 0)
2169 "_pyo.VoiceManager_base", /*tp_name*/
2170 sizeof(VoiceManager), /*tp_basicsize*/
2171 0, /*tp_itemsize*/
2172 (destructor)VoiceManager_dealloc, /*tp_dealloc*/
2173 0, /*tp_print*/
2174 0, /*tp_getattr*/
2175 0, /*tp_setattr*/
2176 0, /*tp_as_async (tp_compare in Python 2)*/
2177 0, /*tp_repr*/
2178 &VoiceManager_as_number, /*tp_as_number*/
2179 0, /*tp_as_sequence*/
2180 0, /*tp_as_mapping*/
2181 0, /*tp_hash */
2182 0, /*tp_call*/
2183 0, /*tp_str*/
2184 0, /*tp_getattro*/
2185 0, /*tp_setattro*/
2186 0, /*tp_as_buffer*/
2187 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2188 "VoiceManager objects. Return the first free voices when receiving a trigger signal.", /* tp_doc */
2189 (traverseproc)VoiceManager_traverse, /* tp_traverse */
2190 (inquiry)VoiceManager_clear, /* tp_clear */
2191 0, /* tp_richcompare */
2192 0, /* tp_weaklistoffset */
2193 0, /* tp_iter */
2194 0, /* tp_iternext */
2195 VoiceManager_methods, /* tp_methods */
2196 VoiceManager_members, /* tp_members */
2197 0, /* tp_getset */
2198 0, /* tp_base */
2199 0, /* tp_dict */
2200 0, /* tp_descr_get */
2201 0, /* tp_descr_set */
2202 0, /* tp_dictoffset */
2203 0, /* tp_init */
2204 0, /* tp_alloc */
2205 VoiceManager_new, /* tp_new */
2206 };
2207
2208 /****************/
2209 /**** Mixer *****/
2210 /****************/
2211 typedef struct
2212 {
2213 pyo_audio_HEAD
2214 PyObject *inputs;
2215 PyObject *gains;
2216 PyObject *lastGains;
2217 PyObject *currentGains;
2218 PyObject *stepVals;
2219 PyObject *timeCounts;
2220 int num_outs;
2221 MYFLT time;
2222 long timeStep;
2223 MYFLT *buffer_streams;
2224 } Mixer;
2225
2226 static void
2227 Mixer_generate(Mixer *self)
2228 {
2229 int num, k, j, i, tmpCount;
2230 MYFLT amp, lastAmp, currentAmp, tmpStepVal;
2231 PyObject *keys;
2232 PyObject *key;
2233 PyObject *list_of_gains, *list_of_last_gains, *list_of_current_gains, *list_of_step_vals, *list_of_time_counts;
2234
2235 for (i = 0; i < (self->num_outs * self->bufsize); i++)
2236 {
2237 self->buffer_streams[i] = 0.0;
2238 }
2239
2240 keys = PyDict_Keys(self->inputs);
2241 num = PyList_Size(keys);
2242
2243 for (j = 0; j < num; j++)
2244 {
2245 key = PyList_GetItem(keys, j);
2246 MYFLT *st = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyDict_GetItem(self->inputs, key), "_getStream", NULL));
2247 list_of_gains = PyDict_GetItem(self->gains, key);
2248 list_of_last_gains = PyDict_GetItem(self->lastGains, key);
2249 list_of_current_gains = PyDict_GetItem(self->currentGains, key);
2250 list_of_step_vals = PyDict_GetItem(self->stepVals, key);
2251 list_of_time_counts = PyDict_GetItem(self->timeCounts, key);
2252
2253 for (k = 0; k < self->num_outs; k++)
2254 {
2255 amp = (MYFLT)PyFloat_AS_DOUBLE(PyList_GetItem(list_of_gains, k));
2256 lastAmp = (MYFLT)PyFloat_AS_DOUBLE(PyList_GetItem(list_of_last_gains, k));
2257 currentAmp = (MYFLT)PyFloat_AS_DOUBLE(PyList_GetItem(list_of_current_gains, k));
2258 tmpStepVal = (MYFLT)PyFloat_AS_DOUBLE(PyList_GetItem(list_of_step_vals, k));
2259 tmpCount = (int)PyLong_AsLong(PyList_GetItem(list_of_time_counts, k));
2260
2261 if (amp != lastAmp)
2262 {
2263 tmpCount = 0;
2264 tmpStepVal = (amp - currentAmp) / self->timeStep;
2265 PyList_SetItem(list_of_last_gains, k, PyFloat_FromDouble(amp));
2266 }
2267
2268 for (i = 0; i < self->bufsize; i++)
2269 {
2270 if (tmpCount == (self->timeStep - 1))
2271 {
2272 currentAmp = amp;
2273 tmpCount++;
2274 }
2275 else if (tmpCount < self->timeStep)
2276 {
2277 currentAmp += tmpStepVal;
2278 tmpCount++;
2279 }
2280
2281 self->buffer_streams[self->bufsize * k + i] += st[i] * currentAmp;
2282 }
2283
2284 PyList_SetItem(list_of_current_gains, k, PyFloat_FromDouble(currentAmp));
2285 PyList_SetItem(list_of_step_vals, k, PyFloat_FromDouble(tmpStepVal));
2286 PyList_SetItem(list_of_time_counts, k, PyLong_FromLong(tmpCount));
2287 }
2288 }
2289
2290 Py_XDECREF(keys);
2291 }
2292
2293 MYFLT *
2294 Mixer_getSamplesBuffer(Mixer *self)
2295 {
2296 return (MYFLT *)self->buffer_streams;
2297 }
2298
2299 static void
2300 Mixer_setProcMode(Mixer *self)
2301 {
2302 self->proc_func_ptr = Mixer_generate;
2303 }
2304
2305 static void
2306 Mixer_compute_next_data_frame(Mixer *self)
2307 {
2308 (*self->proc_func_ptr)(self);
2309 }
2310
2311 static int
2312 Mixer_traverse(Mixer *self, visitproc visit, void *arg)
2313 {
2314 pyo_VISIT
2315 Py_VISIT(self->inputs);
2316 Py_VISIT(self->gains);
2317 Py_VISIT(self->lastGains);
2318 Py_VISIT(self->currentGains);
2319 Py_VISIT(self->stepVals);
2320 Py_VISIT(self->timeCounts);
2321 return 0;
2322 }
2323
2324 static int
2325 Mixer_clear(Mixer *self)
2326 {
2327 pyo_CLEAR
2328 Py_CLEAR(self->inputs);
2329 Py_CLEAR(self->gains);
2330 Py_CLEAR(self->lastGains);
2331 Py_CLEAR(self->currentGains);
2332 Py_CLEAR(self->stepVals);
2333 Py_CLEAR(self->timeCounts);
2334 return 0;
2335 }
2336
2337 static void
2338 Mixer_dealloc(Mixer* self)
2339 {
2340 pyo_DEALLOC
2341 PyMem_RawFree(self->buffer_streams);
2342 Mixer_clear(self);
2343 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2344 Py_TYPE(self)->tp_free((PyObject*)self);
2345 }
2346
2347 static PyObject *
2348 Mixer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2349 {
2350 int i;
2351 PyObject *timetmp = NULL;
2352 Mixer *self;
2353 self = (Mixer *)type->tp_alloc(type, 0);
2354
2355 self->inputs = PyDict_New();
2356 self->gains = PyDict_New();
2357 self->lastGains = PyDict_New();
2358 self->currentGains = PyDict_New();
2359 self->stepVals = PyDict_New();
2360 self->timeCounts = PyDict_New();
2361 self->num_outs = 2;
2362 self->time = 0.025;
2363 self->timeStep = (long)(self->time * self->sr);
2364
2365 INIT_OBJECT_COMMON
2366 Stream_setFunctionPtr(self->stream, Mixer_compute_next_data_frame);
2367 self->mode_func_ptr = Mixer_setProcMode;
2368
2369 static char *kwlist[] = {"outs", "time", NULL};
2370
2371 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist, &self->num_outs, &timetmp))
2372 Py_RETURN_NONE;
2373
2374 if (timetmp)
2375 {
2376 PyObject_CallMethod((PyObject *)self, "setTime", "O", timetmp);
2377 }
2378
2379 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2380
2381 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->num_outs * self->bufsize * sizeof(MYFLT));
2382
2383 (*self->mode_func_ptr)(self);
2384
2385 return (PyObject *)self;
2386 }
2387
2388 static PyObject * Mixer_getServer(Mixer* self) { GET_SERVER };
2389 static PyObject * Mixer_getStream(Mixer* self) { GET_STREAM };
2390
2391 static PyObject * Mixer_play(Mixer *self, PyObject *args, PyObject *kwds) { PLAY };
2392 static PyObject * Mixer_stop(Mixer *self, PyObject *args, PyObject *kwds) { STOP };
2393
2394 static PyObject *
2395 Mixer_setTime(Mixer *self, PyObject *arg)
2396 {
2397 int i, j, num;
2398 PyObject *keys, *key, *list_of_time_counts;
2399
2400 ASSERT_ARG_NOT_NULL
2401
2402 if (PyNumber_Check(arg))
2403 {
2404 self->time = PyFloat_AsDouble(arg);
2405 self->timeStep = (long)(self->time * self->sr);
2406
2407 keys = PyDict_Keys(self->inputs);
2408 num = PyList_Size(keys);
2409
2410 if (num != 0)
2411 {
2412 for (j = 0; j < num; j++)
2413 {
2414 key = PyList_GET_ITEM(keys, j);
2415 list_of_time_counts = PyDict_GetItem(self->timeCounts, key);
2416
2417 for (i = 0; i < self->num_outs; i++)
2418 {
2419 PyList_SET_ITEM(list_of_time_counts, i, PyLong_FromLong(self->timeStep - 1));
2420 }
2421 }
2422 }
2423 }
2424
2425 Py_RETURN_NONE;
2426 }
2427
2428 static PyObject *
2429 Mixer_addInput(Mixer *self, PyObject *args, PyObject *kwds)
2430 {
2431 int i;
2432 PyObject *tmp;
2433 PyObject *initGains;
2434 PyObject *initLastGains;
2435 PyObject *initCurrentGains;
2436 PyObject *initStepVals;
2437 PyObject *initTimeCounts;
2438 PyObject *voice;
2439
2440 static char *kwlist[] = {"voice", "input", NULL};
2441
2442 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &voice, &tmp))
2443 {
2444 Py_RETURN_NONE;
2445 }
2446
2447 PyDict_SetItem(self->inputs, voice, tmp);
2448 initGains = PyList_New(self->num_outs);
2449 initLastGains = PyList_New(self->num_outs);
2450 initCurrentGains = PyList_New(self->num_outs);
2451 initStepVals = PyList_New(self->num_outs);
2452 initTimeCounts = PyList_New(self->num_outs);
2453
2454 PyObject *zero = PyFloat_FromDouble(0.0);
2455 for (i = 0; i < self->num_outs; i++)
2456 {
2457 Py_INCREF(zero);
2458 PyList_SET_ITEM(initGains, i, zero);
2459 Py_INCREF(zero);
2460 PyList_SET_ITEM(initLastGains, i, zero);
2461 Py_INCREF(zero);
2462 PyList_SET_ITEM(initCurrentGains, i, zero);
2463 Py_INCREF(zero);
2464 PyList_SET_ITEM(initStepVals, i, zero);
2465 PyList_SET_ITEM(initTimeCounts, i, PyLong_FromLong(0));
2466 }
2467 Py_DECREF(zero);
2468
2469 PyDict_SetItem(self->gains, voice, initGains);
2470 PyDict_SetItem(self->lastGains, voice, initLastGains);
2471 PyDict_SetItem(self->currentGains, voice, initCurrentGains);
2472 PyDict_SetItem(self->stepVals, voice, initStepVals);
2473 PyDict_SetItem(self->timeCounts, voice, initTimeCounts);
2474
2475 Py_RETURN_NONE;
2476 }
2477
2478 static PyObject *
2479 Mixer_delInput(Mixer *self, PyObject *arg)
2480 {
2481 int ret;
2482
2483 PyObject *key = arg;
2484 ret = PyDict_DelItem(self->inputs, key);
2485
2486 if (ret == 0)
2487 {
2488 PyDict_DelItem(self->gains, key);
2489 PyDict_DelItem(self->lastGains, key);
2490 PyDict_DelItem(self->currentGains, key);
2491 PyDict_DelItem(self->stepVals, key);
2492 PyDict_DelItem(self->timeCounts, key);
2493 }
2494 else
2495 {
2496 Py_RETURN_NONE;
2497 }
2498
2499 Py_RETURN_NONE;
2500 }
2501
2502 static PyObject *
2503 Mixer_setAmp(Mixer *self, PyObject *args, PyObject *kwds)
2504 {
2505 int tmpout;
2506 PyObject *tmpin, *amp;
2507 static char *kwlist[] = {"vin", "vout", "amp", NULL};
2508
2509 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OiO", kwlist, &tmpin, &tmpout, &amp))
2510 {
2511 Py_RETURN_NONE;
2512 }
2513
2514 if (! PyNumber_Check(amp))
2515 {
2516 PySys_WriteStdout("Mixer: amp argument must be a number!n");
2517 Py_RETURN_NONE;
2518 }
2519
2520 Py_INCREF(amp);
2521 PyList_SET_ITEM(PyDict_GetItem(self->gains, tmpin), tmpout, PyNumber_Float(amp));
2522
2523 Py_RETURN_NONE;
2524 }
2525
2526 static PyMemberDef Mixer_members[] =
2527 {
2528 {"server", T_OBJECT_EX, offsetof(Mixer, server), 0, "Pyo server."},
2529 {"stream", T_OBJECT_EX, offsetof(Mixer, stream), 0, "Stream object."},
2530 {"inputs", T_OBJECT_EX, offsetof(Mixer, inputs), 0, "Dictionary of input streams."},
2531 {"gains", T_OBJECT_EX, offsetof(Mixer, gains), 0, "Dictionary of list of amplitudes."},
2532 {NULL} /* Sentinel */
2533 };
2534
2535 static PyMethodDef Mixer_methods[] =
2536 {
2537 {"getServer", (PyCFunction)Mixer_getServer, METH_NOARGS, "Returns server object."},
2538 {"_getStream", (PyCFunction)Mixer_getStream, METH_NOARGS, "Returns stream object."},
2539 {"play", (PyCFunction)Mixer_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2540 {"stop", (PyCFunction)Mixer_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2541 {"setTime", (PyCFunction)Mixer_setTime, METH_O, "Sets ramp time in seconds."},
2542 {"addInput", (PyCFunction)Mixer_addInput, METH_VARARGS | METH_KEYWORDS, "Adds an input to the mixer."},
2543 {"delInput", (PyCFunction)Mixer_delInput, METH_O, "Removes an input from the mixer."},
2544 {"setAmp", (PyCFunction)Mixer_setAmp, METH_VARARGS | METH_KEYWORDS, "Sets the amplitude of a specific input toward a specific output."},
2545 {NULL} /* Sentinel */
2546 };
2547
2548 PyTypeObject MixerType =
2549 {
2550 PyVarObject_HEAD_INIT(NULL, 0)
2551 "_pyo.Mixer_base", /*tp_name*/
2552 sizeof(Mixer), /*tp_basicsize*/
2553 0, /*tp_itemsize*/
2554 (destructor)Mixer_dealloc, /*tp_dealloc*/
2555 0, /*tp_print*/
2556 0, /*tp_getattr*/
2557 0, /*tp_setattr*/
2558 0, /*tp_as_async (tp_compare in Python 2)*/
2559 0, /*tp_repr*/
2560 0, /*tp_as_number*/
2561 0, /*tp_as_sequence*/
2562 0, /*tp_as_mapping*/
2563 0, /*tp_hash */
2564 0, /*tp_call*/
2565 0, /*tp_str*/
2566 0, /*tp_getattro*/
2567 0, /*tp_setattro*/
2568 0, /*tp_as_buffer*/
2569 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2570 "Mixer objects. Mixes multiple inputs toward multiple outputs.", /* tp_doc */
2571 (traverseproc)Mixer_traverse, /* tp_traverse */
2572 (inquiry)Mixer_clear, /* tp_clear */
2573 0, /* tp_richcompare */
2574 0, /* tp_weaklistoffset */
2575 0, /* tp_iter */
2576 0, /* tp_iternext */
2577 Mixer_methods, /* tp_methods */
2578 Mixer_members, /* tp_members */
2579 0, /* tp_getset */
2580 0, /* tp_base */
2581 0, /* tp_dict */
2582 0, /* tp_descr_get */
2583 0, /* tp_descr_set */
2584 0, /* tp_dictoffset */
2585 0, /* tp_init */
2586 0, /* tp_alloc */
2587 Mixer_new, /* tp_new */
2588 };
2589
2590 /************************************************************************************************/
2591 /* MixerVoice streamer object */
2592 /************************************************************************************************/
2593 typedef struct
2594 {
2595 pyo_audio_HEAD
2596 Mixer *mainMixer;
2597 int modebuffer[2];
2598 int chnl; // voice order
2599 } MixerVoice;
2600
2601 static void MixerVoice_postprocessing_ii(MixerVoice *self) { POST_PROCESSING_II };
2602 static void MixerVoice_postprocessing_ai(MixerVoice *self) { POST_PROCESSING_AI };
2603 static void MixerVoice_postprocessing_ia(MixerVoice *self) { POST_PROCESSING_IA };
2604 static void MixerVoice_postprocessing_aa(MixerVoice *self) { POST_PROCESSING_AA };
2605 static void MixerVoice_postprocessing_ireva(MixerVoice *self) { POST_PROCESSING_IREVA };
2606 static void MixerVoice_postprocessing_areva(MixerVoice *self) { POST_PROCESSING_AREVA };
2607 static void MixerVoice_postprocessing_revai(MixerVoice *self) { POST_PROCESSING_REVAI };
2608 static void MixerVoice_postprocessing_revaa(MixerVoice *self) { POST_PROCESSING_REVAA };
2609 static void MixerVoice_postprocessing_revareva(MixerVoice *self) { POST_PROCESSING_REVAREVA };
2610
2611 static void
2612 MixerVoice_setProcMode(MixerVoice *self)
2613 {
2614 int muladdmode;
2615 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2616
2617 switch (muladdmode)
2618 {
2619 case 0:
2620 self->muladd_func_ptr = MixerVoice_postprocessing_ii;
2621 break;
2622
2623 case 1:
2624 self->muladd_func_ptr = MixerVoice_postprocessing_ai;
2625 break;
2626
2627 case 2:
2628 self->muladd_func_ptr = MixerVoice_postprocessing_revai;
2629 break;
2630
2631 case 10:
2632 self->muladd_func_ptr = MixerVoice_postprocessing_ia;
2633 break;
2634
2635 case 11:
2636 self->muladd_func_ptr = MixerVoice_postprocessing_aa;
2637 break;
2638
2639 case 12:
2640 self->muladd_func_ptr = MixerVoice_postprocessing_revaa;
2641 break;
2642
2643 case 20:
2644 self->muladd_func_ptr = MixerVoice_postprocessing_ireva;
2645 break;
2646
2647 case 21:
2648 self->muladd_func_ptr = MixerVoice_postprocessing_areva;
2649 break;
2650
2651 case 22:
2652 self->muladd_func_ptr = MixerVoice_postprocessing_revareva;
2653 break;
2654 }
2655 }
2656
2657 static void
2658 MixerVoice_compute_next_data_frame(MixerVoice *self)
2659 {
2660 int i;
2661 MYFLT *tmp;
2662 int offset = self->chnl * self->bufsize;
2663 tmp = Mixer_getSamplesBuffer((Mixer *)self->mainMixer);
2664
2665 for (i = 0; i < self->bufsize; i++)
2666 {
2667 self->data[i] = tmp[i + offset];
2668 }
2669
2670 (*self->muladd_func_ptr)(self);
2671 }
2672
2673 static int
2674 MixerVoice_traverse(MixerVoice *self, visitproc visit, void *arg)
2675 {
2676 pyo_VISIT
2677 Py_VISIT(self->mainMixer);
2678 return 0;
2679 }
2680
2681 static int
2682 MixerVoice_clear(MixerVoice *self)
2683 {
2684 pyo_CLEAR
2685 Py_CLEAR(self->mainMixer);
2686 return 0;
2687 }
2688
2689 static void
2690 MixerVoice_dealloc(MixerVoice* self)
2691 {
2692 pyo_DEALLOC
2693 MixerVoice_clear(self);
2694 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2695 Py_TYPE(self)->tp_free((PyObject*)self);
2696 }
2697
2698 static PyObject *
2699 MixerVoice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2700 {
2701 int i;
2702 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
2703 MixerVoice *self;
2704 self = (MixerVoice *)type->tp_alloc(type, 0);
2705
2706 self->modebuffer[0] = 0;
2707 self->modebuffer[1] = 0;
2708
2709 INIT_OBJECT_COMMON
2710 Stream_setFunctionPtr(self->stream, MixerVoice_compute_next_data_frame);
2711 self->mode_func_ptr = MixerVoice_setProcMode;
2712
2713 static char *kwlist[] = {"mainMixer", "chnl", "mul", "add", NULL};
2714
2715 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
2716 Py_RETURN_NONE;
2717
2718 self->mainMixer = (Mixer *)maintmp;
2719 Py_INCREF(self->mainMixer);
2720
2721 if (multmp)
2722 {
2723 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2724 }
2725
2726 if (addtmp)
2727 {
2728 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2729 }
2730
2731 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2732
2733 return (PyObject *)self;
2734 }
2735
2736 static PyObject * MixerVoice_getServer(MixerVoice* self) { GET_SERVER };
2737 static PyObject * MixerVoice_getStream(MixerVoice* self) { GET_STREAM };
2738 static PyObject * MixerVoice_setMul(MixerVoice *self, PyObject *arg) { SET_MUL };
2739 static PyObject * MixerVoice_setAdd(MixerVoice *self, PyObject *arg) { SET_ADD };
2740 static PyObject * MixerVoice_setSub(MixerVoice *self, PyObject *arg) { SET_SUB };
2741 static PyObject * MixerVoice_setDiv(MixerVoice *self, PyObject *arg) { SET_DIV };
2742
2743 static PyObject * MixerVoice_play(MixerVoice *self, PyObject *args, PyObject *kwds) { PLAY };
2744 static PyObject * MixerVoice_out(MixerVoice *self, PyObject *args, PyObject *kwds) { OUT };
2745 static PyObject * MixerVoice_stop(MixerVoice *self, PyObject *args, PyObject *kwds) { STOP };
2746
2747 static PyObject * MixerVoice_multiply(MixerVoice *self, PyObject *arg) { MULTIPLY };
2748 static PyObject * MixerVoice_inplace_multiply(MixerVoice *self, PyObject *arg) { INPLACE_MULTIPLY };
2749 static PyObject * MixerVoice_add(MixerVoice *self, PyObject *arg) { ADD };
2750 static PyObject * MixerVoice_inplace_add(MixerVoice *self, PyObject *arg) { INPLACE_ADD };
2751 static PyObject * MixerVoice_sub(MixerVoice *self, PyObject *arg) { SUB };
2752 static PyObject * MixerVoice_inplace_sub(MixerVoice *self, PyObject *arg) { INPLACE_SUB };
2753 static PyObject * MixerVoice_div(MixerVoice *self, PyObject *arg) { DIV };
2754 static PyObject * MixerVoice_inplace_div(MixerVoice *self, PyObject *arg) { INPLACE_DIV };
2755
2756 static PyMemberDef MixerVoice_members[] =
2757 {
2758 {"server", T_OBJECT_EX, offsetof(MixerVoice, server), 0, "Pyo server."},
2759 {"stream", T_OBJECT_EX, offsetof(MixerVoice, stream), 0, "Stream object."},
2760 {"mul", T_OBJECT_EX, offsetof(MixerVoice, mul), 0, "Mul factor."},
2761 {"add", T_OBJECT_EX, offsetof(MixerVoice, add), 0, "Add factor."},
2762 {NULL} /* Sentinel */
2763 };
2764
2765 static PyMethodDef MixerVoice_methods[] =
2766 {
2767 {"getServer", (PyCFunction)MixerVoice_getServer, METH_NOARGS, "Returns server object."},
2768 {"_getStream", (PyCFunction)MixerVoice_getStream, METH_NOARGS, "Returns stream object."},
2769 {"play", (PyCFunction)MixerVoice_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2770 {"out", (PyCFunction)MixerVoice_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2771 {"stop", (PyCFunction)MixerVoice_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2772 {"setMul", (PyCFunction)MixerVoice_setMul, METH_O, "Sets MixerVoice mul factor."},
2773 {"setAdd", (PyCFunction)MixerVoice_setAdd, METH_O, "Sets MixerVoice add factor."},
2774 {"setSub", (PyCFunction)MixerVoice_setSub, METH_O, "Sets inverse add factor."},
2775 {"setDiv", (PyCFunction)MixerVoice_setDiv, METH_O, "Sets inverse mul factor."},
2776 {NULL} /* Sentinel */
2777 };
2778
2779 static PyNumberMethods MixerVoice_as_number =
2780 {
2781 (binaryfunc)MixerVoice_add, /*nb_add*/
2782 (binaryfunc)MixerVoice_sub, /*nb_subtract*/
2783 (binaryfunc)MixerVoice_multiply, /*nb_multiply*/
2784 0, /*nb_remainder*/
2785 0, /*nb_divmod*/
2786 0, /*nb_power*/
2787 0, /*nb_neg*/
2788 0, /*nb_pos*/
2789 0, /*(unaryfunc)array_abs,*/
2790 0, /*nb_nonzero*/
2791 0, /*nb_invert*/
2792 0, /*nb_lshift*/
2793 0, /*nb_rshift*/
2794 0, /*nb_and*/
2795 0, /*nb_xor*/
2796 0, /*nb_or*/
2797 0, /*nb_int*/
2798 0, /*nb_long*/
2799 0, /*nb_float*/
2800 (binaryfunc)MixerVoice_inplace_add, /*inplace_add*/
2801 (binaryfunc)MixerVoice_inplace_sub, /*inplace_subtract*/
2802 (binaryfunc)MixerVoice_inplace_multiply, /*inplace_multiply*/
2803 0, /*inplace_remainder*/
2804 0, /*inplace_power*/
2805 0, /*inplace_lshift*/
2806 0, /*inplace_rshift*/
2807 0, /*inplace_and*/
2808 0, /*inplace_xor*/
2809 0, /*inplace_or*/
2810 0, /*nb_floor_divide*/
2811 (binaryfunc)MixerVoice_div, /*nb_true_divide*/
2812 0, /*nb_inplace_floor_divide*/
2813 (binaryfunc)MixerVoice_inplace_div, /*nb_inplace_true_divide*/
2814 0, /* nb_index */
2815 };
2816
2817 PyTypeObject MixerVoiceType =
2818 {
2819 PyVarObject_HEAD_INIT(NULL, 0)
2820 "_pyo.MixerVoice_base", /*tp_name*/
2821 sizeof(MixerVoice), /*tp_basicsize*/
2822 0, /*tp_itemsize*/
2823 (destructor)MixerVoice_dealloc, /*tp_dealloc*/
2824 0, /*tp_print*/
2825 0, /*tp_getattr*/
2826 0, /*tp_setattr*/
2827 0, /*tp_as_async (tp_compare in Python 2)*/
2828 0, /*tp_repr*/
2829 &MixerVoice_as_number, /*tp_as_number*/
2830 0, /*tp_as_sequence*/
2831 0, /*tp_as_mapping*/
2832 0, /*tp_hash */
2833 0, /*tp_call*/
2834 0, /*tp_str*/
2835 0, /*tp_getattro*/
2836 0, /*tp_setattro*/
2837 0, /*tp_as_buffer*/
2838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2839 "MixerVoice objects. Reads one band from a Mixer.", /* tp_doc */
2840 (traverseproc)MixerVoice_traverse, /* tp_traverse */
2841 (inquiry)MixerVoice_clear, /* tp_clear */
2842 0, /* tp_richcompare */
2843 0, /* tp_weaklistoffset */
2844 0, /* tp_iter */
2845 0, /* tp_iternext */
2846 MixerVoice_methods, /* tp_methods */
2847 MixerVoice_members, /* tp_members */
2848 0, /* tp_getset */
2849 0, /* tp_base */
2850 0, /* tp_dict */
2851 0, /* tp_descr_get */
2852 0, /* tp_descr_set */
2853 0, /* tp_dictoffset */
2854 0, /* tp_init */
2855 0, /* tp_alloc */
2856 MixerVoice_new, /* tp_new */
2857 };
2858
2859 /****************/
2860 /**** Selector *****/
2861 /****************/
2862 typedef struct
2863 {
2864 pyo_audio_HEAD
2865 PyObject *inputs;
2866 PyObject *voice;
2867 Stream *voice_stream;
2868 int chSize;
2869 int mode;
2870 int modebuffer[3]; // need at least 2 slots for mul & add
2871 } Selector;
2872
2873 static MYFLT
2874 Selector_clip_voice(Selector *self, MYFLT v)
2875 {
2876 int chSize = self->chSize - 1;
2877
2878 if (v < 0.0)
2879 return 0.0;
2880 else if (v > chSize)
2881 return chSize;
2882 else
2883 return v;
2884 }
2885
2886 static void
2887 Selector_generate_i(Selector *self)
2888 {
2889 int j1, j, i;
2890 MYFLT voice1, voice2;
2891 MYFLT voice = Selector_clip_voice(self, PyFloat_AS_DOUBLE(self->voice));
2892
2893 j1 = (int)voice;
2894 j = j1 + 1;
2895
2896 if (j1 >= (self->chSize - 1))
2897 {
2898 j1--;
2899 j--;
2900 }
2901
2902 MYFLT *st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j1), "_getStream", NULL));
2903 MYFLT *st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j), "_getStream", NULL));
2904
2905 voice = P_clip(voice - j1);
2906 voice1 = MYSQRT(1.0 - voice);
2907 voice2 = MYSQRT(voice);
2908
2909 for (i = 0; i < self->bufsize; i++)
2910 {
2911 self->data[i] = st1[i] * voice1 + st2[i] * voice2;
2912 }
2913 }
2914
2915 static void
2916 Selector_generate_lin_i(Selector *self)
2917 {
2918 int j1, j, i;
2919 MYFLT voice = Selector_clip_voice(self, PyFloat_AS_DOUBLE(self->voice));
2920
2921 j1 = (int)voice;
2922 j = j1 + 1;
2923
2924 if (j1 >= (self->chSize - 1))
2925 {
2926 j1--;
2927 j--;
2928 }
2929
2930 MYFLT *st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j1), "_getStream", NULL));
2931 MYFLT *st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j), "_getStream", NULL));
2932
2933 voice = P_clip(voice - j1);
2934
2935 for (i = 0; i < self->bufsize; i++)
2936 {
2937 self->data[i] = st1[i] * (1.0 - voice) + st2[i] * voice;
2938 }
2939 }
2940
2941 static void
2942 Selector_generate_a(Selector *self)
2943 {
2944 int old_j1, old_j, j1, j, i;
2945 MYFLT voice;
2946 MYFLT *st1, *st2;
2947 MYFLT *vc = Stream_getData((Stream *)self->voice_stream);
2948
2949 old_j1 = 0;
2950 old_j = 1;
2951 st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, old_j1), "_getStream", NULL));
2952 st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, old_j), "_getStream", NULL));
2953
2954 for (i = 0; i < self->bufsize; i++)
2955 {
2956 voice = Selector_clip_voice(self, vc[i]);
2957
2958 j1 = (int)voice;
2959 j = j1 + 1;
2960
2961 if (j1 >= (self->chSize - 1))
2962 {
2963 j1--;
2964 j--;
2965 }
2966
2967 if (j1 != old_j1)
2968 {
2969 st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j1), "_getStream", NULL));
2970 old_j1 = j1;
2971 }
2972
2973 if (j != old_j)
2974 {
2975 st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j), "_getStream", NULL));
2976 old_j = j;
2977 }
2978
2979 voice = P_clip(voice - j1);
2980
2981 self->data[i] = st1[i] * MYSQRT(1.0 - voice) + st2[i] * MYSQRT(voice);
2982 }
2983 }
2984
2985 static void
2986 Selector_generate_lin_a(Selector *self)
2987 {
2988 int old_j1, old_j, j1, j, i;
2989 MYFLT voice;
2990 MYFLT *st1, *st2;
2991 MYFLT *vc = Stream_getData((Stream *)self->voice_stream);
2992
2993 old_j1 = 0;
2994 old_j = 1;
2995 st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, old_j1), "_getStream", NULL));
2996 st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, old_j), "_getStream", NULL));
2997
2998 for (i = 0; i < self->bufsize; i++)
2999 {
3000 voice = Selector_clip_voice(self, vc[i]);
3001
3002 j1 = (int)voice;
3003 j = j1 + 1;
3004
3005 if (j1 >= (self->chSize - 1))
3006 {
3007 j1--;
3008 j--;
3009 }
3010
3011 if (j1 != old_j1)
3012 {
3013 st1 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j1), "_getStream", NULL));
3014 old_j1 = j1;
3015 }
3016
3017 if (j != old_j)
3018 {
3019 st2 = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->inputs, j), "_getStream", NULL));
3020 old_j = j;
3021 }
3022
3023 voice = P_clip(voice - j1);
3024
3025 self->data[i] = st1[i] * (1.0 - voice) + st2[i] * voice;
3026 }
3027 }
3028
3029 static void Selector_postprocessing_ii(Selector *self) { POST_PROCESSING_II };
3030 static void Selector_postprocessing_ai(Selector *self) { POST_PROCESSING_AI };
3031 static void Selector_postprocessing_ia(Selector *self) { POST_PROCESSING_IA };
3032 static void Selector_postprocessing_aa(Selector *self) { POST_PROCESSING_AA };
3033 static void Selector_postprocessing_ireva(Selector *self) { POST_PROCESSING_IREVA };
3034 static void Selector_postprocessing_areva(Selector *self) { POST_PROCESSING_AREVA };
3035 static void Selector_postprocessing_revai(Selector *self) { POST_PROCESSING_REVAI };
3036 static void Selector_postprocessing_revaa(Selector *self) { POST_PROCESSING_REVAA };
3037 static void Selector_postprocessing_revareva(Selector *self) { POST_PROCESSING_REVAREVA };
3038
3039 static void
3040 Selector_setProcMode(Selector *self)
3041 {
3042 int procmode, muladdmode;
3043 procmode = self->modebuffer[2];
3044 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3045
3046 switch (procmode)
3047 {
3048 case 0:
3049 if (self->mode == 0)
3050 self->proc_func_ptr = Selector_generate_i;
3051 else
3052 self->proc_func_ptr = Selector_generate_lin_i;
3053
3054 break;
3055
3056 case 1:
3057 if (self->mode == 0)
3058 self->proc_func_ptr = Selector_generate_a;
3059 else
3060 self->proc_func_ptr = Selector_generate_lin_a;
3061
3062 break;
3063 }
3064
3065 switch (muladdmode)
3066 {
3067 case 0:
3068 self->muladd_func_ptr = Selector_postprocessing_ii;
3069 break;
3070
3071 case 1:
3072 self->muladd_func_ptr = Selector_postprocessing_ai;
3073 break;
3074
3075 case 2:
3076 self->muladd_func_ptr = Selector_postprocessing_revai;
3077 break;
3078
3079 case 10:
3080 self->muladd_func_ptr = Selector_postprocessing_ia;
3081 break;
3082
3083 case 11:
3084 self->muladd_func_ptr = Selector_postprocessing_aa;
3085 break;
3086
3087 case 12:
3088 self->muladd_func_ptr = Selector_postprocessing_revaa;
3089 break;
3090
3091 case 20:
3092 self->muladd_func_ptr = Selector_postprocessing_ireva;
3093 break;
3094
3095 case 21:
3096 self->muladd_func_ptr = Selector_postprocessing_areva;
3097 break;
3098
3099 case 22:
3100 self->muladd_func_ptr = Selector_postprocessing_revareva;
3101 break;
3102 }
3103 }
3104
3105 static void
3106 Selector_compute_next_data_frame(Selector *self)
3107 {
3108 (*self->proc_func_ptr)(self);
3109 (*self->muladd_func_ptr)(self);
3110 }
3111
3112 static int
3113 Selector_traverse(Selector *self, visitproc visit, void *arg)
3114 {
3115 pyo_VISIT
3116 Py_VISIT(self->inputs);
3117 Py_VISIT(self->voice);
3118 return 0;
3119 }
3120
3121 static int
3122 Selector_clear(Selector *self)
3123 {
3124 pyo_CLEAR
3125 Py_CLEAR(self->inputs);
3126 Py_CLEAR(self->voice);
3127 return 0;
3128 }
3129
3130 static void
3131 Selector_dealloc(Selector* self)
3132 {
3133 pyo_DEALLOC
3134 Selector_clear(self);
3135 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3136 Py_TYPE(self)->tp_free((PyObject*)self);
3137 }
3138
3139 static PyObject *
3140 Selector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3141 {
3142 int i;
3143 PyObject *inputstmp = NULL, *voicetmp = NULL, *multmp = NULL, *addtmp = NULL;
3144 Selector *self;
3145 self = (Selector *)type->tp_alloc(type, 0);
3146
3147 self->voice = PyFloat_FromDouble(0.);
3148 self->mode = 0;
3149 self->modebuffer[0] = 0;
3150 self->modebuffer[1] = 0;
3151 self->modebuffer[2] = 0;
3152
3153 INIT_OBJECT_COMMON
3154 Stream_setFunctionPtr(self->stream, Selector_compute_next_data_frame);
3155 self->mode_func_ptr = Selector_setProcMode;
3156
3157 static char *kwlist[] = {"inputs", "voice", "mul", "add", NULL};
3158
3159 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputstmp, &voicetmp, &multmp, &addtmp))
3160 Py_RETURN_NONE;
3161
3162 if (inputstmp)
3163 {
3164 PyObject_CallMethod((PyObject *)self, "setInputs", "O", inputstmp);
3165 }
3166
3167 if (voicetmp)
3168 {
3169 PyObject_CallMethod((PyObject *)self, "setVoice", "O", voicetmp);
3170 }
3171
3172 if (multmp)
3173 {
3174 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3175 }
3176
3177 if (addtmp)
3178 {
3179 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3180 }
3181
3182 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3183
3184 (*self->mode_func_ptr)(self);
3185
3186 return (PyObject *)self;
3187 }
3188
3189 static PyObject * Selector_getServer(Selector* self) { GET_SERVER };
3190 static PyObject * Selector_getStream(Selector* self) { GET_STREAM };
3191 static PyObject * Selector_setMul(Selector *self, PyObject *arg) { SET_MUL };
3192 static PyObject * Selector_setAdd(Selector *self, PyObject *arg) { SET_ADD };
3193 static PyObject * Selector_setSub(Selector *self, PyObject *arg) { SET_SUB };
3194 static PyObject * Selector_setDiv(Selector *self, PyObject *arg) { SET_DIV };
3195
3196 static PyObject * Selector_play(Selector *self, PyObject *args, PyObject *kwds) { PLAY };
3197 static PyObject * Selector_out(Selector *self, PyObject *args, PyObject *kwds) { OUT };
3198 static PyObject * Selector_stop(Selector *self, PyObject *args, PyObject *kwds) { STOP };
3199
3200 static PyObject * Selector_multiply(Selector *self, PyObject *arg) { MULTIPLY };
3201 static PyObject * Selector_inplace_multiply(Selector *self, PyObject *arg) { INPLACE_MULTIPLY };
3202 static PyObject * Selector_add(Selector *self, PyObject *arg) { ADD };
3203 static PyObject * Selector_inplace_add(Selector *self, PyObject *arg) { INPLACE_ADD };
3204 static PyObject * Selector_sub(Selector *self, PyObject *arg) { SUB };
3205 static PyObject * Selector_inplace_sub(Selector *self, PyObject *arg) { INPLACE_SUB };
3206 static PyObject * Selector_div(Selector *self, PyObject *arg) { DIV };
3207 static PyObject * Selector_inplace_div(Selector *self, PyObject *arg) { INPLACE_DIV };
3208
3209 static PyObject *
3210 Selector_setInputs(Selector *self, PyObject *arg)
3211 {
3212 if (! PyList_Check(arg))
3213 {
3214 PyErr_SetString(PyExc_TypeError, "The inputs attribute must be a list.");
3215 Py_RETURN_NONE;
3216 }
3217
3218 self->chSize = PyList_Size(arg);
3219 Py_XDECREF(self->inputs);
3220 self->inputs = arg;
3221 Py_INCREF(self->inputs);
3222
3223 Py_RETURN_NONE;
3224 }
3225
3226 static PyObject * Selector_setVoice(Selector *self, PyObject *arg) { SET_PARAM(self->voice, self->voice_stream, 2); }
3227
3228 static PyObject *
3229 Selector_setMode(Selector *self, PyObject *arg)
3230 {
3231 ASSERT_ARG_NOT_NULL
3232
3233 if (PyLong_Check(arg) == 1)
3234 {
3235 self->mode = PyLong_AsLong(arg);
3236 }
3237
3238 (*self->mode_func_ptr)(self);
3239
3240 Py_RETURN_NONE;
3241 }
3242
3243 static PyMemberDef Selector_members[] =
3244 {
3245 {"server", T_OBJECT_EX, offsetof(Selector, server), 0, "Pyo server."},
3246 {"stream", T_OBJECT_EX, offsetof(Selector, stream), 0, "Stream object."},
3247 {"inputs", T_OBJECT_EX, offsetof(Selector, inputs), 0, "List of input streams."},
3248 {"voice", T_OBJECT_EX, offsetof(Selector, voice), 0, "Voice position pointer."},
3249 {"mul", T_OBJECT_EX, offsetof(Selector, mul), 0, "Mul factor."},
3250 {"add", T_OBJECT_EX, offsetof(Selector, add), 0, "Add factor."},
3251 {NULL} /* Sentinel */
3252 };
3253
3254 static PyMethodDef Selector_methods[] =
3255 {
3256 {"getServer", (PyCFunction)Selector_getServer, METH_NOARGS, "Returns server object."},
3257 {"_getStream", (PyCFunction)Selector_getStream, METH_NOARGS, "Returns stream object."},
3258 {"play", (PyCFunction)Selector_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3259 {"out", (PyCFunction)Selector_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3260 {"stop", (PyCFunction)Selector_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3261 {"setInputs", (PyCFunction)Selector_setInputs, METH_O, "Sets list of input streams."},
3262 {"setVoice", (PyCFunction)Selector_setVoice, METH_O, "Sets voice position pointer."},
3263 {"setMode", (PyCFunction)Selector_setMode, METH_O, "Sets interpolation algorithm."},
3264 {"setMul", (PyCFunction)Selector_setMul, METH_O, "Sets mul factor."},
3265 {"setAdd", (PyCFunction)Selector_setAdd, METH_O, "Sets add factor."},
3266 {"setSub", (PyCFunction)Selector_setSub, METH_O, "Sets inverse add factor."},
3267 {"setDiv", (PyCFunction)Selector_setDiv, METH_O, "Sets inverse mul factor."},
3268 {NULL} /* Sentinel */
3269 };
3270
3271 static PyNumberMethods Selector_as_number =
3272 {
3273 (binaryfunc)Selector_add, /*nb_add*/
3274 (binaryfunc)Selector_sub, /*nb_subtract*/
3275 (binaryfunc)Selector_multiply, /*nb_multiply*/
3276 0, /*nb_remainder*/
3277 0, /*nb_divmod*/
3278 0, /*nb_power*/
3279 0, /*nb_neg*/
3280 0, /*nb_pos*/
3281 0, /*(unaryfunc)array_abs,*/
3282 0, /*nb_nonzero*/
3283 0, /*nb_invert*/
3284 0, /*nb_lshift*/
3285 0, /*nb_rshift*/
3286 0, /*nb_and*/
3287 0, /*nb_xor*/
3288 0, /*nb_or*/
3289 0, /*nb_int*/
3290 0, /*nb_long*/
3291 0, /*nb_float*/
3292 (binaryfunc)Selector_inplace_add, /*inplace_add*/
3293 (binaryfunc)Selector_inplace_sub, /*inplace_subtract*/
3294 (binaryfunc)Selector_inplace_multiply, /*inplace_multiply*/
3295 0, /*inplace_remainder*/
3296 0, /*inplace_power*/
3297 0, /*inplace_lshift*/
3298 0, /*inplace_rshift*/
3299 0, /*inplace_and*/
3300 0, /*inplace_xor*/
3301 0, /*inplace_or*/
3302 0, /*nb_floor_divide*/
3303 (binaryfunc)Selector_div, /*nb_true_divide*/
3304 0, /*nb_inplace_floor_divide*/
3305 (binaryfunc)Selector_inplace_div, /*nb_inplace_true_divide*/
3306 0, /* nb_index */
3307 };
3308
3309 PyTypeObject SelectorType =
3310 {
3311 PyVarObject_HEAD_INIT(NULL, 0)
3312 "_pyo.Selector_base", /*tp_name*/
3313 sizeof(Selector), /*tp_basicsize*/
3314 0, /*tp_itemsize*/
3315 (destructor)Selector_dealloc, /*tp_dealloc*/
3316 0, /*tp_print*/
3317 0, /*tp_getattr*/
3318 0, /*tp_setattr*/
3319 0, /*tp_as_async (tp_compare in Python 2)*/
3320 0, /*tp_repr*/
3321 &Selector_as_number, /*tp_as_number*/
3322 0, /*tp_as_sequence*/
3323 0, /*tp_as_mapping*/
3324 0, /*tp_hash */
3325 0, /*tp_call*/
3326 0, /*tp_str*/
3327 0, /*tp_getattro*/
3328 0, /*tp_setattro*/
3329 0, /*tp_as_buffer*/
3330 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3331 "Selector objects. Audio interpolation between multiple inputs.", /* tp_doc */
3332 (traverseproc)Selector_traverse, /* tp_traverse */
3333 (inquiry)Selector_clear, /* tp_clear */
3334 0, /* tp_richcompare */
3335 0, /* tp_weaklistoffset */
3336 0, /* tp_iter */
3337 0, /* tp_iternext */
3338 Selector_methods, /* tp_methods */
3339 Selector_members, /* tp_members */
3340 0, /* tp_getset */
3341 0, /* tp_base */
3342 0, /* tp_dict */
3343 0, /* tp_descr_get */
3344 0, /* tp_descr_set */
3345 0, /* tp_dictoffset */
3346 0, /* tp_init */
3347 0, /* tp_alloc */
3348 Selector_new, /* tp_new */
3349 };
3350