~ubuntu-branches/debian/sid/python-pyo/sid

« back to all changes in this revision

Viewing changes to src/objects/bandsplitmodule.c

  • Committer: Package Import Robot
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2012-06-08 20:35:45 UTC
  • Revision ID: package-import@ubuntu.com-20120608203545-4z7kcf2lgvpsk18y
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 * Copyright 2010 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 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.                                    * 
 
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 General Public License for more details.                           *
 
16
 *                                                                        *
 
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
 *************************************************************************/
 
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
    pyo_audio_HEAD
 
31
    PyObject *input;
 
32
    Stream *input_stream;
 
33
    PyObject *q;
 
34
    Stream *q_stream;
 
35
    int bands;
 
36
    MYFLT min_freq;
 
37
    MYFLT max_freq;
 
38
    int init;
 
39
    MYFLT halfSr;
 
40
    MYFLT TwoPiOnSr;
 
41
    MYFLT *band_freqs;
 
42
    // sample memories
 
43
    MYFLT *x1;
 
44
    MYFLT *x2;
 
45
    MYFLT *y1;
 
46
    MYFLT *y2;
 
47
    // coefficients
 
48
    MYFLT *b0;
 
49
    MYFLT *b1;
 
50
    MYFLT *b2;
 
51
    MYFLT *a0;
 
52
    MYFLT *a1;
 
53
    MYFLT *a2;
 
54
    MYFLT *buffer_streams;
 
55
    int modebuffer[1];
 
56
} BandSplitter;
 
57
 
 
58
 
 
59
static void
 
60
BandSplitter_compute_variables(BandSplitter *self, MYFLT q)
 
61
{    
 
62
    int i;
 
63
    MYFLT freq;
 
64
    for (i=0; i<self->bands; i++) {
 
65
        freq = self->band_freqs[i];
 
66
        if (freq <= 1) 
 
67
            freq = 1;
 
68
        else if (freq >= self->halfSr)
 
69
            freq = self->halfSr;
 
70
    
 
71
        MYFLT w0 = self->TwoPiOnSr * freq;
 
72
        MYFLT c = MYCOS(w0);
 
73
        MYFLT alpha = MYSIN(w0) / (2 * q);
 
74
    
 
75
        self->b0[i] = alpha;
 
76
        self->b2[i] = -alpha;
 
77
        self->a0[i] = 1 + alpha;
 
78
        self->a1[i] = -2 * c;
 
79
        self->a2[i] = 1 - alpha;
 
80
    }    
 
81
}
 
82
 
 
83
static void
 
84
BandSplitter_setFrequencies(BandSplitter *self)
 
85
{
 
86
    int i;
 
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;
 
90
    }
 
91
}
 
92
 
 
93
static void
 
94
BandSplitter_filters_i(BandSplitter *self) {
 
95
    MYFLT val;
 
96
    int j, i;
 
97
    MYFLT *in = Stream_getData((Stream *)self->input_stream);
 
98
    
 
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];
 
102
        }    
 
103
        self->init = 0;
 
104
    }
 
105
    
 
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];
 
110
            self->y1[j] = val;
 
111
            self->x2[j] = self->x1[j];
 
112
            self->x1[j] = in[i];
 
113
            self->buffer_streams[i + j * self->bufsize] = val;
 
114
        }    
 
115
    }
 
116
}
 
117
 
 
118
static void
 
119
BandSplitter_filters_a(BandSplitter *self) {
 
120
    MYFLT val;
 
121
    int j, i;
 
122
    MYFLT *in = Stream_getData((Stream *)self->input_stream);
 
123
    
 
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];
 
127
        }    
 
128
        self->init = 0;
 
129
    }
 
130
 
 
131
    MYFLT *q = Stream_getData((Stream *)self->q_stream);
 
132
 
 
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];
 
138
            self->y1[j] = val;
 
139
            self->x2[j] = self->x1[j];
 
140
            self->x1[j] = in[i];
 
141
            self->buffer_streams[i + j * self->bufsize] = val;
 
142
        }    
 
143
    }
 
144
}
 
145
 
 
146
MYFLT *
 
147
BandSplitter_getSamplesBuffer(BandSplitter *self)
 
148
{
 
149
    return (MYFLT *)self->buffer_streams;
 
150
}    
 
151
 
 
152
static void
 
153
BandSplitter_setProcMode(BandSplitter *self)
 
154
{
 
155
    int procmode;
 
156
    procmode = self->modebuffer[0];
 
157
    
 
158
        switch (procmode) {
 
159
        case 0:        
 
160
            self->proc_func_ptr = BandSplitter_filters_i;
 
161
            break;
 
162
        case 1:    
 
163
            self->proc_func_ptr = BandSplitter_filters_a;
 
164
            break;
 
165
    }    
 
166
}
 
167
 
 
168
static void
 
169
BandSplitter_compute_next_data_frame(BandSplitter *self)
 
170
{
 
171
    (*self->proc_func_ptr)(self); 
 
172
}
 
173
 
 
174
static int
 
175
BandSplitter_traverse(BandSplitter *self, visitproc visit, void *arg)
 
176
{
 
177
    pyo_VISIT
 
178
    Py_VISIT(self->input);
 
179
    Py_VISIT(self->input_stream);
 
180
    return 0;
 
181
}
 
182
 
 
183
static int 
 
