Debian Codegraph @

src/objects/compressmodule.c

raw
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #include <Python.h>
22 #include "structmember.h"
23 #include <math.h>
24 #include "pyomodule.h"
25 #include "streammodule.h"
26 #include "servermodule.h"
27 #include "dummymodule.h"
28
29 /* Compressor */
30 typedef struct
31 {
32 pyo_audio_HEAD
33 PyObject *input;
34 Stream *input_stream;
35 PyObject *risetime;
36 PyObject *falltime;
37 PyObject *thresh;
38 PyObject *ratio;
39 Stream *risetime_stream;
40 Stream *falltime_stream;
41 Stream *thresh_stream;
42 Stream *ratio_stream;
43 int modebuffer[6]; // need at least 2 slots for mul & add
44 int outputAmp;
45 MYFLT follow;
46 MYFLT knee;
47 long lh_delay;
48 long lh_size;
49 long lh_in_count;
50 MYFLT *lh_buffer;
51 } Compress;
52
53 static MYFLT
54 C_clip(MYFLT x)
55 {
56 if (x < 1.0e-20)
57 return 1.0e-20;
58 else if (x > 1.0)
59 return 1.0;
60 else
61 return x;
62 }
63
64 static void
65 Compress_compress_soft(Compress *self)
66 {
67 MYFLT samp, ampthresh, absin, indb, diff, outdb, outa;
68 MYFLT kneethresh, kneescl, knee, kneeratio, invKneeRange;
69 MYFLT risetime, falltime, thresh, ratio;
70 int i;
71 long ind;
72
73 MYFLT *in = Stream_getData((Stream *)self->input_stream);
74
75 if (self->modebuffer[2] == 0)
76 risetime = PyFloat_AS_DOUBLE(self->risetime);
77 else
78 risetime = Stream_getData((Stream *)self->risetime_stream)[0];
79
80 if (risetime <= 0.0)
81 risetime = 0.001;
82
83 if (self->modebuffer[3] == 0)
84 falltime = PyFloat_AS_DOUBLE(self->falltime);
85 else
86 falltime = Stream_getData((Stream *)self->falltime_stream)[0];
87
88 if (falltime <= 0.0)
89 falltime = 0.001;
90
91 if (self->modebuffer[4] == 0)
92 thresh = PyFloat_AS_DOUBLE(self->thresh);
93 else
94 thresh = Stream_getData((Stream *)self->thresh_stream)[0];
95
96 if (self->modebuffer[5] == 0)
97 ratio = PyFloat_AS_DOUBLE(self->ratio);
98 else
99 ratio = Stream_getData((Stream *)self->ratio_stream)[0];
100
101 ratio = 1.0 / ratio;
102 risetime = MYEXP(-1.0 / (self->sr * risetime));
103 falltime = MYEXP(-1.0 / (self->sr * falltime));
104 knee = self->knee * 0.999 + 0.001; /* 0 = hard knee, 1 = soft knee */
105 thresh += 3.0 * self->knee;
106
107 if (thresh > 0.0)
108 thresh = 0.0;
109
110 ampthresh = MYPOW(10.0, thresh * 0.05); /* up to 3 dB above threshold */
111 kneethresh = MYPOW(10.0, (thresh - (self->knee * 8.5 + 0.5)) * 0.05); /* up to 6 dB under threshold */
112 invKneeRange = 1.0 / (ampthresh - kneethresh);
113
114 for (i = 0; i < self->bufsize; i++)
115 {
116 /* Envelope follower */
117 absin = in[i];
118
119 if (absin < 0.0)
120 absin = -absin;
121
122 if (self->follow < absin)
123 self->follow = absin + risetime * (self->follow - absin);
124 else
125 self->follow = absin + falltime * (self->follow - absin);
126
127 /* Look ahead */
128 ind = self->lh_in_count - self->lh_delay;
129
130 if (ind < 0)
131 ind += self->lh_size;
132
133 samp = self->lh_buffer[ind];
134
135 self->lh_buffer[self->lh_in_count] = in[i];
136 self->lh_in_count++;
137
138 if (self->lh_in_count >= self->lh_size)
139 self->lh_in_count = 0;
140
141 /* Compress signal */
142 outa = 1.0;
143
144 if (self->follow > ampthresh) /* Above threshold */
145 {
146 indb = 20.0 * MYLOG10(self->follow + 1.0e-20);
147 diff = indb - thresh;
148 outdb = diff - diff * ratio;
149 outa = MYPOW(10.0, -outdb * 0.05);
150 }
151 else if (self->follow > kneethresh) /* Under the knee */
152 {
153 kneescl = (self->follow - kneethresh) * invKneeRange;
154 kneeratio = (((knee + 1.0) * kneescl) / (knee + kneescl)) * (ratio - 1.0) + 1.0;
155 indb = 20.0 * MYLOG10(self->follow + 1.0e-20);
156 diff = indb - thresh;
157 outdb = diff - diff * kneeratio;
158 outa = MYPOW(10.0, -outdb * 0.05);
159 }
160
161 if (self->outputAmp == 0)
162 self->data[i] = samp * C_clip(outa);
163 else
164 self->data[i] = C_clip(outa);
165 }
166 }
167
168 static void Compress_postprocessing_ii(Compress *self) { POST_PROCESSING_II };
169 static void Compress_postprocessing_ai(Compress *self) { POST_PROCESSING_AI };
170 static void Compress_postprocessing_ia(Compress *self) { POST_PROCESSING_IA };
171 static void Compress_postprocessing_aa(Compress *self) { POST_PROCESSING_AA };
172 static void Compress_postprocessing_ireva(Compress *self) { POST_PROCESSING_IREVA };
173 static void Compress_postprocessing_areva(Compress *self) { POST_PROCESSING_AREVA };
174 static void Compress_postprocessing_revai(Compress *self) { POST_PROCESSING_REVAI };
175 static void Compress_postprocessing_revaa(Compress *self) { POST_PROCESSING_REVAA };
176 static void Compress_postprocessing_revareva(Compress *self) { POST_PROCESSING_REVAREVA };
177
178 static void
179 Compress_setProcMode(Compress *self)
180 {
181 int muladdmode;
182 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
183
184 switch (muladdmode)
185 {
186 case 0:
187 self->muladd_func_ptr = Compress_postprocessing_ii;
188 break;
189
190 case 1:
191 self->muladd_func_ptr = Compress_postprocessing_ai;
192 break;
193
194 case 2:
195 self->muladd_func_ptr = Compress_postprocessing_revai;
196 break;
197
198 case 10:
199 self->muladd_func_ptr = Compress_postprocessing_ia;
200 break;
201
202 case 11:
203 self->muladd_func_ptr = Compress_postprocessing_aa;
204 break;
205
206 case 12:
207 self->muladd_func_ptr = Compress_postprocessing_revaa;
208 break;
209
210 case 20:
211 self->muladd_func_ptr = Compress_postprocessing_ireva;
212 break;
213
214 case 21:
215 self->muladd_func_ptr = Compress_postprocessing_areva;
216 break;
217
218 case 22:
219 self->muladd_func_ptr = Compress_postprocessing_revareva;
220 break;
221 }
222 }
223
224 static void
225 Compress_compute_next_data_frame(Compress *self)
226 {
227 (*self->proc_func_ptr)(self);
228 (*self->muladd_func_ptr)(self);
229 }
230
231 static int
232 Compress_traverse(Compress *self, visitproc visit, void *arg)
233 {
234 pyo_VISIT
235 Py_VISIT(self->input);
236 Py_VISIT(self->risetime);
237 Py_VISIT(self->falltime);
238 Py_VISIT(self->thresh);
239 Py_VISIT(self->ratio);
240 return 0;
241 }
242
243 static int
244 Compress_clear(Compress *self)
245 {
246 pyo_CLEAR
247 Py_CLEAR(self->input);
248 Py_CLEAR(self->risetime);
249 Py_CLEAR(self->falltime);
250 Py_CLEAR(self->thresh);
251 Py_CLEAR(self->ratio);
252 return 0;
253 }
254
255 static void
256 Compress_dealloc(Compress* self)
257 {
258 pyo_DEALLOC
259 PyMem_RawFree(self->lh_buffer);
260 Compress_clear(self);
261 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
262 Py_TYPE(self)->tp_free((PyObject*)self);
263 }
264
265 static PyObject *
266 Compress_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
267 {
268 int i;
269 PyObject *inputtmp, *input_streamtmp, *threshtmp = NULL, *ratiotmp = NULL, *risetimetmp = NULL, *falltimetmp = NULL, *multmp = NULL, *addtmp = NULL;
270 PyObject *looktmp = NULL, *kneetmp = NULL;
271 Compress *self;
272 self = (Compress *)type->tp_alloc(type, 0);
273
274 self->thresh = PyFloat_FromDouble(-20.0);
275 self->ratio = PyFloat_FromDouble(2.0);
276 self->risetime = PyFloat_FromDouble(0.01);
277 self->falltime = PyFloat_FromDouble(0.1);
278 self->modebuffer[0] = 0;
279 self->modebuffer[1] = 0;
280 self->modebuffer[2] = 0;
281 self->modebuffer[3] = 0;
282 self->modebuffer[4] = 0;
283 self->modebuffer[5] = 0;
284 self->outputAmp = 0;
285 self->follow = 0.0;
286 self->lh_delay = 0;
287 self->lh_in_count = 0;
288 self->knee = 0.;
289
290 INIT_OBJECT_COMMON
291
292 Stream_setFunctionPtr(self->stream, Compress_compute_next_data_frame);
293 self->mode_func_ptr = Compress_setProcMode;
294
295 static char *kwlist[] = {"input", "thresh", "ratio", "risetime", "falltime", "lookahead", "knee", "outputAmp", "mul", "add", NULL};
296
297 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOOOiOO", kwlist, &inputtmp, &threshtmp, &ratiotmp, &risetimetmp, &falltimetmp, &looktmp, &kneetmp, &self->outputAmp, &multmp, &addtmp))
298 Py_RETURN_NONE;
299
300 INIT_INPUT_STREAM
301
302 if (threshtmp)
303 {
304 PyObject_CallMethod((PyObject *)self, "setThresh", "O", threshtmp);
305 }
306
307 if (ratiotmp)
308 {
309 PyObject_CallMethod((PyObject *)self, "setRatio", "O", ratiotmp);
310 }
311
312 if (risetimetmp)
313 {
314 PyObject_CallMethod((PyObject *)self, "setRiseTime", "O", risetimetmp);
315 }
316
317 if (falltimetmp)
318 {
319 PyObject_CallMethod((PyObject *)self, "setFallTime", "O", falltimetmp);
320 }
321
322 if (multmp)
323 {
324 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
325 }
326
327 if (addtmp)
328 {
329 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
330 }
331
332 PyObject_CallMethod((PyObject *)self, "setLookAhead", "O", looktmp);
333 PyObject_CallMethod((PyObject *)self, "setKnee", "O", kneetmp);
334
335 self->lh_size = (long)(0.025 * self->sr + 0.5);
336 self->lh_buffer = (MYFLT *)PyMem_RawRealloc(self->lh_buffer, (self->lh_size + 1) * sizeof(MYFLT));
337
338 for (i = 0; i < (self->lh_size + 1); i++)
339 {
340 self->lh_buffer[i] = 0.;
341 }
342
343 self->proc_func_ptr = Compress_compress_soft;
344
345 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
346
347 (*self->mode_func_ptr)(self);
348
349 return (PyObject *)self;
350 }
351
352 static PyObject * Compress_getServer(Compress* self) { GET_SERVER };
353 static PyObject * Compress_getStream(Compress* self) { GET_STREAM };
354 static PyObject * Compress_setMul(Compress *self, PyObject *arg) { SET_MUL };
355 static PyObject * Compress_setAdd(Compress *self, PyObject *arg) { SET_ADD };
356 static PyObject * Compress_setSub(Compress *self, PyObject *arg) { SET_SUB };
357 static PyObject * Compress_setDiv(Compress *self, PyObject *arg) { SET_DIV };
358
359 static PyObject * Compress_play(Compress *self, PyObject *args, PyObject *kwds) { PLAY };
360 static PyObject * Compress_out(Compress *self, PyObject *args, PyObject *kwds) { OUT };
361 static PyObject * Compress_stop(Compress *self, PyObject *args, PyObject *kwds) { STOP };
362
363 static PyObject * Compress_multiply(Compress *self, PyObject *arg) { MULTIPLY };
364 static PyObject * Compress_inplace_multiply(Compress *self, PyObject *arg) { INPLACE_MULTIPLY };
365 static PyObject * Compress_add(Compress *self, PyObject *arg) { ADD };
366 static PyObject * Compress_inplace_add(Compress *self, PyObject *arg) { INPLACE_ADD };
367 static PyObject * Compress_sub(Compress *self, PyObject *arg) { SUB };
368 static PyObject * Compress_inplace_sub(Compress *self, PyObject *arg) { INPLACE_SUB };
369 static PyObject * Compress_div(Compress *self, PyObject *arg) { DIV };
370 static PyObject * Compress_inplace_div(Compress *self, PyObject *arg) { INPLACE_DIV };
371
372 static PyObject * Compress_setRiseTime(Compress *self, PyObject *arg) { SET_PARAM(self->risetime, self->risetime_stream, 2); }
373 static PyObject * Compress_setFallTime(Compress *self, PyObject *arg) { SET_PARAM(self->falltime, self->falltime_stream, 3); }
374 static PyObject * Compress_setThresh(Compress *self, PyObject *arg) { SET_PARAM(self->thresh, self->thresh_stream, 4); }
375 static PyObject * Compress_setRatio(Compress *self, PyObject *arg) { SET_PARAM(self->ratio, self->ratio_stream, 5); }
376
377 static PyObject *
378 Compress_setLookAhead(Compress *self, PyObject *arg)
379 {
380 MYFLT tmp;
381
382 ASSERT_ARG_NOT_NULL
383
384 if (PyNumber_Check(arg))
385 {
386 tmp = PyFloat_AsDouble(arg);
387
388 if (tmp <= 25.0)
389 self->lh_delay = (long)(tmp * 0.001 * self->sr);
390 else
391 PySys_WriteStdout("Compress: lookahead argument must be less than 25.0 ms.\n");
392 }
393
394 Py_RETURN_NONE;
395 }
396
397 static PyObject *
398 Compress_setKnee(Compress *self, PyObject *arg)
399 {
400 MYFLT tmp;
401
402 ASSERT_ARG_NOT_NULL
403
404 if (PyNumber_Check(arg))
405 {
406 tmp = PyFloat_AsDouble(arg);
407
408 if (tmp >= 0.0 && tmp <= 1.0)
409 self->knee = tmp;
410 else
411 PySys_WriteStdout("Compress: knee argument must be in range 0 (hard) -> 1 (soft).\n");
412 }
413
414 Py_RETURN_NONE;
415 }
416
417 static PyMemberDef Compress_members[] =
418 {
419 {"server", T_OBJECT_EX, offsetof(Compress, server), 0, "Pyo server."},
420 {"stream", T_OBJECT_EX, offsetof(Compress, stream), 0, "Stream object."},
421 {"input", T_OBJECT_EX, offsetof(Compress, input), 0, "Input sound object."},
422 {"thresh", T_OBJECT_EX, offsetof(Compress, thresh), 0, "Compressor threshold."},
423 {"ratio", T_OBJECT_EX, offsetof(Compress, ratio), 0, "Compressor ratio."},
424 {"risetime", T_OBJECT_EX, offsetof(Compress, risetime), 0, "Rising portamento time in seconds."},
425 {"falltime", T_OBJECT_EX, offsetof(Compress, falltime), 0, "Falling portamento time in seconds."},
426 {"mul", T_OBJECT_EX, offsetof(Compress, mul), 0, "Mul factor."},
427 {"add", T_OBJECT_EX, offsetof(Compress, add), 0, "Add factor."},
428 {NULL} /* Sentinel */
429 };
430
431 static PyMethodDef Compress_methods[] =
432 {
433 {"getServer", (PyCFunction)Compress_getServer, METH_NOARGS, "Returns server object."},
434 {"_getStream", (PyCFunction)Compress_getStream, METH_NOARGS, "Returns stream object."},
435 {"play", (PyCFunction)Compress_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
436 {"out", (PyCFunction)Compress_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
437 {"stop", (PyCFunction)Compress_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
438 {"setThresh", (PyCFunction)Compress_setThresh, METH_O, "Sets compressor threshold."},
439 {"setRatio", (PyCFunction)Compress_setRatio, METH_O, "Sets compressor ratio."},
440 {"setRiseTime", (PyCFunction)Compress_setRiseTime, METH_O, "Sets rising portamento time in seconds."},
441 {"setFallTime", (PyCFunction)Compress_setFallTime, METH_O, "Sets falling portamento time in seconds."},
442 {"setLookAhead", (PyCFunction)Compress_setLookAhead, METH_O, "Sets look ahead time in ms."},
443 {"setKnee", (PyCFunction)Compress_setKnee, METH_O, "Sets the knee between 0 (hard) and 1 (soft)."},
444 {"setMul", (PyCFunction)Compress_setMul, METH_O, "Sets mul factor."},
445 {"setAdd", (PyCFunction)Compress_setAdd, METH_O, "Sets add factor."},
446 {"setSub", (PyCFunction)Compress_setSub, METH_O, "Sets inverse add factor."},
447 {"setDiv", (PyCFunction)Compress_setDiv, METH_O, "Sets inverse mul factor."},
448 {NULL} /* Sentinel */
449 };
450
451 static PyNumberMethods Compress_as_number =
452 {
453 (binaryfunc)Compress_add, /*nb_add*/
454 (binaryfunc)Compress_sub, /*nb_subtract*/
455 (binaryfunc)Compress_multiply, /*nb_multiply*/
456 0, /*nb_remainder*/
457 0, /*nb_divmod*/
458 0, /*nb_power*/
459 0, /*nb_neg*/
460 0, /*nb_pos*/
461 0, /*(unaryfunc)array_abs,*/
462 0, /*nb_nonzero*/
463 0, /*nb_invert*/
464 0, /*nb_lshift*/
465 0, /*nb_rshift*/
466 0, /*nb_and*/
467 0, /*nb_xor*/
468 0, /*nb_or*/
469 0, /*nb_int*/
470 0, /*nb_long*/
471 0, /*nb_float*/
472 (binaryfunc)Compress_inplace_add, /*inplace_add*/
473 (binaryfunc)Compress_inplace_sub, /*inplace_subtract*/
474 (binaryfunc)Compress_inplace_multiply, /*inplace_multiply*/
475 0, /*inplace_remainder*/
476 0, /*inplace_power*/
477 0, /*inplace_lshift*/
478 0, /*inplace_rshift*/
479 0, /*inplace_and*/
480 0, /*inplace_xor*/
481 0, /*inplace_or*/
482 0, /*nb_floor_divide*/
483 (binaryfunc)Compress_div, /*nb_true_divide*/
484 0, /*nb_inplace_floor_divide*/
485 (binaryfunc)Compress_inplace_div, /*nb_inplace_true_divide*/
486 0, /* nb_index */
487 };
488
489 PyTypeObject CompressType =
490 {
491 PyVarObject_HEAD_INIT(NULL, 0)
492 "_pyo.Compress_base", /*tp_name*/
493 sizeof(Compress), /*tp_basicsize*/
494 0, /*tp_itemsize*/
495 (destructor)Compress_dealloc, /*tp_dealloc*/
496 0, /*tp_print*/
497 0, /*tp_getattr*/
498 0, /*tp_setattr*/
499 0, /*tp_as_async (tp_compare in Python 2)*/
500 0, /*tp_repr*/
501 &Compress_as_number, /*tp_as_number*/
502 0, /*tp_as_sequence*/
503 0, /*tp_as_mapping*/
504 0, /*tp_hash */
505 0, /*tp_call*/
506 0, /*tp_str*/
507 0, /*tp_getattro*/
508 0, /*tp_setattro*/
509 0, /*tp_as_buffer*/
510 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
511 "Compress objects. Compress audio signal by a certain ratio above a threshold.", /* tp_doc */
512 (traverseproc)Compress_traverse, /* tp_traverse */
513 (inquiry)Compress_clear, /* tp_clear */
514 0, /* tp_richcompare */
515 0, /* tp_weaklistoffset */
516 0, /* tp_iter */
517 0, /* tp_iternext */
518 Compress_methods, /* tp_methods */
519 Compress_members, /* tp_members */
520 0, /* tp_getset */
521 0, /* tp_base */
522 0, /* tp_dict */
523 0, /* tp_descr_get */
524 0, /* tp_descr_set */
525 0, /* tp_dictoffset */
526 0, /* tp_init */
527 0, /* tp_alloc */
528 Compress_new, /* tp_new */
529 };
530
531 const MYFLT GATE_MIN_RAMP_TIME = 0.0001;
532 /************/
533 /* Gate */
534 /************/
535 typedef struct
536 {
537 pyo_audio_HEAD
538 PyObject *input;
539 Stream *input_stream;
540 PyObject *thresh;
541 Stream *thresh_stream;
542 PyObject *risetime;
543 Stream *risetime_stream;
544 PyObject *falltime;
545 Stream *falltime_stream;
546 int modebuffer[5]; // need at least 2 slots for mul & add
547 int outputAmp;
548 MYFLT lpfollow;
549 MYFLT lpfactor;
550 MYFLT gate;
551 MYFLT last_risetime;
552 MYFLT last_falltime;
553 MYFLT risefactor;
554 MYFLT fallfactor;
555 long lh_delay;
556 long lh_size;
557 long lh_in_count;
558 MYFLT *lh_buffer;
559 } Gate;
560
561 static void
562 Gate_filters_iii(Gate *self)
563 {
564 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
565 int i, ind;
566
567 MYFLT *in = Stream_getData((Stream *)self->input_stream);
568
569 thresh = PyFloat_AS_DOUBLE(self->thresh);
570 risetime = PyFloat_AS_DOUBLE(self->risetime);
571
572 if (risetime <= 0.0)
573 risetime = GATE_MIN_RAMP_TIME;
574
575 falltime = PyFloat_AS_DOUBLE(self->falltime);
576
577 if (falltime <= 0.0)
578 falltime = GATE_MIN_RAMP_TIME;
579
580 if (risetime != self->last_risetime)
581 {
582 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
583 self->last_risetime = risetime;
584 }
585
586 if (falltime != self->last_falltime)
587 {
588 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
589 self->last_falltime = falltime;
590 }
591
592 ampthresh = MYPOW(10.0, thresh * 0.05);
593
594 for (i = 0; i < self->bufsize; i++)
595 {
596 /* Follower */
597 absin = in[i] * in[i];
598 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
599
600 /* Gate slope */
601 if (self->lpfollow >= ampthresh)
602 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
603 else
604 self->gate = self->fallfactor * self->gate;
605
606 /* Look ahead */
607 ind = self->lh_in_count - self->lh_delay;
608
609 if (ind < 0)
610 ind += self->lh_size;
611
612 samp = self->lh_buffer[ind];
613
614 self->lh_buffer[self->lh_in_count] = in[i];
615 self->lh_in_count++;
616
617 if (self->lh_in_count >= self->lh_size)
618 self->lh_in_count = 0;
619
620 /* Gate the signal */
621 if (self->outputAmp == 0)
622 self->data[i] = samp * self->gate;
623 else
624 self->data[i] = self->gate;
625 }
626 }
627
628 static void
629 Gate_filters_aii(Gate *self)
630 {
631 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
632 int i, ind;
633
634 MYFLT *in = Stream_getData((Stream *)self->input_stream);
635 MYFLT *tr = Stream_getData((Stream *)self->thresh_stream);
636
637 risetime = PyFloat_AS_DOUBLE(self->risetime);
638
639 if (risetime <= 0.0)
640 risetime = GATE_MIN_RAMP_TIME;
641
642 falltime = PyFloat_AS_DOUBLE(self->falltime);
643
644 if (falltime <= 0.0)
645 falltime = GATE_MIN_RAMP_TIME;
646
647 if (risetime != self->last_risetime)
648 {
649 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
650 self->last_risetime = risetime;
651 }
652
653 if (falltime != self->last_falltime)
654 {
655 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
656 self->last_falltime = falltime;
657 }
658
659 for (i = 0; i < self->bufsize; i++)
660 {
661 thresh = tr[i];
662 ampthresh = MYPOW(10.0, thresh * 0.05);
663 /* Follower */
664 absin = in[i] * in[i];
665 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
666
667 /* Gate slope */
668 if (self->lpfollow >= ampthresh)
669 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
670 else
671 self->gate = self->fallfactor * self->gate;
672
673 /* Look ahead */
674 ind = self->lh_in_count - self->lh_delay;
675
676 if (ind < 0)
677 ind += self->lh_size;
678
679 samp = self->lh_buffer[ind];
680
681 self->lh_buffer[self->lh_in_count] = in[i];
682 self->lh_in_count++;
683
684 if (self->lh_in_count >= self->lh_size)
685 self->lh_in_count = 0;
686
687 /* Gate the signal */
688 if (self->outputAmp == 0)
689 self->data[i] = samp * self->gate;
690 else
691 self->data[i] = self->gate;
692 }
693 }
694
695 static void
696 Gate_filters_iai(Gate *self)
697 {
698 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
699 int i, ind;
700
701 MYFLT *in = Stream_getData((Stream *)self->input_stream);
702
703 thresh = PyFloat_AS_DOUBLE(self->thresh);
704 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
705
706 falltime = PyFloat_AS_DOUBLE(self->falltime);
707
708 if (falltime <= 0.0)
709 falltime = GATE_MIN_RAMP_TIME;
710
711 if (falltime != self->last_falltime)
712 {
713 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
714 self->last_falltime = falltime;
715 }
716
717 ampthresh = MYPOW(10.0, thresh * 0.05);
718
719 for (i = 0; i < self->bufsize; i++)
720 {
721 risetime = rise[i];
722
723 if (risetime <= 0.0)
724 risetime = GATE_MIN_RAMP_TIME;
725
726 if (risetime != self->last_risetime)
727 {
728 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
729 self->last_risetime = risetime;
730 }
731
732 /* Follower */
733 absin = in[i] * in[i];
734 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
735
736 /* Gate slope */
737 if (self->lpfollow >= ampthresh)
738 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
739 else
740 self->gate = self->fallfactor * self->gate;
741
742 /* Look ahead */
743 ind = self->lh_in_count - self->lh_delay;
744
745 if (ind < 0)
746 ind += self->lh_size;
747
748 samp = self->lh_buffer[ind];
749
750 self->lh_buffer[self->lh_in_count] = in[i];
751 self->lh_in_count++;
752
753 if (self->lh_in_count >= self->lh_size)
754 self->lh_in_count = 0;
755
756 /* Gate the signal */
757 if (self->outputAmp == 0)
758 self->data[i] = samp * self->gate;
759 else
760 self->data[i] = self->gate;
761 }
762 }
763
764 static void
765 Gate_filters_aai(Gate *self)
766 {
767 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
768 int i, ind;
769
770 MYFLT *in = Stream_getData((Stream *)self->input_stream);
771
772 MYFLT *tr = Stream_getData((Stream *)self->thresh_stream);
773 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
774
775 falltime = PyFloat_AS_DOUBLE(self->falltime);
776
777 if (falltime <= 0.0)
778 falltime = GATE_MIN_RAMP_TIME;
779
780 if (falltime != self->last_falltime)
781 {
782 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
783 self->last_falltime = falltime;
784 }
785
786 for (i = 0; i < self->bufsize; i++)
787 {
788 thresh = tr[i];
789 ampthresh = MYPOW(10.0, thresh * 0.05);
790 risetime = rise[i];
791
792 if (risetime <= 0.0)
793 risetime = GATE_MIN_RAMP_TIME;
794
795 if (risetime != self->last_risetime)
796 {
797 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
798 self->last_risetime = risetime;
799 }
800
801 /* Follower */
802 absin = in[i] * in[i];
803 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
804
805 /* Gate slope */
806 if (self->lpfollow >= ampthresh)
807 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
808 else
809 self->gate = self->fallfactor * self->gate;
810
811 /* Look ahead */
812 ind = self->lh_in_count - self->lh_delay;
813
814 if (ind < 0)
815 ind += self->lh_size;
816
817 samp = self->lh_buffer[ind];
818
819 self->lh_buffer[self->lh_in_count] = in[i];
820 self->lh_in_count++;
821
822 if (self->lh_in_count >= self->lh_size)
823 self->lh_in_count = 0;
824
825 /* Gate the signal */
826 if (self->outputAmp == 0)
827 self->data[i] = samp * self->gate;
828 else
829 self->data[i] = self->gate;
830 }
831 }
832
833 static void
834 Gate_filters_iia(Gate *self)
835 {
836 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
837 int i, ind;
838
839 MYFLT *in = Stream_getData((Stream *)self->input_stream);
840
841 thresh = PyFloat_AS_DOUBLE(self->thresh);
842 risetime = PyFloat_AS_DOUBLE(self->risetime);
843
844 if (risetime <= 0.0)
845 risetime = GATE_MIN_RAMP_TIME;
846
847 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
848
849 if (risetime != self->last_risetime)
850 {
851 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
852 self->last_risetime = risetime;
853 }
854
855 ampthresh = MYPOW(10.0, thresh * 0.05);
856
857 for (i = 0; i < self->bufsize; i++)
858 {
859 falltime = fall[i];
860
861 if (falltime <= 0.0)
862 falltime = GATE_MIN_RAMP_TIME;
863
864 if (falltime != self->last_falltime)
865 {
866 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
867 self->last_falltime = falltime;
868 }
869
870 /* Follower */
871 absin = in[i] * in[i];
872 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
873
874 /* Gate slope */
875 if (self->lpfollow >= ampthresh)
876 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
877 else
878 self->gate = self->fallfactor * self->gate;
879
880 /* Look ahead */
881 ind = self->lh_in_count - self->lh_delay;
882
883 if (ind < 0)
884 ind += self->lh_size;
885
886 samp = self->lh_buffer[ind];
887
888 self->lh_buffer[self->lh_in_count] = in[i];
889 self->lh_in_count++;
890
891 if (self->lh_in_count >= self->lh_size)
892 self->lh_in_count = 0;
893
894 /* Gate the signal */
895 if (self->outputAmp == 0)
896 self->data[i] = samp * self->gate;
897 else
898 self->data[i] = self->gate;
899 }
900 }
901
902 static void
903 Gate_filters_aia(Gate *self)
904 {
905 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
906 int i, ind;
907
908 MYFLT *in = Stream_getData((Stream *)self->input_stream);
909
910 MYFLT *tr = Stream_getData((Stream *)self->thresh_stream);
911 risetime = PyFloat_AS_DOUBLE(self->risetime);
912
913 if (risetime <= 0.0)
914 risetime = GATE_MIN_RAMP_TIME;
915
916 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
917
918 if (risetime != self->last_risetime)
919 {
920 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
921 self->last_risetime = risetime;
922 }
923
924 for (i = 0; i < self->bufsize; i++)
925 {
926 thresh = tr[i];
927 ampthresh = MYPOW(10.0, thresh * 0.05);
928 falltime = fall[i];
929
930 if (falltime <= 0.0)
931 falltime = GATE_MIN_RAMP_TIME;
932
933 if (falltime != self->last_falltime)
934 {
935 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
936 self->last_falltime = falltime;
937 }
938
939 /* Follower */
940 absin = in[i] * in[i];
941 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
942
943 /* Gate slope */
944 if (self->lpfollow >= ampthresh)
945 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
946 else
947 self->gate = self->fallfactor * self->gate;
948
949 /* Look ahead */
950 ind = self->lh_in_count - self->lh_delay;
951
952 if (ind < 0)
953 ind += self->lh_size;
954
955 samp = self->lh_buffer[ind];
956
957 self->lh_buffer[self->lh_in_count] = in[i];
958 self->lh_in_count++;
959
960 if (self->lh_in_count >= self->lh_size)
961 self->lh_in_count = 0;
962
963 /* Gate the signal */
964 if (self->outputAmp == 0)
965 self->data[i] = samp * self->gate;
966 else
967 self->data[i] = self->gate;
968 }
969 }
970
971 static void
972 Gate_filters_iaa(Gate *self)
973 {
974 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
975 int i, ind;
976
977 MYFLT *in = Stream_getData((Stream *)self->input_stream);
978
979 thresh = PyFloat_AS_DOUBLE(self->thresh);
980 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
981 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
982
983 ampthresh = MYPOW(10.0, thresh * 0.05);
984
985 for (i = 0; i < self->bufsize; i++)
986 {
987 risetime = rise[i];
988
989 if (risetime <= 0.0)
990 risetime = GATE_MIN_RAMP_TIME;
991
992 if (risetime != self->last_risetime)
993 {
994 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
995 self->last_risetime = risetime;
996 }
997
998 falltime = fall[i];
999
1000 if (falltime <= 0.0)
1001 falltime = GATE_MIN_RAMP_TIME;
1002
1003 if (falltime != self->last_falltime)
1004 {
1005 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
1006 self->last_falltime = falltime;
1007 }
1008
1009 /* Follower */
1010 absin = in[i] * in[i];
1011 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
1012
1013 /* Gate slope */
1014 if (self->lpfollow >= ampthresh)
1015 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
1016 else
1017 self->gate = self->fallfactor * self->gate;
1018
1019 /* Look ahead */
1020 ind = self->lh_in_count - self->lh_delay;
1021
1022 if (ind < 0)
1023 ind += self->lh_size;
1024
1025 samp = self->lh_buffer[ind];
1026
1027 self->lh_buffer[self->lh_in_count] = in[i];
1028 self->lh_in_count++;
1029
1030 if (self->lh_in_count >= self->lh_size)
1031 self->lh_in_count = 0;
1032
1033 /* Gate the signal */
1034 if (self->outputAmp == 0)
1035 self->data[i] = samp * self->gate;
1036 else
1037 self->data[i] = self->gate;
1038 }
1039 }
1040
1041 static void
1042 Gate_filters_aaa(Gate *self)
1043 {
1044 MYFLT samp, absin, thresh, ampthresh, risetime, falltime;
1045 int i, ind;
1046
1047 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1048
1049 MYFLT *tr = Stream_getData((Stream *)self->thresh_stream);
1050 MYFLT *rise = Stream_getData((Stream *)self->risetime_stream);
1051 MYFLT *fall = Stream_getData((Stream *)self->falltime_stream);
1052
1053 for (i = 0; i < self->bufsize; i++)
1054 {
1055 thresh = tr[i];
1056 ampthresh = MYPOW(10.0, thresh * 0.05);
1057 risetime = rise[i];
1058
1059 if (risetime <= 0.0)
1060 risetime = 0.001;
1061
1062 if (risetime != self->last_risetime)
1063 {
1064 self->risefactor = MYEXP(-1.0 / (self->sr * risetime));
1065 self->last_risetime = risetime;
1066 }
1067
1068 falltime = fall[i];
1069
1070 if (falltime <= 0.0)
1071 falltime = 0.001;
1072
1073 if (falltime != self->last_falltime)
1074 {
1075 self->fallfactor = MYEXP(-1.0 / (self->sr * falltime));
1076 self->last_falltime = falltime;
1077 }
1078
1079 /* Follower */
1080 absin = in[i] * in[i];
1081 self->lpfollow = absin + self->lpfactor * (self->lpfollow - absin);
1082
1083 /* Gate slope */
1084 if (self->lpfollow >= ampthresh)
1085 self->gate = 1.0 + self->risefactor * (self->gate - 1.0);
1086 else
1087 self->gate = self->fallfactor * self->gate;
1088
1089 /* Look ahead */
1090 ind = self->lh_in_count - self->lh_delay;
1091
1092 if (ind < 0)
1093 ind += self->lh_size;
1094
1095 samp = self->lh_buffer[ind];
1096
1097 self->lh_buffer[self->lh_in_count] = in[i];
1098 self->lh_in_count++;
1099
1100 if (self->lh_in_count >= self->lh_size)
1101 self->lh_in_count = 0;
1102
1103 /* Gate the signal */
1104 if (self->outputAmp == 0)
1105 self->data[i] = samp * self->gate;
1106 else
1107 self->data[i] = self->gate;
1108 }
1109 }
1110
1111 static void Gate_postprocessing_ii(Gate *self) { POST_PROCESSING_II };
1112 static void Gate_postprocessing_ai(Gate *self) { POST_PROCESSING_AI };
1113 static void Gate_postprocessing_ia(Gate *self) { POST_PROCESSING_IA };
1114 static void Gate_postprocessing_aa(Gate *self) { POST_PROCESSING_AA };
1115 static void Gate_postprocessing_ireva(Gate *self) { POST_PROCESSING_IREVA };
1116 static void Gate_postprocessing_areva(Gate *self) { POST_PROCESSING_AREVA };
1117 static void Gate_postprocessing_revai(Gate *self) { POST_PROCESSING_REVAI };
1118 static void Gate_postprocessing_revaa(Gate *self) { POST_PROCESSING_REVAA };
1119 static void Gate_postprocessing_revareva(Gate *self) { POST_PROCESSING_REVAREVA };
1120
1121 static void
1122 Gate_setProcMode(Gate *self)
1123 {
1124 int procmode, muladdmode;
1125 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
1126 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1127
1128 switch (procmode)
1129 {
1130 case 0:
1131 self->proc_func_ptr = Gate_filters_iii;
1132 break;
1133
1134 case 1:
1135 self->proc_func_ptr = Gate_filters_aii;
1136 break;
1137
1138 case 10:
1139 self->proc_func_ptr = Gate_filters_iai;
1140 break;
1141
1142 case 11:
1143 self->proc_func_ptr = Gate_filters_aai;
1144 break;
1145
1146 case 100:
1147 self->proc_func_ptr = Gate_filters_iia;
1148 break;
1149
1150 case 101:
1151 self->proc_func_ptr = Gate_filters_aia;
1152 break;
1153
1154 case 110:
1155 self->proc_func_ptr = Gate_filters_iaa;
1156 break;
1157
1158 case 111:
1159 self->proc_func_ptr = Gate_filters_aaa;
1160 break;
1161 }
1162
1163 switch (muladdmode)
1164 {
1165 case 0:
1166 self->muladd_func_ptr = Gate_postprocessing_ii;
1167 break;
1168
1169 case 1:
1170 self->muladd_func_ptr = Gate_postprocessing_ai;
1171 break;
1172
1173 case 2:
1174 self->muladd_func_ptr = Gate_postprocessing_revai;
1175 break;
1176
1177 case 10:
1178 self->muladd_func_ptr = Gate_postprocessing_ia;
1179 break;
1180
1181 case 11:
1182 self->muladd_func_ptr = Gate_postprocessing_aa;
1183 break;
1184
1185 case 12:
1186 self->muladd_func_ptr = Gate_postprocessing_revaa;
1187 break;
1188
1189 case 20:
1190 self->muladd_func_ptr = Gate_postprocessing_ireva;
1191 break;
1192
1193 case 21:
1194 self->muladd_func_ptr = Gate_postprocessing_areva;
1195 break;
1196
1197 case 22:
1198 self->muladd_func_ptr = Gate_postprocessing_revareva;
1199 break;
1200 }
1201 }
1202
1203 static void
1204 Gate_compute_next_data_frame(Gate *self)
1205 {
1206 (*self->proc_func_ptr)(self);
1207 (*self->muladd_func_ptr)(self);
1208 }
1209
1210 static int
1211 Gate_traverse(Gate *self, visitproc visit, void *arg)
1212 {
1213 pyo_VISIT
1214 Py_VISIT(self->input);
1215 Py_VISIT(self->thresh);
1216 Py_VISIT(self->risetime);
1217 Py_VISIT(self->falltime);
1218 return 0;
1219 }
1220
1221 static int
1222 Gate_clear(Gate *self)
1223 {
1224 pyo_CLEAR
1225 Py_CLEAR(self->input);
1226 Py_CLEAR(self->thresh);
1227 Py_CLEAR(self->risetime);
1228 Py_CLEAR(self->falltime);
1229 return 0;
1230 }
1231
1232 static void
1233 Gate_dealloc(Gate* self)
1234 {
1235 pyo_DEALLOC
1236 PyMem_RawFree(self->lh_buffer);
1237 Gate_clear(self);
1238 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1239 Py_TYPE(self)->tp_free((PyObject*)self);
1240 }
1241
1242 static PyObject *
1243 Gate_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1244 {
1245 int i;
1246 PyObject *inputtmp, *input_streamtmp, *threshtmp = NULL, *risetimetmp = NULL, *falltimetmp = NULL, *multmp = NULL, *addtmp = NULL;
1247 PyObject *looktmp = NULL;
1248 Gate *self;
1249 self = (Gate *)type->tp_alloc(type, 0);
1250
1251 self->thresh = PyFloat_FromDouble(-70.0);
1252 self->risetime = PyFloat_FromDouble(0.01);
1253 self->falltime = PyFloat_FromDouble(0.05);
1254 self->gate = self->lpfollow = 0.0;
1255 self->last_risetime = -1.0;
1256 self->last_falltime = -1.0;
1257 self->risefactor = self->fallfactor = 0.99;
1258 self->modebuffer[0] = 0;
1259 self->modebuffer[1] = 0;
1260 self->modebuffer[2] = 0;
1261 self->modebuffer[3] = 0;
1262 self->modebuffer[4] = 0;
1263 self->lh_delay = 0;
1264 self->lh_in_count = 0;
1265 self->outputAmp = 0;
1266
1267 INIT_OBJECT_COMMON
1268
1269 self->lpfactor = MYEXP(-1.0 / (self->sr / 20.0));
1270
1271 Stream_setFunctionPtr(self->stream, Gate_compute_next_data_frame);
1272 self->mode_func_ptr = Gate_setProcMode;
1273
1274 static char *kwlist[] = {"input", "thresh", "risetime", "falltime", "lookahead", "outputAmp", "mul", "add", NULL};
1275
1276 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOiOO", kwlist, &inputtmp, &threshtmp, &risetimetmp, &falltimetmp, &looktmp, &self->outputAmp, &multmp, &addtmp))
1277 Py_RETURN_NONE;
1278
1279 INIT_INPUT_STREAM
1280
1281 if (threshtmp)
1282 {
1283 PyObject_CallMethod((PyObject *)self, "setThresh", "O", threshtmp);
1284 }
1285
1286 if (risetimetmp)
1287 {
1288 PyObject_CallMethod((PyObject *)self, "setRiseTime", "O", risetimetmp);
1289 }
1290
1291 if (falltimetmp)
1292 {
1293 PyObject_CallMethod((PyObject *)self, "setFallTime", "O", falltimetmp);
1294 }
1295
1296 if (multmp)
1297 {
1298 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1299 }
1300
1301 if (addtmp)
1302 {
1303 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1304 }
1305
1306 PyObject_CallMethod((PyObject *)self, "setLookAhead", "O", looktmp);
1307
1308 self->lh_size = (long)(0.025 * self->sr + 0.5);
1309 self->lh_buffer = (MYFLT *)PyMem_RawRealloc(self->lh_buffer, (self->lh_size + 1) * sizeof(MYFLT));
1310
1311 for (i = 0; i < (self->lh_size + 1); i++)
1312 {
1313 self->lh_buffer[i] = 0.;
1314 }
1315
1316 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1317
1318 (*self->mode_func_ptr)(self);
1319
1320 return (PyObject *)self;
1321 }
1322
1323 static PyObject * Gate_getServer(Gate* self) { GET_SERVER };
1324 static PyObject * Gate_getStream(Gate* self) { GET_STREAM };
1325 static PyObject * Gate_setMul(Gate *self, PyObject *arg) { SET_MUL };
1326 static PyObject * Gate_setAdd(Gate *self, PyObject *arg) { SET_ADD };
1327 static PyObject * Gate_setSub(Gate *self, PyObject *arg) { SET_SUB };
1328 static PyObject * Gate_setDiv(Gate *self, PyObject *arg) { SET_DIV };
1329
1330 static PyObject * Gate_play(Gate *self, PyObject *args, PyObject *kwds) { PLAY };
1331 static PyObject * Gate_out(Gate *self, PyObject *args, PyObject *kwds) { OUT };
1332 static PyObject * Gate_stop(Gate *self, PyObject *args, PyObject *kwds) { STOP };
1333
1334 static PyObject * Gate_multiply(Gate *self, PyObject *arg) { MULTIPLY };
1335 static PyObject * Gate_inplace_multiply(Gate *self, PyObject *arg) { INPLACE_MULTIPLY };
1336 static PyObject * Gate_add(Gate *self, PyObject *arg) { ADD };
1337 static PyObject * Gate_inplace_add(Gate *self, PyObject *arg) { INPLACE_ADD };
1338 static PyObject * Gate_sub(Gate *self, PyObject *arg) { SUB };
1339 static PyObject * Gate_inplace_sub(Gate *self, PyObject *arg) { INPLACE_SUB };
1340 static PyObject * Gate_div(Gate *self, PyObject *arg) { DIV };
1341 static PyObject * Gate_inplace_div(Gate *self, PyObject *arg) { INPLACE_DIV };
1342
1343 static PyObject * Gate_setThresh(Gate *self, PyObject *arg) { SET_PARAM(self->thresh, self->thresh_stream, 2); }
1344 static PyObject * Gate_setRiseTime(Gate *self, PyObject *arg) { SET_PARAM(self->risetime, self->risetime_stream, 3); }
1345 static PyObject * Gate_setFallTime(Gate *self, PyObject *arg) { SET_PARAM(self->falltime, self->falltime_stream, 4); }
1346
1347 static PyObject *
1348 Gate_setLookAhead(Gate *self, PyObject *arg)
1349 {
1350 MYFLT tmp;
1351
1352 ASSERT_ARG_NOT_NULL
1353
1354 if (PyNumber_Check(arg))
1355 {
1356 tmp = PyFloat_AsDouble(arg);
1357
1358 if (tmp <= 25.0)
1359 self->lh_delay = (long)(tmp * 0.001 * self->sr);
1360 else
1361 PySys_WriteStdout("Gate: lookahead argument must be less than 25.0 ms.\n");
1362 }
1363
1364 Py_RETURN_NONE;
1365 }
1366
1367 static PyMemberDef Gate_members[] =
1368 {
1369 {"server", T_OBJECT_EX, offsetof(Gate, server), 0, "Pyo server."},
1370 {"stream", T_OBJECT_EX, offsetof(Gate, stream), 0, "Stream object."},
1371 {"input", T_OBJECT_EX, offsetof(Gate, input), 0, "Input sound object."},
1372 {"thresh", T_OBJECT_EX, offsetof(Gate, thresh), 0, "Threshold in dB."},
1373 {"risetime", T_OBJECT_EX, offsetof(Gate, risetime), 0, "Risetime in second."},
1374 {"falltime", T_OBJECT_EX, offsetof(Gate, falltime), 0, "Falltime in second."},
1375 {"mul", T_OBJECT_EX, offsetof(Gate, mul), 0, "Mul factor."},
1376 {"add", T_OBJECT_EX, offsetof(Gate, add), 0, "Add factor."},
1377 {NULL} /* Sentinel */
1378 };
1379
1380 static PyMethodDef Gate_methods[] =
1381 {
1382 {"getServer", (PyCFunction)Gate_getServer, METH_NOARGS, "Returns server object."},
1383 {"_getStream", (PyCFunction)Gate_getStream, METH_NOARGS, "Returns stream object."},
1384 {"play", (PyCFunction)Gate_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1385 {"out", (PyCFunction)Gate_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1386 {"stop", (PyCFunction)Gate_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1387 {"setThresh", (PyCFunction)Gate_setThresh, METH_O, "Sets the threshold in dB."},
1388 {"setRiseTime", (PyCFunction)Gate_setRiseTime, METH_O, "Sets filter risetime in second."},
1389 {"setFallTime", (PyCFunction)Gate_setFallTime, METH_O, "Sets filter falltime in second."},
1390 {"setLookAhead", (PyCFunction)Gate_setLookAhead, METH_O, "Sets look ahead time in ms."},
1391 {"setMul", (PyCFunction)Gate_setMul, METH_O, "Sets oscillator mul factor."},
1392 {"setAdd", (PyCFunction)Gate_setAdd, METH_O, "Sets oscillator add factor."},
1393 {"setSub", (PyCFunction)Gate_setSub, METH_O, "Sets inverse add factor."},
1394 {"setDiv", (PyCFunction)Gate_setDiv, METH_O, "Sets inverse mul factor."},
1395 {NULL} /* Sentinel */
1396 };
1397
1398 static PyNumberMethods Gate_as_number =
1399 {
1400 (binaryfunc)Gate_add, /*nb_add*/
1401 (binaryfunc)Gate_sub, /*nb_subtract*/
1402 (binaryfunc)Gate_multiply, /*nb_multiply*/
1403 0, /*nb_remainder*/
1404 0, /*nb_divmod*/
1405 0, /*nb_power*/
1406 0, /*nb_neg*/
1407 0, /*nb_pos*/
1408 0, /*(unaryfunc)array_abs,*/
1409 0, /*nb_nonzero*/
1410 0, /*nb_invert*/
1411 0, /*nb_lshift*/
1412 0, /*nb_rshift*/
1413 0, /*nb_and*/
1414 0, /*nb_xor*/
1415 0, /*nb_or*/
1416 0, /*nb_int*/
1417 0, /*nb_long*/
1418 0, /*nb_float*/
1419 (binaryfunc)Gate_inplace_add, /*inplace_add*/
1420 (binaryfunc)Gate_inplace_sub, /*inplace_subtract*/
1421 (binaryfunc)Gate_inplace_multiply, /*inplace_multiply*/
1422 0, /*inplace_remainder*/
1423 0, /*inplace_power*/
1424 0, /*inplace_lshift*/
1425 0, /*inplace_rshift*/
1426 0, /*inplace_and*/
1427 0, /*inplace_xor*/
1428 0, /*inplace_or*/
1429 0, /*nb_floor_divide*/
1430 (binaryfunc)Gate_div, /*nb_true_divide*/
1431 0, /*nb_inplace_floor_divide*/
1432 (binaryfunc)Gate_inplace_div, /*nb_inplace_true_divide*/
1433 0, /* nb_index */
1434 };
1435
1436 PyTypeObject GateType =
1437 {
1438 PyVarObject_HEAD_INIT(NULL, 0)
1439 "_pyo.Gate_base", /*tp_name*/
1440 sizeof(Gate), /*tp_basicsize*/
1441 0, /*tp_itemsize*/
1442 (destructor)Gate_dealloc, /*tp_dealloc*/
1443 0, /*tp_print*/
1444 0, /*tp_getattr*/
1445 0, /*tp_setattr*/
1446 0, /*tp_as_async (tp_compare in Python 2)*/
1447 0, /*tp_repr*/
1448 &Gate_as_number, /*tp_as_number*/
1449 0, /*tp_as_sequence*/
1450 0, /*tp_as_mapping*/
1451 0, /*tp_hash */
1452 0, /*tp_call*/
1453 0, /*tp_str*/
1454 0, /*tp_getattro*/
1455 0, /*tp_setattro*/
1456 0, /*tp_as_buffer*/
1457 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1458 "Gate objects. Noise gate dynamic processor.", /* tp_doc */
1459 (traverseproc)Gate_traverse, /* tp_traverse */
1460 (inquiry)Gate_clear, /* tp_clear */
1461 0, /* tp_richcompare */
1462 0, /* tp_weaklistoffset */
1463 0, /* tp_iter */
1464 0, /* tp_iternext */
1465 Gate_methods, /* tp_methods */
1466 Gate_members, /* tp_members */
1467 0, /* tp_getset */
1468 0, /* tp_base */
1469 0, /* tp_dict */
1470 0, /* tp_descr_get */
1471 0, /* tp_descr_set */
1472 0, /* tp_dictoffset */
1473 0, /* tp_init */
1474 0, /* tp_alloc */
1475 Gate_new, /* tp_new */
1476 };
1477
1478 /************/
1479 /* Balance */
1480 /************/
1481 typedef struct
1482 {
1483 pyo_audio_HEAD
1484 PyObject *input;
1485 Stream *input_stream;
1486 PyObject *input2;
1487 Stream *input2_stream;
1488 PyObject *freq;
1489 Stream *freq_stream;
1490 int modebuffer[3]; // need at least 2 slots for mul & add
1491 MYFLT follow1;
1492 MYFLT follow2;
1493 MYFLT last_freq;
1494 MYFLT factor;
1495 } Balance;
1496
1497 static void
1498 Balance_filters_i(Balance *self)
1499 {
1500 MYFLT absin, freq;
1501 int i;
1502 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1503 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
1504 freq = PyFloat_AS_DOUBLE(self->freq);
1505
1506 if (freq < 0.1)
1507 freq = 0.1;
1508
1509 if (freq != self->last_freq)
1510 {
1511 self->factor = MYEXP(-1.0 / (self->sr / freq));
1512 self->last_freq = freq;
1513 }
1514
1515 for (i = 0; i < self->bufsize; i++)
1516 {
1517 absin = in[i];
1518
1519 if (absin < 0.0)
1520 absin = -absin;
1521
1522 self->follow1 = absin + self->factor * (self->follow1 - absin);
1523
1524 if (self->follow1 < 0.001)
1525 self->follow1 = 0.001;
1526
1527 absin = in2[i];
1528
1529 if (absin < 0.0)
1530 absin = -absin;
1531
1532 self->follow2 = absin + self->factor * (self->follow2 - absin);
1533 self->data[i] = in[i] * (self->follow2 / self->follow1);
1534 }
1535 }
1536
1537 static void
1538 Balance_filters_a(Balance *self)
1539 {
1540 MYFLT absin, freq;
1541 int i;
1542 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1543 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
1544 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1545
1546 for (i = 0; i < self->bufsize; i++)
1547 {
1548 freq = fr[i];
1549
1550 if (freq < 0.1)
1551 freq = 0.1;
1552
1553 if (freq != self->last_freq)
1554 {
1555 self->factor = MYEXP(-1.0 / (self->sr / freq));
1556 self->last_freq = freq;
1557 }
1558
1559 absin = in[i];
1560
1561 if (absin < 0.0)
1562 absin = -absin;
1563
1564 self->follow1 = absin + self->factor * (self->follow1 - absin);
1565
1566 if (self->follow1 < 0.001)
1567 self->follow1 = 0.001;
1568
1569 absin = in2[i];
1570
1571 if (absin < 0.0)
1572 absin = -absin;
1573
1574 self->follow2 = absin + self->factor * (self->follow2 - absin);
1575 self->data[i] = in[i] * (self->follow2 / self->follow1);
1576 }
1577 }
1578
1579 static void Balance_postprocessing_ii(Balance *self) { POST_PROCESSING_II };
1580 static void Balance_postprocessing_ai(Balance *self) { POST_PROCESSING_AI };
1581 static void Balance_postprocessing_ia(Balance *self) { POST_PROCESSING_IA };
1582 static void Balance_postprocessing_aa(Balance *self) { POST_PROCESSING_AA };
1583 static void Balance_postprocessing_ireva(Balance *self) { POST_PROCESSING_IREVA };
1584 static void Balance_postprocessing_areva(Balance *self) { POST_PROCESSING_AREVA };
1585 static void Balance_postprocessing_revai(Balance *self) { POST_PROCESSING_REVAI };
1586 static void Balance_postprocessing_revaa(Balance *self) { POST_PROCESSING_REVAA };
1587 static void Balance_postprocessing_revareva(Balance *self) { POST_PROCESSING_REVAREVA };
1588
1589 static void
1590 Balance_setProcMode(Balance *self)
1591 {
1592 int procmode, muladdmode;
1593 procmode = self->modebuffer[2];
1594 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1595
1596 switch (procmode)
1597 {
1598 case 0:
1599 self->proc_func_ptr = Balance_filters_i;
1600 break;
1601
1602 case 1:
1603 self->proc_func_ptr = Balance_filters_a;
1604 break;
1605 }
1606
1607 switch (muladdmode)
1608 {
1609 case 0:
1610 self->muladd_func_ptr = Balance_postprocessing_ii;
1611 break;
1612
1613 case 1:
1614 self->muladd_func_ptr = Balance_postprocessing_ai;
1615 break;
1616
1617 case 2:
1618 self->muladd_func_ptr = Balance_postprocessing_revai;
1619 break;
1620
1621 case 10:
1622 self->muladd_func_ptr = Balance_postprocessing_ia;
1623 break;
1624
1625 case 11:
1626 self->muladd_func_ptr = Balance_postprocessing_aa;
1627 break;
1628
1629 case 12:
1630 self->muladd_func_ptr = Balance_postprocessing_revaa;
1631 break;
1632
1633 case 20:
1634 self->muladd_func_ptr = Balance_postprocessing_ireva;
1635 break;
1636
1637 case 21:
1638 self->muladd_func_ptr = Balance_postprocessing_areva;
1639 break;
1640
1641 case 22:
1642 self->muladd_func_ptr = Balance_postprocessing_revareva;
1643 break;
1644 }
1645 }
1646
1647 static void
1648 Balance_compute_next_data_frame(Balance *self)
1649 {
1650 (*self->proc_func_ptr)(self);
1651 (*self->muladd_func_ptr)(self);
1652 }
1653
1654 static int
1655 Balance_traverse(Balance *self, visitproc visit, void *arg)
1656 {
1657 pyo_VISIT
1658 Py_VISIT(self->input);
1659 Py_VISIT(self->input2);
1660 Py_VISIT(self->freq);
1661 return 0;
1662 }
1663
1664 static int
1665 Balance_clear(Balance *self)
1666 {
1667 pyo_CLEAR
1668 Py_CLEAR(self->input);
1669 Py_CLEAR(self->input2);
1670 Py_CLEAR(self->freq);
1671 return 0;
1672 }
1673
1674 static void
1675 Balance_dealloc(Balance* self)
1676 {
1677 pyo_DEALLOC
1678 Balance_clear(self);
1679 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1680 Py_TYPE(self)->tp_free((PyObject*)self);
1681 }
1682
1683 static PyObject *
1684 Balance_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1685 {
1686 int i;
1687 PyObject *inputtmp, *input_streamtmp, *input2tmp, *input2_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
1688 Balance *self;
1689 self = (Balance *)type->tp_alloc(type, 0);
1690
1691 self->freq = PyFloat_FromDouble(10);
1692 self->follow1 = self->follow2 = 0.0;
1693 self->last_freq = -1.0;
1694 self->factor = 0.99;
1695 self->modebuffer[0] = 0;
1696 self->modebuffer[1] = 0;
1697 self->modebuffer[2] = 0;
1698
1699 INIT_OBJECT_COMMON
1700 Stream_setFunctionPtr(self->stream, Balance_compute_next_data_frame);
1701 self->mode_func_ptr = Balance_setProcMode;
1702
1703 static char *kwlist[] = {"input", "input2", "freq", "mul", "add", NULL};
1704
1705 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOO", kwlist, &inputtmp, &input2tmp, &freqtmp, &multmp, &addtmp))
1706 Py_RETURN_NONE;
1707
1708 INIT_INPUT_STREAM
1709
1710 self->input2 = input2tmp;
1711 Py_INCREF(self->input2);
1712 input2_streamtmp = PyObject_CallMethod((PyObject *)self->input2, "_getStream", NULL);
1713 self->input2_stream = (Stream *)input2_streamtmp;
1714 Py_INCREF(self->input2_stream);
1715
1716 if (freqtmp)
1717 {
1718 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1719 }
1720
1721 if (multmp)
1722 {
1723 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1724 }
1725
1726 if (addtmp)
1727 {
1728 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1729 }
1730
1731 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1732
1733 (*self->mode_func_ptr)(self);
1734
1735 return (PyObject *)self;
1736 }
1737
1738 static PyObject * Balance_getServer(Balance* self) { GET_SERVER };
1739 static PyObject * Balance_getStream(Balance* self) { GET_STREAM };
1740 static PyObject * Balance_setMul(Balance *self, PyObject *arg) { SET_MUL };
1741 static PyObject * Balance_setAdd(Balance *self, PyObject *arg) { SET_ADD };
1742 static PyObject * Balance_setSub(Balance *self, PyObject *arg) { SET_SUB };
1743 static PyObject * Balance_setDiv(Balance *self, PyObject *arg) { SET_DIV };
1744
1745 static PyObject * Balance_play(Balance *self, PyObject *args, PyObject *kwds) { PLAY };
1746 static PyObject * Balance_out(Balance *self, PyObject *args, PyObject *kwds) { OUT };
1747 static PyObject * Balance_stop(Balance *self, PyObject *args, PyObject *kwds) { STOP };
1748
1749 static PyObject * Balance_multiply(Balance *self, PyObject *arg) { MULTIPLY };
1750 static PyObject * Balance_inplace_multiply(Balance *self, PyObject *arg) { INPLACE_MULTIPLY };
1751 static PyObject * Balance_add(Balance *self, PyObject *arg) { ADD };
1752 static PyObject * Balance_inplace_add(Balance *self, PyObject *arg) { INPLACE_ADD };
1753 static PyObject * Balance_sub(Balance *self, PyObject *arg) { SUB };
1754 static PyObject * Balance_inplace_sub(Balance *self, PyObject *arg) { INPLACE_SUB };
1755 static PyObject * Balance_div(Balance *self, PyObject *arg) { DIV };
1756 static PyObject * Balance_inplace_div(Balance *self, PyObject *arg) { INPLACE_DIV };
1757
1758 static PyObject * Balance_setFreq(Balance *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
1759
1760 static PyMemberDef Balance_members[] =
1761 {
1762 {"server", T_OBJECT_EX, offsetof(Balance, server), 0, "Pyo server."},
1763 {"stream", T_OBJECT_EX, offsetof(Balance, stream), 0, "Stream object."},
1764 {"input", T_OBJECT_EX, offsetof(Balance, input), 0, "Input sound object."},
1765 {"input2", T_OBJECT_EX, offsetof(Balance, input2), 0, "Comparator input sound object."},
1766 {"freq", T_OBJECT_EX, offsetof(Balance, freq), 0, "Cutoff frequency in cycle per second."},
1767 {"mul", T_OBJECT_EX, offsetof(Balance, mul), 0, "Mul factor."},
1768 {"add", T_OBJECT_EX, offsetof(Balance, add), 0, "Add factor."},
1769 {NULL} /* Sentinel */
1770 };
1771
1772 static PyMethodDef Balance_methods[] =
1773 {
1774 {"getServer", (PyCFunction)Balance_getServer, METH_NOARGS, "Returns server object."},
1775 {"_getStream", (PyCFunction)Balance_getStream, METH_NOARGS, "Returns stream object."},
1776 {"play", (PyCFunction)Balance_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1777 {"out", (PyCFunction)Balance_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1778 {"stop", (PyCFunction)Balance_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1779 {"setFreq", (PyCFunction)Balance_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
1780 {"setMul", (PyCFunction)Balance_setMul, METH_O, "Sets oscillator mul factor."},
1781 {"setAdd", (PyCFunction)Balance_setAdd, METH_O, "Sets oscillator add factor."},
1782 {"setSub", (PyCFunction)Balance_setSub, METH_O, "Sets inverse add factor."},
1783 {"setDiv", (PyCFunction)Balance_setDiv, METH_O, "Sets inverse mul factor."},
1784 {NULL} /* Sentinel */
1785 };
1786
1787 static PyNumberMethods Balance_as_number =
1788 {
1789 (binaryfunc)Balance_add, /*nb_add*/
1790 (binaryfunc)Balance_sub, /*nb_subtract*/
1791 (binaryfunc)Balance_multiply, /*nb_multiply*/
1792 0, /*nb_remainder*/
1793 0, /*nb_divmod*/
1794 0, /*nb_power*/
1795 0, /*nb_neg*/
1796 0, /*nb_pos*/
1797 0, /*(unaryfunc)array_abs,*/
1798 0, /*nb_nonzero*/
1799 0, /*nb_invert*/
1800 0, /*nb_lshift*/
1801 0, /*nb_rshift*/
1802 0, /*nb_and*/
1803 0, /*nb_xor*/
1804 0, /*nb_or*/
1805 0, /*nb_int*/
1806 0, /*nb_long*/
1807 0, /*nb_float*/
1808 (binaryfunc)Balance_inplace_add, /*inplace_add*/
1809 (binaryfunc)Balance_inplace_sub, /*inplace_subtract*/
1810 (binaryfunc)Balance_inplace_multiply, /*inplace_multiply*/
1811 0, /*inplace_remainder*/
1812 0, /*inplace_power*/
1813 0, /*inplace_lshift*/
1814 0, /*inplace_rshift*/
1815 0, /*inplace_and*/
1816 0, /*inplace_xor*/
1817 0, /*inplace_or*/
1818 0, /*nb_floor_divide*/
1819 (binaryfunc)Balance_div, /*nb_true_divide*/
1820 0, /*nb_inplace_floor_divide*/
1821 (binaryfunc)Balance_inplace_div, /*nb_inplace_true_divide*/
1822 0, /* nb_index */
1823 };
1824
1825 PyTypeObject BalanceType =
1826 {
1827 PyVarObject_HEAD_INIT(NULL, 0)
1828 "_pyo.Balance_base", /*tp_name*/
1829 sizeof(Balance), /*tp_basicsize*/
1830 0, /*tp_itemsize*/
1831 (destructor)Balance_dealloc, /*tp_dealloc*/
1832 0, /*tp_print*/
1833 0, /*tp_getattr*/
1834 0, /*tp_setattr*/
1835 0, /*tp_as_async (tp_compare in Python 2)*/
1836 0, /*tp_repr*/
1837 &Balance_as_number, /*tp_as_number*/
1838 0, /*tp_as_sequence*/
1839 0, /*tp_as_mapping*/
1840 0, /*tp_hash */
1841 0, /*tp_call*/
1842 0, /*tp_str*/
1843 0, /*tp_getattro*/
1844 0, /*tp_setattro*/
1845 0, /*tp_as_buffer*/
1846 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1847 "Balance objects. The rms power of a signal is adjusted to match that of a comparator signal.", /* tp_doc */
1848 (traverseproc)Balance_traverse, /* tp_traverse */
1849 (inquiry)Balance_clear, /* tp_clear */
1850 0, /* tp_richcompare */
1851 0, /* tp_weaklistoffset */
1852 0, /* tp_iter */
1853 0, /* tp_iternext */
1854 Balance_methods, /* tp_methods */
1855 Balance_members, /* tp_members */
1856 0, /* tp_getset */
1857 0, /* tp_base */
1858 0, /* tp_dict */
1859 0, /* tp_descr_get */
1860 0, /* tp_descr_set */
1861 0, /* tp_dictoffset */
1862 0, /* tp_init */
1863 0, /* tp_alloc */
1864 Balance_new, /* tp_new */
1865 };
1866
1867 /* Expander */
1868 typedef struct
1869 {
1870 pyo_audio_HEAD
1871 PyObject *input;
1872 Stream *input_stream;
1873 PyObject *risetime;
1874 PyObject *falltime;
1875 PyObject *upthresh;
1876 PyObject *downthresh;
1877 PyObject *ratio;
1878 Stream *risetime_stream;
1879 Stream *falltime_stream;
1880 Stream *upthresh_stream;
1881 Stream *downthresh_stream;
1882 Stream *ratio_stream;
1883 int modebuffer[7]; // need at least 2 slots for mul & add
1884 int outputAmp;
1885 MYFLT follow;
1886 long lh_delay;
1887 long lh_size;
1888 long lh_in_count;
1889 MYFLT *lh_buffer;
1890 } Expand;
1891
1892 static void
1893 Expand_compress_soft(Expand *self)
1894 {
1895 MYFLT samp, absin, indb, diff, outa;
1896 MYFLT risetime, falltime, upthresh, downthresh, ratio;
1897 int i;
1898 long ind;
1899
1900 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1901
1902 if (self->modebuffer[2] == 0)
1903 risetime = PyFloat_AS_DOUBLE(self->risetime);
1904 else
1905 risetime = Stream_getData((Stream *)self->risetime_stream)[0];
1906
1907 if (risetime <= 0.0)
1908 risetime = 0.001;
1909
1910 if (self->modebuffer[3] == 0)
1911 falltime = PyFloat_AS_DOUBLE(self->falltime);
1912 else
1913 falltime = Stream_getData((Stream *)self->falltime_stream)[0];
1914
1915 if (falltime <= 0.0)
1916 falltime = 0.001;
1917
1918 if (self->modebuffer[4] == 0)
1919 upthresh = PyFloat_AS_DOUBLE(self->upthresh);
1920 else
1921 upthresh = Stream_getData((Stream *)self->upthresh_stream)[0];
1922
1923 if (upthresh > 0.0)
1924 upthresh = 0.0;
1925
1926 if (self->modebuffer[5] == 0)
1927 downthresh = PyFloat_AS_DOUBLE(self->downthresh);
1928 else
1929 downthresh = Stream_getData((Stream *)self->downthresh_stream)[0];
1930
1931 if (downthresh < -120.0)
1932 downthresh = -120.0;
1933 else if (downthresh > upthresh)
1934 downthresh = upthresh;
1935
1936 if (self->modebuffer[6] == 0)
1937 ratio = PyFloat_AS_DOUBLE(self->ratio);
1938 else
1939 ratio = Stream_getData((Stream *)self->ratio_stream)[0];
1940
1941 ratio = 1.0 / ratio;
1942 risetime = MYEXP(-1.0 / (self->sr * risetime));
1943 falltime = MYEXP(-1.0 / (self->sr * falltime));
1944
1945 for (i = 0; i < self->bufsize; i++)
1946 {
1947 /* Envelope follower */
1948 absin = in[i];
1949
1950 if (absin < 0.0)
1951 absin = -absin;
1952
1953 if (self->follow < absin)
1954 self->follow = absin + risetime * (self->follow - absin);
1955 else
1956 self->follow = absin + falltime * (self->follow - absin);
1957
1958 /* Look ahead */
1959 ind = self->lh_in_count - self->lh_delay;
1960
1961 if (ind < 0)
1962 ind += self->lh_size;
1963
1964 samp = self->lh_buffer[ind];
1965
1966 self->lh_buffer[self->lh_in_count] = in[i];
1967 self->lh_in_count++;
1968
1969 if (self->lh_in_count >= self->lh_size)
1970 self->lh_in_count = 0;
1971
1972 /* Expand signal */
1973 outa = 1.0;
1974 indb = 20.0 * MYLOG10(C_clip(self->follow));
1975
1976 if (indb > upthresh) /* Above upper threshold */
1977 {
1978 diff = indb - upthresh;
1979 outa = MYPOW(10.0, (diff * ratio - diff) * 0.05);
1980 }
1981 else if (indb < downthresh) /* Below lower threshold */
1982 {
1983 diff = downthresh - indb;
1984 outa = MYPOW(10.0, (diff - diff * ratio) * 0.05);
1985 }
1986
1987 outa = 1.0 / outa;
1988
1989 if (self->outputAmp == 0)
1990 self->data[i] = samp * outa;
1991 else
1992 self->data[i] = outa;
1993 }
1994 }
1995
1996 static void Expand_postprocessing_ii(Expand *self) { POST_PROCESSING_II };
1997 static void Expand_postprocessing_ai(Expand *self) { POST_PROCESSING_AI };
1998 static void Expand_postprocessing_ia(Expand *self) { POST_PROCESSING_IA };
1999 static void Expand_postprocessing_aa(Expand *self) { POST_PROCESSING_AA };
2000 static void Expand_postprocessing_ireva(Expand *self) { POST_PROCESSING_IREVA };
2001 static void Expand_postprocessing_areva(Expand *self) { POST_PROCESSING_AREVA };
2002 static void Expand_postprocessing_revai(Expand *self) { POST_PROCESSING_REVAI };
2003 static void Expand_postprocessing_revaa(Expand *self) { POST_PROCESSING_REVAA };
2004 static void Expand_postprocessing_revareva(Expand *self) { POST_PROCESSING_REVAREVA };
2005
2006 static void
2007 Expand_setProcMode(Expand *self)
2008 {
2009 int muladdmode;
2010 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2011
2012 switch (muladdmode)
2013 {
2014 case 0:
2015 self->muladd_func_ptr = Expand_postprocessing_ii;
2016 break;
2017
2018 case 1:
2019 self->muladd_func_ptr = Expand_postprocessing_ai;
2020 break;
2021
2022 case 2:
2023 self->muladd_func_ptr = Expand_postprocessing_revai;
2024 break;
2025
2026 case 10:
2027 self->muladd_func_ptr = Expand_postprocessing_ia;
2028 break;
2029
2030 case 11:
2031 self->muladd_func_ptr = Expand_postprocessing_aa;
2032 break;
2033
2034 case 12:
2035 self->muladd_func_ptr = Expand_postprocessing_revaa;
2036 break;
2037
2038 case 20:
2039 self->muladd_func_ptr = Expand_postprocessing_ireva;
2040 break;
2041
2042 case 21:
2043 self->muladd_func_ptr = Expand_postprocessing_areva;
2044 break;
2045
2046 case 22:
2047 self->muladd_func_ptr = Expand_postprocessing_revareva;
2048 break;
2049 }
2050 }
2051
2052 static void
2053 Expand_compute_next_data_frame(Expand *self)
2054 {
2055 (*self->proc_func_ptr)(self);
2056 (*self->muladd_func_ptr)(self);
2057 }
2058
2059 static int
2060 Expand_traverse(Expand *self, visitproc visit, void *arg)
2061 {
2062 pyo_VISIT
2063 Py_VISIT(self->input);
2064 Py_VISIT(self->risetime);
2065 Py_VISIT(self->falltime);
2066 Py_VISIT(self->upthresh);
2067 Py_VISIT(self->downthresh);
2068 Py_VISIT(self->ratio);
2069 return 0;
2070 }
2071
2072 static int
2073 Expand_clear(Expand *self)
2074 {
2075 pyo_CLEAR
2076 Py_CLEAR(self->input);
2077 Py_CLEAR(self->risetime);
2078 Py_CLEAR(self->falltime);
2079 Py_CLEAR(self->upthresh);
2080 Py_CLEAR(self->downthresh);
2081 Py_CLEAR(self->ratio);
2082 return 0;
2083 }
2084
2085 static void
2086 Expand_dealloc(Expand* self)
2087 {
2088 pyo_DEALLOC
2089 PyMem_RawFree(self->lh_buffer);
2090 Expand_clear(self);
2091 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2092 Py_TYPE(self)->tp_free((PyObject*)self);
2093 }
2094
2095 static PyObject *
2096 Expand_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2097 {
2098 int i;
2099 PyObject *inputtmp, *input_streamtmp, *upthreshtmp = NULL, *downthreshtmp = NULL, *ratiotmp = NULL, *risetimetmp = NULL, *falltimetmp = NULL, *multmp = NULL, *addtmp = NULL;
2100 PyObject *looktmp = NULL;
2101 Expand *self;
2102 self = (Expand *)type->tp_alloc(type, 0);
2103
2104 self->downthresh = PyFloat_FromDouble(-60.0);
2105 self->upthresh = PyFloat_FromDouble(-20.0);
2106 self->ratio = PyFloat_FromDouble(2.0);
2107 self->risetime = PyFloat_FromDouble(0.01);
2108 self->falltime = PyFloat_FromDouble(0.1);
2109 self->modebuffer[0] = 0;
2110 self->modebuffer[1] = 0;
2111 self->modebuffer[2] = 0;
2112 self->modebuffer[3] = 0;
2113 self->modebuffer[4] = 0;
2114 self->modebuffer[5] = 0;
2115 self->modebuffer[6] = 0;
2116 self->outputAmp = 0;
2117 self->follow = 0.0;
2118 self->lh_delay = 0;
2119 self->lh_in_count = 0;
2120
2121 INIT_OBJECT_COMMON
2122
2123 Stream_setFunctionPtr(self->stream, Expand_compute_next_data_frame);
2124 self->mode_func_ptr = Expand_setProcMode;
2125
2126 static char *kwlist[] = {"input", "downthresh", "upthresh", "ratio", "risetime", "falltime", "lookahead", "outputAmp", "mul", "add", NULL};
2127
2128 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOOOiOO", kwlist, &inputtmp, &downthreshtmp, &upthreshtmp, &ratiotmp, &risetimetmp, &falltimetmp, &looktmp, &self->outputAmp, &multmp, &addtmp))
2129 Py_RETURN_NONE;
2130
2131 INIT_INPUT_STREAM
2132
2133 if (downthreshtmp)
2134 {
2135 PyObject_CallMethod((PyObject *)self, "setDownThresh", "O", downthreshtmp);
2136 }
2137
2138 if (upthreshtmp)
2139 {
2140 PyObject_CallMethod((PyObject *)self, "setUpThresh", "O", upthreshtmp);
2141 }
2142
2143 if (ratiotmp)
2144 {
2145 PyObject_CallMethod((PyObject *)self, "setRatio", "O", ratiotmp);
2146 }
2147
2148 if (risetimetmp)
2149 {
2150 PyObject_CallMethod((PyObject *)self, "setRiseTime", "O", risetimetmp);
2151 }
2152
2153 if (falltimetmp)
2154 {
2155 PyObject_CallMethod((PyObject *)self, "setFallTime", "O", falltimetmp);
2156 }
2157
2158 if (multmp)
2159 {
2160 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2161 }
2162
2163 if (addtmp)
2164 {
2165 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2166 }
2167
2168 PyObject_CallMethod((PyObject *)self, "setLookAhead", "O", looktmp);
2169
2170 self->lh_size = (long)(0.025 * self->sr + 0.5);
2171 self->lh_buffer = (MYFLT *)PyMem_RawRealloc(self->lh_buffer, (self->lh_size + 1) * sizeof(MYFLT));
2172
2173 for (i = 0; i < (self->lh_size + 1); i++)
2174 {
2175 self->lh_buffer[i] = 0.;
2176 }
2177
2178 self->proc_func_ptr = Expand_compress_soft;
2179
2180 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2181
2182 (*self->mode_func_ptr)(self);
2183
2184 return (PyObject *)self;
2185 }
2186
2187 static PyObject * Expand_getServer(Expand* self) { GET_SERVER };
2188 static PyObject * Expand_getStream(Expand* self) { GET_STREAM };
2189 static PyObject * Expand_setMul(Expand *self, PyObject *arg) { SET_MUL };
2190 static PyObject * Expand_setAdd(Expand *self, PyObject *arg) { SET_ADD };
2191 static PyObject * Expand_setSub(Expand *self, PyObject *arg) { SET_SUB };
2192 static PyObject * Expand_setDiv(Expand *self, PyObject *arg) { SET_DIV };
2193
2194 static PyObject * Expand_play(Expand *self, PyObject *args, PyObject *kwds) { PLAY };
2195 static PyObject * Expand_out(Expand *self, PyObject *args, PyObject *kwds) { OUT };
2196 static PyObject * Expand_stop(Expand *self, PyObject *args, PyObject *kwds) { STOP };
2197
2198 static PyObject * Expand_multiply(Expand *self, PyObject *arg) { MULTIPLY };
2199 static PyObject * Expand_inplace_multiply(Expand *self, PyObject *arg) { INPLACE_MULTIPLY };
2200 static PyObject * Expand_add(Expand *self, PyObject *arg) { ADD };
2201 static PyObject * Expand_inplace_add(Expand *self, PyObject *arg) { INPLACE_ADD };
2202 static PyObject * Expand_sub(Expand *self, PyObject *arg) { SUB };
2203 static PyObject * Expand_inplace_sub(Expand *self, PyObject *arg) { INPLACE_SUB };
2204 static PyObject * Expand_div(Expand *self, PyObject *arg) { DIV };
2205 static PyObject * Expand_inplace_div(Expand *self, PyObject *arg) { INPLACE_DIV };
2206
2207 static PyObject * Expand_setRiseTime(Expand *self, PyObject *arg) { SET_PARAM(self->risetime, self->risetime_stream, 2); }
2208 static PyObject * Expand_setFallTime(Expand *self, PyObject *arg) { SET_PARAM(self->falltime, self->falltime_stream, 3); }
2209 static PyObject * Expand_setUpThresh(Expand *self, PyObject *arg) { SET_PARAM(self->upthresh, self->upthresh_stream, 4); }
2210 static PyObject * Expand_setDownThresh(Expand *self, PyObject *arg) { SET_PARAM(self->downthresh, self->downthresh_stream, 5); }
2211 static PyObject * Expand_setRatio(Expand *self, PyObject *arg) { SET_PARAM(self->ratio, self->ratio_stream, 6); }
2212
2213 static PyObject *
2214 Expand_setLookAhead(Expand *self, PyObject *arg)
2215 {
2216 MYFLT tmp;
2217
2218 ASSERT_ARG_NOT_NULL
2219
2220 if (PyNumber_Check(arg))
2221 {
2222 tmp = PyFloat_AsDouble(arg);
2223
2224 if (tmp <= 25.0)
2225 self->lh_delay = (long)(tmp * 0.001 * self->sr);
2226 else
2227 PySys_WriteStdout("Expand: lookahead argument must be less than 25.0 ms.\n");
2228 }
2229
2230 Py_RETURN_NONE;
2231 }
2232
2233 static PyMemberDef Expand_members[] =
2234 {
2235 {"server", T_OBJECT_EX, offsetof(Expand, server), 0, "Pyo server."},
2236 {"stream", T_OBJECT_EX, offsetof(Expand, stream), 0, "Stream object."},
2237 {"input", T_OBJECT_EX, offsetof(Expand, input), 0, "Input sound object."},
2238 {"downthresh", T_OBJECT_EX, offsetof(Expand, downthresh), 0, "Expander lower threshold."},
2239 {"upthresh", T_OBJECT_EX, offsetof(Expand, upthresh), 0, "Expander upper threshold."},
2240 {"ratio", T_OBJECT_EX, offsetof(Expand, ratio), 0, "Expandor ratio."},
2241 {"risetime", T_OBJECT_EX, offsetof(Expand, risetime), 0, "Rising portamento time in seconds."},
2242 {"falltime", T_OBJECT_EX, offsetof(Expand, falltime), 0, "Falling portamento time in seconds."},
2243 {"mul", T_OBJECT_EX, offsetof(Expand, mul), 0, "Mul factor."},
2244 {"add", T_OBJECT_EX, offsetof(Expand, add), 0, "Add factor."},
2245 {NULL} /* Sentinel */
2246 };
2247
2248 static PyMethodDef Expand_methods[] =
2249 {
2250 {"getServer", (PyCFunction)Expand_getServer, METH_NOARGS, "Returns server object."},
2251 {"_getStream", (PyCFunction)Expand_getStream, METH_NOARGS, "Returns stream object."},
2252 {"play", (PyCFunction)Expand_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2253 {"out", (PyCFunction)Expand_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2254 {"stop", (PyCFunction)Expand_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2255 {"setDownThresh", (PyCFunction)Expand_setDownThresh, METH_O, "Sets expander lower threshold."},
2256 {"setUpThresh", (PyCFunction)Expand_setUpThresh, METH_O, "Sets expander upper threshold."},
2257 {"setRatio", (PyCFunction)Expand_setRatio, METH_O, "Sets expander ratio."},
2258 {"setRiseTime", (PyCFunction)Expand_setRiseTime, METH_O, "Sets rising portamento time in seconds."},
2259 {"setFallTime", (PyCFunction)Expand_setFallTime, METH_O, "Sets falling portamento time in seconds."},
2260 {"setLookAhead", (PyCFunction)Expand_setLookAhead, METH_O, "Sets look ahead time in ms."},
2261 {"setMul", (PyCFunction)Expand_setMul, METH_O, "Sets mul factor."},
2262 {"setAdd", (PyCFunction)Expand_setAdd, METH_O, "Sets add factor."},
2263 {"setSub", (PyCFunction)Expand_setSub, METH_O, "Sets inverse add factor."},
2264 {"setDiv", (PyCFunction)Expand_setDiv, METH_O, "Sets inverse mul factor."},
2265 {NULL} /* Sentinel */
2266 };
2267
2268 static PyNumberMethods Expand_as_number =
2269 {
2270 (binaryfunc)Expand_add, /*nb_add*/
2271 (binaryfunc)Expand_sub, /*nb_subtract*/
2272 (binaryfunc)Expand_multiply, /*nb_multiply*/
2273 0, /*nb_remainder*/
2274 0, /*nb_divmod*/
2275 0, /*nb_power*/
2276 0, /*nb_neg*/
2277 0, /*nb_pos*/
2278 0, /*(unaryfunc)array_abs,*/
2279 0, /*nb_nonzero*/
2280 0, /*nb_invert*/
2281 0, /*nb_lshift*/
2282 0, /*nb_rshift*/
2283 0, /*nb_and*/
2284 0, /*nb_xor*/
2285 0, /*nb_or*/
2286 0, /*nb_int*/
2287 0, /*nb_long*/
2288 0, /*nb_float*/
2289 (binaryfunc)Expand_inplace_add, /*inplace_add*/
2290 (binaryfunc)Expand_inplace_sub, /*inplace_subtract*/
2291 (binaryfunc)Expand_inplace_multiply, /*inplace_multiply*/
2292 0, /*inplace_remainder*/
2293 0, /*inplace_power*/
2294 0, /*inplace_lshift*/
2295 0, /*inplace_rshift*/
2296 0, /*inplace_and*/
2297 0, /*inplace_xor*/
2298 0, /*inplace_or*/
2299 0, /*nb_floor_divide*/
2300 (binaryfunc)Expand_div, /*nb_true_divide*/
2301 0, /*nb_inplace_floor_divide*/
2302 (binaryfunc)Expand_inplace_div, /*nb_inplace_true_divide*/
2303 0, /* nb_index */
2304 };
2305
2306 PyTypeObject ExpandType =
2307 {
2308 PyVarObject_HEAD_INIT(NULL, 0)
2309 "_pyo.Expand_base", /*tp_name*/
2310 sizeof(Expand), /*tp_basicsize*/
2311 0, /*tp_itemsize*/
2312 (destructor)Expand_dealloc, /*tp_dealloc*/
2313 0, /*tp_print*/
2314 0, /*tp_getattr*/
2315 0, /*tp_setattr*/
2316 0, /*tp_as_async (tp_compare in Python 2)*/
2317 0, /*tp_repr*/
2318 &Expand_as_number, /*tp_as_number*/
2319 0, /*tp_as_sequence*/
2320 0, /*tp_as_mapping*/
2321 0, /*tp_hash */
2322 0, /*tp_call*/
2323 0, /*tp_str*/
2324 0, /*tp_getattro*/
2325 0, /*tp_setattro*/
2326 0, /*tp_as_buffer*/
2327 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2328 "Expand objects. Expand audio signal by a certain ratio when below or above certain thresholds.", /* tp_doc */
2329 (traverseproc)Expand_traverse, /* tp_traverse */
2330 (inquiry)Expand_clear, /* tp_clear */
2331 0, /* tp_richcompare */
2332 0, /* tp_weaklistoffset */
2333 0, /* tp_iter */
2334 0, /* tp_iternext */
2335 Expand_methods, /* tp_methods */
2336 Expand_members, /* tp_members */
2337 0, /* tp_getset */
2338 0, /* tp_base */
2339 0, /* tp_dict */
2340 0, /* tp_descr_get */
2341 0, /* tp_descr_set */
2342 0, /* tp_dictoffset */
2343 0, /* tp_init */
2344 0, /* tp_alloc */
2345 Expand_new, /* tp_new */
2346 };
2347