Debian Codegraph @

src/objects/analysismodule.c

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