184
BandSplitter_clear(BandSplitter *self)
 
185
{
 
186
    pyo_CLEAR
 
187
    Py_CLEAR(self->input);
 
188
    Py_CLEAR(self->input_stream);
 
189
    return 0;
 
190
}
 
191
 
 
192
static void
 
193
BandSplitter_dealloc(BandSplitter* self)
 
194
{
 
195
    free(self->data);
 
196
    free(self->band_freqs);
 
197
    free(self->x1);
 
198
    free(self->x2);
 
199
    free(self->y1);
 
200
    free(self->y2);
 
201
    free(self->b0);
 
202
    free(self->b1);
 
203
    free(self->b2);
 
204
    free(self->a0);
 
205
    free(self->a1);
 
206
    free(self->a2);
 
207
    free(self->buffer_streams);
 
208
    BandSplitter_clear(self);
 
209
    self->ob_type->tp_free((PyObject*)self);
 
210
}
 
211
 
 
212
static PyObject * BandSplitter_deleteStream(BandSplitter *self) { DELETE_STREAM };
 
213
 
 
214
static PyObject *
 
215
BandSplitter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
216
{
 
217
    int i;
 
218
    BandSplitter *self;
 
219
    self = (BandSplitter *)type->tp_alloc(type, 0);
 
220
 
 
221
 
 
222
    self->bands = 4;
 
223
    self->q = PyFloat_FromDouble(1.);
 
224
    self->init = 1;
 
225
    
 
226
    INIT_OBJECT_COMMON
 
227
    Stream_setFunctionPtr(self->stream, BandSplitter_compute_next_data_frame);
 
228
    self->mode_func_ptr = BandSplitter_setProcMode;
 
229
    
 
230
    self->halfSr = self->sr / 2.;
 
231
    self->TwoPiOnSr = TWOPI / self->sr;
 
232
    return (PyObject *)self;
 
233
}
 
234
 
 
235
static int
 
236
BandSplitter_init(BandSplitter *self, PyObject *args, PyObject *kwds)
 
237
{
 
238
    PyObject *inputtmp, *input_streamtmp, *qtmp=NULL;
 
239
    
 
240
    static char *kwlist[] = {"input", "bands", "min", "max", "q", NULL};
 
241
    
 
242
    if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_IFFO, kwlist, &inputtmp, &self->bands, &self->min_freq, &self->max_freq, &qtmp))
 
243
        return -1; 
 
244
 
 
245
    INIT_INPUT_STREAM
 
246
 
 
247
    Py_INCREF(self->stream);
 
248
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
249
    
 
250
    self->band_freqs = (MYFLT *)realloc(self->band_freqs, self->bands * sizeof(MYFLT));
 
251
    
 
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));
 
256
 
 
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));
 
263
 
 
264
    self->buffer_streams = (MYFLT *)realloc(self->buffer_streams, self->bands * self->bufsize * sizeof(MYFLT));
 
265
 
 
266
    BandSplitter_setFrequencies((BandSplitter *)self);
 
267
 
 
268
    if (qtmp) {
 
269
        PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
 
270
    }
 
271
    else {
 
272
        BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
 
273
    }
 
274
 
 
275
    (*self->mode_func_ptr)(self);
 
276
    
 
277
    Py_INCREF(self);
 
278
    return 0;
 
279
}
 
280
 
 
281
static PyObject *
 
282
BandSplitter_setQ(BandSplitter *self, PyObject *arg)
 
283
{
 
284
        PyObject *tmp, *streamtmp;
 
285
        
 
286
        if (arg == NULL) {
 
287
                Py_INCREF(Py_None);
 
288
                return Py_None;
 
289
        }
 
290
    
 
291
        int isNumber = PyNumber_Check(arg);
 
292
        
 
293
        tmp = arg;
 
294
        Py_INCREF(tmp);
 
295
        Py_DECREF(self->q);
 
296
        if (isNumber == 1) {
 
297
                self->q = PyNumber_Float(tmp);
 
298
        self->modebuffer[0] = 0;
 
299
        BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
 
300
        }
 
301
        else {
 
302
                self->q = tmp;
 
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;
 
308
        }
 
309
 
 
310
    (*self->mode_func_ptr)(self);
 
311
 
 
312
        Py_INCREF(Py_None);
 
313
        return Py_None;
 
314
}       
 
315
 
 
316
static PyObject * BandSplitter_getServer(BandSplitter* self) { GET_SERVER };
 
317
static PyObject * BandSplitter_getStream(BandSplitter* self) { GET_STREAM };
 
318
 
 
319
static PyObject * BandSplitter_play(BandSplitter *self, PyObject *args, PyObject *kwds) { PLAY };
 
320
static PyObject * BandSplitter_stop(BandSplitter *self) { STOP };
 
321
 
 
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 */
 
328
};
 
329
 
 
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 */
 
338
};
 
