Debian Codegraph @

src/objects/analysismodule.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 #include <Python.h>
21 #include "structmember.h"
22 #include <math.h>
23 #include "pyomodule.h"
24 #include "streammodule.h"
25 #include "servermodule.h"
26 #include "dummymodule.h"
27 #include "interpolation.h"
28 #include "fft.h"
29 #include "wind.h"
30
31 /************/
32 /* Follower */
33 /************/
34 typedef struct
35 {
36 pyo_audio_HEAD
37 PyObject *input;
38 Stream *input_stream;
39 PyObject *freq;
40 Stream *freq_stream;
41 int modebuffer[3]; // need at least 2 slots for mul & add
42 MYFLT follow;
43 MYFLT last_freq;
44 MYFLT factor;
45 } Follower;
46
47 static void
48 Follower_filters_i(Follower *self)
49 {
50 MYFLT absin, freq;
51 int i;
52
53 MYFLT *in = Stream_getData((Stream *)self->input_stream);
54 freq = PyFloat_AS_DOUBLE(self->freq);
55
56 if (freq != self->last_freq)
57 {
58 if (freq < 0)
59 freq = 0.0;
60
61 self->factor = MYEXP(-TWOPI * freq / self->sr);
62 self->last_freq = freq;
63 }
64
65 for (i = 0; i < self->bufsize; i++)
66 {
67 absin = in[i];
68
69 if (absin < 0.0)
70 absin = -absin;
71
72 self->follow = self->data[i] = absin + self->factor * (self->follow - absin);
73 }
74 }
75
76 static void
77 Follower_filters_a(Follower *self)
78 {
79 MYFLT freq, absin;
80 int i;
81
82 MYFLT *in = Stream_getData((Stream *)self->input_stream);
83 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
84
85 for (i = 0; i < self->bufsize; i++)
86 {
87 freq = fr[i];
88
89 if (freq != self->last_freq)
90 {
91 if (freq < 0)
92 freq = 0.0;
93
94 self->factor = MYEXP(-TWOPI * freq / self->sr);
95 self->last_freq = freq;
96 }
97
98 absin = in[i];
99
100 if (absin < 0.0)
101 absin = -absin;
102
103 self->follow = self->data[i] = absin + self->factor * (self->follow - absin);
104 }
105 }
106
107 static void Follower_postprocessing_ii(Follower *self) { POST_PROCESSING_II };
108 static void Follower_postprocessing_ai(Follower *self) { POST_PROCESSING_AI };
109 static void Follower_postprocessing_ia(Follower *self) { POST_PROCESSING_IA };
110 static void Follower_postprocessing_aa(Follower *self) { POST_PROCESSING_AA };
111 static void Follower_postprocessing_ireva(Follower *self) { POST_PROCESSING_IREVA };
112 static void Follower_postprocessing_areva(Follower *self) { POST_PROCESSING_AREVA };
113 static void Follower_postprocessing_revai(Follower *self) { POST_PROCESSING_REVAI };
114 static void Follower_postprocessing_revaa(Follower *self) { POST_PROCESSING_REVAA };
115 static void Follower_postprocessing_revareva(Follower *self) { POST_PROCESSING_REVAREVA };
116
117 static void
118 Follower_setProcMode(Follower *self)
119 {
120 int procmode, muladdmode;
121 procmode = self->modebuffer[2];
122 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
123
124 switch (procmode)
125 {
126 case 0:
127 self->proc_func_ptr = Follower_filters_i;
128 break;
129
130 case 1:
131 self->proc_func_ptr = Follower_filters_a;
132 break;
133 }
134
135 switch (muladdmode)
136 {
137 case 0:
138 self->muladd_func_ptr = Follower_postprocessing_ii;
139 break;
140
141 case 1:
142 self->muladd_func_ptr = Follower_postprocessing_ai;
143 break;
144
145 case 2:
146 self->muladd_func_ptr = Follower_postprocessing_revai;
147 break;
148
149 case 10:
150 self->muladd_func_ptr = Follower_postprocessing_ia;
151 break;
152
153 case 11:
154 self->muladd_func_ptr = Follower_postprocessing_aa;
155 break;
156
157 case 12:
158 self->muladd_func_ptr = Follower_postprocessing_revaa;
159 break;
160
161 case 20:
162 self->muladd_func_ptr = Follower_postprocessing_ireva;
163 break;
164
165 case 21:
166 self->muladd_func_ptr = Follower_postprocessing_areva;
167 break;
168
169 case 22:
170 self->muladd_func_ptr = Follower_postprocessing_revareva;
171 break;
172 }
173 }
174
175 static void
176 Follower_compute_next_data_frame(Follower *self)
177 {
178 (*self->proc_func_ptr)(self);
179 (*self->muladd_func_ptr)(self);
180 }
181
182 static int
183 Follower_traverse(Follower *self, visitproc visit, void *arg)
184 {
185 pyo_VISIT
186 Py_VISIT(self->input);
187 Py_VISIT(self->freq);
188 return 0;
189 }
190
191 static int
192 Follower_clear(Follower *self)
193 {
194 pyo_CLEAR
195 Py_CLEAR(self->input);
196 Py_CLEAR(self->freq);
197 return 0;
198 }
199
200 static void
201 Follower_dealloc(Follower* self)
202 {
203 pyo_DEALLOC
204 Follower_clear(self);
205 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
206 Py_TYPE(self)->tp_free((PyObject*)self);
207 }
208
209 static PyObject *
210 Follower_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
211 {
212 int i;
213 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
214 Follower *self;
215 self = (Follower *)type->tp_alloc(type, 0);
216
217 self->freq = PyFloat_FromDouble(20);
218 self->follow = 0.0;
219 self->last_freq = -1.0;
220 self->factor = 0.99;
221 self->modebuffer[0] = 0;
222 self->modebuffer[1] = 0;
223 self->modebuffer[2] = 0;
224
225 INIT_OBJECT_COMMON
226 Stream_setFunctionPtr(self->stream, Follower_compute_next_data_frame);
227 self->mode_func_ptr = Follower_setProcMode;
228
229 static char *kwlist[] = {"input", "freq", "mul", "add", NULL};
230
231 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freqtmp, &multmp, &addtmp))
232 Py_RETURN_NONE;
233
234 INIT_INPUT_STREAM
235
236 if (freqtmp)
237 {
238 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
239 }
240
241 if (multmp)
242 {
243 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
244 }
245
246 if (addtmp)
247 {
248 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
249 }
250
251 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
252
253 (*self->mode_func_ptr)(self);
254
255 return (PyObject *)self;
256 }
257
258 static PyObject * Follower_getServer(Follower* self) { GET_SERVER };
259 static PyObject * Follower_getStream(Follower* self) { GET_STREAM };
260 static PyObject * Follower_setMul(Follower *self, PyObject *arg) { SET_MUL };
261 static PyObject * Follower_setAdd(Follower *self, PyObject *arg) { SET_ADD };
262 static PyObject * Follower_setSub(Follower *self, PyObject *arg) { SET_SUB };
263 static PyObject * Follower_setDiv(Follower *self, PyObject *arg) { SET_DIV };
264
265 static PyObject * Follower_play(Follower *self, PyObject *args, PyObject *kwds) { PLAY };
266 static PyObject * Follower_stop(Follower *self, PyObject *args, PyObject *kwds) { STOP };
267
268 static PyObject * Follower_multiply(Follower *self, PyObject *arg) { MULTIPLY };
269 static PyObject * Follower_inplace_multiply(Follower *self, PyObject *arg) { INPLACE_MULTIPLY };
270 static PyObject * Follower_add(Follower *self, PyObject *arg) { ADD };
271 static PyObject * Follower_inplace_add(Follower *self, PyObject *arg) { INPLACE_ADD };
272 static PyObject * Follower_sub(Follower *self, PyObject *arg) { SUB };
273 static PyObject * Follower_inplace_sub(Follower *self, PyObject *arg) { INPLACE_SUB };
274 static PyObject * Follower_div(Follower *self, PyObject *arg) { DIV };
275 static PyObject * Follower_inplace_div(Follower *self, PyObject *arg) { INPLACE_DIV };
276
277 static PyObject * Follower_setFreq(Follower *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
278
279 static PyMemberDef Follower_members[] =
280 {
281 {"server", T_OBJECT_EX, offsetof(Follower, server), 0, "Pyo server."},
282 {"stream", T_OBJECT_EX, offsetof(Follower, stream), 0, "Stream object."},
283 {"input", T_OBJECT_EX, offsetof(Follower, input), 0, "Input sound object."},
284 {"freq", T_OBJECT_EX, offsetof(Follower, freq), 0, "Cutoff frequency in cycle per second."},
285 {"mul", T_OBJECT_EX, offsetof(Follower, mul), 0, "Mul factor."},
286 {"add", T_OBJECT_EX, offsetof(Follower, add), 0, "Add factor."},
287 {NULL} /* Sentinel */
288 };
289
290 static PyMethodDef Follower_methods[] =
291 {
292 {"getServer", (PyCFunction)Follower_getServer, METH_NOARGS, "Returns server object."},
293 {"_getStream", (PyCFunction)Follower_getStream, METH_NOARGS, "Returns stream object."},
294 {"play", (PyCFunction)Follower_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
295 {"stop", (PyCFunction)Follower_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
296 {"setFreq", (PyCFunction)Follower_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
297 {"setMul", (PyCFunction)Follower_setMul, METH_O, "Sets oscillator mul factor."},
298 {"setAdd", (PyCFunction)Follower_setAdd, METH_O, "Sets oscillator add factor."},
299 {"setSub", (PyCFunction)Follower_setSub, METH_O, "Sets inverse add factor."},
300 {"setDiv", (PyCFunction)Follower_setDiv, METH_O, "Sets inverse mul factor."},
301 {NULL} /* Sentinel */
302 };
303
304 static PyNumberMethods Follower_as_number =
305 {
306 (binaryfunc)Follower_add, /*nb_add*/
307 (binaryfunc)Follower_sub, /*nb_subtract*/
308 (binaryfunc)Follower_multiply, /*nb_multiply*/
309 0, /*nb_remainder*/
310 0, /*nb_divmod*/
311 0, /*nb_power*/
312 0, /*nb_neg*/
313 0, /*nb_pos*/
314 0, /*(unaryfunc)array_abs,*/
315 0, /*nb_nonzero*/
316 0, /*nb_invert*/
317 0, /*nb_lshift*/
318 0, /*nb_rshift*/
319 0, /*nb_and*/
320 0, /*nb_xor*/
321 0, /*nb_or*/
322 0, /*nb_int*/
323 0, /*nb_long*/
324 0, /*nb_float*/
325 (binaryfunc)Follower_inplace_add, /*inplace_add*/
326 (binaryfunc)Follower_inplace_sub, /*inplace_subtract*/
327 (binaryfunc)Follower_inplace_multiply, /*inplace_multiply*/
328 0, /*inplace_remainder*/
329 0, /*inplace_power*/
330 0, /*inplace_lshift*/
331 0, /*inplace_rshift*/
332 0, /*inplace_and*/
333 0, /*inplace_xor*/
334 0, /*inplace_or*/
335 0, /*nb_floor_divide*/
336 (binaryfunc)Follower_div, /*nb_true_divide*/
337 0, /*nb_inplace_floor_divide*/
338 (binaryfunc)Follower_inplace_div, /*nb_inplace_true_divide*/
339 0, /* nb_index */
340 };
341
342 PyTypeObject FollowerType =
343 {
344 PyVarObject_HEAD_INIT(NULL, 0)
345 "_pyo.Follower_base", /*tp_name*/
346 sizeof(Follower), /*tp_basicsize*/
347 0, /*tp_itemsize*/
348 (destructor)Follower_dealloc, /*tp_dealloc*/
349 0, /*tp_print*/
350 0, /*tp_getattr*/
351 0, /*tp_setattr*/
352 0, /*tp_as_async (tp_compare in Python 2)*/
353 0, /*tp_repr*/
354 &Follower_as_number, /*tp_as_number*/
355 0, /*tp_as_sequence*/
356 0, /*tp_as_mapping*/
357 0, /*tp_hash */
358 0, /*tp_call*/
359 0, /*tp_str*/
360 0, /*tp_getattro*/
361 0, /*tp_setattro*/
362 0, /*tp_as_buffer*/
363 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
364 "Follower objects. Envelope follower.", /* tp_doc */
365 (traverseproc)Follower_traverse, /* tp_traverse */
366 (inquiry)Follower_clear, /* tp_clear */
367 0, /* tp_richcompare */
368 0, /* tp_weaklistoffset */
369 0, /* tp_iter */
370 0, /* tp_iternext */
371 Follower_methods, /* tp_methods */
372 Follower_members, /* tp_members */
373 0, /* tp_getset */
374 0, /* tp_base */
375 0, /* tp_dict */
376 0, /* tp_descr_get */
377 0, /* tp_descr_set */
378 0, /* tp_dictoffset */
379 0, /* tp_init */
380 0, /* tp_alloc */
381 Follower_new, /* tp_new */
382 };
383
384 /************/
385 /* Follower2 */
386 /************/
387 typedef struct
388 {
389 pyo_audio_HEAD
390 PyObject *input;
391 Stream *input_stream;
392 PyObject *risetime;
393 Stream *risetime_stream;
394 PyObject *falltime;
395 Stream *falltime_stream;
396 int modebuffer[4]; // need at least 2 slots for mul & add
397 MYFLT follow;
398 MYFLT last_risetime;
399 MYFLT last_falltime;
400 MYFLT risefactor;
401 MYFLT fallfactor;
402 MYFLT mTwoPiOverSr;
403 } Follower2;
404
405 static void
406 Follower2_filters_ii(Follower2 *self)
407 {
408 MYFLT absin, risetime, falltime;
409 int i;
410
411 MYFLT *in = Stream_getData((Stream *)self->input_stream);
412 risetime = PyFloat_AS_DOUBLE(self->risetime);
413
414 if (risetime <= 0.0)
415 risetime = 0.000001;
416
417 falltime = PyFloat_AS_DOUBLE(self->falltime);
418
419 if (falltime <= 0.0)
420 falltime = 0.000001;
421
422 if (risetime != self->last_risetime)
423 {
424 self->risefactor = MYEXP(self->mTwoPiOverSr / risetime);
425 self->last_risetime = risetime;
426 }
427
428 if (falltime != self->last_falltime)
429 {
430 self->fallfactor = MYEXP(self->mTwoPiOverSr / falltime);
431 self->last_falltime = falltime;
432 }
433
434 for (i = 0; i < self->bufsize; i++)
435 {
436 absin = in[i];
437
438 if (absin < 0.0)
439 absin = -absin;
440
441 if (self->follow < absin)
442 self->follow = self->data[i] = absin + self->risefactor * (self->follow - absin);
443 else
444 self->follow = self->data[i] = absin + self->fallfactor * (self->follow - absin);
445 }
446 }
447
448 static void
449 Follower2_filters_ai(Follower2 *self)
450 {
451 MYFLT absin, risetime, falltime;
452 int i;
453
454 MYFLT *in = Stream_getData((Stream *)self->input_stream);
455 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
456 falltime = PyFloat_AS_DOUBLE(self->falltime);
457
458 if (falltime <= 0.0)
459 falltime = 0.000001;
460
461 if (falltime != self->last_falltime)
462 {
463 self->fallfactor = MYEXP(self->mTwoPiOverSr / falltime);
464 self->last_falltime = falltime;
465 }
466
467 for (i = 0; i < self->bufsize; i++)
468 {
469 risetime = rise[i];
470
471 if (risetime <= 0.0)
472 risetime = 0.000001;
473
474 if (risetime != self->last_risetime)
475 {
476 self->risefactor = MYEXP(self->mTwoPiOverSr / risetime);
477 self->last_risetime = risetime;
478 }
479
480 absin = in[i];
481
482 if (absin < 0.0)
483 absin = -absin;
484
485 if (self->follow < absin)
486 self->follow = self->data[i] = absin + self->risefactor * (self->follow - absin);
487 else
488 self->follow = self->data[i] = absin + self->fallfactor * (self->follow - absin);
489 }
490 }
491
492 static void
493 Follower2_filters_ia(Follower2 *self)
494 {
495 MYFLT absin, risetime, falltime;
496 int i;
497
498 MYFLT *in = Stream_getData((Stream *)self->input_stream);
499 risetime = PyFloat_AS_DOUBLE(self->risetime);
500
501 if (risetime <= 0.0)
502 risetime = 0.000001;
503
504 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
505
506 if (risetime != self->last_risetime)
507 {
508 self->risefactor = MYEXP(self->mTwoPiOverSr / risetime);
509 self->last_risetime = risetime;
510 }
511
512 for (i = 0; i < self->bufsize; i++)
513 {
514 falltime = fall[i];
515
516 if (falltime <= 0.0)
517 falltime = 0.000001;
518
519 if (falltime != self->last_falltime)
520 {
521 self->fallfactor = MYEXP(self->mTwoPiOverSr / falltime);
522 self->last_falltime = falltime;
523 }
524
525 absin = in[i];
526
527 if (absin < 0.0)
528 absin = -absin;
529
530 if (self->follow < absin)
531 self->follow = self->data[i] = absin + self->risefactor * (self->follow - absin);
532 else
533 self->follow = self->data[i] = absin + self->fallfactor * (self->follow - absin);
534 }
535 }
536
537 static void
538 Follower2_filters_aa(Follower2 *self)
539 {
540 MYFLT absin, risetime, falltime;
541 int i;
542
543 MYFLT *in = Stream_getData((Stream *)self->input_stream);
544 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
545 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
546
547 for (i = 0; i < self->bufsize; i++)
548 {
549 risetime = rise[i];
550
551 if (risetime <= 0.0)
552 risetime = 0.000001;
553
554 if (risetime != self->last_risetime)
555 {
556 self->risefactor = MYEXP(self->mTwoPiOverSr / risetime);
557 self->last_risetime = risetime;
558 }
559
560 falltime = fall[i];
561
562 if (falltime <= 0.0)
563 falltime = 0.000001;
564
565 if (falltime != self->last_falltime)
566 {
567 self->fallfactor = MYEXP(self->mTwoPiOverSr / falltime);
568 self->last_falltime = falltime;
569 }
570
571 absin = in[i];
572
573 if (absin < 0.0)
574 absin = -absin;
575
576 if (self->follow < absin)
577 self->follow = self->data[i] = absin + self->risefactor * (self->follow - absin);
578 else
579 self->follow = self->data[i] = absin + self->fallfactor * (self->follow - absin);
580 }
581 }
582
583 static void Follower2_postprocessing_ii(Follower2 *self) { POST_PROCESSING_II };
584 static void Follower2_postprocessing_ai(Follower2 *self) { POST_PROCESSING_AI };
585 static void Follower2_postprocessing_ia(Follower2 *self) { POST_PROCESSING_IA };
586 static void Follower2_postprocessing_aa(Follower2 *self) { POST_PROCESSING_AA };
587 static void Follower2_postprocessing_ireva(Follower2 *self) { POST_PROCESSING_IREVA };
588 static void Follower2_postprocessing_areva(Follower2 *self) { POST_PROCESSING_AREVA };
589 static void Follower2_postprocessing_revai(Follower2 *self) { POST_PROCESSING_REVAI };
590 static void Follower2_postprocessing_revaa(Follower2 *self) { POST_PROCESSING_REVAA };
591 static void Follower2_postprocessing_revareva(Follower2 *self) { POST_PROCESSING_REVAREVA };
592
593 static void
594 Follower2_setProcMode(Follower2 *self)
595 {
596 int procmode, muladdmode;
597 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
598 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
599
600 switch (procmode)
601 {
602 case 0:
603 self->proc_func_ptr = Follower2_filters_ii;
604 break;
605
606 case 1:
607 self->proc_func_ptr = Follower2_filters_ai;
608 break;
609
610 case 10:
611 self->proc_func_ptr = Follower2_filters_ia;
612 break;
613
614 case 11:
615 self->proc_func_ptr = Follower2_filters_aa;
616 break;
617 }
618
619 switch (muladdmode)
620 {
621 case 0:
622 self->muladd_func_ptr = Follower2_postprocessing_ii;
623 break;
624
625 case 1:
626 self->muladd_func_ptr = Follower2_postprocessing_ai;
627 break;
628
629 case 2:
630 self->muladd_func_ptr = Follower2_postprocessing_revai;
631 break;
632
633 case 10:
634 self->muladd_func_ptr = Follower2_postprocessing_ia;
635 break;
636
637 case 11:
638 self->muladd_func_ptr = Follower2_postprocessing_aa;
639 break;
640
641 case 12:
642 self->muladd_func_ptr = Follower2_postprocessing_revaa;
643 break;
644
645 case 20:
646 self->muladd_func_ptr = Follower2_postprocessing_ireva;
647 break;
648
649 case 21:
650 self->muladd_func_ptr = Follower2_postprocessing_areva;
651 break;
652
653 case 22:
654 self->muladd_func_ptr = Follower2_postprocessing_revareva;
655 break;
656 }
657 }
658
659 static void
660 Follower2_compute_next_data_frame(Follower2 *self)
661 {
662 (*self->proc_func_ptr)(self);
663 (*self->muladd_func_ptr)(self);
664 }
665
666 static int
667 Follower2_traverse(Follower2 *self, visitproc visit, void *arg)
668 {
669 pyo_VISIT
670 Py_VISIT(self->input);
671 Py_VISIT(self->risetime);
672 Py_VISIT(self->falltime);
673 return 0;
674 }
675
676 static int
677 Follower2_clear(Follower2 *self)
678 {
679 pyo_CLEAR
680 Py_CLEAR(self->input);
681 Py_CLEAR(self->risetime);
682 Py_CLEAR(self->falltime);
683 return 0;
684 }
685
686 static void
687 Follower2_dealloc(Follower2* self)
688 {
689 pyo_DEALLOC
690 Follower2_clear(self);
691 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
692 Py_TYPE(self)->tp_free((PyObject*)self);
693 }
694
695 static PyObject *
696 Follower2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
697 {
698 int i;
699 PyObject *inputtmp, *input_streamtmp, *risetimetmp = NULL, *falltimetmp = NULL, *multmp = NULL, *addtmp = NULL;
700 Follower2 *self;
701 self = (Follower2 *)type->tp_alloc(type, 0);
702
703 self->risetime = PyFloat_FromDouble(0.01);
704 self->falltime = PyFloat_FromDouble(0.1);
705 self->follow = 0.0;
706 self->last_risetime = -1.0;
707 self->last_falltime = -1.0;
708 self->risefactor = self->fallfactor = 0.99;
709 self->modebuffer[0] = 0;
710 self->modebuffer[1] = 0;
711 self->modebuffer[2] = 0;
712 self->modebuffer[3] = 0;
713
714 INIT_OBJECT_COMMON
715 Stream_setFunctionPtr(self->stream, Follower2_compute_next_data_frame);
716 self->mode_func_ptr = Follower2_setProcMode;
717
718 self->mTwoPiOverSr = -TWOPI / self->sr;
719
720 static char *kwlist[] = {"input", "risetime", "falltime", "mul", "add", NULL};
721
722 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &risetimetmp, &falltimetmp, &multmp, &addtmp))
723 Py_RETURN_NONE;
724
725 INIT_INPUT_STREAM
726
727 if (risetimetmp)
728 {
729 PyObject_CallMethod((PyObject *)self, "setRisetime", "O", risetimetmp);
730 }
731
732 if (falltimetmp)
733 {
734 PyObject_CallMethod((PyObject *)self, "setFalltime", "O", falltimetmp);
735 }
736
737 if (multmp)
738 {
739 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
740 }
741
742 if (addtmp)
743 {
744 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
745 }
746
747 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
748
749 (*self->mode_func_ptr)(self);
750
751 return (PyObject *)self;
752 }
753
754 static PyObject * Follower2_getServer(Follower2* self) { GET_SERVER };
755 static PyObject * Follower2_getStream(Follower2* self) { GET_STREAM };
756 static PyObject * Follower2_setMul(Follower2 *self, PyObject *arg) { SET_MUL };
757 static PyObject * Follower2_setAdd(Follower2 *self, PyObject *arg) { SET_ADD };
758 static PyObject * Follower2_setSub(Follower2 *self, PyObject *arg) { SET_SUB };
759 static PyObject * Follower2_setDiv(Follower2 *self, PyObject *arg) { SET_DIV };
760
761 static PyObject * Follower2_play(Follower2 *self, PyObject *args, PyObject *kwds) { PLAY };
762 static PyObject * Follower2_stop(Follower2 *self, PyObject *args, PyObject *kwds) { STOP };
763
764 static PyObject * Follower2_multiply(Follower2 *self, PyObject *arg) { MULTIPLY };
765 static PyObject * Follower2_inplace_multiply(Follower2 *self, PyObject *arg) { INPLACE_MULTIPLY };
766 static PyObject * Follower2_add(Follower2 *self, PyObject *arg) { ADD };
767 static PyObject * Follower2_inplace_add(Follower2 *self, PyObject *arg) { INPLACE_ADD };
768 static PyObject * Follower2_sub(Follower2 *self, PyObject *arg) { SUB };
769 static PyObject * Follower2_inplace_sub(Follower2 *self, PyObject *arg) { INPLACE_SUB };
770 static PyObject * Follower2_div(Follower2 *self, PyObject *arg) { DIV };
771 static PyObject * Follower2_inplace_div(Follower2 *self, PyObject *arg) { INPLACE_DIV };
772
773 static PyObject * Follower2_setRisetime(Follower2 *self, PyObject *arg) { SET_PARAM(self->risetime, self->risetime_stream, 2); }
774 static PyObject * Follower2_setFalltime(Follower2 *self, PyObject *arg) { SET_PARAM(self->falltime, self->falltime_stream, 3); }
775
776 static PyMemberDef Follower2_members[] =
777 {
778 {"server", T_OBJECT_EX, offsetof(Follower2, server), 0, "Pyo server."},
779 {"stream", T_OBJECT_EX, offsetof(Follower2, stream), 0, "Stream object."},
780 {"input", T_OBJECT_EX, offsetof(Follower2, input), 0, "Input sound object."},
781 {"risetime", T_OBJECT_EX, offsetof(Follower2, risetime), 0, "Risetime in second."},
782 {"falltime", T_OBJECT_EX, offsetof(Follower2, falltime), 0, "Falltime in second."},
783 {"mul", T_OBJECT_EX, offsetof(Follower2, mul), 0, "Mul factor."},
784 {"add", T_OBJECT_EX, offsetof(Follower2, add), 0, "Add factor."},
785 {NULL} /* Sentinel */
786 };
787
788 static PyMethodDef Follower2_methods[] =
789 {
790 {"getServer", (PyCFunction)Follower2_getServer, METH_NOARGS, "Returns server object."},
791 {"_getStream", (PyCFunction)Follower2_getStream, METH_NOARGS, "Returns stream object."},
792 {"play", (PyCFunction)Follower2_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
793 {"stop", (PyCFunction)Follower2_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
794 {"setRisetime", (PyCFunction)Follower2_setRisetime, METH_O, "Sets filter risetime in second."},
795 {"setFalltime", (PyCFunction)Follower2_setFalltime, METH_O, "Sets filter falltime in second."},
796 {"setMul", (PyCFunction)Follower2_setMul, METH_O, "Sets oscillator mul factor."},
797 {"setAdd", (PyCFunction)Follower2_setAdd, METH_O, "Sets oscillator add factor."},
798 {"setSub", (PyCFunction)Follower2_setSub, METH_O, "Sets inverse add factor."},
799 {"setDiv", (PyCFunction)Follower2_setDiv, METH_O, "Sets inverse mul factor."},
800 {NULL} /* Sentinel */
801 };
802
803 static PyNumberMethods Follower2_as_number =
804 {
805 (binaryfunc)Follower2_add, /*nb_add*/
806 (binaryfunc)Follower2_sub, /*nb_subtract*/
807 (binaryfunc)Follower2_multiply, /*nb_multiply*/
808 0, /*nb_remainder*/
809 0, /*nb_divmod*/
810 0, /*nb_power*/
811 0, /*nb_neg*/
812 0, /*nb_pos*/
813 0, /*(unaryfunc)array_abs,*/
814 0, /*nb_nonzero*/
815 0, /*nb_invert*/
816 0, /*nb_lshift*/
817 0, /*nb_rshift*/
818 0, /*nb_and*/
819 0, /*nb_xor*/
820 0, /*nb_or*/
821 0, /*nb_int*/
822 0, /*nb_long*/
823 0, /*nb_float*/
824 (binaryfunc)Follower2_inplace_add, /*inplace_add*/
825 (binaryfunc)Follower2_inplace_sub, /*inplace_subtract*/
826 (binaryfunc)Follower2_inplace_multiply, /*inplace_multiply*/
827 0, /*inplace_remainder*/
828 0, /*inplace_power*/
829 0, /*inplace_lshift*/
830 0, /*inplace_rshift*/
831 0, /*inplace_and*/
832 0, /*inplace_xor*/
833 0, /*inplace_or*/
834 0, /*nb_floor_divide*/
835 (binaryfunc)Follower2_div, /*nb_true_divide*/
836 0, /*nb_inplace_floor_divide*/
837 (binaryfunc)Follower2_inplace_div, /*nb_inplace_true_divide*/
838 0, /* nb_index */
839 };
840
841 PyTypeObject Follower2Type =
842 {
843 PyVarObject_HEAD_INIT(NULL, 0)
844 "_pyo.Follower2_base", /*tp_name*/
845 sizeof(Follower2), /*tp_basicsize*/
846 0, /*tp_itemsize*/
847 (destructor)Follower2_dealloc, /*tp_dealloc*/
848 0, /*tp_print*/
849 0, /*tp_getattr*/
850 0, /*tp_setattr*/
851 0, /*tp_as_async (tp_compare in Python 2)*/
852 0, /*tp_repr*/
853 &Follower2_as_number, /*tp_as_number*/
854 0, /*tp_as_sequence*/
855 0, /*tp_as_mapping*/
856 0, /*tp_hash */
857 0, /*tp_call*/
858 0, /*tp_str*/
859 0, /*tp_getattro*/
860 0, /*tp_setattro*/
861 0, /*tp_as_buffer*/
862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
863 "Follower2 objects. Envelope follower.", /* tp_doc */
864 (traverseproc)Follower2_traverse, /* tp_traverse */
865 (inquiry)Follower2_clear, /* tp_clear */
866 0, /* tp_richcompare */
867 0, /* tp_weaklistoffset */
868 0, /* tp_iter */
869 0, /* tp_iternext */
870 Follower2_methods, /* tp_methods */
871 Follower2_members, /* tp_members */
872 0, /* tp_getset */
873 0, /* tp_base */
874 0, /* tp_dict */
875 0, /* tp_descr_get */
876 0, /* tp_descr_set */
877 0, /* tp_dictoffset */
878 0, /* tp_init */
879 0, /* tp_alloc */
880 Follower2_new, /* tp_new */
881 };
882
883 /************/
884 /* ZCross */
885 /************/
886 typedef struct
887 {
888 pyo_audio_HEAD
889 PyObject *input;
890 Stream *input_stream;
891 MYFLT thresh;
892 MYFLT lastValue;
893 MYFLT lastSample;
894 int modebuffer[2]; // need at least 2 slots for mul & add
895 } ZCross;
896
897 static void
898 ZCross_process(ZCross *self)
899 {
900 int i;
901 int count = 0;
902 MYFLT inval;
903 MYFLT *in = Stream_getData((Stream *)self->input_stream);
904
905 for (i = 0; i < self->bufsize; i++)
906 {
907 self->data[i] = self->lastValue;
908 inval = in[i];
909
910 if (self->lastSample >= 0.0)
911 {
912 if (inval < 0.0 && (self->lastSample - inval) > self->thresh)
913 count++;
914 }
915 else
916 {
917 if (inval >= 0.0 && (inval - self->lastSample) > self->thresh)
918 count++;
919 }
920
921 self->lastSample = inval;
922 }
923
924 self->lastValue = (MYFLT)count / self->bufsize;
925 }
926
927 static void ZCross_postprocessing_ii(ZCross *self) { POST_PROCESSING_II };
928 static void ZCross_postprocessing_ai(ZCross *self) { POST_PROCESSING_AI };
929 static void ZCross_postprocessing_ia(ZCross *self) { POST_PROCESSING_IA };
930 static void ZCross_postprocessing_aa(ZCross *self) { POST_PROCESSING_AA };
931 static void ZCross_postprocessing_ireva(ZCross *self) { POST_PROCESSING_IREVA };
932 static void ZCross_postprocessing_areva(ZCross *self) { POST_PROCESSING_AREVA };
933 static void ZCross_postprocessing_revai(ZCross *self) { POST_PROCESSING_REVAI };
934 static void ZCross_postprocessing_revaa(ZCross *self) { POST_PROCESSING_REVAA };
935 static void ZCross_postprocessing_revareva(ZCross *self) { POST_PROCESSING_REVAREVA };
936
937 static void
938 ZCross_setProcMode(ZCross *self)
939 {
940 int muladdmode;
941 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
942
943 self->proc_func_ptr = ZCross_process;
944
945 switch (muladdmode)
946 {
947 case 0:
948 self->muladd_func_ptr = ZCross_postprocessing_ii;
949 break;
950
951 case 1:
952 self->muladd_func_ptr = ZCross_postprocessing_ai;
953 break;
954
955 case 2:
956 self->muladd_func_ptr = ZCross_postprocessing_revai;
957 break;
958
959 case 10:
960 self->muladd_func_ptr = ZCross_postprocessing_ia;
961 break;
962
963 case 11:
964 self->muladd_func_ptr = ZCross_postprocessing_aa;
965 break;
966
967 case 12:
968 self->muladd_func_ptr = ZCross_postprocessing_revaa;
969 break;
970
971 case 20:
972 self->muladd_func_ptr = ZCross_postprocessing_ireva;
973 break;
974
975 case 21:
976 self->muladd_func_ptr = ZCross_postprocessing_areva;
977 break;
978
979 case 22:
980 self->muladd_func_ptr = ZCross_postprocessing_revareva;
981 break;
982 }
983 }
984
985 static void
986 ZCross_compute_next_data_frame(ZCross *self)
987 {
988 (*self->proc_func_ptr)(self);
989 (*self->muladd_func_ptr)(self);
990 }
991
992 static int
993 ZCross_traverse(ZCross *self, visitproc visit, void *arg)
994 {
995 pyo_VISIT
996 Py_VISIT(self->input);
997 return 0;
998 }
999
1000 static int
1001 ZCross_clear(ZCross *self)
1002 {
1003 pyo_CLEAR
1004 Py_CLEAR(self->input);
1005 return 0;
1006 }
1007
1008 static void
1009 ZCross_dealloc(ZCross* self)
1010 {
1011 pyo_DEALLOC
1012 ZCross_clear(self);
1013 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1014 Py_TYPE(self)->tp_free((PyObject*)self);
1015 }
1016
1017 static PyObject *
1018 ZCross_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1019 {
1020 int i;
1021 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
1022 ZCross *self;
1023 self = (ZCross *)type->tp_alloc(type, 0);
1024
1025 self->thresh = 0.0;
1026 self->lastValue = self->lastSample = 0.0;
1027 self->modebuffer[0] = 0;
1028 self->modebuffer[1] = 0;
1029
1030 INIT_OBJECT_COMMON
1031 Stream_setFunctionPtr(self->stream, ZCross_compute_next_data_frame);
1032 self->mode_func_ptr = ZCross_setProcMode;
1033
1034 static char *kwlist[] = {"input", "thresh", "mul", "add", NULL};
1035
1036 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_FOO, kwlist, &inputtmp, &self->thresh, &multmp, &addtmp))
1037 Py_RETURN_NONE;
1038
1039 INIT_INPUT_STREAM
1040
1041 if (multmp)
1042 {
1043 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1044 }
1045
1046 if (addtmp)
1047 {
1048 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1049 }
1050
1051 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1052
1053 (*self->mode_func_ptr)(self);
1054
1055 return (PyObject *)self;
1056 }
1057
1058 static PyObject * ZCross_getServer(ZCross* self) { GET_SERVER };
1059 static PyObject * ZCross_getStream(ZCross* self) { GET_STREAM };
1060 static PyObject * ZCross_setMul(ZCross *self, PyObject *arg) { SET_MUL };
1061 static PyObject * ZCross_setAdd(ZCross *self, PyObject *arg) { SET_ADD };
1062 static PyObject * ZCross_setSub(ZCross *self, PyObject *arg) { SET_SUB };
1063 static PyObject * ZCross_setDiv(ZCross *self, PyObject *arg) { SET_DIV };
1064
1065 static PyObject * ZCross_play(ZCross *self, PyObject *args, PyObject *kwds) { PLAY };
1066 static PyObject * ZCross_stop(ZCross *self, PyObject *args, PyObject *kwds) { STOP };
1067
1068 static PyObject * ZCross_multiply(ZCross *self, PyObject *arg) { MULTIPLY };
1069 static PyObject * ZCross_inplace_multiply(ZCross *self, PyObject *arg) { INPLACE_MULTIPLY };
1070 static PyObject * ZCross_add(ZCross *self, PyObject *arg) { ADD };
1071 static PyObject * ZCross_inplace_add(ZCross *self, PyObject *arg) { INPLACE_ADD };
1072 static PyObject * ZCross_sub(ZCross *self, PyObject *arg) { SUB };
1073 static PyObject * ZCross_inplace_sub(ZCross *self, PyObject *arg) { INPLACE_SUB };
1074 static PyObject * ZCross_div(ZCross *self, PyObject *arg) { DIV };
1075 static PyObject * ZCross_inplace_div(ZCross *self, PyObject *arg) { INPLACE_DIV };
1076
1077 static PyObject *
1078 ZCross_setThresh(ZCross *self, PyObject *arg)
1079 {
1080 ASSERT_ARG_NOT_NULL
1081
1082 if (PyNumber_Check(arg) == 1)
1083 {
1084 self->thresh = PyFloat_AsDouble(arg);
1085 }
1086
1087 Py_RETURN_NONE;
1088 }
1089
1090 static PyMemberDef ZCross_members[] =
1091 {
1092 {"server", T_OBJECT_EX, offsetof(ZCross, server), 0, "Pyo server."},
1093 {"stream", T_OBJECT_EX, offsetof(ZCross, stream), 0, "Stream object."},
1094 {"input", T_OBJECT_EX, offsetof(ZCross, input), 0, "Input sound object."},
1095 {"mul", T_OBJECT_EX, offsetof(ZCross, mul), 0, "Mul factor."},
1096 {"add", T_OBJECT_EX, offsetof(ZCross, add), 0, "Add factor."},
1097 {NULL} /* Sentinel */
1098 };
1099
1100 static PyMethodDef ZCross_methods[] =
1101 {
1102 {"getServer", (PyCFunction)ZCross_getServer, METH_NOARGS, "Returns server object."},
1103 {"_getStream", (PyCFunction)ZCross_getStream, METH_NOARGS, "Returns stream object."},
1104 {"play", (PyCFunction)ZCross_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1105 {"stop", (PyCFunction)ZCross_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1106 {"setThresh", (PyCFunction)ZCross_setThresh, METH_O, "Sets the threshold factor."},
1107 {"setMul", (PyCFunction)ZCross_setMul, METH_O, "Sets oscillator mul factor."},
1108 {"setAdd", (PyCFunction)ZCross_setAdd, METH_O, "Sets oscillator add factor."},
1109 {"setSub", (PyCFunction)ZCross_setSub, METH_O, "Sets inverse add factor."},
1110 {"setDiv", (PyCFunction)ZCross_setDiv, METH_O, "Sets inverse mul factor."},
1111 {NULL} /* Sentinel */
1112 };
1113
1114 static PyNumberMethods ZCross_as_number =
1115 {
1116 (binaryfunc)ZCross_add, /*nb_add*/
1117 (binaryfunc)ZCross_sub, /*nb_subtract*/
1118 (binaryfunc)ZCross_multiply, /*nb_multiply*/
1119 0, /*nb_remainder*/
1120 0, /*nb_divmod*/
1121 0, /*nb_power*/
1122 0, /*nb_neg*/
1123 0, /*nb_pos*/
1124 0, /*(unaryfunc)array_abs,*/
1125 0, /*nb_nonzero*/
1126 0, /*nb_invert*/
1127 0, /*nb_lshift*/
1128 0, /*nb_rshift*/
1129 0, /*nb_and*/
1130 0, /*nb_xor*/
1131 0, /*nb_or*/
1132 0, /*nb_int*/
1133 0, /*nb_long*/
1134 0, /*nb_float*/
1135 (binaryfunc)ZCross_inplace_add, /*inplace_add*/
1136 (binaryfunc)ZCross_inplace_sub, /*inplace_subtract*/
1137 (binaryfunc)ZCross_inplace_multiply, /*inplace_multiply*/
1138 0, /*inplace_remainder*/
1139 0, /*inplace_power*/
1140 0, /*inplace_lshift*/
1141 0, /*inplace_rshift*/
1142 0, /*inplace_and*/
1143 0, /*inplace_xor*/
1144 0, /*inplace_or*/
1145 0, /*nb_floor_divide*/
1146 (binaryfunc)ZCross_div, /*nb_true_divide*/
1147 0, /*nb_inplace_floor_divide*/
1148 (binaryfunc)ZCross_inplace_div, /*nb_inplace_true_divide*/
1149 0, /* nb_index */
1150 };
1151
1152 PyTypeObject ZCrossType =
1153 {
1154 PyVarObject_HEAD_INIT(NULL, 0)
1155 "_pyo.ZCross_base", /*tp_name*/
1156 sizeof(ZCross), /*tp_basicsize*/
1157 0, /*tp_itemsize*/
1158 (destructor)ZCross_dealloc, /*tp_dealloc*/
1159 0, /*tp_print*/
1160 0, /*tp_getattr*/
1161 0, /*tp_setattr*/
1162 0, /*tp_as_async (tp_compare in Python 2)*/
1163 0, /*tp_repr*/
1164 &ZCross_as_number, /*tp_as_number*/
1165 0, /*tp_as_sequence*/
1166 0, /*tp_as_mapping*/
1167 0, /*tp_hash */
1168 0, /*tp_call*/
1169 0, /*tp_str*/
1170 0, /*tp_getattro*/
1171 0, /*tp_setattro*/
1172 0, /*tp_as_buffer*/
1173 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1174 "ZCross objects. Returns number of zero crossing.", /* tp_doc */
1175 (traverseproc)ZCross_traverse, /* tp_traverse */
1176 (inquiry)ZCross_clear, /* tp_clear */
1177 0, /* tp_richcompare */
1178 0, /* tp_weaklistoffset */
1179 0, /* tp_iter */
1180 0, /* tp_iternext */
1181 ZCross_methods, /* tp_methods */
1182 ZCross_members, /* tp_members */
1183 0, /* tp_getset */
1184 0, /* tp_base */
1185 0, /* tp_dict */
1186 0, /* tp_descr_get */
1187 0, /* tp_descr_set */
1188 0, /* tp_dictoffset */
1189 0, /* tp_init */
1190 0, /* tp_alloc */
1191 ZCross_new, /* tp_new */
1192 };
1193
1194 static int
1195 min_elem_pos(MYFLT *buf, int size )
1196 {
1197 int i;
1198 int pos = 0;
1199
1200 for (i = 1; i < size; i++)
1201 {
1202 if (buf[i] < buf[pos])
1203 pos = i;
1204 }
1205
1206 return pos;
1207 }
1208
1209 static MYFLT
1210 quadraticInterpolation(MYFLT *buf, int period, int size)
1211 {
1212 int x0, x2;
1213 MYFLT pitch, s0, s1, s2;
1214 x0 = period < 1 ? period : period - 1;
1215 x2 = period + 1 < size ? period + 1 : period;
1216
1217 if (x0 == period)
1218 pitch = buf[period] <= buf[x2] ? period : x2;
1219 else if (x2 == period)
1220 pitch = buf[period] <= buf[x0] ? period : x0;
1221 else
1222 {
1223 s0 = buf[x0];
1224 s1 = buf[period];
1225 s2 = buf[x2];
1226 pitch = period + 0.5 * (s2 - s0) / (s2 - 2.0 * s1 + s0);
1227 }
1228
1229 return pitch;
1230 }
1231
1232 /************/
1233 /* Yin */
1234 /************/
1235 typedef struct
1236 {
1237 pyo_audio_HEAD
1238 PyObject *input;
1239 Stream *input_stream;
1240 MYFLT *input_buffer;
1241 MYFLT *yin_buffer;
1242 int winsize;
1243 int halfsize;
1244 int input_count;
1245 MYFLT tolerance;
1246 MYFLT pitch;
1247 MYFLT minfreq;
1248 MYFLT maxfreq;
1249 MYFLT cutoff;
1250 MYFLT last_cutoff;
1251 MYFLT y1;
1252 MYFLT c2;
1253 int modebuffer[2]; // need at least 2 slots for mul & add
1254 } Yin;
1255
1256 static void
1257 Yin_process(Yin *self)
1258 {
1259 int i, j, period, tau = 0;
1260 MYFLT candidate, tmp = 0.0, tmp2 = 0.0;
1261 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1262
1263 if (self->cutoff != self->last_cutoff)
1264 {
1265 if (self->cutoff <= 1.0)
1266 self->cutoff = 1.0;
1267 else if (self->cutoff >= self->sr * 0.5)
1268 self->cutoff = self->sr * 0.5;
1269
1270 self->last_cutoff = self->cutoff;
1271 self->c2 = MYEXP(-TWOPI * self->cutoff / self->sr);
1272 }
1273
1274 for (i = 0; i < self->bufsize; i++)
1275 {
1276 self->y1 = in[i] + (self->y1 - in[i]) * self->c2;
1277 self->input_buffer[self->input_count] = self->y1;
1278
1279 if (self->input_count++ == self->winsize)
1280 {
1281 self->input_count = 0;
1282
1283 self->yin_buffer[0] = 1.0;
1284
1285 for (tau = 1; tau < self->halfsize; tau++)
1286 {
1287 self->yin_buffer[tau] = 0.0;
1288
1289 for (j = 0; j < self->halfsize; j++)
1290 {
1291 tmp = self->input_buffer[j] - self->input_buffer[j + tau];
1292 self->yin_buffer[tau] += tmp * tmp;
1293 }
1294
1295 tmp2 += self->yin_buffer[tau];
1296 self->yin_buffer[tau] *= tau / tmp2;
1297 period = tau - 3;
1298
1299 if (tau > 4 && (self->yin_buffer[period] < self->tolerance) &&
1300 (self->yin_buffer[period] < self->yin_buffer[period + 1]))
1301 {
1302 candidate = quadraticInterpolation(self->yin_buffer, period, self->halfsize);
1303 goto founded;
1304 }
1305 }
1306
1307 candidate = quadraticInterpolation(self->yin_buffer, min_elem_pos(self->yin_buffer, self->halfsize), self->halfsize);
1308
1309 founded:
1310
1311 candidate = self->sr / candidate;
1312
1313 if (candidate > self->minfreq && candidate < self->maxfreq)
1314 self->pitch = candidate;
1315
1316 }
1317
1318 self->data[i] = self->pitch;
1319 }
1320 }
1321
1322 static void Yin_postprocessing_ii(Yin *self) { POST_PROCESSING_II };
1323 static void Yin_postprocessing_ai(Yin *self) { POST_PROCESSING_AI };
1324 static void Yin_postprocessing_ia(Yin *self) { POST_PROCESSING_IA };
1325 static void Yin_postprocessing_aa(Yin *self) { POST_PROCESSING_AA };
1326 static void Yin_postprocessing_ireva(Yin *self) { POST_PROCESSING_IREVA };
1327 static void Yin_postprocessing_areva(Yin *self) { POST_PROCESSING_AREVA };
1328 static void Yin_postprocessing_revai(Yin *self) { POST_PROCESSING_REVAI };
1329 static void Yin_postprocessing_revaa(Yin *self) { POST_PROCESSING_REVAA };
1330 static void Yin_postprocessing_revareva(Yin *self) { POST_PROCESSING_REVAREVA };
1331
1332 static void
1333 Yin_setProcMode(Yin *self)
1334 {
1335 int muladdmode;
1336 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1337
1338 self->proc_func_ptr = Yin_process;
1339
1340 switch (muladdmode)
1341 {
1342 case 0:
1343 self->muladd_func_ptr = Yin_postprocessing_ii;
1344 break;
1345
1346 case 1:
1347 self->muladd_func_ptr = Yin_postprocessing_ai;
1348 break;
1349
1350 case 2:
1351 self->muladd_func_ptr = Yin_postprocessing_revai;
1352 break;
1353
1354 case 10:
1355 self->muladd_func_ptr = Yin_postprocessing_ia;
1356 break;
1357
1358 case 11:
1359 self->muladd_func_ptr = Yin_postprocessing_aa;
1360 break;
1361
1362 case 12:
1363 self->muladd_func_ptr = Yin_postprocessing_revaa;
1364 break;
1365
1366 case 20:
1367 self->muladd_func_ptr = Yin_postprocessing_ireva;
1368 break;
1369
1370 case 21:
1371 self->muladd_func_ptr = Yin_postprocessing_areva;
1372 break;
1373
1374 case 22:
1375 self->muladd_func_ptr = Yin_postprocessing_revareva;
1376 break;
1377 }
1378 }
1379
1380 static void
1381 Yin_compute_next_data_frame(Yin *self)
1382 {
1383 (*self->proc_func_ptr)(self);
1384 (*self->muladd_func_ptr)(self);
1385 }
1386
1387 static int
1388 Yin_traverse(Yin *self, visitproc visit, void *arg)
1389 {
1390 pyo_VISIT
1391 Py_VISIT(self->input);
1392 return 0;
1393 }
1394
1395 static int
1396 Yin_clear(Yin *self)
1397 {
1398 pyo_CLEAR
1399 Py_CLEAR(self->input);
1400 return 0;
1401 }
1402
1403 static void
1404 Yin_dealloc(Yin* self)
1405 {
1406 pyo_DEALLOC
1407 PyMem_RawFree(self->input_buffer);
1408 PyMem_RawFree(self->yin_buffer);
1409 Yin_clear(self);
1410 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1411 Py_TYPE(self)->tp_free((PyObject*)self);
1412 }
1413
1414 static PyObject *
1415 Yin_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1416 {
1417 int i;
1418 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
1419 Yin *self;
1420 self = (Yin *)type->tp_alloc(type, 0);
1421
1422 self->winsize = 1024;
1423 self->halfsize = 512;
1424 self->input_count = 0;
1425 self->pitch = 0.;
1426 self->tolerance = 0.15;
1427 self->minfreq = 40;
1428 self->maxfreq = 1000;
1429 self->cutoff = 1000;
1430 self->last_cutoff = -1.0;
1431 self->y1 = self->c2 = 0.0;
1432 self->modebuffer[0] = 0;
1433 self->modebuffer[1] = 0;
1434
1435 INIT_OBJECT_COMMON
1436 Stream_setFunctionPtr(self->stream, Yin_compute_next_data_frame);
1437 self->mode_func_ptr = Yin_setProcMode;
1438
1439 static char *kwlist[] = {"input", "tolerance", "minfreq", "maxfreq", "cutoff", "winsize", "mul", "add", NULL};
1440
1441 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_FFFFIOO, kwlist, &inputtmp, &self->tolerance, &self->minfreq, &self->maxfreq, &self->cutoff, &self->winsize, &multmp, &addtmp))
1442 Py_RETURN_NONE;
1443
1444 INIT_INPUT_STREAM
1445
1446 if (multmp)
1447 {
1448 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1449 }
1450
1451 if (addtmp)
1452 {
1453 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1454 }
1455
1456 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1457
1458 if (self->winsize % 2 == 1)
1459 self->winsize += 1;
1460
1461 self->input_buffer = (MYFLT *)PyMem_RawRealloc(self->input_buffer, self->winsize * sizeof(MYFLT));
1462
1463 for (i = 0; i < self->winsize; i++)
1464 self->input_buffer[i] = 0.0;
1465
1466 self->halfsize = self->winsize / 2;
1467 self->yin_buffer = (MYFLT *)PyMem_RawRealloc(self->yin_buffer, self->halfsize * sizeof(MYFLT));
1468
1469 for (i = 0; i < self->halfsize; i++)
1470 self->yin_buffer[i] = 0.0;
1471
1472 (*self->mode_func_ptr)(self);
1473
1474 return (PyObject *)self;
1475 }
1476
1477 static PyObject * Yin_getServer(Yin* self) { GET_SERVER };
1478 static PyObject * Yin_getStream(Yin* self) { GET_STREAM };
1479 static PyObject * Yin_setMul(Yin *self, PyObject *arg) { SET_MUL };
1480 static PyObject * Yin_setAdd(Yin *self, PyObject *arg) { SET_ADD };
1481 static PyObject * Yin_setSub(Yin *self, PyObject *arg) { SET_SUB };
1482 static PyObject * Yin_setDiv(Yin *self, PyObject *arg) { SET_DIV };
1483
1484 static PyObject * Yin_play(Yin *self, PyObject *args, PyObject *kwds) { PLAY };
1485 static PyObject * Yin_stop(Yin *self, PyObject *args, PyObject *kwds) { STOP };
1486
1487 static PyObject * Yin_multiply(Yin *self, PyObject *arg) { MULTIPLY };
1488 static PyObject * Yin_inplace_multiply(Yin *self, PyObject *arg) { INPLACE_MULTIPLY };
1489 static PyObject * Yin_add(Yin *self, PyObject *arg) { ADD };
1490 static PyObject * Yin_inplace_add(Yin *self, PyObject *arg) { INPLACE_ADD };
1491 static PyObject * Yin_sub(Yin *self, PyObject *arg) { SUB };
1492 static PyObject * Yin_inplace_sub(Yin *self, PyObject *arg) { INPLACE_SUB };
1493 static PyObject * Yin_div(Yin *self, PyObject *arg) { DIV };
1494 static PyObject * Yin_inplace_div(Yin *self, PyObject *arg) { INPLACE_DIV };
1495
1496 static PyObject *
1497 Yin_setTolerance(Yin *self, PyObject *arg)
1498 {
1499 ASSERT_ARG_NOT_NULL
1500
1501 if (PyNumber_Check(arg))
1502 {
1503 self->tolerance = PyFloat_AsDouble(arg);
1504 }
1505
1506 Py_RETURN_NONE;
1507 }
1508
1509 static PyObject *
1510 Yin_setMinfreq(Yin *self, PyObject *arg)
1511 {
1512 ASSERT_ARG_NOT_NULL
1513
1514 if (PyNumber_Check(arg))
1515 {
1516 self->minfreq = PyFloat_AsDouble(arg);
1517 }
1518
1519 Py_RETURN_NONE;
1520 }
1521
1522 static PyObject *
1523 Yin_setMaxfreq(Yin *self, PyObject *arg)
1524 {
1525 ASSERT_ARG_NOT_NULL
1526
1527 if (PyNumber_Check(arg))
1528 {
1529 self->maxfreq = PyFloat_AsDouble(arg);
1530 }
1531
1532 Py_RETURN_NONE;
1533 }
1534
1535 static PyObject *
1536 Yin_setCutoff(Yin *self, PyObject *arg)
1537 {
1538 ASSERT_ARG_NOT_NULL
1539
1540 if (PyNumber_Check(arg))
1541 {
1542 self->cutoff = PyFloat_AsDouble(arg);
1543 }
1544
1545 Py_RETURN_NONE;
1546 }
1547
1548 static PyMemberDef Yin_members[] =
1549 {
1550 {"server", T_OBJECT_EX, offsetof(Yin, server), 0, "Pyo server."},
1551 {"stream", T_OBJECT_EX, offsetof(Yin, stream), 0, "Stream object."},
1552 {"input", T_OBJECT_EX, offsetof(Yin, input), 0, "Input sound object."},
1553 {"mul", T_OBJECT_EX, offsetof(Yin, mul), 0, "Mul factor."},
1554 {"add", T_OBJECT_EX, offsetof(Yin, add), 0, "Add factor."},
1555 {NULL} /* Sentinel */
1556 };
1557
1558 static PyMethodDef Yin_methods[] =
1559 {
1560 {"getServer", (PyCFunction)Yin_getServer, METH_NOARGS, "Returns server object."},
1561 {"_getStream", (PyCFunction)Yin_getStream, METH_NOARGS, "Returns stream object."},
1562 {"play", (PyCFunction)Yin_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1563 {"stop", (PyCFunction)Yin_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1564 {"setTolerance", (PyCFunction)Yin_setTolerance, METH_O, "Sets the tolerance factor."},
1565 {"setMinfreq", (PyCFunction)Yin_setMinfreq, METH_O, "Sets the minimum frequency in output."},
1566 {"setMaxfreq", (PyCFunction)Yin_setMaxfreq, METH_O, "Sets the maximum frequency in output."},
1567 {"setCutoff", (PyCFunction)Yin_setCutoff, METH_O, "Sets the input lowpass filter cutoff frequency."},
1568 {"setMul", (PyCFunction)Yin_setMul, METH_O, "Sets oscillator mul factor."},
1569 {"setAdd", (PyCFunction)Yin_setAdd, METH_O, "Sets oscillator add factor."},
1570 {"setSub", (PyCFunction)Yin_setSub, METH_O, "Sets inverse add factor."},
1571 {"setDiv", (PyCFunction)Yin_setDiv, METH_O, "Sets inverse mul factor."},
1572 {NULL} /* Sentinel */
1573 };
1574
1575 static PyNumberMethods Yin_as_number =
1576 {
1577 (binaryfunc)Yin_add, /*nb_add*/
1578 (binaryfunc)Yin_sub, /*nb_subtract*/
1579 (binaryfunc)Yin_multiply, /*nb_multiply*/
1580 0, /*nb_remainder*/
1581 0, /*nb_divmod*/
1582 0, /*nb_power*/
1583 0, /*nb_neg*/
1584 0, /*nb_pos*/
1585 0, /*(unaryfunc)array_abs,*/
1586 0, /*nb_nonzero*/
1587 0, /*nb_invert*/
1588 0, /*nb_lshift*/
1589 0, /*nb_rshift*/
1590 0, /*nb_and*/
1591 0, /*nb_xor*/
1592 0, /*nb_or*/
1593 0, /*nb_int*/
1594 0, /*nb_long*/
1595 0, /*nb_float*/
1596 (binaryfunc)Yin_inplace_add, /*inplace_add*/
1597 (binaryfunc)Yin_inplace_sub, /*inplace_subtract*/
1598 (binaryfunc)Yin_inplace_multiply, /*inplace_multiply*/
1599 0, /*inplace_remainder*/
1600 0, /*inplace_power*/
1601 0, /*inplace_lshift*/
1602 0, /*inplace_rshift*/
1603 0, /*inplace_and*/
1604 0, /*inplace_xor*/
1605 0, /*inplace_or*/
1606 0, /*nb_floor_divide*/
1607 (binaryfunc)Yin_div, /*nb_true_divide*/
1608 0, /*nb_inplace_floor_divide*/
1609 (binaryfunc)Yin_inplace_div, /*nb_inplace_true_divide*/
1610 0, /* nb_index */
1611 };
1612
1613 PyTypeObject YinType =
1614 {
1615 PyVarObject_HEAD_INIT(NULL, 0)
1616 "_pyo.Yin_base", /*tp_name*/
1617 sizeof(Yin), /*tp_basicsize*/
1618 0, /*tp_itemsize*/
1619 (destructor)Yin_dealloc, /*tp_dealloc*/
1620 0, /*tp_print*/
1621 0, /*tp_getattr*/
1622 0, /*tp_setattr*/
1623 0, /*tp_as_async (tp_compare in Python 2)*/
1624 0, /*tp_repr*/
1625 &Yin_as_number, /*tp_as_number*/
1626 0, /*tp_as_sequence*/
1627 0, /*tp_as_mapping*/
1628 0, /*tp_hash */
1629 0, /*tp_call*/
1630 0, /*tp_str*/
1631 0, /*tp_getattro*/
1632 0, /*tp_setattro*/
1633 0, /*tp_as_buffer*/
1634 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1635 "Yin objects. Pitch tracker using the Yin algorithm.", /* tp_doc */
1636 (traverseproc)Yin_traverse, /* tp_traverse */
1637 (inquiry)Yin_clear, /* tp_clear */
1638 0, /* tp_richcompare */
1639 0, /* tp_weaklistoffset */
1640 0, /* tp_iter */
1641 0, /* tp_iternext */
1642 Yin_methods, /* tp_methods */
1643 Yin_members, /* tp_members */
1644 0, /* tp_getset */
1645 0, /* tp_base */
1646 0, /* tp_dict */
1647 0, /* tp_descr_get */
1648 0, /* tp_descr_set */
1649 0, /* tp_dictoffset */
1650 0, /* tp_init */
1651 0, /* tp_alloc */
1652 Yin_new, /* tp_new */
1653 };
1654
1655 /********************/
1656 /* Centroid */
1657 /********************/
1658
1659 typedef struct
1660 {
1661 pyo_audio_HEAD
1662 PyObject *input;
1663 Stream *input_stream;
1664 int size;
1665 int hsize;
1666 int incount;
1667 MYFLT centroid;
1668 MYFLT *inframe;
1669 MYFLT *outframe;
1670 MYFLT **twiddle;
1671 MYFLT *input_buffer;
1672 MYFLT *window;
1673 int modebuffer[2];
1674 } Centroid;
1675
1676 static void
1677 Centroid_alloc_memories(Centroid *self)
1678 {
1679 int i, n8;
1680 self->hsize = self->size / 2;
1681 n8 = self->size >> 3;
1682 self->inframe = (MYFLT *)PyMem_RawRealloc(self->inframe, self->size * sizeof(MYFLT));
1683 self->outframe = (MYFLT *)PyMem_RawRealloc(self->outframe, self->size * sizeof(MYFLT));
1684 self->input_buffer = (MYFLT *)PyMem_RawRealloc(self->input_buffer, self->size * sizeof(MYFLT));
1685
1686 for (i = 0; i < self->size; i++)
1687 self->inframe[i] = self->outframe[i] = self->input_buffer[i] = 0.0;
1688
1689 self->twiddle = (MYFLT **)PyMem_RawRealloc(self->twiddle, 4 * sizeof(MYFLT *));
1690
1691 for (i = 0; i < 4; i++)
1692 self->twiddle[i] = (MYFLT *)PyMem_RawMalloc(n8 * sizeof(MYFLT));
1693
1694 fft_compute_split_twiddle(self->twiddle, self->size);
1695 self->window = (MYFLT *)PyMem_RawRealloc(self->window, self->size * sizeof(MYFLT));
1696 gen_window(self->window, self->size, 2);
1697 }
1698
1699 static void
1700 Centroid_process_i(Centroid *self)
1701 {
1702 int i;
1703 MYFLT re, im, tmp, sum1, sum2;
1704 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1705
1706 for (i = 0; i < self->bufsize; i++)
1707 {
1708 self->input_buffer[self->incount] = in[i];
1709 self->data[i] = self->centroid;
1710
1711 self->incount++;
1712
1713 if (self->incount == self->size)
1714 {
1715 self->incount = self->hsize;
1716
1717 for (i = 0; i < self->size; i++)
1718 {
1719 self->inframe[i] = self->input_buffer[i] * self->window[i];
1720 }
1721
1722 realfft_split(self->inframe, self->outframe, self->size, self->twiddle);
1723 sum1 = sum2 = 0.0;
1724
1725 for (i = 1; i < self->hsize; i++)
1726 {
1727 re = self->outframe[i];
1728 im = self->outframe[self->size - i];
1729 tmp = MYSQRT(re * re + im * im);
1730 sum1 += tmp * i;
1731 sum2 += tmp;
1732 }
1733
1734 if (sum2 < 0.000000001)
1735 tmp = 0.0;
1736 else
1737 tmp = sum1 / sum2;
1738
1739 self->centroid += tmp * self->sr / self->size;
1740 self->centroid *= 0.5;
1741
1742 for (i = 0; i < self->hsize; i++)
1743 {
1744 self->input_buffer[i] = self->input_buffer[i + self->hsize];
1745 }
1746 }
1747 }
1748 }
1749
1750 static void Centroid_postprocessing_ii(Centroid *self) { POST_PROCESSING_II };
1751 static void Centroid_postprocessing_ai(Centroid *self) { POST_PROCESSING_AI };
1752 static void Centroid_postprocessing_ia(Centroid *self) { POST_PROCESSING_IA };
1753 static void Centroid_postprocessing_aa(Centroid *self) { POST_PROCESSING_AA };
1754 static void Centroid_postprocessing_ireva(Centroid *self) { POST_PROCESSING_IREVA };
1755 static void Centroid_postprocessing_areva(Centroid *self) { POST_PROCESSING_AREVA };
1756 static void Centroid_postprocessing_revai(Centroid *self) { POST_PROCESSING_REVAI };
1757 static void Centroid_postprocessing_revaa(Centroid *self) { POST_PROCESSING_REVAA };
1758 static void Centroid_postprocessing_revareva(Centroid *self) { POST_PROCESSING_REVAREVA };
1759
1760 static void
1761 Centroid_setProcMode(Centroid *self)
1762 {
1763 int muladdmode;
1764 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1765
1766 self->proc_func_ptr = Centroid_process_i;
1767
1768 switch (muladdmode)
1769 {
1770 case 0:
1771 self->muladd_func_ptr = Centroid_postprocessing_ii;
1772 break;
1773
1774 case 1:
1775 self->muladd_func_ptr = Centroid_postprocessing_ai;
1776 break;
1777
1778 case 2:
1779 self->muladd_func_ptr = Centroid_postprocessing_revai;
1780 break;
1781
1782 case 10:
1783 self->muladd_func_ptr = Centroid_postprocessing_ia;
1784 break;
1785
1786 case 11:
1787 self->muladd_func_ptr = Centroid_postprocessing_aa;
1788 break;
1789
1790 case 12:
1791 self->muladd_func_ptr = Centroid_postprocessing_revaa;
1792 break;
1793
1794 case 20:
1795 self->muladd_func_ptr = Centroid_postprocessing_ireva;
1796 break;
1797
1798 case 21:
1799 self->muladd_func_ptr = Centroid_postprocessing_areva;
1800 break;
1801
1802 case 22:
1803 self->muladd_func_ptr = Centroid_postprocessing_revareva;
1804 break;
1805 }
1806 }
1807
1808 static void
1809 Centroid_compute_next_data_frame(Centroid *self)
1810 {
1811 (*self->proc_func_ptr)(self);
1812 (*self->muladd_func_ptr)(self);
1813 }
1814
1815 static int
1816 Centroid_traverse(Centroid *self, visitproc visit, void *arg)
1817 {
1818 pyo_VISIT
1819 Py_VISIT(self->input);
1820 return 0;
1821 }
1822
1823 static int
1824 Centroid_clear(Centroid *self)
1825 {
1826 pyo_CLEAR
1827 Py_CLEAR(self->input);
1828 return 0;
1829 }
1830
1831 static void
1832 Centroid_dealloc(Centroid* self)
1833 {
1834 int i;
1835 pyo_DEALLOC
1836 PyMem_RawFree(self->inframe);
1837 PyMem_RawFree(self->outframe);
1838 PyMem_RawFree(self->input_buffer);
1839
1840 for (i = 0; i < 4; i++)
1841 {
1842 PyMem_RawFree(self->twiddle[i]);
1843 }
1844
1845 PyMem_RawFree(self->twiddle);
1846 PyMem_RawFree(self->window);
1847 Centroid_clear(self);
1848 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1849 Py_TYPE(self)->tp_free((PyObject*)self);
1850 }
1851
1852 static PyObject *
1853 Centroid_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1854 {
1855 int i, k;
1856 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
1857 Centroid *self;
1858 self = (Centroid *)type->tp_alloc(type, 0);
1859
1860 self->centroid = 0;
1861 self->size = 1024;
1862 INIT_OBJECT_COMMON
1863 Stream_setFunctionPtr(self->stream, Centroid_compute_next_data_frame);
1864 self->mode_func_ptr = Centroid_setProcMode;
1865
1866 static char *kwlist[] = {"input", "size", "mul", "add", NULL};
1867
1868 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &inputtmp, &self->size, &multmp, &addtmp))
1869 Py_RETURN_NONE;
1870
1871 if (self->size < self->bufsize)
1872 {
1873 PySys_WriteStdout("Warning : Centroid size less than buffer size!\nCentroid size set to buffersize: %d\n", self->bufsize);
1874 self->size = self->bufsize;
1875 }
1876
1877 k = 1;
1878
1879 while (k < self->size)
1880 k <<= 1;
1881
1882 self->size = k;
1883
1884 INIT_INPUT_STREAM
1885
1886 if (multmp)
1887 {
1888 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1889 }
1890
1891 if (addtmp)
1892 {
1893 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1894 }
1895
1896 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1897
1898 Centroid_alloc_memories(self);
1899
1900 self->incount = self->hsize;
1901
1902 (*self->mode_func_ptr)(self);
1903
1904 return (PyObject *)self;
1905 }
1906
1907 static PyObject * Centroid_getServer(Centroid* self) { GET_SERVER };
1908 static PyObject * Centroid_getStream(Centroid* self) { GET_STREAM };
1909 static PyObject * Centroid_setMul(Centroid *self, PyObject *arg) { SET_MUL };
1910 static PyObject * Centroid_setAdd(Centroid *self, PyObject *arg) { SET_ADD };
1911 static PyObject * Centroid_setSub(Centroid *self, PyObject *arg) { SET_SUB };
1912 static PyObject * Centroid_setDiv(Centroid *self, PyObject *arg) { SET_DIV };
1913
1914 static PyObject * Centroid_play(Centroid *self, PyObject *args, PyObject *kwds) { PLAY };
1915 static PyObject * Centroid_stop(Centroid *self, PyObject *args, PyObject *kwds) { STOP };
1916
1917 static PyObject * Centroid_multiply(Centroid *self, PyObject *arg) { MULTIPLY };
1918 static PyObject * Centroid_inplace_multiply(Centroid *self, PyObject *arg) { INPLACE_MULTIPLY };
1919 static PyObject * Centroid_add(Centroid *self, PyObject *arg) { ADD };
1920 static PyObject * Centroid_inplace_add(Centroid *self, PyObject *arg) { INPLACE_ADD };
1921 static PyObject * Centroid_sub(Centroid *self, PyObject *arg) { SUB };
1922 static PyObject * Centroid_inplace_sub(Centroid *self, PyObject *arg) { INPLACE_SUB };
1923 static PyObject * Centroid_div(Centroid *self, PyObject *arg) { DIV };
1924 static PyObject * Centroid_inplace_div(Centroid *self, PyObject *arg) { INPLACE_DIV };
1925
1926
1927 static PyMemberDef Centroid_members[] =
1928 {
1929 {"server", T_OBJECT_EX, offsetof(Centroid, server), 0, "Pyo server."},
1930 {"stream", T_OBJECT_EX, offsetof(Centroid, stream), 0, "Stream object."},
1931 {"mul", T_OBJECT_EX, offsetof(Centroid, mul), 0, "Mul factor."},
1932 {"add", T_OBJECT_EX, offsetof(Centroid, add), 0, "Add factor."},
1933 {"input", T_OBJECT_EX, offsetof(Centroid, input), 0, "Input sound object."},
1934 {NULL} /* Sentinel */
1935 };
1936
1937 static PyMethodDef Centroid_methods[] =
1938 {
1939 {"getServer", (PyCFunction)Centroid_getServer, METH_NOARGS, "Returns server object."},
1940 {"_getStream", (PyCFunction)Centroid_getStream, METH_NOARGS, "Returns stream object."},
1941 {"play", (PyCFunction)Centroid_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1942 {"stop", (PyCFunction)Centroid_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1943 {"setMul", (PyCFunction)Centroid_setMul, METH_O, "Sets Centroid mul factor."},
1944 {"setAdd", (PyCFunction)Centroid_setAdd, METH_O, "Sets Centroid add factor."},
1945 {"setSub", (PyCFunction)Centroid_setSub, METH_O, "Sets Centroid add factor."},
1946 {"setDiv", (PyCFunction)Centroid_setDiv, METH_O, "Sets Centroid mul factor."},
1947 {NULL} /* Sentinel */
1948 };
1949
1950 static PyNumberMethods Centroid_as_number =
1951 {
1952 (binaryfunc)Centroid_add, /*nb_add*/
1953 (binaryfunc)Centroid_sub, /*nb_subtract*/
1954 (binaryfunc)Centroid_multiply, /*nb_multiply*/
1955 0, /*nb_remainder*/
1956 0, /*nb_divmod*/
1957 0, /*nb_power*/
1958 0, /*nb_neg*/
1959 0, /*nb_pos*/
1960 0, /*(unaryfunc)array_abs,*/
1961 0, /*nb_nonzero*/
1962 0, /*nb_invert*/
1963 0, /*nb_lshift*/
1964 0, /*nb_rshift*/
1965 0, /*nb_and*/
1966 0, /*nb_xor*/
1967 0, /*nb_or*/
1968 0, /*nb_int*/
1969 0, /*nb_long*/
1970 0, /*nb_float*/
1971 (binaryfunc)Centroid_inplace_add, /*inplace_add*/
1972 (binaryfunc)Centroid_inplace_sub, /*inplace_subtract*/
1973 (binaryfunc)Centroid_inplace_multiply, /*inplace_multiply*/
1974 0, /*inplace_remainder*/
1975 0, /*inplace_power*/
1976 0, /*inplace_lshift*/
1977 0, /*inplace_rshift*/
1978 0, /*inplace_and*/
1979 0, /*inplace_xor*/
1980 0, /*inplace_or*/
1981 0, /*nb_floor_divide*/
1982 (binaryfunc)Centroid_div, /*nb_true_divide*/
1983 0, /*nb_inplace_floor_divide*/
1984 (binaryfunc)Centroid_inplace_div, /*nb_inplace_true_divide*/
1985 0, /* nb_index */
1986 };
1987
1988 PyTypeObject CentroidType =
1989 {
1990 PyVarObject_HEAD_INIT(NULL, 0)
1991 "_pyo.Centroid_base", /*tp_name*/
1992 sizeof(Centroid), /*tp_basicsize*/
1993 0, /*tp_itemsize*/
1994 (destructor)Centroid_dealloc, /*tp_dealloc*/
1995 0, /*tp_print*/
1996 0, /*tp_getattr*/
1997 0, /*tp_setattr*/
1998 0, /*tp_as_async (tp_compare in Python 2)*/
1999 0, /*tp_repr*/
2000 &Centroid_as_number, /*tp_as_number*/
2001 0, /*tp_as_sequence*/
2002 0, /*tp_as_mapping*/
2003 0, /*tp_hash */
2004 0, /*tp_call*/
2005 0, /*tp_str*/
2006 0, /*tp_getattro*/
2007 0, /*tp_setattro*/
2008 0, /*tp_as_buffer*/
2009 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2010 "Centroid objects. FFT transform.", /* tp_doc */
2011 (traverseproc)Centroid_traverse, /* tp_traverse */
2012 (inquiry)Centroid_clear, /* tp_clear */
2013 0, /* tp_richcompare */
2014 0, /* tp_weaklistoffset */
2015 0, /* tp_iter */
2016 0, /* tp_iternext */
2017 Centroid_methods, /* tp_methods */
2018 Centroid_members, /* tp_members */
2019 0, /* tp_getset */
2020 0, /* tp_base */
2021 0, /* tp_dict */
2022 0, /* tp_descr_get */
2023 0, /* tp_descr_set */
2024 0, /* tp_dictoffset */
2025 0, /* tp_init */
2026 0, /* tp_alloc */
2027 Centroid_new, /* tp_new */
2028 };
2029
2030 /************/
2031 /* AttackDetector */
2032 /************/
2033 typedef struct
2034 {
2035 pyo_audio_HEAD
2036 PyObject *input;
2037 Stream *input_stream;
2038 MYFLT deltime;
2039 MYFLT cutoff;
2040 MYFLT maxthresh;
2041 MYFLT minthresh;
2042 MYFLT reltime;
2043 MYFLT folfactor;
2044 MYFLT follow;
2045 MYFLT followdb;
2046 MYFLT *buffer;
2047 MYFLT previous;
2048 int memsize;
2049 int sampdel;
2050 int incount;
2051 int overminok;
2052 int belowminok;
2053 long maxtime;
2054 long timer;
2055 int modebuffer[2]; // need at least 2 slots for mul & add
2056 } AttackDetector;
2057
2058 static void
2059 AttackDetector_process(AttackDetector *self)
2060 {
2061 int i, ind;
2062 MYFLT absin;
2063 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2064
2065 for (i = 0; i < self->bufsize; i++)
2066 {
2067 self->data[i] = 0.0;
2068 absin = in[i];
2069
2070 // envelope follower
2071 if (absin < 0.0)
2072 absin = -absin;
2073
2074 self->follow = absin + self->folfactor * (self->follow - absin);
2075
2076 // follower in dB
2077 if (self->follow <= 0.000001)
2078 self->followdb = -120.0;
2079 else
2080 self->followdb = 20.0 * MYLOG10(self->follow);
2081
2082 // previous analysis
2083 ind = self->incount - self->sampdel;
2084
2085 if (ind < 0)
2086 ind += self->memsize;
2087
2088 self->previous = self->buffer[ind];
2089 self->buffer[self->incount] = self->followdb;
2090 self->incount++;
2091
2092 if (self->incount >= self->memsize)
2093 self->incount = 0;
2094
2095 // if release time has past
2096 if (self->timer >= self->maxtime)
2097 {
2098 // if rms is over min threshold
2099 if (self->overminok)
2100 {
2101 // if rms is greater than previous + maxthresh
2102 if (self->followdb > (self->previous + self->maxthresh))
2103 {
2104 self->data[i] = 1.0;
2105 self->overminok = self->belowminok = 0;
2106 self->timer = 0;
2107 }
2108 }
2109 }
2110
2111 if (self->belowminok == 0 && self->followdb < self->minthresh)
2112 self->belowminok = 1;
2113 else if (self->belowminok == 1 && self->followdb > self->minthresh)
2114 self->overminok = 1;
2115
2116 self->timer++;
2117 }
2118 }
2119
2120 static void AttackDetector_postprocessing_ii(AttackDetector *self) { POST_PROCESSING_II };
2121 static void AttackDetector_postprocessing_ai(AttackDetector *self) { POST_PROCESSING_AI };
2122 static void AttackDetector_postprocessing_ia(AttackDetector *self) { POST_PROCESSING_IA };
2123 static void AttackDetector_postprocessing_aa(AttackDetector *self) { POST_PROCESSING_AA };
2124 static void AttackDetector_postprocessing_ireva(AttackDetector *self) { POST_PROCESSING_IREVA };
2125 static void AttackDetector_postprocessing_areva(AttackDetector *self) { POST_PROCESSING_AREVA };
2126 static void AttackDetector_postprocessing_revai(AttackDetector *self) { POST_PROCESSING_REVAI };
2127 static void AttackDetector_postprocessing_revaa(AttackDetector *self) { POST_PROCESSING_REVAA };
2128 static void AttackDetector_postprocessing_revareva(AttackDetector *self) { POST_PROCESSING_REVAREVA };
2129
2130 static void
2131 AttackDetector_setProcMode(AttackDetector *self)
2132 {
2133 int muladdmode;
2134 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2135
2136 self->proc_func_ptr = AttackDetector_process;
2137
2138 switch (muladdmode)
2139 {
2140 case 0:
2141 self->muladd_func_ptr = AttackDetector_postprocessing_ii;
2142 break;
2143
2144 case 1:
2145 self->muladd_func_ptr = AttackDetector_postprocessing_ai;
2146 break;
2147
2148 case 2:
2149 self->muladd_func_ptr = AttackDetector_postprocessing_revai;
2150 break;
2151
2152 case 10:
2153 self->muladd_func_ptr = AttackDetector_postprocessing_ia;
2154 break;
2155
2156 case 11:
2157 self->muladd_func_ptr = AttackDetector_postprocessing_aa;
2158 break;
2159
2160 case 12:
2161 self->muladd_func_ptr = AttackDetector_postprocessing_revaa;
2162 break;
2163
2164 case 20:
2165 self->muladd_func_ptr = AttackDetector_postprocessing_ireva;
2166 break;
2167
2168 case 21:
2169 self->muladd_func_ptr = AttackDetector_postprocessing_areva;
2170 break;
2171
2172 case 22:
2173 self->muladd_func_ptr = AttackDetector_postprocessing_revareva;
2174 break;
2175 }
2176 }
2177
2178 static void
2179 AttackDetector_compute_next_data_frame(AttackDetector *self)
2180 {
2181 (*self->proc_func_ptr)(self);
2182 (*self->muladd_func_ptr)(self);
2183 }
2184
2185 static int
2186 AttackDetector_traverse(AttackDetector *self, visitproc visit, void *arg)
2187 {
2188 pyo_VISIT
2189 Py_VISIT(self->input);
2190 return 0;
2191 }
2192
2193 static int
2194 AttackDetector_clear(AttackDetector *self)
2195 {
2196 pyo_CLEAR
2197 Py_CLEAR(self->input);
2198 return 0;
2199 }
2200
2201 static void
2202 AttackDetector_dealloc(AttackDetector* self)
2203 {
2204 pyo_DEALLOC
2205 PyMem_RawFree(self->buffer);
2206 AttackDetector_clear(self);
2207 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2208 Py_TYPE(self)->tp_free((PyObject*)self);
2209 }
2210
2211 static PyObject *
2212 AttackDetector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2213 {
2214 int i;
2215 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
2216 AttackDetector *self;
2217 self = (AttackDetector *)type->tp_alloc(type, 0);
2218
2219 self->deltime = 0.005;
2220 self->cutoff = 10.0;
2221 self->maxthresh = 3.0;
2222 self->minthresh = -30.0;
2223 self->reltime = 0.1;
2224 self->follow = 0.0;
2225 self->followdb = -120.0;
2226 self->previous = 0.0;
2227 self->incount = 0;
2228 self->overminok = 0;
2229 self->belowminok = 0;
2230 self->timer = 0;
2231 self->modebuffer[0] = 0;
2232 self->modebuffer[1] = 0;
2233
2234 INIT_OBJECT_COMMON
2235 Stream_setFunctionPtr(self->stream, AttackDetector_compute_next_data_frame);
2236 self->mode_func_ptr = AttackDetector_setProcMode;
2237
2238 static char *kwlist[] = {"input", "deltime", "cutoff", "maxthresh", "minthresh", "reltime", "mul", "add", NULL};
2239
2240 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_FFFFFOO, kwlist, &inputtmp, &self->deltime, &self->cutoff, &self->maxthresh, &self->minthresh, &self->reltime, &multmp, &addtmp))
2241 Py_RETURN_NONE;
2242
2243 INIT_INPUT_STREAM
2244
2245 if (multmp)
2246 {
2247 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2248 }
2249
2250 if (addtmp)
2251 {
2252 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2253 }
2254
2255 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2256
2257 self->memsize = (int)(0.055 * self->sr + 0.5);
2258 self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, (self->memsize + 1) * sizeof(MYFLT));
2259
2260 for (i = 0; i < (self->memsize + 1); i++)
2261 {
2262 self->buffer[i] = 0.0;
2263 }
2264
2265 if (self->deltime < 0.001) self->deltime = 0.001;
2266 else if (self->deltime > 0.05) self->deltime = 0.05;
2267
2268 self->sampdel = (int)(self->deltime * self->sr);
2269
2270 if (self->cutoff < 1.0) self->cutoff = 1.0;
2271 else if (self->cutoff > 1000.0) self->cutoff = 1000.0;
2272
2273 self->folfactor = MYEXP(-TWOPI * self->cutoff / self->sr);
2274
2275 if (self->cutoff < 1.0) self->cutoff = 1.0;
2276 else if (self->cutoff > 1000.0) self->cutoff = 1000.0;
2277
2278 if (self->maxthresh < 0.0) self->maxthresh = 0.0;
2279 else if (self->maxthresh > 18.0) self->maxthresh = 18.0;
2280
2281 if (self->minthresh < -90.0) self->minthresh = -90.0;
2282 else if (self->minthresh > 0.0) self->minthresh = 0.0;
2283
2284 if (self->reltime < 0.001) self->reltime = 0.001;
2285
2286 self->maxtime = (long)(self->reltime * self->sr + 0.5);
2287
2288 (*self->mode_func_ptr)(self);
2289
2290 return (PyObject *)self;
2291 }
2292
2293 static PyObject * AttackDetector_getServer(AttackDetector* self) { GET_SERVER };
2294 static PyObject * AttackDetector_getStream(AttackDetector* self) { GET_STREAM };
2295 static PyObject * AttackDetector_setMul(AttackDetector *self, PyObject *arg) { SET_MUL };
2296 static PyObject * AttackDetector_setAdd(AttackDetector *self, PyObject *arg) { SET_ADD };
2297 static PyObject * AttackDetector_setSub(AttackDetector *self, PyObject *arg) { SET_SUB };
2298 static PyObject * AttackDetector_setDiv(AttackDetector *self, PyObject *arg) { SET_DIV };
2299
2300 static PyObject * AttackDetector_play(AttackDetector *self, PyObject *args, PyObject *kwds) { PLAY };
2301 static PyObject * AttackDetector_stop(AttackDetector *self, PyObject *args, PyObject *kwds) { STOP };
2302
2303 static PyObject * AttackDetector_multiply(AttackDetector *self, PyObject *arg) { MULTIPLY };
2304 static PyObject * AttackDetector_inplace_multiply(AttackDetector *self, PyObject *arg) { INPLACE_MULTIPLY };
2305 static PyObject * AttackDetector_add(AttackDetector *self, PyObject *arg) { ADD };
2306 static PyObject * AttackDetector_inplace_add(AttackDetector *self, PyObject *arg) { INPLACE_ADD };
2307 static PyObject * AttackDetector_sub(AttackDetector *self, PyObject *arg) { SUB };
2308 static PyObject * AttackDetector_inplace_sub(AttackDetector *self, PyObject *arg) { INPLACE_SUB };
2309 static PyObject * AttackDetector_div(AttackDetector *self, PyObject *arg) { DIV };
2310 static PyObject * AttackDetector_inplace_div(AttackDetector *self, PyObject *arg) { INPLACE_DIV };
2311
2312 static PyObject *
2313 AttackDetector_setDeltime(AttackDetector *self, PyObject *arg)
2314 {
2315 ASSERT_ARG_NOT_NULL
2316
2317 if (PyNumber_Check(arg))
2318 {
2319 self->deltime = PyFloat_AsDouble(arg);
2320
2321 if (self->deltime < 0.001) self->deltime = 0.001;
2322 else if (self->deltime > 0.05) self->deltime = 0.05;
2323
2324 self->sampdel = (int)(self->deltime * self->sr);
2325 }
2326
2327 Py_RETURN_NONE;
2328 }
2329
2330 static PyObject *
2331 AttackDetector_setCutoff(AttackDetector *self, PyObject *arg)
2332 {
2333 ASSERT_ARG_NOT_NULL
2334
2335 if (PyNumber_Check(arg))
2336 {
2337 self->cutoff = PyFloat_AsDouble(arg);
2338
2339 if (self->cutoff < 1.0) self->cutoff = 1.0;
2340 else if (self->cutoff > 1000.0) self->cutoff = 1000.0;
2341
2342 self->folfactor = MYEXP(-TWOPI * self->cutoff / self->sr);
2343 }
2344
2345 Py_RETURN_NONE;
2346 }
2347
2348 static PyObject *
2349 AttackDetector_setMaxthresh(AttackDetector *self, PyObject *arg)
2350 {
2351 ASSERT_ARG_NOT_NULL
2352
2353 if (PyNumber_Check(arg))
2354 {
2355 self->maxthresh = PyFloat_AsDouble(arg);
2356
2357 if (self->maxthresh < 0.0) self->maxthresh = 0.0;
2358 else if (self->maxthresh > 18.0) self->maxthresh = 18.0;
2359 }
2360
2361 Py_RETURN_NONE;
2362 }
2363
2364 static PyObject *
2365 AttackDetector_setMinthresh(AttackDetector *self, PyObject *arg)
2366 {
2367 ASSERT_ARG_NOT_NULL
2368
2369 if (PyNumber_Check(arg))
2370 {
2371 self->minthresh = PyFloat_AsDouble(arg);
2372
2373 if (self->minthresh < -90.0) self->minthresh = -90.0;
2374 else if (self->minthresh > 0.0) self->minthresh = 0.0;
2375 }
2376
2377 Py_RETURN_NONE;
2378 }
2379
2380 static PyObject *
2381 AttackDetector_setReltime(AttackDetector *self, PyObject *arg)
2382 {
2383 ASSERT_ARG_NOT_NULL
2384
2385 if (PyNumber_Check(arg))
2386 {
2387 self->reltime = PyFloat_AsDouble(arg);
2388
2389 if (self->reltime < 0.001) self->reltime = 0.001;
2390
2391 self->maxtime = (long)(self->reltime * self->sr + 0.5);
2392 }
2393
2394 Py_RETURN_NONE;
2395 }
2396
2397 static PyObject *
2398 AttackDetector_readyToDetect(AttackDetector *self)
2399 {
2400 self->overminok = 1;
2401 self->timer = self->maxtime;
2402 Py_RETURN_NONE;
2403 }
2404
2405 static PyMemberDef AttackDetector_members[] =
2406 {
2407 {"server", T_OBJECT_EX, offsetof(AttackDetector, server), 0, "Pyo server."},
2408 {"stream", T_OBJECT_EX, offsetof(AttackDetector, stream), 0, "Stream object."},
2409 {"input", T_OBJECT_EX, offsetof(AttackDetector, input), 0, "Input sound object."},
2410 {"mul", T_OBJECT_EX, offsetof(AttackDetector, mul), 0, "Mul factor."},
2411 {"add", T_OBJECT_EX, offsetof(AttackDetector, add), 0, "Add factor."},
2412 {NULL} /* Sentinel */
2413 };
2414
2415 static PyMethodDef AttackDetector_methods[] =
2416 {
2417 {"getServer", (PyCFunction)AttackDetector_getServer, METH_NOARGS, "Returns server object."},
2418 {"_getStream", (PyCFunction)AttackDetector_getStream, METH_NOARGS, "Returns stream object."},
2419 {"play", (PyCFunction)AttackDetector_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2420 {"stop", (PyCFunction)AttackDetector_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2421 {"setDeltime", (PyCFunction)AttackDetector_setDeltime, METH_O, "Sets the delay time between current and previous analysis."},
2422 {"setCutoff", (PyCFunction)AttackDetector_setCutoff, METH_O, "Sets the frequency of the internal lowpass filter."},
2423 {"setMaxthresh", (PyCFunction)AttackDetector_setMaxthresh, METH_O, "Sets the higher threshold."},
2424 {"setMinthresh", (PyCFunction)AttackDetector_setMinthresh, METH_O, "Sets the lower threshold."},
2425 {"setReltime", (PyCFunction)AttackDetector_setReltime, METH_O, "Sets the release time (min time between two detected attacks)."},
2426 {"readyToDetect", (PyCFunction)AttackDetector_readyToDetect, METH_NOARGS, "Initializes thresholds."},
2427 {"setMul", (PyCFunction)AttackDetector_setMul, METH_O, "Sets oscillator mul factor."},
2428 {"setAdd", (PyCFunction)AttackDetector_setAdd, METH_O, "Sets oscillator add factor."},
2429 {"setSub", (PyCFunction)AttackDetector_setSub, METH_O, "Sets inverse add factor."},
2430 {"setDiv", (PyCFunction)AttackDetector_setDiv, METH_O, "Sets inverse mul factor."},
2431 {NULL} /* Sentinel */
2432 };
2433
2434 static PyNumberMethods AttackDetector_as_number =
2435 {
2436 (binaryfunc)AttackDetector_add, /*nb_add*/
2437 (binaryfunc)AttackDetector_sub, /*nb_subtract*/
2438 (binaryfunc)AttackDetector_multiply, /*nb_multiply*/
2439 0, /*nb_remainder*/
2440 0, /*nb_divmod*/
2441 0, /*nb_power*/
2442 0, /*nb_neg*/
2443 0, /*nb_pos*/
2444 0, /*(unaryfunc)array_abs,*/
2445 0, /*nb_nonzero*/
2446 0, /*nb_invert*/
2447 0, /*nb_lshift*/
2448 0, /*nb_rshift*/
2449 0, /*nb_and*/
2450 0, /*nb_xor*/
2451 0, /*nb_or*/
2452 0, /*nb_int*/
2453 0, /*nb_long*/
2454 0, /*nb_float*/
2455 (binaryfunc)AttackDetector_inplace_add, /*inplace_add*/
2456 (binaryfunc)AttackDetector_inplace_sub, /*inplace_subtract*/
2457 (binaryfunc)AttackDetector_inplace_multiply, /*inplace_multiply*/
2458 0, /*inplace_remainder*/
2459 0, /*inplace_power*/
2460 0, /*inplace_lshift*/
2461 0, /*inplace_rshift*/
2462 0, /*inplace_and*/
2463 0, /*inplace_xor*/
2464 0, /*inplace_or*/
2465 0, /*nb_floor_divide*/
2466 (binaryfunc)AttackDetector_div, /*nb_true_divide*/
2467 0, /*nb_inplace_floor_divide*/
2468 (binaryfunc)AttackDetector_inplace_div, /*nb_inplace_true_divide*/
2469 0, /* nb_index */
2470 };
2471
2472 PyTypeObject AttackDetectorType =
2473 {
2474 PyVarObject_HEAD_INIT(NULL, 0)
2475 "_pyo.AttackDetector_base", /*tp_name*/
2476 sizeof(AttackDetector), /*tp_basicsize*/
2477 0, /*tp_itemsize*/
2478 (destructor)AttackDetector_dealloc, /*tp_dealloc*/
2479 0, /*tp_print*/
2480 0, /*tp_getattr*/
2481 0, /*tp_setattr*/
2482 0, /*tp_as_async (tp_compare in Python 2)*/
2483 0, /*tp_repr*/
2484 &AttackDetector_as_number, /*tp_as_number*/
2485 0, /*tp_as_sequence*/
2486 0, /*tp_as_mapping*/
2487 0, /*tp_hash */
2488 0, /*tp_call*/
2489 0, /*tp_str*/
2490 0, /*tp_getattro*/
2491 0, /*tp_setattro*/
2492 0, /*tp_as_buffer*/
2493 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2494 "AttackDetector objects. Audio signal peak detection.", /* tp_doc */
2495 (traverseproc)AttackDetector_traverse, /* tp_traverse */
2496 (inquiry)AttackDetector_clear, /* tp_clear */
2497 0, /* tp_richcompare */
2498 0, /* tp_weaklistoffset */
2499 0, /* tp_iter */
2500 0, /* tp_iternext */
2501 AttackDetector_methods, /* tp_methods */
2502 AttackDetector_members, /* tp_members */
2503 0, /* tp_getset */
2504 0, /* tp_base */
2505 0, /* tp_dict */
2506 0, /* tp_descr_get */
2507 0, /* tp_descr_set */
2508 0, /* tp_dictoffset */
2509 0, /* tp_init */
2510 0, /* tp_alloc */
2511 AttackDetector_new, /* tp_new */
2512 };
2513
2514 /**********************************/
2515 /********* Scope ******************/
2516 /**********************************/
2517 typedef struct
2518 {
2519 pyo_audio_HEAD
2520 PyObject *input;
2521 Stream *input_stream;
2522 PyObject *func;
2523 int size;
2524 int newsize;
2525 int width;
2526 int height;
2527 int pointer;
2528 int poll;
2529 MYFLT gain;
2530 MYFLT *buffer;
2531 } Scope;
2532
2533 static void
2534 Scope_generate(Scope *self)
2535 {
2536 int i;
2537 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2538
2539 for (i = 0; i < self->bufsize; i++)
2540 {
2541 if (self->pointer >= self->size)
2542 {
2543 if (self->func != Py_None && self->poll)
2544 {
2545 PyObject_Call(self->func, PyTuple_New(0), NULL);
2546 }
2547
2548 self->pointer = 0;
2549
2550 if (self->newsize != self->size)
2551 self->size = self->newsize;
2552 }
2553
2554 self->buffer[self->pointer] = in[i];
2555 self->pointer++;
2556 }
2557 }
2558
2559 static PyObject *
2560 Scope_display(Scope *self)
2561 {
2562 int i, ipos;
2563 MYFLT pos, step, mag, h2;
2564 PyObject *points, *tuple;
2565
2566 step = self->size / (MYFLT)(self->width);
2567 h2 = self->height * 0.5;
2568
2569 points = PyList_New(self->width);
2570
2571 for (i = 0; i < self->width; i++)
2572 {
2573 pos = i * step;
2574 ipos = (int)pos;
2575 tuple = PyTuple_New(2);
2576 mag = ((self->buffer[ipos] + (self->buffer[ipos + 1] - self->buffer[ipos]) * (pos - ipos)) * self->gain * h2 + h2);
2577 PyTuple_SET_ITEM(tuple, 0, PyLong_FromLong(i));
2578 PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(self->height - (int)mag));
2579 PyList_SET_ITEM(points, i, tuple);
2580 }
2581
2582 return points;
2583 }
2584
2585 static void
2586 Scope_compute_next_data_frame(Scope *self)
2587 {
2588 Scope_generate(self);
2589 }
2590
2591 static int
2592 Scope_traverse(Scope *self, visitproc visit, void *arg)
2593 {
2594 pyo_VISIT
2595 Py_VISIT(self->input);
2596
2597 if (self->func != Py_None)
2598 {
2599 Py_VISIT(self->func);
2600 }
2601
2602 return 0;
2603 }
2604
2605 static int
2606 Scope_clear(Scope *self)
2607 {
2608 pyo_CLEAR
2609 Py_CLEAR(self->input);
2610
2611 if (self->func != Py_None)
2612 {
2613 Py_CLEAR(self->func);
2614 }
2615
2616 return 0;
2617 }
2618
2619 static void
2620 Scope_dealloc(Scope* self)
2621 {
2622 pyo_DEALLOC
2623 PyMem_RawFree(self->buffer);
2624 Scope_clear(self);
2625 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2626 Py_TYPE(self)->tp_free((PyObject*)self);
2627 }
2628
2629 static PyObject *
2630 Scope_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2631 {
2632 int i, maxsize, target;
2633 MYFLT length = 0.05;
2634 PyObject *inputtmp, *input_streamtmp;
2635 Scope *self;
2636 self = (Scope *)type->tp_alloc(type, 0);
2637
2638 self->gain = 1.0;
2639 self->width = 500;
2640 self->height = 400;
2641 self->poll = 1;
2642
2643 self->func = Py_None;
2644
2645 INIT_OBJECT_COMMON
2646 Stream_setFunctionPtr(self->stream, Scope_compute_next_data_frame);
2647
2648 static char *kwlist[] = {"input", "length", NULL};
2649
2650 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_F, kwlist, &inputtmp, &length))
2651 Py_RETURN_NONE;
2652
2653 INIT_INPUT_STREAM
2654
2655 maxsize = (int)(self->sr);
2656 self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, maxsize * sizeof(MYFLT));
2657
2658 self->size = 0;
2659 target = (int)(length * self->sr);
2660
2661 while (self->size < target)
2662 {
2663 self->size += self->bufsize;
2664 }
2665
2666 self->size -= self->bufsize;
2667
2668 if (self->size < self->bufsize)
2669 self->size += self->bufsize;
2670 else if (self->size > maxsize)
2671 self->size = maxsize;
2672
2673 self->newsize = self->size;
2674 self->pointer = 0;
2675
2676 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2677
2678 return (PyObject *)self;
2679 }
2680
2681 static PyObject * Scope_getServer(Scope* self) { GET_SERVER };
2682 static PyObject * Scope_getStream(Scope* self) { GET_STREAM };
2683
2684 static PyObject * Scope_play(Scope *self, PyObject *args, PyObject *kwds) { PLAY };
2685 static PyObject * Scope_stop(Scope *self, PyObject *args, PyObject *kwds) { STOP };
2686
2687 static PyObject *
2688 Scope_setLength(Scope *self, PyObject *arg)
2689 {
2690 int target;
2691 MYFLT length;
2692 int maxsize = (int)(self->sr);
2693
2694 if (PyNumber_Check(arg))
2695 {
2696 length = PyFloat_AsDouble(arg);
2697 self->newsize = 0;
2698 target = (int)(length * self->sr);
2699
2700 while (self->newsize < target)
2701 {
2702 self->newsize += self->bufsize;
2703 }
2704
2705 self->newsize -= self->bufsize;
2706
2707 if (self->newsize < self->bufsize)
2708 self->newsize += self->bufsize;
2709 else if (self->newsize > maxsize)
2710 self->newsize = maxsize;
2711 }
2712
2713 Py_RETURN_NONE;
2714 }
2715
2716 static PyObject *
2717 Scope_setGain(Scope *self, PyObject *arg)
2718 {
2719 if (PyNumber_Check(arg))
2720 {
2721 self->gain = PyFloat_AsDouble(arg);
2722 }
2723
2724 Py_RETURN_NONE;
2725 }
2726
2727 static PyObject *
2728 Scope_setWidth(Scope *self, PyObject *arg)
2729 {
2730 if (PyLong_Check(arg))
2731 {
2732 self->width = PyLong_AsLong(arg);
2733 }
2734
2735 Py_RETURN_NONE;
2736 }
2737
2738 static PyObject *
2739 Scope_setHeight(Scope *self, PyObject *arg)
2740 {
2741 if (PyLong_Check(arg))
2742 {
2743 self->height = PyLong_AsLong(arg);
2744 }
2745
2746 Py_RETURN_NONE;
2747 }
2748
2749 static PyObject *
2750 Scope_setFunc(Scope *self, PyObject *arg)
2751 {
2752 if (! PyCallable_Check(arg))
2753 {
2754 PyErr_SetString(PyExc_TypeError, "The function attribute must be callable.");
2755 Py_RETURN_NONE;
2756 }
2757
2758 Py_XDECREF(self->func);
2759 self->func = arg;
2760 Py_INCREF(self->func);
2761
2762 Py_RETURN_NONE;
2763 }
2764
2765 static PyObject *
2766 Scope_setPoll(Scope *self, PyObject *arg)
2767 {
2768 if (PyLong_Check(arg))
2769 {
2770 self->poll = PyLong_AsLong(arg);
2771 }
2772
2773 Py_RETURN_NONE;
2774 }
2775
2776 static PyMemberDef Scope_members[] =
2777 {
2778 {"server", T_OBJECT_EX, offsetof(Scope, server), 0, "Pyo server."},
2779 {"stream", T_OBJECT_EX, offsetof(Scope, stream), 0, "Stream object."},
2780 {"input", T_OBJECT_EX, offsetof(Scope, input), 0, "Input sound object."},
2781 {NULL} /* Sentinel */
2782 };
2783
2784 static PyMethodDef Scope_methods[] =
2785 {
2786 {"getServer", (PyCFunction)Scope_getServer, METH_NOARGS, "Returns server object."},
2787 {"_getStream", (PyCFunction)Scope_getStream, METH_NOARGS, "Returns stream object."},
2788 {"play", (PyCFunction)Scope_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2789 {"stop", (PyCFunction)Scope_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2790 {"display", (PyCFunction)Scope_display, METH_NOARGS, "Computes the samples to draw."},
2791 {"setLength", (PyCFunction)Scope_setLength, METH_O, "Sets function's argument."},
2792 {"setGain", (PyCFunction)Scope_setGain, METH_O, "Sets gain compensation."},
2793 {"setWidth", (PyCFunction)Scope_setWidth, METH_O, "Sets the width of the display."},
2794 {"setHeight", (PyCFunction)Scope_setHeight, METH_O, "Sets the height of the display."},
2795 {"setFunc", (PyCFunction)Scope_setFunc, METH_O, "Sets callback function."},
2796 {"setPoll", (PyCFunction)Scope_setPoll, METH_O, "Activate/deactivate polling."},
2797 {NULL} /* Sentinel */
2798 };
2799
2800 PyTypeObject ScopeType =
2801 {
2802 PyVarObject_HEAD_INIT(NULL, 0)
2803 "_pyo.Scope_base", /*tp_name*/
2804 sizeof(Scope), /*tp_basicsize*/
2805 0, /*tp_itemsize*/
2806 (destructor)Scope_dealloc, /*tp_dealloc*/
2807 0, /*tp_print*/
2808 0, /*tp_getattr*/
2809 0, /*tp_setattr*/
2810 0, /*tp_as_async (tp_compare in Python 2)*/
2811 0, /*tp_repr*/
2812 0, /*tp_as_number*/
2813 0, /*tp_as_sequence*/
2814 0, /*tp_as_mapping*/
2815 0, /*tp_hash */
2816 0, /*tp_call*/
2817 0, /*tp_str*/
2818 0, /*tp_getattro*/
2819 0, /*tp_setattro*/
2820 0, /*tp_as_buffer*/
2821 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2822 "Scope objects. Show the waveform of an input signal.", /* tp_doc */
2823 (traverseproc)Scope_traverse, /* tp_traverse */
2824 (inquiry)Scope_clear, /* tp_clear */
2825 0, /* tp_richcompare */
2826 0, /* tp_weaklistoffset */
2827 0, /* tp_iter */
2828 0, /* tp_iternext */
2829 Scope_methods, /* tp_methods */
2830 Scope_members, /* tp_members */
2831 0, /* tp_getset */
2832 0, /* tp_base */
2833 0, /* tp_dict */
2834 0, /* tp_descr_get */
2835 0, /* tp_descr_set */
2836 0, /* tp_dictoffset */
2837 0, /* tp_init */
2838 0, /* tp_alloc */
2839 Scope_new, /* tp_new */
2840 };
2841
2842 /************/
2843 /* PeakAmp */
2844 /************/
2845 typedef struct
2846 {
2847 pyo_audio_HEAD
2848 PyObject *input;
2849 Stream *input_stream;
2850 int modebuffer[2]; // need at least 2 slots for mul & add
2851 MYFLT follow;
2852 } PeakAmp;
2853
2854 static void
2855 PeakAmp_filters_i(PeakAmp *self)
2856 {
2857 MYFLT absin, peak;
2858 int i;
2859
2860 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2861
2862 peak = 0.0;
2863
2864 for (i = 0; i < self->bufsize; i++)
2865 {
2866 absin = in[i];
2867
2868 if (absin < 0.0)
2869 absin = -absin;
2870
2871 if (absin > peak)
2872 peak = absin;
2873
2874 self->data[i] = self->follow;
2875 }
2876
2877 self->follow = peak;
2878 }
2879
2880 static void PeakAmp_postprocessing_ii(PeakAmp *self) { POST_PROCESSING_II };
2881 static void PeakAmp_postprocessing_ai(PeakAmp *self) { POST_PROCESSING_AI };
2882 static void PeakAmp_postprocessing_ia(PeakAmp *self) { POST_PROCESSING_IA };
2883 static void PeakAmp_postprocessing_aa(PeakAmp *self) { POST_PROCESSING_AA };
2884 static void PeakAmp_postprocessing_ireva(PeakAmp *self) { POST_PROCESSING_IREVA };
2885 static void PeakAmp_postprocessing_areva(PeakAmp *self) { POST_PROCESSING_AREVA };
2886 static void PeakAmp_postprocessing_revai(PeakAmp *self) { POST_PROCESSING_REVAI };
2887 static void PeakAmp_postprocessing_revaa(PeakAmp *self) { POST_PROCESSING_REVAA };
2888 static void PeakAmp_postprocessing_revareva(PeakAmp *self) { POST_PROCESSING_REVAREVA };
2889
2890 static void
2891 PeakAmp_setProcMode(PeakAmp *self)
2892 {
2893 int muladdmode;
2894 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2895
2896 self->proc_func_ptr = PeakAmp_filters_i;
2897
2898 switch (muladdmode)
2899 {
2900 case 0:
2901 self->muladd_func_ptr = PeakAmp_postprocessing_ii;
2902 break;
2903
2904 case 1:
2905 self->muladd_func_ptr = PeakAmp_postprocessing_ai;
2906 break;
2907
2908 case 2:
2909 self->muladd_func_ptr = PeakAmp_postprocessing_revai;
2910 break;
2911
2912 case 10:
2913 self->muladd_func_ptr = PeakAmp_postprocessing_ia;
2914 break;
2915
2916 case 11:
2917 self->muladd_func_ptr = PeakAmp_postprocessing_aa;
2918 break;
2919
2920 case 12:
2921 self->muladd_func_ptr = PeakAmp_postprocessing_revaa;
2922 break;
2923
2924 case 20:
2925 self->muladd_func_ptr = PeakAmp_postprocessing_ireva;
2926 break;
2927
2928 case 21:
2929 self->muladd_func_ptr = PeakAmp_postprocessing_areva;
2930 break;
2931
2932 case 22:
2933 self->muladd_func_ptr = PeakAmp_postprocessing_revareva;
2934 break;
2935 }
2936 }
2937
2938 static void
2939 PeakAmp_compute_next_data_frame(PeakAmp *self)
2940 {
2941 (*self->proc_func_ptr)(self);
2942 (*self->muladd_func_ptr)(self);
2943 }
2944
2945 static int
2946 PeakAmp_traverse(PeakAmp *self, visitproc visit, void *arg)
2947 {
2948 pyo_VISIT
2949 Py_VISIT(self->input);
2950 return 0;
2951 }
2952
2953 static int
2954 PeakAmp_clear(PeakAmp *self)
2955 {
2956 pyo_CLEAR
2957 Py_CLEAR(self->input);
2958 return 0;
2959 }
2960
2961 static void
2962 PeakAmp_dealloc(PeakAmp* self)
2963 {
2964 pyo_DEALLOC
2965 PeakAmp_clear(self);
2966 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2967 Py_TYPE(self)->tp_free((PyObject*)self);
2968 }
2969
2970 static PyObject *
2971 PeakAmp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2972 {
2973 int i;
2974 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
2975 PeakAmp *self;
2976 self = (PeakAmp *)type->tp_alloc(type, 0);
2977
2978 self->follow = 0.0;
2979 self->modebuffer[0] = 0;
2980 self->modebuffer[1] = 0;
2981
2982 INIT_OBJECT_COMMON
2983 Stream_setFunctionPtr(self->stream, PeakAmp_compute_next_data_frame);
2984 self->mode_func_ptr = PeakAmp_setProcMode;
2985
2986 static char *kwlist[] = {"input", "mul", "add", NULL};
2987
2988 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &inputtmp, &multmp, &addtmp))
2989 Py_RETURN_NONE;
2990
2991 INIT_INPUT_STREAM
2992
2993 if (multmp)
2994 {
2995 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2996 }
2997
2998 if (addtmp)
2999 {
3000 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3001 }
3002
3003 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3004
3005 (*self->mode_func_ptr)(self);
3006
3007 return (PyObject *)self;
3008 }
3009
3010 static PyObject * PeakAmp_getServer(PeakAmp* self) { GET_SERVER };
3011 static PyObject * PeakAmp_getStream(PeakAmp* self) { GET_STREAM };
3012 static PyObject * PeakAmp_setMul(PeakAmp *self, PyObject *arg) { SET_MUL };
3013 static PyObject * PeakAmp_setAdd(PeakAmp *self, PyObject *arg) { SET_ADD };
3014 static PyObject * PeakAmp_setSub(PeakAmp *self, PyObject *arg) { SET_SUB };
3015 static PyObject * PeakAmp_setDiv(PeakAmp *self, PyObject *arg) { SET_DIV };
3016
3017 static PyObject * PeakAmp_play(PeakAmp *self, PyObject *args, PyObject *kwds) { PLAY };
3018 static PyObject * PeakAmp_stop(PeakAmp *self, PyObject *args, PyObject *kwds) { STOP };
3019
3020 static PyObject * PeakAmp_multiply(PeakAmp *self, PyObject *arg) { MULTIPLY };
3021 static PyObject * PeakAmp_inplace_multiply(PeakAmp *self, PyObject *arg) { INPLACE_MULTIPLY };
3022 static PyObject * PeakAmp_add(PeakAmp *self, PyObject *arg) { ADD };
3023 static PyObject * PeakAmp_inplace_add(PeakAmp *self, PyObject *arg) { INPLACE_ADD };
3024 static PyObject * PeakAmp_sub(PeakAmp *self, PyObject *arg) { SUB };
3025 static PyObject * PeakAmp_inplace_sub(PeakAmp *self, PyObject *arg) { INPLACE_SUB };
3026 static PyObject * PeakAmp_div(PeakAmp *self, PyObject *arg) { DIV };
3027 static PyObject * PeakAmp_inplace_div(PeakAmp *self, PyObject *arg) { INPLACE_DIV };
3028
3029 static PyObject *
3030 PeakAmp_getValue(PeakAmp *self)
3031 {
3032 return PyFloat_FromDouble(self->follow);
3033 }
3034
3035 static PyMemberDef PeakAmp_members[] =
3036 {
3037 {"server", T_OBJECT_EX, offsetof(PeakAmp, server), 0, "Pyo server."},
3038 {"stream", T_OBJECT_EX, offsetof(PeakAmp, stream), 0, "Stream object."},
3039 {"input", T_OBJECT_EX, offsetof(PeakAmp, input), 0, "Input sound object."},
3040 {"mul", T_OBJECT_EX, offsetof(PeakAmp, mul), 0, "Mul factor."},
3041 {"add", T_OBJECT_EX, offsetof(PeakAmp, add), 0, "Add factor."},
3042 {NULL} /* Sentinel */
3043 };
3044
3045 static PyMethodDef PeakAmp_methods[] =
3046 {
3047 {"getServer", (PyCFunction)PeakAmp_getServer, METH_NOARGS, "Returns server object."},
3048 {"_getStream", (PyCFunction)PeakAmp_getStream, METH_NOARGS, "Returns stream object."},
3049 {"play", (PyCFunction)PeakAmp_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3050 {"stop", (PyCFunction)PeakAmp_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3051 {"getValue", (PyCFunction)PeakAmp_getValue, METH_NOARGS, "Returns the current peaking value."},
3052 {"setMul", (PyCFunction)PeakAmp_setMul, METH_O, "Sets oscillator mul factor."},
3053 {"setAdd", (PyCFunction)PeakAmp_setAdd, METH_O, "Sets oscillator add factor."},
3054 {"setSub", (PyCFunction)PeakAmp_setSub, METH_O, "Sets inverse add factor."},
3055 {"setDiv", (PyCFunction)PeakAmp_setDiv, METH_O, "Sets inverse mul factor."},
3056 {NULL} /* Sentinel */
3057 };
3058
3059 static PyNumberMethods PeakAmp_as_number =
3060 {
3061 (binaryfunc)PeakAmp_add, /*nb_add*/
3062 (binaryfunc)PeakAmp_sub, /*nb_subtract*/
3063 (binaryfunc)PeakAmp_multiply, /*nb_multiply*/
3064 0, /*nb_remainder*/
3065 0, /*nb_divmod*/
3066 0, /*nb_power*/
3067 0, /*nb_neg*/
3068 0, /*nb_pos*/
3069 0, /*(unaryfunc)array_abs,*/
3070 0, /*nb_nonzero*/
3071 0, /*nb_invert*/
3072 0, /*nb_lshift*/
3073 0, /*nb_rshift*/
3074 0, /*nb_and*/
3075 0, /*nb_xor*/
3076 0, /*nb_or*/
3077 0, /*nb_int*/
3078 0, /*nb_long*/
3079 0, /*nb_float*/
3080 (binaryfunc)PeakAmp_inplace_add, /*inplace_add*/
3081 (binaryfunc)PeakAmp_inplace_sub, /*inplace_subtract*/
3082 (binaryfunc)PeakAmp_inplace_multiply, /*inplace_multiply*/
3083 0, /*inplace_remainder*/
3084 0, /*inplace_power*/
3085 0, /*inplace_lshift*/
3086 0, /*inplace_rshift*/
3087 0, /*inplace_and*/
3088 0, /*inplace_xor*/
3089 0, /*inplace_or*/
3090 0, /*nb_floor_divide*/
3091 (binaryfunc)PeakAmp_div, /*nb_true_divide*/
3092 0, /*nb_inplace_floor_divide*/
3093 (binaryfunc)PeakAmp_inplace_div, /*nb_inplace_true_divide*/
3094 0, /* nb_index */
3095 };
3096
3097 PyTypeObject PeakAmpType =
3098 {
3099 PyVarObject_HEAD_INIT(NULL, 0)
3100 "_pyo.PeakAmp_base", /*tp_name*/
3101 sizeof(PeakAmp), /*tp_basicsize*/
3102 0, /*tp_itemsize*/
3103 (destructor)PeakAmp_dealloc, /*tp_dealloc*/
3104 0, /*tp_print*/
3105 0, /*tp_getattr*/
3106 0, /*tp_setattr*/
3107 0, /*tp_as_async (tp_compare in Python 2)*/
3108 0, /*tp_repr*/
3109 &PeakAmp_as_number, /*tp_as_number*/
3110 0, /*tp_as_sequence*/
3111 0, /*tp_as_mapping*/
3112 0, /*tp_hash */
3113 0, /*tp_call*/
3114 0, /*tp_str*/
3115 0, /*tp_getattro*/
3116 0, /*tp_setattro*/
3117 0, /*tp_as_buffer*/
3118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3119 "PeakAmp objects. Envelope follower.", /* tp_doc */
3120 (traverseproc)PeakAmp_traverse, /* tp_traverse */
3121 (inquiry)PeakAmp_clear, /* tp_clear */
3122 0, /* tp_richcompare */
3123 0, /* tp_weaklistoffset */
3124 0, /* tp_iter */
3125 0, /* tp_iternext */
3126 PeakAmp_methods, /* tp_methods */
3127 PeakAmp_members, /* tp_members */
3128 0, /* tp_getset */
3129 0, /* tp_base */
3130 0, /* tp_dict */
3131 0, /* tp_descr_get */
3132 0, /* tp_descr_set */
3133 0, /* tp_dictoffset */
3134 0, /* tp_init */
3135 0, /* tp_alloc */
3136 PeakAmp_new, /* tp_new */
3137 };
3138
3139 /************/
3140 /* RMS */
3141 /************/
3142 typedef struct
3143 {
3144 pyo_audio_HEAD
3145 PyObject *input;
3146 Stream *input_stream;
3147 int modebuffer[2]; // need at least 2 slots for mul & add
3148 MYFLT follow;
3149 } RMS;
3150
3151 static void
3152 RMS_filters_i(RMS *self)
3153 {
3154 MYFLT sum = 0.0;
3155 int i;
3156
3157 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3158
3159 for (i = 0; i < self->bufsize; i++)
3160 {
3161 self->data[i] = self->follow;
3162 sum += in[i] * in[i];
3163 }
3164
3165 self->follow = MYSQRT(sum / self->bufsize);
3166 }
3167
3168 static void RMS_postprocessing_ii(RMS *self) { POST_PROCESSING_II };
3169 static void RMS_postprocessing_ai(RMS *self) { POST_PROCESSING_AI };
3170 static void RMS_postprocessing_ia(RMS *self) { POST_PROCESSING_IA };
3171 static void RMS_postprocessing_aa(RMS *self) { POST_PROCESSING_AA };
3172 static void RMS_postprocessing_ireva(RMS *self) { POST_PROCESSING_IREVA };
3173 static void RMS_postprocessing_areva(RMS *self) { POST_PROCESSING_AREVA };
3174 static void RMS_postprocessing_revai(RMS *self) { POST_PROCESSING_REVAI };
3175 static void RMS_postprocessing_revaa(RMS *self) { POST_PROCESSING_REVAA };
3176 static void RMS_postprocessing_revareva(RMS *self) { POST_PROCESSING_REVAREVA };
3177
3178 static void
3179 RMS_setProcMode(RMS *self)
3180 {
3181 int muladdmode;
3182 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3183
3184 self->proc_func_ptr = RMS_filters_i;
3185
3186 switch (muladdmode)
3187 {
3188 case 0:
3189 self->muladd_func_ptr = RMS_postprocessing_ii;
3190 break;
3191
3192 case 1:
3193 self->muladd_func_ptr = RMS_postprocessing_ai;
3194 break;
3195
3196 case 2:
3197 self->muladd_func_ptr = RMS_postprocessing_revai;
3198 break;
3199
3200 case 10:
3201 self->muladd_func_ptr = RMS_postprocessing_ia;
3202 break;
3203
3204 case 11:
3205 self->muladd_func_ptr = RMS_postprocessing_aa;
3206 break;
3207
3208 case 12:
3209 self->muladd_func_ptr = RMS_postprocessing_revaa;
3210 break;
3211
3212 case 20:
3213 self->muladd_func_ptr = RMS_postprocessing_ireva;
3214 break;
3215
3216 case 21:
3217 self->muladd_func_ptr = RMS_postprocessing_areva;
3218 break;
3219
3220 case 22:
3221 self->muladd_func_ptr = RMS_postprocessing_revareva;
3222 break;
3223 }
3224 }
3225
3226 static void
3227 RMS_compute_next_data_frame(RMS *self)
3228 {
3229 (*self->proc_func_ptr)(self);
3230 (*self->muladd_func_ptr)(self);
3231 }
3232
3233 static int
3234 RMS_traverse(RMS *self, visitproc visit, void *arg)
3235 {
3236 pyo_VISIT
3237 Py_VISIT(self->input);
3238 return 0;
3239 }
3240
3241 static int
3242 RMS_clear(RMS *self)
3243 {
3244 pyo_CLEAR
3245 Py_CLEAR(self->input);
3246 return 0;
3247 }
3248
3249 static void
3250 RMS_dealloc(RMS* self)
3251 {
3252 pyo_DEALLOC
3253 RMS_clear(self);
3254 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3255 Py_TYPE(self)->tp_free((PyObject*)self);
3256 }
3257
3258 static PyObject *
3259 RMS_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3260 {
3261 int i;
3262 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
3263 RMS *self;
3264 self = (RMS *)type->tp_alloc(type, 0);
3265
3266 self->follow = 0.0;
3267 self->modebuffer[0] = 0;
3268 self->modebuffer[1] = 0;
3269
3270 INIT_OBJECT_COMMON
3271 Stream_setFunctionPtr(self->stream, RMS_compute_next_data_frame);
3272 self->mode_func_ptr = RMS_setProcMode;
3273
3274 static char *kwlist[] = {"input", "mul", "add", NULL};
3275
3276 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &inputtmp, &multmp, &addtmp))
3277 Py_RETURN_NONE;
3278
3279 INIT_INPUT_STREAM
3280
3281 if (multmp)
3282 {
3283 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3284 }
3285
3286 if (addtmp)
3287 {
3288 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3289 }
3290
3291 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3292
3293 (*self->mode_func_ptr)(self);
3294
3295 return (PyObject *)self;
3296 }
3297
3298 static PyObject * RMS_getServer(RMS* self) { GET_SERVER };
3299 static PyObject * RMS_getStream(RMS* self) { GET_STREAM };
3300 static PyObject * RMS_setMul(RMS *self, PyObject *arg) { SET_MUL };
3301 static PyObject * RMS_setAdd(RMS *self, PyObject *arg) { SET_ADD };
3302 static PyObject * RMS_setSub(RMS *self, PyObject *arg) { SET_SUB };
3303 static PyObject * RMS_setDiv(RMS *self, PyObject *arg) { SET_DIV };
3304
3305 static PyObject * RMS_play(RMS *self, PyObject *args, PyObject *kwds) { PLAY };
3306 static PyObject * RMS_stop(RMS *self, PyObject *args, PyObject *kwds) { STOP };
3307
3308 static PyObject * RMS_multiply(RMS *self, PyObject *arg) { MULTIPLY };
3309 static PyObject * RMS_inplace_multiply(RMS *self, PyObject *arg) { INPLACE_MULTIPLY };
3310 static PyObject * RMS_add(RMS *self, PyObject *arg) { ADD };
3311 static PyObject * RMS_inplace_add(RMS *self, PyObject *arg) { INPLACE_ADD };
3312 static PyObject * RMS_sub(RMS *self, PyObject *arg) { SUB };
3313 static PyObject * RMS_inplace_sub(RMS *self, PyObject *arg) { INPLACE_SUB };
3314 static PyObject * RMS_div(RMS *self, PyObject *arg) { DIV };
3315 static PyObject * RMS_inplace_div(RMS *self, PyObject *arg) { INPLACE_DIV };
3316
3317 static PyObject *
3318 RMS_getValue(RMS *self)
3319 {
3320 return PyFloat_FromDouble(self->follow);
3321 }
3322
3323 static PyMemberDef RMS_members[] =
3324 {
3325 {"server", T_OBJECT_EX, offsetof(RMS, server), 0, "Pyo server."},
3326 {"stream", T_OBJECT_EX, offsetof(RMS, stream), 0, "Stream object."},
3327 {"input", T_OBJECT_EX, offsetof(RMS, input), 0, "Input sound object."},
3328 {"mul", T_OBJECT_EX, offsetof(RMS, mul), 0, "Mul factor."},
3329 {"add", T_OBJECT_EX, offsetof(RMS, add), 0, "Add factor."},
3330 {NULL} /* Sentinel */
3331 };
3332
3333 static PyMethodDef RMS_methods[] =
3334 {
3335 {"getServer", (PyCFunction)RMS_getServer, METH_NOARGS, "Returns server object."},
3336 {"_getStream", (PyCFunction)RMS_getStream, METH_NOARGS, "Returns stream object."},
3337 {"play", (PyCFunction)RMS_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3338 {"stop", (PyCFunction)RMS_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3339 {"getValue", (PyCFunction)RMS_getValue, METH_NOARGS, "Returns the current peaking value."},
3340 {"setMul", (PyCFunction)RMS_setMul, METH_O, "Sets oscillator mul factor."},
3341 {"setAdd", (PyCFunction)RMS_setAdd, METH_O, "Sets oscillator add factor."},
3342 {"setSub", (PyCFunction)RMS_setSub, METH_O, "Sets inverse add factor."},
3343 {"setDiv", (PyCFunction)RMS_setDiv, METH_O, "Sets inverse mul factor."},
3344 {NULL} /* Sentinel */
3345 };
3346
3347 static PyNumberMethods RMS_as_number =
3348 {
3349 (binaryfunc)RMS_add, /*nb_add*/
3350 (binaryfunc)RMS_sub, /*nb_subtract*/
3351 (binaryfunc)RMS_multiply, /*nb_multiply*/
3352 0, /*nb_remainder*/
3353 0, /*nb_divmod*/
3354 0, /*nb_power*/
3355 0, /*nb_neg*/
3356 0, /*nb_pos*/
3357 0, /*(unaryfunc)array_abs,*/
3358 0, /*nb_nonzero*/
3359 0, /*nb_invert*/
3360 0, /*nb_lshift*/
3361 0, /*nb_rshift*/
3362 0, /*nb_and*/
3363 0, /*nb_xor*/
3364 0, /*nb_or*/
3365 0, /*nb_int*/
3366 0, /*nb_long*/
3367 0, /*nb_float*/
3368 (binaryfunc)RMS_inplace_add, /*inplace_add*/
3369 (binaryfunc)RMS_inplace_sub, /*inplace_subtract*/
3370 (binaryfunc)RMS_inplace_multiply, /*inplace_multiply*/
3371 0, /*inplace_remainder*/
3372 0, /*inplace_power*/
3373 0, /*inplace_lshift*/
3374 0, /*inplace_rshift*/
3375 0, /*inplace_and*/
3376 0, /*inplace_xor*/
3377 0, /*inplace_or*/
3378 0, /*nb_floor_divide*/
3379 (binaryfunc)RMS_div, /*nb_true_divide*/
3380 0, /*nb_inplace_floor_divide*/
3381 (binaryfunc)RMS_inplace_div, /*nb_inplace_true_divide*/
3382 0, /* nb_index */
3383 };
3384
3385 PyTypeObject RMSType =
3386 {
3387 PyVarObject_HEAD_INIT(NULL, 0)
3388 "_pyo.RMS_base", /*tp_name*/
3389 sizeof(RMS), /*tp_basicsize*/
3390 0, /*tp_itemsize*/
3391 (destructor)RMS_dealloc, /*tp_dealloc*/
3392 0, /*tp_print*/
3393 0, /*tp_getattr*/
3394 0, /*tp_setattr*/
3395 0, /*tp_as_async (tp_compare in Python 2)*/
3396 0, /*tp_repr*/
3397 &RMS_as_number, /*tp_as_number*/
3398 0, /*tp_as_sequence*/
3399 0, /*tp_as_mapping*/
3400 0, /*tp_hash */
3401 0, /*tp_call*/
3402 0, /*tp_str*/
3403 0, /*tp_getattro*/
3404 0, /*tp_setattro*/
3405 0, /*tp_as_buffer*/
3406 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3407 "RMS objects. Envelope follower.", /* tp_doc */
3408 (traverseproc)RMS_traverse, /* tp_traverse */
3409 (inquiry)RMS_clear, /* tp_clear */
3410 0, /* tp_richcompare */
3411 0, /* tp_weaklistoffset */
3412 0, /* tp_iter */
3413 0, /* tp_iternext */
3414 RMS_methods, /* tp_methods */
3415 RMS_members, /* tp_members */
3416 0, /* tp_getset */
3417 0, /* tp_base */
3418 0, /* tp_dict */
3419 0, /* tp_descr_get */
3420 0, /* tp_descr_set */
3421 0, /* tp_dictoffset */
3422 0, /* tp_init */
3423 0, /* tp_alloc */
3424 RMS_new, /* tp_new */
3425 };