Debian Codegraph @

src/objects/fftmodule.c

raw
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #define PY_SSIZE_T_CLEAN
22
23 #include <Python.h>
24 #include "structmember.h"
25 #include <math.h>
26 #include "pyomodule.h"
27 #include "streammodule.h"
28 #include "servermodule.h"
29 #include "dummymodule.h"
30 #include "fft.h"
31 #include "wind.h"
32 #include "sndfile.h"
33 #include "matrixmodule.h"
34
35 static int
36 isPowerOfTwo(int x)
37 {
38 return (x != 0) && ((x & (x - 1)) == 0);
39 }
40
41 typedef struct
42 {
43 pyo_audio_HEAD
44 PyObject *input;
45 Stream *input_stream;
46 int size;
47 int hsize;
48 int hopsize;
49 int wintype;
50 int incount;
51 MYFLT *inframe;
52 MYFLT *outframe;
53 MYFLT *window;
54 MYFLT **twiddle;
55 //MYFLT *twiddle2;
56 MYFLT *buffer_streams;
57 int allocated;
58 } FFTMain;
59
60 static void
61 FFTMain_realloc_memories(FFTMain *self)
62 {
63 int i, n8;
64 self->hsize = self->size / 2;
65 n8 = self->size >> 3;
66 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size * sizeof(MYFLT));
67 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size * sizeof(MYFLT));
68
69 for (i = 0; i < self->size; i++)
70 self->inframe[i] = self->outframe[i] = 0.0;
71
72 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, 3 * self->bufsize * sizeof(MYFLT));
73
74 for (i = 0; i < (self->bufsize * 3); i++)
75 self->buffer_streams[i] = 0.0;
76
77 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
78
79 for (i = 0; i < 4; i++)
80 {
81 if (self->allocated)
82 {
83 PyMem_RawFree(self->twiddle[i]);
84 }
85 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
86 }
87
88 fft_compute_split_twiddle(self->twiddle, self->size);
89 //self->twiddle2 = (MYFLT *)PyMem_RawRealloc(self->twiddle2, self->size * sizeof(MYFLT));
90 //fft_compute_radix2_twiddle(self->twiddle2, self->size);
91 self->window = (MYFLT *)PyMem_RawRealloc(self->window, self->size * sizeof(MYFLT));
92 gen_window(self->window, self->size, self->wintype);
93 self->incount = -self->hopsize;
94
95 self->allocated = 1;
96 }
97
98 static void
99 FFTMain_filters(FFTMain *self)
100 {
101 int i, incount;
102 MYFLT *in = Stream_getData((Stream *)self->input_stream);
103
104 incount = self->incount;
105
106 for (i = 0; i < self->bufsize; i++)
107 {
108 if (incount >= 0)
109 {
110 self->inframe[incount] = in[i] * self->window[incount];
111
112 if (incount < self->hsize)
113 {
114 self->buffer_streams[i] = self->outframe[incount];
115
116 if (incount)
117 self->buffer_streams[i + self->bufsize] = self->outframe[self->size - incount];
118 else
119 self->buffer_streams[i + self->bufsize] = 0.0;
120 }
121 else if (incount == self->hsize)
122 self->buffer_streams[i] = self->outframe[incount];
123 else
124 self->buffer_streams[i] = self->buffer_streams[i + self->bufsize] = 0.0;
125
126 self->buffer_streams[i + self->bufsize * 2] = (MYFLT)incount;
127 }
128
129 incount++;
130
131 if (incount >= self->size)
132 {
133 incount -= self->size;
134 realfft_split(self->inframe, self->outframe, self->size, self->twiddle);
135 }
136 }
137
138 /*
139 for (i=0; i<self->bufsize; i++) {
140 if (incount >= 0) {
141 self->inframe[incount] = in[i] * self->window[incount];
142 if (incount < self->hsize) {
143 self->buffer_streams[i] = self->outframe[incount*2];
144 self->buffer_streams[i+self->bufsize] = self->outframe[incount*2+1];
145 }
146 else
147 self->buffer_streams[i] = self->buffer_streams[i+self->bufsize] = 0.0;
148 self->buffer_streams[i+self->bufsize*2] = (MYFLT)incount;
149 }
150 incount++;
151 if (incount >= self->size) {
152 incount -= self->size;
153 realfft_packed(self->inframe, self->outframe, self->size, self->twiddle2);
154 }
155 }
156 */
157 self->incount = incount;
158 }
159
160 MYFLT *
161 FFTMain_getSamplesBuffer(FFTMain *self)
162 {
163 return (MYFLT *)self->buffer_streams;
164 }
165
166 static void
167 FFTMain_setProcMode(FFTMain *self)
168 {
169 self->proc_func_ptr = FFTMain_filters;
170 }
171
172 static void
173 FFTMain_compute_next_data_frame(FFTMain *self)
174 {
175 (*self->proc_func_ptr)(self);
176 }
177
178 static int
179 FFTMain_traverse(FFTMain *self, visitproc visit, void *arg)
180 {
181 pyo_VISIT
182 Py_VISIT(self->input);
183 return 0;
184 }
185
186 static int
187 FFTMain_clear(FFTMain *self)
188 {
189 pyo_CLEAR
190 Py_CLEAR(self->input);
191 return 0;
192 }
193
194 static void
195 FFTMain_dealloc(FFTMain* self)
196 {
197 int i;
198 pyo_DEALLOC
199 PyMem_RawFree(self->inframe);
200 PyMem_RawFree(self->outframe);
201 PyMem_RawFree(self->window);
202 PyMem_RawFree(self->buffer_streams);
203
204 for (i = 0; i < 4; i++)
205 {
206 PyMem_RawFree(self->twiddle[i]);
207 }
208
209 PyMem_RawFree(self->twiddle);
210 //free(self->twiddle2);
211 FFTMain_clear(self);
212 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
213 Py_TYPE(self)->tp_free((PyObject*)self);
214 }
215
216 static PyObject *
217 FFTMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
218 {
219 int i;
220 PyObject *inputtmp, *input_streamtmp;
221 FFTMain *self;
222 self = (FFTMain *)type->tp_alloc(type, 0);
223
224 self->size = 1024;
225 self->wintype = 2;
226 self->allocated = 0;
227 INIT_OBJECT_COMMON
228 Stream_setFunctionPtr(self->stream, FFTMain_compute_next_data_frame);
229 self->mode_func_ptr = FFTMain_setProcMode;
230
231 static char *kwlist[] = {"input", "size", "hopsize", "wintype", NULL};
232
233 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iii", kwlist, &inputtmp, &self->size, &self->hopsize, &self->wintype))
234 Py_RETURN_NONE;
235
236 INIT_INPUT_STREAM
237
238 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
239
240 FFTMain_realloc_memories(self);
241
242 (*self->mode_func_ptr)(self);
243
244 return (PyObject *)self;
245 }
246
247 static PyObject * FFTMain_getServer(FFTMain* self) { GET_SERVER };
248 static PyObject * FFTMain_getStream(FFTMain* self) { GET_STREAM };
249
250 static PyObject * FFTMain_play(FFTMain *self, PyObject *args, PyObject *kwds) { PLAY };
251 static PyObject * FFTMain_stop(FFTMain *self, PyObject *args, PyObject *kwds) { STOP };
252
253 static PyObject *
254 FFTMain_setSize(FFTMain *self, PyObject *args, PyObject *kwds)
255 {
256 int size, hopsize;
257
258 static char *kwlist[] = {"size", "hopsize", NULL};
259
260 if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwlist, &size, &hopsize))
261 {
262 Py_RETURN_NONE;
263 }
264
265 if (isPowerOfTwo(size))
266 {
267 self->size = size;
268 self->hopsize = hopsize;
269 FFTMain_realloc_memories(self);
270 }
271 else
272 PySys_WriteStdout("FFT size must be a power of two!\n");
273
274 Py_RETURN_NONE;
275 }
276
277 static PyObject *
278 FFTMain_setWinType(FFTMain *self, PyObject *arg)
279 {
280 if (PyLong_Check(arg))
281 {
282 self->wintype = PyLong_AsLong(arg);
283 gen_window(self->window, self->size, self->wintype);
284 }
285
286 Py_RETURN_NONE;
287 }
288
289 static PyMemberDef FFTMain_members[] =
290 {
291 {"server", T_OBJECT_EX, offsetof(FFTMain, server), 0, "Pyo server."},
292 {"stream", T_OBJECT_EX, offsetof(FFTMain, stream), 0, "Stream object."},
293 {"input", T_OBJECT_EX, offsetof(FFTMain, input), 0, "FFT sound object."},
294 {NULL} /* Sentinel */
295 };
296
297 static PyMethodDef FFTMain_methods[] =
298 {
299 {"getServer", (PyCFunction)FFTMain_getServer, METH_NOARGS, "Returns server object."},
300 {"_getStream", (PyCFunction)FFTMain_getStream, METH_NOARGS, "Returns stream object."},
301 {"play", (PyCFunction)FFTMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
302 {"stop", (PyCFunction)FFTMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
303 {"setSize", (PyCFunction)FFTMain_setSize, METH_VARARGS | METH_KEYWORDS, "Sets a new FFT size."},
304 {"setWinType", (PyCFunction)FFTMain_setWinType, METH_O, "Sets a new window."},
305 {NULL} /* Sentinel */
306 };
307
308 PyTypeObject FFTMainType =
309 {
310 PyVarObject_HEAD_INIT(NULL, 0)
311 "_pyo.FFTMain_base", /*tp_name*/
312 sizeof(FFTMain), /*tp_basicsize*/
313 0, /*tp_itemsize*/
314 (destructor)FFTMain_dealloc, /*tp_dealloc*/
315 0, /*tp_print*/
316 0, /*tp_getattr*/
317 0, /*tp_setattr*/
318 0, /*tp_as_async (tp_compare in Python 2)*/
319 0, /*tp_repr*/
320 0, /*tp_as_number*/
321 0, /*tp_as_sequence*/
322 0, /*tp_as_mapping*/
323 0, /*tp_hash */
324 0, /*tp_call*/
325 0, /*tp_str*/
326 0, /*tp_getattro*/
327 0, /*tp_setattro*/
328 0, /*tp_as_buffer*/
329 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
330 "FFTMain objects. FFT transform.", /* tp_doc */
331 (traverseproc)FFTMain_traverse, /* tp_traverse */
332 (inquiry)FFTMain_clear, /* tp_clear */
333 0, /* tp_richcompare */
334 0, /* tp_weaklistoffset */
335 0, /* tp_iter */
336 0, /* tp_iternext */
337 FFTMain_methods, /* tp_methods */
338 FFTMain_members, /* tp_members */
339 0, /* tp_getset */
340 0, /* tp_base */
341 0, /* tp_dict */
342 0, /* tp_descr_get */
343 0, /* tp_descr_set */
344 0, /* tp_dictoffset */
345 0, /* tp_init */
346 0, /* tp_alloc */
347 FFTMain_new, /* tp_new */
348 };
349
350 /************************************************************************************************/
351 /* FFT streamer object */
352 /************************************************************************************************/
353 typedef struct
354 {
355 pyo_audio_HEAD
356 FFTMain *mainSplitter;
357 int modebuffer[2];
358 int chnl; // 0 = real, 1 = imag, 2 = bin
359 } FFT;
360
361 static void FFT_postprocessing_ii(FFT *self) { POST_PROCESSING_II };
362 static void FFT_postprocessing_ai(FFT *self) { POST_PROCESSING_AI };
363 static void FFT_postprocessing_ia(FFT *self) { POST_PROCESSING_IA };
364 static void FFT_postprocessing_aa(FFT *self) { POST_PROCESSING_AA };
365 static void FFT_postprocessing_ireva(FFT *self) { POST_PROCESSING_IREVA };
366 static void FFT_postprocessing_areva(FFT *self) { POST_PROCESSING_AREVA };
367 static void FFT_postprocessing_revai(FFT *self) { POST_PROCESSING_REVAI };
368 static void FFT_postprocessing_revaa(FFT *self) { POST_PROCESSING_REVAA };
369 static void FFT_postprocessing_revareva(FFT *self) { POST_PROCESSING_REVAREVA };
370
371 static void
372 FFT_setProcMode(FFT *self)
373 {
374 int muladdmode;
375 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
376
377 switch (muladdmode)
378 {
379 case 0:
380 self->muladd_func_ptr = FFT_postprocessing_ii;
381 break;
382
383 case 1:
384 self->muladd_func_ptr = FFT_postprocessing_ai;
385 break;
386
387 case 2:
388 self->muladd_func_ptr = FFT_postprocessing_revai;
389 break;
390
391 case 10:
392 self->muladd_func_ptr = FFT_postprocessing_ia;
393 break;
394
395 case 11:
396 self->muladd_func_ptr = FFT_postprocessing_aa;
397 break;
398
399 case 12:
400 self->muladd_func_ptr = FFT_postprocessing_revaa;
401 break;
402
403 case 20:
404 self->muladd_func_ptr = FFT_postprocessing_ireva;
405 break;
406
407 case 21:
408 self->muladd_func_ptr = FFT_postprocessing_areva;
409 break;
410
411 case 22:
412 self->muladd_func_ptr = FFT_postprocessing_revareva;
413 break;
414 }
415 }
416
417 static void
418 FFT_compute_next_data_frame(FFT *self)
419 {
420 int i;
421 MYFLT *tmp;
422 int offset = self->chnl * self->bufsize;
423 tmp = FFTMain_getSamplesBuffer((FFTMain *)self->mainSplitter);
424
425 for (i = 0; i < self->bufsize; i++)
426 {
427 self->data[i] = tmp[i + offset];
428 }
429
430 (*self->muladd_func_ptr)(self);
431 }
432
433 static int
434 FFT_traverse(FFT *self, visitproc visit, void *arg)
435 {
436 pyo_VISIT
437 Py_VISIT(self->mainSplitter);
438 return 0;
439 }
440
441 static int
442 FFT_clear(FFT *self)
443 {
444 pyo_CLEAR
445 Py_CLEAR(self->mainSplitter);
446 return 0;
447 }
448
449 static void
450 FFT_dealloc(FFT* self)
451 {
452 pyo_DEALLOC
453 FFT_clear(self);
454 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
455 Py_TYPE(self)->tp_free((PyObject*)self);
456 }
457
458 static PyObject *
459 FFT_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
460 {
461 int i;
462 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
463 FFT *self;
464 self = (FFT *)type->tp_alloc(type, 0);
465
466 self->modebuffer[0] = 0;
467 self->modebuffer[1] = 0;
468
469 INIT_OBJECT_COMMON
470 Stream_setFunctionPtr(self->stream, FFT_compute_next_data_frame);
471 self->mode_func_ptr = FFT_setProcMode;
472
473 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
474
475 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
476 Py_RETURN_NONE;
477
478 self->mainSplitter = (FFTMain *)maintmp;
479 Py_INCREF(self->mainSplitter);
480
481 if (multmp)
482 {
483 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
484 }
485
486 if (addtmp)
487 {
488 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
489 }
490
491 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
492
493 (*self->mode_func_ptr)(self);
494
495 return (PyObject *)self;
496 }
497
498 static PyObject * FFT_getServer(FFT* self) { GET_SERVER };
499 static PyObject * FFT_getStream(FFT* self) { GET_STREAM };
500 static PyObject * FFT_setMul(FFT *self, PyObject *arg) { SET_MUL };
501 static PyObject * FFT_setAdd(FFT *self, PyObject *arg) { SET_ADD };
502 static PyObject * FFT_setSub(FFT *self, PyObject *arg) { SET_SUB };
503 static PyObject * FFT_setDiv(FFT *self, PyObject *arg) { SET_DIV };
504
505 static PyObject * FFT_play(FFT *self, PyObject *args, PyObject *kwds) { PLAY };
506 static PyObject * FFT_stop(FFT *self, PyObject *args, PyObject *kwds) { STOP };
507
508 static PyObject * FFT_multiply(FFT *self, PyObject *arg) { MULTIPLY };
509 static PyObject * FFT_inplace_multiply(FFT *self, PyObject *arg) { INPLACE_MULTIPLY };
510 static PyObject * FFT_add(FFT *self, PyObject *arg) { ADD };
511 static PyObject * FFT_inplace_add(FFT *self, PyObject *arg) { INPLACE_ADD };
512 static PyObject * FFT_sub(FFT *self, PyObject *arg) { SUB };
513 static PyObject * FFT_inplace_sub(FFT *self, PyObject *arg) { INPLACE_SUB };
514 static PyObject * FFT_div(FFT *self, PyObject *arg) { DIV };
515 static PyObject * FFT_inplace_div(FFT *self, PyObject *arg) { INPLACE_DIV };
516
517 static PyMemberDef FFT_members[] =
518 {
519 {"server", T_OBJECT_EX, offsetof(FFT, server), 0, "Pyo server."},
520 {"stream", T_OBJECT_EX, offsetof(FFT, stream), 0, "Stream object."},
521 {"mul", T_OBJECT_EX, offsetof(FFT, mul), 0, "Mul factor."},
522 {"add", T_OBJECT_EX, offsetof(FFT, add), 0, "Add factor."},
523 {NULL} /* Sentinel */
524 };
525
526 static PyMethodDef FFT_methods[] =
527 {
528 {"getServer", (PyCFunction)FFT_getServer, METH_NOARGS, "Returns server object."},
529 {"_getStream", (PyCFunction)FFT_getStream, METH_NOARGS, "Returns stream object."},
530 {"play", (PyCFunction)FFT_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
531 {"stop", (PyCFunction)FFT_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
532 {"setMul", (PyCFunction)FFT_setMul, METH_O, "Sets FFT mul factor."},
533 {"setAdd", (PyCFunction)FFT_setAdd, METH_O, "Sets FFT add factor."},
534 {"setSub", (PyCFunction)FFT_setSub, METH_O, "Sets inverse add factor."},
535 {"setDiv", (PyCFunction)FFT_setDiv, METH_O, "Sets inverse mul factor."},
536 {NULL} /* Sentinel */
537 };
538
539 static PyNumberMethods FFT_as_number =
540 {
541 (binaryfunc)FFT_add, /*nb_add*/
542 (binaryfunc)FFT_sub, /*nb_subtract*/
543 (binaryfunc)FFT_multiply, /*nb_multiply*/
544 0, /*nb_remainder*/
545 0, /*nb_divmod*/
546 0, /*nb_power*/
547 0, /*nb_neg*/
548 0, /*nb_pos*/
549 0, /*(unaryfunc)array_abs,*/
550 0, /*nb_nonzero*/
551 0, /*nb_invert*/
552 0, /*nb_lshift*/
553 0, /*nb_rshift*/
554 0, /*nb_and*/
555 0, /*nb_xor*/
556 0, /*nb_or*/
557 0, /*nb_int*/
558 0, /*nb_long*/
559 0, /*nb_float*/
560 (binaryfunc)FFT_inplace_add, /*inplace_add*/
561 (binaryfunc)FFT_inplace_sub, /*inplace_subtract*/
562 (binaryfunc)FFT_inplace_multiply, /*inplace_multiply*/
563 0, /*inplace_remainder*/
564 0, /*inplace_power*/
565 0, /*inplace_lshift*/
566 0, /*inplace_rshift*/
567 0, /*inplace_and*/
568 0, /*inplace_xor*/
569 0, /*inplace_or*/
570 0, /*nb_floor_divide*/
571 (binaryfunc)FFT_div, /*nb_true_divide*/
572 0, /*nb_inplace_floor_divide*/
573 (binaryfunc)FFT_inplace_div, /*nb_inplace_true_divide*/
574 0, /* nb_index */
575 };
576
577 PyTypeObject FFTType =
578 {
579 PyVarObject_HEAD_INIT(NULL, 0)
580 "_pyo.FFT_base", /*tp_name*/
581 sizeof(FFT), /*tp_basicsize*/
582 0, /*tp_itemsize*/
583 (destructor)FFT_dealloc, /*tp_dealloc*/
584 0, /*tp_print*/
585 0, /*tp_getattr*/
586 0, /*tp_setattr*/
587 0, /*tp_as_async (tp_compare in Python 2)*/
588 0, /*tp_repr*/
589 &FFT_as_number, /*tp_as_number*/
590 0, /*tp_as_sequence*/
591 0, /*tp_as_mapping*/
592 0, /*tp_hash */
593 0, /*tp_call*/
594 0, /*tp_str*/
595 0, /*tp_getattro*/
596 0, /*tp_setattro*/
597 0, /*tp_as_buffer*/
598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
599 "FFT objects. Reads one band (real, imag or bins) from a FFT transform.", /* tp_doc */
600 (traverseproc)FFT_traverse, /* tp_traverse */
601 (inquiry)FFT_clear, /* tp_clear */
602 0, /* tp_richcompare */
603 0, /* tp_weaklistoffset */
604 0, /* tp_iter */
605 0, /* tp_iternext */
606 FFT_methods, /* tp_methods */
607 FFT_members, /* tp_members */
608 0, /* tp_getset */
609 0, /* tp_base */
610 0, /* tp_dict */
611 0, /* tp_descr_get */
612 0, /* tp_descr_set */
613 0, /* tp_dictoffset */
614 0, /* tp_init */
615 0, /* tp_alloc */
616 FFT_new, /* tp_new */
617 };
618
619 typedef struct
620 {
621 pyo_audio_HEAD
622 PyObject *inreal;
623 Stream *inreal_stream;
624 PyObject *inimag;
625 Stream *inimag_stream;
626 int size;
627 int hsize;
628 int hopsize;
629 int wintype;
630 int incount;
631 MYFLT *inframe;
632 MYFLT *outframe;
633 MYFLT *window;
634 MYFLT **twiddle;
635 //MYFLT *twiddle2;
636 int modebuffer[2];
637 int allocated;
638 } IFFT;
639
640 static void
641 IFFT_realloc_memories(IFFT *self)
642 {
643 int i, n8;
644 self->hsize = self->size / 2;
645 n8 = self->size >> 3;
646 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size * sizeof(MYFLT));
647 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size * sizeof(MYFLT));
648
649 for (i = 0; i < self->size; i++)
650 self->inframe[i] = self->outframe[i] = 0.0;
651
652 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
653
654 for (i = 0; i < 4; i++)
655 {
656 if (self->allocated)
657 {
658 PyMem_RawFree(self->twiddle[i]);
659 }
660 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
661 }
662
663 fft_compute_split_twiddle(self->twiddle, self->size);
664 //self->twiddle2 = (MYFLT *)PyMem_RawRealloc(self->twiddle2, self->size * sizeof(MYFLT));
665 //fft_compute_radix2_twiddle(self->twiddle2, self->size);
666 self->window = (MYFLT *)PyMem_RawRealloc(self->window, self->size * sizeof(MYFLT));
667 gen_window(self->window, self->size, self->wintype);
668 self->incount = -self->hopsize;
669
670 self->allocated = 1;
671 }
672
673 static void
674 IFFT_filters(IFFT *self)
675 {
676 int i, incount;
677 MYFLT data;
678 MYFLT *inreal = Stream_getData((Stream *)self->inreal_stream);
679 MYFLT *inimag = Stream_getData((Stream *)self->inimag_stream);
680
681 incount = self->incount;
682
683 for (i = 0; i < self->bufsize; i++)
684 {
685 if (incount >= 0)
686 {
687 if (incount < self->hsize)
688 {
689 self->inframe[incount] = inreal[i];
690
691 if (incount)
692 self->inframe[self->size - incount] = inimag[i];
693 }
694 else if (incount == self->hsize)
695 self->inframe[incount] = inreal[i];
696
697 data = self->outframe[incount] * self->window[incount];
698 self->data[i] = data;
699 }
700
701 incount++;
702
703 if (incount >= self->size)
704 {
705 incount -= self->size;
706 irealfft_split(self->inframe, self->outframe, self->size, self->twiddle);
707 }
708 }
709
710 /*
711 for (i=0; i<self->bufsize; i++) {
712 if (incount >= 0) {
713 if (incount < self->hsize) {
714 self->inframe[incount*2] = inreal[i];
715 self->inframe[incount*2+1] = inimag[i];
716 }
717 self->data[i] = self->outframe[incount] * self->window[incount];
718 }
719 incount++;
720 if (incount >= self->size) {
721 incount -= self->size;
722 irealfft_packed(self->inframe, self->outframe, self->size, self->twiddle2);
723 }
724 }
725 */
726 self->incount = incount;
727 }
728
729 static void IFFT_postprocessing_ii(IFFT *self) { POST_PROCESSING_II };
730 static void IFFT_postprocessing_ai(IFFT *self) { POST_PROCESSING_AI };
731 static void IFFT_postprocessing_ia(IFFT *self) { POST_PROCESSING_IA };
732 static void IFFT_postprocessing_aa(IFFT *self) { POST_PROCESSING_AA };
733 static void IFFT_postprocessing_ireva(IFFT *self) { POST_PROCESSING_IREVA };
734 static void IFFT_postprocessing_areva(IFFT *self) { POST_PROCESSING_AREVA };
735 static void IFFT_postprocessing_revai(IFFT *self) { POST_PROCESSING_REVAI };
736 static void IFFT_postprocessing_revaa(IFFT *self) { POST_PROCESSING_REVAA };
737 static void IFFT_postprocessing_revareva(IFFT *self) { POST_PROCESSING_REVAREVA };
738
739 static void
740 IFFT_setProcMode(IFFT *self)
741 {
742 int muladdmode;
743 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
744
745 self->proc_func_ptr = IFFT_filters;
746
747 switch (muladdmode)
748 {
749 case 0:
750 self->muladd_func_ptr = IFFT_postprocessing_ii;
751 break;
752
753 case 1:
754 self->muladd_func_ptr = IFFT_postprocessing_ai;
755 break;
756
757 case 2:
758 self->muladd_func_ptr = IFFT_postprocessing_revai;
759 break;
760
761 case 10:
762 self->muladd_func_ptr = IFFT_postprocessing_ia;
763 break;
764
765 case 11:
766 self->muladd_func_ptr = IFFT_postprocessing_aa;
767 break;
768
769 case 12:
770 self->muladd_func_ptr = IFFT_postprocessing_revaa;
771 break;
772
773 case 20:
774 self->muladd_func_ptr = IFFT_postprocessing_ireva;
775 break;
776
777 case 21:
778 self->muladd_func_ptr = IFFT_postprocessing_areva;
779 break;
780
781 case 22:
782 self->muladd_func_ptr = IFFT_postprocessing_revareva;
783 break;
784 }
785 }
786
787 static void
788 IFFT_compute_next_data_frame(IFFT *self)
789 {
790 (*self->proc_func_ptr)(self);
791 (*self->muladd_func_ptr)(self);
792 }
793
794 static int
795 IFFT_traverse(IFFT *self, visitproc visit, void *arg)
796 {
797 pyo_VISIT
798 Py_VISIT(self->inreal);
799 Py_VISIT(self->inimag);
800 return 0;
801 }
802
803 static int
804 IFFT_clear(IFFT *self)
805 {
806 pyo_CLEAR
807 Py_CLEAR(self->inreal);
808 Py_CLEAR(self->inimag);
809 return 0;
810 }
811
812 static void
813 IFFT_dealloc(IFFT* self)
814 {
815 int i;
816 pyo_DEALLOC
817 PyMem_RawFree(self->inframe);
818 PyMem_RawFree(self->outframe);
819 PyMem_RawFree(self->window);
820
821 for (i = 0; i < 4; i++)
822 {
823 PyMem_RawFree(self->twiddle[i]);
824 }
825
826 PyMem_RawFree(self->twiddle);
827 //free(self->twiddle2);
828 IFFT_clear(self);
829 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
830 Py_TYPE(self)->tp_free((PyObject*)self);
831 }
832
833 static PyObject *
834 IFFT_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
835 {
836 int i;
837 PyObject *inrealtmp, *inreal_streamtmp, *inimagtmp, *inimag_streamtmp, *multmp = NULL, *addtmp = NULL;
838 IFFT *self;
839 self = (IFFT *)type->tp_alloc(type, 0);
840
841 self->size = 1024;
842 self->wintype = 2;
843 self->modebuffer[0] = 0;
844 self->modebuffer[1] = 0;
845 self->allocated = 0;
846
847 INIT_OBJECT_COMMON
848 Stream_setFunctionPtr(self->stream, IFFT_compute_next_data_frame);
849 self->mode_func_ptr = IFFT_setProcMode;
850
851 static char *kwlist[] = {"inreal", "inimag", "size", "hopsize", "wintype", "mul", "add", NULL};
852
853 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|iiiOO", kwlist, &inrealtmp, &inimagtmp, &self->size, &self->hopsize, &self->wintype, &multmp, &addtmp))
854 Py_RETURN_NONE;
855
856 self->inimag = inimagtmp;
857 Py_INCREF(self->inimag);
858 inimag_streamtmp = PyObject_CallMethod((PyObject *)self->inimag, "_getStream", NULL);
859 self->inimag_stream = (Stream *)inimag_streamtmp;
860 Py_INCREF(self->inimag_stream);
861
862 self->inreal = inrealtmp;
863 Py_INCREF(self->inreal);
864 inreal_streamtmp = PyObject_CallMethod((PyObject *)self->inreal, "_getStream", NULL);
865 self->inreal_stream = (Stream *)inreal_streamtmp;
866 Py_INCREF(self->inreal_stream);
867
868 if (multmp)
869 {
870 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
871 }
872
873 if (addtmp)
874 {
875 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
876 }
877
878 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
879
880 IFFT_realloc_memories(self);
881
882 (*self->mode_func_ptr)(self);
883
884 return (PyObject *)self;
885 }
886
887 static PyObject * IFFT_getServer(IFFT* self) { GET_SERVER };
888 static PyObject * IFFT_getStream(IFFT* self) { GET_STREAM };
889 static PyObject * IFFT_setMul(IFFT *self, PyObject *arg) { SET_MUL };
890 static PyObject * IFFT_setAdd(IFFT *self, PyObject *arg) { SET_ADD };
891 static PyObject * IFFT_setSub(IFFT *self, PyObject *arg) { SET_SUB };
892 static PyObject * IFFT_setDiv(IFFT *self, PyObject *arg) { SET_DIV };
893
894 static PyObject * IFFT_play(IFFT *self, PyObject *args, PyObject *kwds) { PLAY };
895 static PyObject * IFFT_out(IFFT *self, PyObject *args, PyObject *kwds) { OUT };
896 static PyObject * IFFT_stop(IFFT *self, PyObject *args, PyObject *kwds) { STOP };
897
898 static PyObject * IFFT_multiply(IFFT *self, PyObject *arg) { MULTIPLY };
899 static PyObject * IFFT_inplace_multiply(IFFT *self, PyObject *arg) { INPLACE_MULTIPLY };
900 static PyObject * IFFT_add(IFFT *self, PyObject *arg) { ADD };
901 static PyObject * IFFT_inplace_add(IFFT *self, PyObject *arg) { INPLACE_ADD };
902 static PyObject * IFFT_sub(IFFT *self, PyObject *arg) { SUB };
903 static PyObject * IFFT_inplace_sub(IFFT *self, PyObject *arg) { INPLACE_SUB };
904 static PyObject * IFFT_div(IFFT *self, PyObject *arg) { DIV };
905 static PyObject * IFFT_inplace_div(IFFT *self, PyObject *arg) { INPLACE_DIV };
906
907 static PyObject *
908 IFFT_setSize(IFFT *self, PyObject *args, PyObject *kwds)
909 {
910 int size, hopsize;
911
912 static char *kwlist[] = {"size", "hopsize", NULL};
913
914 if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwlist, &size, &hopsize))
915 {
916 Py_RETURN_NONE;
917 }
918
919 if (isPowerOfTwo(size))
920 {
921 self->size = size;
922 self->hopsize = hopsize;
923 IFFT_realloc_memories(self);
924 }
925 else
926 PySys_WriteStdout("IFFT size must be a power of two!\n");
927
928 Py_RETURN_NONE;
929 }
930
931 static PyObject *
932 IFFT_setWinType(IFFT *self, PyObject *arg)
933 {
934 if (PyLong_Check(arg))
935 {
936 self->wintype = PyLong_AsLong(arg);
937 gen_window(self->window, self->size, self->wintype);
938 }
939
940 Py_RETURN_NONE;
941 }
942
943 static PyMemberDef IFFT_members[] =
944 {
945 {"server", T_OBJECT_EX, offsetof(IFFT, server), 0, "Pyo server."},
946 {"stream", T_OBJECT_EX, offsetof(IFFT, stream), 0, "Stream object."},
947 {"inreal", T_OBJECT_EX, offsetof(IFFT, inreal), 0, "Real input."},
948 {"inimag", T_OBJECT_EX, offsetof(IFFT, inimag), 0, "Imaginary input."},
949 {"mul", T_OBJECT_EX, offsetof(IFFT, mul), 0, "Mul factor."},
950 {"add", T_OBJECT_EX, offsetof(IFFT, add), 0, "Add factor."},
951 {NULL} /* Sentinel */
952 };
953
954 static PyMethodDef IFFT_methods[] =
955 {
956 {"getServer", (PyCFunction)IFFT_getServer, METH_NOARGS, "Returns server object."},
957 {"_getStream", (PyCFunction)IFFT_getStream, METH_NOARGS, "Returns stream object."},
958 {"play", (PyCFunction)IFFT_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
959 {"out", (PyCFunction)IFFT_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
960 {"stop", (PyCFunction)IFFT_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
961 {"setSize", (PyCFunction)IFFT_setSize, METH_VARARGS | METH_KEYWORDS, "Sets a new IFFT size."},
962 {"setWinType", (PyCFunction)IFFT_setWinType, METH_O, "Sets a new window."},
963 {"setMul", (PyCFunction)IFFT_setMul, METH_O, "Sets oscillator mul factor."},
964 {"setAdd", (PyCFunction)IFFT_setAdd, METH_O, "Sets oscillator add factor."},
965 {"setSub", (PyCFunction)IFFT_setSub, METH_O, "Sets inverse add factor."},
966 {"setDiv", (PyCFunction)IFFT_setDiv, METH_O, "Sets inverse mul factor."},
967 {NULL} /* Sentinel */
968 };
969
970 static PyNumberMethods IFFT_as_number =
971 {
972 (binaryfunc)IFFT_add, /*nb_add*/
973 (binaryfunc)IFFT_sub, /*nb_subtract*/
974 (binaryfunc)IFFT_multiply, /*nb_multiply*/
975 0, /*nb_remainder*/
976 0, /*nb_divmod*/
977 0, /*nb_power*/
978 0, /*nb_neg*/
979 0, /*nb_pos*/
980 0, /*(unaryfunc)array_abs,*/
981 0, /*nb_nonzero*/
982 0, /*nb_invert*/
983 0, /*nb_lshift*/
984 0, /*nb_rshift*/
985 0, /*nb_and*/
986 0, /*nb_xor*/
987 0, /*nb_or*/
988 0, /*nb_int*/
989 0, /*nb_long*/
990 0, /*nb_float*/
991 (binaryfunc)IFFT_inplace_add, /*inplace_add*/
992 (binaryfunc)IFFT_inplace_sub, /*inplace_subtract*/
993 (binaryfunc)IFFT_inplace_multiply, /*inplace_multiply*/
994 0, /*inplace_remainder*/
995 0, /*inplace_power*/
996 0, /*inplace_lshift*/
997 0, /*inplace_rshift*/
998 0, /*inplace_and*/
999 0, /*inplace_xor*/
1000 0, /*inplace_or*/
1001 0, /*nb_floor_divide*/
1002 (binaryfunc)IFFT_div, /*nb_true_divide*/
1003 0, /*nb_inplace_floor_divide*/
1004 (binaryfunc)IFFT_inplace_div, /*nb_inplace_true_divide*/
1005 0, /* nb_index */
1006 };
1007
1008 PyTypeObject IFFTType =
1009 {
1010 PyVarObject_HEAD_INIT(NULL, 0)
1011 "_pyo.IFFT_base", /*tp_name*/
1012 sizeof(IFFT), /*tp_basicsize*/
1013 0, /*tp_itemsize*/
1014 (destructor)IFFT_dealloc, /*tp_dealloc*/
1015 0, /*tp_print*/
1016 0, /*tp_getattr*/
1017 0, /*tp_setattr*/
1018 0, /*tp_as_async (tp_compare in Python 2)*/
1019 0, /*tp_repr*/
1020 &IFFT_as_number, /*tp_as_number*/
1021 0, /*tp_as_sequence*/
1022 0, /*tp_as_mapping*/
1023 0, /*tp_hash */
1024 0, /*tp_call*/
1025 0, /*tp_str*/
1026 0, /*tp_getattro*/
1027 0, /*tp_setattro*/
1028 0, /*tp_as_buffer*/
1029 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1030 "IFFT objects. Synthesize audio from an FFT real and imaginary parts.", /* tp_doc */
1031 (traverseproc)IFFT_traverse, /* tp_traverse */
1032 (inquiry)IFFT_clear, /* tp_clear */
1033 0, /* tp_richcompare */
1034 0, /* tp_weaklistoffset */
1035 0, /* tp_iter */
1036 0, /* tp_iternext */
1037 IFFT_methods, /* tp_methods */
1038 IFFT_members, /* tp_members */
1039 0, /* tp_getset */
1040 0, /* tp_base */
1041 0, /* tp_dict */
1042 0, /* tp_descr_get */
1043 0, /* tp_descr_set */
1044 0, /* tp_dictoffset */
1045 0, /* tp_init */
1046 0, /* tp_alloc */
1047 IFFT_new, /* tp_new */
1048 };
1049
1050 typedef struct
1051 {
1052 pyo_audio_HEAD
1053 PyObject *input; /* real input */
1054 Stream *input_stream;
1055 PyObject *input2; /* imag input */
1056 Stream *input2_stream;
1057 int modebuffer[2];
1058 int chnl; // 0 = mag, 1 = ang
1059 } CarToPol;
1060
1061 static void
1062 CarToPol_generate(CarToPol *self)
1063 {
1064 int i;
1065 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1066 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
1067
1068 if (self->chnl == 0)
1069 {
1070 for (i = 0; i < self->bufsize; i++)
1071 {
1072 self->data[i] = MYSQRT(in[i] * in[i] + in2[i] * in2[i]);
1073 }
1074 }
1075 else
1076 {
1077 for (i = 0; i < self->bufsize; i++)
1078 {
1079 self->data[i] = MYATAN2(in2[i], in[i]);
1080 }
1081 }
1082 }
1083
1084 static void CarToPol_postprocessing_ii(CarToPol *self) { POST_PROCESSING_II };
1085 static void CarToPol_postprocessing_ai(CarToPol *self) { POST_PROCESSING_AI };
1086 static void CarToPol_postprocessing_ia(CarToPol *self) { POST_PROCESSING_IA };
1087 static void CarToPol_postprocessing_aa(CarToPol *self) { POST_PROCESSING_AA };
1088 static void CarToPol_postprocessing_ireva(CarToPol *self) { POST_PROCESSING_IREVA };
1089 static void CarToPol_postprocessing_areva(CarToPol *self) { POST_PROCESSING_AREVA };
1090 static void CarToPol_postprocessing_revai(CarToPol *self) { POST_PROCESSING_REVAI };
1091 static void CarToPol_postprocessing_revaa(CarToPol *self) { POST_PROCESSING_REVAA };
1092 static void CarToPol_postprocessing_revareva(CarToPol *self) { POST_PROCESSING_REVAREVA };
1093
1094 static void
1095 CarToPol_setProcMode(CarToPol *self)
1096 {
1097 int muladdmode;
1098 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1099
1100 self->proc_func_ptr = CarToPol_generate;
1101
1102 switch (muladdmode)
1103 {
1104 case 0:
1105 self->muladd_func_ptr = CarToPol_postprocessing_ii;
1106 break;
1107
1108 case 1:
1109 self->muladd_func_ptr = CarToPol_postprocessing_ai;
1110 break;
1111
1112 case 2:
1113 self->muladd_func_ptr = CarToPol_postprocessing_revai;
1114 break;
1115
1116 case 10:
1117 self->muladd_func_ptr = CarToPol_postprocessing_ia;
1118 break;
1119
1120 case 11:
1121 self->muladd_func_ptr = CarToPol_postprocessing_aa;
1122 break;
1123
1124 case 12:
1125 self->muladd_func_ptr = CarToPol_postprocessing_revaa;
1126 break;
1127
1128 case 20:
1129 self->muladd_func_ptr = CarToPol_postprocessing_ireva;
1130 break;
1131
1132 case 21:
1133 self->muladd_func_ptr = CarToPol_postprocessing_areva;
1134 break;
1135
1136 case 22:
1137 self->muladd_func_ptr = CarToPol_postprocessing_revareva;
1138 break;
1139 }
1140 }
1141
1142 static void
1143 CarToPol_compute_next_data_frame(CarToPol *self)
1144 {
1145 (*self->proc_func_ptr)(self);
1146 (*self->muladd_func_ptr)(self);
1147 }
1148
1149 static int
1150 CarToPol_traverse(CarToPol *self, visitproc visit, void *arg)
1151 {
1152 pyo_VISIT
1153 Py_VISIT(self->input);
1154 Py_VISIT(self->input2);
1155 return 0;
1156 }
1157
1158 static int
1159 CarToPol_clear(CarToPol *self)
1160 {
1161 pyo_CLEAR
1162 Py_CLEAR(self->input);
1163 Py_CLEAR(self->input2);
1164 return 0;
1165 }
1166
1167 static void
1168 CarToPol_dealloc(CarToPol* self)
1169 {
1170 pyo_DEALLOC
1171 CarToPol_clear(self);
1172 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1173 Py_TYPE(self)->tp_free((PyObject*)self);
1174 }
1175
1176 static PyObject *
1177 CarToPol_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1178 {
1179 int i;
1180 PyObject *inputtmp, *input_streamtmp, *input2tmp, *input2_streamtmp, *multmp = NULL, *addtmp = NULL;
1181 CarToPol *self;
1182 self = (CarToPol *)type->tp_alloc(type, 0);
1183
1184 self->modebuffer[0] = 0;
1185 self->modebuffer[1] = 0;
1186
1187 INIT_OBJECT_COMMON
1188 Stream_setFunctionPtr(self->stream, CarToPol_compute_next_data_frame);
1189 self->mode_func_ptr = CarToPol_setProcMode;
1190
1191 static char *kwlist[] = {"input", "input2", "chnl", "mul", "add", NULL};
1192
1193 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOi|OO", kwlist, &inputtmp, &input2tmp, &self->chnl, &multmp, &addtmp))
1194 Py_RETURN_NONE;
1195
1196 INIT_INPUT_STREAM
1197
1198 self->input2 = input2tmp;
1199 Py_INCREF(self->input2);
1200 input2_streamtmp = PyObject_CallMethod((PyObject *)self->input2, "_getStream", NULL);
1201 self->input2_stream = (Stream *)input2_streamtmp;
1202 Py_INCREF(self->input2_stream);
1203
1204 if (multmp)
1205 {
1206 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1207 }
1208
1209 if (addtmp)
1210 {
1211 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1212 }
1213
1214 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1215
1216 (*self->mode_func_ptr)(self);
1217
1218 return (PyObject *)self;
1219 }
1220
1221 static PyObject * CarToPol_getServer(CarToPol* self) { GET_SERVER };
1222 static PyObject * CarToPol_getStream(CarToPol* self) { GET_STREAM };
1223 static PyObject * CarToPol_setMul(CarToPol *self, PyObject *arg) { SET_MUL };
1224 static PyObject * CarToPol_setAdd(CarToPol *self, PyObject *arg) { SET_ADD };
1225 static PyObject * CarToPol_setSub(CarToPol *self, PyObject *arg) { SET_SUB };
1226 static PyObject * CarToPol_setDiv(CarToPol *self, PyObject *arg) { SET_DIV };
1227
1228 static PyObject * CarToPol_play(CarToPol *self, PyObject *args, PyObject *kwds) { PLAY };
1229 static PyObject * CarToPol_stop(CarToPol *self, PyObject *args, PyObject *kwds) { STOP };
1230
1231 static PyObject * CarToPol_multiply(CarToPol *self, PyObject *arg) { MULTIPLY };
1232 static PyObject * CarToPol_inplace_multiply(CarToPol *self, PyObject *arg) { INPLACE_MULTIPLY };
1233 static PyObject * CarToPol_add(CarToPol *self, PyObject *arg) { ADD };
1234 static PyObject * CarToPol_inplace_add(CarToPol *self, PyObject *arg) { INPLACE_ADD };
1235 static PyObject * CarToPol_sub(CarToPol *self, PyObject *arg) { SUB };
1236 static PyObject * CarToPol_inplace_sub(CarToPol *self, PyObject *arg) { INPLACE_SUB };
1237 static PyObject * CarToPol_div(CarToPol *self, PyObject *arg) { DIV };
1238 static PyObject * CarToPol_inplace_div(CarToPol *self, PyObject *arg) { INPLACE_DIV };
1239
1240 static PyMemberDef CarToPol_members[] =
1241 {
1242 {"server", T_OBJECT_EX, offsetof(CarToPol, server), 0, "Pyo server."},
1243 {"stream", T_OBJECT_EX, offsetof(CarToPol, stream), 0, "Stream object."},
1244 {"input", T_OBJECT_EX, offsetof(CarToPol, input), 0, "Real sound object."},
1245 {"input2", T_OBJECT_EX, offsetof(CarToPol, input2), 0, "Imaginary sound object."},
1246 {"mul", T_OBJECT_EX, offsetof(CarToPol, mul), 0, "Mul factor."},
1247 {"add", T_OBJECT_EX, offsetof(CarToPol, add), 0, "Add factor."},
1248 {NULL} /* Sentinel */
1249 };
1250
1251 static PyMethodDef CarToPol_methods[] =
1252 {
1253 {"getServer", (PyCFunction)CarToPol_getServer, METH_NOARGS, "Returns server object."},
1254 {"_getStream", (PyCFunction)CarToPol_getStream, METH_NOARGS, "Returns stream object."},
1255 {"play", (PyCFunction)CarToPol_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1256 {"stop", (PyCFunction)CarToPol_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1257 {"setMul", (PyCFunction)CarToPol_setMul, METH_O, "Sets CarToPol mul factor."},
1258 {"setAdd", (PyCFunction)CarToPol_setAdd, METH_O, "Sets CarToPol add factor."},
1259 {"setSub", (PyCFunction)CarToPol_setSub, METH_O, "Sets inverse add factor."},
1260 {"setDiv", (PyCFunction)CarToPol_setDiv, METH_O, "Sets inverse mul factor."},
1261 {NULL} /* Sentinel */
1262 };
1263
1264 static PyNumberMethods CarToPol_as_number =
1265 {
1266 (binaryfunc)CarToPol_add, /*nb_add*/
1267 (binaryfunc)CarToPol_sub, /*nb_subtract*/
1268 (binaryfunc)CarToPol_multiply, /*nb_multiply*/
1269 0, /*nb_remainder*/
1270 0, /*nb_divmod*/
1271 0, /*nb_power*/
1272 0, /*nb_neg*/
1273 0, /*nb_pos*/
1274 0, /*(unaryfunc)array_abs,*/
1275 0, /*nb_nonzero*/
1276 0, /*nb_invert*/
1277 0, /*nb_lshift*/
1278 0, /*nb_rshift*/
1279 0, /*nb_and*/
1280 0, /*nb_xor*/
1281 0, /*nb_or*/
1282 0, /*nb_int*/
1283 0, /*nb_long*/
1284 0, /*nb_float*/
1285 (binaryfunc)CarToPol_inplace_add, /*inplace_add*/
1286 (binaryfunc)CarToPol_inplace_sub, /*inplace_subtract*/
1287 (binaryfunc)CarToPol_inplace_multiply, /*inplace_multiply*/
1288 0, /*inplace_remainder*/
1289 0, /*inplace_power*/
1290 0, /*inplace_lshift*/
1291 0, /*inplace_rshift*/
1292 0, /*inplace_and*/
1293 0, /*inplace_xor*/
1294 0, /*inplace_or*/
1295 0, /*nb_floor_divide*/
1296 (binaryfunc)CarToPol_div, /*nb_true_divide*/
1297 0, /*nb_inplace_floor_divide*/
1298 (binaryfunc)CarToPol_inplace_div, /*nb_inplace_true_divide*/
1299 0, /* nb_index */
1300 };
1301
1302 PyTypeObject CarToPolType =
1303 {
1304 PyVarObject_HEAD_INIT(NULL, 0)
1305 "_pyo.CarToPol_base", /*tp_name*/
1306 sizeof(CarToPol), /*tp_basicsize*/
1307 0, /*tp_itemsize*/
1308 (destructor)CarToPol_dealloc, /*tp_dealloc*/
1309 0, /*tp_print*/
1310 0, /*tp_getattr*/
1311 0, /*tp_setattr*/
1312 0, /*tp_as_async (tp_compare in Python 2)*/
1313 0, /*tp_repr*/
1314 &CarToPol_as_number, /*tp_as_number*/
1315 0, /*tp_as_sequence*/
1316 0, /*tp_as_mapping*/
1317 0, /*tp_hash */
1318 0, /*tp_call*/
1319 0, /*tp_str*/
1320 0, /*tp_getattro*/
1321 0, /*tp_setattro*/
1322 0, /*tp_as_buffer*/
1323 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1324 "CarToPol objects. Cartesian to polar transform.", /* tp_doc */
1325 (traverseproc)CarToPol_traverse, /* tp_traverse */
1326 (inquiry)CarToPol_clear, /* tp_clear */
1327 0, /* tp_richcompare */
1328 0, /* tp_weaklistoffset */
1329 0, /* tp_iter */
1330 0, /* tp_iternext */
1331 CarToPol_methods, /* tp_methods */
1332 CarToPol_members, /* tp_members */
1333 0, /* tp_getset */
1334 0, /* tp_base */
1335 0, /* tp_dict */
1336 0, /* tp_descr_get */
1337 0, /* tp_descr_set */
1338 0, /* tp_dictoffset */
1339 0, /* tp_init */
1340 0, /* tp_alloc */
1341 CarToPol_new, /* tp_new */
1342 };
1343
1344 typedef struct
1345 {
1346 pyo_audio_HEAD
1347 PyObject *input; /* mag input */
1348 Stream *input_stream;
1349 PyObject *input2; /* ang input */
1350 Stream *input2_stream;
1351 int modebuffer[2];
1352 int chnl; // 0 = real, 1 = imag
1353 } PolToCar;
1354
1355 static void
1356 PolToCar_generate(PolToCar *self)
1357 {
1358 int i;
1359 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1360 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
1361
1362 if (self->chnl == 0)
1363 {
1364 for (i = 0; i < self->bufsize; i++)
1365 {
1366 self->data[i] = in[i] * MYCOS(in2[i]);
1367 }
1368 }
1369 else
1370 {
1371 for (i = 0; i < self->bufsize; i++)
1372 {
1373 self->data[i] = in[i] * MYSIN(in2[i]);
1374 }
1375 }
1376 }
1377
1378 static void PolToCar_postprocessing_ii(PolToCar *self) { POST_PROCESSING_II };
1379 static void PolToCar_postprocessing_ai(PolToCar *self) { POST_PROCESSING_AI };
1380 static void PolToCar_postprocessing_ia(PolToCar *self) { POST_PROCESSING_IA };
1381 static void PolToCar_postprocessing_aa(PolToCar *self) { POST_PROCESSING_AA };
1382 static void PolToCar_postprocessing_ireva(PolToCar *self) { POST_PROCESSING_IREVA };
1383 static void PolToCar_postprocessing_areva(PolToCar *self) { POST_PROCESSING_AREVA };
1384 static void PolToCar_postprocessing_revai(PolToCar *self) { POST_PROCESSING_REVAI };
1385 static void PolToCar_postprocessing_revaa(PolToCar *self) { POST_PROCESSING_REVAA };
1386 static void PolToCar_postprocessing_revareva(PolToCar *self) { POST_PROCESSING_REVAREVA };
1387
1388 static void
1389 PolToCar_setProcMode(PolToCar *self)
1390 {
1391 int muladdmode;
1392 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1393
1394 self->proc_func_ptr = PolToCar_generate;
1395
1396 switch (muladdmode)
1397 {
1398 case 0:
1399 self->muladd_func_ptr = PolToCar_postprocessing_ii;
1400 break;
1401
1402 case 1:
1403 self->muladd_func_ptr = PolToCar_postprocessing_ai;
1404 break;
1405
1406 case 2:
1407 self->muladd_func_ptr = PolToCar_postprocessing_revai;
1408 break;
1409
1410 case 10:
1411 self->muladd_func_ptr = PolToCar_postprocessing_ia;
1412 break;
1413
1414 case 11:
1415 self->muladd_func_ptr = PolToCar_postprocessing_aa;
1416 break;
1417
1418 case 12:
1419 self->muladd_func_ptr = PolToCar_postprocessing_revaa;
1420 break;
1421
1422 case 20:
1423 self->muladd_func_ptr = PolToCar_postprocessing_ireva;
1424 break;
1425
1426 case 21:
1427 self->muladd_func_ptr = PolToCar_postprocessing_areva;
1428 break;
1429
1430 case 22:
1431 self->muladd_func_ptr = PolToCar_postprocessing_revareva;
1432 break;
1433 }
1434 }
1435
1436 static void
1437 PolToCar_compute_next_data_frame(PolToCar *self)
1438 {
1439 (*self->proc_func_ptr)(self);
1440 (*self->muladd_func_ptr)(self);
1441 }
1442
1443 static int
1444 PolToCar_traverse(PolToCar *self, visitproc visit, void *arg)
1445 {
1446 pyo_VISIT
1447 Py_VISIT(self->input);
1448 Py_VISIT(self->input2);
1449 return 0;
1450 }
1451
1452 static int
1453 PolToCar_clear(PolToCar *self)
1454 {
1455 pyo_CLEAR
1456 Py_CLEAR(self->input);
1457 Py_CLEAR(self->input2);
1458 return 0;
1459 }
1460
1461 static void
1462 PolToCar_dealloc(PolToCar* self)
1463 {
1464 pyo_DEALLOC
1465 PolToCar_clear(self);
1466 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1467 Py_TYPE(self)->tp_free((PyObject*)self);
1468 }
1469
1470 static PyObject *
1471 PolToCar_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1472 {
1473 int i;
1474 PyObject *inputtmp, *input_streamtmp, *input2tmp, *input2_streamtmp, *multmp = NULL, *addtmp = NULL;
1475 PolToCar *self;
1476 self = (PolToCar *)type->tp_alloc(type, 0);
1477
1478 self->modebuffer[0] = 0;
1479 self->modebuffer[1] = 0;
1480
1481 INIT_OBJECT_COMMON
1482 Stream_setFunctionPtr(self->stream, PolToCar_compute_next_data_frame);
1483 self->mode_func_ptr = PolToCar_setProcMode;
1484
1485 static char *kwlist[] = {"input", "input2", "chnl", "mul", "add", NULL};
1486
1487 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOi|OO", kwlist, &inputtmp, &input2tmp, &self->chnl, &multmp, &addtmp))
1488 Py_RETURN_NONE;
1489
1490 INIT_INPUT_STREAM
1491
1492 self->input2 = input2tmp;
1493 Py_INCREF(self->input2);
1494 input2_streamtmp = PyObject_CallMethod((PyObject *)self->input2, "_getStream", NULL);
1495 self->input2_stream = (Stream *)input2_streamtmp;
1496 Py_INCREF(self->input2_stream);
1497
1498 if (multmp)
1499 {
1500 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1501 }
1502
1503 if (addtmp)
1504 {
1505 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1506 }
1507
1508 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1509
1510 (*self->mode_func_ptr)(self);
1511
1512 return (PyObject *)self;
1513 }
1514
1515 static PyObject * PolToCar_getServer(PolToCar* self) { GET_SERVER };
1516 static PyObject * PolToCar_getStream(PolToCar* self) { GET_STREAM };
1517 static PyObject * PolToCar_setMul(PolToCar *self, PyObject *arg) { SET_MUL };
1518 static PyObject * PolToCar_setAdd(PolToCar *self, PyObject *arg) { SET_ADD };
1519 static PyObject * PolToCar_setSub(PolToCar *self, PyObject *arg) { SET_SUB };
1520 static PyObject * PolToCar_setDiv(PolToCar *self, PyObject *arg) { SET_DIV };
1521
1522 static PyObject * PolToCar_play(PolToCar *self, PyObject *args, PyObject *kwds) { PLAY };
1523 static PyObject * PolToCar_stop(PolToCar *self, PyObject *args, PyObject *kwds) { STOP };
1524
1525 static PyObject * PolToCar_multiply(PolToCar *self, PyObject *arg) { MULTIPLY };
1526 static PyObject * PolToCar_inplace_multiply(PolToCar *self, PyObject *arg) { INPLACE_MULTIPLY };
1527 static PyObject * PolToCar_add(PolToCar *self, PyObject *arg) { ADD };
1528 static PyObject * PolToCar_inplace_add(PolToCar *self, PyObject *arg) { INPLACE_ADD };
1529 static PyObject * PolToCar_sub(PolToCar *self, PyObject *arg) { SUB };
1530 static PyObject * PolToCar_inplace_sub(PolToCar *self, PyObject *arg) { INPLACE_SUB };
1531 static PyObject * PolToCar_div(PolToCar *self, PyObject *arg) { DIV };
1532 static PyObject * PolToCar_inplace_div(PolToCar *self, PyObject *arg) { INPLACE_DIV };
1533
1534 static PyMemberDef PolToCar_members[] =
1535 {
1536 {"server", T_OBJECT_EX, offsetof(PolToCar, server), 0, "Pyo server."},
1537 {"stream", T_OBJECT_EX, offsetof(PolToCar, stream), 0, "Stream object."},
1538 {"input", T_OBJECT_EX, offsetof(PolToCar, input), 0, "Magnitude sound object."},
1539 {"input2", T_OBJECT_EX, offsetof(PolToCar, input2), 0, "Angle sound object."},
1540 {"mul", T_OBJECT_EX, offsetof(PolToCar, mul), 0, "Mul factor."},
1541 {"add", T_OBJECT_EX, offsetof(PolToCar, add), 0, "Add factor."},
1542 {NULL} /* Sentinel */
1543 };
1544
1545 static PyMethodDef PolToCar_methods[] =
1546 {
1547 {"getServer", (PyCFunction)PolToCar_getServer, METH_NOARGS, "Returns server object."},
1548 {"_getStream", (PyCFunction)PolToCar_getStream, METH_NOARGS, "Returns stream object."},
1549 {"play", (PyCFunction)PolToCar_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1550 {"stop", (PyCFunction)PolToCar_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1551 {"setMul", (PyCFunction)PolToCar_setMul, METH_O, "Sets PolToCar mul factor."},
1552 {"setAdd", (PyCFunction)PolToCar_setAdd, METH_O, "Sets PolToCar add factor."},
1553 {"setSub", (PyCFunction)PolToCar_setSub, METH_O, "Sets inverse add factor."},
1554 {"setDiv", (PyCFunction)PolToCar_setDiv, METH_O, "Sets inverse mul factor."},
1555 {NULL} /* Sentinel */
1556 };
1557
1558 static PyNumberMethods PolToCar_as_number =
1559 {
1560 (binaryfunc)PolToCar_add, /*nb_add*/
1561 (binaryfunc)PolToCar_sub, /*nb_subtract*/
1562 (binaryfunc)PolToCar_multiply, /*nb_multiply*/
1563 0, /*nb_remainder*/
1564 0, /*nb_divmod*/
1565 0, /*nb_power*/
1566 0, /*nb_neg*/
1567 0, /*nb_pos*/
1568 0, /*(unaryfunc)array_abs,*/
1569 0, /*nb_nonzero*/
1570 0, /*nb_invert*/
1571 0, /*nb_lshift*/
1572 0, /*nb_rshift*/
1573 0, /*nb_and*/
1574 0, /*nb_xor*/
1575 0, /*nb_or*/
1576 0, /*nb_int*/
1577 0, /*nb_long*/
1578 0, /*nb_float*/
1579 (binaryfunc)PolToCar_inplace_add, /*inplace_add*/
1580 (binaryfunc)PolToCar_inplace_sub, /*inplace_subtract*/
1581 (binaryfunc)PolToCar_inplace_multiply, /*inplace_multiply*/
1582 0, /*inplace_remainder*/
1583 0, /*inplace_power*/
1584 0, /*inplace_lshift*/
1585 0, /*inplace_rshift*/
1586 0, /*inplace_and*/
1587 0, /*inplace_xor*/
1588 0, /*inplace_or*/
1589 0, /*nb_floor_divide*/
1590 (binaryfunc)PolToCar_div, /*nb_true_divide*/
1591 0, /*nb_inplace_floor_divide*/
1592 (binaryfunc)PolToCar_inplace_div, /*nb_inplace_true_divide*/
1593 0, /* nb_index */
1594 };
1595
1596 PyTypeObject PolToCarType =
1597 {
1598 PyVarObject_HEAD_INIT(NULL, 0)
1599 "_pyo.PolToCar_base", /*tp_name*/
1600 sizeof(PolToCar), /*tp_basicsize*/
1601 0, /*tp_itemsize*/
1602 (destructor)PolToCar_dealloc, /*tp_dealloc*/
1603 0, /*tp_print*/
1604 0, /*tp_getattr*/
1605 0, /*tp_setattr*/
1606 0, /*tp_as_async (tp_compare in Python 2)*/
1607 0, /*tp_repr*/
1608 &PolToCar_as_number, /*tp_as_number*/
1609 0, /*tp_as_sequence*/
1610 0, /*tp_as_mapping*/
1611 0, /*tp_hash */
1612 0, /*tp_call*/
1613 0, /*tp_str*/
1614 0, /*tp_getattro*/
1615 0, /*tp_setattro*/
1616 0, /*tp_as_buffer*/
1617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1618 "PolToCar objects. Polar to cartesian transform.", /* tp_doc */
1619 (traverseproc)PolToCar_traverse, /* tp_traverse */
1620 (inquiry)PolToCar_clear, /* tp_clear */
1621 0, /* tp_richcompare */
1622 0, /* tp_weaklistoffset */
1623 0, /* tp_iter */
1624 0, /* tp_iternext */
1625 PolToCar_methods, /* tp_methods */
1626 PolToCar_members, /* tp_members */
1627 0, /* tp_getset */
1628 0, /* tp_base */
1629 0, /* tp_dict */
1630 0, /* tp_descr_get */
1631 0, /* tp_descr_set */
1632 0, /* tp_dictoffset */
1633 0, /* tp_init */
1634 0, /* tp_alloc */
1635 PolToCar_new, /* tp_new */
1636 };
1637
1638 typedef struct
1639 {
1640 pyo_audio_HEAD
1641 PyObject *input;
1642 int inputSize;
1643 int modebuffer[2];
1644 int frameSize;
1645 int overlaps;
1646 int hopsize;
1647 int count;
1648 MYFLT **frameBuffer;
1649 MYFLT *buffer_streams;
1650 } FrameDeltaMain;
1651
1652 static void
1653 FrameDeltaMain_generate(FrameDeltaMain *self)
1654 {
1655 int i, j, which, where;
1656 MYFLT curPhase, lastPhase, diff;
1657
1658 MYFLT ins[self->overlaps][self->bufsize];
1659
1660 for (j = 0; j < self->overlaps; j++)
1661 {
1662 MYFLT *in = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->input, j), "_getStream", NULL));
1663
1664 for (i = 0; i < self->bufsize; i++)
1665 {
1666 ins[j][i] = in[i];
1667 }
1668 }
1669
1670 for (i = 0; i < self->bufsize; i++)
1671 {
1672 for (j = 0; j < self->overlaps; j++)
1673 {
1674 curPhase = ins[j][i];
1675 which = j - 1;
1676
1677 if (which < 0)
1678 which = self->overlaps - 1;
1679
1680 where = self->count - self->hopsize;
1681
1682 if (where < 0)
1683 where += self->frameSize;
1684
1685 lastPhase = self->frameBuffer[which][where];
1686 diff = curPhase - lastPhase;
1687
1688 while (diff < -PI)
1689 {
1690 diff += TWOPI;
1691 }
1692
1693 while (diff > PI)
1694 {
1695 diff -= TWOPI;
1696 }
1697
1698 self->frameBuffer[j][self->count] = curPhase;
1699 self->buffer_streams[i + j * self->bufsize] = diff;
1700 }
1701
1702 self->count++;
1703
1704 if (self->count >= self->frameSize)
1705 self->count = 0;
1706 }
1707 }
1708
1709 MYFLT *
1710 FrameDeltaMain_getSamplesBuffer(FrameDeltaMain *self)
1711 {
1712 return (MYFLT *)self->buffer_streams;
1713 }
1714
1715 static void
1716 FrameDeltaMain_setProcMode(FrameDeltaMain *self)
1717 {
1718 self->proc_func_ptr = FrameDeltaMain_generate;
1719 }
1720
1721 static void
1722 FrameDeltaMain_compute_next_data_frame(FrameDeltaMain *self)
1723 {
1724 (*self->proc_func_ptr)(self);
1725 }
1726
1727 static int
1728 FrameDeltaMain_traverse(FrameDeltaMain *self, visitproc visit, void *arg)
1729 {
1730 pyo_VISIT
1731 Py_VISIT(self->input);
1732 return 0;
1733 }
1734
1735 static int
1736 FrameDeltaMain_clear(FrameDeltaMain *self)
1737 {
1738 pyo_CLEAR
1739 Py_CLEAR(self->input);
1740 return 0;
1741 }
1742
1743 static void
1744 FrameDeltaMain_dealloc(FrameDeltaMain* self)
1745 {
1746 int i;
1747 pyo_DEALLOC
1748
1749 for (i = 0; i < self->overlaps; i++)
1750 {
1751 PyMem_RawFree(self->frameBuffer[i]);
1752 }
1753
1754 PyMem_RawFree(self->frameBuffer);
1755 PyMem_RawFree(self->buffer_streams);
1756 FrameDeltaMain_clear(self);
1757 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1758 Py_TYPE(self)->tp_free((PyObject*)self);
1759 }
1760
1761 static PyObject *
1762 FrameDeltaMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1763 {
1764 int i, j;
1765 PyObject *inputtmp;
1766 FrameDeltaMain *self;
1767 self = (FrameDeltaMain *)type->tp_alloc(type, 0);
1768
1769 self->count = 0;
1770 self->input = PyList_New(0);
1771
1772 INIT_OBJECT_COMMON
1773 Stream_setFunctionPtr(self->stream, FrameDeltaMain_compute_next_data_frame);
1774 self->mode_func_ptr = FrameDeltaMain_setProcMode;
1775
1776 static char *kwlist[] = {"input", "frameSize", "overlaps", NULL};
1777
1778 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oii", kwlist, &inputtmp, &self->frameSize, &self->overlaps))
1779 Py_RETURN_NONE;
1780
1781 if (inputtmp)
1782 {
1783 PyObject_CallMethod((PyObject *)self, "setInput", "O", inputtmp);
1784 }
1785
1786 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1787
1788 self->hopsize = self->frameSize / self->overlaps;
1789 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
1790
1791 for (i = 0; i < self->overlaps; i++)
1792 {
1793 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
1794
1795 for (j = 0; j < self->frameSize; j++)
1796 {
1797 self->frameBuffer[i][j] = 0.0;
1798 }
1799 }
1800
1801 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->overlaps * self->bufsize * sizeof(MYFLT));
1802
1803 for (i = 0; i < (self->overlaps * self->bufsize); i++)
1804 {
1805 self->buffer_streams[i] = 0.0;
1806 }
1807
1808 (*self->mode_func_ptr)(self);
1809
1810 return (PyObject *)self;
1811 }
1812
1813 static PyObject * FrameDeltaMain_getServer(FrameDeltaMain* self) { GET_SERVER };
1814 static PyObject * FrameDeltaMain_getStream(FrameDeltaMain* self) { GET_STREAM };
1815
1816 static PyObject * FrameDeltaMain_play(FrameDeltaMain *self, PyObject *args, PyObject *kwds) { PLAY };
1817 static PyObject * FrameDeltaMain_stop(FrameDeltaMain *self, PyObject *args, PyObject *kwds) { STOP };
1818
1819 static PyObject *
1820 FrameDeltaMain_setInput(FrameDeltaMain *self, PyObject *arg)
1821 {
1822 if (! PyList_Check(arg))
1823 {
1824 PyErr_SetString(PyExc_TypeError, "The inputs attribute must be a list.");
1825 Py_RETURN_NONE;
1826 }
1827
1828 Py_DECREF(self->input);
1829
1830 self->inputSize = PyList_Size(arg);
1831 self->input = arg;
1832 Py_INCREF(self->input);
1833
1834 Py_RETURN_NONE;
1835 }
1836
1837 static PyObject *
1838 FrameDeltaMain_setFrameSize(FrameDeltaMain *self, PyObject *arg)
1839 {
1840 int i, j, tmp;
1841
1842 if (PyLong_Check(arg))
1843 {
1844 tmp = PyLong_AsLong(arg);
1845
1846 if (isPowerOfTwo(tmp))
1847 {
1848 self->frameSize = tmp;
1849 self->hopsize = self->frameSize / self->overlaps;
1850
1851 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
1852
1853 for (i = 0; i < self->overlaps; i++)
1854 {
1855 PyMem_RawFree(self->frameBuffer[i]);
1856 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
1857
1858 for (j = 0; j < self->frameSize; j++)
1859 {
1860 self->frameBuffer[i][j] = 0.0;
1861 }
1862 }
1863
1864 self->count = 0;
1865 }
1866 }
1867 else
1868 PySys_WriteStdout("frameSize must be a power of two!\n");
1869
1870 Py_RETURN_NONE;
1871 }
1872
1873 static PyMemberDef FrameDeltaMain_members[] =
1874 {
1875 {"server", T_OBJECT_EX, offsetof(FrameDeltaMain, server), 0, "Pyo server."},
1876 {"stream", T_OBJECT_EX, offsetof(FrameDeltaMain, stream), 0, "Stream object."},
1877 {"input", T_OBJECT_EX, offsetof(FrameDeltaMain, input), 0, "Phase input object."},
1878 {NULL} /* Sentinel */
1879 };
1880
1881 static PyMethodDef FrameDeltaMain_methods[] =
1882 {
1883 {"getServer", (PyCFunction)FrameDeltaMain_getServer, METH_NOARGS, "Returns server object."},
1884 {"_getStream", (PyCFunction)FrameDeltaMain_getStream, METH_NOARGS, "Returns stream object."},
1885 {"play", (PyCFunction)FrameDeltaMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1886 {"stop", (PyCFunction)FrameDeltaMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1887 {"setInput", (PyCFunction)FrameDeltaMain_setInput, METH_O, "Sets list of input streams."},
1888 {"setFrameSize", (PyCFunction)FrameDeltaMain_setFrameSize, METH_O, "Sets frame size."},
1889 {NULL} /* Sentinel */
1890 };
1891
1892 PyTypeObject FrameDeltaMainType =
1893 {
1894 PyVarObject_HEAD_INIT(NULL, 0)
1895 "_pyo.FrameDeltaMain_base", /*tp_name*/
1896 sizeof(FrameDeltaMain), /*tp_basicsize*/
1897 0, /*tp_itemsize*/
1898 (destructor)FrameDeltaMain_dealloc, /*tp_dealloc*/
1899 0, /*tp_print*/
1900 0, /*tp_getattr*/
1901 0, /*tp_setattr*/
1902 0, /*tp_as_async (tp_compare in Python 2)*/
1903 0, /*tp_repr*/
1904 0, /*tp_as_number*/
1905 0, /*tp_as_sequence*/
1906 0, /*tp_as_mapping*/
1907 0, /*tp_hash */
1908 0, /*tp_call*/
1909 0, /*tp_str*/
1910 0, /*tp_getattro*/
1911 0, /*tp_setattro*/
1912 0, /*tp_as_buffer*/
1913 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1914 "FrameDeltaMain objects. Compute the phase difference between successive frames.", /* tp_doc */
1915 (traverseproc)FrameDeltaMain_traverse, /* tp_traverse */
1916 (inquiry)FrameDeltaMain_clear, /* tp_clear */
1917 0, /* tp_richcompare */
1918 0, /* tp_weaklistoffset */
1919 0, /* tp_iter */
1920 0, /* tp_iternext */
1921 FrameDeltaMain_methods, /* tp_methods */
1922 FrameDeltaMain_members, /* tp_members */
1923 0, /* tp_getset */
1924 0, /* tp_base */
1925 0, /* tp_dict */
1926 0, /* tp_descr_get */
1927 0, /* tp_descr_set */
1928 0, /* tp_dictoffset */
1929 0, /* tp_init */
1930 0, /* tp_alloc */
1931 FrameDeltaMain_new, /* tp_new */
1932 };
1933
1934 /************************************************************************************************/
1935 /* FrameDelta streamer object */
1936 /************************************************************************************************/
1937 typedef struct
1938 {
1939 pyo_audio_HEAD
1940 FrameDeltaMain *mainSplitter;
1941 int modebuffer[2];
1942 int chnl; // panning order
1943 } FrameDelta;
1944
1945 static void FrameDelta_postprocessing_ii(FrameDelta *self) { POST_PROCESSING_II };
1946 static void FrameDelta_postprocessing_ai(FrameDelta *self) { POST_PROCESSING_AI };
1947 static void FrameDelta_postprocessing_ia(FrameDelta *self) { POST_PROCESSING_IA };
1948 static void FrameDelta_postprocessing_aa(FrameDelta *self) { POST_PROCESSING_AA };
1949 static void FrameDelta_postprocessing_ireva(FrameDelta *self) { POST_PROCESSING_IREVA };
1950 static void FrameDelta_postprocessing_areva(FrameDelta *self) { POST_PROCESSING_AREVA };
1951 static void FrameDelta_postprocessing_revai(FrameDelta *self) { POST_PROCESSING_REVAI };
1952 static void FrameDelta_postprocessing_revaa(FrameDelta *self) { POST_PROCESSING_REVAA };
1953 static void FrameDelta_postprocessing_revareva(FrameDelta *self) { POST_PROCESSING_REVAREVA };
1954
1955 static void
1956 FrameDelta_setProcMode(FrameDelta *self)
1957 {
1958 int muladdmode;
1959 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1960
1961 switch (muladdmode)
1962 {
1963 case 0:
1964 self->muladd_func_ptr = FrameDelta_postprocessing_ii;
1965 break;
1966
1967 case 1:
1968 self->muladd_func_ptr = FrameDelta_postprocessing_ai;
1969 break;
1970
1971 case 2:
1972 self->muladd_func_ptr = FrameDelta_postprocessing_revai;
1973 break;
1974
1975 case 10:
1976 self->muladd_func_ptr = FrameDelta_postprocessing_ia;
1977 break;
1978
1979 case 11:
1980 self->muladd_func_ptr = FrameDelta_postprocessing_aa;
1981 break;
1982
1983 case 12:
1984 self->muladd_func_ptr = FrameDelta_postprocessing_revaa;
1985 break;
1986
1987 case 20:
1988 self->muladd_func_ptr = FrameDelta_postprocessing_ireva;
1989 break;
1990
1991 case 21:
1992 self->muladd_func_ptr = FrameDelta_postprocessing_areva;
1993 break;
1994
1995 case 22:
1996 self->muladd_func_ptr = FrameDelta_postprocessing_revareva;
1997 break;
1998 }
1999 }
2000
2001 static void
2002 FrameDelta_compute_next_data_frame(FrameDelta *self)
2003 {
2004 int i;
2005 MYFLT *tmp;
2006 int offset = self->chnl * self->bufsize;
2007 tmp = FrameDeltaMain_getSamplesBuffer((FrameDeltaMain *)self->mainSplitter);
2008
2009 for (i = 0; i < self->bufsize; i++)
2010 {
2011 self->data[i] = tmp[i + offset];
2012 }
2013
2014 (*self->muladd_func_ptr)(self);
2015 }
2016
2017 static int
2018 FrameDelta_traverse(FrameDelta *self, visitproc visit, void *arg)
2019 {
2020 pyo_VISIT
2021 Py_VISIT(self->mainSplitter);
2022 return 0;
2023 }
2024
2025 static int
2026 FrameDelta_clear(FrameDelta *self)
2027 {
2028 pyo_CLEAR
2029 Py_CLEAR(self->mainSplitter);
2030 return 0;
2031 }
2032
2033 static void
2034 FrameDelta_dealloc(FrameDelta* self)
2035 {
2036 pyo_DEALLOC
2037 FrameDelta_clear(self);
2038 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2039 Py_TYPE(self)->tp_free((PyObject*)self);
2040 }
2041
2042 static PyObject *
2043 FrameDelta_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2044 {
2045 int i;
2046 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
2047 FrameDelta *self;
2048 self = (FrameDelta *)type->tp_alloc(type, 0);
2049
2050 self->modebuffer[0] = 0;
2051 self->modebuffer[1] = 0;
2052
2053 INIT_OBJECT_COMMON
2054 Stream_setFunctionPtr(self->stream, FrameDelta_compute_next_data_frame);
2055 self->mode_func_ptr = FrameDelta_setProcMode;
2056
2057 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
2058
2059 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
2060 Py_RETURN_NONE;
2061
2062 self->mainSplitter = (FrameDeltaMain *)maintmp;
2063 Py_INCREF(self->mainSplitter);
2064
2065 if (multmp)
2066 {
2067 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2068 }
2069
2070 if (addtmp)
2071 {
2072 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2073 }
2074
2075 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2076
2077 (*self->mode_func_ptr)(self);
2078
2079 return (PyObject *)self;
2080 }
2081
2082 static PyObject * FrameDelta_getServer(FrameDelta* self) { GET_SERVER };
2083 static PyObject * FrameDelta_getStream(FrameDelta* self) { GET_STREAM };
2084 static PyObject * FrameDelta_setMul(FrameDelta *self, PyObject *arg) { SET_MUL };
2085 static PyObject * FrameDelta_setAdd(FrameDelta *self, PyObject *arg) { SET_ADD };
2086 static PyObject * FrameDelta_setSub(FrameDelta *self, PyObject *arg) { SET_SUB };
2087 static PyObject * FrameDelta_setDiv(FrameDelta *self, PyObject *arg) { SET_DIV };
2088
2089 static PyObject * FrameDelta_play(FrameDelta *self, PyObject *args, PyObject *kwds) { PLAY };
2090 static PyObject * FrameDelta_out(FrameDelta *self, PyObject *args, PyObject *kwds) { OUT };
2091 static PyObject * FrameDelta_stop(FrameDelta *self, PyObject *args, PyObject *kwds) { STOP };
2092
2093 static PyObject * FrameDelta_multiply(FrameDelta *self, PyObject *arg) { MULTIPLY };
2094 static PyObject * FrameDelta_inplace_multiply(FrameDelta *self, PyObject *arg) { INPLACE_MULTIPLY };
2095 static PyObject * FrameDelta_add(FrameDelta *self, PyObject *arg) { ADD };
2096 static PyObject * FrameDelta_inplace_add(FrameDelta *self, PyObject *arg) { INPLACE_ADD };
2097 static PyObject * FrameDelta_sub(FrameDelta *self, PyObject *arg) { SUB };
2098 static PyObject * FrameDelta_inplace_sub(FrameDelta *self, PyObject *arg) { INPLACE_SUB };
2099 static PyObject * FrameDelta_div(FrameDelta *self, PyObject *arg) { DIV };
2100 static PyObject * FrameDelta_inplace_div(FrameDelta *self, PyObject *arg) { INPLACE_DIV };
2101
2102 static PyMemberDef FrameDelta_members[] =
2103 {
2104 {"server", T_OBJECT_EX, offsetof(FrameDelta, server), 0, "Pyo server."},
2105 {"stream", T_OBJECT_EX, offsetof(FrameDelta, stream), 0, "Stream object."},
2106 {"mul", T_OBJECT_EX, offsetof(FrameDelta, mul), 0, "Mul factor."},
2107 {"add", T_OBJECT_EX, offsetof(FrameDelta, add), 0, "Add factor."},
2108 {NULL} /* Sentinel */
2109 };
2110
2111 static PyMethodDef FrameDelta_methods[] =
2112 {
2113 {"getServer", (PyCFunction)FrameDelta_getServer, METH_NOARGS, "Returns server object."},
2114 {"_getStream", (PyCFunction)FrameDelta_getStream, METH_NOARGS, "Returns stream object."},
2115 {"play", (PyCFunction)FrameDelta_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2116 {"out", (PyCFunction)FrameDelta_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2117 {"stop", (PyCFunction)FrameDelta_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2118 {"setMul", (PyCFunction)FrameDelta_setMul, METH_O, "Sets FrameDelta mul factor."},
2119 {"setAdd", (PyCFunction)FrameDelta_setAdd, METH_O, "Sets FrameDelta add factor."},
2120 {"setSub", (PyCFunction)FrameDelta_setSub, METH_O, "Sets inverse add factor."},
2121 {"setDiv", (PyCFunction)FrameDelta_setDiv, METH_O, "Sets inverse mul factor."},
2122 {NULL} /* Sentinel */
2123 };
2124
2125 static PyNumberMethods FrameDelta_as_number =
2126 {
2127 (binaryfunc)FrameDelta_add, /*nb_add*/
2128 (binaryfunc)FrameDelta_sub, /*nb_subtract*/
2129 (binaryfunc)FrameDelta_multiply, /*nb_multiply*/
2130 0, /*nb_remainder*/
2131 0, /*nb_divmod*/
2132 0, /*nb_power*/
2133 0, /*nb_neg*/
2134 0, /*nb_pos*/
2135 0, /*(unaryfunc)array_abs,*/
2136 0, /*nb_nonzero*/
2137 0, /*nb_invert*/
2138 0, /*nb_lshift*/
2139 0, /*nb_rshift*/
2140 0, /*nb_and*/
2141 0, /*nb_xor*/
2142 0, /*nb_or*/
2143 0, /*nb_int*/
2144 0, /*nb_long*/
2145 0, /*nb_float*/
2146 (binaryfunc)FrameDelta_inplace_add, /*inplace_add*/
2147 (binaryfunc)FrameDelta_inplace_sub, /*inplace_subtract*/
2148 (binaryfunc)FrameDelta_inplace_multiply, /*inplace_multiply*/
2149 0, /*inplace_remainder*/
2150 0, /*inplace_power*/
2151 0, /*inplace_lshift*/
2152 0, /*inplace_rshift*/
2153 0, /*inplace_and*/
2154 0, /*inplace_xor*/
2155 0, /*inplace_or*/
2156 0, /*nb_floor_divide*/
2157 (binaryfunc)FrameDelta_div, /*nb_true_divide*/
2158 0, /*nb_inplace_floor_divide*/
2159 (binaryfunc)FrameDelta_inplace_div, /*nb_inplace_true_divide*/
2160 0, /* nb_index */
2161 };
2162
2163 PyTypeObject FrameDeltaType =
2164 {
2165 PyVarObject_HEAD_INIT(NULL, 0)
2166 "_pyo.FrameDelta_base", /*tp_name*/
2167 sizeof(FrameDelta), /*tp_basicsize*/
2168 0, /*tp_itemsize*/
2169 (destructor)FrameDelta_dealloc, /*tp_dealloc*/
2170 0, /*tp_print*/
2171 0, /*tp_getattr*/
2172 0, /*tp_setattr*/
2173 0, /*tp_as_async (tp_compare in Python 2)*/
2174 0, /*tp_repr*/
2175 &FrameDelta_as_number, /*tp_as_number*/
2176 0, /*tp_as_sequence*/
2177 0, /*tp_as_mapping*/
2178 0, /*tp_hash */
2179 0, /*tp_call*/
2180 0, /*tp_str*/
2181 0, /*tp_getattro*/
2182 0, /*tp_setattro*/
2183 0, /*tp_as_buffer*/
2184 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2185 "FrameDelta objects. Reads one band from a FrameDeltaMain object.", /* tp_doc */
2186 (traverseproc)FrameDelta_traverse, /* tp_traverse */
2187 (inquiry)FrameDelta_clear, /* tp_clear */
2188 0, /* tp_richcompare */
2189 0, /* tp_weaklistoffset */
2190 0, /* tp_iter */
2191 0, /* tp_iternext */
2192 FrameDelta_methods, /* tp_methods */
2193 FrameDelta_members, /* tp_members */
2194 0, /* tp_getset */
2195 0, /* tp_base */
2196 0, /* tp_dict */
2197 0, /* tp_descr_get */
2198 0, /* tp_descr_set */
2199 0, /* tp_dictoffset */
2200 0, /* tp_init */
2201 0, /* tp_alloc */
2202 FrameDelta_new, /* tp_new */
2203 };
2204
2205 typedef struct
2206 {
2207 pyo_audio_HEAD
2208 PyObject *input;
2209 int inputSize;
2210 int modebuffer[2];
2211 int frameSize;
2212 int overlaps;
2213 int hopsize;
2214 int count;
2215 MYFLT **frameBuffer;
2216 MYFLT *buffer_streams;
2217 } FrameAccumMain;
2218
2219 static void
2220 FrameAccumMain_generate(FrameAccumMain *self)
2221 {
2222 int i, j, which, where;
2223 MYFLT curPhase, lastPhase, diff;
2224
2225 MYFLT ins[self->overlaps][self->bufsize];
2226
2227 for (j = 0; j < self->overlaps; j++)
2228 {
2229 MYFLT *in = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->input, j), "_getStream", NULL));
2230
2231 for (i = 0; i < self->bufsize; i++)
2232 {
2233 ins[j][i] = in[i];
2234 }
2235 }
2236
2237 for (i = 0; i < self->bufsize; i++)
2238 {
2239 for (j = 0; j < self->overlaps; j++)
2240 {
2241 curPhase = ins[j][i];
2242 which = j - 1;
2243
2244 if (which < 0)
2245 which = self->overlaps - 1;
2246
2247 where = self->count - self->hopsize;
2248
2249 if (where < 0)
2250 where += self->frameSize;
2251
2252 lastPhase = self->frameBuffer[which][where];
2253 diff = curPhase + lastPhase;
2254 self->frameBuffer[j][self->count] = diff;
2255 self->buffer_streams[i + j * self->bufsize] = diff;
2256 }
2257
2258 self->count++;
2259
2260 if (self->count >= self->frameSize)
2261 self->count = 0;
2262 }
2263 }
2264
2265 MYFLT *
2266 FrameAccumMain_getSamplesBuffer(FrameAccumMain *self)
2267 {
2268 return (MYFLT *)self->buffer_streams;
2269 }
2270
2271 static void
2272 FrameAccumMain_setProcMode(FrameAccumMain *self)
2273 {
2274 self->proc_func_ptr = FrameAccumMain_generate;
2275 }
2276
2277 static void
2278 FrameAccumMain_compute_next_data_frame(FrameAccumMain *self)
2279 {
2280 (*self->proc_func_ptr)(self);
2281 }
2282
2283 static int
2284 FrameAccumMain_traverse(FrameAccumMain *self, visitproc visit, void *arg)
2285 {
2286 pyo_VISIT
2287 Py_VISIT(self->input);
2288 return 0;
2289 }
2290
2291 static int
2292 FrameAccumMain_clear(FrameAccumMain *self)
2293 {
2294 pyo_CLEAR
2295 Py_CLEAR(self->input);
2296 return 0;
2297 }
2298
2299 static void
2300 FrameAccumMain_dealloc(FrameAccumMain* self)
2301 {
2302 int i;
2303 pyo_DEALLOC
2304
2305 for (i = 0; i < self->overlaps; i++)
2306 {
2307 PyMem_RawFree(self->frameBuffer[i]);
2308 }
2309
2310 PyMem_RawFree(self->frameBuffer);
2311 PyMem_RawFree(self->buffer_streams);
2312 FrameAccumMain_clear(self);
2313 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2314 Py_TYPE(self)->tp_free((PyObject*)self);
2315 }
2316
2317 static PyObject *
2318 FrameAccumMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2319 {
2320 int i, j;
2321 PyObject *inputtmp;
2322 FrameAccumMain *self;
2323 self = (FrameAccumMain *)type->tp_alloc(type, 0);
2324
2325 self->count = 0;
2326 self->input = PyList_New(0);
2327
2328 INIT_OBJECT_COMMON
2329 Stream_setFunctionPtr(self->stream, FrameAccumMain_compute_next_data_frame);
2330 self->mode_func_ptr = FrameAccumMain_setProcMode;
2331
2332 static char *kwlist[] = {"input", "framesize", "overlaps", NULL};
2333
2334 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oii", kwlist, &inputtmp, &self->frameSize, &self->overlaps))
2335 Py_RETURN_NONE;
2336
2337 if (inputtmp)
2338 {
2339 PyObject_CallMethod((PyObject *)self, "setInput", "O", inputtmp);
2340 }
2341
2342 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2343
2344 self->hopsize = self->frameSize / self->overlaps;
2345 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
2346
2347 for (i = 0; i < self->overlaps; i++)
2348 {
2349 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
2350
2351 for (j = 0; j < self->frameSize; j++)
2352 {
2353 self->frameBuffer[i][j] = 0.0;
2354 }
2355 }
2356
2357 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->overlaps * self->bufsize * sizeof(MYFLT));
2358
2359 for (i = 0; i < (self->overlaps * self->bufsize); i++)
2360 {
2361 self->buffer_streams[i] = 0.0;
2362 }
2363
2364 (*self->mode_func_ptr)(self);
2365
2366 return (PyObject *)self;
2367 }
2368
2369 static PyObject * FrameAccumMain_getServer(FrameAccumMain* self) { GET_SERVER };
2370 static PyObject * FrameAccumMain_getStream(FrameAccumMain* self) { GET_STREAM };
2371
2372 static PyObject * FrameAccumMain_play(FrameAccumMain *self, PyObject *args, PyObject *kwds) { PLAY };
2373 static PyObject * FrameAccumMain_stop(FrameAccumMain *self, PyObject *args, PyObject *kwds) { STOP };
2374
2375 static PyObject *
2376 FrameAccumMain_setInput(FrameAccumMain *self, PyObject *arg)
2377 {
2378 if (! PyList_Check(arg))
2379 {
2380 PyErr_SetString(PyExc_TypeError, "The inputs attribute must be a list.");
2381 Py_RETURN_NONE;
2382 }
2383
2384 Py_DECREF(self->input);
2385
2386 self->inputSize = PyList_Size(arg);
2387 self->input = arg;
2388 Py_INCREF(self->input);
2389
2390 Py_RETURN_NONE;
2391 }
2392
2393 static PyObject *
2394 FrameAccumMain_setFrameSize(FrameAccumMain *self, PyObject *arg)
2395 {
2396 int i, j, tmp;
2397
2398 if (PyLong_Check(arg))
2399 {
2400 tmp = PyLong_AsLong(arg);
2401
2402 if (isPowerOfTwo(tmp))
2403 {
2404 self->frameSize = tmp;
2405 self->hopsize = self->frameSize / self->overlaps;
2406
2407 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
2408
2409 for (i = 0; i < self->overlaps; i++)
2410 {
2411 PyMem_RawFree(self->frameBuffer[i]);
2412 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
2413
2414 for (j = 0; j < self->frameSize; j++)
2415 {
2416 self->frameBuffer[i][j] = 0.0;
2417 }
2418 }
2419
2420 self->count = 0;
2421 }
2422 }
2423 else
2424 PySys_WriteStdout("frameSize must be a power of two!\n");
2425
2426 Py_RETURN_NONE;
2427 }
2428
2429 static PyMemberDef FrameAccumMain_members[] =
2430 {
2431 {"server", T_OBJECT_EX, offsetof(FrameAccumMain, server), 0, "Pyo server."},
2432 {"stream", T_OBJECT_EX, offsetof(FrameAccumMain, stream), 0, "Stream object."},
2433 {"input", T_OBJECT_EX, offsetof(FrameAccumMain, input), 0, "Phase input object."},
2434 {NULL} /* Sentinel */
2435 };
2436
2437 static PyMethodDef FrameAccumMain_methods[] =
2438 {
2439 {"getServer", (PyCFunction)FrameAccumMain_getServer, METH_NOARGS, "Returns server object."},
2440 {"_getStream", (PyCFunction)FrameAccumMain_getStream, METH_NOARGS, "Returns stream object."},
2441 {"play", (PyCFunction)FrameAccumMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2442 {"stop", (PyCFunction)FrameAccumMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2443 {"setInput", (PyCFunction)FrameAccumMain_setInput, METH_O, "Sets list of input streams."},
2444 {"setFrameSize", (PyCFunction)FrameAccumMain_setFrameSize, METH_O, "Sets frame size."},
2445 {NULL} /* Sentinel */
2446 };
2447
2448 PyTypeObject FrameAccumMainType =
2449 {
2450 PyVarObject_HEAD_INIT(NULL, 0)
2451 "_pyo.FrameAccumMain_base", /*tp_name*/
2452 sizeof(FrameAccumMain), /*tp_basicsize*/
2453 0, /*tp_itemsize*/
2454 (destructor)FrameAccumMain_dealloc, /*tp_dealloc*/
2455 0, /*tp_print*/
2456 0, /*tp_getattr*/
2457 0, /*tp_setattr*/
2458 0, /*tp_as_async (tp_compare in Python 2)*/
2459 0, /*tp_repr*/
2460 0, /*tp_as_number*/
2461 0, /*tp_as_sequence*/
2462 0, /*tp_as_mapping*/
2463 0, /*tp_hash */
2464 0, /*tp_call*/
2465 0, /*tp_str*/
2466 0, /*tp_getattro*/
2467 0, /*tp_setattro*/
2468 0, /*tp_as_buffer*/
2469 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2470 "FrameAccumMain objects. Integrate the phase difference between successive frames.", /* tp_doc */
2471 (traverseproc)FrameAccumMain_traverse, /* tp_traverse */
2472 (inquiry)FrameAccumMain_clear, /* tp_clear */
2473 0, /* tp_richcompare */
2474 0, /* tp_weaklistoffset */
2475 0, /* tp_iter */
2476 0, /* tp_iternext */
2477 FrameAccumMain_methods, /* tp_methods */
2478 FrameAccumMain_members, /* tp_members */
2479 0, /* tp_getset */
2480 0, /* tp_base */
2481 0, /* tp_dict */
2482 0, /* tp_descr_get */
2483 0, /* tp_descr_set */
2484 0, /* tp_dictoffset */
2485 0, /* tp_init */
2486 0, /* tp_alloc */
2487 FrameAccumMain_new, /* tp_new */
2488 };
2489
2490 /************************************************************************************************/
2491 /* FrameAccum streamer object */
2492 /************************************************************************************************/
2493 typedef struct
2494 {
2495 pyo_audio_HEAD
2496 FrameAccumMain *mainSplitter;
2497 int modebuffer[2];
2498 int chnl; // panning order
2499 } FrameAccum;
2500
2501 static void FrameAccum_postprocessing_ii(FrameAccum *self) { POST_PROCESSING_II };
2502 static void FrameAccum_postprocessing_ai(FrameAccum *self) { POST_PROCESSING_AI };
2503 static void FrameAccum_postprocessing_ia(FrameAccum *self) { POST_PROCESSING_IA };
2504 static void FrameAccum_postprocessing_aa(FrameAccum *self) { POST_PROCESSING_AA };
2505 static void FrameAccum_postprocessing_ireva(FrameAccum *self) { POST_PROCESSING_IREVA };
2506 static void FrameAccum_postprocessing_areva(FrameAccum *self) { POST_PROCESSING_AREVA };
2507 static void FrameAccum_postprocessing_revai(FrameAccum *self) { POST_PROCESSING_REVAI };
2508 static void FrameAccum_postprocessing_revaa(FrameAccum *self) { POST_PROCESSING_REVAA };
2509 static void FrameAccum_postprocessing_revareva(FrameAccum *self) { POST_PROCESSING_REVAREVA };
2510
2511 static void
2512 FrameAccum_setProcMode(FrameAccum *self)
2513 {
2514 int muladdmode;
2515 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2516
2517 switch (muladdmode)
2518 {
2519 case 0:
2520 self->muladd_func_ptr = FrameAccum_postprocessing_ii;
2521 break;
2522
2523 case 1:
2524 self->muladd_func_ptr = FrameAccum_postprocessing_ai;
2525 break;
2526
2527 case 2:
2528 self->muladd_func_ptr = FrameAccum_postprocessing_revai;
2529 break;
2530
2531 case 10:
2532 self->muladd_func_ptr = FrameAccum_postprocessing_ia;
2533 break;
2534
2535 case 11:
2536 self->muladd_func_ptr = FrameAccum_postprocessing_aa;
2537 break;
2538
2539 case 12:
2540 self->muladd_func_ptr = FrameAccum_postprocessing_revaa;
2541 break;
2542
2543 case 20:
2544 self->muladd_func_ptr = FrameAccum_postprocessing_ireva;
2545 break;
2546
2547 case 21:
2548 self->muladd_func_ptr = FrameAccum_postprocessing_areva;
2549 break;
2550
2551 case 22:
2552 self->muladd_func_ptr = FrameAccum_postprocessing_revareva;
2553 break;
2554 }
2555 }
2556
2557 static void
2558 FrameAccum_compute_next_data_frame(FrameAccum *self)
2559 {
2560 int i;
2561 MYFLT *tmp;
2562 int offset = self->chnl * self->bufsize;
2563 tmp = FrameAccumMain_getSamplesBuffer((FrameAccumMain *)self->mainSplitter);
2564
2565 for (i = 0; i < self->bufsize; i++)
2566 {
2567 self->data[i] = tmp[i + offset];
2568 }
2569
2570 (*self->muladd_func_ptr)(self);
2571 }
2572
2573 static int
2574 FrameAccum_traverse(FrameAccum *self, visitproc visit, void *arg)
2575 {
2576 pyo_VISIT
2577 Py_VISIT(self->mainSplitter);
2578 return 0;
2579 }
2580
2581 static int
2582 FrameAccum_clear(FrameAccum *self)
2583 {
2584 pyo_CLEAR
2585 Py_CLEAR(self->mainSplitter);
2586 return 0;
2587 }
2588
2589 static void
2590 FrameAccum_dealloc(FrameAccum* self)
2591 {
2592 pyo_DEALLOC
2593 FrameAccum_clear(self);
2594 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2595 Py_TYPE(self)->tp_free((PyObject*)self);
2596 }
2597
2598 static PyObject *
2599 FrameAccum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2600 {
2601 int i;
2602 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
2603 FrameAccum *self;
2604 self = (FrameAccum *)type->tp_alloc(type, 0);
2605
2606 self->modebuffer[0] = 0;
2607 self->modebuffer[1] = 0;
2608
2609 INIT_OBJECT_COMMON
2610 Stream_setFunctionPtr(self->stream, FrameAccum_compute_next_data_frame);
2611 self->mode_func_ptr = FrameAccum_setProcMode;
2612
2613 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
2614
2615 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
2616 Py_RETURN_NONE;
2617
2618 self->mainSplitter = (FrameAccumMain *)maintmp;
2619 Py_INCREF(self->mainSplitter);
2620
2621 if (multmp)
2622 {
2623 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2624 }
2625
2626 if (addtmp)
2627 {
2628 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2629 }
2630
2631 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2632
2633 (*self->mode_func_ptr)(self);
2634
2635 return (PyObject *)self;
2636 }
2637
2638 static PyObject * FrameAccum_getServer(FrameAccum* self) { GET_SERVER };
2639 static PyObject * FrameAccum_getStream(FrameAccum* self) { GET_STREAM };
2640 static PyObject * FrameAccum_setMul(FrameAccum *self, PyObject *arg) { SET_MUL };
2641 static PyObject * FrameAccum_setAdd(FrameAccum *self, PyObject *arg) { SET_ADD };
2642 static PyObject * FrameAccum_setSub(FrameAccum *self, PyObject *arg) { SET_SUB };
2643 static PyObject * FrameAccum_setDiv(FrameAccum *self, PyObject *arg) { SET_DIV };
2644
2645 static PyObject * FrameAccum_play(FrameAccum *self, PyObject *args, PyObject *kwds) { PLAY };
2646 static PyObject * FrameAccum_out(FrameAccum *self, PyObject *args, PyObject *kwds) { OUT };
2647 static PyObject * FrameAccum_stop(FrameAccum *self, PyObject *args, PyObject *kwds) { STOP };
2648
2649 static PyObject * FrameAccum_multiply(FrameAccum *self, PyObject *arg) { MULTIPLY };
2650 static PyObject * FrameAccum_inplace_multiply(FrameAccum *self, PyObject *arg) { INPLACE_MULTIPLY };
2651 static PyObject * FrameAccum_add(FrameAccum *self, PyObject *arg) { ADD };
2652 static PyObject * FrameAccum_inplace_add(FrameAccum *self, PyObject *arg) { INPLACE_ADD };
2653 static PyObject * FrameAccum_sub(FrameAccum *self, PyObject *arg) { SUB };
2654 static PyObject * FrameAccum_inplace_sub(FrameAccum *self, PyObject *arg) { INPLACE_SUB };
2655 static PyObject * FrameAccum_div(FrameAccum *self, PyObject *arg) { DIV };
2656 static PyObject * FrameAccum_inplace_div(FrameAccum *self, PyObject *arg) { INPLACE_DIV };
2657
2658 static PyMemberDef FrameAccum_members[] =
2659 {
2660 {"server", T_OBJECT_EX, offsetof(FrameAccum, server), 0, "Pyo server."},
2661 {"stream", T_OBJECT_EX, offsetof(FrameAccum, stream), 0, "Stream object."},
2662 {"mul", T_OBJECT_EX, offsetof(FrameAccum, mul), 0, "Mul factor."},
2663 {"add", T_OBJECT_EX, offsetof(FrameAccum, add), 0, "Add factor."},
2664 {NULL} /* Sentinel */
2665 };
2666
2667 static PyMethodDef FrameAccum_methods[] =
2668 {
2669 {"getServer", (PyCFunction)FrameAccum_getServer, METH_NOARGS, "Returns server object."},
2670 {"_getStream", (PyCFunction)FrameAccum_getStream, METH_NOARGS, "Returns stream object."},
2671 {"play", (PyCFunction)FrameAccum_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2672 {"out", (PyCFunction)FrameAccum_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2673 {"stop", (PyCFunction)FrameAccum_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2674 {"setMul", (PyCFunction)FrameAccum_setMul, METH_O, "Sets FrameAccum mul factor."},
2675 {"setAdd", (PyCFunction)FrameAccum_setAdd, METH_O, "Sets FrameAccum add factor."},
2676 {"setSub", (PyCFunction)FrameAccum_setSub, METH_O, "Sets inverse add factor."},
2677 {"setDiv", (PyCFunction)FrameAccum_setDiv, METH_O, "Sets inverse mul factor."},
2678 {NULL} /* Sentinel */
2679 };
2680
2681 static PyNumberMethods FrameAccum_as_number =
2682 {
2683 (binaryfunc)FrameAccum_add, /*nb_add*/
2684 (binaryfunc)FrameAccum_sub, /*nb_subtract*/
2685 (binaryfunc)FrameAccum_multiply, /*nb_multiply*/
2686 0, /*nb_remainder*/
2687 0, /*nb_divmod*/
2688 0, /*nb_power*/
2689 0, /*nb_neg*/
2690 0, /*nb_pos*/
2691 0, /*(unaryfunc)array_abs,*/
2692 0, /*nb_nonzero*/
2693 0, /*nb_invert*/
2694 0, /*nb_lshift*/
2695 0, /*nb_rshift*/
2696 0, /*nb_and*/
2697 0, /*nb_xor*/
2698 0, /*nb_or*/
2699 0, /*nb_int*/
2700 0, /*nb_long*/
2701 0, /*nb_float*/
2702 (binaryfunc)FrameAccum_inplace_add, /*inplace_add*/
2703 (binaryfunc)FrameAccum_inplace_sub, /*inplace_subtract*/
2704 (binaryfunc)FrameAccum_inplace_multiply, /*inplace_multiply*/
2705 0, /*inplace_remainder*/
2706 0, /*inplace_power*/
2707 0, /*inplace_lshift*/
2708 0, /*inplace_rshift*/
2709 0, /*inplace_and*/
2710 0, /*inplace_xor*/
2711 0, /*inplace_or*/
2712 0, /*nb_floor_divide*/
2713 (binaryfunc)FrameAccum_div, /*nb_true_divide*/
2714 0, /*nb_inplace_floor_divide*/
2715 (binaryfunc)FrameAccum_inplace_div, /*nb_inplace_true_divide*/
2716 0, /* nb_index */
2717 };
2718
2719 PyTypeObject FrameAccumType =
2720 {
2721 PyVarObject_HEAD_INIT(NULL, 0)
2722 "_pyo.FrameAccum_base", /*tp_name*/
2723 sizeof(FrameAccum), /*tp_basicsize*/
2724 0, /*tp_itemsize*/
2725 (destructor)FrameAccum_dealloc, /*tp_dealloc*/
2726 0, /*tp_print*/
2727 0, /*tp_getattr*/
2728 0, /*tp_setattr*/
2729 0, /*tp_as_async (tp_compare in Python 2)*/
2730 0, /*tp_repr*/
2731 &FrameAccum_as_number, /*tp_as_number*/
2732 0, /*tp_as_sequence*/
2733 0, /*tp_as_mapping*/
2734 0, /*tp_hash */
2735 0, /*tp_call*/
2736 0, /*tp_str*/
2737 0, /*tp_getattro*/
2738 0, /*tp_setattro*/
2739 0, /*tp_as_buffer*/
2740 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2741 "FrameAccum objects. Reads one band from a FrameAccumMain object.", /* tp_doc */
2742 (traverseproc)FrameAccum_traverse, /* tp_traverse */
2743 (inquiry)FrameAccum_clear, /* tp_clear */
2744 0, /* tp_richcompare */
2745 0, /* tp_weaklistoffset */
2746 0, /* tp_iter */
2747 0, /* tp_iternext */
2748 FrameAccum_methods, /* tp_methods */
2749 FrameAccum_members, /* tp_members */
2750 0, /* tp_getset */
2751 0, /* tp_base */
2752 0, /* tp_dict */
2753 0, /* tp_descr_get */
2754 0, /* tp_descr_set */
2755 0, /* tp_dictoffset */
2756 0, /* tp_init */
2757 0, /* tp_alloc */
2758 FrameAccum_new, /* tp_new */
2759 };
2760
2761 typedef struct
2762 {
2763 pyo_audio_HEAD
2764 PyObject *input;
2765 PyObject *up;
2766 Stream *up_stream;
2767 PyObject *down;
2768 Stream *down_stream;
2769 PyObject *damp;
2770 Stream *damp_stream;
2771 int inputSize;
2772 int modebuffer[5];
2773 int frameSize;
2774 int overlaps;
2775 int hopsize;
2776 int count;
2777 MYFLT **frameBuffer;
2778 MYFLT *buffer_streams;
2779 } VectralMain;
2780
2781 static void
2782 VectralMain_generate(VectralMain *self)
2783 {
2784 int i, j, which, where, bin, halfSize;
2785 MYFLT curMag, lastMag, diff, slope, up, down, damp;
2786
2787 halfSize = self->frameSize / 2;
2788
2789 if (self->modebuffer[2] == 0)
2790 up = PyFloat_AS_DOUBLE(self->up);
2791 else
2792 up = Stream_getData((Stream *)self->up_stream)[0];
2793
2794 if (up < 0.001)
2795 up = 0.001;
2796 else if (up > 1.0)
2797 up = 1.0;
2798
2799 up = MYPOW(up, 4.0);
2800
2801 if (self->modebuffer[3] == 0)
2802 down = PyFloat_AS_DOUBLE(self->down);
2803 else
2804 down = Stream_getData((Stream *)self->down_stream)[0];
2805
2806 if (down < 0.001)
2807 down = 0.001;
2808 else if (down > 1.0)
2809 down = 1.0;
2810
2811 down = MYPOW(down, 4.0);
2812
2813 if (self->modebuffer[4] == 0)
2814 damp = PyFloat_AS_DOUBLE(self->damp);
2815 else
2816 damp = Stream_getData((Stream *)self->damp_stream)[0];
2817
2818 if (damp < 0.)
2819 damp = 0.;
2820 else if (damp > 1.0)
2821 damp = 1.0;
2822
2823 damp = damp * 0.1 + 0.9;
2824
2825 MYFLT ins[self->overlaps][self->bufsize];
2826
2827 for (j = 0; j < self->overlaps; j++)
2828 {
2829 MYFLT *in = Stream_getData((Stream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->input, j), "_getStream", NULL));
2830
2831 for (i = 0; i < self->bufsize; i++)
2832 {
2833 ins[j][i] = in[i];
2834 }
2835 }
2836
2837 for (i = 0; i < self->bufsize; i++)
2838 {
2839 for (j = 0; j < self->overlaps; j++)
2840 {
2841 which = j - 1;
2842
2843 if (which < 0)
2844 which = self->overlaps - 1;
2845
2846 where = self->count - self->hopsize;
2847
2848 if (where < 0)
2849 where += self->frameSize;
2850
2851 bin = self->count - (self->hopsize * j);
2852
2853 if (bin < 0)
2854 bin += self->frameSize;
2855
2856 slope = MYPOW(damp, (MYFLT)(bin % halfSize));
2857 curMag = ins[j][i] * slope;
2858 lastMag = self->frameBuffer[which][where];
2859 diff = curMag - lastMag;
2860
2861 if (diff < 0.0)
2862 curMag = curMag * down + lastMag * (1.0 - down);
2863 else if (diff >= 0.0)
2864 curMag = curMag * up + lastMag * (1.0 - up);
2865
2866 self->frameBuffer[j][self->count] = curMag;
2867 self->buffer_streams[i + j * self->bufsize] = curMag;
2868 }
2869
2870 self->count++;
2871
2872 if (self->count >= self->frameSize)
2873 self->count = 0;
2874 }
2875 }
2876
2877 MYFLT *
2878 VectralMain_getSamplesBuffer(VectralMain *self)
2879 {
2880 return (MYFLT *)self->buffer_streams;
2881 }
2882
2883 static void
2884 VectralMain_setProcMode(VectralMain *self)
2885 {
2886 self->proc_func_ptr = VectralMain_generate;
2887 }
2888
2889 static void
2890 VectralMain_compute_next_data_frame(VectralMain *self)
2891 {
2892 (*self->proc_func_ptr)(self);
2893 }
2894
2895 static int
2896 VectralMain_traverse(VectralMain *self, visitproc visit, void *arg)
2897 {
2898 pyo_VISIT
2899 Py_VISIT(self->input);
2900 Py_VISIT(self->up);
2901 Py_VISIT(self->down);
2902 Py_VISIT(self->damp);
2903 return 0;
2904 }
2905
2906 static int
2907 VectralMain_clear(VectralMain *self)
2908 {
2909 pyo_CLEAR
2910 Py_CLEAR(self->input);
2911 Py_CLEAR(self->up);
2912 Py_CLEAR(self->down);
2913 Py_CLEAR(self->damp);
2914 return 0;
2915 }
2916
2917 static void
2918 VectralMain_dealloc(VectralMain* self)
2919 {
2920 int i;
2921 pyo_DEALLOC
2922
2923 for (i = 0; i < self->overlaps; i++)
2924 {
2925 PyMem_RawFree(self->frameBuffer[i]);
2926 }
2927
2928 PyMem_RawFree(self->frameBuffer);
2929 PyMem_RawFree(self->buffer_streams);
2930 VectralMain_clear(self);
2931 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2932 Py_TYPE(self)->tp_free((PyObject*)self);
2933 }
2934
2935 static PyObject *
2936 VectralMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2937 {
2938 int i, j;
2939 PyObject *inputtmp, *uptmp = NULL, *downtmp = NULL, *damptmp = NULL;
2940 VectralMain *self;
2941 self = (VectralMain *)type->tp_alloc(type, 0);
2942
2943 self->input = PyList_New(0);
2944 self->up = PyFloat_FromDouble(1.0);
2945 self->down = PyFloat_FromDouble(0.7);
2946 self->damp = PyFloat_FromDouble(0.9);
2947 self->count = 0;
2948 self->modebuffer[0] = 0;
2949 self->modebuffer[1] = 0;
2950 self->modebuffer[2] = 0;
2951 self->modebuffer[3] = 0;
2952 self->modebuffer[4] = 0;
2953
2954 INIT_OBJECT_COMMON
2955 Stream_setFunctionPtr(self->stream, VectralMain_compute_next_data_frame);
2956 self->mode_func_ptr = VectralMain_setProcMode;
2957
2958 static char *kwlist[] = {"input", "frameSize", "overlaps", "up", "down", "damp", NULL};
2959
2960 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oii|OOO", kwlist, &inputtmp, &self->frameSize, &self->overlaps, &uptmp, &downtmp, &damptmp))
2961 Py_RETURN_NONE;
2962
2963 if (inputtmp)
2964 {
2965 PyObject_CallMethod((PyObject *)self, "setInput", "O", inputtmp);
2966 }
2967
2968 if (uptmp)
2969 {
2970 PyObject_CallMethod((PyObject *)self, "setUp", "O", uptmp);
2971 }
2972
2973 if (downtmp)
2974 {
2975 PyObject_CallMethod((PyObject *)self, "setDown", "O", downtmp);
2976 }
2977
2978 if (damptmp)
2979 {
2980 PyObject_CallMethod((PyObject *)self, "setDamp", "O", damptmp);
2981 }
2982
2983 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2984
2985 self->hopsize = self->frameSize / self->overlaps;
2986 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
2987
2988 for (i = 0; i < self->overlaps; i++)
2989 {
2990 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
2991
2992 for (j = 0; j < self->frameSize; j++)
2993 {
2994 self->frameBuffer[i][j] = 0.0;
2995 }
2996 }
2997
2998 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, self->overlaps * self->bufsize * sizeof(MYFLT));
2999
3000 for (i = 0; i < (self->overlaps * self->bufsize); i++)
3001 {
3002 self->buffer_streams[i] = 0.0;
3003 }
3004
3005 (*self->mode_func_ptr)(self);
3006
3007 return (PyObject *)self;
3008 }
3009
3010 static PyObject * VectralMain_getServer(VectralMain* self) { GET_SERVER };
3011 static PyObject * VectralMain_getStream(VectralMain* self) { GET_STREAM };
3012
3013 static PyObject * VectralMain_play(VectralMain *self, PyObject *args, PyObject *kwds) { PLAY };
3014 static PyObject * VectralMain_stop(VectralMain *self, PyObject *args, PyObject *kwds) { STOP };
3015
3016 static PyObject *
3017 VectralMain_setInput(VectralMain *self, PyObject *arg)
3018 {
3019 if (! PyList_Check(arg))
3020 {
3021 PyErr_SetString(PyExc_TypeError, "The inputs attribute must be a list.");
3022 Py_RETURN_NONE;
3023 }
3024
3025 Py_DECREF(self->input);
3026
3027 self->inputSize = PyList_Size(arg);
3028 self->input = arg;
3029 Py_INCREF(self->input);
3030
3031 Py_RETURN_NONE;
3032 }
3033
3034 static PyObject *
3035 VectralMain_setFrameSize(VectralMain *self, PyObject *arg)
3036 {
3037 int i, j, tmp;
3038
3039 if (PyLong_Check(arg))
3040 {
3041 tmp = PyLong_AsLong(arg);
3042
3043 if (isPowerOfTwo(tmp))
3044 {
3045 self->frameSize = tmp;
3046 self->hopsize = self->frameSize / self->overlaps;
3047
3048 self->frameBuffer = (MYFLT **)PyMem_RawRealloc(self->frameBuffer, self->overlaps * sizeof(MYFLT *));
3049
3050 for (i = 0; i < self->overlaps; i++)
3051 {
3052 PyMem_RawFree(self->frameBuffer[i]);
3053 self->frameBuffer[i] = (MYFLT *)PyMem_RawMalloc(self->frameSize * sizeof(MYFLT));
3054
3055 for (j = 0; j < self->frameSize; j++)
3056 {
3057 self->frameBuffer[i][j] = 0.0;
3058 }
3059 }
3060
3061 self->count = 0;
3062 }
3063 }
3064 else
3065 PySys_WriteStdout("frameSize must be a power of two!\n");
3066
3067 Py_RETURN_NONE;
3068 }
3069
3070 static PyObject * VectralMain_setUp(VectralMain *self, PyObject *arg) { SET_PARAM(self->up, self->up_stream, 2); }
3071 static PyObject * VectralMain_setDown(VectralMain *self, PyObject *arg) { SET_PARAM(self->down, self->down_stream, 3); }
3072 static PyObject * VectralMain_setDamp(VectralMain *self, PyObject *arg) { SET_PARAM(self->damp, self->damp_stream, 4); }
3073
3074 static PyMemberDef VectralMain_members[] =
3075 {
3076 {"server", T_OBJECT_EX, offsetof(VectralMain, server), 0, "Pyo server."},
3077 {"stream", T_OBJECT_EX, offsetof(VectralMain, stream), 0, "Stream object."},
3078 {"input", T_OBJECT_EX, offsetof(VectralMain, input), 0, "Phase input object."},
3079 {NULL} /* Sentinel */
3080 };
3081
3082 static PyMethodDef VectralMain_methods[] =
3083 {
3084 {"getServer", (PyCFunction)VectralMain_getServer, METH_NOARGS, "Returns server object."},
3085 {"_getStream", (PyCFunction)VectralMain_getStream, METH_NOARGS, "Returns stream object."},
3086 {"play", (PyCFunction)VectralMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3087 {"stop", (PyCFunction)VectralMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3088 {"setInput", (PyCFunction)VectralMain_setInput, METH_O, "Sets list of input streams."},
3089 {"setFrameSize", (PyCFunction)VectralMain_setFrameSize, METH_O, "Sets frame size."},
3090 {"setUp", (PyCFunction)VectralMain_setUp, METH_O, "Sets clipping upward factor."},
3091 {"setDown", (PyCFunction)VectralMain_setDown, METH_O, "Sets clipping downward factor."},
3092 {"setDamp", (PyCFunction)VectralMain_setDamp, METH_O, "Sets high frequencies damping factor."},
3093 {NULL} /* Sentinel */
3094 };
3095
3096 PyTypeObject VectralMainType =
3097 {
3098 PyVarObject_HEAD_INIT(NULL, 0)
3099 "_pyo.VectralMain_base", /*tp_name*/
3100 sizeof(VectralMain), /*tp_basicsize*/
3101 0, /*tp_itemsize*/
3102 (destructor)VectralMain_dealloc, /*tp_dealloc*/
3103 0, /*tp_print*/
3104 0, /*tp_getattr*/
3105 0, /*tp_setattr*/
3106 0, /*tp_as_async (tp_compare in Python 2)*/
3107 0, /*tp_repr*/
3108 0, /*tp_as_number*/
3109 0, /*tp_as_sequence*/
3110 0, /*tp_as_mapping*/
3111 0, /*tp_hash */
3112 0, /*tp_call*/
3113 0, /*tp_str*/
3114 0, /*tp_getattro*/
3115 0, /*tp_setattro*/
3116 0, /*tp_as_buffer*/
3117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3118 "VectralMain objects. Smoothing between successive magnitude FFT frames.", /* tp_doc */
3119 (traverseproc)VectralMain_traverse, /* tp_traverse */
3120 (inquiry)VectralMain_clear, /* tp_clear */
3121 0, /* tp_richcompare */
3122 0, /* tp_weaklistoffset */
3123 0, /* tp_iter */
3124 0, /* tp_iternext */
3125 VectralMain_methods, /* tp_methods */
3126 VectralMain_members, /* tp_members */
3127 0, /* tp_getset */
3128 0, /* tp_base */
3129 0, /* tp_dict */
3130 0, /* tp_descr_get */
3131 0, /* tp_descr_set */
3132 0, /* tp_dictoffset */
3133 0, /* tp_init */
3134 0, /* tp_alloc */
3135 VectralMain_new, /* tp_new */
3136 };
3137
3138 /************************************************************************************************/
3139 /* Vectral streamer object */
3140 /************************************************************************************************/
3141 typedef struct
3142 {
3143 pyo_audio_HEAD
3144 VectralMain *mainSplitter;
3145 int modebuffer[2];
3146 int chnl; // panning order
3147 } Vectral;
3148
3149 static void Vectral_postprocessing_ii(Vectral *self) { POST_PROCESSING_II };
3150 static void Vectral_postprocessing_ai(Vectral *self) { POST_PROCESSING_AI };
3151 static void Vectral_postprocessing_ia(Vectral *self) { POST_PROCESSING_IA };
3152 static void Vectral_postprocessing_aa(Vectral *self) { POST_PROCESSING_AA };
3153 static void Vectral_postprocessing_ireva(Vectral *self) { POST_PROCESSING_IREVA };
3154 static void Vectral_postprocessing_areva(Vectral *self) { POST_PROCESSING_AREVA };
3155 static void Vectral_postprocessing_revai(Vectral *self) { POST_PROCESSING_REVAI };
3156 static void Vectral_postprocessing_revaa(Vectral *self) { POST_PROCESSING_REVAA };
3157 static void Vectral_postprocessing_revareva(Vectral *self) { POST_PROCESSING_REVAREVA };
3158
3159 static void
3160 Vectral_setProcMode(Vectral *self)
3161 {
3162 int muladdmode;
3163 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3164
3165 switch (muladdmode)
3166 {
3167 case 0:
3168 self->muladd_func_ptr = Vectral_postprocessing_ii;
3169 break;
3170
3171 case 1:
3172 self->muladd_func_ptr = Vectral_postprocessing_ai;
3173 break;
3174
3175 case 2:
3176 self->muladd_func_ptr = Vectral_postprocessing_revai;
3177 break;
3178
3179 case 10:
3180 self->muladd_func_ptr = Vectral_postprocessing_ia;
3181 break;
3182
3183 case 11:
3184 self->muladd_func_ptr = Vectral_postprocessing_aa;
3185 break;
3186
3187 case 12:
3188 self->muladd_func_ptr = Vectral_postprocessing_revaa;
3189 break;
3190
3191 case 20:
3192 self->muladd_func_ptr = Vectral_postprocessing_ireva;
3193 break;
3194
3195 case 21:
3196 self->muladd_func_ptr = Vectral_postprocessing_areva;
3197 break;
3198
3199 case 22:
3200 self->muladd_func_ptr = Vectral_postprocessing_revareva;
3201 break;
3202 }
3203 }
3204
3205 static void
3206 Vectral_compute_next_data_frame(Vectral *self)
3207 {
3208 int i;
3209 MYFLT *tmp;
3210 int offset = self->chnl * self->bufsize;
3211 tmp = VectralMain_getSamplesBuffer((VectralMain *)self->mainSplitter);
3212
3213 for (i = 0; i < self->bufsize; i++)
3214 {
3215 self->data[i] = tmp[i + offset];
3216 }
3217
3218 (*self->muladd_func_ptr)(self);
3219 }
3220
3221 static int
3222 Vectral_traverse(Vectral *self, visitproc visit, void *arg)
3223 {
3224 pyo_VISIT
3225 Py_VISIT(self->mainSplitter);
3226 return 0;
3227 }
3228
3229 static int
3230 Vectral_clear(Vectral *self)
3231 {
3232 pyo_CLEAR
3233 Py_CLEAR(self->mainSplitter);
3234 return 0;
3235 }
3236
3237 static void
3238 Vectral_dealloc(Vectral* self)
3239 {
3240 pyo_DEALLOC
3241 Vectral_clear(self);
3242 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3243 Py_TYPE(self)->tp_free((PyObject*)self);
3244 }
3245
3246 static PyObject *
3247 Vectral_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3248 {
3249 int i;
3250 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
3251 Vectral *self;
3252 self = (Vectral *)type->tp_alloc(type, 0);
3253
3254 self->modebuffer[0] = 0;
3255 self->modebuffer[1] = 0;
3256
3257 INIT_OBJECT_COMMON
3258 Stream_setFunctionPtr(self->stream, Vectral_compute_next_data_frame);
3259 self->mode_func_ptr = Vectral_setProcMode;
3260
3261 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
3262
3263 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
3264 Py_RETURN_NONE;
3265
3266 self->mainSplitter = (VectralMain *)maintmp;
3267 Py_INCREF(self->mainSplitter);
3268
3269 if (multmp)
3270 {
3271 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3272 }
3273
3274 if (addtmp)
3275 {
3276 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3277 }
3278
3279 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3280
3281 (*self->mode_func_ptr)(self);
3282
3283 return (PyObject *)self;
3284 }
3285
3286 static PyObject * Vectral_getServer(Vectral* self) { GET_SERVER };
3287 static PyObject * Vectral_getStream(Vectral* self) { GET_STREAM };
3288 static PyObject * Vectral_setMul(Vectral *self, PyObject *arg) { SET_MUL };
3289 static PyObject * Vectral_setAdd(Vectral *self, PyObject *arg) { SET_ADD };
3290 static PyObject * Vectral_setSub(Vectral *self, PyObject *arg) { SET_SUB };
3291 static PyObject * Vectral_setDiv(Vectral *self, PyObject *arg) { SET_DIV };
3292
3293 static PyObject * Vectral_play(Vectral *self, PyObject *args, PyObject *kwds) { PLAY };
3294 static PyObject * Vectral_out(Vectral *self, PyObject *args, PyObject *kwds) { OUT };
3295 static PyObject * Vectral_stop(Vectral *self, PyObject *args, PyObject *kwds) { STOP };
3296
3297 static PyObject * Vectral_multiply(Vectral *self, PyObject *arg) { MULTIPLY };
3298 static PyObject * Vectral_inplace_multiply(Vectral *self, PyObject *arg) { INPLACE_MULTIPLY };
3299 static PyObject * Vectral_add(Vectral *self, PyObject *arg) { ADD };
3300 static PyObject * Vectral_inplace_add(Vectral *self, PyObject *arg) { INPLACE_ADD };
3301 static PyObject * Vectral_sub(Vectral *self, PyObject *arg) { SUB };
3302 static PyObject * Vectral_inplace_sub(Vectral *self, PyObject *arg) { INPLACE_SUB };
3303 static PyObject * Vectral_div(Vectral *self, PyObject *arg) { DIV };
3304 static PyObject * Vectral_inplace_div(Vectral *self, PyObject *arg) { INPLACE_DIV };
3305
3306 static PyMemberDef Vectral_members[] =
3307 {
3308 {"server", T_OBJECT_EX, offsetof(Vectral, server), 0, "Pyo server."},
3309 {"stream", T_OBJECT_EX, offsetof(Vectral, stream), 0, "Stream object."},
3310 {"mul", T_OBJECT_EX, offsetof(Vectral, mul), 0, "Mul factor."},
3311 {"add", T_OBJECT_EX, offsetof(Vectral, add), 0, "Add factor."},
3312 {NULL} /* Sentinel */
3313 };
3314
3315 static PyMethodDef Vectral_methods[] =
3316 {
3317 {"getServer", (PyCFunction)Vectral_getServer, METH_NOARGS, "Returns server object."},
3318 {"_getStream", (PyCFunction)Vectral_getStream, METH_NOARGS, "Returns stream object."},
3319 {"play", (PyCFunction)Vectral_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3320 {"out", (PyCFunction)Vectral_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3321 {"stop", (PyCFunction)Vectral_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3322 {"setMul", (PyCFunction)Vectral_setMul, METH_O, "Sets Vectral mul factor."},
3323 {"setAdd", (PyCFunction)Vectral_setAdd, METH_O, "Sets Vectral add factor."},
3324 {"setSub", (PyCFunction)Vectral_setSub, METH_O, "Sets inverse add factor."},
3325 {"setDiv", (PyCFunction)Vectral_setDiv, METH_O, "Sets inverse mul factor."},
3326 {NULL} /* Sentinel */
3327 };
3328
3329 static PyNumberMethods Vectral_as_number =
3330 {
3331 (binaryfunc)Vectral_add, /*nb_add*/
3332 (binaryfunc)Vectral_sub, /*nb_subtract*/
3333 (binaryfunc)Vectral_multiply, /*nb_multiply*/
3334 0, /*nb_remainder*/
3335 0, /*nb_divmod*/
3336 0, /*nb_power*/
3337 0, /*nb_neg*/
3338 0, /*nb_pos*/
3339 0, /*(unaryfunc)array_abs,*/
3340 0, /*nb_nonzero*/
3341 0, /*nb_invert*/
3342 0, /*nb_lshift*/
3343 0, /*nb_rshift*/
3344 0, /*nb_and*/
3345 0, /*nb_xor*/
3346 0, /*nb_or*/
3347 0, /*nb_int*/
3348 0, /*nb_long*/
3349 0, /*nb_float*/
3350 (binaryfunc)Vectral_inplace_add, /*inplace_add*/
3351 (binaryfunc)Vectral_inplace_sub, /*inplace_subtract*/
3352 (binaryfunc)Vectral_inplace_multiply, /*inplace_multiply*/
3353 0, /*inplace_remainder*/
3354 0, /*inplace_power*/
3355 0, /*inplace_lshift*/
3356 0, /*inplace_rshift*/
3357 0, /*inplace_and*/
3358 0, /*inplace_xor*/
3359 0, /*inplace_or*/
3360 0, /*nb_floor_divide*/
3361 (binaryfunc)Vectral_div, /*nb_true_divide*/
3362 0, /*nb_inplace_floor_divide*/
3363 (binaryfunc)Vectral_inplace_div, /*nb_inplace_true_divide*/
3364 0, /* nb_index */
3365 };
3366
3367 PyTypeObject VectralType =
3368 {
3369 PyVarObject_HEAD_INIT(NULL, 0)
3370 "_pyo.Vectral_base", /*tp_name*/
3371 sizeof(Vectral), /*tp_basicsize*/
3372 0, /*tp_itemsize*/
3373 (destructor)Vectral_dealloc, /*tp_dealloc*/
3374 0, /*tp_print*/
3375 0, /*tp_getattr*/
3376 0, /*tp_setattr*/
3377 0, /*tp_as_async (tp_compare in Python 2)*/
3378 0, /*tp_repr*/
3379 &Vectral_as_number, /*tp_as_number*/
3380 0, /*tp_as_sequence*/
3381 0, /*tp_as_mapping*/
3382 0, /*tp_hash */
3383 0, /*tp_call*/
3384 0, /*tp_str*/
3385 0, /*tp_getattro*/
3386 0, /*tp_setattro*/
3387 0, /*tp_as_buffer*/
3388 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3389 "Vectral objects. Reads one band from a VectralMain object.", /* tp_doc */
3390 (traverseproc)Vectral_traverse, /* tp_traverse */
3391 (inquiry)Vectral_clear, /* tp_clear */
3392 0, /* tp_richcompare */
3393 0, /* tp_weaklistoffset */
3394 0, /* tp_iter */
3395 0, /* tp_iternext */
3396 Vectral_methods, /* tp_methods */
3397 Vectral_members, /* tp_members */
3398 0, /* tp_getset */
3399 0, /* tp_base */
3400 0, /* tp_dict */
3401 0, /* tp_descr_get */
3402 0, /* tp_descr_set */
3403 0, /* tp_dictoffset */
3404 0, /* tp_init */
3405 0, /* tp_alloc */
3406 Vectral_new, /* tp_new */
3407 };
3408
3409 typedef struct
3410 {
3411 pyo_audio_HEAD
3412 PyObject *input;
3413 Stream *input_stream;
3414 PyObject *bal;
3415 Stream *bal_stream;
3416 char *impulse_path;
3417 int chnl;
3418 int size;
3419 int size2;
3420 int hsize;
3421 int incount;
3422 int num_iter;
3423 int current_iter;
3424 int impulse_len;
3425 MYFLT *inframe;
3426 MYFLT *outframe;
3427 MYFLT *last_half_frame;
3428 MYFLT **twiddle;
3429 MYFLT *input_buffer;
3430 MYFLT *output_buffer;
3431 MYFLT **impulse_real;
3432 MYFLT **impulse_imag;
3433 MYFLT **accum_real;
3434 MYFLT **accum_imag;
3435 MYFLT *real;
3436 MYFLT *imag;
3437 int modebuffer[3];
3438 } CvlVerb;
3439
3440 static void
3441 CvlVerb_alloc_memories(CvlVerb *self)
3442 {
3443 int i, n8;
3444 self->hsize = self->size / 2;
3445 self->size2 = self->size * 2;
3446 n8 = self->size2 >> 3;
3447 self->real = (MYFLT *)PyMem_RawRealloc(self->real, self->size * sizeof(MYFLT));
3448 self->imag = (MYFLT *)PyMem_RawRealloc(self->imag, self->size * sizeof(MYFLT));
3449 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size2 * sizeof(MYFLT));
3450 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size2 * sizeof(MYFLT));
3451 self->last_half_frame = (MYFLT *)PyMem_RawRealloc(self->last_half_frame, self->size * sizeof(MYFLT));
3452 self->input_buffer = (MYFLT *)PyMem_RawRealloc(self->input_buffer, self->size * sizeof(MYFLT));
3453 self->output_buffer = (MYFLT *)PyMem_RawRealloc(self->output_buffer, self->size2 * sizeof(MYFLT));
3454
3455 for (i = 0; i < self->size2; i++)
3456 self->inframe[i] = self->outframe[i] = self->output_buffer[i] = 0.0;
3457
3458 for (i = 0; i < self->size; i++)
3459 self->last_half_frame[i] = self->input_buffer[i] = 0.0;
3460
3461 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
3462
3463 for (i = 0; i < 4; i++)
3464 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
3465
3466 fft_compute_split_twiddle(self->twiddle, self->size2);
3467 }
3468
3469 static void
3470 CvlVerb_analyse_impulse(CvlVerb *self)
3471 {
3472 SNDFILE *sf;
3473 SF_INFO info;
3474 int i, j, snd_size, snd_sr, snd_chnls, num_items, num;
3475 MYFLT *tmp, *tmp2, *inframe, *outframe;
3476
3477 info.format = 0;
3478 sf = sf_open(self->impulse_path, SFM_READ, &info);
3479
3480 if (sf == NULL)
3481 {
3482 PySys_WriteStdout("CvlVerb failed to open the impulse file %s.\n", self->impulse_path);
3483 return;
3484 }
3485
3486 snd_size = info.frames;
3487 snd_sr = info.samplerate;
3488 snd_chnls = info.channels;
3489 num_items = snd_size * snd_chnls;
3490
3491 if (snd_sr != self->sr)
3492 {
3493 PySys_WriteStdout("CvlVerb warning: Impulse sampling rate does't match the sampling rate of the server.\n");
3494 }
3495
3496 self->num_iter = (int)MYCEIL((MYFLT)snd_size / self->size);
3497 self->impulse_len = self->num_iter * self->size;
3498
3499 tmp = (MYFLT *)PyMem_RawMalloc(num_items * sizeof(MYFLT));
3500 tmp2 = (MYFLT *)PyMem_RawMalloc(self->impulse_len * sizeof(MYFLT));
3501
3502 sf_seek(sf, 0, SEEK_SET);
3503 num = SF_READ(sf, tmp, num_items);
3504 sf_close(sf);
3505
3506 for (i = 0; i < snd_size; i++)
3507 {
3508 tmp2[i] = tmp[i * snd_chnls + self->chnl];
3509 }
3510
3511 for (i = snd_size; i < self->impulse_len; i++)
3512 {
3513 tmp2[i] = 0.0;
3514 }
3515
3516 self->impulse_real = (MYFLT **)PyMem_RawRealloc(self->impulse_real, self->num_iter * sizeof(MYFLT *));
3517 self->impulse_imag = (MYFLT **)PyMem_RawRealloc(self->impulse_imag, self->num_iter * sizeof(MYFLT *));
3518 self->accum_real = (MYFLT **)PyMem_RawRealloc(self->accum_real, self->num_iter * sizeof(MYFLT *));
3519 self->accum_imag = (MYFLT **)PyMem_RawRealloc(self->accum_imag, self->num_iter * sizeof(MYFLT *));
3520
3521 for (i = 0; i < self->num_iter; i++)
3522 {
3523 self->impulse_real[i] = (MYFLT *)PyMem_RawMalloc(self->size * sizeof(MYFLT));
3524 self->impulse_imag[i] = (MYFLT *)PyMem_RawMalloc(self->size * sizeof(MYFLT));
3525 self->accum_real[i] = (MYFLT *)PyMem_RawMalloc(self->size * sizeof(MYFLT));
3526 self->accum_imag[i] = (MYFLT *)PyMem_RawMalloc(self->size * sizeof(MYFLT));
3527
3528 for (j = 0; j < self->size; j++)
3529 {
3530 self->accum_real[i][j] = 0.0;
3531 self->accum_imag[i][j] = 0.0;
3532 }
3533 }
3534
3535 inframe = (MYFLT *)PyMem_RawMalloc(self->size2 * sizeof(MYFLT));
3536 outframe = (MYFLT *)PyMem_RawMalloc(self->size2 * sizeof(MYFLT));
3537
3538 for (j = 0; j < self->num_iter; j++)
3539 {
3540 num = j * self->size;
3541
3542 for (i = 0; i < self->size; i++)
3543 {
3544 inframe[i] = tmp2[num + i];
3545 }
3546
3547 for (i = self->size; i < self->size2; i++)
3548 {
3549 inframe[i] = 0.0;
3550 }
3551
3552 realfft_split(inframe, outframe, self->size2, self->twiddle);
3553 self->impulse_real[j][0] = outframe[0];
3554 self->impulse_imag[j][0] = 0.0;
3555
3556 for (i = 1; i < self->size; i++)
3557 {
3558 self->impulse_real[j][i] = outframe[i];
3559 self->impulse_imag[j][i] = outframe[self->size2 - i];
3560 }
3561 }
3562
3563 PyMem_RawFree(tmp);
3564 PyMem_RawFree(tmp2);
3565 PyMem_RawFree(inframe);
3566 PyMem_RawFree(outframe);
3567 }
3568
3569 static void
3570 CvlVerb_process_i(CvlVerb *self)
3571 {
3572 int i, j, k;
3573 MYFLT gdry;
3574 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3575 MYFLT bal = PyFloat_AS_DOUBLE(self->bal);
3576
3577 if (bal < 0)
3578 bal = 0.0;
3579 else if (bal > 1)
3580 bal = 1.0;
3581
3582 gdry = 1.0 - bal;
3583
3584 for (i = 0; i < self->bufsize; i++)
3585 {
3586 self->input_buffer[self->incount] = in[i];
3587 self->data[i] = (self->output_buffer[self->incount] * 100 * bal) + (in[i] * gdry);
3588
3589 self->incount++;
3590
3591 if (self->incount == self->size)
3592 {
3593 self->incount = 0;
3594
3595 k = self->current_iter - 1;
3596
3597 if (k < 0)
3598 k += self->num_iter;
3599
3600 for (i = 0; i < self->size; i++)
3601 {
3602 self->accum_real[k][i] = self->accum_imag[k][i] = 0.0;
3603 self->inframe[i] = self->last_half_frame[i];
3604 self->inframe[i + self->size] = self->last_half_frame[i] = self->input_buffer[i];
3605 }
3606
3607 realfft_split(self->inframe, self->outframe, self->size2, self->twiddle);
3608 self->real[0] = self->outframe[0];
3609 self->imag[0] = 0.0;
3610
3611 for (i = 1; i < self->size; i++)
3612 {
3613 self->real[i] = self->outframe[i];
3614 self->imag[i] = self->outframe[self->size2 - i];
3615 }
3616
3617 for (j = 0; j < self->num_iter; j++)
3618 {
3619 k = self->current_iter + j;
3620
3621 if (k >= self->num_iter)
3622 k -= self->num_iter;
3623
3624 for (i = 0; i < self->size; i++)
3625 {
3626 self->accum_real[k][i] += self->real[i] * self->impulse_real[j][i] - self->imag[i] * self->impulse_imag[j][i];
3627 self->accum_imag[k][i] += self->real[i] * self->impulse_imag[j][i] + self->imag[i] * self->impulse_real[j][i];
3628 }
3629 }
3630
3631 self->inframe[0] = self->accum_real[self->current_iter][0];
3632 self->inframe[self->size] = 0.0;
3633
3634 for (i = 1; i < self->size; i++)
3635 {
3636 self->inframe[i] = self->accum_real[self->current_iter][i];
3637 self->inframe[self->size2 - i] = self->accum_imag[self->current_iter][i];
3638 }
3639
3640 irealfft_split(self->inframe, self->outframe, self->size2, self->twiddle);
3641
3642 for (i = 0; i < self->size; i++)
3643 {
3644 self->output_buffer[i] = self->outframe[i + self->size];
3645 }
3646
3647 self->current_iter++;
3648
3649 if (self->current_iter == self->num_iter)
3650 self->current_iter = 0;
3651 }
3652 }
3653 }
3654
3655 static void
3656 CvlVerb_process_a(CvlVerb *self)
3657 {
3658 int i, j, k;
3659 MYFLT gwet, gdry;
3660 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3661 MYFLT *bal = Stream_getData((Stream *)self->bal_stream);
3662
3663 for (i = 0; i < self->bufsize; i++)
3664 {
3665 gwet = bal[i];
3666
3667 if (gwet < 0)
3668 gwet = 0.0;
3669 else if (gwet > 1)
3670 gwet = 1.0;
3671
3672 gdry = 1.0 - gwet;
3673 self->input_buffer[self->incount] = in[i];
3674 self->data[i] = (self->output_buffer[self->incount] * 100 * gwet) + in[i] * gdry;
3675
3676 self->incount++;
3677
3678 if (self->incount == self->size)
3679 {
3680 self->incount = 0;
3681
3682 k = self->current_iter - 1;
3683
3684 if (k < 0)
3685 k += self->num_iter;
3686
3687 for (i = 0; i < self->size; i++)
3688 {
3689 self->accum_real[k][i] = self->accum_imag[k][i] = 0.0;
3690 self->inframe[i] = self->last_half_frame[i];
3691 self->inframe[i + self->size] = self->last_half_frame[i] = self->input_buffer[i];
3692 }
3693
3694 realfft_split(self->inframe, self->outframe, self->size2, self->twiddle);
3695 self->real[0] = self->outframe[0];
3696 self->imag[0] = 0.0;
3697
3698 for (i = 1; i < self->size; i++)
3699 {
3700 self->real[i] = self->outframe[i];
3701 self->imag[i] = self->outframe[self->size2 - i];
3702 }
3703
3704 for (j = 0; j < self->num_iter; j++)
3705 {
3706 k = self->current_iter + j;
3707
3708 if (k >= self->num_iter)
3709 k -= self->num_iter;
3710
3711 for (i = 0; i < self->size; i++)
3712 {
3713 self->accum_real[k][i] += self->real[i] * self->impulse_real[j][i] - self->imag[i] * self->impulse_imag[j][i];
3714 self->accum_imag[k][i] += self->real[i] * self->impulse_imag[j][i] + self->imag[i] * self->impulse_real[j][i];
3715 }
3716 }
3717
3718 self->inframe[0] = self->accum_real[self->current_iter][0];
3719 self->inframe[self->size] = 0.0;
3720
3721 for (i = 1; i < self->size; i++)
3722 {
3723 self->inframe[i] = self->accum_real[self->current_iter][i];
3724 self->inframe[self->size2 - i] = self->accum_imag[self->current_iter][i];
3725 }
3726
3727 irealfft_split(self->inframe, self->outframe, self->size2, self->twiddle);
3728
3729 for (i = 0; i < self->size; i++)
3730 {
3731 self->output_buffer[i] = self->outframe[i + self->size];
3732 }
3733
3734 self->current_iter++;
3735
3736 if (self->current_iter == self->num_iter)
3737 self->current_iter = 0;
3738 }
3739 }
3740 }
3741
3742 static void CvlVerb_postprocessing_ii(CvlVerb *self) { POST_PROCESSING_II };
3743 static void CvlVerb_postprocessing_ai(CvlVerb *self) { POST_PROCESSING_AI };
3744 static void CvlVerb_postprocessing_ia(CvlVerb *self) { POST_PROCESSING_IA };
3745 static void CvlVerb_postprocessing_aa(CvlVerb *self) { POST_PROCESSING_AA };
3746 static void CvlVerb_postprocessing_ireva(CvlVerb *self) { POST_PROCESSING_IREVA };
3747 static void CvlVerb_postprocessing_areva(CvlVerb *self) { POST_PROCESSING_AREVA };
3748 static void CvlVerb_postprocessing_revai(CvlVerb *self) { POST_PROCESSING_REVAI };
3749 static void CvlVerb_postprocessing_revaa(CvlVerb *self) { POST_PROCESSING_REVAA };
3750 static void CvlVerb_postprocessing_revareva(CvlVerb *self) { POST_PROCESSING_REVAREVA };
3751
3752 static void
3753 CvlVerb_setProcMode(CvlVerb *self)
3754 {
3755 int procmode, muladdmode;
3756 procmode = self->modebuffer[2];
3757 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3758
3759 switch (procmode)
3760 {
3761 case 0:
3762 self->proc_func_ptr = CvlVerb_process_i;
3763 break;
3764
3765 case 1:
3766 self->proc_func_ptr = CvlVerb_process_a;
3767 break;
3768 }
3769
3770 switch (muladdmode)
3771 {
3772 case 0:
3773 self->muladd_func_ptr = CvlVerb_postprocessing_ii;
3774 break;
3775
3776 case 1:
3777 self->muladd_func_ptr = CvlVerb_postprocessing_ai;
3778 break;
3779
3780 case 2:
3781 self->muladd_func_ptr = CvlVerb_postprocessing_revai;
3782 break;
3783
3784 case 10:
3785 self->muladd_func_ptr = CvlVerb_postprocessing_ia;
3786 break;
3787
3788 case 11:
3789 self->muladd_func_ptr = CvlVerb_postprocessing_aa;
3790 break;
3791
3792 case 12:
3793 self->muladd_func_ptr = CvlVerb_postprocessing_revaa;
3794 break;
3795
3796 case 20:
3797 self->muladd_func_ptr = CvlVerb_postprocessing_ireva;
3798 break;
3799
3800 case 21:
3801 self->muladd_func_ptr = CvlVerb_postprocessing_areva;
3802 break;
3803
3804 case 22:
3805 self->muladd_func_ptr = CvlVerb_postprocessing_revareva;
3806 break;
3807 }
3808 }
3809
3810 static void
3811 CvlVerb_compute_next_data_frame(CvlVerb *self)
3812 {
3813 (*self->proc_func_ptr)(self);
3814 (*self->muladd_func_ptr)(self);
3815 }
3816
3817 static int
3818 CvlVerb_traverse(CvlVerb *self, visitproc visit, void *arg)
3819 {
3820 pyo_VISIT
3821 Py_VISIT(self->input);
3822 Py_VISIT(self->bal);
3823 return 0;
3824 }
3825
3826 static int
3827 CvlVerb_clear(CvlVerb *self)
3828 {
3829 pyo_CLEAR
3830 Py_CLEAR(self->input);
3831 Py_CLEAR(self->bal);
3832 return 0;
3833 }
3834
3835 static void
3836 CvlVerb_dealloc(CvlVerb* self)
3837 {
3838 int i;
3839 pyo_DEALLOC
3840 PyMem_RawFree(self->inframe);
3841 PyMem_RawFree(self->outframe);
3842 PyMem_RawFree(self->input_buffer);
3843 PyMem_RawFree(self->output_buffer);
3844 PyMem_RawFree(self->last_half_frame);
3845
3846 for (i = 0; i < 4; i++)
3847 {
3848 PyMem_RawFree(self->twiddle[i]);
3849 }
3850
3851 PyMem_RawFree(self->twiddle);
3852
3853 for (i = 0; i < self->num_iter; i++)
3854 {
3855 PyMem_RawFree(self->impulse_real[i]);
3856 PyMem_RawFree(self->impulse_imag[i]);
3857 PyMem_RawFree(self->accum_real[i]);
3858 PyMem_RawFree(self->accum_imag[i]);
3859 }
3860
3861 PyMem_RawFree(self->impulse_real);
3862 PyMem_RawFree(self->impulse_imag);
3863 PyMem_RawFree(self->accum_real);
3864 PyMem_RawFree(self->accum_imag);
3865 PyMem_RawFree(self->real);
3866 PyMem_RawFree(self->imag);
3867 CvlVerb_clear(self);
3868 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3869 Py_TYPE(self)->tp_free((PyObject*)self);
3870 }
3871
3872 static PyObject *
3873 CvlVerb_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3874 {
3875 int i, k;
3876 Py_ssize_t psize;
3877 PyObject *inputtmp, *input_streamtmp, *baltmp = NULL, *multmp = NULL, *addtmp = NULL;
3878 CvlVerb *self;
3879 self = (CvlVerb *)type->tp_alloc(type, 0);
3880
3881 self->bal = PyFloat_FromDouble(0.25);
3882 self->size = 1024;
3883 self->chnl = 0;
3884 self->incount = 0;
3885 self->current_iter = 0;
3886 INIT_OBJECT_COMMON
3887 Stream_setFunctionPtr(self->stream, CvlVerb_compute_next_data_frame);
3888 self->mode_func_ptr = CvlVerb_setProcMode;
3889
3890 static char *kwlist[] = {"input", "impulse", "bal", "size", "chnl", "mul", "add", NULL};
3891
3892 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Os#|OiiOO", kwlist, &inputtmp, &self->impulse_path, &psize, &baltmp, &self->size, &self->chnl, &multmp, &addtmp))
3893 Py_RETURN_NONE;
3894
3895 if (self->size < self->bufsize)
3896 {
3897 PySys_WriteStdout("Warning: CvlVerb size less than buffer size!\nCvlVerb size set to buffersize: %d\n", self->bufsize);
3898 self->size = self->bufsize;
3899 }
3900
3901 k = 1;
3902
3903 while (k < self->size)
3904 k <<= 1;
3905
3906 self->size = k;
3907
3908 INIT_INPUT_STREAM
3909
3910 if (baltmp)
3911 {
3912 PyObject_CallMethod((PyObject *)self, "setBal", "O", baltmp);
3913 }
3914
3915 if (multmp)
3916 {
3917 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3918 }
3919
3920 if (addtmp)
3921 {
3922 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3923 }
3924
3925 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3926
3927 CvlVerb_alloc_memories(self);
3928 CvlVerb_analyse_impulse(self);
3929
3930 (*self->mode_func_ptr)(self);
3931
3932 return (PyObject *)self;
3933 }
3934
3935 static PyObject * CvlVerb_setBal(CvlVerb *self, PyObject *arg) { SET_PARAM(self->bal, self->bal_stream, 2); }
3936
3937 static PyObject * CvlVerb_getServer(CvlVerb* self) { GET_SERVER };
3938 static PyObject * CvlVerb_getStream(CvlVerb* self) { GET_STREAM };
3939 static PyObject * CvlVerb_setMul(CvlVerb *self, PyObject *arg) { SET_MUL };
3940 static PyObject * CvlVerb_setAdd(CvlVerb *self, PyObject *arg) { SET_ADD };
3941 static PyObject * CvlVerb_setSub(CvlVerb *self, PyObject *arg) { SET_SUB };
3942 static PyObject * CvlVerb_setDiv(CvlVerb *self, PyObject *arg) { SET_DIV };
3943
3944 static PyObject * CvlVerb_play(CvlVerb *self, PyObject *args, PyObject *kwds) { PLAY };
3945 static PyObject * CvlVerb_out(CvlVerb *self, PyObject *args, PyObject *kwds) { OUT };
3946 static PyObject * CvlVerb_stop(CvlVerb *self, PyObject *args, PyObject *kwds) { STOP };
3947
3948 static PyObject * CvlVerb_multiply(CvlVerb *self, PyObject *arg) { MULTIPLY };
3949 static PyObject * CvlVerb_inplace_multiply(CvlVerb *self, PyObject *arg) { INPLACE_MULTIPLY };
3950 static PyObject * CvlVerb_add(CvlVerb *self, PyObject *arg) { ADD };
3951 static PyObject * CvlVerb_inplace_add(CvlVerb *self, PyObject *arg) { INPLACE_ADD };
3952 static PyObject * CvlVerb_sub(CvlVerb *self, PyObject *arg) { SUB };
3953 static PyObject * CvlVerb_inplace_sub(CvlVerb *self, PyObject *arg) { INPLACE_SUB };
3954 static PyObject * CvlVerb_div(CvlVerb *self, PyObject *arg) { DIV };
3955 static PyObject * CvlVerb_inplace_div(CvlVerb *self, PyObject *arg) { INPLACE_DIV };
3956
3957
3958 static PyMemberDef CvlVerb_members[] =
3959 {
3960 {"server", T_OBJECT_EX, offsetof(CvlVerb, server), 0, "Pyo server."},
3961 {"stream", T_OBJECT_EX, offsetof(CvlVerb, stream), 0, "Stream object."},
3962 {"mul", T_OBJECT_EX, offsetof(CvlVerb, mul), 0, "Mul factor."},
3963 {"add", T_OBJECT_EX, offsetof(CvlVerb, add), 0, "Add factor."},
3964 {"input", T_OBJECT_EX, offsetof(CvlVerb, input), 0, "Input sound object."},
3965 {"bal", T_OBJECT_EX, offsetof(CvlVerb, bal), 0, "Wet/dry balance."},
3966 {NULL} /* Sentinel */
3967 };
3968
3969 static PyMethodDef CvlVerb_methods[] =
3970 {
3971 {"getServer", (PyCFunction)CvlVerb_getServer, METH_NOARGS, "Returns server object."},
3972 {"_getStream", (PyCFunction)CvlVerb_getStream, METH_NOARGS, "Returns stream object."},
3973 {"out", (PyCFunction)CvlVerb_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3974 {"play", (PyCFunction)CvlVerb_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3975 {"stop", (PyCFunction)CvlVerb_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3976 {"setBal", (PyCFunction)CvlVerb_setBal, METH_O, "Sets wet/dry balance."},
3977 {"setMul", (PyCFunction)CvlVerb_setMul, METH_O, "Sets CvlVerb mul factor."},
3978 {"setAdd", (PyCFunction)CvlVerb_setAdd, METH_O, "Sets CvlVerb add factor."},
3979 {"setSub", (PyCFunction)CvlVerb_setSub, METH_O, "Sets CvlVerb add factor."},
3980 {"setDiv", (PyCFunction)CvlVerb_setDiv, METH_O, "Sets CvlVerb mul factor."},
3981 {NULL} /* Sentinel */
3982 };
3983
3984 static PyNumberMethods CvlVerb_as_number =
3985 {
3986 (binaryfunc)CvlVerb_add, /*nb_add*/
3987 (binaryfunc)CvlVerb_sub, /*nb_subtract*/
3988 (binaryfunc)CvlVerb_multiply, /*nb_multiply*/
3989 0, /*nb_remainder*/
3990 0, /*nb_divmod*/
3991 0, /*nb_power*/
3992 0, /*nb_neg*/
3993 0, /*nb_pos*/
3994 0, /*(unaryfunc)array_abs,*/
3995 0, /*nb_nonzero*/
3996 0, /*nb_invert*/
3997 0, /*nb_lshift*/
3998 0, /*nb_rshift*/
3999 0, /*nb_and*/
4000 0, /*nb_xor*/
4001 0, /*nb_or*/
4002 0, /*nb_int*/
4003 0, /*nb_long*/
4004 0, /*nb_float*/
4005 (binaryfunc)CvlVerb_inplace_add, /*inplace_add*/
4006 (binaryfunc)CvlVerb_inplace_sub, /*inplace_subtract*/
4007 (binaryfunc)CvlVerb_inplace_multiply, /*inplace_multiply*/
4008 0, /*inplace_remainder*/
4009 0, /*inplace_power*/
4010 0, /*inplace_lshift*/
4011 0, /*inplace_rshift*/
4012 0, /*inplace_and*/
4013 0, /*inplace_xor*/
4014 0, /*inplace_or*/
4015 0, /*nb_floor_divide*/
4016 (binaryfunc)CvlVerb_div, /*nb_true_divide*/
4017 0, /*nb_inplace_floor_divide*/
4018 (binaryfunc)CvlVerb_inplace_div, /*nb_inplace_true_divide*/
4019 0, /* nb_index */
4020 };
4021
4022 PyTypeObject CvlVerbType =
4023 {
4024 PyVarObject_HEAD_INIT(NULL, 0)
4025 "_pyo.CvlVerb_base", /*tp_name*/
4026 sizeof(CvlVerb), /*tp_basicsize*/
4027 0, /*tp_itemsize*/
4028 (destructor)CvlVerb_dealloc, /*tp_dealloc*/
4029 0, /*tp_print*/
4030 0, /*tp_getattr*/
4031 0, /*tp_setattr*/
4032 0, /*tp_as_async (tp_compare in Python 2)*/
4033 0, /*tp_repr*/
4034 &CvlVerb_as_number, /*tp_as_number*/
4035 0, /*tp_as_sequence*/
4036 0, /*tp_as_mapping*/
4037 0, /*tp_hash */
4038 0, /*tp_call*/
4039 0, /*tp_str*/
4040 0, /*tp_getattro*/
4041 0, /*tp_setattro*/
4042 0, /*tp_as_buffer*/
4043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4044 "CvlVerb objects. FFT transform.", /* tp_doc */
4045 (traverseproc)CvlVerb_traverse, /* tp_traverse */
4046 (inquiry)CvlVerb_clear, /* tp_clear */
4047 0, /* tp_richcompare */
4048 0, /* tp_weaklistoffset */
4049 0, /* tp_iter */
4050 0, /* tp_iternext */
4051 CvlVerb_methods, /* tp_methods */
4052 CvlVerb_members, /* tp_members */
4053 0, /* tp_getset */
4054 0, /* tp_base */
4055 0, /* tp_dict */
4056 0, /* tp_descr_get */
4057 0, /* tp_descr_set */
4058 0, /* tp_dictoffset */
4059 0, /* tp_init */
4060 0, /* tp_alloc */
4061 CvlVerb_new, /* tp_new */
4062 };
4063
4064 typedef struct
4065 {
4066 pyo_audio_HEAD
4067 PyObject *input;
4068 Stream *input_stream;
4069 int size;
4070 int hsize;
4071 int wintype;
4072 int incount;
4073 int freqone;
4074 int freqtwo;
4075 int width;
4076 int height;
4077 int fscaling; /* frequency scaling : 0 = lin, 1 = log */
4078 int mscaling; /* magnitude scaling : 0 = lin, 1 = log */
4079 MYFLT gain;
4080 MYFLT oneOverSr;
4081 MYFLT freqPerBin;
4082 MYFLT *input_buffer;
4083 MYFLT *inframe;
4084 MYFLT *outframe;
4085 MYFLT *magnitude;
4086 MYFLT *last_magnitude;
4087 MYFLT *tmpmag;
4088 MYFLT *window;
4089 MYFLT **twiddle;
4090 int allocated;
4091 } Spectrum;
4092
4093 static void
4094 Spectrum_realloc_memories(Spectrum *self)
4095 {
4096 int i, n8;
4097 self->hsize = self->size / 2;
4098 n8 = self->size >> 3;
4099 self->input_buffer = (MYFLT *)PyMem_RawRealloc(self->input_buffer, self->size * sizeof(MYFLT));
4100 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size * sizeof(MYFLT));
4101 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size * sizeof(MYFLT));
4102
4103 for (i = 0; i < self->size; i++)
4104 self->input_buffer[i] = self->inframe[i] = self->outframe[i] = 0.0;
4105
4106 self->magnitude = (MYFLT *)PyMem_RawRealloc(self->magnitude, self->hsize * sizeof(MYFLT));
4107 self->last_magnitude = (MYFLT *)PyMem_RawRealloc(self->last_magnitude, self->hsize * sizeof(MYFLT));
4108 self->tmpmag = (MYFLT *)PyMem_RawRealloc(self->tmpmag, (self->hsize + 6) * sizeof(MYFLT));
4109
4110 for (i = 0; i < self->hsize; i++)
4111 self->magnitude[i] = self->last_magnitude[i] = self->tmpmag[i + 3] = 0.0;
4112
4113 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
4114
4115 for (i = 0; i < 4; i++)
4116 {
4117 if (self->allocated)
4118 {
4119 PyMem_RawFree(self->twiddle[i]);
4120 }
4121 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
4122 }
4123
4124 fft_compute_split_twiddle(self->twiddle, self->size);
4125 self->window = (MYFLT *)PyMem_RawRealloc(self->window, self->size * sizeof(MYFLT));
4126 gen_window(self->window, self->size, self->wintype);
4127 self->incount = self->hsize;
4128 self->freqPerBin = self->sr / self->size;
4129
4130 self->allocated = 1;
4131 }
4132
4133 static PyObject *
4134 Spectrum_display(Spectrum *self)
4135 {
4136 int i, p1, b1, b2, bins;
4137 MYFLT pos, step, frac, iw, mag, h4;
4138 MYFLT logmin, logrange;
4139 PyObject *points, *tuple;
4140
4141 b1 = (int)(self->freqone / self->freqPerBin);
4142 b2 = (int)(self->freqtwo / self->freqPerBin);
4143 bins = b2 - b1;
4144 step = bins / (MYFLT)(self->width);
4145 iw = 1.0 / (MYFLT)(self->width);
4146 h4 = self->height * 0.75;
4147
4148 points = PyList_New(self->width + 2);
4149
4150 tuple = PyTuple_New(2);
4151 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(0));
4152 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height));
4153 PyList_SET_ITEM(points, 0, tuple);
4154 tuple = PyTuple_New(2);
4155 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(self->width));
4156 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height));
4157 PyList_SET_ITEM(points, self->width + 1, tuple);
4158
4159 if (!self->fscaling && !self->mscaling)
4160 {
4161 for (i = 0; i < self->width; i++)
4162 {
4163 pos = i * step + b1;
4164 p1 = (int)pos;
4165 frac = pos - p1;
4166 tuple = PyTuple_New(2);
4167 mag = ((self->magnitude[p1] + (self->magnitude[p1 + 1] - self->magnitude[p1]) * frac) * self->gain * 4 * h4);
4168 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(i));
4169 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height - (int)mag));
4170 PyList_SET_ITEM(points, i + 1, tuple);
4171 }
4172 }
4173 else if (!self->fscaling && self->mscaling)
4174 {
4175 for (i = 0; i < self->width; i++)
4176 {
4177 pos = i * step + b1;
4178 p1 = (int)pos;
4179 frac = pos - p1;
4180 tuple = PyTuple_New(2);
4181 mag = ((self->magnitude[p1] + (self->magnitude[p1 + 1] - self->magnitude[p1]) * frac) * 0.7 * self->gain);
4182 mag = mag > 0.001 ? mag : 0.001;
4183 mag = (60.0 + (20.0 * MYLOG10(mag))) * 0.01666 * h4;
4184 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(i));
4185 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height - (int)mag));
4186 PyList_SET_ITEM(points, i + 1, tuple);
4187 }
4188 }
4189 else if (self->fscaling && !self->mscaling)
4190 {
4191 if (self->freqone <= 20.0)
4192 self->freqone = 20.0;
4193
4194 logmin = MYLOG10(self->freqone);
4195 logrange = MYLOG10(self->freqtwo) - logmin;
4196
4197 for (i = 0; i < self->width; i++)
4198 {
4199 pos = MYPOW(10.0, i * iw * logrange + logmin) / self->freqPerBin;
4200 p1 = (int)pos;
4201 frac = pos - p1;
4202 tuple = PyTuple_New(2);
4203 mag = ((self->magnitude[p1] + (self->magnitude[p1 + 1] - self->magnitude[p1]) * frac) * self->gain * 4 * h4);
4204 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(i));
4205 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height - (int)mag));
4206 PyList_SET_ITEM(points, i + 1, tuple);
4207 }
4208 }
4209 else
4210 {
4211 if (self->freqone <= 20.0)
4212 self->freqone = 20.0;
4213
4214 logmin = MYLOG10(self->freqone);
4215 logrange = MYLOG10(self->freqtwo) - logmin;
4216
4217 for (i = 0; i < self->width; i++)
4218 {
4219 pos = MYPOW(10.0, i * iw * logrange + logmin) / self->freqPerBin;
4220 p1 = (int)pos;
4221 frac = pos - p1;
4222 tuple = PyTuple_New(2);
4223 mag = ((self->magnitude[p1] + (self->magnitude[p1 + 1] - self->magnitude[p1]) * frac) * 0.7 * self->gain);
4224 mag = mag > 0.001 ? mag : 0.001;
4225 mag = (60.0 + (20.0 * MYLOG10(mag))) * 0.01666 * self->height;
4226 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(i));
4227 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height - (int)mag));
4228 PyList_SET_ITEM(points, i + 1, tuple);
4229 }
4230 }
4231
4232 return points;
4233 }
4234
4235 static void
4236 Spectrum_filters(Spectrum *self)
4237 {
4238 int i, j = 0, impos = 0;
4239 MYFLT tmp = 0.0;
4240 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4241
4242 for (i = 0; i < self->bufsize; i++)
4243 {
4244 self->input_buffer[self->incount] = in[i];
4245 self->incount++;
4246
4247 if (self->incount == self->size)
4248 {
4249 for (j = 0; j < self->size; j++)
4250 {
4251 self->inframe[j] = self->input_buffer[j] * self->window[j];
4252 }
4253
4254 self->incount = self->hsize;
4255 realfft_split(self->inframe, self->outframe, self->size, self->twiddle);
4256 self->tmpmag[0] = self->tmpmag[1] = self->tmpmag[2] = 0.0;
4257 self->tmpmag[self->hsize] = self->tmpmag[self->hsize + 1] = self->tmpmag[self->hsize + 2] = 0.0;
4258 self->tmpmag[3] = MYSQRT(self->outframe[0] * self->outframe[0]);
4259
4260 for (j = 1; j < self->hsize; j++)
4261 {
4262 impos = self->size - j;
4263 tmp = MYSQRT(self->outframe[j] * self->outframe[j] + self->outframe[impos] * self->outframe[impos]) * 2;
4264 self->tmpmag[j + 3] = self->last_magnitude[j] = tmp + self->last_magnitude[j] * 0.5;
4265 }
4266
4267 for (j = 0; j < self->hsize; j++)
4268 {
4269 tmp = (self->tmpmag[j] + self->tmpmag[j + 6]) * 0.05 +
4270 (self->tmpmag[j + 1] + self->tmpmag[j + 5]) * 0.15 +
4271 (self->tmpmag[j + 2] + self->tmpmag[j + 4]) * 0.3 +
4272 self->tmpmag[j + 3] * 0.5;
4273 self->magnitude[j] = tmp;
4274 self->input_buffer[j] = self->input_buffer[j + self->hsize];
4275 }
4276 }
4277 }
4278 }
4279
4280 static void
4281 Spectrum_setProcMode(Spectrum *self)
4282 {
4283 self->proc_func_ptr = Spectrum_filters;
4284 }
4285
4286 static void
4287 Spectrum_compute_next_data_frame(Spectrum *self)
4288 {
4289 (*self->proc_func_ptr)(self);
4290 }
4291
4292 static int
4293 Spectrum_traverse(Spectrum *self, visitproc visit, void *arg)
4294 {
4295 pyo_VISIT
4296 Py_VISIT(self->input);
4297 return 0;
4298 }
4299
4300 static int
4301 Spectrum_clear(Spectrum *self)
4302 {
4303 pyo_CLEAR
4304 Py_CLEAR(self->input);
4305 return 0;
4306 }
4307
4308 static void
4309 Spectrum_dealloc(Spectrum* self)
4310 {
4311 int i;
4312 pyo_DEALLOC
4313 PyMem_RawFree(self->input_buffer);
4314 PyMem_RawFree(self->inframe);
4315 PyMem_RawFree(self->outframe);
4316 PyMem_RawFree(self->window);
4317 PyMem_RawFree(self->magnitude);
4318 PyMem_RawFree(self->last_magnitude);
4319 PyMem_RawFree(self->tmpmag);
4320
4321 for (i = 0; i < 4; i++)
4322 {
4323 PyMem_RawFree(self->twiddle[i]);
4324 }
4325
4326 PyMem_RawFree(self->twiddle);
4327 Spectrum_clear(self);
4328 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4329 Py_TYPE(self)->tp_free((PyObject*)self);
4330 }
4331
4332 static PyObject *
4333 Spectrum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4334 {
4335 int i, k;
4336 PyObject *inputtmp, *input_streamtmp;
4337 Spectrum *self;
4338 self = (Spectrum *)type->tp_alloc(type, 0);
4339
4340 self->size = 1024;
4341 self->wintype = 2;
4342
4343 INIT_OBJECT_COMMON
4344
4345 self->gain = 1.0;
4346 self->oneOverSr = 1.0 / self->sr;
4347 self->freqone = 0.0;
4348 self->freqtwo = self->sr * 0.5;
4349 self->width = 500;
4350 self->height = 400;
4351 self->fscaling = 0;
4352 self->mscaling = 1;
4353 self->allocated = 0;
4354
4355 Stream_setFunctionPtr(self->stream, Spectrum_compute_next_data_frame);
4356 self->mode_func_ptr = Spectrum_setProcMode;
4357
4358 static char *kwlist[] = {"input", "size", "wintype", NULL};
4359
4360 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|ii", kwlist, &inputtmp, &self->size, &self->wintype))
4361 Py_RETURN_NONE;
4362
4363 INIT_INPUT_STREAM
4364
4365 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4366
4367 if (!isPowerOfTwo(self->size))
4368 {
4369 k = 1;
4370
4371 while (k < self->size)
4372 k *= 2;
4373
4374 self->size = k;
4375 PySys_WriteStdout("Spectrum: size argument must be a power-of-2, using the next power-of-2 greater than size : %d\n", self->size);
4376 }
4377
4378 Spectrum_realloc_memories(self);
4379
4380 (*self->mode_func_ptr)(self);
4381
4382 return (PyObject *)self;
4383 }
4384
4385 static PyObject * Spectrum_getServer(Spectrum* self) { GET_SERVER };
4386 static PyObject * Spectrum_getStream(Spectrum* self) { GET_STREAM };
4387 static PyObject * Spectrum_play(Spectrum *self, PyObject *args, PyObject *kwds) { PLAY };
4388 static PyObject * Spectrum_stop(Spectrum *self, PyObject *args, PyObject *kwds) { STOP };
4389
4390 static PyObject *
4391 Spectrum_setSize(Spectrum *self, PyObject *arg)
4392 {
4393 int tmp;
4394
4395 if (PyLong_Check(arg))
4396 {
4397 tmp = PyLong_AsLong(arg);
4398
4399 if (isPowerOfTwo(tmp))
4400 {
4401 self->size = tmp;
4402 Spectrum_realloc_memories(self);
4403 }
4404 else
4405 PySys_WriteStdout("FFT size must be a power of two!\n");
4406 }
4407
4408 Py_RETURN_NONE;
4409 }
4410
4411 static PyObject *
4412 Spectrum_setWinType(Spectrum *self, PyObject *arg)
4413 {
4414 if (PyLong_Check(arg))
4415 {
4416 self->wintype = PyLong_AsLong(arg);
4417 gen_window(self->window, self->size, self->wintype);
4418 }
4419
4420 Py_RETURN_NONE;
4421 }
4422
4423 static PyObject *
4424 Spectrum_setLowbound(Spectrum *self, PyObject *arg)
4425 {
4426 MYFLT tmp;
4427
4428 if (PyNumber_Check(arg))
4429 {
4430 tmp = PyFloat_AsDouble(arg);
4431
4432 if (tmp >= 0.0 && tmp <= 0.5)
4433 self->freqone = tmp * self->sr;
4434 else
4435 self->freqone = 0.0;
4436 }
4437 else
4438 self->freqone = 0.0;
4439
4440 return PyFloat_FromDouble(MYFLOOR(self->freqone / self->freqPerBin) * self->freqPerBin);
4441 }
4442
4443 static PyObject *
4444 Spectrum_getLowfreq(Spectrum *self)
4445 {
4446 return PyFloat_FromDouble(self->freqone);
4447 }
4448
4449 static PyObject *
4450 Spectrum_setHighbound(Spectrum *self, PyObject *arg)
4451 {
4452 MYFLT tmp;
4453
4454 if (PyNumber_Check(arg))
4455 {
4456 tmp = PyFloat_AsDouble(arg);
4457
4458 if (tmp >= 0.0 && tmp <= 0.5)
4459 self->freqtwo = tmp * self->sr;
4460 else
4461 self->freqtwo = self->sr * 0.5;
4462 }
4463 else
4464 self->freqtwo = self->sr * 0.5;
4465
4466 return PyFloat_FromDouble(MYFLOOR(self->freqtwo / self->freqPerBin) * self->freqPerBin);
4467 }
4468
4469 static PyObject *
4470 Spectrum_getHighfreq(Spectrum *self)
4471 {
4472 return PyFloat_FromDouble(self->freqtwo);
4473 }
4474
4475 static PyObject *
4476 Spectrum_setWidth(Spectrum *self, PyObject *arg)
4477 {
4478 if (PyLong_Check(arg))
4479 self->width = PyLong_AsLong(arg);
4480
4481 Py_RETURN_NONE;
4482 }
4483
4484 static PyObject *
4485 Spectrum_setHeight(Spectrum *self, PyObject *arg)
4486 {
4487 if (PyLong_Check(arg))
4488 self->height = PyLong_AsLong(arg);
4489
4490 Py_RETURN_NONE;
4491 }
4492
4493 static PyObject *
4494 Spectrum_setFscaling(Spectrum *self, PyObject *arg)
4495 {
4496 if (PyLong_Check(arg))
4497 self->fscaling = PyLong_AsLong(arg);
4498
4499 Py_RETURN_NONE;
4500 }
4501
4502 static PyObject *
4503 Spectrum_setMscaling(Spectrum *self, PyObject *arg)
4504 {
4505 if (PyLong_Check(arg))
4506 self->mscaling = PyLong_AsLong(arg);
4507
4508 Py_RETURN_NONE;
4509 }
4510
4511 static PyObject *
4512 Spectrum_setGain(Spectrum *self, PyObject *arg)
4513 {
4514 if (PyNumber_Check(arg))
4515 self->gain = PyFloat_AsDouble(arg);
4516
4517 Py_RETURN_NONE;
4518 }
4519
4520 static PyMemberDef Spectrum_members[] =
4521 {
4522 {"server", T_OBJECT_EX, offsetof(Spectrum, server), 0, "Pyo server."},
4523 {"stream", T_OBJECT_EX, offsetof(Spectrum, stream), 0, "Stream object."},
4524 {"input", T_OBJECT_EX, offsetof(Spectrum, input), 0, "FFT sound object."},
4525 {NULL} /* Sentinel */
4526 };
4527
4528 static PyMethodDef Spectrum_methods[] =
4529 {
4530 {"getServer", (PyCFunction)Spectrum_getServer, METH_NOARGS, "Returns server object."},
4531 {"_getStream", (PyCFunction)Spectrum_getStream, METH_NOARGS, "Returns stream object."},
4532 {"play", (PyCFunction)Spectrum_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4533 {"stop", (PyCFunction)Spectrum_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4534 {"setSize", (PyCFunction)Spectrum_setSize, METH_O, "Sets a new FFT size."},
4535 {"setWinType", (PyCFunction)Spectrum_setWinType, METH_O, "Sets a new window."},
4536 {"setLowbound", (PyCFunction)Spectrum_setLowbound, METH_O, "Sets the first frequency to display."},
4537 {"setHighbound", (PyCFunction)Spectrum_setHighbound, METH_O, "Sets the last frequency to display."},
4538 {"setWidth", (PyCFunction)Spectrum_setWidth, METH_O, "Sets the width of the display."},
4539 {"setHeight", (PyCFunction)Spectrum_setHeight, METH_O, "Sets the height of the display."},
4540 {"setFscaling", (PyCFunction)Spectrum_setFscaling, METH_O, "Sets the frequency scaling of the display."},
4541 {"setMscaling", (PyCFunction)Spectrum_setMscaling, METH_O, "Sets the magnitude scaling of the display."},
4542 {"setGain", (PyCFunction)Spectrum_setGain, METH_O, "Sets the magnitude gain of the display."},
4543 {"display", (PyCFunction)Spectrum_display, METH_NOARGS, "Gets points to display."},
4544 {"getLowfreq", (PyCFunction)Spectrum_getLowfreq, METH_NOARGS, "Returns the lowest frequency to display."},
4545 {"getHighfreq", (PyCFunction)Spectrum_getHighfreq, METH_NOARGS, "Returns the highest frequency to display."},
4546 {NULL} /* Sentinel */
4547 };
4548
4549 PyTypeObject SpectrumType =
4550 {
4551 PyVarObject_HEAD_INIT(NULL, 0)
4552 "_pyo.Spectrum_base", /*tp_name*/
4553 sizeof(Spectrum), /*tp_basicsize*/
4554 0, /*tp_itemsize*/
4555 (destructor)Spectrum_dealloc, /*tp_dealloc*/
4556 0, /*tp_print*/
4557 0, /*tp_getattr*/
4558 0, /*tp_setattr*/
4559 0, /*tp_as_async (tp_compare in Python 2)*/
4560 0, /*tp_repr*/
4561 0, /*tp_as_number*/
4562 0, /*tp_as_sequence*/
4563 0, /*tp_as_mapping*/
4564 0, /*tp_hash */
4565 0, /*tp_call*/
4566 0, /*tp_str*/
4567 0, /*tp_getattro*/
4568 0, /*tp_setattro*/
4569 0, /*tp_as_buffer*/
4570 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4571 "Spectrum objects. FFT spectrum analyser.", /* tp_doc */
4572 (traverseproc)Spectrum_traverse, /* tp_traverse */
4573 (inquiry)Spectrum_clear, /* tp_clear */
4574 0, /* tp_richcompare */
4575 0, /* tp_weaklistoffset */
4576 0, /* tp_iter */
4577 0, /* tp_iternext */
4578 Spectrum_methods, /* tp_methods */
4579 Spectrum_members, /* tp_members */
4580 0, /* tp_getset */
4581 0, /* tp_base */
4582 0, /* tp_dict */
4583 0, /* tp_descr_get */
4584 0, /* tp_descr_set */
4585 0, /* tp_dictoffset */
4586 0, /* tp_init */
4587 0, /* tp_alloc */
4588 Spectrum_new, /* tp_new */
4589 };
4590
4591 typedef struct
4592 {
4593 pyo_audio_HEAD
4594 PyObject *matrix;
4595 PyObject *index;
4596 Stream *index_stream;
4597 PyObject *phase;
4598 Stream *phase_stream;
4599 int size;
4600 int hsize;
4601 int hopsize;
4602 int wintype;
4603 int incount;
4604 MYFLT *inframe;
4605 MYFLT *outframe;
4606 MYFLT *window;
4607 MYFLT **twiddle;
4608 int modebuffer[2];
4609 } IFFTMatrix;
4610
4611 static void
4612 IFFTMatrix_realloc_memories(IFFTMatrix *self)
4613 {
4614 int i, n8;
4615 self->hsize = self->size / 2;
4616 n8 = self->size >> 3;
4617 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size * sizeof(MYFLT));
4618 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size * sizeof(MYFLT));
4619
4620 for (i = 0; i < self->size; i++)
4621 self->inframe[i] = self->outframe[i] = 0.0;
4622
4623 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
4624
4625 for (i = 0; i < 4; i++)
4626 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
4627
4628 fft_compute_split_twiddle(self->twiddle, self->size);
4629 self->window = (MYFLT *)PyMem_RawRealloc(self->window, self->size * sizeof(MYFLT));
4630 gen_window(self->window, self->size, self->wintype);
4631 self->incount = -self->hopsize;
4632 }
4633
4634 static void
4635 IFFTMatrix_filters(IFFTMatrix *self)
4636 {
4637 int i;
4638 MYFLT data, amp, phase, real, imag, pos;
4639
4640 MYFLT *ind = Stream_getData((Stream *)self->index_stream);
4641 MYFLT *ph = Stream_getData((Stream *)self->phase_stream);
4642
4643 int yWidth = MatrixStream_getHeight((MatrixStream *)self->matrix);
4644 MYFLT yScaling = (MYFLT)yWidth / self->hsize;
4645
4646 for (i = 0; i < self->bufsize; i++)
4647 {
4648 if (self->incount >= 0)
4649 {
4650 if (self->incount < self->hsize)
4651 {
4652 pos = 1.0 - MYSQRT((MYFLT)self->incount * yScaling / yWidth);
4653 amp = MatrixStream_getInterpPointFromPos((MatrixStream *)self->matrix, ind[i], pos) * 0.5 + 0.5;
4654 phase = ph[i] * PI;
4655 real = amp * MYCOS(phase);
4656 imag = amp * MYSIN(phase);
4657 self->inframe[self->incount] = real;
4658
4659 if (self->incount)
4660 self->inframe[self->size - self->incount] = imag;
4661 }
4662 else if (self->incount == self->hsize)
4663 {
4664 self->inframe[self->incount] = self->inframe[0];
4665 }
4666
4667 data = self->outframe[self->incount] * self->window[self->incount] / self->hsize;
4668 self->data[i] = data;
4669 }
4670
4671 self->incount++;
4672
4673 if (self->incount >= self->size)
4674 {
4675 self->incount -= self->size;
4676 irealfft_split(self->inframe, self->outframe, self->size, self->twiddle);
4677 }
4678 }
4679 }
4680
4681 static void IFFTMatrix_postprocessing_ii(IFFTMatrix *self) { POST_PROCESSING_II };
4682 static void IFFTMatrix_postprocessing_ai(IFFTMatrix *self) { POST_PROCESSING_AI };
4683 static void IFFTMatrix_postprocessing_ia(IFFTMatrix *self) { POST_PROCESSING_IA };
4684 static void IFFTMatrix_postprocessing_aa(IFFTMatrix *self) { POST_PROCESSING_AA };
4685 static void IFFTMatrix_postprocessing_ireva(IFFTMatrix *self) { POST_PROCESSING_IREVA };
4686 static void IFFTMatrix_postprocessing_areva(IFFTMatrix *self) { POST_PROCESSING_AREVA };
4687 static void IFFTMatrix_postprocessing_revai(IFFTMatrix *self) { POST_PROCESSING_REVAI };
4688 static void IFFTMatrix_postprocessing_revaa(IFFTMatrix *self) { POST_PROCESSING_REVAA };
4689 static void IFFTMatrix_postprocessing_revareva(IFFTMatrix *self) { POST_PROCESSING_REVAREVA };
4690
4691 static void
4692 IFFTMatrix_setProcMode(IFFTMatrix *self)
4693 {
4694 int muladdmode;
4695 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
4696
4697 self->proc_func_ptr = IFFTMatrix_filters;
4698
4699 switch (muladdmode)
4700 {
4701 case 0:
4702 self->muladd_func_ptr = IFFTMatrix_postprocessing_ii;
4703 break;
4704
4705 case 1:
4706 self->muladd_func_ptr = IFFTMatrix_postprocessing_ai;
4707 break;
4708
4709 case 2:
4710 self->muladd_func_ptr = IFFTMatrix_postprocessing_revai;
4711 break;
4712
4713 case 10:
4714 self->muladd_func_ptr = IFFTMatrix_postprocessing_ia;
4715 break;
4716
4717 case 11:
4718 self->muladd_func_ptr = IFFTMatrix_postprocessing_aa;
4719 break;
4720
4721 case 12:
4722 self->muladd_func_ptr = IFFTMatrix_postprocessing_revaa;
4723 break;
4724
4725 case 20:
4726 self->muladd_func_ptr = IFFTMatrix_postprocessing_ireva;
4727 break;
4728
4729 case 21:
4730 self->muladd_func_ptr = IFFTMatrix_postprocessing_areva;
4731 break;
4732
4733 case 22:
4734 self->muladd_func_ptr = IFFTMatrix_postprocessing_revareva;
4735 break;
4736 }
4737 }
4738
4739 static void
4740 IFFTMatrix_compute_next_data_frame(IFFTMatrix *self)
4741 {
4742 (*self->proc_func_ptr)(self);
4743 (*self->muladd_func_ptr)(self);
4744 }
4745
4746 static int
4747 IFFTMatrix_traverse(IFFTMatrix *self, visitproc visit, void *arg)
4748 {
4749 pyo_VISIT
4750 Py_VISIT(self->matrix);
4751 Py_VISIT(self->index);
4752 Py_VISIT(self->phase);
4753 return 0;
4754 }
4755
4756 static int
4757 IFFTMatrix_clear(IFFTMatrix *self)
4758 {
4759 pyo_CLEAR
4760 Py_CLEAR(self->matrix);
4761 Py_CLEAR(self->index);
4762 Py_CLEAR(self->phase);
4763 return 0;
4764 }
4765
4766 static void
4767 IFFTMatrix_dealloc(IFFTMatrix* self)
4768 {
4769 int i;
4770 pyo_DEALLOC
4771 PyMem_RawFree(self->inframe);
4772 PyMem_RawFree(self->outframe);
4773 PyMem_RawFree(self->window);
4774
4775 for (i = 0; i < 4; i++)
4776 {
4777 PyMem_RawFree(self->twiddle[i]);
4778 }
4779
4780 PyMem_RawFree(self->twiddle);
4781 IFFTMatrix_clear(self);
4782 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4783 Py_TYPE(self)->tp_free((PyObject*)self);
4784 }
4785
4786 static PyObject *
4787 IFFTMatrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4788 {
4789 int i;
4790 PyObject *matrixtmp, *indextmp, *phasetmp, *multmp = NULL, *addtmp = NULL;
4791 IFFTMatrix *self;
4792 self = (IFFTMatrix *)type->tp_alloc(type, 0);
4793
4794 self->index = PyFloat_FromDouble(0.0);
4795 self->phase = PyFloat_FromDouble(0.0);
4796
4797 self->size = 1024;
4798 self->wintype = 2;
4799 self->modebuffer[0] = 0;
4800 self->modebuffer[1] = 0;
4801
4802 INIT_OBJECT_COMMON
4803 Stream_setFunctionPtr(self->stream, IFFTMatrix_compute_next_data_frame);
4804 self->mode_func_ptr = IFFTMatrix_setProcMode;
4805
4806 static char *kwlist[] = {"matrix", "index", "phase", "size", "hopsize", "wintype", "mul", "add", NULL};
4807
4808 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO|iiiOO", kwlist, &matrixtmp, &indextmp, &phasetmp, &self->size, &self->hopsize, &self->wintype, &multmp, &addtmp))
4809 Py_RETURN_NONE;
4810
4811 if ( PyObject_HasAttrString((PyObject *)matrixtmp, "getMatrixStream") == 0 )
4812 {
4813 PyErr_SetString(PyExc_TypeError, "\"matrix\" argument of IFFTMatrix must be a PyoMatrixObject.\n");
4814 Py_RETURN_NONE;
4815 }
4816
4817 self->matrix = PyObject_CallMethod((PyObject *)matrixtmp, "getMatrixStream", "");
4818
4819 if (indextmp)
4820 {
4821 PyObject_CallMethod((PyObject *)self, "setIndex", "O", indextmp);
4822 }
4823
4824 if (phasetmp)
4825 {
4826 PyObject_CallMethod((PyObject *)self, "setPhase", "O", phasetmp);
4827 }
4828
4829 if (multmp)
4830 {
4831 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4832 }
4833
4834 if (addtmp)
4835 {
4836 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4837 }
4838
4839 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4840
4841 IFFTMatrix_realloc_memories(self);
4842
4843 (*self->mode_func_ptr)(self);
4844
4845 return (PyObject *)self;
4846 }
4847
4848 static PyObject * IFFTMatrix_getServer(IFFTMatrix* self) { GET_SERVER };
4849 static PyObject * IFFTMatrix_getStream(IFFTMatrix* self) { GET_STREAM };
4850 static PyObject * IFFTMatrix_setMul(IFFTMatrix *self, PyObject *arg) { SET_MUL };
4851 static PyObject * IFFTMatrix_setAdd(IFFTMatrix *self, PyObject *arg) { SET_ADD };
4852 static PyObject * IFFTMatrix_setSub(IFFTMatrix *self, PyObject *arg) { SET_SUB };
4853 static PyObject * IFFTMatrix_setDiv(IFFTMatrix *self, PyObject *arg) { SET_DIV };
4854
4855 static PyObject * IFFTMatrix_play(IFFTMatrix *self, PyObject *args, PyObject *kwds) { PLAY };
4856 static PyObject * IFFTMatrix_out(IFFTMatrix *self, PyObject *args, PyObject *kwds) { OUT };
4857 static PyObject * IFFTMatrix_stop(IFFTMatrix *self, PyObject *args, PyObject *kwds) { STOP };
4858
4859 static PyObject * IFFTMatrix_multiply(IFFTMatrix *self, PyObject *arg) { MULTIPLY };
4860 static PyObject * IFFTMatrix_inplace_multiply(IFFTMatrix *self, PyObject *arg) { INPLACE_MULTIPLY };
4861 static PyObject * IFFTMatrix_add(IFFTMatrix *self, PyObject *arg) { ADD };
4862 static PyObject * IFFTMatrix_inplace_add(IFFTMatrix *self, PyObject *arg) { INPLACE_ADD };
4863 static PyObject * IFFTMatrix_sub(IFFTMatrix *self, PyObject *arg) { SUB };
4864 static PyObject * IFFTMatrix_inplace_sub(IFFTMatrix *self, PyObject *arg) { INPLACE_SUB };
4865 static PyObject * IFFTMatrix_div(IFFTMatrix *self, PyObject *arg) { DIV };
4866 static PyObject * IFFTMatrix_inplace_div(IFFTMatrix *self, PyObject *arg) { INPLACE_DIV };
4867
4868 static PyObject * IFFTMatrix_setIndex(IFFTMatrix *self, PyObject *arg)
4869 {
4870 ASSERT_ARG_NOT_NULL
4871
4872 if (PyObject_HasAttrString((PyObject *)arg, "server") == 0)
4873 {
4874 PyErr_SetString(PyExc_TypeError, "\"index\" attribute of IFFTMatrix must be a PyoObject.\n");
4875 Py_RETURN_NONE;
4876 }
4877
4878 Py_DECREF(self->index);
4879
4880 self->index = arg;
4881 Py_INCREF(self->index);
4882 PyObject *streamtmp = PyObject_CallMethod((PyObject *)self->index, "_getStream", NULL);
4883 self->index_stream = (Stream *)streamtmp;
4884 Py_INCREF(self->index_stream);
4885
4886 Py_RETURN_NONE;
4887 }
4888
4889 static PyObject *
4890 IFFTMatrix_setPhase(IFFTMatrix *self, PyObject *arg)
4891 {
4892 ASSERT_ARG_NOT_NULL
4893
4894 if (PyObject_HasAttrString((PyObject *)arg, "server") == 0)
4895 {
4896 PyErr_SetString(PyExc_TypeError, "\"phase\" attribute of IFFTMatrix must be a PyoObject.\n");
4897 Py_RETURN_NONE;
4898 }
4899
4900 Py_DECREF(self->phase);
4901
4902 self->phase = arg;
4903 Py_INCREF(self->phase);
4904 PyObject *streamtmp = PyObject_CallMethod((PyObject *)self->phase, "_getStream", NULL);
4905 self->phase_stream = (Stream *)streamtmp;
4906 Py_INCREF(self->phase_stream);
4907
4908 Py_RETURN_NONE;
4909 }
4910
4911 static PyObject *
4912 IFFTMatrix_setSize(IFFTMatrix *self, PyObject *args, PyObject *kwds)
4913 {
4914 int size, hopsize;
4915
4916 static char *kwlist[] = {"size", "hopsize", NULL};
4917
4918 if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwlist, &size, &hopsize))
4919 {
4920 Py_RETURN_NONE;
4921 }
4922
4923 if (isPowerOfTwo(size))
4924 {
4925 self->size = size;
4926 self->hopsize = hopsize;
4927 IFFTMatrix_realloc_memories(self);
4928 }
4929 else
4930 PySys_WriteStdout("IFFTMatrix size must be a power of two!\n");
4931
4932 Py_RETURN_NONE;
4933 }
4934
4935 static PyObject *
4936 IFFTMatrix_setWinType(IFFTMatrix *self, PyObject *arg)
4937 {
4938 if (PyLong_Check(arg))
4939 {
4940 self->wintype = PyLong_AsLong(arg);
4941 gen_window(self->window, self->size, self->wintype);
4942 }
4943
4944 Py_RETURN_NONE;
4945 }
4946
4947 static PyMemberDef IFFTMatrix_members[] =
4948 {
4949 {"server", T_OBJECT_EX, offsetof(IFFTMatrix, server), 0, "Pyo server."},
4950 {"stream", T_OBJECT_EX, offsetof(IFFTMatrix, stream), 0, "Stream object."},
4951 {"matrix", T_OBJECT_EX, offsetof(IFFTMatrix, matrix), 0, "Matrix to read."},
4952 {"index", T_OBJECT_EX, offsetof(IFFTMatrix, index), 0, "Reading position."},
4953 {"phase", T_OBJECT_EX, offsetof(IFFTMatrix, phase), 0, "Instantaneous phase value."},
4954 {"mul", T_OBJECT_EX, offsetof(IFFTMatrix, mul), 0, "Mul factor."},
4955 {"add", T_OBJECT_EX, offsetof(IFFTMatrix, add), 0, "Add factor."},
4956 {NULL} /* Sentinel */
4957 };
4958
4959 static PyMethodDef IFFTMatrix_methods[] =
4960 {
4961 {"getServer", (PyCFunction)IFFTMatrix_getServer, METH_NOARGS, "Returns server object."},
4962 {"_getStream", (PyCFunction)IFFTMatrix_getStream, METH_NOARGS, "Returns stream object."},
4963 {"play", (PyCFunction)IFFTMatrix_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4964 {"out", (PyCFunction)IFFTMatrix_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
4965 {"stop", (PyCFunction)IFFTMatrix_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4966 {"setSize", (PyCFunction)IFFTMatrix_setSize, METH_VARARGS | METH_KEYWORDS, "Sets a new IFFTMatrix size."},
4967 {"setWinType", (PyCFunction)IFFTMatrix_setWinType, METH_O, "Sets a new window."},
4968 {"setIndex", (PyCFunction)IFFTMatrix_setIndex, METH_O, "Sets reading position."},
4969 {"setPhase", (PyCFunction)IFFTMatrix_setPhase, METH_O, "Sets instantaneous phase."},
4970 {"setMul", (PyCFunction)IFFTMatrix_setMul, METH_O, "Sets oscillator mul factor."},
4971 {"setAdd", (PyCFunction)IFFTMatrix_setAdd, METH_O, "Sets oscillator add factor."},
4972 {"setSub", (PyCFunction)IFFTMatrix_setSub, METH_O, "Sets inverse add factor."},
4973 {"setDiv", (PyCFunction)IFFTMatrix_setDiv, METH_O, "Sets inverse mul factor."},
4974 {NULL} /* Sentinel */
4975 };
4976
4977 static PyNumberMethods IFFTMatrix_as_number =
4978 {
4979 (binaryfunc)IFFTMatrix_add, /*nb_add*/
4980 (binaryfunc)IFFTMatrix_sub, /*nb_subtract*/
4981 (binaryfunc)IFFTMatrix_multiply, /*nb_multiply*/
4982 0, /*nb_remainder*/
4983 0, /*nb_divmod*/
4984 0, /*nb_power*/
4985 0, /*nb_neg*/
4986 0, /*nb_pos*/
4987 0, /*(unaryfunc)array_abs,*/
4988 0, /*nb_nonzero*/
4989 0, /*nb_invert*/
4990 0, /*nb_lshift*/
4991 0, /*nb_rshift*/
4992 0, /*nb_and*/
4993 0, /*nb_xor*/
4994 0, /*nb_or*/
4995 0, /*nb_int*/
4996 0, /*nb_long*/
4997 0, /*nb_float*/
4998 (binaryfunc)IFFTMatrix_inplace_add, /*inplace_add*/
4999 (binaryfunc)IFFTMatrix_inplace_sub, /*inplace_subtract*/
5000 (binaryfunc)IFFTMatrix_inplace_multiply, /*inplace_multiply*/
5001 0, /*inplace_remainder*/
5002 0, /*inplace_power*/
5003 0, /*inplace_lshift*/
5004 0, /*inplace_rshift*/
5005 0, /*inplace_and*/
5006 0, /*inplace_xor*/
5007 0, /*inplace_or*/
5008 0, /*nb_floor_divide*/
5009 (binaryfunc)IFFTMatrix_div, /*nb_true_divide*/
5010 0, /*nb_inplace_floor_divide*/
5011 (binaryfunc)IFFTMatrix_inplace_div, /*nb_inplace_true_divide*/
5012 0, /* nb_index */
5013 };
5014
5015 PyTypeObject IFFTMatrixType =
5016 {
5017 PyVarObject_HEAD_INIT(NULL, 0)
5018 "_pyo.IFFTMatrix_base", /*tp_name*/
5019 sizeof(IFFTMatrix), /*tp_basicsize*/
5020 0, /*tp_itemsize*/
5021 (destructor)IFFTMatrix_dealloc, /*tp_dealloc*/
5022 0, /*tp_print*/
5023 0, /*tp_getattr*/
5024 0, /*tp_setattr*/
5025 0, /*tp_as_async (tp_compare in Python 2)*/
5026 0, /*tp_repr*/
5027 &IFFTMatrix_as_number, /*tp_as_number*/
5028 0, /*tp_as_sequence*/
5029 0, /*tp_as_mapping*/
5030 0, /*tp_hash */
5031 0, /*tp_call*/
5032 0, /*tp_str*/
5033 0, /*tp_getattro*/
5034 0, /*tp_setattro*/
5035 0, /*tp_as_buffer*/
5036 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5037 "IFFTMatrix objects. Synthesize audio from a Matrix object.", /* tp_doc */
5038 (traverseproc)IFFTMatrix_traverse, /* tp_traverse */
5039 (inquiry)IFFTMatrix_clear, /* tp_clear */
5040 0, /* tp_richcompare */
5041 0, /* tp_weaklistoffset */
5042 0, /* tp_iter */
5043 0, /* tp_iternext */
5044 IFFTMatrix_methods, /* tp_methods */
5045 IFFTMatrix_members, /* tp_members */
5046 0, /* tp_getset */
5047 0, /* tp_base */
5048 0, /* tp_dict */
5049 0, /* tp_descr_get */
5050 0, /* tp_descr_set */
5051 0, /* tp_dictoffset */
5052 0, /* tp_init */
5053 0, /* tp_alloc */
5054 IFFTMatrix_new, /* tp_new */
5055 };