339
 
 
340
PyTypeObject BandSplitterType = {
 
341
PyObject_HEAD_INIT(NULL)
 
342
0,                                              /*ob_size*/
 
343
"_pyo.BandSplitter_base",                                   /*tp_name*/
 
344
sizeof(BandSplitter),                                 /*tp_basicsize*/
 
345
0,                                              /*tp_itemsize*/
 
346
(destructor)BandSplitter_dealloc,                     /*tp_dealloc*/
 
347
0,                                              /*tp_print*/
 
348
0,                                              /*tp_getattr*/
 
349
0,                                              /*tp_setattr*/
 
350
0,                                              /*tp_compare*/
 
351
0,                                              /*tp_repr*/
 
352
0,                              /*tp_as_number*/
 
353
0,                                              /*tp_as_sequence*/
 
354
0,                                              /*tp_as_mapping*/
 
355
0,                                              /*tp_hash */
 
356
0,                                              /*tp_call*/
 
357
0,                                              /*tp_str*/
 
358
0,                                              /*tp_getattro*/
 
359
0,                                              /*tp_setattro*/
 
360
0,                                              /*tp_as_buffer*/
 
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 */
 
367
0,                                              /* tp_iter */
 
368
0,                                              /* tp_iternext */
 
369
BandSplitter_methods,                                 /* tp_methods */
 
370
BandSplitter_members,                                 /* tp_members */
 
371
0,                                              /* tp_getset */
 
372
0,                                              /* tp_base */
 
373
0,                                              /* tp_dict */
 
374
0,                                              /* tp_descr_get */
 
375
0,                                              /* tp_descr_set */
 
376
0,                                              /* tp_dictoffset */
 
377
(initproc)BandSplitter_init,                          /* tp_init */
 
378
0,                                              /* tp_alloc */
 
379
BandSplitter_new,                                     /* tp_new */
 
380
};
 
381
 
 
382
/************************************************************************************************/
 
383
/* BandSplit streamer object */
 
384
/************************************************************************************************/
 
385
typedef struct {
 
386
    pyo_audio_HEAD
 
387
    BandSplitter *mainSplitter;
 
388
    int modebuffer[2];
 
389
    int chnl; 
 
390
} BandSplit;
 
391
 
 
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 };
 
401
 
 
402
static void
 
403
BandSplit_setProcMode(BandSplit *self)
 
404
{
 
405
    int muladdmode;
 
406
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
407
    
 
408
        switch (muladdmode) {
 
409
        case 0:        
 
410
            self->muladd_func_ptr = BandSplit_postprocessing_ii;
 
411
            break;
 
412
        case 1:    
 
413
            self->muladd_func_ptr = BandSplit_postprocessing_ai;
 
414
            break;
 
415
        case 2:    
 
416
            self->muladd_func_ptr = BandSplit_postprocessing_revai;
 
417
            break;
 
418
        case 10:        
 
419
            self->muladd_func_ptr = BandSplit_postprocessing_ia;
 
420
            break;
 
421
        case 11:    
 
422
            self->muladd_func_ptr = BandSplit_postprocessing_aa;
 
423
            break;
 
424
        case 12:    
 
425
            self->muladd_func_ptr = BandSplit_postprocessing_revaa;
 
426
            break;
 
427
        case 20:        
 
428
            self->muladd_func_ptr = BandSplit_postprocessing_ireva;
 
429
            break;
 
430
        case 21:    
 
431
            self->muladd_func_ptr = BandSplit_postprocessing_areva;
 
432
            break;
 
433
        case 22:    
 
434
            self->muladd_func_ptr = BandSplit_postprocessing_revareva;
 
435
            break;
 
436
    }
 
437
}
 
438
 
 
439
static void
 
440
BandSplit_compute_next_data_frame(BandSplit *self)
 
441
{
 
442
    int i;
 
443
    MYFLT *tmp;
 
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];
 
448
    }    
 
449
    (*self->muladd_func_ptr)(self);
 
450
}
 
451
 
 
452
static int
 
453
BandSplit_traverse(BandSplit *self, visitproc visit, void *arg)
 
454
{
 
455
    pyo_VISIT
 
456
    Py_VISIT(self->mainSplitter);
 
457
    return 0;
 
458
}
 
459
 
 
460
static int 
 
461
BandSplit_clear(BandSplit *self)
 
462
{
 
463
    pyo_CLEAR
 
464
    Py_CLEAR(self->mainSplitter);    
 
465
    return 0;
 
466
}
 
467
 
 
468
static void
 
469
BandSplit_dealloc(BandSplit* self)
 
470
{
 
471
    free(self->data);
 
472
    BandSplit_clear(self);
 
473
    self->ob_type->tp_free((PyObject*)self);
 
474
}
 
475
 
 
476
static PyObject * BandSplit_deleteStream(BandSplit *self) { DELETE_STREAM };
 
477
 
 
478
static PyObject *
 
479
BandSplit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
480
{
 
481
    int i;
 
482
    BandSplit *self;
 
483
    self = (BandSplit *)type->tp_alloc(type, 0);
 
484
    
 
485
    self->chnl = 0;
 
486
        self->modebuffer[0] = 0;
 
487
        self->modebuffer[1] = 0;
 
488
    
 
489
    INIT_OBJECT_COMMON
 
490
    Stream_setFunctionPtr(self->stream, BandSplit_compute_next_data_frame);
 
491
    self->mode_func_ptr = BandSplit_setProcMode;
 
492
    
 
493
    return (PyObject *)self;
 
494
}
 
495
 
 
496
static int
 
497
BandSplit_init(BandSplit *self, PyObject *args, PyObject *kwds)
 
