Debian Codegraph @

src/objects/bandsplitmodule.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 *q;
35 Stream *q_stream;
36 int bands;
37 MYFLT min_freq;
38 MYFLT max_freq;
39 int init;
40 MYFLT halfSr;
41 MYFLT TwoPiOnSr;
42 MYFLT *band_freqs;
43 // sample memories
44 MYFLT *x1;
45 MYFLT *x2;
46 MYFLT *y1;
47 MYFLT *y2;
48 // coefficients
49 MYFLT *b0;
50 MYFLT *b2;
51 MYFLT *a0;
52 MYFLT *a1;
53 MYFLT *a2;
54 MYFLT *buffer_streams;
55 int modebuffer[1];
56 } BandSplitter;
57
58 static void
59 BandSplitter_compute_variables(BandSplitter *self, MYFLT q)
60 {
61 int i;
62 MYFLT freq;
63
64 for (i = 0; i < self->bands; i++)
65 {
66 freq = self->band_freqs[i];
67
68 if (freq <= 1)
69 freq = 1;
70 else if (freq >= self->halfSr)
71 freq = self->halfSr;
72
73 MYFLT w0 = self->TwoPiOnSr * freq;
74 MYFLT c = MYCOS(w0);
75 MYFLT alpha = MYSIN(w0) / (2 * q);
76
77 self->b0[i] = alpha;
78 self->b2[i] = -alpha;
79 self->a0[i] = 1.0 / (1 + alpha);
80 self->a1[i] = -2 * c;
81 self->a2[i] = 1 - alpha;
82 }
83 }
84
85 static void
86 BandSplitter_setFrequencies(BandSplitter *self)
87 {
88 int i;
89 MYFLT frac = 1. / self->bands;
90
91 for (i = 0; i < self->bands; i++)
92 {
93 self->band_freqs[i] = MYPOW(MYPOW(self->max_freq / self->min_freq, frac), i) * self->min_freq;
94 }
95 }
96
97 static void
98 BandSplitter_filters_i(BandSplitter *self)
99 {
100 MYFLT val;
101 int j, i;
102 MYFLT *in = Stream_getData((Stream *)self->input_stream);
103
104 if (self->init == 1)
105 {
106 for (j = 0; j < self->bands; j++)
107 {
108 self->x1[j] = self->x2[j] = self->y1[j] = self->y2[j] = in[0];
109 }
110
111 self->init = 0;
112 }
113
114 for (j = 0; j < self->bands; j++)
115 {
116 for (i = 0; i < self->bufsize; i++)
117 {
118 val = ( (self->b0[j] * in[i]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
119 self->y2[j] = self->y1[j];
120 self->buffer_streams[i + j * self->bufsize] = self->y1[j] = val;
121 self->x2[j] = self->x1[j];
122 self->x1[j] = in[i];
123 }
124 }
125 }
126
127 static void
128 BandSplitter_filters_a(BandSplitter *self)
129 {
130 MYFLT val;
131 int j, i;
132 MYFLT *in = Stream_getData((Stream *)self->input_stream);
133
134 if (self->init == 1)
135 {
136 for (j = 0; j < self->bands; j++)
137 {
138 self->x1[j] = self->x2[j] = self->y1[j] = self->y2[j] = in[0];
139 }
140
141 self->init = 0;
142 }
143
144 MYFLT *q = Stream_getData((Stream *)self->q_stream);
145
146 for (i = 0; i < self->bufsize; i++)
147 {
148 BandSplitter_compute_variables((BandSplitter *)self, q[i]);
149
150 for (j = 0; j < self->bands; j++)
151 {
152 val = ( (self->b0[j] * in[i]) + (self->b2[j] * self->x2[j]) - (self->a1[j] * self->y1[j]) - (self->a2[j] * self->y2[j]) ) * self->a0[j];
153 self->y2[j] = self->y1[j];
154 self->buffer_streams[i + j * self->bufsize] = self->y1[j] = val;
155 self->x2[j] = self->x1[j];
156 self->x1[j] = in[i];
157 }
158 }
159 }
160
161 MYFLT *
162 BandSplitter_getSamplesBuffer(BandSplitter *self)
163 {
164 return (MYFLT *)self->buffer_streams;
165 }
166
167 static void
168 BandSplitter_setProcMode(BandSplitter *self)
169 {
170 int procmode;
171 procmode = self->modebuffer[0];
172
173 switch (procmode)
174 {
175 case 0:
176 self->proc_func_ptr = BandSplitter_filters_i;
177 break;
178
179 case 1:
180 self->proc_func_ptr = BandSplitter_filters_a;
181 break;
182 }
183 }
184
185 static void
186 BandSplitter_compute_next_data_frame(BandSplitter *self)
187 {
188 (*self->proc_func_ptr)(self);
189 }
190
191 static int
192 BandSplitter_traverse(BandSplitter *self, visitproc visit, void *arg)
193 {
194 pyo_VISIT
195 Py_VISIT(self->input);
196 Py_VISIT(self->q);
197 return 0;
198 }
199
200 static int
201 BandSplitter_clear(BandSplitter *self)
202 {
203 pyo_CLEAR
204 Py_CLEAR(self->input);
205 Py_CLEAR(self->q);
206 return 0;
207 }
208
209 static void
210 BandSplitter_dealloc(BandSplitter* self)
211 {
212 pyo_DEALLOC
213 PyMem_RawFree(self->band_freqs);
214 PyMem_RawFree(self->x1);
215 PyMem_RawFree(self->x2);
216 PyMem_RawFree(self->y1);
217 PyMem_RawFree(self->y2);
218 PyMem_RawFree(self->b0);
219 PyMem_RawFree(self->b2);
220 PyMem_RawFree(self->a0);
221 PyMem_RawFree(self->a1);
222 PyMem_RawFree(self->a2);
223 PyMem_RawFree(self->buffer_streams);
224 BandSplitter_clear(self);
225 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
226 Py_TYPE(self)->tp_free((PyObject*)self);
227 }
228
229 static PyObject *
230 BandSplitter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
231 {
232 int i;
233 PyObject *inputtmp, *input_streamtmp, *qtmp = NULL;
234 BandSplitter *self;
235 self = (BandSplitter *)type->tp_alloc(type, 0);
236
237
238 self->bands = 4;
239 self->q = PyFloat_FromDouble(1.);
240 self->init = 1;
241
242 INIT_OBJECT_COMMON
243 Stream_setFunctionPtr(self->stream, BandSplitter_compute_next_data_frame);
244 self->mode_func_ptr = BandSplitter_setProcMode;
245
246 self->halfSr = self->sr / 2.01;
247 self->TwoPiOnSr = TWOPI / self->sr;
248
249 static char *kwlist[] = {"input", "bands", "min", "max", "q", NULL};
250
251 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_IFFO, kwlist, &inputtmp, &self->bands, &self->min_freq, &self->max_freq, &qtmp))
252 Py_RETURN_NONE;
253
254 INIT_INPUT_STREAM
255
256 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
257
258 self->band_freqs = (MYFLT *)PyMem_RawRealloc(self->band_freqs, self->bands * sizeof(MYFLT));
259 self->x1 = (MYFLT *)PyMem_RawRealloc(self->x1, self->bands * sizeof(MYFLT));
260 self->x2 = (MYFLT *)PyMem_RawRealloc(self->x2, self->bands * sizeof(MYFLT));
261 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, self->bands * sizeof(MYFLT));
262 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, self->bands * sizeof(MYFLT));
263 self->b0 = (MYFLT *)PyMem_RawRealloc(self->b0, self->bands * sizeof(MYFLT));
264 self->b2 = (MYFLT *)PyMem_RawRealloc(self->b2, self->bands * sizeof(MYFLT));
265 self->a0 = (MYFLT *)PyMem_RawRealloc(self->a0, self->bands * sizeof(MYFLT));
266 self->a1 = (MYFLT *)PyMem_RawRealloc(self->a1, self->bands * sizeof(MYFLT));
267 self->a2 = (MYFLT *)PyMem_RawRealloc(self->a2, self->bands * sizeof(MYFLT));
268 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->bands * self->bufsize * sizeof(MYFLT));
269
270 BandSplitter_setFrequencies((BandSplitter *)self);
271
272 if (qtmp)
273 {
274 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
275 }
276 else
277 {
278 BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
279 }
280
281 (*self->mode_func_ptr)(self);
282
283 return (PyObject *)self;
284 }
285
286 static PyObject * BandSplitter_setQ(BandSplitter *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 0); }
287
288 static PyObject * BandSplitter_getServer(BandSplitter* self) { GET_SERVER };
289 static PyObject * BandSplitter_getStream(BandSplitter* self) { GET_STREAM };
290
291 static PyObject * BandSplitter_play(BandSplitter *self, PyObject *args, PyObject *kwds) { PLAY };
292 static PyObject * BandSplitter_stop(BandSplitter *self, PyObject *args, PyObject *kwds) { STOP };
293
294 static PyMemberDef BandSplitter_members[] =
295 {
296 {"server", T_OBJECT_EX, offsetof(BandSplitter, server), 0, "Pyo server."},
297 {"stream", T_OBJECT_EX, offsetof(BandSplitter, stream), 0, "Stream object."},
298 {"input", T_OBJECT_EX, offsetof(BandSplitter, input), 0, "Input sound object."},
299 {"q", T_OBJECT_EX, offsetof(BandSplitter, q), 0, "Filters Q."},
300 {NULL} /* Sentinel */
301 };
302
303 static PyMethodDef BandSplitter_methods[] =
304 {
305 {"getServer", (PyCFunction)BandSplitter_getServer, METH_NOARGS, "Returns server object."},
306 {"_getStream", (PyCFunction)BandSplitter_getStream, METH_NOARGS, "Returns stream object."},
307 {"setQ", (PyCFunction)BandSplitter_setQ, METH_O, "Sets the filters Q."},
308 {"play", (PyCFunction)BandSplitter_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
309 {"stop", (PyCFunction)BandSplitter_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
310 {NULL} /* Sentinel */
311 };
312
313 PyTypeObject BandSplitterType =
314 {
315 PyVarObject_HEAD_INIT(NULL, 0)
316 "_pyo.BandSplitter_base", /*tp_name*/
317 sizeof(BandSplitter), /*tp_basicsize*/
318 0, /*tp_itemsize*/
319 (destructor)BandSplitter_dealloc, /*tp_dealloc*/
320 0, /*tp_print*/
321 0, /*tp_getattr*/
322 0, /*tp_setattr*/
323 0, /*tp_as_async (tp_compare in Python 2)*/
324 0, /*tp_repr*/
325 0, /*tp_as_number*/
326 0, /*tp_as_sequence*/
327 0, /*tp_as_mapping*/
328 0, /*tp_hash */
329 0, /*tp_call*/
330 0, /*tp_str*/
331 0, /*tp_getattro*/
332 0, /*tp_setattro*/
333 0, /*tp_as_buffer*/
334 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
335 "BandSplitter objects. Split audio stream in multiple frequency bands.", /* tp_doc */
336 (traverseproc)BandSplitter_traverse, /* tp_traverse */
337 (inquiry)BandSplitter_clear, /* tp_clear */
338 0, /* tp_richcompare */
339 0, /* tp_weaklistoffset */
340 0, /* tp_iter */
341 0, /* tp_iternext */
342 BandSplitter_methods, /* tp_methods */
343 BandSplitter_members, /* tp_members */
344 0, /* tp_getset */
345 0, /* tp_base */
346 0, /* tp_dict */
347 0, /* tp_descr_get */
348 0, /* tp_descr_set */
349 0, /* tp_dictoffset */
350 0, /* tp_init */
351 0, /* tp_alloc */
352 BandSplitter_new, /* tp_new */
353 };
354
355 /************************************************************************************************/
356 /* BandSplit streamer object */
357 /************************************************************************************************/
358 typedef struct
359 {
360 pyo_audio_HEAD
361 BandSplitter *mainSplitter;
362 int modebuffer[2];
363 int chnl;
364 } BandSplit;
365
366 static void BandSplit_postprocessing_ii(BandSplit *self) { POST_PROCESSING_II };
367 static void BandSplit_postprocessing_ai(BandSplit *self) { POST_PROCESSING_AI };
368 static void BandSplit_postprocessing_ia(BandSplit *self) { POST_PROCESSING_IA };
369 static void BandSplit_postprocessing_aa(BandSplit *self) { POST_PROCESSING_AA };
370 static void BandSplit_postprocessing_ireva(BandSplit *self) { POST_PROCESSING_IREVA };
371 static void BandSplit_postprocessing_areva(BandSplit *self) { POST_PROCESSING_AREVA };
372 static void BandSplit_postprocessing_revai(BandSplit *self) { POST_PROCESSING_REVAI };
373 static void BandSplit_postprocessing_revaa(BandSplit *self) { POST_PROCESSING_REVAA };
374 static void BandSplit_postprocessing_revareva(BandSplit *self) { POST_PROCESSING_REVAREVA };
375
376 static void
377 BandSplit_setProcMode(BandSplit *self)
378 {
379 int muladdmode;
380 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
381
382 switch (muladdmode)
383 {
384 case 0:
385 self->muladd_func_ptr = BandSplit_postprocessing_ii;
386 break;
387
388 case 1:
389 self->muladd_func_ptr = BandSplit_postprocessing_ai;
390 break;
391
392 case 2:
393 self->muladd_func_ptr = BandSplit_postprocessing_revai;
394 break;
395
396 case 10:
397 self->muladd_func_ptr = BandSplit_postprocessing_ia;
398 break;
399
400 case 11:
401 self->muladd_func_ptr = BandSplit_postprocessing_aa;
402 break;
403
404 case 12:
405 self->muladd_func_ptr = BandSplit_postprocessing_revaa;
406 break;
407
408 case 20:
409 self->muladd_func_ptr = BandSplit_postprocessing_ireva;
410 break;
411
412 case 21:
413 self->muladd_func_ptr = BandSplit_postprocessing_areva;
414 break;
415
416 case 22:
417 self->muladd_func_ptr = BandSplit_postprocessing_revareva;
418 break;
419 }
420 }
421
422 static void
423 BandSplit_compute_next_data_frame(BandSplit *self)
424 {
425 int i;
426 MYFLT *tmp;
427 int offset = self->chnl * self->bufsize;
428 tmp = BandSplitter_getSamplesBuffer((BandSplitter *)self->mainSplitter);
429
430 for (i = 0; i < self->bufsize; i++)
431 {
432 self->data[i] = tmp[i + offset];
433 }
434
435 (*self->muladd_func_ptr)(self);
436 }
437
438 static int
439 BandSplit_traverse(BandSplit *self, visitproc visit, void *arg)
440 {
441 pyo_VISIT
442 Py_VISIT(self->mainSplitter);
443 return 0;
444 }
445
446 static int
447 BandSplit_clear(BandSplit *self)
448 {
449 pyo_CLEAR
450 Py_CLEAR(self->mainSplitter);
451 return 0;
452 }
453
454 static void
455 BandSplit_dealloc(BandSplit* self)
456 {
457 pyo_DEALLOC
458 BandSplit_clear(self);
459 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
460 Py_TYPE(self)->tp_free((PyObject*)self);
461 }
462
463 static PyObject *
464 BandSplit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
465 {
466 int i;
467 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
468 BandSplit *self;
469 self = (BandSplit *)type->tp_alloc(type, 0);
470
471 self->chnl = 0;
472 self->modebuffer[0] = 0;
473 self->modebuffer[1] = 0;
474
475 INIT_OBJECT_COMMON
476 Stream_setFunctionPtr(self->stream, BandSplit_compute_next_data_frame);
477 self->mode_func_ptr = BandSplit_setProcMode;
478
479 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
480
481 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
482 Py_RETURN_NONE;
483
484 self->mainSplitter = (BandSplitter *)maintmp;
485 Py_INCREF(self->mainSplitter);
486
487 if (multmp)
488 {
489 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
490 }
491
492 if (addtmp)
493 {
494 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
495 }
496
497 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
498
499 (*self->mode_func_ptr)(self);
500
501 return (PyObject *)self;
502 }
503
504 static PyObject * BandSplit_getServer(BandSplit* self) { GET_SERVER };
505 static PyObject * BandSplit_getStream(BandSplit* self) { GET_STREAM };
506 static PyObject * BandSplit_setMul(BandSplit *self, PyObject *arg) { SET_MUL };
507 static PyObject * BandSplit_setAdd(BandSplit *self, PyObject *arg) { SET_ADD };
508 static PyObject * BandSplit_setSub(BandSplit *self, PyObject *arg) { SET_SUB };
509 static PyObject * BandSplit_setDiv(BandSplit *self, PyObject *arg) { SET_DIV };
510
511 static PyObject * BandSplit_play(BandSplit *self, PyObject *args, PyObject *kwds) { PLAY };
512 static PyObject * BandSplit_out(BandSplit *self, PyObject *args, PyObject *kwds) { OUT };
513 static PyObject * BandSplit_stop(BandSplit *self, PyObject *args, PyObject *kwds) { STOP };
514
515 static PyObject * BandSplit_multiply(BandSplit *self, PyObject *arg) { MULTIPLY };
516 static PyObject * BandSplit_inplace_multiply(BandSplit *self, PyObject *arg) { INPLACE_MULTIPLY };
517 static PyObject * BandSplit_add(BandSplit *self, PyObject *arg) { ADD };
518 static PyObject * BandSplit_inplace_add(BandSplit *self, PyObject *arg) { INPLACE_ADD };
519 static PyObject * BandSplit_sub(BandSplit *self, PyObject *arg) { SUB };
520 static PyObject * BandSplit_inplace_sub(BandSplit *self, PyObject *arg) { INPLACE_SUB };
521 static PyObject * BandSplit_div(BandSplit *self, PyObject *arg) { DIV };
522 static PyObject * BandSplit_inplace_div(BandSplit *self, PyObject *arg) { INPLACE_DIV };
523
524 static PyMemberDef BandSplit_members[] =
525 {
526 {"server", T_OBJECT_EX, offsetof(BandSplit, server), 0, "Pyo server."},
527 {"stream", T_OBJECT_EX, offsetof(BandSplit, stream), 0, "Stream object."},
528 {"mul", T_OBJECT_EX, offsetof(BandSplit, mul), 0, "Mul factor."},
529 {"add", T_OBJECT_EX, offsetof(BandSplit, add), 0, "Add factor."},
530 {NULL} /* Sentinel */
531 };
532
533 static PyMethodDef BandSplit_methods[] =
534 {
535 {"getServer", (PyCFunction)BandSplit_getServer, METH_NOARGS, "Returns server object."},
536 {"_getStream", (PyCFunction)BandSplit_getStream, METH_NOARGS, "Returns stream object."},
537 {"play", (PyCFunction)BandSplit_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
538 {"out", (PyCFunction)BandSplit_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
539 {"stop", (PyCFunction)BandSplit_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
540 {"setMul", (PyCFunction)BandSplit_setMul, METH_O, "Sets BandSplit mul factor."},
541 {"setAdd", (PyCFunction)BandSplit_setAdd, METH_O, "Sets BandSplit add factor."},
542 {"setSub", (PyCFunction)BandSplit_setSub, METH_O, "Sets inverse add factor."},
543 {"setDiv", (PyCFunction)BandSplit_setDiv, METH_O, "Sets inverse mul factor."},
544 {NULL} /* Sentinel */
545 };
546
547 static PyNumberMethods BandSplit_as_number =
548 {
549 (binaryfunc)BandSplit_add, /*nb_add*/
550 (binaryfunc)BandSplit_sub, /*nb_subtract*/
551 (binaryfunc)BandSplit_multiply, /*nb_multiply*/
552 0, /*nb_remainder*/
553 0, /*nb_divmod*/
554 0, /*nb_power*/
555 0, /*nb_neg*/
556 0, /*nb_pos*/
557 0, /*(unaryfunc)array_abs,*/
558 0, /*nb_nonzero*/
559 0, /*nb_invert*/
560 0, /*nb_lshift*/
561 0, /*nb_rshift*/
562 0, /*nb_and*/
563 0, /*nb_xor*/
564 0, /*nb_or*/
565 0, /*nb_int*/
566 0, /*nb_long*/
567 0, /*nb_float*/
568 (binaryfunc)BandSplit_inplace_add, /*inplace_add*/
569 (binaryfunc)BandSplit_inplace_sub, /*inplace_subtract*/
570 (binaryfunc)BandSplit_inplace_multiply, /*inplace_multiply*/
571 0, /*inplace_remainder*/
572 0, /*inplace_power*/
573 0, /*inplace_lshift*/
574 0, /*inplace_rshift*/
575 0, /*inplace_and*/
576 0, /*inplace_xor*/
577 0, /*inplace_or*/
578 0, /*nb_floor_divide*/
579 (binaryfunc)BandSplit_div, /*nb_true_divide*/
580 0, /*nb_inplace_floor_divide*/
581 (binaryfunc)BandSplit_inplace_div, /*nb_inplace_true_divide*/
582 0, /* nb_index */
583 };
584
585 PyTypeObject BandSplitType =
586 {
587 PyVarObject_HEAD_INIT(NULL, 0)
588 "_pyo.BandSplit_base", /*tp_name*/
589 sizeof(BandSplit), /*tp_basicsize*/
590 0, /*tp_itemsize*/
591 (destructor)BandSplit_dealloc, /*tp_dealloc*/
592 0, /*tp_print*/
593 0, /*tp_getattr*/
594 0, /*tp_setattr*/
595 0, /*tp_as_async (tp_compare in Python 2)*/
596 0, /*tp_repr*/
597 &BandSplit_as_number, /*tp_as_number*/
598 0, /*tp_as_sequence*/
599 0, /*tp_as_mapping*/
600 0, /*tp_hash */
601 0, /*tp_call*/
602 0, /*tp_str*/
603 0, /*tp_getattro*/
604 0, /*tp_setattro*/
605 0, /*tp_as_buffer*/
606 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
607 "BandSplit objects. Reads one band from a BandSplitter process.", /* tp_doc */
608 (traverseproc)BandSplit_traverse, /* tp_traverse */
609 (inquiry)BandSplit_clear, /* tp_clear */
610 0, /* tp_richcompare */
611 0, /* tp_weaklistoffset */
612 0, /* tp_iter */
613 0, /* tp_iternext */
614 BandSplit_methods, /* tp_methods */
615 BandSplit_members, /* tp_members */
616 0, /* tp_getset */
617 0, /* tp_base */
618 0, /* tp_dict */
619 0, /* tp_descr_get */
620 0, /* tp_descr_set */
621 0, /* tp_dictoffset */
622 0, /* tp_init */
623 0, /* tp_alloc */
624 BandSplit_new, /* tp_new */
625 };
626
627 /*****************/
628 /* FourBand main */
629 /*****************/
630 typedef struct
631 {
632 pyo_audio_HEAD
633 PyObject *input;
634 Stream *input_stream;
635 PyObject *freq1;
636 Stream *freq1_stream;
637 PyObject *freq2;
638 Stream *freq2_stream;
639 PyObject *freq3;
640 Stream *freq3_stream;
641 double last_freq1;
642 double last_freq2;
643 double last_freq3;
644 // sample memories
645 double x1[12];
646 double x2[12];
647 double x3[12];
648 double x4[12];
649 double y1[12];
650 double y2[12];
651 double y3[12];
652 double y4[12];
653 // coefficients
654 double b1[3];
655 double b2[3];
656 double b3[3];
657 double b4[3];
658 double la0[3];
659 double la1[3];
660 double la2[3];
661 double ha0[3];
662 double ha1[3];
663 double ha2[3];
664 MYFLT *buffer_streams;
665 int modebuffer[3];
666 } FourBandMain;
667
668 static void
669 FourBandMain_compute_variables(FourBandMain *self, double freq, int bound)
670 {
671 double wc = TWOPI * freq;
672 double wc2 = wc * wc;
673 double wc3 = wc2 * wc;
674 double wc4 = wc2 * wc2;
675 double k = wc / tan(PI * freq / self->sr);
676 double k2 = k * k;
677 double k3 = k2 * k;
678 double k4 = k2 * k2;
679 double sqrt2 = sqrt(2.0);
680 double sq_tmp1 = sqrt2 * wc3 * k;
681 double sq_tmp2 = sqrt2 * wc * k3;
682 double a_tmp = 4.0 * wc2 * k2 + 2.0 * sq_tmp1 + k4 + 2.0 * sq_tmp2 + wc4;
683 double wc4_a_tmp = wc4 / a_tmp;
684 double k4_a_tmp = k4 / a_tmp;
685
686 /* common */
687 self->b1[bound] = (4.0 * (wc4 + sq_tmp1 - k4 - sq_tmp2)) / a_tmp;
688 self->b2[bound] = (6.0 * wc4 - 8.0 * wc2 * k2 + 6.0 * k4) / a_tmp;
689 self->b3[bound] = (4.0 * (wc4 - sq_tmp1 + sq_tmp2 - k4)) / a_tmp;
690 self->b4[bound] = (k4 - 2.0 * sq_tmp1 + wc4 - 2.0 * sq_tmp2 + 4.0 * wc2 * k2) / a_tmp;
691
692 /* lowpass */
693 self->la0[bound] = wc4_a_tmp;
694 self->la1[bound] = 4.0 * wc4_a_tmp;
695 self->la2[bound] = 6.0 * wc4_a_tmp;
696
697 /* highpass */
698 self->ha0[bound] = k4_a_tmp;
699 self->ha1[bound] = -4.0 * k4_a_tmp;
700 self->ha2[bound] = 6.0 * k4_a_tmp;
701 }
702
703 static int
704 FourBandMain_splitter(FourBandMain *self, MYFLT *input, MYFLT *outlow, MYFLT *outhigh, int bound, int filtercount)
705 {
706 int i, indl = filtercount, indh = filtercount + 1;
707 double val, inval;
708
709 for (i = 0; i < self->bufsize; i++)
710 {
711 inval = (double)input[i];
712 /* lowpass */
713 val = self->la0[bound] * inval + self->la1[bound] * self->x1[indl] + self->la2[bound] * self->x2[indl] +
714 self->la1[bound] * self->x3[indl] + self->la0[bound] * self->x4[indl] - self->b1[bound] * self->y1[indl] -
715 self->b2[bound] * self->y2[indl] - self->b3[bound] * self->y3[indl] - self->b4[bound] * self->y4[indl];
716 self->y4[indl] = self->y3[indl];
717 self->y3[indl] = self->y2[indl];
718 self->y2[indl] = self->y1[indl];
719 self->y1[indl] = val;
720 self->x4[indl] = self->x3[indl];
721 self->x3[indl] = self->x2[indl];
722 self->x2[indl] = self->x1[indl];
723 self->x1[indl] = inval;
724 outlow[i] = (MYFLT)val;
725
726 /* highpass */
727 val = self->ha0[bound] * inval + self->ha1[bound] * self->x1[indh] + self->ha2[bound] * self->x2[indh] +
728 self->ha1[bound] * self->x3[indh] + self->ha0[bound] * self->x4[indh] - self->b1[bound] * self->y1[indh] -
729 self->b2[bound] * self->y2[indh] - self->b3[bound] * self->y3[indh] - self->b4[bound] * self->y4[indh];
730 self->y4[indh] = self->y3[indh];
731 self->y3[indh] = self->y2[indh];
732 self->y2[indh] = self->y1[indh];
733 self->y1[indh] = val;
734 self->x4[indh] = self->x3[indh];
735 self->x3[indh] = self->x2[indh];
736 self->x2[indh] = self->x1[indh];
737 self->x1[indh] = inval;
738 outhigh[i] = (MYFLT)val;
739 }
740
741 return filtercount + 2;
742 }
743
744 static int
745 FourBandMain_phase_align(FourBandMain *self, MYFLT *input, int bound, int filtercount)
746 {
747 int i, indl = filtercount, indh = filtercount + 1;
748 double val, inval;
749 MYFLT tmplow[self->bufsize], tmphigh[self->bufsize];
750
751 for (i = 0; i < self->bufsize; i++)
752 {
753 inval = (double)input[i];
754 /* lowpass */
755 val = self->la0[bound] * inval + self->la1[bound] * self->x1[indl] + self->la2[bound] * self->x2[indl] +
756 self->la1[bound] * self->x3[indl] + self->la0[bound] * self->x4[indl] - self->b1[bound] * self->y1[indl] -
757 self->b2[bound] * self->y2[indl] - self->b3[bound] * self->y3[indl] - self->b4[bound] * self->y4[indl];
758 self->y4[indl] = self->y3[indl];
759 self->y3[indl] = self->y2[indl];
760 self->y2[indl] = self->y1[indl];
761 self->y1[indl] = val;
762 self->x4[indl] = self->x3[indl];
763 self->x3[indl] = self->x2[indl];
764 self->x2[indl] = self->x1[indl];
765 self->x1[indl] = inval;
766 tmplow[i] = (MYFLT)val;
767
768 /* highpass */
769 val = self->ha0[bound] * inval + self->ha1[bound] * self->x1[indh] + self->ha2[bound] * self->x2[indh] +
770 self->ha1[bound] * self->x3[indh] + self->ha0[bound] * self->x4[indh] - self->b1[bound] * self->y1[indh] -
771 self->b2[bound] * self->y2[indh] - self->b3[bound] * self->y3[indh] - self->b4[bound] * self->y4[indh];
772 self->y4[indh] = self->y3[indh];
773 self->y3[indh] = self->y2[indh];
774 self->y2[indh] = self->y1[indh];
775 self->y1[indh] = val;
776 self->x4[indh] = self->x3[indh];
777 self->x3[indh] = self->x2[indh];
778 self->x2[indh] = self->x1[indh];
779 self->x1[indh] = inval;
780 tmphigh[i] = (MYFLT)val;
781 }
782
783 for (i = 0; i < self->bufsize; i++)
784 {
785 input[i] = tmplow[i] + tmphigh[i];
786 }
787
788 return filtercount + 2;
789 }
790
791 static void
792 FourBandMain_filters(FourBandMain *self)
793 {
794 int i, bound, align, filtercount = 0;
795 int bounds = 3;
796 double f1, f2, f3;
797 MYFLT *input;
798 MYFLT outlow[self->bufsize], outhigh[self->bufsize];
799
800 MYFLT *in = Stream_getData((Stream *)self->input_stream);
801
802 if (self->modebuffer[0] == 0)
803 f1 = PyFloat_AS_DOUBLE(self->freq1);
804 else
805 f1 = (double)Stream_getData((Stream *)self->freq1_stream)[0];
806
807 if (self->modebuffer[1] == 0)
808 f2 = PyFloat_AS_DOUBLE(self->freq2);
809 else
810 f2 = (double)Stream_getData((Stream *)self->freq2_stream)[0];
811
812 if (self->modebuffer[2] == 0)
813 f3 = PyFloat_AS_DOUBLE(self->freq3);
814 else
815 f3 = (double)Stream_getData((Stream *)self->freq3_stream)[0];
816
817 if (f1 != self->last_freq1)
818 {
819 self->last_freq1 = f1;
820 FourBandMain_compute_variables(self, f1, 0);
821 }
822
823 if (f2 != self->last_freq2)
824 {
825 self->last_freq2 = f2;
826 FourBandMain_compute_variables(self, f2, 1);
827 }
828
829 if (f3 != self->last_freq3)
830 {
831 self->last_freq3 = f3;
832 FourBandMain_compute_variables(self, f3, 2);
833 }
834
835 input = in;
836
837 for (bound = 0; bound < bounds; bound++)
838 {
839 filtercount = FourBandMain_splitter(self, input, outlow, outhigh, bound, filtercount);
840
841 for (align = bound + 1; align < bounds; align++)
842 {
843 filtercount = FourBandMain_phase_align(self, outlow, align, filtercount);
844 }
845
846 for (i = 0; i < self->bufsize; i++)
847 {
848 self->buffer_streams[i + bound * self->bufsize] = outlow[i];
849 }
850
851 input = outhigh;
852 }
853
854 for (i = 0; i < self->bufsize; i++)
855 {
856 self->buffer_streams[i + bounds * self->bufsize] = outhigh[i];
857 }
858 }
859
860 MYFLT *
861 FourBandMain_getSamplesBuffer(FourBandMain *self)
862 {
863 return (MYFLT *)self->buffer_streams;
864 }
865
866 static void
867 FourBandMain_setProcMode(FourBandMain *self)
868 {
869 self->proc_func_ptr = FourBandMain_filters;
870 }
871
872 static void
873 FourBandMain_compute_next_data_frame(FourBandMain *self)
874 {
875 (*self->proc_func_ptr)(self);
876 }
877
878 static int
879 FourBandMain_traverse(FourBandMain *self, visitproc visit, void *arg)
880 {
881 pyo_VISIT
882 Py_VISIT(self->input);
883 Py_VISIT(self->freq1);
884 Py_VISIT(self->freq2);
885 Py_VISIT(self->freq3);
886 return 0;
887 }
888
889 static int
890 FourBandMain_clear(FourBandMain *self)
891 {
892 pyo_CLEAR
893 Py_CLEAR(self->input);
894 Py_CLEAR(self->freq1);
895 Py_CLEAR(self->freq2);
896 Py_CLEAR(self->freq3);
897 return 0;
898 }
899
900 static void
901 FourBandMain_dealloc(FourBandMain* self)
902 {
903 pyo_DEALLOC
904 PyMem_RawFree(self->buffer_streams);
905 FourBandMain_clear(self);
906 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
907 Py_TYPE(self)->tp_free((PyObject*)self);
908 }
909
910 static PyObject *
911 FourBandMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
912 {
913 int i;
914 PyObject *inputtmp, *input_streamtmp, *freq1tmp = NULL, *freq2tmp = NULL, *freq3tmp = NULL;
915 FourBandMain *self;
916 self = (FourBandMain *)type->tp_alloc(type, 0);
917
918 self->freq1 = PyFloat_FromDouble(150);
919 self->freq2 = PyFloat_FromDouble(500);
920 self->freq3 = PyFloat_FromDouble(2000);
921 self->last_freq1 = self->last_freq2 = self->last_freq3 = -1.0;
922
923 self->modebuffer[0] = 0;
924 self->modebuffer[1] = 0;
925 self->modebuffer[2] = 0;
926
927 INIT_OBJECT_COMMON
928 Stream_setFunctionPtr(self->stream, FourBandMain_compute_next_data_frame);
929 self->mode_func_ptr = FourBandMain_setProcMode;
930
931 static char *kwlist[] = {"input", "freq1", "freq2", "freq3", NULL};
932
933 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freq1tmp, &freq2tmp, &freq3tmp))
934 Py_RETURN_NONE;
935
936 INIT_INPUT_STREAM
937
938 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
939
940 for (i = 0; i < 6; i++)
941 {
942 self->x1[i] = self->x2[i] = self->x3[i] = self->x4[i] = self->y1[i] = self->y2[i] = self->y3[i] = self->y4[i] = 0.0;
943 }
944
945 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, 4 * self->bufsize * sizeof(MYFLT));
946
947 for (i = 0; i < (4 * self->bufsize); i++)
948 {
949 self->buffer_streams[i] = 0.0;
950 }
951
952 if (freq1tmp)
953 {
954 PyObject_CallMethod((PyObject *)self, "setFreq1", "O", freq1tmp);
955 }
956
957 if (freq2tmp)
958 {
959 PyObject_CallMethod((PyObject *)self, "setFreq2", "O", freq2tmp);
960 }
961
962 if (freq3tmp)
963 {
964 PyObject_CallMethod((PyObject *)self, "setFreq3", "O", freq3tmp);
965 }
966
967 (*self->mode_func_ptr)(self);
968
969 return (PyObject *)self;
970 }
971
972 static PyObject * FourBandMain_setFreq1(FourBandMain *self, PyObject *arg) { SET_PARAM(self->freq1, self->freq1_stream, 0); }
973 static PyObject * FourBandMain_setFreq2(FourBandMain *self, PyObject *arg) { SET_PARAM(self->freq2, self->freq2_stream, 1); }
974 static PyObject * FourBandMain_setFreq3(FourBandMain *self, PyObject *arg) { SET_PARAM(self->freq3, self->freq3_stream, 2); }
975
976 static PyObject * FourBandMain_getServer(FourBandMain* self) { GET_SERVER };
977 static PyObject * FourBandMain_getStream(FourBandMain* self) { GET_STREAM };
978
979 static PyObject * FourBandMain_play(FourBandMain *self, PyObject *args, PyObject *kwds) { PLAY };
980 static PyObject * FourBandMain_stop(FourBandMain *self, PyObject *args, PyObject *kwds) { STOP };
981
982 static PyMemberDef FourBandMain_members[] =
983 {
984 {"server", T_OBJECT_EX, offsetof(FourBandMain, server), 0, "Pyo server."},
985 {"stream", T_OBJECT_EX, offsetof(FourBandMain, stream), 0, "Stream object."},
986 {"input", T_OBJECT_EX, offsetof(FourBandMain, input), 0, "Input sound object."},
987 {"freq1", T_OBJECT_EX, offsetof(FourBandMain, freq1), 0, "First cutoff frequency."},
988 {"freq2", T_OBJECT_EX, offsetof(FourBandMain, freq2), 0, "Second cutoff frequency."},
989 {"freq3", T_OBJECT_EX, offsetof(FourBandMain, freq3), 0, "Third cutoff frequency."},
990 {NULL} /* Sentinel */
991 };
992
993 static PyMethodDef FourBandMain_methods[] =
994 {
995 {"getServer", (PyCFunction)FourBandMain_getServer, METH_NOARGS, "Returns server object."},
996 {"_getStream", (PyCFunction)FourBandMain_getStream, METH_NOARGS, "Returns stream object."},
997 {"setFreq1", (PyCFunction)FourBandMain_setFreq1, METH_O, "Sets the first cutoff frequency."},
998 {"setFreq2", (PyCFunction)FourBandMain_setFreq2, METH_O, "Sets the second cutoff frequency."},
999 {"setFreq3", (PyCFunction)FourBandMain_setFreq3, METH_O, "Sets the third cutoff frequency."},
1000 {"play", (PyCFunction)FourBandMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1001 {"stop", (PyCFunction)FourBandMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1002 {NULL} /* Sentinel */
1003 };
1004
1005 PyTypeObject FourBandMainType =
1006 {
1007 PyVarObject_HEAD_INIT(NULL, 0)
1008 "_pyo.FourBandMain_base", /*tp_name*/
1009 sizeof(FourBandMain), /*tp_basicsize*/
1010 0, /*tp_itemsize*/
1011 (destructor)FourBandMain_dealloc, /*tp_dealloc*/
1012 0, /*tp_print*/
1013 0, /*tp_getattr*/
1014 0, /*tp_setattr*/
1015 0, /*tp_as_async (tp_compare in Python 2)*/
1016 0, /*tp_repr*/
1017 0, /*tp_as_number*/
1018 0, /*tp_as_sequence*/
1019 0, /*tp_as_mapping*/
1020 0, /*tp_hash */
1021 0, /*tp_call*/
1022 0, /*tp_str*/
1023 0, /*tp_getattro*/
1024 0, /*tp_setattro*/
1025 0, /*tp_as_buffer*/
1026 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1027 "FourBandMain objects. Split audio stream in four flat frequency and phase bands.", /* tp_doc */
1028 (traverseproc)FourBandMain_traverse, /* tp_traverse */
1029 (inquiry)FourBandMain_clear, /* tp_clear */
1030 0, /* tp_richcompare */
1031 0, /* tp_weaklistoffset */
1032 0, /* tp_iter */
1033 0, /* tp_iternext */
1034 FourBandMain_methods, /* tp_methods */
1035 FourBandMain_members, /* tp_members */
1036 0, /* tp_getset */
1037 0, /* tp_base */
1038 0, /* tp_dict */
1039 0, /* tp_descr_get */
1040 0, /* tp_descr_set */
1041 0, /* tp_dictoffset */
1042 0, /* tp_init */
1043 0, /* tp_alloc */
1044 FourBandMain_new, /* tp_new */
1045 };
1046
1047 /************************************************************************************************/
1048 /* FourBand streamer object */
1049 /************************************************************************************************/
1050 typedef struct
1051 {
1052 pyo_audio_HEAD
1053 FourBandMain *mainSplitter;
1054 int modebuffer[2];
1055 int chnl;
1056 } FourBand;
1057
1058 static void FourBand_postprocessing_ii(FourBand *self) { POST_PROCESSING_II };
1059 static void FourBand_postprocessing_ai(FourBand *self) { POST_PROCESSING_AI };
1060 static void FourBand_postprocessing_ia(FourBand *self) { POST_PROCESSING_IA };
1061 static void FourBand_postprocessing_aa(FourBand *self) { POST_PROCESSING_AA };
1062 static void FourBand_postprocessing_ireva(FourBand *self) { POST_PROCESSING_IREVA };
1063 static void FourBand_postprocessing_areva(FourBand *self) { POST_PROCESSING_AREVA };
1064 static void FourBand_postprocessing_revai(FourBand *self) { POST_PROCESSING_REVAI };
1065 static void FourBand_postprocessing_revaa(FourBand *self) { POST_PROCESSING_REVAA };
1066 static void FourBand_postprocessing_revareva(FourBand *self) { POST_PROCESSING_REVAREVA };
1067
1068 static void
1069 FourBand_setProcMode(FourBand *self)
1070 {
1071 int muladdmode;
1072 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1073
1074 switch (muladdmode)
1075 {
1076 case 0:
1077 self->muladd_func_ptr = FourBand_postprocessing_ii;
1078 break;
1079
1080 case 1:
1081 self->muladd_func_ptr = FourBand_postprocessing_ai;
1082 break;
1083
1084 case 2:
1085 self->muladd_func_ptr = FourBand_postprocessing_revai;
1086 break;
1087
1088 case 10:
1089 self->muladd_func_ptr = FourBand_postprocessing_ia;
1090 break;
1091
1092 case 11:
1093 self->muladd_func_ptr = FourBand_postprocessing_aa;
1094 break;
1095
1096 case 12:
1097 self->muladd_func_ptr = FourBand_postprocessing_revaa;
1098 break;
1099
1100 case 20:
1101 self->muladd_func_ptr = FourBand_postprocessing_ireva;
1102 break;
1103
1104 case 21:
1105 self->muladd_func_ptr = FourBand_postprocessing_areva;
1106 break;
1107
1108 case 22:
1109 self->muladd_func_ptr = FourBand_postprocessing_revareva;
1110 break;
1111 }
1112 }
1113
1114 static void
1115 FourBand_compute_next_data_frame(FourBand *self)
1116 {
1117 int i;
1118 MYFLT *tmp;
1119 int offset = self->chnl * self->bufsize;
1120 tmp = FourBandMain_getSamplesBuffer((FourBandMain *)self->mainSplitter);
1121
1122 for (i = 0; i < self->bufsize; i++)
1123 {
1124 self->data[i] = tmp[i + offset];
1125 }
1126
1127 (*self->muladd_func_ptr)(self);
1128 }
1129
1130 static int
1131 FourBand_traverse(FourBand *self, visitproc visit, void *arg)
1132 {
1133 pyo_VISIT
1134 Py_VISIT(self->mainSplitter);
1135 return 0;
1136 }
1137
1138 static int
1139 FourBand_clear(FourBand *self)
1140 {
1141 pyo_CLEAR
1142 Py_CLEAR(self->mainSplitter);
1143 return 0;
1144 }
1145
1146 static void
1147 FourBand_dealloc(FourBand* self)
1148 {
1149 pyo_DEALLOC
1150 FourBand_clear(self);
1151 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1152 Py_TYPE(self)->tp_free((PyObject*)self);
1153 }
1154
1155 static PyObject *
1156 FourBand_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1157 {
1158 int i;
1159 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1160 FourBand *self;
1161 self = (FourBand *)type->tp_alloc(type, 0);
1162
1163 self->chnl = 0;
1164 self->modebuffer[0] = 0;
1165 self->modebuffer[1] = 0;
1166
1167 INIT_OBJECT_COMMON
1168 Stream_setFunctionPtr(self->stream, FourBand_compute_next_data_frame);
1169 self->mode_func_ptr = FourBand_setProcMode;
1170
1171 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1172
1173 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1174 Py_RETURN_NONE;
1175
1176 self->mainSplitter = (FourBandMain *)maintmp;
1177 Py_INCREF(self->mainSplitter);
1178
1179 if (multmp)
1180 {
1181 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1182 }
1183
1184 if (addtmp)
1185 {
1186 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1187 }
1188
1189 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1190
1191 (*self->mode_func_ptr)(self);
1192
1193 return (PyObject *)self;
1194 }
1195
1196 static PyObject * FourBand_getServer(FourBand* self) { GET_SERVER };
1197 static PyObject * FourBand_getStream(FourBand* self) { GET_STREAM };
1198 static PyObject * FourBand_setMul(FourBand *self, PyObject *arg) { SET_MUL };
1199 static PyObject * FourBand_setAdd(FourBand *self, PyObject *arg) { SET_ADD };
1200 static PyObject * FourBand_setSub(FourBand *self, PyObject *arg) { SET_SUB };
1201 static PyObject * FourBand_setDiv(FourBand *self, PyObject *arg) { SET_DIV };
1202
1203 static PyObject * FourBand_play(FourBand *self, PyObject *args, PyObject *kwds) { PLAY };
1204 static PyObject * FourBand_out(FourBand *self, PyObject *args, PyObject *kwds) { OUT };
1205 static PyObject * FourBand_stop(FourBand *self, PyObject *args, PyObject *kwds) { STOP };
1206
1207 static PyObject * FourBand_multiply(FourBand *self, PyObject *arg) { MULTIPLY };
1208 static PyObject * FourBand_inplace_multiply(FourBand *self, PyObject *arg) { INPLACE_MULTIPLY };
1209 static PyObject * FourBand_add(FourBand *self, PyObject *arg) { ADD };
1210 static PyObject * FourBand_inplace_add(FourBand *self, PyObject *arg) { INPLACE_ADD };
1211 static PyObject * FourBand_sub(FourBand *self, PyObject *arg) { SUB };
1212 static PyObject * FourBand_inplace_sub(FourBand *self, PyObject *arg) { INPLACE_SUB };
1213 static PyObject * FourBand_div(FourBand *self, PyObject *arg) { DIV };
1214 static PyObject * FourBand_inplace_div(FourBand *self, PyObject *arg) { INPLACE_DIV };
1215
1216 static PyMemberDef FourBand_members[] =
1217 {
1218 {"server", T_OBJECT_EX, offsetof(FourBand, server), 0, "Pyo server."},
1219 {"stream", T_OBJECT_EX, offsetof(FourBand, stream), 0, "Stream object."},
1220 {"mul", T_OBJECT_EX, offsetof(FourBand, mul), 0, "Mul factor."},
1221 {"add", T_OBJECT_EX, offsetof(FourBand, add), 0, "Add factor."},
1222 {NULL} /* Sentinel */
1223 };
1224
1225 static PyMethodDef FourBand_methods[] =
1226 {
1227 {"getServer", (PyCFunction)FourBand_getServer, METH_NOARGS, "Returns server object."},
1228 {"_getStream", (PyCFunction)FourBand_getStream, METH_NOARGS, "Returns stream object."},
1229 {"play", (PyCFunction)FourBand_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1230 {"out", (PyCFunction)FourBand_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1231 {"stop", (PyCFunction)FourBand_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1232 {"setMul", (PyCFunction)FourBand_setMul, METH_O, "Sets FourBand mul factor."},
1233 {"setAdd", (PyCFunction)FourBand_setAdd, METH_O, "Sets FourBand add factor."},
1234 {"setSub", (PyCFunction)FourBand_setSub, METH_O, "Sets inverse add factor."},
1235 {"setDiv", (PyCFunction)FourBand_setDiv, METH_O, "Sets inverse mul factor."},
1236 {NULL} /* Sentinel */
1237 };
1238
1239 static PyNumberMethods FourBand_as_number =
1240 {
1241 (binaryfunc)FourBand_add, /*nb_add*/
1242 (binaryfunc)FourBand_sub, /*nb_subtract*/
1243 (binaryfunc)FourBand_multiply, /*nb_multiply*/
1244 0, /*nb_remainder*/
1245 0, /*nb_divmod*/
1246 0, /*nb_power*/
1247 0, /*nb_neg*/
1248 0, /*nb_pos*/
1249 0, /*(unaryfunc)array_abs,*/
1250 0, /*nb_nonzero*/
1251 0, /*nb_invert*/
1252 0, /*nb_lshift*/
1253 0, /*nb_rshift*/
1254 0, /*nb_and*/
1255 0, /*nb_xor*/
1256 0, /*nb_or*/
1257 0, /*nb_int*/
1258 0, /*nb_long*/
1259 0, /*nb_float*/
1260 (binaryfunc)FourBand_inplace_add, /*inplace_add*/
1261 (binaryfunc)FourBand_inplace_sub, /*inplace_subtract*/
1262 (binaryfunc)FourBand_inplace_multiply, /*inplace_multiply*/
1263 0, /*inplace_remainder*/
1264 0, /*inplace_power*/
1265 0, /*inplace_lshift*/
1266 0, /*inplace_rshift*/
1267 0, /*inplace_and*/
1268 0, /*inplace_xor*/
1269 0, /*inplace_or*/
1270 0, /*nb_floor_divide*/
1271 (binaryfunc)FourBand_div, /*nb_true_divide*/
1272 0, /*nb_inplace_floor_divide*/
1273 (binaryfunc)FourBand_inplace_div, /*nb_inplace_true_divide*/
1274 0, /* nb_index */
1275 };
1276
1277 PyTypeObject FourBandType =
1278 {
1279 PyVarObject_HEAD_INIT(NULL, 0)
1280 "_pyo.FourBand_base", /*tp_name*/
1281 sizeof(FourBand), /*tp_basicsize*/
1282 0, /*tp_itemsize*/
1283 (destructor)FourBand_dealloc, /*tp_dealloc*/
1284 0, /*tp_print*/
1285 0, /*tp_getattr*/
1286 0, /*tp_setattr*/
1287 0, /*tp_as_async (tp_compare in Python 2)*/
1288 0, /*tp_repr*/
1289 &FourBand_as_number, /*tp_as_number*/
1290 0, /*tp_as_sequence*/
1291 0, /*tp_as_mapping*/
1292 0, /*tp_hash */
1293 0, /*tp_call*/
1294 0, /*tp_str*/
1295 0, /*tp_getattro*/
1296 0, /*tp_setattro*/
1297 0, /*tp_as_buffer*/
1298 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1299 "FourBand objects. Reads one band from a FourBandMain process.", /* tp_doc */
1300 (traverseproc)FourBand_traverse, /* tp_traverse */
1301 (inquiry)FourBand_clear, /* tp_clear */
1302 0, /* tp_richcompare */
1303 0, /* tp_weaklistoffset */
1304 0, /* tp_iter */
1305 0, /* tp_iternext */
1306 FourBand_methods, /* tp_methods */
1307 FourBand_members, /* tp_members */
1308 0, /* tp_getset */
1309 0, /* tp_base */
1310 0, /* tp_dict */
1311 0, /* tp_descr_get */
1312 0, /* tp_descr_set */
1313 0, /* tp_dictoffset */
1314 0, /* tp_init */
1315 0, /* tp_alloc */
1316 FourBand_new, /* tp_new */
1317 };
1318
1319 /*****************/
1320 /* MultiBand main */
1321 /*****************/
1322 typedef struct
1323 {
1324 pyo_audio_HEAD
1325 PyObject *input;
1326 Stream *input_stream;
1327 int nbands; // 2 -> 16
1328 // sample memories
1329 double x1[240];
1330 double x2[240];
1331 double x3[240];
1332 double x4[240];
1333 double y1[240];
1334 double y2[240];
1335 double y3[240];
1336 double y4[240];
1337 // coefficients
1338 double b1[15];
1339 double b2[15];
1340 double b3[15];
1341 double b4[15];
1342 double la0[15];
1343 double la1[15];
1344 double la2[15];
1345 double ha0[15];
1346 double ha1[15];
1347 double ha2[15];
1348 MYFLT *buffer_streams;
1349 } MultiBandMain;
1350
1351 static void
1352 MultiBandMain_compute_variables(MultiBandMain *self, double freq, int bound)
1353 {
1354 double wc = TWOPI * freq;
1355 double wc2 = wc * wc;
1356 double wc3 = wc2 * wc;
1357 double wc4 = wc2 * wc2;
1358 double k = wc / tan(PI * freq / self->sr);
1359 double k2 = k * k;
1360 double k3 = k2 * k;
1361 double k4 = k2 * k2;
1362 double sqrt2 = sqrt(2.0);
1363 double sq_tmp1 = sqrt2 * wc3 * k;
1364 double sq_tmp2 = sqrt2 * wc * k3;
1365 double a_tmp = 4.0 * wc2 * k2 + 2.0 * sq_tmp1 + k4 + 2.0 * sq_tmp2 + wc4;
1366 double wc4_a_tmp = wc4 / a_tmp;
1367 double k4_a_tmp = k4 / a_tmp;
1368
1369 /* common */
1370 self->b1[bound] = (4.0 * (wc4 + sq_tmp1 - k4 - sq_tmp2)) / a_tmp;
1371 self->b2[bound] = (6.0 * wc4 - 8.0 * wc2 * k2 + 6.0 * k4) / a_tmp;
1372 self->b3[bound] = (4.0 * (wc4 - sq_tmp1 + sq_tmp2 - k4)) / a_tmp;
1373 self->b4[bound] = (k4 - 2.0 * sq_tmp1 + wc4 - 2.0 * sq_tmp2 + 4.0 * wc2 * k2) / a_tmp;
1374
1375 /* lowpass */
1376 self->la0[bound] = wc4_a_tmp;
1377 self->la1[bound] = 4.0 * wc4_a_tmp;
1378 self->la2[bound] = 6.0 * wc4_a_tmp;
1379
1380 /* highpass */
1381 self->ha0[bound] = k4_a_tmp;
1382 self->ha1[bound] = -4.0 * k4_a_tmp;
1383 self->ha2[bound] = 6.0 * k4_a_tmp;
1384 }
1385
1386 static void
1387 MultiBandMain_set_filter_frequencies(MultiBandMain *self)
1388 {
1389 int i, nbounds = self->nbands - 1, minfreq = 50, maxfreq = 15000;
1390 int frange = maxfreq - minfreq;
1391 double norm, freq, step = 1.0 / self->nbands;
1392
1393 for (i = 0; i < nbounds; i++)
1394 {
1395 norm = pow((i + 1) * step, 3);
1396 freq = norm * frange + minfreq;
1397 MultiBandMain_compute_variables(self, freq, i);
1398 }
1399 }
1400
1401 static int
1402 MultiBandMain_splitter(MultiBandMain *self, MYFLT *input, MYFLT *outlow, MYFLT *outhigh, int bound, int filtercount)
1403 {
1404 int i, indl = filtercount, indh = filtercount + 1;
1405 double val, inval;
1406
1407 for (i = 0; i < self->bufsize; i++)
1408 {
1409 inval = (double)input[i];
1410 /* lowpass */
1411 val = self->la0[bound] * inval + self->la1[bound] * self->x1[indl] + self->la2[bound] * self->x2[indl] +
1412 self->la1[bound] * self->x3[indl] + self->la0[bound] * self->x4[indl] - self->b1[bound] * self->y1[indl] -
1413 self->b2[bound] * self->y2[indl] - self->b3[bound] * self->y3[indl] - self->b4[bound] * self->y4[indl];
1414 self->y4[indl] = self->y3[indl];
1415 self->y3[indl] = self->y2[indl];
1416 self->y2[indl] = self->y1[indl];
1417 self->y1[indl] = val;
1418 self->x4[indl] = self->x3[indl];
1419 self->x3[indl] = self->x2[indl];
1420 self->x2[indl] = self->x1[indl];
1421 self->x1[indl] = inval;
1422 outlow[i] = (MYFLT)val;
1423
1424 /* highpass */
1425 val = self->ha0[bound] * inval + self->ha1[bound] * self->x1[indh] + self->ha2[bound] * self->x2[indh] +
1426 self->ha1[bound] * self->x3[indh] + self->ha0[bound] * self->x4[indh] - self->b1[bound] * self->y1[indh] -
1427 self->b2[bound] * self->y2[indh] - self->b3[bound] * self->y3[indh] - self->b4[bound] * self->y4[indh];
1428 self->y4[indh] = self->y3[indh];
1429 self->y3[indh] = self->y2[indh];
1430 self->y2[indh] = self->y1[indh];
1431 self->y1[indh] = val;
1432 self->x4[indh] = self->x3[indh];
1433 self->x3[indh] = self->x2[indh];
1434 self->x2[indh] = self->x1[indh];
1435 self->x1[indh] = inval;
1436 outhigh[i] = (MYFLT)val;
1437 }
1438
1439 return filtercount + 2;
1440 }
1441
1442 static int
1443 MultiBandMain_phase_align(MultiBandMain *self, MYFLT *input, int bound, int filtercount)
1444 {
1445 int i, indl = filtercount, indh = filtercount + 1;
1446 double val, inval;
1447 MYFLT tmplow[self->bufsize], tmphigh[self->bufsize];
1448
1449 for (i = 0; i < self->bufsize; i++)
1450 {
1451 inval = (double)input[i];
1452 /* lowpass */
1453 val = self->la0[bound] * inval + self->la1[bound] * self->x1[indl] + self->la2[bound] * self->x2[indl] +
1454 self->la1[bound] * self->x3[indl] + self->la0[bound] * self->x4[indl] - self->b1[bound] * self->y1[indl] -
1455 self->b2[bound] * self->y2[indl] - self->b3[bound] * self->y3[indl] - self->b4[bound] * self->y4[indl];
1456 self->y4[indl] = self->y3[indl];
1457 self->y3[indl] = self->y2[indl];
1458 self->y2[indl] = self->y1[indl];
1459 self->y1[indl] = val;
1460 self->x4[indl] = self->x3[indl];
1461 self->x3[indl] = self->x2[indl];
1462 self->x2[indl] = self->x1[indl];
1463 self->x1[indl] = inval;
1464 tmplow[i] = (MYFLT)val;
1465
1466 /* highpass */
1467 val = self->ha0[bound] * inval + self->ha1[bound] * self->x1[indh] + self->ha2[bound] * self->x2[indh] +
1468 self->ha1[bound] * self->x3[indh] + self->ha0[bound] * self->x4[indh] - self->b1[bound] * self->y1[indh] -
1469 self->b2[bound] * self->y2[indh] - self->b3[bound] * self->y3[indh] - self->b4[bound] * self->y4[indh];
1470 self->y4[indh] = self->y3[indh];
1471 self->y3[indh] = self->y2[indh];
1472 self->y2[indh] = self->y1[indh];
1473 self->y1[indh] = val;
1474 self->x4[indh] = self->x3[indh];
1475 self->x3[indh] = self->x2[indh];
1476 self->x2[indh] = self->x1[indh];
1477 self->x1[indh] = inval;
1478 tmphigh[i] = (MYFLT)val;
1479 }
1480
1481 for (i = 0; i < self->bufsize; i++)
1482 {
1483 input[i] = tmplow[i] + tmphigh[i];
1484 }
1485
1486 return filtercount + 2;
1487 }
1488
1489 static void
1490 MultiBandMain_filters(MultiBandMain *self)
1491 {
1492 int i, bound, align, filtercount = 0;
1493 int bounds = self->nbands - 1;
1494 MYFLT *input;
1495 MYFLT outlow[self->bufsize], outhigh[self->bufsize];
1496
1497 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1498
1499 input = in;
1500
1501 for (bound = 0; bound < bounds; bound++)
1502 {
1503 filtercount = MultiBandMain_splitter(self, input, outlow, outhigh, bound, filtercount);
1504
1505 for (align = bound + 1; align < bounds; align++)
1506 {
1507 filtercount = MultiBandMain_phase_align(self, outlow, align, filtercount);
1508 }
1509
1510 for (i = 0; i < self->bufsize; i++)
1511 {
1512 self->buffer_streams[i + bound * self->bufsize] = outlow[i];
1513 }
1514
1515 input = outhigh;
1516 }
1517
1518 for (i = 0; i < self->bufsize; i++)
1519 {
1520 self->buffer_streams[i + bounds * self->bufsize] = outhigh[i];
1521 }
1522 }
1523
1524 MYFLT *
1525 MultiBandMain_getSamplesBuffer(MultiBandMain *self)
1526 {
1527 return (MYFLT *)self->buffer_streams;
1528 }
1529
1530 static void
1531 MultiBandMain_setProcMode(MultiBandMain *self)
1532 {
1533 self->proc_func_ptr = MultiBandMain_filters;
1534 }
1535
1536 static void
1537 MultiBandMain_compute_next_data_frame(MultiBandMain *self)
1538 {
1539 (*self->proc_func_ptr)(self);
1540 }
1541
1542 static int
1543 MultiBandMain_traverse(MultiBandMain *self, visitproc visit, void *arg)
1544 {
1545 pyo_VISIT
1546 Py_VISIT(self->input);
1547 return 0;
1548 }
1549
1550 static int
1551 MultiBandMain_clear(MultiBandMain *self)
1552 {
1553 pyo_CLEAR
1554 Py_CLEAR(self->input);
1555 return 0;
1556 }
1557
1558 static void
1559 MultiBandMain_dealloc(MultiBandMain* self)
1560 {
1561 pyo_DEALLOC
1562 PyMem_RawFree(self->buffer_streams);
1563 MultiBandMain_clear(self);
1564 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1565 Py_TYPE(self)->tp_free((PyObject*)self);
1566 }
1567
1568 static PyObject *
1569 MultiBandMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1570 {
1571 int i;
1572 PyObject *inputtmp, *input_streamtmp;
1573 MultiBandMain *self;
1574 self = (MultiBandMain *)type->tp_alloc(type, 0);
1575
1576 INIT_OBJECT_COMMON
1577 Stream_setFunctionPtr(self->stream, MultiBandMain_compute_next_data_frame);
1578 self->mode_func_ptr = MultiBandMain_setProcMode;
1579
1580 static char *kwlist[] = {"input", "num", NULL};
1581
1582 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi", kwlist, &inputtmp, &self->nbands))
1583 Py_RETURN_NONE;
1584
1585 INIT_INPUT_STREAM
1586
1587 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1588
1589 if (self->nbands < 2)
1590 self->nbands = 2;
1591 else if (self->nbands > 16)
1592 self->nbands = 16;
1593
1594 for (i = 0; i < 240; i++)
1595 {
1596 self->x1[i] = self->x2[i] = self->x3[i] = self->x4[i] = 0.0;
1597 self->y1[i] = self->y2[i] = self->y3[i] = self->y4[i] = 0.0;
1598 }
1599
1600 for (i = 0; i < 15; i++)
1601 {
1602 self->b1[i] = self->b2[i] = self->b3[i] = self->b4[i] = self->la0[i] = 0.0;
1603 self->la1[i] = self->la2[i] = self->ha0[i] = self->ha1[i] = self->ha2[i] = 0.0;
1604 }
1605
1606 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->nbands * self->bufsize * sizeof(MYFLT));
1607
1608 for (i = 0; i < (self->nbands * self->bufsize); i++)
1609 {
1610 self->buffer_streams[i] = 0.0;
1611 }
1612
1613 MultiBandMain_set_filter_frequencies(self);
1614
1615 (*self->mode_func_ptr)(self);
1616
1617 return (PyObject *)self;
1618 }
1619
1620 static PyObject *
1621 MultiBandMain_setFrequencies(MultiBandMain* self, PyObject *arg)
1622 {
1623 int i, bounds = self->nbands - 1;
1624
1625 if (PyList_Check(arg))
1626 {
1627 if (PyList_Size(arg) == bounds)
1628 {
1629 for (i = 0; i < bounds; i++)
1630 {
1631 MultiBandMain_compute_variables(self, PyFloat_AsDouble(PyList_GetItem(arg, i)), i);
1632 }
1633 }
1634 }
1635
1636 Py_RETURN_NONE;
1637 }
1638
1639 static PyObject * MultiBandMain_getServer(MultiBandMain* self) { GET_SERVER };
1640 static PyObject * MultiBandMain_getStream(MultiBandMain* self) { GET_STREAM };
1641
1642 static PyObject * MultiBandMain_play(MultiBandMain *self, PyObject *args, PyObject *kwds) { PLAY };
1643 static PyObject * MultiBandMain_stop(MultiBandMain *self, PyObject *args, PyObject *kwds) { STOP };
1644
1645 static PyMemberDef MultiBandMain_members[] =
1646 {
1647 {"server", T_OBJECT_EX, offsetof(MultiBandMain, server), 0, "Pyo server."},
1648 {"stream", T_OBJECT_EX, offsetof(MultiBandMain, stream), 0, "Stream object."},
1649 {"input", T_OBJECT_EX, offsetof(MultiBandMain, input), 0, "Input sound object."},
1650 {NULL} /* Sentinel */
1651 };
1652
1653 static PyMethodDef MultiBandMain_methods[] =
1654 {
1655 {"getServer", (PyCFunction)MultiBandMain_getServer, METH_NOARGS, "Returns server object."},
1656 {"_getStream", (PyCFunction)MultiBandMain_getStream, METH_NOARGS, "Returns stream object."},
1657 {"setFrequencies", (PyCFunction)MultiBandMain_setFrequencies, METH_O, "Sets new filter cutoff frequencies."},
1658 {"play", (PyCFunction)MultiBandMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1659 {"stop", (PyCFunction)MultiBandMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1660 {NULL} /* Sentinel */
1661 };
1662
1663 PyTypeObject MultiBandMainType =
1664 {
1665 PyVarObject_HEAD_INIT(NULL, 0)
1666 "_pyo.MultiBandMain_base", /*tp_name*/
1667 sizeof(MultiBandMain), /*tp_basicsize*/
1668 0, /*tp_itemsize*/
1669 (destructor)MultiBandMain_dealloc, /*tp_dealloc*/
1670 0, /*tp_print*/
1671 0, /*tp_getattr*/
1672 0, /*tp_setattr*/
1673 0, /*tp_as_async (tp_compare in Python 2)*/
1674 0, /*tp_repr*/
1675 0, /*tp_as_number*/
1676 0, /*tp_as_sequence*/
1677 0, /*tp_as_mapping*/
1678 0, /*tp_hash */
1679 0, /*tp_call*/
1680 0, /*tp_str*/
1681 0, /*tp_getattro*/
1682 0, /*tp_setattro*/
1683 0, /*tp_as_buffer*/
1684 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1685 "MultiBandMain objects. Split audio stream in four flat frequency and phase bands.", /* tp_doc */
1686 (traverseproc)MultiBandMain_traverse, /* tp_traverse */
1687 (inquiry)MultiBandMain_clear, /* tp_clear */
1688 0, /* tp_richcompare */
1689 0, /* tp_weaklistoffset */
1690 0, /* tp_iter */
1691 0, /* tp_iternext */
1692 MultiBandMain_methods, /* tp_methods */
1693 MultiBandMain_members, /* tp_members */
1694 0, /* tp_getset */
1695 0, /* tp_base */
1696 0, /* tp_dict */
1697 0, /* tp_descr_get */
1698 0, /* tp_descr_set */
1699 0, /* tp_dictoffset */
1700 0, /* tp_init */
1701 0, /* tp_alloc */
1702 MultiBandMain_new, /* tp_new */
1703 };
1704
1705 /************************************************************************************************/
1706 /* MultiBand streamer object */
1707 /************************************************************************************************/
1708 typedef struct
1709 {
1710 pyo_audio_HEAD
1711 MultiBandMain *mainSplitter;
1712 int modebuffer[2];
1713 int chnl;
1714 } MultiBand;
1715
1716 static void MultiBand_postprocessing_ii(MultiBand *self) { POST_PROCESSING_II };
1717 static void MultiBand_postprocessing_ai(MultiBand *self) { POST_PROCESSING_AI };
1718 static void MultiBand_postprocessing_ia(MultiBand *self) { POST_PROCESSING_IA };
1719 static void MultiBand_postprocessing_aa(MultiBand *self) { POST_PROCESSING_AA };
1720 static void MultiBand_postprocessing_ireva(MultiBand *self) { POST_PROCESSING_IREVA };
1721 static void MultiBand_postprocessing_areva(MultiBand *self) { POST_PROCESSING_AREVA };
1722 static void MultiBand_postprocessing_revai(MultiBand *self) { POST_PROCESSING_REVAI };
1723 static void MultiBand_postprocessing_revaa(MultiBand *self) { POST_PROCESSING_REVAA };
1724 static void MultiBand_postprocessing_revareva(MultiBand *self) { POST_PROCESSING_REVAREVA };
1725
1726 static void
1727 MultiBand_setProcMode(MultiBand *self)
1728 {
1729 int muladdmode;
1730 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1731
1732 switch (muladdmode)
1733 {
1734 case 0:
1735 self->muladd_func_ptr = MultiBand_postprocessing_ii;
1736 break;
1737
1738 case 1:
1739 self->muladd_func_ptr = MultiBand_postprocessing_ai;
1740 break;
1741
1742 case 2:
1743 self->muladd_func_ptr = MultiBand_postprocessing_revai;
1744 break;
1745
1746 case 10:
1747 self->muladd_func_ptr = MultiBand_postprocessing_ia;
1748 break;
1749
1750 case 11:
1751 self->muladd_func_ptr = MultiBand_postprocessing_aa;
1752 break;
1753
1754 case 12:
1755 self->muladd_func_ptr = MultiBand_postprocessing_revaa;
1756 break;
1757
1758 case 20:
1759 self->muladd_func_ptr = MultiBand_postprocessing_ireva;
1760 break;
1761
1762 case 21:
1763 self->muladd_func_ptr = MultiBand_postprocessing_areva;
1764 break;
1765
1766 case 22:
1767 self->muladd_func_ptr = MultiBand_postprocessing_revareva;
1768 break;
1769 }
1770 }
1771
1772 static void
1773 MultiBand_compute_next_data_frame(MultiBand *self)
1774 {
1775 int i;
1776 MYFLT *tmp;
1777 int offset = self->chnl * self->bufsize;
1778 tmp = MultiBandMain_getSamplesBuffer((MultiBandMain *)self->mainSplitter);
1779
1780 for (i = 0; i < self->bufsize; i++)
1781 {
1782 self->data[i] = tmp[i + offset];
1783 }
1784
1785 (*self->muladd_func_ptr)(self);
1786 }
1787
1788 static int
1789 MultiBand_traverse(MultiBand *self, visitproc visit, void *arg)
1790 {
1791 pyo_VISIT
1792 Py_VISIT(self->mainSplitter);
1793 return 0;
1794 }
1795
1796 static int
1797 MultiBand_clear(MultiBand *self)
1798 {
1799 pyo_CLEAR
1800 Py_CLEAR(self->mainSplitter);
1801 return 0;
1802 }
1803
1804 static void
1805 MultiBand_dealloc(MultiBand* self)
1806 {
1807 pyo_DEALLOC
1808 MultiBand_clear(self);
1809 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1810 Py_TYPE(self)->tp_free((PyObject*)self);
1811 }
1812
1813 static PyObject *
1814 MultiBand_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1815 {
1816 int i;
1817 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1818 MultiBand *self;
1819 self = (MultiBand *)type->tp_alloc(type, 0);
1820
1821 self->chnl = 0;
1822 self->modebuffer[0] = 0;
1823 self->modebuffer[1] = 0;
1824
1825 INIT_OBJECT_COMMON
1826 Stream_setFunctionPtr(self->stream, MultiBand_compute_next_data_frame);
1827 self->mode_func_ptr = MultiBand_setProcMode;
1828
1829 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1830
1831 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1832 Py_RETURN_NONE;
1833
1834 self->mainSplitter = (MultiBandMain *)maintmp;
1835 Py_INCREF(self->mainSplitter);
1836
1837 if (multmp)
1838 {
1839 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1840 }
1841
1842 if (addtmp)
1843 {
1844 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1845 }
1846
1847 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1848
1849 (*self->mode_func_ptr)(self);
1850
1851 return (PyObject *)self;
1852 }
1853
1854 static PyObject * MultiBand_getServer(MultiBand* self) { GET_SERVER };
1855 static PyObject * MultiBand_getStream(MultiBand* self) { GET_STREAM };
1856 static PyObject * MultiBand_setMul(MultiBand *self, PyObject *arg) { SET_MUL };
1857 static PyObject * MultiBand_setAdd(MultiBand *self, PyObject *arg) { SET_ADD };
1858 static PyObject * MultiBand_setSub(MultiBand *self, PyObject *arg) { SET_SUB };
1859 static PyObject * MultiBand_setDiv(MultiBand *self, PyObject *arg) { SET_DIV };
1860
1861 static PyObject * MultiBand_play(MultiBand *self, PyObject *args, PyObject *kwds) { PLAY };
1862 static PyObject * MultiBand_out(MultiBand *self, PyObject *args, PyObject *kwds) { OUT };
1863 static PyObject * MultiBand_stop(MultiBand *self, PyObject *args, PyObject *kwds) { STOP };
1864
1865 static PyObject * MultiBand_multiply(MultiBand *self, PyObject *arg) { MULTIPLY };
1866 static PyObject * MultiBand_inplace_multiply(MultiBand *self, PyObject *arg) { INPLACE_MULTIPLY };
1867 static PyObject * MultiBand_add(MultiBand *self, PyObject *arg) { ADD };
1868 static PyObject * MultiBand_inplace_add(MultiBand *self, PyObject *arg) { INPLACE_ADD };
1869 static PyObject * MultiBand_sub(MultiBand *self, PyObject *arg) { SUB };
1870 static PyObject * MultiBand_inplace_sub(MultiBand *self, PyObject *arg) { INPLACE_SUB };
1871 static PyObject * MultiBand_div(MultiBand *self, PyObject *arg) { DIV };
1872 static PyObject * MultiBand_inplace_div(MultiBand *self, PyObject *arg) { INPLACE_DIV };
1873
1874 static PyMemberDef MultiBand_members[] =
1875 {
1876 {"server", T_OBJECT_EX, offsetof(MultiBand, server), 0, "Pyo server."},
1877 {"stream", T_OBJECT_EX, offsetof(MultiBand, stream), 0, "Stream object."},
1878 {"mul", T_OBJECT_EX, offsetof(MultiBand, mul), 0, "Mul factor."},
1879 {"add", T_OBJECT_EX, offsetof(MultiBand, add), 0, "Add factor."},
1880 {NULL} /* Sentinel */
1881 };
1882
1883 static PyMethodDef MultiBand_methods[] =
1884 {
1885 {"getServer", (PyCFunction)MultiBand_getServer, METH_NOARGS, "Returns server object."},
1886 {"_getStream", (PyCFunction)MultiBand_getStream, METH_NOARGS, "Returns stream object."},
1887 {"play", (PyCFunction)MultiBand_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1888 {"out", (PyCFunction)MultiBand_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1889 {"stop", (PyCFunction)MultiBand_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1890 {"setMul", (PyCFunction)MultiBand_setMul, METH_O, "Sets MultiBand mul factor."},
1891 {"setAdd", (PyCFunction)MultiBand_setAdd, METH_O, "Sets MultiBand add factor."},
1892 {"setSub", (PyCFunction)MultiBand_setSub, METH_O, "Sets inverse add factor."},
1893 {"setDiv", (PyCFunction)MultiBand_setDiv, METH_O, "Sets inverse mul factor."},
1894 {NULL} /* Sentinel */
1895 };
1896
1897 static PyNumberMethods MultiBand_as_number =
1898 {
1899 (binaryfunc)MultiBand_add, /*nb_add*/
1900 (binaryfunc)MultiBand_sub, /*nb_subtract*/
1901 (binaryfunc)MultiBand_multiply, /*nb_multiply*/
1902 0, /*nb_remainder*/
1903 0, /*nb_divmod*/
1904 0, /*nb_power*/
1905 0, /*nb_neg*/
1906 0, /*nb_pos*/
1907 0, /*(unaryfunc)array_abs,*/
1908 0, /*nb_nonzero*/
1909 0, /*nb_invert*/
1910 0, /*nb_lshift*/
1911 0, /*nb_rshift*/
1912 0, /*nb_and*/
1913 0, /*nb_xor*/
1914 0, /*nb_or*/
1915 0, /*nb_int*/
1916 0, /*nb_long*/
1917 0, /*nb_float*/
1918 (binaryfunc)MultiBand_inplace_add, /*inplace_add*/
1919 (binaryfunc)MultiBand_inplace_sub, /*inplace_subtract*/
1920 (binaryfunc)MultiBand_inplace_multiply, /*inplace_multiply*/
1921 0, /*inplace_remainder*/
1922 0, /*inplace_power*/
1923 0, /*inplace_lshift*/
1924 0, /*inplace_rshift*/
1925 0, /*inplace_and*/
1926 0, /*inplace_xor*/
1927 0, /*inplace_or*/
1928 0, /*nb_floor_divide*/
1929 (binaryfunc)MultiBand_div, /*nb_true_divide*/
1930 0, /*nb_inplace_floor_divide*/
1931 (binaryfunc)MultiBand_inplace_div, /*nb_inplace_true_divide*/
1932 0, /* nb_index */
1933 };
1934
1935 PyTypeObject MultiBandType =
1936 {
1937 PyVarObject_HEAD_INIT(NULL, 0)
1938 "_pyo.MultiBand_base", /*tp_name*/
1939 sizeof(MultiBand), /*tp_basicsize*/
1940 0, /*tp_itemsize*/
1941 (destructor)MultiBand_dealloc, /*tp_dealloc*/
1942 0, /*tp_print*/
1943 0, /*tp_getattr*/
1944 0, /*tp_setattr*/
1945 0, /*tp_as_async (tp_compare in Python 2)*/
1946 0, /*tp_repr*/
1947 &MultiBand_as_number, /*tp_as_number*/
1948 0, /*tp_as_sequence*/
1949 0, /*tp_as_mapping*/
1950 0, /*tp_hash */
1951 0, /*tp_call*/
1952 0, /*tp_str*/
1953 0, /*tp_getattro*/
1954 0, /*tp_setattro*/
1955 0, /*tp_as_buffer*/
1956 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1957 "MultiBand objects. Reads one band from a MultiBandMain process.", /* tp_doc */
1958 (traverseproc)MultiBand_traverse, /* tp_traverse */
1959 (inquiry)MultiBand_clear, /* tp_clear */
1960 0, /* tp_richcompare */
1961 0, /* tp_weaklistoffset */
1962 0, /* tp_iter */
1963 0, /* tp_iternext */
1964 MultiBand_methods, /* tp_methods */
1965 MultiBand_members, /* tp_members */
1966 0, /* tp_getset */
1967 0, /* tp_base */
1968 0, /* tp_dict */
1969 0, /* tp_descr_get */
1970 0, /* tp_descr_set */
1971 0, /* tp_dictoffset */
1972 0, /* tp_init */
1973 0, /* tp_alloc */
1974 MultiBand_new, /* tp_new */
1975 };