Debian Codegraph @

src/objects/hilbertmodule.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 typedef struct
30 {
31 pyo_audio_HEAD
32 PyObject *input;
33 Stream *input_stream;
34 MYFLT coefs[12];
35 // sample memories
36 MYFLT x1[12];
37 MYFLT y1[12];
38 MYFLT *buffer_streams;
39 } HilbertMain;
40
41 /* 6th order allpass poles */
42 static const MYFLT poles[12] = {.3609, 2.7412, 11.1573, 44.7581, 179.6242, 798.4578,
43 1.2524, 5.5671, 22.3423, 89.6271, 364.7914, 2770.1114
44 };
45
46 static void
47 HilbertMain_compute_variables(HilbertMain *self)
48 {
49 int i;
50 MYFLT polefreq[12];
51 MYFLT rc[12];
52 MYFLT alpha[12];
53
54 for (i = 0; i < 12; i++)
55 {
56 polefreq[i] = poles[i] * 15.0;
57 rc[i] = 1.0 / (TWOPI * polefreq[i]);
58 alpha[i] = 1.0 / rc[i];
59 self->coefs[i] = - (1.0 - (alpha[i] / (2.0 * self->sr))) / (1.0 + (alpha[i] / (2.0 * self->sr)));
60 }
61 }
62
63 static void
64 HilbertMain_filters(HilbertMain *self)
65 {
66 MYFLT xn1, xn2, yn1, yn2;
67 int j, i;
68 MYFLT *in = Stream_getData((Stream *)self->input_stream);
69
70 for (i = 0; i < self->bufsize; i++)
71 {
72 xn1 = in[i];
73
74 for (j = 0; j < 6; j++)
75 {
76 yn1 = self->coefs[j] * (xn1 - self->y1[j]) + self->x1[j];
77 self->x1[j] = xn1;
78 self->y1[j] = yn1;
79 xn1 = yn1;
80 }
81
82 xn2 = in[i];
83
84 for (j = 6; j < 12; j++)
85 {
86 yn2 = self->coefs[j] * (xn2 - self->y1[j]) + self->x1[j];
87 self->x1[j] = xn2;
88 self->y1[j] = yn2;
89 xn2 = yn2;
90 }
91
92 self->buffer_streams[i] = yn1;
93 self->buffer_streams[i + self->bufsize] = yn2;
94
95 }
96 }
97
98 MYFLT *
99 HilbertMain_getSamplesBuffer(HilbertMain *self)
100 {
101 return (MYFLT *)self->buffer_streams;
102 }
103
104 static void
105 HilbertMain_setProcMode(HilbertMain *self)
106 {
107 self->proc_func_ptr = HilbertMain_filters;
108 }
109
110 static void
111 HilbertMain_compute_next_data_frame(HilbertMain *self)
112 {
113 (*self->proc_func_ptr)(self);
114 }
115
116 static int
117 HilbertMain_traverse(HilbertMain *self, visitproc visit, void *arg)
118 {
119 pyo_VISIT
120 Py_VISIT(self->input);
121 return 0;
122 }
123
124 static int
125 HilbertMain_clear(HilbertMain *self)
126 {
127 pyo_CLEAR
128 Py_CLEAR(self->input);
129 return 0;
130 }
131
132 static void
133 HilbertMain_dealloc(HilbertMain* self)
134 {
135 pyo_DEALLOC
136 PyMem_RawFree(self->buffer_streams);
137 HilbertMain_clear(self);
138 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
139 Py_TYPE(self)->tp_free((PyObject*)self);
140 }
141
142 static PyObject *
143 HilbertMain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
144 {
145 int i;
146 PyObject *inputtmp, *input_streamtmp;
147 HilbertMain *self;
148 self = (HilbertMain *)type->tp_alloc(type, 0);
149
150 INIT_OBJECT_COMMON
151 Stream_setFunctionPtr(self->stream, HilbertMain_compute_next_data_frame);
152 self->mode_func_ptr = HilbertMain_setProcMode;
153
154 for (i = 0; i < 12; i++)
155 {
156 self->x1[i] = 0.0;
157 self->y1[i] = 0.0;
158 }
159
160 static char *kwlist[] = {"input", NULL};
161
162 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &inputtmp))
163 Py_RETURN_NONE;
164
165 INIT_INPUT_STREAM
166
167 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
168
169 self->buffer_streams = (MYFLT *)PyMem_RawRealloc(self->buffer_streams, 2 * self->bufsize * sizeof(MYFLT));
170
171 HilbertMain_compute_variables((HilbertMain *)self);
172
173 (*self->mode_func_ptr)(self);
174
175 return (PyObject *)self;
176 }
177
178 static PyObject * HilbertMain_getServer(HilbertMain* self) { GET_SERVER };
179 static PyObject * HilbertMain_getStream(HilbertMain* self) { GET_STREAM };
180
181 static PyObject * HilbertMain_play(HilbertMain *self, PyObject *args, PyObject *kwds) { PLAY };
182 static PyObject * HilbertMain_stop(HilbertMain *self, PyObject *args, PyObject *kwds) { STOP };
183
184 static PyMemberDef HilbertMain_members[] =
185 {
186 {"server", T_OBJECT_EX, offsetof(HilbertMain, server), 0, "Pyo server."},
187 {"stream", T_OBJECT_EX, offsetof(HilbertMain, stream), 0, "Stream object."},
188 {"input", T_OBJECT_EX, offsetof(HilbertMain, input), 0, "Input sound object."},
189 {NULL} /* Sentinel */
190 };
191
192 static PyMethodDef HilbertMain_methods[] =
193 {
194 {"getServer", (PyCFunction)HilbertMain_getServer, METH_NOARGS, "Returns server object."},
195 {"_getStream", (PyCFunction)HilbertMain_getStream, METH_NOARGS, "Returns stream object."},
196 {"play", (PyCFunction)HilbertMain_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
197 {"stop", (PyCFunction)HilbertMain_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
198 {NULL} /* Sentinel */
199 };
200
201 PyTypeObject HilbertMainType =
202 {
203 PyVarObject_HEAD_INIT(NULL, 0)
204 "_pyo.HilbertMain_base", /*tp_name*/
205 sizeof(HilbertMain), /*tp_basicsize*/
206 0, /*tp_itemsize*/
207 (destructor)HilbertMain_dealloc, /*tp_dealloc*/
208 0, /*tp_print*/
209 0, /*tp_getattr*/
210 0, /*tp_setattr*/
211 0, /*tp_as_async (tp_compare in Python 2)*/
212 0, /*tp_repr*/
213 0, /*tp_as_number*/
214 0, /*tp_as_sequence*/
215 0, /*tp_as_mapping*/
216 0, /*tp_hash */
217 0, /*tp_call*/
218 0, /*tp_str*/
219 0, /*tp_getattro*/
220 0, /*tp_setattro*/
221 0, /*tp_as_buffer*/
222 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
223 "HilbertMain objects. Hilbert transform. Created real and imaginary parts from an audio stream", /* tp_doc */
224 (traverseproc)HilbertMain_traverse, /* tp_traverse */
225 (inquiry)HilbertMain_clear, /* tp_clear */
226 0, /* tp_richcompare */
227 0, /* tp_weaklistoffset */
228 0, /* tp_iter */
229 0, /* tp_iternext */
230 HilbertMain_methods, /* tp_methods */
231 HilbertMain_members, /* tp_members */
232 0, /* tp_getset */
233 0, /* tp_base */
234 0, /* tp_dict */
235 0, /* tp_descr_get */
236 0, /* tp_descr_set */
237 0, /* tp_dictoffset */
238 0, /* tp_init */
239 0, /* tp_alloc */
240 HilbertMain_new, /* tp_new */
241 };
242
243 /************************************************************************************************/
244 /* Hilbert streamer object */
245 /************************************************************************************************/
246 typedef struct
247 {
248 pyo_audio_HEAD
249 HilbertMain *mainSplitter;
250 int modebuffer[2];
251 int chnl; // 0 = real, 1 = imag
252 } Hilbert;
253
254 static void Hilbert_postprocessing_ii(Hilbert *self) { POST_PROCESSING_II };
255 static void Hilbert_postprocessing_ai(Hilbert *self) { POST_PROCESSING_AI };
256 static void Hilbert_postprocessing_ia(Hilbert *self) { POST_PROCESSING_IA };
257 static void Hilbert_postprocessing_aa(Hilbert *self) { POST_PROCESSING_AA };
258 static void Hilbert_postprocessing_ireva(Hilbert *self) { POST_PROCESSING_IREVA };
259 static void Hilbert_postprocessing_areva(Hilbert *self) { POST_PROCESSING_AREVA };
260 static void Hilbert_postprocessing_revai(Hilbert *self) { POST_PROCESSING_REVAI };
261 static void Hilbert_postprocessing_revaa(Hilbert *self) { POST_PROCESSING_REVAA };
262 static void Hilbert_postprocessing_revareva(Hilbert *self) { POST_PROCESSING_REVAREVA };
263
264 static void
265 Hilbert_setProcMode(Hilbert *self)
266 {
267 int muladdmode;
268 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
269
270 switch (muladdmode)
271 {
272 case 0:
273 self->muladd_func_ptr = Hilbert_postprocessing_ii;
274 break;
275
276 case 1:
277 self->muladd_func_ptr = Hilbert_postprocessing_ai;
278 break;
279
280 case 2:
281 self->muladd_func_ptr = Hilbert_postprocessing_revai;
282 break;
283
284 case 10:
285 self->muladd_func_ptr = Hilbert_postprocessing_ia;
286 break;
287
288 case 11:
289 self->muladd_func_ptr = Hilbert_postprocessing_aa;
290 break;
291
292 case 12:
293 self->muladd_func_ptr = Hilbert_postprocessing_revaa;
294 break;
295
296 case 20:
297 self->muladd_func_ptr = Hilbert_postprocessing_ireva;
298 break;
299
300 case 21:
301 self->muladd_func_ptr = Hilbert_postprocessing_areva;
302 break;
303
304 case 22:
305 self->muladd_func_ptr = Hilbert_postprocessing_revareva;
306 break;
307 }
308 }
309
310 static void
311 Hilbert_compute_next_data_frame(Hilbert *self)
312 {
313 int i;
314 MYFLT *tmp;
315 int offset = self->chnl * self->bufsize;
316 tmp = HilbertMain_getSamplesBuffer((HilbertMain *)self->mainSplitter);
317
318 for (i = 0; i < self->bufsize; i++)
319 {
320 self->data[i] = tmp[i + offset];
321 }
322
323 (*self->muladd_func_ptr)(self);
324 }
325
326 static int
327 Hilbert_traverse(Hilbert *self, visitproc visit, void *arg)
328 {
329 pyo_VISIT
330 Py_VISIT(self->mainSplitter);
331 return 0;
332 }
333
334 static int
335 Hilbert_clear(Hilbert *self)
336 {
337 pyo_CLEAR
338 Py_CLEAR(self->mainSplitter);
339 return 0;
340 }
341
342 static void
343 Hilbert_dealloc(Hilbert* self)
344 {
345 pyo_DEALLOC
346 Hilbert_clear(self);
347 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
348 Py_TYPE(self)->tp_free((PyObject*)self);
349 }
350
351 static PyObject *
352 Hilbert_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
353 {
354 int i;
355 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
356 Hilbert *self;
357 self = (Hilbert *)type->tp_alloc(type, 0);
358
359 self->modebuffer[0] = 0;
360 self->modebuffer[1] = 0;
361
362 INIT_OBJECT_COMMON
363 Stream_setFunctionPtr(self->stream, Hilbert_compute_next_data_frame);
364 self->mode_func_ptr = Hilbert_setProcMode;
365
366 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
367
368 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
369 Py_RETURN_NONE;
370
371 self->mainSplitter = (HilbertMain *)maintmp;
372 Py_INCREF(self->mainSplitter);
373
374 if (multmp)
375 {
376 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
377 }
378
379 if (addtmp)
380 {
381 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
382 }
383
384 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
385
386 (*self->mode_func_ptr)(self);
387
388 return (PyObject *)self;
389 }
390
391 static PyObject * Hilbert_getServer(Hilbert* self) { GET_SERVER };
392 static PyObject * Hilbert_getStream(Hilbert* self) { GET_STREAM };
393 static PyObject * Hilbert_setMul(Hilbert *self, PyObject *arg) { SET_MUL };
394 static PyObject * Hilbert_setAdd(Hilbert *self, PyObject *arg) { SET_ADD };
395 static PyObject * Hilbert_setSub(Hilbert *self, PyObject *arg) { SET_SUB };
396 static PyObject * Hilbert_setDiv(Hilbert *self, PyObject *arg) { SET_DIV };
397
398 static PyObject * Hilbert_play(Hilbert *self, PyObject *args, PyObject *kwds) { PLAY };
399 static PyObject * Hilbert_out(Hilbert *self, PyObject *args, PyObject *kwds) { OUT };
400 static PyObject * Hilbert_stop(Hilbert *self, PyObject *args, PyObject *kwds) { STOP };
401
402 static PyObject * Hilbert_multiply(Hilbert *self, PyObject *arg) { MULTIPLY };
403 static PyObject * Hilbert_inplace_multiply(Hilbert *self, PyObject *arg) { INPLACE_MULTIPLY };
404 static PyObject * Hilbert_add(Hilbert *self, PyObject *arg) { ADD };
405 static PyObject * Hilbert_inplace_add(Hilbert *self, PyObject *arg) { INPLACE_ADD };
406 static PyObject * Hilbert_sub(Hilbert *self, PyObject *arg) { SUB };
407 static PyObject * Hilbert_inplace_sub(Hilbert *self, PyObject *arg) { INPLACE_SUB };
408 static PyObject * Hilbert_div(Hilbert *self, PyObject *arg) { DIV };
409 static PyObject * Hilbert_inplace_div(Hilbert *self, PyObject *arg) { INPLACE_DIV };
410
411 static PyMemberDef Hilbert_members[] =
412 {
413 {"server", T_OBJECT_EX, offsetof(Hilbert, server), 0, "Pyo server."},
414 {"stream", T_OBJECT_EX, offsetof(Hilbert, stream), 0, "Stream object."},
415 {"mul", T_OBJECT_EX, offsetof(Hilbert, mul), 0, "Mul factor."},
416 {"add", T_OBJECT_EX, offsetof(Hilbert, add), 0, "Add factor."},
417 {NULL} /* Sentinel */
418 };
419
420 static PyMethodDef Hilbert_methods[] =
421 {
422 {"getServer", (PyCFunction)Hilbert_getServer, METH_NOARGS, "Returns server object."},
423 {"_getStream", (PyCFunction)Hilbert_getStream, METH_NOARGS, "Returns stream object."},
424 {"play", (PyCFunction)Hilbert_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
425 {"out", (PyCFunction)Hilbert_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
426 {"stop", (PyCFunction)Hilbert_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
427 {"setMul", (PyCFunction)Hilbert_setMul, METH_O, "Sets Hilbert mul factor."},
428 {"setAdd", (PyCFunction)Hilbert_setAdd, METH_O, "Sets Hilbert add factor."},
429 {"setSub", (PyCFunction)Hilbert_setSub, METH_O, "Sets inverse add factor."},
430 {"setDiv", (PyCFunction)Hilbert_setDiv, METH_O, "Sets inverse mul factor."},
431 {NULL} /* Sentinel */
432 };
433
434 static PyNumberMethods Hilbert_as_number =
435 {
436 (binaryfunc)Hilbert_add, /*nb_add*/
437 (binaryfunc)Hilbert_sub, /*nb_subtract*/
438 (binaryfunc)Hilbert_multiply, /*nb_multiply*/
439 0, /*nb_remainder*/
440 0, /*nb_divmod*/
441 0, /*nb_power*/
442 0, /*nb_neg*/
443 0, /*nb_pos*/
444 0, /*(unaryfunc)array_abs,*/
445 0, /*nb_nonzero*/
446 0, /*nb_invert*/
447 0, /*nb_lshift*/
448 0, /*nb_rshift*/
449 0, /*nb_and*/
450 0, /*nb_xor*/
451 0, /*nb_or*/
452 0, /*nb_int*/
453 0, /*nb_long*/
454 0, /*nb_float*/
455 (binaryfunc)Hilbert_inplace_add, /*inplace_add*/
456 (binaryfunc)Hilbert_inplace_sub, /*inplace_subtract*/
457 (binaryfunc)Hilbert_inplace_multiply, /*inplace_multiply*/
458 0, /*inplace_remainder*/
459 0, /*inplace_power*/
460 0, /*inplace_lshift*/
461 0, /*inplace_rshift*/
462 0, /*inplace_and*/
463 0, /*inplace_xor*/
464 0, /*inplace_or*/
465 0, /*nb_floor_divide*/
466 (binaryfunc)Hilbert_div, /*nb_true_divide*/
467 0, /*nb_inplace_floor_divide*/
468 (binaryfunc)Hilbert_inplace_div, /*nb_inplace_true_divide*/
469 0, /* nb_index */
470 };
471
472 PyTypeObject HilbertType =
473 {
474 PyVarObject_HEAD_INIT(NULL, 0)
475 "_pyo.Hilbert_base", /*tp_name*/
476 sizeof(Hilbert), /*tp_basicsize*/
477 0, /*tp_itemsize*/
478 (destructor)Hilbert_dealloc, /*tp_dealloc*/
479 0, /*tp_print*/
480 0, /*tp_getattr*/
481 0, /*tp_setattr*/
482 0, /*tp_as_async (tp_compare in Python 2)*/
483 0, /*tp_repr*/
484 &Hilbert_as_number, /*tp_as_number*/
485 0, /*tp_as_sequence*/
486 0, /*tp_as_mapping*/
487 0, /*tp_hash */
488 0, /*tp_call*/
489 0, /*tp_str*/
490 0, /*tp_getattro*/
491 0, /*tp_setattro*/
492 0, /*tp_as_buffer*/
493 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
494 "Hilbert objects. Reads one band from a Hilbert transform.", /* tp_doc */
495 (traverseproc)Hilbert_traverse, /* tp_traverse */
496 (inquiry)Hilbert_clear, /* tp_clear */
497 0, /* tp_richcompare */
498 0, /* tp_weaklistoffset */
499 0, /* tp_iter */
500 0, /* tp_iternext */
501 Hilbert_methods, /* tp_methods */
502 Hilbert_members, /* tp_members */
503 0, /* tp_getset */
504 0, /* tp_base */
505 0, /* tp_dict */
506 0, /* tp_descr_get */
507 0, /* tp_descr_set */
508 0, /* tp_dictoffset */
509 0, /* tp_init */
510 0, /* tp_alloc */
511 Hilbert_new, /* tp_new */
512 };