498
{
 
499
    PyObject *maintmp=NULL, *multmp=NULL, *addtmp=NULL;
 
500
    
 
501
    static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
 
502
    
 
503
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
 
504
        return -1; 
 
505
    
 
506
    Py_XDECREF(self->mainSplitter);
 
507
    Py_INCREF(maintmp);
 
508
    self->mainSplitter = (BandSplitter *)maintmp;
 
509
    
 
510
    if (multmp) {
 
511
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
512
    }
 
513
    
 
514
    if (addtmp) {
 
515
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
516
    }
 
517
    
 
518
    Py_INCREF(self->stream);
 
519
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
520
    
 
521
    (*self->mode_func_ptr)(self);
 
522
        
 
523
    Py_INCREF(self);
 
524
    return 0;
 
525
}
 
526
 
 
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 }; 
 
533
 
 
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 };
 
537
 
 
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 };
 
546
 
 
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 */
 
553
};
 
554
 
 
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 */
 
567
};
 
568
 
 
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*/
 
574
0,                /*nb_remainder*/
 
575
0,                   /*nb_divmod*/
 
576
0,                   /*nb_power*/
 
577
0,                  /*nb_neg*/
 
578
0,                /*nb_pos*/
 
579
0,                  /*(unaryfunc)array_abs,*/
 
580
0,                    /*nb_nonzero*/
 
581
0,                    /*nb_invert*/
 
582
0,               /*nb_lshift*/
 
583
0,              /*nb_rshift*/
 
584
0,              /*nb_and*/
 
585
0,              /*nb_xor*/
 
586
0,               /*nb_or*/
 
587
0,                                          /*nb_coerce*/
 
588
0,                       /*nb_int*/
 
589
0,                      /*nb_long*/
 
590
0,                     /*nb_float*/
 
591
0,                       /*nb_oct*/
 
592
0,                       /*nb_hex*/
 
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*/
 
598
0,           /*inplace_power*/
 
599
0,       /*inplace_lshift*/
 
600
0,      /*inplace_rshift*/
 
601
0,      /*inplace_and*/
 
602
0,      /*inplace_xor*/
 
603
0,       /*inplace_or*/
 
604
0,             /*nb_floor_divide*/
 
605
0,              /*nb_true_divide*/
 
606
0,     /*nb_inplace_floor_divide*/
 
607
0,      /*nb_inplace_true_divide*/
 
608
0,                     /* nb_index */
 
609
};
 
610
 
 
611
PyTypeObject BandSplitType = {
 
612
PyObject_HEAD_INIT(NULL)
 
613
0,                         /*ob_size*/
 
614
"_pyo.BandSplit_base",         /*tp_name*/
 
615
sizeof(BandSplit),         /*tp_basicsize*/
 
616
0,                         /*tp_itemsize*/
 
617
(destructor)BandSplit_dealloc, /*tp_dealloc*/
 
618
0,                         /*tp_print*/
 
619
0,                         /*tp_getattr*/
 
620
0,                         /*tp_setattr*/
 
621
0,                         /*tp_compare*/
 
622
0,                         /*tp_repr*/
 
623
&BandSplit_as_number,             /*tp_as_number*/
 
624
0,                         /*tp_as_sequence*/
 
625
0,                         /*tp_as_mapping*/
 
626
0,                         /*tp_hash */
 
627
0,                         /*tp_call*/
 
628
0,                         /*tp_str*/
 
629
0,                         /*tp_getattro*/
 
630
0,                         /*tp_setattro*/
 
631
0,                         /*tp_as_buffer*/
 
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 */
 
638
0,                             /* tp_iter */
 
639
0,                             /* tp_iternext */
 
640
BandSplit_methods,             /* tp_methods */
 
641
BandSplit_members,             /* tp_members */
 
642
0,                      /* tp_getset */
 
643
0,                         /* tp_base */
 
644
0,                         /* tp_dict */
 
645
0,                         /* tp_descr_get */
 
646
0,                         /* tp_descr_set */
 
647
0,                         /* tp_dictoffset */
 
648
(initproc)BandSplit_init,      /* tp_init */
 
649
0,                         /* tp_alloc */
 
650
BandSplit_new,                 /* tp_new */
 
651
};
 
652
 
 
653
/*****************/
 
654
/* FourBand main */
 
655
/*****************/
 
656
typedef struct {
 
657
    pyo_audio_HEAD
 
658
    PyObject *input;
 
659
    Stream *input_stream;
 
660
    PyObject *freq1;
 
661
    Stream *freq1_stream;
 
662
    PyObject *freq2;
 
663
    Stream *freq2_stream;
 
664
    PyObject *freq3;
 
665
    Stream *freq3_stream;
 
666
    double last_freq1;
 
667
    double last_freq2;
 
668
    double last_freq3;
 
669
    // sample memories
 
670
    double x1[6];
 
671
    double x2[6];
 
672
    double x3[6];
 
673
    double x4[6];
 
674
    double y1[6];
 
675
    double y2[6];
 
676
    double y3[6];
 
677
    double y4[6];
 
678
    // coefficients
 
679
    double b1[3];
 
680
    double b2[3];
 
681
    double b3[3];
 
682
    double b4[3];
 
683
    double la0[3];
 
684
    double la1[3];
 
685
    double la2[3];
 
686
    double ha0[3];
 
687
    double ha1[3];
 
688
    double ha2[3];
 
689
    MYFLT *buffer_streams;
 
690
    int modebuffer[3];
 
691
} FourBandMain;
 
692
 
 
693
 
 
694
static void
 
