1
/*************************************************************************
2
* Copyright 2010 Olivier Belanger *
4
* This file is part of pyo, a python module to help digital signal *
5
* processing script creation. *
7
* pyo is free software: you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation, either version 3 of the License, or *
10
* (at your option) any later version. *
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 General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19
*************************************************************************/
22
#include "structmember.h"
24
#include "pyomodule.h"
25
#include "streammodule.h"
26
#include "servermodule.h"
27
#include "dummymodule.h"
54
MYFLT *buffer_streams;
60
BandSplitter_compute_variables(BandSplitter *self, MYFLT q)
64
for (i=0; i<self->bands; i++) {
65
freq = self->band_freqs[i];
68
else if (freq >= self->halfSr)
71
MYFLT w0 = self->TwoPiOnSr * freq;
73
MYFLT alpha = MYSIN(w0) / (2 * q);
77
self->a0[i] = 1 + alpha;
79
self->a2[i] = 1 - alpha;
84
BandSplitter_setFrequencies(BandSplitter *self)
87
MYFLT frac = 1. / self->bands;
88
for (i=0; i<self->bands; i++) {
89
self->band_freqs[i] = MYPOW(MYPOW(self->max_freq/self->min_freq, frac), i) * self->min_freq;
94
BandSplitter_filters_i(BandSplitter *self) {
97
MYFLT *in = Stream_getData((Stream *)self->input_stream);
99
if (self->init == 1) {
100
for (j=0; j<self->bands; j++) {
101
self->x1[j] = self->x2[j] = self->y1[j] = self->y2[j] = in[0];
106
for (j=0; j<self->bands; j++) {
107
for (i=0; i<self->bufsize; i++) {
108
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];
109
self->y2[j] = self->y1[j];
111
self->x2[j] = self->x1[j];
113
self->buffer_streams[i + j * self->bufsize] = val;
119
BandSplitter_filters_a(BandSplitter *self) {
122
MYFLT *in = Stream_getData((Stream *)self->input_stream);
124
if (self->init == 1) {
125
for (j=0; j<self->bands; j++) {
126
self->x1[j] = self->x2[j] = self->y1[j] = self->y2[j] = in[0];
131
MYFLT *q = Stream_getData((Stream *)self->q_stream);
133
for (i=0; i<self->bufsize; i++) {
134
BandSplitter_compute_variables((BandSplitter *)self, q[i]);
135
for (j=0; j<self->bands; j++) {
136
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];
137
self->y2[j] = self->y1[j];
139
self->x2[j] = self->x1[j];
141
self->buffer_streams[i + j * self->bufsize] = val;
147
BandSplitter_getSamplesBuffer(BandSplitter *self)
149
return (MYFLT *)self->buffer_streams;
153
BandSplitter_setProcMode(BandSplitter *self)
156
procmode = self->modebuffer[0];
160
self->proc_func_ptr = BandSplitter_filters_i;
163
self->proc_func_ptr = BandSplitter_filters_a;
169
BandSplitter_compute_next_data_frame(BandSplitter *self)
171
(*self->proc_func_ptr)(self);
175
BandSplitter_traverse(BandSplitter *self, visitproc visit, void *arg)
178
Py_VISIT(self->input);
179
Py_VISIT(self->input_stream);
184
BandSplitter_clear(BandSplitter *self)
187
Py_CLEAR(self->input);
188
Py_CLEAR(self->input_stream);
193
BandSplitter_dealloc(BandSplitter* self)
196
free(self->band_freqs);
207
free(self->buffer_streams);
208
BandSplitter_clear(self);
209
self->ob_type->tp_free((PyObject*)self);
212
static PyObject * BandSplitter_deleteStream(BandSplitter *self) { DELETE_STREAM };
215
BandSplitter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
219
self = (BandSplitter *)type->tp_alloc(type, 0);
223
self->q = PyFloat_FromDouble(1.);
227
Stream_setFunctionPtr(self->stream, BandSplitter_compute_next_data_frame);
228
self->mode_func_ptr = BandSplitter_setProcMode;
230
self->halfSr = self->sr / 2.;
231
self->TwoPiOnSr = TWOPI / self->sr;
232
return (PyObject *)self;
236
BandSplitter_init(BandSplitter *self, PyObject *args, PyObject *kwds)
238
PyObject *inputtmp, *input_streamtmp, *qtmp=NULL;
240
static char *kwlist[] = {"input", "bands", "min", "max", "q", NULL};
242
if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_IFFO, kwlist, &inputtmp, &self->bands, &self->min_freq, &self->max_freq, &qtmp))
247
Py_INCREF(self->stream);
248
PyObject_CallMethod(self->server, "addStream", "O", self->stream);
250
self->band_freqs = (MYFLT *)realloc(self->band_freqs, self->bands * sizeof(MYFLT));
252
self->x1 = (MYFLT *)realloc(self->x1, self->bands * sizeof(MYFLT));
253
self->x2 = (MYFLT *)realloc(self->x2, self->bands * sizeof(MYFLT));
254
self->y1 = (MYFLT *)realloc(self->y1, self->bands * sizeof(MYFLT));
255
self->y2 = (MYFLT *)realloc(self->y2, self->bands * sizeof(MYFLT));
257
self->b0 = (MYFLT *)realloc(self->b0, self->bands * sizeof(MYFLT));
258
self->b1 = (MYFLT *)realloc(self->b1, self->bands * sizeof(MYFLT));
259
self->b2 = (MYFLT *)realloc(self->b2, self->bands * sizeof(MYFLT));
260
self->a0 = (MYFLT *)realloc(self->a0, self->bands * sizeof(MYFLT));
261
self->a1 = (MYFLT *)realloc(self->a1, self->bands * sizeof(MYFLT));
262
self->a2 = (MYFLT *)realloc(self->a2, self->bands * sizeof(MYFLT));
264
self->buffer_streams = (MYFLT *)realloc(self->buffer_streams, self->bands * self->bufsize * sizeof(MYFLT));
266
BandSplitter_setFrequencies((BandSplitter *)self);
269
PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
272
BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
275
(*self->mode_func_ptr)(self);
282
BandSplitter_setQ(BandSplitter *self, PyObject *arg)
284
PyObject *tmp, *streamtmp;
291
int isNumber = PyNumber_Check(arg);
297
self->q = PyNumber_Float(tmp);
298
self->modebuffer[0] = 0;
299
BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
303
streamtmp = PyObject_CallMethod((PyObject *)self->q, "_getStream", NULL);
304
Py_INCREF(streamtmp);
305
Py_XDECREF(self->q_stream);
306
self->q_stream = (Stream *)streamtmp;
307
self->modebuffer[0] = 1;
310
(*self->mode_func_ptr)(self);
316
static PyObject * BandSplitter_getServer(BandSplitter* self) { GET_SERVER };
317
static PyObject * BandSplitter_getStream(BandSplitter* self) { GET_STREAM };
319
static PyObject * BandSplitter_play(BandSplitter *self, PyObject *args, PyObject *kwds) { PLAY };
320
static PyObject * BandSplitter_stop(BandSplitter *self) { STOP };
322
static PyMemberDef BandSplitter_members[] = {
323
{"server", T_OBJECT_EX, offsetof(BandSplitter, server), 0, "Pyo server."},
324
{"stream", T_OBJECT_EX, offsetof(BandSplitter, stream), 0, "Stream object."},
325
{"input", T_OBJECT_EX, offsetof(BandSplitter, input), 0, "Input sound object."},
326
{"q", T_OBJECT_EX, offsetof(BandSplitter, q), 0, "Filters Q."},
327
{NULL} /* Sentinel */
330
static PyMethodDef BandSplitter_methods[] = {
331
{"getServer", (PyCFunction)BandSplitter_getServer, METH_NOARGS, "Returns server object."},
332
{"_getStream", (PyCFunction)BandSplitter_getStream, METH_NOARGS, "Returns stream object."},
333
{"deleteStream", (PyCFunction)BandSplitter_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
334
{"setQ", (PyCFunction)BandSplitter_setQ, METH_O, "Sets the filters Q."},
335
{"play", (PyCFunction)BandSplitter_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
336
{"stop", (PyCFunction)BandSplitter_stop, METH_NOARGS, "Stops computing."},
337
{NULL} /* Sentinel */
340
PyTypeObject BandSplitterType = {
341
PyObject_HEAD_INIT(NULL)
343
"_pyo.BandSplitter_base", /*tp_name*/
344
sizeof(BandSplitter), /*tp_basicsize*/
346
(destructor)BandSplitter_dealloc, /*tp_dealloc*/
353
0, /*tp_as_sequence*/
361
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
362
"BandSplitter objects. Split audio stream in multiple frequency bands.", /* tp_doc */
363
(traverseproc)BandSplitter_traverse, /* tp_traverse */
364
(inquiry)BandSplitter_clear, /* tp_clear */
365
0, /* tp_richcompare */
366
0, /* tp_weaklistoffset */
369
BandSplitter_methods, /* tp_methods */
370
BandSplitter_members, /* tp_members */
374
0, /* tp_descr_get */
375
0, /* tp_descr_set */
376
0, /* tp_dictoffset */
377
(initproc)BandSplitter_init, /* tp_init */
379
BandSplitter_new, /* tp_new */
382
/************************************************************************************************/
383
/* BandSplit streamer object */
384
/************************************************************************************************/
387
BandSplitter *mainSplitter;
392
static void BandSplit_postprocessing_ii(BandSplit *self) { POST_PROCESSING_II };
393
static void BandSplit_postprocessing_ai(BandSplit *self) { POST_PROCESSING_AI };
394
static void BandSplit_postprocessing_ia(BandSplit *self) { POST_PROCESSING_IA };
395
static void BandSplit_postprocessing_aa(BandSplit *self) { POST_PROCESSING_AA };
396
static void BandSplit_postprocessing_ireva(BandSplit *self) { POST_PROCESSING_IREVA };
397
static void BandSplit_postprocessing_areva(BandSplit *self) { POST_PROCESSING_AREVA };
398
static void BandSplit_postprocessing_revai(BandSplit *self) { POST_PROCESSING_REVAI };
399
static void BandSplit_postprocessing_revaa(BandSplit *self) { POST_PROCESSING_REVAA };
400
static void BandSplit_postprocessing_revareva(BandSplit *self) { POST_PROCESSING_REVAREVA };
403
BandSplit_setProcMode(BandSplit *self)
406
muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
408
switch (muladdmode) {
410
self->muladd_func_ptr = BandSplit_postprocessing_ii;
413
self->muladd_func_ptr = BandSplit_postprocessing_ai;
416
self->muladd_func_ptr = BandSplit_postprocessing_revai;
419
self->muladd_func_ptr = BandSplit_postprocessing_ia;
422
self->muladd_func_ptr = BandSplit_postprocessing_aa;
425
self->muladd_func_ptr = BandSplit_postprocessing_revaa;
428
self->muladd_func_ptr = BandSplit_postprocessing_ireva;
431
self->muladd_func_ptr = BandSplit_postprocessing_areva;
434
self->muladd_func_ptr = BandSplit_postprocessing_revareva;
440
BandSplit_compute_next_data_frame(BandSplit *self)
444
int offset = self->chnl * self->bufsize;
445
tmp = BandSplitter_getSamplesBuffer((BandSplitter *)self->mainSplitter);
446
for (i=0; i<self->bufsize; i++) {
447
self->data[i] = tmp[i + offset];
449
(*self->muladd_func_ptr)(self);
453
BandSplit_traverse(BandSplit *self, visitproc visit, void *arg)
456
Py_VISIT(self->mainSplitter);
461
BandSplit_clear(BandSplit *self)
464
Py_CLEAR(self->mainSplitter);
469
BandSplit_dealloc(BandSplit* self)
472
BandSplit_clear(self);
473
self->ob_type->tp_free((PyObject*)self);
476
static PyObject * BandSplit_deleteStream(BandSplit *self) { DELETE_STREAM };
479
BandSplit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
483
self = (BandSplit *)type->tp_alloc(type, 0);
486
self->modebuffer[0] = 0;
487
self->modebuffer[1] = 0;
490
Stream_setFunctionPtr(self->stream, BandSplit_compute_next_data_frame);
491
self->mode_func_ptr = BandSplit_setProcMode;
493
return (PyObject *)self;
497
BandSplit_init(BandSplit *self, PyObject *args, PyObject *kwds)
499
PyObject *maintmp=NULL, *multmp=NULL, *addtmp=NULL;
501
static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
503
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
506
Py_XDECREF(self->mainSplitter);
508
self->mainSplitter = (BandSplitter *)maintmp;
511
PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
515
PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
518
Py_INCREF(self->stream);
519
PyObject_CallMethod(self->server, "addStream", "O", self->stream);
521
(*self->mode_func_ptr)(self);
527
static PyObject * BandSplit_getServer(BandSplit* self) { GET_SERVER };
528
static PyObject * BandSplit_getStream(BandSplit* self) { GET_STREAM };
529
static PyObject * BandSplit_setMul(BandSplit *self, PyObject *arg) { SET_MUL };
530
static PyObject * BandSplit_setAdd(BandSplit *self, PyObject *arg) { SET_ADD };
531
static PyObject * BandSplit_setSub(BandSplit *self, PyObject *arg) { SET_SUB };
532
static PyObject * BandSplit_setDiv(BandSplit *self, PyObject *arg) { SET_DIV };
534
static PyObject * BandSplit_play(BandSplit *self, PyObject *args, PyObject *kwds) { PLAY };
535
static PyObject * BandSplit_out(BandSplit *self, PyObject *args, PyObject *kwds) { OUT };
536
static PyObject * BandSplit_stop(BandSplit *self) { STOP };
538
static PyObject * BandSplit_multiply(BandSplit *self, PyObject *arg) { MULTIPLY };
539
static PyObject * BandSplit_inplace_multiply(BandSplit *self, PyObject *arg) { INPLACE_MULTIPLY };
540
static PyObject * BandSplit_add(BandSplit *self, PyObject *arg) { ADD };
541
static PyObject * BandSplit_inplace_add(BandSplit *self, PyObject *arg) { INPLACE_ADD };
542
static PyObject * BandSplit_sub(BandSplit *self, PyObject *arg) { SUB };
543
static PyObject * BandSplit_inplace_sub(BandSplit *self, PyObject *arg) { INPLACE_SUB };
544
static PyObject * BandSplit_div(BandSplit *self, PyObject *arg) { DIV };
545
static PyObject * BandSplit_inplace_div(BandSplit *self, PyObject *arg) { INPLACE_DIV };
547
static PyMemberDef BandSplit_members[] = {
548
{"server", T_OBJECT_EX, offsetof(BandSplit, server), 0, "Pyo server."},
549
{"stream", T_OBJECT_EX, offsetof(BandSplit, stream), 0, "Stream object."},
550
{"mul", T_OBJECT_EX, offsetof(BandSplit, mul), 0, "Mul factor."},
551
{"add", T_OBJECT_EX, offsetof(BandSplit, add), 0, "Add factor."},
552
{NULL} /* Sentinel */
555
static PyMethodDef BandSplit_methods[] = {
556
{"getServer", (PyCFunction)BandSplit_getServer, METH_NOARGS, "Returns server object."},
557
{"_getStream", (PyCFunction)BandSplit_getStream, METH_NOARGS, "Returns stream object."},
558
{"deleteStream", (PyCFunction)BandSplit_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
559
{"play", (PyCFunction)BandSplit_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
560
{"out", (PyCFunction)BandSplit_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
561
{"stop", (PyCFunction)BandSplit_stop, METH_NOARGS, "Stops computing."},
562
{"setMul", (PyCFunction)BandSplit_setMul, METH_O, "Sets BandSplit mul factor."},
563
{"setAdd", (PyCFunction)BandSplit_setAdd, METH_O, "Sets BandSplit add factor."},
564
{"setSub", (PyCFunction)BandSplit_setSub, METH_O, "Sets inverse add factor."},
565
{"setDiv", (PyCFunction)BandSplit_setDiv, METH_O, "Sets inverse mul factor."},
566
{NULL} /* Sentinel */
569
static PyNumberMethods BandSplit_as_number = {
570
(binaryfunc)BandSplit_add, /*nb_add*/
571
(binaryfunc)BandSplit_sub, /*nb_subtract*/
572
(binaryfunc)BandSplit_multiply, /*nb_multiply*/
573
(binaryfunc)BandSplit_div, /*nb_divide*/
579
0, /*(unaryfunc)array_abs,*/
593
(binaryfunc)BandSplit_inplace_add, /*inplace_add*/
594
(binaryfunc)BandSplit_inplace_sub, /*inplace_subtract*/
595
(binaryfunc)BandSplit_inplace_multiply, /*inplace_multiply*/
596
(binaryfunc)BandSplit_inplace_div, /*inplace_divide*/
597
0, /*inplace_remainder*/
599
0, /*inplace_lshift*/
600
0, /*inplace_rshift*/
604
0, /*nb_floor_divide*/
605
0, /*nb_true_divide*/
606
0, /*nb_inplace_floor_divide*/
607
0, /*nb_inplace_true_divide*/
611
PyTypeObject BandSplitType = {
612
PyObject_HEAD_INIT(NULL)
614
"_pyo.BandSplit_base", /*tp_name*/
615
sizeof(BandSplit), /*tp_basicsize*/
617
(destructor)BandSplit_dealloc, /*tp_dealloc*/
623
&BandSplit_as_number, /*tp_as_number*/
624
0, /*tp_as_sequence*/
632
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
633
"BandSplit objects. Reads one band from a BandSplitter process.", /* tp_doc */
634
(traverseproc)BandSplit_traverse, /* tp_traverse */
635
(inquiry)BandSplit_clear, /* tp_clear */
636
0, /* tp_richcompare */
637
0, /* tp_weaklistoffset */
640
BandSplit_methods, /* tp_methods */
641
BandSplit_members, /* tp_members */
645
0, /* tp_descr_get */
646
0, /* tp_descr_set */
647
0, /* tp_dictoffset */
648
(initproc)BandSplit_init, /* tp_init */
650
BandSplit_new, /* tp_new */
659
Stream *input_stream;
661
Stream *freq1_stream;
663
Stream *freq2_stream;
665
Stream *freq3_stream;
689
MYFLT *buffer_streams;
695
FourBandMain_compute_variables(FourBandMain *self, double freq, int band)
697
double wc = TWOPI * freq;
698
double wc2 = wc * wc;
699
double wc3 = wc2 * wc;
700
double wc4 = wc2 * wc2;
701
double k = wc / tan(PI * freq / self->sr);
705
double sqrt2 = sqrt(2.0);
706
double sq_tmp1 = sqrt2 * wc3 * k;
707
double sq_tmp2 = sqrt2 * wc * k3;
708
double a_tmp = 4.0 * wc2 * k2 + 2.0 * sq_tmp1 + k4 + 2.0 * sq_tmp2 + wc4;
709
double wc4_a_tmp = wc4 / a_tmp;
710
double k4_a_tmp = k4 / a_tmp;
713
self->b1[band] = (4.0 * (wc4 + sq_tmp1 - k4 - sq_tmp2)) / a_tmp;
714
self->b2[band] = (6.0 * wc4 - 8.0 * wc2 * k2 + 6.0 * k4) / a_tmp;
715
self->b3[band] = (4.0 * (wc4 - sq_tmp1 + sq_tmp2 - k4)) / a_tmp;
716
self->b4[band] = (k4 - 2.0 * sq_tmp1 + wc4 - 2.0 * sq_tmp2 + 4.0 * wc2 * k2) / a_tmp;
719
self->la0[band] = wc4_a_tmp;
720
self->la1[band] = 4.0 * wc4_a_tmp;
721
self->la2[band] = 6.0 * wc4_a_tmp;
724
self->ha0[band] = k4_a_tmp;
725
self->ha1[band] = -4.0 * k4_a_tmp;
726
self->ha2[band] = 6.0 * k4_a_tmp;
730
FourBandMain_filters(FourBandMain *self) {
731
double val, inval, tmp, f1, f2, f3;
732
int i, j, j1, ind, ind1;
734
MYFLT *in = Stream_getData((Stream *)self->input_stream);
736
if (self->modebuffer[0] == 0)
737
f1 = PyFloat_AS_DOUBLE(self->freq1);
739
f1 = (double)Stream_getData((Stream *)self->freq1_stream)[0];
740
if (self->modebuffer[1] == 0)
741
f2 = PyFloat_AS_DOUBLE(self->freq2);
743
f2 = (double)Stream_getData((Stream *)self->freq2_stream)[0];
744
if (self->modebuffer[2] == 0)
745
f3 = PyFloat_AS_DOUBLE(self->freq3);
747
f3 = (double)Stream_getData((Stream *)self->freq3_stream)[0];
749
if (f1 != self->last_freq1) {
750
self->last_freq1 = f1;
751
FourBandMain_compute_variables(self, f1, 0);
754
if (f2 != self->last_freq2) {
755
self->last_freq2 = f2;
756
FourBandMain_compute_variables(self, f2, 1);
759
if (f3 != self->last_freq3) {
760
self->last_freq3 = f3;
761
FourBandMain_compute_variables(self, f3, 2);
765
for (i=0; i<self->bufsize; i++) {
766
inval = (double)in[i];
768
val = self->la0[0] * inval + self->la1[0] * self->x1[0] + self->la2[0] * self->x2[0] + self->la1[0] * self->x3[0] + self->la0[0] * self->x4[0] -
769
self->b1[0] * self->y1[0] - self->b2[0] * self->y2[0] - self->b3[0] * self->y3[0] - self->b4[0] * self->y4[0];
770
self->y4[0] = self->y3[0];
771
self->y3[0] = self->y2[0];
772
self->y2[0] = self->y1[0];
774
self->x4[0] = self->x3[0];
775
self->x3[0] = self->x2[0];
776
self->x2[0] = self->x1[0];
778
self->buffer_streams[i] = (MYFLT)val;
780
/* Second and third bands */
781
for (j=0; j<2; j++) {
785
tmp = self->ha0[j] * inval + self->ha1[j] * self->x1[ind] + self->ha2[j] * self->x2[ind] + self->ha1[j] * self->x3[ind] + self->ha0[j] * self->x4[ind] -
786
self->b1[j] * self->y1[ind] - self->b2[j] * self->y2[ind] - self->b3[j] * self->y3[ind] - self->b4[j] * self->y4[ind];
787
self->y4[ind] = self->y3[ind];
788
self->y3[ind] = self->y2[ind];
789
self->y2[ind] = self->y1[ind];
791
self->x4[ind] = self->x3[ind];
792
self->x3[ind] = self->x2[ind];
793
self->x2[ind] = self->x1[ind];
794
self->x1[ind] = inval;
796
val = self->la0[j1] * tmp + self->la1[j1] * self->x1[ind1] + self->la2[j1] * self->x2[ind1] + self->la1[j1] * self->x3[ind1] + self->la0[j1] * self->x4[ind1] -
797
self->b1[j1] * self->y1[ind1] - self->b2[j1] * self->y2[ind1] - self->b3[j1] * self->y3[ind1] - self->b4[j1] * self->y4[ind1];
798
self->y4[ind1] = self->y3[ind1];
799
self->y3[ind1] = self->y2[ind1];
800
self->y2[ind1] = self->y1[ind1];
801
self->y1[ind1] = val;
802
self->x4[ind1] = self->x3[ind1];
803
self->x3[ind1] = self->x2[ind1];
804
self->x2[ind1] = self->x1[ind1];
805
self->x1[ind1] = tmp;
807
self->buffer_streams[i + j1 * self->bufsize] = (MYFLT)val;
810
val = self->ha0[2] * inval + self->ha1[2] * self->x1[5] + self->ha2[2] * self->x2[5] + self->ha1[2] * self->x3[5] + self->ha0[2] * self->x4[5] -
811
self->b1[2] * self->y1[5] - self->b2[2] * self->y2[5] - self->b3[2] * self->y3[5] - self->b4[2] * self->y4[5];
812
self->y4[5] = self->y3[5];
813
self->y3[5] = self->y2[5];
814
self->y2[5] = self->y1[5];
816
self->x4[5] = self->x3[5];
817
self->x3[5] = self->x2[5];
818
self->x2[5] = self->x1[5];
820
self->buffer_streams[i + 3 * self->bufsize] = (MYFLT)val;
825
FourBandMain_getSamplesBuffer(FourBandMain *self)
827
return (MYFLT *)self->buffer_streams;
831
FourBandMain_setProcMode(FourBandMain *self)
833
self->proc_func_ptr = FourBandMain_filters;
837
FourBandMain_compute_next_data_frame(FourBandMain *self)
839
(*self->proc_func_ptr)(self);
843
FourBandMain_traverse(FourBandMain *self, visitproc visit, void *arg)
846
Py_VISIT(self->input);
847
Py_VISIT(self->input_stream);
848
Py_VISIT(self->freq1);
849
Py_VISIT(self->freq1_stream);
850
Py_VISIT(self->freq2);
851
Py_VISIT(self->freq2_stream);
852
Py_VISIT(self->freq3);
853
Py_VISIT(self->freq3_stream);
858
FourBandMain_clear(FourBandMain *self)
861
Py_CLEAR(self->input);
862
Py_CLEAR(self->input_stream);
863
Py_CLEAR(self->freq1);
864
Py_CLEAR(self->freq1_stream);
865
Py_CLEAR(self->freq2);
866
Py_CLEAR(self->freq2_stream);
867
Py_CLEAR(self->freq3);
868
Py_CLEAR(self->freq3_stream);
873
FourBandMain_dealloc(FourBandMain* self)
876
free(self->buffer_streams);
877
FourBandMain_clear(self);
878
self->ob_type->tp_free((PyObject*)self);
881
static PyObject * FourBandMain_deleteStream(FourBandMain *self) { DELETE_STREAM };
884
FourBandMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
888
self = (FourBandMain *)type->tp_alloc(type, 0);
890
self->freq1 = PyFloat_FromDouble(150);
891
self->freq2 = PyFloat_FromDouble(500);
892
self->freq3 = PyFloat_FromDouble(2000);
893
self->last_freq1 = self->last_freq2 = self->last_freq3 = -1.0;
895
self->modebuffer[0] = 0;
896
self->modebuffer[1] = 0;
897
self->modebuffer[2] = 0;
900
Stream_setFunctionPtr(self->stream, FourBandMain_compute_next_data_frame);
901
self->mode_func_ptr = FourBandMain_setProcMode;
903
return (PyObject *)self;
907
FourBandMain_init(FourBandMain *self, PyObject *args, PyObject *kwds)
910
PyObject *inputtmp, *input_streamtmp, *freq1tmp=NULL, *freq2tmp=NULL, *freq3tmp=NULL;
912
static char *kwlist[] = {"input", "freq1", "freq2", "freq3", NULL};
914
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freq1tmp, &freq2tmp, &freq3tmp))
919
Py_INCREF(self->stream);
920
PyObject_CallMethod(self->server, "addStream", "O", self->stream);
922
for (i=0; i<6; i++) {
923
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;
926
self->buffer_streams = (MYFLT *)realloc(self->buffer_streams, 4 * self->bufsize * sizeof(MYFLT));
928
for (i=0; i<(4 * self->bufsize); i++) {
929
self->buffer_streams[i] = 0.0;
933
PyObject_CallMethod((PyObject *)self, "setFreq1", "O", freq1tmp);
937
PyObject_CallMethod((PyObject *)self, "setFreq2", "O", freq2tmp);
941
PyObject_CallMethod((PyObject *)self, "setFreq3", "O", freq3tmp);
944
(*self->mode_func_ptr)(self);
951
FourBandMain_setFreq1(FourBandMain *self, PyObject *arg)
953
PyObject *tmp, *streamtmp;
960
int isNumber = PyNumber_Check(arg);
964
Py_DECREF(self->freq1);
966
self->freq1 = PyNumber_Float(tmp);
967
self->modebuffer[0] = 0;
971
streamtmp = PyObject_CallMethod((PyObject *)self->freq1, "_getStream", NULL);
972
Py_INCREF(streamtmp);
973
Py_XDECREF(self->freq1_stream);
974
self->freq1_stream = (Stream *)streamtmp;
975
self->modebuffer[0] = 1;
983
FourBandMain_setFreq2(FourBandMain *self, PyObject *arg)
985
PyObject *tmp, *streamtmp;
992
int isNumber = PyNumber_Check(arg);
996
Py_DECREF(self->freq2);
998
self->freq2 = PyNumber_Float(tmp);
999
self->modebuffer[1] = 0;
1003
streamtmp = PyObject_CallMethod((PyObject *)self->freq2, "_getStream", NULL);
1004
Py_INCREF(streamtmp);
1005
Py_XDECREF(self->freq2_stream);
1006
self->freq2_stream = (Stream *)streamtmp;
1007
self->modebuffer[1] = 1;
1015
FourBandMain_setFreq3(FourBandMain *self, PyObject *arg)
1017
PyObject *tmp, *streamtmp;
1024
int isNumber = PyNumber_Check(arg);
1028
Py_DECREF(self->freq3);
1029
if (isNumber == 1) {
1030
self->freq3 = PyNumber_Float(tmp);
1031
self->modebuffer[2] = 0;
1035
streamtmp = PyObject_CallMethod((PyObject *)self->freq3, "_getStream", NULL);
1036
Py_INCREF(streamtmp);
1037
Py_XDECREF(self->freq3_stream);
1038
self->freq3_stream = (Stream *)streamtmp;
1039
self->modebuffer[2] = 1;
1046
static PyObject * FourBandMain_getServer(FourBandMain* self) { GET_SERVER };
1047
static PyObject * FourBandMain_getStream(FourBandMain* self) { GET_STREAM };
1049
static PyObject * FourBandMain_play(FourBandMain *self, PyObject *args, PyObject *kwds) { PLAY };
1050
static PyObject * FourBandMain_stop(FourBandMain *self) { STOP };
1052
static PyMemberDef FourBandMain_members[] = {
1053
{"server", T_OBJECT_EX, offsetof(FourBandMain, server), 0, "Pyo server."},
1054
{"stream", T_OBJECT_EX, offsetof(FourBandMain, stream), 0, "Stream object."},
1055
{"input", T_OBJECT_EX, offsetof(FourBandMain, input), 0, "Input sound object."},
1056
{"freq1", T_OBJECT_EX, offsetof(FourBandMain, freq1), 0, "First cutoff frequency."},
1057
{"freq2", T_OBJECT_EX, offsetof(FourBandMain, freq2), 0, "Second cutoff frequency."},
1058
{"freq3", T_OBJECT_EX, offsetof(FourBandMain, freq3), 0, "Third cutoff frequency."},
1059
{NULL} /* Sentinel */
1062
static PyMethodDef FourBandMain_methods[] = {
1063
{"getServer", (PyCFunction)FourBandMain_getServer, METH_NOARGS, "Returns server object."},
1064
{"_getStream", (PyCFunction)FourBandMain_getStream, METH_NOARGS, "Returns stream object."},
1065
{"deleteStream", (PyCFunction)FourBandMain_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
1066
{"setFreq1", (PyCFunction)FourBandMain_setFreq1, METH_O, "Sets the first cutoff frequency."},
1067
{"setFreq2", (PyCFunction)FourBandMain_setFreq2, METH_O, "Sets the second cutoff frequency."},
1068
{"setFreq3", (PyCFunction)FourBandMain_setFreq3, METH_O, "Sets the third cutoff frequency."},
1069
{"play", (PyCFunction)FourBandMain_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1070
{"stop", (PyCFunction)FourBandMain_stop, METH_NOARGS, "Stops computing."},
1071
{NULL} /* Sentinel */
1074
PyTypeObject FourBandMainType = {
1075
PyObject_HEAD_INIT(NULL)
1077
"_pyo.FourBandMain_base", /*tp_name*/
1078
sizeof(FourBandMain), /*tp_basicsize*/
1080
(destructor)FourBandMain_dealloc, /*tp_dealloc*/
1087
0, /*tp_as_sequence*/
1088
0, /*tp_as_mapping*/
1095
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
1096
"FourBandMain objects. Split audio stream in four flat frequency and phase bands.", /* tp_doc */
1097
(traverseproc)FourBandMain_traverse, /* tp_traverse */
1098
(inquiry)FourBandMain_clear, /* tp_clear */
1099
0, /* tp_richcompare */
1100
0, /* tp_weaklistoffset */
1102
0, /* tp_iternext */
1103
FourBandMain_methods, /* tp_methods */
1104
FourBandMain_members, /* tp_members */
1108
0, /* tp_descr_get */
1109
0, /* tp_descr_set */
1110
0, /* tp_dictoffset */
1111
(initproc)FourBandMain_init, /* tp_init */
1113
FourBandMain_new, /* tp_new */
1116
/************************************************************************************************/
1117
/* FourBand streamer object */
1118
/************************************************************************************************/
1121
FourBandMain *mainSplitter;
1126
static void FourBand_postprocessing_ii(FourBand *self) { POST_PROCESSING_II };
1127
static void FourBand_postprocessing_ai(FourBand *self) { POST_PROCESSING_AI };
1128
static void FourBand_postprocessing_ia(FourBand *self) { POST_PROCESSING_IA };
1129
static void FourBand_postprocessing_aa(FourBand *self) { POST_PROCESSING_AA };
1130
static void FourBand_postprocessing_ireva(FourBand *self) { POST_PROCESSING_IREVA };
1131
static void FourBand_postprocessing_areva(FourBand *self) { POST_PROCESSING_AREVA };
1132
static void FourBand_postprocessing_revai(FourBand *self) { POST_PROCESSING_REVAI };
1133
static void FourBand_postprocessing_revaa(FourBand *self) { POST_PROCESSING_REVAA };
1134
static void FourBand_postprocessing_revareva(FourBand *self) { POST_PROCESSING_REVAREVA };
1137
FourBand_setProcMode(FourBand *self)
1140
muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1142
switch (muladdmode) {
1144
self->muladd_func_ptr = FourBand_postprocessing_ii;
1147
self->muladd_func_ptr = FourBand_postprocessing_ai;
1150
self->muladd_func_ptr = FourBand_postprocessing_revai;
1153
self->muladd_func_ptr = FourBand_postprocessing_ia;
1156
self->muladd_func_ptr = FourBand_postprocessing_aa;
1159
self->muladd_func_ptr = FourBand_postprocessing_revaa;
1162
self->muladd_func_ptr = FourBand_postprocessing_ireva;
1165
self->muladd_func_ptr = FourBand_postprocessing_areva;
1168
self->muladd_func_ptr = FourBand_postprocessing_revareva;
1174
FourBand_compute_next_data_frame(FourBand *self)
1178
int offset = self->chnl * self->bufsize;
1179
tmp = FourBandMain_getSamplesBuffer((FourBandMain *)self->mainSplitter);
1180
for (i=0; i<self->bufsize; i++) {
1181
self->data[i] = tmp[i + offset];
1183
(*self->muladd_func_ptr)(self);
1187
FourBand_traverse(FourBand *self, visitproc visit, void *arg)
1190
Py_VISIT(self->mainSplitter);
1195
FourBand_clear(FourBand *self)
1198
Py_CLEAR(self->mainSplitter);
1203
FourBand_dealloc(FourBand* self)
1206
FourBand_clear(self);
1207
self->ob_type->tp_free((PyObject*)self);
1210
static PyObject * FourBand_deleteStream(FourBand *self) { DELETE_STREAM };
1213
FourBand_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1217
self = (FourBand *)type->tp_alloc(type, 0);
1220
self->modebuffer[0] = 0;
1221
self->modebuffer[1] = 0;
1224
Stream_setFunctionPtr(self->stream, FourBand_compute_next_data_frame);
1225
self->mode_func_ptr = FourBand_setProcMode;
1227
return (PyObject *)self;
1231
FourBand_init(FourBand *self, PyObject *args, PyObject *kwds)
1233
PyObject *maintmp=NULL, *multmp=NULL, *addtmp=NULL;
1235
static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1237
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1240
Py_XDECREF(self->mainSplitter);
1242
self->mainSplitter = (FourBandMain *)maintmp;
1245
PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1249
PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1252
Py_INCREF(self->stream);
1253
PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1255
(*self->mode_func_ptr)(self);
1261
static PyObject * FourBand_getServer(FourBand* self) { GET_SERVER };
1262
static PyObject * FourBand_getStream(FourBand* self) { GET_STREAM };
1263
static PyObject * FourBand_setMul(FourBand *self, PyObject *arg) { SET_MUL };
1264
static PyObject * FourBand_setAdd(FourBand *self, PyObject *arg) { SET_ADD };
1265
static PyObject * FourBand_setSub(FourBand *self, PyObject *arg) { SET_SUB };
1266
static PyObject * FourBand_setDiv(FourBand *self, PyObject *arg) { SET_DIV };
1268
static PyObject * FourBand_play(FourBand *self, PyObject *args, PyObject *kwds) { PLAY };
1269
static PyObject * FourBand_out(FourBand *self, PyObject *args, PyObject *kwds) { OUT };
1270
static PyObject * FourBand_stop(FourBand *self) { STOP };
1272
static PyObject * FourBand_multiply(FourBand *self, PyObject *arg) { MULTIPLY };
1273
static PyObject * FourBand_inplace_multiply(FourBand *self, PyObject *arg) { INPLACE_MULTIPLY };
1274
static PyObject * FourBand_add(FourBand *self, PyObject *arg) { ADD };
1275
static PyObject * FourBand_inplace_add(FourBand *self, PyObject *arg) { INPLACE_ADD };
1276
static PyObject * FourBand_sub(FourBand *self, PyObject *arg) { SUB };
1277
static PyObject * FourBand_inplace_sub(FourBand *self, PyObject *arg) { INPLACE_SUB };
1278
static PyObject * FourBand_div(FourBand *self, PyObject *arg) { DIV };
1279
static PyObject * FourBand_inplace_div(FourBand *self, PyObject *arg) { INPLACE_DIV };
1281
static PyMemberDef FourBand_members[] = {
1282
{"server", T_OBJECT_EX, offsetof(FourBand, server), 0, "Pyo server."},
1283
{"stream", T_OBJECT_EX, offsetof(FourBand, stream), 0, "Stream object."},
1284
{"mul", T_OBJECT_EX, offsetof(FourBand, mul), 0, "Mul factor."},
1285
{"add", T_OBJECT_EX, offsetof(FourBand, add), 0, "Add factor."},
1286
{NULL} /* Sentinel */
1289
static PyMethodDef FourBand_methods[] = {
1290
{"getServer", (PyCFunction)FourBand_getServer, METH_NOARGS, "Returns server object."},
1291
{"_getStream", (PyCFunction)FourBand_getStream, METH_NOARGS, "Returns stream object."},
1292
{"deleteStream", (PyCFunction)FourBand_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
1293
{"play", (PyCFunction)FourBand_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1294
{"out", (PyCFunction)FourBand_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1295
{"stop", (PyCFunction)FourBand_stop, METH_NOARGS, "Stops computing."},
1296
{"setMul", (PyCFunction)FourBand_setMul, METH_O, "Sets FourBand mul factor."},
1297
{"setAdd", (PyCFunction)FourBand_setAdd, METH_O, "Sets FourBand add factor."},
1298
{"setSub", (PyCFunction)FourBand_setSub, METH_O, "Sets inverse add factor."},
1299
{"setDiv", (PyCFunction)FourBand_setDiv, METH_O, "Sets inverse mul factor."},
1300
{NULL} /* Sentinel */
1303
static PyNumberMethods FourBand_as_number = {
1304
(binaryfunc)FourBand_add, /*nb_add*/
1305
(binaryfunc)FourBand_sub, /*nb_subtract*/
1306
(binaryfunc)FourBand_multiply, /*nb_multiply*/
1307
(binaryfunc)FourBand_div, /*nb_divide*/
1313
0, /*(unaryfunc)array_abs,*/
1327
(binaryfunc)FourBand_inplace_add, /*inplace_add*/
1328
(binaryfunc)FourBand_inplace_sub, /*inplace_subtract*/
1329
(binaryfunc)FourBand_inplace_multiply, /*inplace_multiply*/
1330
(binaryfunc)FourBand_inplace_div, /*inplace_divide*/
1331
0, /*inplace_remainder*/
1332
0, /*inplace_power*/
1333
0, /*inplace_lshift*/
1334
0, /*inplace_rshift*/
1338
0, /*nb_floor_divide*/
1339
0, /*nb_true_divide*/
1340
0, /*nb_inplace_floor_divide*/
1341
0, /*nb_inplace_true_divide*/
1345
PyTypeObject FourBandType = {
1346
PyObject_HEAD_INIT(NULL)
1348
"_pyo.FourBand_base", /*tp_name*/
1349
sizeof(FourBand), /*tp_basicsize*/
1351
(destructor)FourBand_dealloc, /*tp_dealloc*/
1357
&FourBand_as_number, /*tp_as_number*/
1358
0, /*tp_as_sequence*/
1359
0, /*tp_as_mapping*/
1366
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
1367
"FourBand objects. Reads one band from a FourBandMain process.", /* tp_doc */
1368
(traverseproc)FourBand_traverse, /* tp_traverse */
1369
(inquiry)FourBand_clear, /* tp_clear */
1370
0, /* tp_richcompare */
1371
0, /* tp_weaklistoffset */
1373
0, /* tp_iternext */
1374
FourBand_methods, /* tp_methods */
1375
FourBand_members, /* tp_members */
1379
0, /* tp_descr_get */
1380
0, /* tp_descr_set */
1381
0, /* tp_dictoffset */
1382
(initproc)FourBand_init, /* tp_init */
1384
FourBand_new, /* tp_new */