695
FourBandMain_compute_variables(FourBandMain *self, double freq, int band)
 
696
{    
 
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);
 
702
    double k2 = k * k;
 
703
    double k3 = k2 * k;
 
704
    double k4 = k2 * k2;
 
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;
 
711
    
 
712
    /* common */
 
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;
 
717
    
 
718
    /* lowpass */
 
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;
 
722
    
 
723
    /* highpass */
 
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;    
 
727
}
 
728
 
 
729
static void
 
730
FourBandMain_filters(FourBandMain *self) {
 
731
    double val, inval, tmp, f1, f2, f3;
 
732
    int i, j, j1, ind, ind1;
 
733
    
 
734
    MYFLT *in = Stream_getData((Stream *)self->input_stream);
 
735
    
 
736
    if (self->modebuffer[0] == 0)
 
737
        f1 = PyFloat_AS_DOUBLE(self->freq1);
 
738
    else
 
739
        f1 = (double)Stream_getData((Stream *)self->freq1_stream)[0];
 
740
    if (self->modebuffer[1] == 0)
 
741
        f2 = PyFloat_AS_DOUBLE(self->freq2);
 
742
    else
 
743
        f2 = (double)Stream_getData((Stream *)self->freq2_stream)[0];
 
744
    if (self->modebuffer[2] == 0)
 
745
        f3 = PyFloat_AS_DOUBLE(self->freq3);
 
746
    else
 
747
        f3 = (double)Stream_getData((Stream *)self->freq3_stream)[0];
 
748
    
 
749
    if (f1 != self->last_freq1) {
 
750
        self->last_freq1 = f1;
 
751
        FourBandMain_compute_variables(self, f1, 0);
 
752
    }
 
753
 
 
754
    if (f2 != self->last_freq2) {
 
755
        self->last_freq2 = f2;
 
756
        FourBandMain_compute_variables(self, f2, 1);
 
757
    }
 
758
 
 
759
    if (f3 != self->last_freq3) {
 
760
        self->last_freq3 = f3;
 
761
        FourBandMain_compute_variables(self, f3, 2);
 
762
    }
 
763
    
 
764
    
 
765
    for (i=0; i<self->bufsize; i++) {   
 
766
        inval = (double)in[i];
 
767
        /* First band */
 
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];
 
773
        self->y1[0] = val;
 
774
        self->x4[0] = self->x3[0];
 
775
        self->x3[0] = self->x2[0];
 
776
        self->x2[0] = self->x1[0];
 
777
        self->x1[0] = inval;
 
778
        self->buffer_streams[i] = (MYFLT)val;
 
779
        
 
780
        /* Second and third bands */
 
781
        for (j=0; j<2; j++) {
 
782
            j1 = j + 1;
 
783
            ind = j * 2 + 1;
 
784
            ind1 = ind + 1;
 
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];
 
790
            self->y1[ind] = tmp;
 
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;
 
795
            
 
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;
 
806
            
 
807
            self->buffer_streams[i + j1 * self->bufsize] = (MYFLT)val;            
 
808
        }
 
809
 
 
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];
 
815
        self->y1[5] = val;
 
816
        self->x4[5] = self->x3[5];
 
817
        self->x3[5] = self->x2[5];
 
818
        self->x2[5] = self->x1[5];
 
819
        self->x1[5] = inval;
 
820
        self->buffer_streams[i + 3 * self->bufsize] = (MYFLT)val;
 
821
    }    
 
822
}
 
823
 
 
824
MYFLT *
 
825
FourBandMain_getSamplesBuffer(FourBandMain *self)
 
826
{
 
827
    return (MYFLT *)self->buffer_streams;
 
828
}    
 
829
 
 
830
static void
 
831
FourBandMain_setProcMode(FourBandMain *self)
 
832
{
 
833
    self->proc_func_ptr = FourBandMain_filters;
 
834
}
 
835
 
 
836
static void
 
837
FourBandMain_compute_next_data_frame(FourBandMain *self)
 
838
{
 
839
    (*self->proc_func_ptr)(self); 
 
840
}
 
841
 
 
842
static int
 
843
FourBandMain_traverse(FourBandMain *self, visitproc visit, void *arg)
 
844
{
 
845
    pyo_VISIT
 
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);
 
854
    return 0;
 
855
}
 
856
 
 
857
static int 
 
858
FourBandMain_clear(FourBandMain *self)
 
859
{
 
860
    pyo_CLEAR
 
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);
 
869
    return 0;
 
870
}
 
871
 
 
872
static void
 
873
FourBandMain_dealloc(FourBandMain* self)
 
874
{
 
875
    free(self->data);
 
876
    free(self->buffer_streams);
 
877
    FourBandMain_clear(self);
 
878
    self->ob_type->tp_free((PyObject*)self);
 
879
}
 
880
 
 
881
static PyObject * FourBandMain_deleteStream(FourBandMain *self) { DELETE_STREAM };
 
882
 
 
883
static PyObject *
 
884
FourBandMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
885
{
 
886
    int i;
 
887
    FourBandMain *self;
 
888
    self = (FourBandMain *)type->tp_alloc(type, 0);
 
889
    
 
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;
 
894
    
 
895
    self->modebuffer[0] = 0;
 
896
    self->modebuffer[1] = 0;
 
897
    self->modebuffer[2] = 0;
 
898
 
 
899
    INIT_OBJECT_COMMON
 
900
    Stream_setFunctionPtr(self->stream, FourBandMain_compute_next_data_frame);
 
901
    self->mode_func_ptr = FourBandMain_setProcMode;
 
902
    
 
903
    return (PyObject *)self;
 
904
}
 
905
 
 
906
static int
 
907
FourBandMain_init(FourBandMain *self, PyObject *args, PyObject *kwds)
 
908
{
 
909
    int i;
 
910
    PyObject *inputtmp, *input_streamtmp, *freq1tmp=NULL, *freq2tmp=NULL, *freq3tmp=NULL;
 
911
    
 
912
    static char *kwlist[] = {"input", "freq1", "freq2", "freq3", NULL};
 
913
    
 
914
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freq1tmp, &freq2tmp, &freq3tmp))
 
915
        return -1; 
 
916
    
 
917
    INIT_INPUT_STREAM
 
918
    
 
919
    Py_INCREF(self->stream);
 
920
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
921
    
 
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;
 
924
    }
 
925
 
 
926
    self->buffer_streams = (MYFLT *)realloc(self->buffer_streams, 4 * self->bufsize * sizeof(MYFLT));
 
927
 
 
928
    for (i=0; i<(4 * self->bufsize); i++) {
 
929
        self->buffer_streams[i] = 0.0;
 
930
    }
 
931
    
 
932
    if (freq1tmp) {
 
933
        PyObject_CallMethod((PyObject *)self, "setFreq1", "O", freq1tmp);
 
934
    }
 
935
 
 
936
    if (freq2tmp) {
 
937
        PyObject_CallMethod((PyObject *)self, "setFreq2", "O", freq2tmp);
 
938
    }
 
939
 
 
940
    if (freq3tmp) {
 
941
        PyObject_CallMethod((PyObject *)self, "setFreq3", "O", freq3tmp);
 
942
    }
 
943
    
 
944
    (*self->mode_func_ptr)(self);
 
945
    
 
946
    Py_INCREF(self);
 
947
    return 0;
 
948
}
 
949
 
 
950
static PyObject *
 
951
FourBandMain_setFreq1(FourBandMain *self, PyObject *arg)
 
952
{
 
953
        PyObject *tmp, *streamtmp;
 
954
        
 
955
        if (arg == NULL) {
 
956
                Py_INCREF(Py_None);
 
957
                return Py_None;
 
958
        }
 
959
    
 
960
        int isNumber = PyNumber_Check(arg);
 
961
        
 
962
        tmp = arg;
 
963
        Py_INCREF(tmp);
 
964
        Py_DECREF(self->freq1);
 
965
        if (isNumber == 1) {
 
966
                self->freq1 = PyNumber_Float(tmp);
 
967
        self->modebuffer[0] = 0;
 
968
        }
 
969
        else {
 
970
                self->freq1 = tmp;
 
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;
 
976
        }
 
977
 
 
978
        Py_INCREF(Py_None);
 
979
        return Py_None;
 
980
}       
 
981
 
 
982
static PyObject *
 
983
FourBandMain_setFreq2(FourBandMain *self, PyObject *arg)
 
984
{
 
985
        PyObject *tmp, *streamtmp;
 
986
        
 
987
        if (arg == NULL) {
 
988
                Py_INCREF(Py_None);
 
989
                return Py_None;
 
990
        }
 
991
    
 
992
        int isNumber = PyNumber_Check(arg);
 
993
        
 
994
        tmp = arg;
 
995
        Py_INCREF(tmp);
 
996
        Py_DECREF(self->freq2);
 
997
        if (isNumber == 1) {
 
998
                self->freq2 = PyNumber_Float(tmp);
 
999
        self->modebuffer[1] = 0;
 
1000
        }
 
1001
        else {
 
1002
                self->freq2 = tmp;
 
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;
 
1008
        }
 
1009
 
 
1010
        Py_INCREF(Py_None);
 
1011
        return Py_None;
 
1012
}       
 
1013
 
 
1014
static PyObject *
 
1015
FourBandMain_setFreq3(FourBandMain *self, PyObject *arg)
 
1016
{
 
1017
        PyObject *tmp, *streamtmp;
 
1018
        
 
1019
        if (arg == NULL) {
 
1020
                Py_INCREF(Py_None);
 
1021
                return Py_None;
 
1022
        }
 
1023
    
 
1024
        int isNumber = PyNumber_Check(arg);
 
1025
        
 
1026
        tmp = arg;
 
1027
        Py_INCREF(tmp);
 
1028
        Py_DECREF(self->freq3);
 
1029
        if (isNumber == 1) {
 
1030
                self->freq3 = PyNumber_Float(tmp);
 
1031
        self->modebuffer[2] = 0;
 
1032
        }
 
1033
        else {
 
1034
                self->freq3 = tmp;
 
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;
 
1040
        }
 
1041
 
 
1042
        Py_INCREF(Py_None);
 
1043
        return Py_None;
 
1044
}       
 
1045
 
 
1046
static PyObject * FourBandMain_getServer(FourBandMain* self) { GET_SERVER };
 
1047
static PyObject * FourBandMain_getStream(FourBandMain* self) { GET_STREAM };
 
1048
 
 
1049
static PyObject * FourBandMain_play(FourBandMain *self, PyObject *args, PyObject *kwds) { PLAY };
 
1050
static PyObject * FourBandMain_stop(FourBandMain *self) { STOP };
 
1051
 
 
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 */
 
1060
};
 
1061
 
 
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 */
 
1072
};
 
1073
 
 
1074
PyTypeObject FourBandMainType = {
 
1075
    PyObject_HEAD_INIT(NULL)
 
1076
    0,                                              /*ob_size*/
 
1077
    "_pyo.FourBandMain_base",                                   /*tp_name*/
 
1078
    sizeof(FourBandMain),                                 /*tp_basicsize*/
 
1079
    0,                                              /*tp_itemsize*/
 
1080
    (destructor)FourBandMain_dealloc,                     /*tp_dealloc*/
 
1081
    0,                                              /*tp_print*/
 
1082
    0,                                              /*tp_getattr*/
 
1083
    0,                                              /*tp_setattr*/
 
1084
    0,                                              /*tp_compare*/
 
1085
    0,                                              /*tp_repr*/
 
1086
    0,                              /*tp_as_number*/
 
1087
    0,                                              /*tp_as_sequence*/
 
1088
    0,                                              /*tp_as_mapping*/
 
1089
    0,                                              /*tp_hash */
 
1090
    0,                                              /*tp_call*/
 
1091
    0,                                              /*tp_str*/
 
1092
    0,                                              /*tp_getattro*/
 
1093
    0,                                              /*tp_setattro*/
 
1094
    0,                                              /*tp_as_buffer*/
 
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 */
 
1101
    0,                                              /* tp_iter */
 
1102
    0,                                              /* tp_iternext */
 
1103
    FourBandMain_methods,                                 /* tp_methods */
 
1104
    FourBandMain_members,                                 /* tp_members */
 
1105
    0,                                              /* tp_getset */
 
1106
    0,                                              /* tp_base */
 
1107
    0,                                              /* tp_dict */
 
1108
    0,                                              /* tp_descr_get */
 
1109
    0,                                              /* tp_descr_set */
 
1110
    0,                                              /* tp_dictoffset */
 
1111
    (initproc)FourBandMain_init,                          /* tp_init */
 
1112
    0,                                              /* tp_alloc */
 
1113
    FourBandMain_new,                                     /* tp_new */
 
1114
};
 
1115
 
 
1116
/************************************************************************************************/
 
1117
/* FourBand streamer object */
 
1118
/************************************************************************************************/
 
1119
typedef struct {
 
1120
    pyo_audio_HEAD
 
1121
    FourBandMain *mainSplitter;
 
1122
    int modebuffer[2];
 
1123
    int chnl; 
 
1124
} FourBand;
 
1125
 
 
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 };
 
1135
 
 
1136
static void
 
1137
FourBand_setProcMode(FourBand *self)
 
1138
{
 
1139
    int muladdmode;
 
1140
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
1141
    
 
1142
        switch (muladdmode) {
 
1143
        case 0:        
 
1144
            self->muladd_func_ptr = FourBand_postprocessing_ii;
 
1145
            break;
 
1146
        case 1:    
 
1147
            self->muladd_func_ptr = FourBand_postprocessing_ai;
 
1148
            break;
 
1149
        case 2:    
 
1150
            self->muladd_func_ptr = FourBand_postprocessing_revai;
 
1151
            break;
 
1152
        case 10:        
 
1153
            self->muladd_func_ptr = FourBand_postprocessing_ia;
 
1154
            break;
 
1155
        case 11:    
 
1156
            self->muladd_func_ptr = FourBand_postprocessing_aa;
 
1157
            break;
 
1158
        case 12:    
 
1159
            self->muladd_func_ptr = FourBand_postprocessing_revaa;
 
1160
            break;
 
1161
        case 20:        
 
1162
            self->muladd_func_ptr = FourBand_postprocessing_ireva;
 
1163
            break;
 
1164
        case 21:    
 
1165
            self->muladd_func_ptr = FourBand_postprocessing_areva;
 
1166
            break;
 
1167
        case 22:    
 
1168
            self->muladd_func_ptr = FourBand_postprocessing_revareva;
 
1169
            break;
 
1170
    }
 
1171
}
 
1172
 
 
1173
static void
 
1174
FourBand_compute_next_data_frame(FourBand *self)
 
1175
{
 
1176
    int i;
 
1177
    MYFLT *tmp;
 
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];
 
1182
    }    
 
1183
    (*self->muladd_func_ptr)(self);
 
1184
}
 
1185
 
 
1186
static int
 
1187
FourBand_traverse(FourBand *self, visitproc visit, void *arg)
 
1188
{
 
1189
    pyo_VISIT
 
1190
    Py_VISIT(self->mainSplitter);
 
1191
    return 0;
 
1192
}
 
1193
 
 
1194
static int 
 
1195
FourBand_clear(FourBand *self)
 
1196
{
 
1197
    pyo_CLEAR
 
1198
    Py_CLEAR(self->mainSplitter);    
 
1199
    return 0;
 
1200
}
 
1201
 
 
1202
static void
 
1203
FourBand_dealloc(FourBand* self)
 
1204
{
 
1205
    free(self->data);
 
1206
    FourBand_clear(self);
 
1207
    self->ob_type->tp_free((PyObject*)self);
 
1208
}
 
1209
 
 
1210
static PyObject * FourBand_deleteStream(FourBand *self) { DELETE_STREAM };
 
1211
 
 
1212
static PyObject *
 
1213
FourBand_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
1214
{
 
1215
    int i;
 
1216
    FourBand *self;
 
1217
    self = (FourBand *)type->tp_alloc(type, 0);
 
1218
    
 
1219
    self->chnl = 0;
 
1220
        self->modebuffer[0] = 0;
 
1221
        self->modebuffer[1] = 0;
 
1222
    
 
1223
    INIT_OBJECT_COMMON
 
1224
    Stream_setFunctionPtr(self->stream, FourBand_compute_next_data_frame);
 
1225
    self->mode_func_ptr = FourBand_setProcMode;
 
1226
    
 
1227
    return (PyObject *)self;
 
1228
}
 
1229
 
 
1230
static int
 
1231
FourBand_init(FourBand *self, PyObject *args, PyObject *kwds)
 
1232
{
 
1233
    PyObject *maintmp=NULL, *multmp=NULL, *addtmp=NULL;
 
1234
    
 
1235
    static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
 
1236
    
 
1237
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
 
1238
        return -1; 
 
1239
    
 
1240
    Py_XDECREF(self->mainSplitter);
 
1241
    Py_INCREF(maintmp);
 
1242
    self->mainSplitter = (FourBandMain *)maintmp;
 
1243
    
 
1244
    if (multmp) {
 
1245
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
1246
    }
 
1247
    
 
1248
    if (addtmp) {
 
1249
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
1250
    }
 
1251
    
 
1252
    Py_INCREF(self->stream);
 
1253
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
1254
    
 
1255
    (*self->mode_func_ptr)(self);
 
1256
    
 
1257
    Py_INCREF(self);
 
1258
    return 0;
 
1259
}
 
1260
 
 
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 };   
 
1267
 
 
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 };
 
1271
 
 
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 };
 
1280
 
 
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 */
 
1287
};
 
1288
 
 
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 */
 
1301
};
 
1302
 
 
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*/
 
1308
    0,                /*nb_remainder*/
 
1309
    0,                   /*nb_divmod*/
 
1310
    0,                   /*nb_power*/
 
1311
    0,                  /*nb_neg*/
 
1312
    0,                /*nb_pos*/
 
1313
    0,                  /*(unaryfunc)array_abs,*/
 
1314
    0,                    /*nb_nonzero*/
 
1315
    0,                    /*nb_invert*/
 
1316
    0,               /*nb_lshift*/
 
1317
    0,              /*nb_rshift*/
 
1318
    0,              /*nb_and*/
 
1319
    0,              /*nb_xor*/
 
1320
    0,               /*nb_or*/
 
1321
    0,                                          /*nb_coerce*/
 
1322
    0,                       /*nb_int*/
 
1323
    0,                      /*nb_long*/
 
1324
    0,                     /*nb_float*/
 
1325
    0,                       /*nb_oct*/
 
1326
    0,                       /*nb_hex*/
 
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*/
 
1335
    0,      /*inplace_and*/
 
1336
    0,      /*inplace_xor*/
 
1337
    0,       /*inplace_or*/
 
1338
    0,             /*nb_floor_divide*/
 
1339
    0,              /*nb_true_divide*/
 
1340
    0,     /*nb_inplace_floor_divide*/
 
1341
    0,      /*nb_inplace_true_divide*/
 
1342
    0,                     /* nb_index */
 
1343
};
 
1344
 
 
1345
PyTypeObject FourBandType = {
 
1346
    PyObject_HEAD_INIT(NULL)
 
1347
    0,                         /*ob_size*/
 
1348
    "_pyo.FourBand_base",         /*tp_name*/
 
1349
    sizeof(FourBand),         /*tp_basicsize*/
 
1350
    0,                         /*tp_itemsize*/
 
1351
    (destructor)FourBand_dealloc, /*tp_dealloc*/
 
1352
    0,                         /*tp_print*/
 
1353
    0,                         /*tp_getattr*/
 
1354
    0,                         /*tp_setattr*/
 
1355
    0,                         /*tp_compare*/
 
1356
    0,                         /*tp_repr*/
 
1357
    &FourBand_as_number,             /*tp_as_number*/
 
1358
    0,                         /*tp_as_sequence*/
 
1359
    0,                         /*tp_as_mapping*/
 
1360
    0,                         /*tp_hash */
 
1361
    0,                         /*tp_call*/
 
1362
    0,                         /*tp_str*/
 
1363
    0,                         /*tp_getattro*/
 
1364
    0,                         /*tp_setattro*/
 
1365
    0,                         /*tp_as_buffer*/
 
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 */
 
1372
    0,                         /* tp_iter */
 
1373
    0,                         /* tp_iternext */
 
1374
    FourBand_methods,             /* tp_methods */
 
1375
    FourBand_members,             /* tp_members */
 
1376
    0,                      /* tp_getset */
 
1377
    0,                         /* tp_base */
 
1378
    0,                         /* tp_dict */
 
1379
    0,                         /* tp_descr_get */
 
1380
    0,                         /* tp_descr_set */
 
1381
    0,                         /* tp_dictoffset */
 
1382
    (initproc)FourBand_init,      /* tp_init */
 
1383
    0,                         /* tp_alloc */
 
1384
    FourBand_new,                 /* tp_new */
 
1385
};