Debian Codegraph @

src/objects/convolvemodule.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 "pyomodule.h"
24 #include "streammodule.h"
25 #include "servermodule.h"
26 #include "dummymodule.h"
27 #include "tablemodule.h"
28
29 static MYFLT BLACKMAN2[257] = {0.0, 0.00001355457612407795, 0.00005422714831165853, 0.00012204424012042525, 0.00021705003118953348, 0.00033930631782219667, 0.0004888924578373699, 0.00066590529972627988, 0.00087045909615995898, 0.0011026854019042381, 0.0013627329562085205, 0.0016507675497451635, 0.001966971876186191, 0.0023115453685137038, 0.0026847040201710415, 0.0030866801911709624, 0.0035177223992877149, 0.0039780950964686118, 0.004468078430611172, 0.0049879679928611226, 0.0055380745505964057, 0.0061187237662708727, 0.0067302559023018349, 0.0073730255121936678, 0.0080474011180991928, 0.0087537648750297542, 0.0094925122219332442, 0.010264051519867853, 0.011068803677508544, 0.011907201764231275, 0.012779690611027246, 0.013686726399509554, 0.014628776239280425, 0.015606317733936455, 0.016619838535996113, 0.017669835891040944, 0.018756816171369962, 0.019881294399473233, 0.021043793761637002, 0.022244845112000651, 0.023484986467390646, 0.024764762493264474, 0.026084723981101995, 0.027445427317589366, 0.028847433945943753, 0.030291309819735913, 0.031777624849569003, 0.033306952342980187, 0.034879868437934558, 0.036496951530286398, 0.038158781695585731, 0.039865940105613923, 0.041619008440034494, 0.043418568293550078, 0.045265200578957936, 0.047159484926502321, 0.04910199907992175, 0.051093318289594611, 0.053134014703186641, 0.055224656754207603, 0.05736580854888524, 0.059558029251766974, 0.061801872470459936, 0.064097885639923663, 0.066446609406726198, 0.068848577013680551, 0.071304313685273069, 0.073814336014300028, 0.076379151350125907, 0.078999257188976796, 0.081675140566682625, 0.08440727745428013, 0.08719613215688693, 0.090042156716257177, 0.092945790317425406, 0.095907458699845349, 0.09892757357342627, 0.10200653203986923, 0.10514471601969966, 0.10834249168539431, 0.11160020890099166, 0.11491820066857752, 0.11829678258202875, 0.12173625228839696, 0.12523688895730928, 0.12879895275875847, 0.13242268434965018, 0.1361083043694708, 0.13985601294543293, 0.14366598920745235, 0.14753839081330203, 0.15147335348428598, 0.15547099055176727, 0.15953139251487919, 0.16365462660974361, 0.16784073639051059, 0.17208974132253127, 0.17640163638796383, 0.18077639170410914, 0.18521395215476394, 0.18971423703487098, 0.19427713970874003, 0.19890252728210264, 0.2035902402882592, 0.20834009238856521, 0.21315187008749686, 0.218025332462529, 0.22296021090904578, 0.22795620890049961, 0.23301300176402318, 0.2381302364716896, 0.24330753144760825, 0.24854447639103289, 0.25384063211565033, 0.25919553040520765, 0.26460867388562637, 0.27007953591374234, 0.27560756048280166, 0.28119216214482828, 0.28683272594997611, 0.29252860740296116, 0.29827913243666476, 0.30408359740298374, 0.30994126908099884, 0.31585138470251517, 0.3218131519950253, 0.32782574924213004, 0.33388832536144369, 0.33999999999999991, 0.34615986364716356, 0.35236697776504228, 0.35862037493638421, 0.36491905902993321, 0.37126200538320747, 0.37764816100265119, 0.38407644478110459, 0.39054574773252188, 0.39705493324385926, 0.40360283734404451, 0.41018826898992783, 0.41681001036910403, 0.42346681721948765, 0.43015741916550887, 0.43688052007079137, 0.44363479840716119, 0.45041890763982673, 0.45723147662855934, 0.46407111004469437, 0.47093638880376354, 0.47782587051356035, 0.4847380899374274, 0.49167155947254987, 0.49862476964302743, 0.50559618960748731, 0.51258426768099419, 0.51958743187100298, 0.526604090427091, 0.53363263240419834, 0.54067142823909731, 0.5477188303398014, 0.55477317368762102, 0.5618327764515586, 0.56889594061473336, 0.57596095261251634, 0.58302608398204925, 0.59008959202281352, 0.5971497204679086, 0.60420470016569239, 0.61125274977143074, 0.61829207644859363, 0.62532087657943414, 0.63233733648447599, 0.63933963315053088, 0.64632593496686574, 0.65329440246912585, 0.66024318909062385, 0.66717044192059383, 0.67407430246900757, 0.68095290743754511, 0.68780438949630818, 0.69462687806585954, 0.70141850010417084, 0.70817738089805216, 0.71490164485864349, 0.72158941632053231, 0.7282388203440715, 0.73484798352045921, 0.74141503477914861, 0.74793810619714429, 0.75441533380975301, 0.76084485842234006, 0.76722482642265344, 0.77355339059327366, 0.77982871092374229, 0.78604895542192688, 0.7922123009241796, 0.79831693390384428, 0.80436105127766677, 0.81034286120967125, 0.81626058391205358, 0.82211245244265874, 0.82789671349859684, 0.83361162820556423, 0.83925547290243352, 0.84482653992067935, 0.85032313835820861, 0.85574359484716933, 0.86108625431531149, 0.86634948074047979, 0.87153165789781828, 0.87663119009927604, 0.88164650292500113, 0.88657604394621592, 0.89141828343917606, 0.89617171508981341, 0.90083485668867092, 0.90540625081574555, 0.90988446551485458, 0.91426809495715211, 0.9185557600934271, 0.92274610929481327, 0.92683781898156326, 0.93082959423952683, 0.93472016942399416, 0.93850830875056723, 0.94219280687272511, 0.94577248944576608, 0.94924621367680617, 0.9526128688605292, 0.95587137690038915, 0.95902069281497004, 0.96205980522922363, 0.96498773685030803, 0.96780354492775944, 0.97050632169774165, 0.97309519481112294, 0.97556932774514038, 0.97792792019842123, 0.9801702084691396, 0.98229546581609617, 0.98430300280251803, 0.98619216762238726, 0.98796234640911229, 0.98961296352637218, 0.99114348184096723, 0.99255340297752515, 0.99384226755491845, 0.99500965540426034, 0.99605518576835683, 0.99697851748250432, 0.99777934913652766, 0.99845741921797138, 0.99901250623636195, 0.99944442882846996, 0.99975304584451585, 0.99993825641526857, 1.0};
30
31 /************/
32 /* Convolve */
33 /************/
34 typedef struct
35 {
36 pyo_audio_HEAD
37 PyObject *table;
38 PyObject *input;
39 Stream *input_stream;
40 int modebuffer[2]; // need at least 2 slots for mul & add
41 MYFLT *input_tmp;
42 int size;
43 int count;
44 } Convolve;
45
46 static void
47 Convolve_filters(Convolve *self)
48 {
49 int i, j, tmp_count;
50 MYFLT *in = Stream_getData((Stream *)self->input_stream);
51
52 MYFLT *impulse = TableStream_getData((TableStream *)self->table);
53
54 for (i = 0; i < self->bufsize; i++)
55 {
56 self->data[i] = 0.0;
57 tmp_count = self->count;
58
59 for (j = 0; j < self->size; j++)
60 {
61 if (tmp_count < 0)
62 tmp_count += self->size;
63
64 self->data[i] += self->input_tmp[tmp_count--] * impulse[j];
65 }
66
67 self->count++;
68
69 if (self->count == self->size)
70 self->count = 0;
71
72 self->input_tmp[self->count] = in[i];
73 }
74 }
75
76 static void Convolve_postprocessing_ii(Convolve *self) { POST_PROCESSING_II };
77 static void Convolve_postprocessing_ai(Convolve *self) { POST_PROCESSING_AI };
78 static void Convolve_postprocessing_ia(Convolve *self) { POST_PROCESSING_IA };
79 static void Convolve_postprocessing_aa(Convolve *self) { POST_PROCESSING_AA };
80 static void Convolve_postprocessing_ireva(Convolve *self) { POST_PROCESSING_IREVA };
81 static void Convolve_postprocessing_areva(Convolve *self) { POST_PROCESSING_AREVA };
82 static void Convolve_postprocessing_revai(Convolve *self) { POST_PROCESSING_REVAI };
83 static void Convolve_postprocessing_revaa(Convolve *self) { POST_PROCESSING_REVAA };
84 static void Convolve_postprocessing_revareva(Convolve *self) { POST_PROCESSING_REVAREVA };
85
86 static void
87 Convolve_setProcMode(Convolve *self)
88 {
89 int muladdmode;
90 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
91
92 self->proc_func_ptr = Convolve_filters;
93
94 switch (muladdmode)
95 {
96 case 0:
97 self->muladd_func_ptr = Convolve_postprocessing_ii;
98 break;
99
100 case 1:
101 self->muladd_func_ptr = Convolve_postprocessing_ai;
102 break;
103
104 case 2:
105 self->muladd_func_ptr = Convolve_postprocessing_revai;
106 break;
107
108 case 10:
109 self->muladd_func_ptr = Convolve_postprocessing_ia;
110 break;
111
112 case 11:
113 self->muladd_func_ptr = Convolve_postprocessing_aa;
114 break;
115
116 case 12:
117 self->muladd_func_ptr = Convolve_postprocessing_revaa;
118 break;
119
120 case 20:
121 self->muladd_func_ptr = Convolve_postprocessing_ireva;
122 break;
123
124 case 21:
125 self->muladd_func_ptr = Convolve_postprocessing_areva;
126 break;
127
128 case 22:
129 self->muladd_func_ptr = Convolve_postprocessing_revareva;
130 break;
131 }
132 }
133
134 static void
135 Convolve_compute_next_data_frame(Convolve *self)
136 {
137 (*self->proc_func_ptr)(self);
138 (*self->muladd_func_ptr)(self);
139 }
140
141 static int
142 Convolve_traverse(Convolve *self, visitproc visit, void *arg)
143 {
144 pyo_VISIT
145 Py_VISIT(self->input);
146 return 0;
147 }
148
149 static int
150 Convolve_clear(Convolve *self)
151 {
152 pyo_CLEAR
153 Py_CLEAR(self->input);
154 return 0;
155 }
156
157 static void
158 Convolve_dealloc(Convolve* self)
159 {
160 pyo_DEALLOC
161 PyMem_RawFree(self->input_tmp);
162 Convolve_clear(self);
163 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
164 Py_TYPE(self)->tp_free((PyObject*)self);
165 }
166
167 static PyObject *
168 Convolve_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
169 {
170 int i;
171 PyObject *inputtmp, *input_streamtmp, *tabletmp, *multmp = NULL, *addtmp = NULL;
172 Convolve *self;
173 self = (Convolve *)type->tp_alloc(type, 0);
174
175 self->count = 0;
176 self->modebuffer[0] = 0;
177 self->modebuffer[1] = 0;
178
179 INIT_OBJECT_COMMON
180 Stream_setFunctionPtr(self->stream, Convolve_compute_next_data_frame);
181 self->mode_func_ptr = Convolve_setProcMode;
182
183 static char *kwlist[] = {"input", "table", "size", "mul", "add", NULL};
184
185 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOi|OO", kwlist, &inputtmp, &tabletmp, &self->size, &multmp, &addtmp))
186 Py_RETURN_NONE;
187
188 INIT_INPUT_STREAM
189
190 if ( PyObject_HasAttrString((PyObject *)tabletmp, "getTableStream") == 0 )
191 {
192 PyErr_SetString(PyExc_TypeError, "\"table\" argument of Convolve must be a PyoTableObject.\n");
193 Py_RETURN_NONE;
194 }
195
196 self->table = PyObject_CallMethod((PyObject *)tabletmp, "getTableStream", "");
197
198 if (multmp)
199 {
200 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
201 }
202
203 if (addtmp)
204 {
205 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
206 }
207
208 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
209
210 (*self->mode_func_ptr)(self);
211
212 self->input_tmp = (MYFLT *)PyMem_RawRealloc(self->input_tmp, self->size * sizeof(MYFLT));
213
214 for (i = 0; i < self->size; i++)
215 {
216 self->input_tmp[i] = 0.0;
217 }
218
219 return (PyObject *)self;
220 }
221
222 static PyObject * Convolve_getServer(Convolve* self) { GET_SERVER };
223 static PyObject * Convolve_getStream(Convolve* self) { GET_STREAM };
224 static PyObject * Convolve_setMul(Convolve *self, PyObject *arg) { SET_MUL };
225 static PyObject * Convolve_setAdd(Convolve *self, PyObject *arg) { SET_ADD };
226 static PyObject * Convolve_setSub(Convolve *self, PyObject *arg) { SET_SUB };
227 static PyObject * Convolve_setDiv(Convolve *self, PyObject *arg) { SET_DIV };
228
229 static PyObject * Convolve_play(Convolve *self, PyObject *args, PyObject *kwds) { PLAY };
230 static PyObject * Convolve_out(Convolve *self, PyObject *args, PyObject *kwds) { OUT };
231 static PyObject * Convolve_stop(Convolve *self, PyObject *args, PyObject *kwds) { STOP };
232
233 static PyObject * Convolve_multiply(Convolve *self, PyObject *arg) { MULTIPLY };
234 static PyObject * Convolve_inplace_multiply(Convolve *self, PyObject *arg) { INPLACE_MULTIPLY };
235 static PyObject * Convolve_add(Convolve *self, PyObject *arg) { ADD };
236 static PyObject * Convolve_inplace_add(Convolve *self, PyObject *arg) { INPLACE_ADD };
237 static PyObject * Convolve_sub(Convolve *self, PyObject *arg) { SUB };
238 static PyObject * Convolve_inplace_sub(Convolve *self, PyObject *arg) { INPLACE_SUB };
239 static PyObject * Convolve_div(Convolve *self, PyObject *arg) { DIV };
240 static PyObject * Convolve_inplace_div(Convolve *self, PyObject *arg) { INPLACE_DIV };
241
242 static PyObject *
243 Convolve_getTable(Convolve* self)
244 {
245 Py_INCREF(self->table);
246 return self->table;
247 };
248
249 static PyObject *
250 Convolve_setTable(Convolve *self, PyObject *arg)
251 {
252 ASSERT_ARG_NOT_NULL
253
254 Py_DECREF(self->table);
255 self->table = PyObject_CallMethod((PyObject *)arg, "getTableStream", "");
256
257 Py_RETURN_NONE;
258 }
259
260 static PyMemberDef Convolve_members[] =
261 {
262 {"server", T_OBJECT_EX, offsetof(Convolve, server), 0, "Pyo server."},
263 {"stream", T_OBJECT_EX, offsetof(Convolve, stream), 0, "Stream object."},
264 {"input", T_OBJECT_EX, offsetof(Convolve, input), 0, "Input sound object."},
265 {"mul", T_OBJECT_EX, offsetof(Convolve, mul), 0, "Mul factor."},
266 {"add", T_OBJECT_EX, offsetof(Convolve, add), 0, "Add factor."},
267 {NULL} /* Sentinel */
268 };
269
270 static PyMethodDef Convolve_methods[] =
271 {
272 {"getTable", (PyCFunction)Convolve_getTable, METH_NOARGS, "Returns impulse response table object."},
273 {"getServer", (PyCFunction)Convolve_getServer, METH_NOARGS, "Returns server object."},
274 {"_getStream", (PyCFunction)Convolve_getStream, METH_NOARGS, "Returns stream object."},
275 {"play", (PyCFunction)Convolve_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
276 {"out", (PyCFunction)Convolve_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
277 {"stop", (PyCFunction)Convolve_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
278 {"setTable", (PyCFunction)Convolve_setTable, METH_O, "Sets inpulse response table."},
279 {"setMul", (PyCFunction)Convolve_setMul, METH_O, "Sets mul factor."},
280 {"setAdd", (PyCFunction)Convolve_setAdd, METH_O, "Sets add factor."},
281 {"setSub", (PyCFunction)Convolve_setSub, METH_O, "Sets inverse add factor."},
282 {"setDiv", (PyCFunction)Convolve_setDiv, METH_O, "Sets inverse mul factor."},
283 {NULL} /* Sentinel */
284 };
285
286 static PyNumberMethods Convolve_as_number =
287 {
288 (binaryfunc)Convolve_add, /*nb_add*/
289 (binaryfunc)Convolve_sub, /*nb_subtract*/
290 (binaryfunc)Convolve_multiply, /*nb_multiply*/
291 0, /*nb_remainder*/
292 0, /*nb_divmod*/
293 0, /*nb_power*/
294 0, /*nb_neg*/
295 0, /*nb_pos*/
296 0, /*(unaryfunc)array_abs,*/
297 0, /*nb_nonzero*/
298 0, /*nb_invert*/
299 0, /*nb_lshift*/
300 0, /*nb_rshift*/
301 0, /*nb_and*/
302 0, /*nb_xor*/
303 0, /*nb_or*/
304 0, /*nb_int*/
305 0, /*nb_long*/
306 0, /*nb_float*/
307 (binaryfunc)Convolve_inplace_add, /*inplace_add*/
308 (binaryfunc)Convolve_inplace_sub, /*inplace_subtract*/
309 (binaryfunc)Convolve_inplace_multiply, /*inplace_multiply*/
310 0, /*inplace_remainder*/
311 0, /*inplace_power*/
312 0, /*inplace_lshift*/
313 0, /*inplace_rshift*/
314 0, /*inplace_and*/
315 0, /*inplace_xor*/
316 0, /*inplace_or*/
317 0, /*nb_floor_divide*/
318 (binaryfunc)Convolve_div, /*nb_true_divide*/
319 0, /*nb_inplace_floor_divide*/
320 (binaryfunc)Convolve_inplace_div, /*nb_inplace_true_divide*/
321 0, /* nb_index */
322 };
323
324 PyTypeObject ConvolveType =
325 {
326 PyVarObject_HEAD_INIT(NULL, 0)
327 "_pyo.Convolve_base", /*tp_name*/
328 sizeof(Convolve), /*tp_basicsize*/
329 0, /*tp_itemsize*/
330 (destructor)Convolve_dealloc, /*tp_dealloc*/
331 0, /*tp_print*/
332 0, /*tp_getattr*/
333 0, /*tp_setattr*/
334 0, /*tp_as_async (tp_compare in Python 2)*/
335 0, /*tp_repr*/
336 &Convolve_as_number, /*tp_as_number*/
337 0, /*tp_as_sequence*/
338 0, /*tp_as_mapping*/
339 0, /*tp_hash */
340 0, /*tp_call*/
341 0, /*tp_str*/
342 0, /*tp_getattro*/
343 0, /*tp_setattro*/
344 0, /*tp_as_buffer*/
345 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
346 "Convolve objects. Implements a circular convolution.", /* tp_doc */
347 (traverseproc)Convolve_traverse, /* tp_traverse */
348 (inquiry)Convolve_clear, /* tp_clear */
349 0, /* tp_richcompare */
350 0, /* tp_weaklistoffset */
351 0, /* tp_iter */
352 0, /* tp_iternext */
353 Convolve_methods, /* tp_methods */
354 Convolve_members, /* tp_members */
355 0, /* tp_getset */
356 0, /* tp_base */
357 0, /* tp_dict */
358 0, /* tp_descr_get */
359 0, /* tp_descr_set */
360 0, /* tp_dictoffset */
361 0, /* tp_init */
362 0, /* tp_alloc */
363 Convolve_new, /* tp_new */
364 };
365
366 /************/
367 /* IRWinSinc */
368 /************/
369 typedef struct
370 {
371 pyo_audio_HEAD
372 PyObject *input;
373 Stream *input_stream;
374 PyObject *freq;
375 Stream *freq_stream;
376 PyObject *bandwidth;
377 Stream *bandwidth_stream;
378 int modebuffer[4]; // need at least 2 slots for mul & add
379 MYFLT *impulse;
380 MYFLT *impulse_tmp;
381 MYFLT *input_tmp;
382 int count;
383 int filtertype;
384 int order;
385 int size;
386 int changed;
387 MYFLT last_freq;
388 MYFLT last_bandwidth;
389 } IRWinSinc;
390
391 static void
392 IRWinSinc_alloc_memory(IRWinSinc *self)
393 {
394 int i;
395
396 if ((self->order % 2) != 0)
397 self->order += 1;
398
399 self->size = self->order + 1;
400
401 self->input_tmp = (MYFLT *)PyMem_RawRealloc(self->input_tmp, self->size * sizeof(MYFLT));
402 self->impulse = (MYFLT *)PyMem_RawRealloc(self->impulse, self->size * sizeof(MYFLT));
403 self->impulse_tmp = (MYFLT *)PyMem_RawRealloc(self->impulse_tmp, self->size * sizeof(MYFLT));
404
405 for (i = 0; i < self->size; i++)
406 {
407 self->input_tmp[i] = self->impulse[i] = self->impulse_tmp[i] = 0.0;
408 }
409 }
410
411 static void
412 IRWinSinc_create_impulse(IRWinSinc *self, MYFLT freq, MYFLT bandwidth)
413 {
414 int i, half, ipart;
415 MYFLT val, fpart, sum, invSum, env, envPointer, envPointerScaling, sincScaling, w;
416
417 half = self->order / 2;
418 sum = 0.0;
419 envPointerScaling = 1.0 / self->size * 512.0;
420 sincScaling = self->order / 2.0;
421
422 if (freq < 1)
423 freq = 1.0;
424 else if (freq > (self->sr * 0.5))
425 freq = self->sr * 0.5;
426
427 if (bandwidth < 1)
428 bandwidth = 1.0;
429 else if (bandwidth > (self->sr * 0.5))
430 bandwidth = self->sr * 0.5;
431
432 if (self->filtertype <= 1)
433 w = TWOPI * freq / self->sr;
434 else
435 w = TWOPI * (freq - bandwidth / 2.0) / self->sr;
436
437 /* LOWPASS */
438 for (i = 0; i < half; i++)
439 {
440 envPointer = i * envPointerScaling;
441 ipart = (int)envPointer;
442 fpart = envPointer - ipart;
443 env = BLACKMAN2[ipart] * (1.0 - fpart) + BLACKMAN2[ipart + 1] * fpart;
444 val = MYSIN(w * (i - sincScaling)) / (i - sincScaling) * env;
445 sum += val;
446 self->impulse[i] = val;
447 }
448
449 sum *= 2.0;
450 sum += w;
451 invSum = 1.0 / sum;
452 self->impulse[half] = w * invSum;
453
454 for (i = 0; i < half; i++)
455 {
456 self->impulse[i] *= invSum;
457 }
458
459 for (i = half + 1; i < self->size; i++)
460 {
461 self->impulse[i] = self->impulse[self->order - i];
462 }
463
464 /* HIGHPASS */
465 if (self->filtertype == 1)
466 {
467 for (i = 0; i < self->size; i++)
468 {
469 self->impulse[i] = -self->impulse[i];
470 }
471
472 self->impulse[half] = self->impulse[half] + 1.0;
473 }
474
475 /* BANDREJECT */
476 if (self->filtertype >= 2)
477 {
478 sum = 0.0;
479 w = TWOPI * (freq + bandwidth / 2.0) / self->sr;
480
481 for (i = 0; i < half; i++)
482 {
483 envPointer = i * envPointerScaling;
484 ipart = (int)envPointer;
485 fpart = envPointer - ipart;
486 env = BLACKMAN2[ipart] * (1.0 - fpart) + BLACKMAN2[ipart + 1] * fpart;
487 val = MYSIN(w * (i - sincScaling)) / (i - sincScaling) * env;
488 sum += val;
489 self->impulse_tmp[i] = val;
490 }
491
492 sum *= 2.0;
493 sum += w;
494 invSum = 1.0 / sum;
495 self->impulse_tmp[half] = w * invSum;
496
497 for (i = 0; i < half; i++)
498 {
499 self->impulse_tmp[i] *= invSum;
500 }
501
502 for (i = half + 1; i < self->size; i++)
503 {
504 self->impulse_tmp[i] = self->impulse_tmp[self->order - i];
505 }
506
507 for (i = 0; i < self->size; i++)
508 {
509 self->impulse_tmp[i] = -self->impulse_tmp[i];
510 }
511
512 self->impulse_tmp[half] = self->impulse_tmp[half] + 1.0;
513
514 for (i = 0; i < self->size; i++)
515 {
516 self->impulse[i] = self->impulse[i] + self->impulse_tmp[i];
517 }
518
519 /* BANDPASS */
520 if (self->filtertype == 3)
521 {
522 for (i = 0; i < self->size; i++)
523 {
524 self->impulse[i] = -self->impulse[i];
525 }
526
527 self->impulse[half] = self->impulse[half] + 1.0;
528 }
529 }
530 }
531
532 static void
533 IRWinSinc_filters(IRWinSinc *self)
534 {
535 int i, j, tmp_count;
536 MYFLT freq, bw;
537
538 MYFLT *in = Stream_getData((Stream *)self->input_stream);
539
540 if (self->modebuffer[2] == 0)
541 freq = PyFloat_AS_DOUBLE(self->freq);
542 else
543 {
544 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
545 freq = fr[0];
546 }
547
548 if (self->modebuffer[3] == 0)
549 bw = PyFloat_AS_DOUBLE(self->bandwidth);
550 else
551 {
552 MYFLT *band = Stream_getData((Stream *)self->bandwidth_stream);
553 bw = band[0];
554 }
555
556 if (freq != self->last_freq || bw != self->last_bandwidth || self->changed == 1)
557 {
558 IRWinSinc_create_impulse(self, freq, bw);
559 self->last_freq = freq;
560 self->last_bandwidth = bw;
561 self->changed = 0;
562 }
563
564 for (i = 0; i < self->bufsize; i++)
565 {
566 self->data[i] = 0.0;
567 tmp_count = self->count;
568
569 for (j = 0; j < self->size; j++)
570 {
571 if (tmp_count < 0)
572 tmp_count += self->size;
573
574 self->data[i] += self->input_tmp[tmp_count--] * self->impulse[j];
575 }
576
577 self->count++;
578
579 if (self->count == self->size)
580 self->count = 0;
581
582 self->input_tmp[self->count] = in[i];
583 }
584 }
585
586 static void IRWinSinc_postprocessing_ii(IRWinSinc *self) { POST_PROCESSING_II };
587 static void IRWinSinc_postprocessing_ai(IRWinSinc *self) { POST_PROCESSING_AI };
588 static void IRWinSinc_postprocessing_ia(IRWinSinc *self) { POST_PROCESSING_IA };
589 static void IRWinSinc_postprocessing_aa(IRWinSinc *self) { POST_PROCESSING_AA };
590 static void IRWinSinc_postprocessing_ireva(IRWinSinc *self) { POST_PROCESSING_IREVA };
591 static void IRWinSinc_postprocessing_areva(IRWinSinc *self) { POST_PROCESSING_AREVA };
592 static void IRWinSinc_postprocessing_revai(IRWinSinc *self) { POST_PROCESSING_REVAI };
593 static void IRWinSinc_postprocessing_revaa(IRWinSinc *self) { POST_PROCESSING_REVAA };
594 static void IRWinSinc_postprocessing_revareva(IRWinSinc *self) { POST_PROCESSING_REVAREVA };
595
596 static void
597 IRWinSinc_setProcMode(IRWinSinc *self)
598 {
599 int muladdmode;
600 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
601
602 self->proc_func_ptr = IRWinSinc_filters;
603
604 switch (muladdmode)
605 {
606 case 0:
607 self->muladd_func_ptr = IRWinSinc_postprocessing_ii;
608 break;
609
610 case 1:
611 self->muladd_func_ptr = IRWinSinc_postprocessing_ai;
612 break;
613
614 case 2:
615 self->muladd_func_ptr = IRWinSinc_postprocessing_revai;
616 break;
617
618 case 10:
619 self->muladd_func_ptr = IRWinSinc_postprocessing_ia;
620 break;
621
622 case 11:
623 self->muladd_func_ptr = IRWinSinc_postprocessing_aa;
624 break;
625
626 case 12:
627 self->muladd_func_ptr = IRWinSinc_postprocessing_revaa;
628 break;
629
630 case 20:
631 self->muladd_func_ptr = IRWinSinc_postprocessing_ireva;
632 break;
633
634 case 21:
635 self->muladd_func_ptr = IRWinSinc_postprocessing_areva;
636 break;
637
638 case 22:
639 self->muladd_func_ptr = IRWinSinc_postprocessing_revareva;
640 break;
641 }
642 }
643
644 static void
645 IRWinSinc_compute_next_data_frame(IRWinSinc *self)
646 {
647 (*self->proc_func_ptr)(self);
648 (*self->muladd_func_ptr)(self);
649 }
650
651 static int
652 IRWinSinc_traverse(IRWinSinc *self, visitproc visit, void *arg)
653 {
654 pyo_VISIT
655 Py_VISIT(self->input);
656 Py_VISIT(self->freq);
657 Py_VISIT(self->bandwidth);
658 return 0;
659 }
660
661 static int
662 IRWinSinc_clear(IRWinSinc *self)
663 {
664 pyo_CLEAR
665 Py_CLEAR(self->input);
666 Py_CLEAR(self->freq);
667 Py_CLEAR(self->bandwidth);
668 return 0;
669 }
670
671 static void
672 IRWinSinc_dealloc(IRWinSinc* self)
673 {
674 pyo_DEALLOC
675 PyMem_RawFree(self->input_tmp);
676 PyMem_RawFree(self->impulse);
677 PyMem_RawFree(self->impulse_tmp);
678 IRWinSinc_clear(self);
679 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
680 Py_TYPE(self)->tp_free((PyObject*)self);
681 }
682
683 static PyObject *
684 IRWinSinc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
685 {
686 int i;
687 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *bandwidthtmp = NULL, *multmp = NULL, *addtmp = NULL;
688 IRWinSinc *self;
689 self = (IRWinSinc *)type->tp_alloc(type, 0);
690
691 self->last_freq = -1.0;
692 self->last_bandwidth = -1.0;
693 self->freq = PyFloat_FromDouble(1000.0);
694 self->bandwidth = PyFloat_FromDouble(500.0);
695 self->filtertype = 0;
696 self->order = 256;
697 self->count = 0;
698 self->changed = 0;
699 self->modebuffer[0] = 0;
700 self->modebuffer[1] = 0;
701 self->modebuffer[2] = 0;
702 self->modebuffer[3] = 0;
703
704 INIT_OBJECT_COMMON
705 Stream_setFunctionPtr(self->stream, IRWinSinc_compute_next_data_frame);
706 self->mode_func_ptr = IRWinSinc_setProcMode;
707
708 static char *kwlist[] = {"input", "freq", "bw", "type", "order", "mul", "add", NULL};
709
710 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOiiOO", kwlist, &inputtmp, &freqtmp, &bandwidthtmp, &self->filtertype, &self->order, &multmp, &addtmp))
711 Py_RETURN_NONE;
712
713 INIT_INPUT_STREAM
714
715 if (freqtmp)
716 {
717 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
718 }
719
720 if (bandwidthtmp)
721 {
722 PyObject_CallMethod((PyObject *)self, "setBandwidth", "O", bandwidthtmp);
723 }
724
725 if (multmp)
726 {
727 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
728 }
729
730 if (addtmp)
731 {
732 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
733 }
734
735 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
736
737 IRWinSinc_alloc_memory(self);
738
739 (*self->mode_func_ptr)(self);
740
741 return (PyObject *)self;
742 }
743
744 static PyObject * IRWinSinc_getServer(IRWinSinc* self) { GET_SERVER };
745 static PyObject * IRWinSinc_getStream(IRWinSinc* self) { GET_STREAM };
746 static PyObject * IRWinSinc_setMul(IRWinSinc *self, PyObject *arg) { SET_MUL };
747 static PyObject * IRWinSinc_setAdd(IRWinSinc *self, PyObject *arg) { SET_ADD };
748 static PyObject * IRWinSinc_setSub(IRWinSinc *self, PyObject *arg) { SET_SUB };
749 static PyObject * IRWinSinc_setDiv(IRWinSinc *self, PyObject *arg) { SET_DIV };
750
751 static PyObject * IRWinSinc_play(IRWinSinc *self, PyObject *args, PyObject *kwds) { PLAY };
752 static PyObject * IRWinSinc_out(IRWinSinc *self, PyObject *args, PyObject *kwds) { OUT };
753 static PyObject * IRWinSinc_stop(IRWinSinc *self, PyObject *args, PyObject *kwds) { STOP };
754
755 static PyObject * IRWinSinc_multiply(IRWinSinc *self, PyObject *arg) { MULTIPLY };
756 static PyObject * IRWinSinc_inplace_multiply(IRWinSinc *self, PyObject *arg) { INPLACE_MULTIPLY };
757 static PyObject * IRWinSinc_add(IRWinSinc *self, PyObject *arg) { ADD };
758 static PyObject * IRWinSinc_inplace_add(IRWinSinc *self, PyObject *arg) { INPLACE_ADD };
759 static PyObject * IRWinSinc_sub(IRWinSinc *self, PyObject *arg) { SUB };
760 static PyObject * IRWinSinc_inplace_sub(IRWinSinc *self, PyObject *arg) { INPLACE_SUB };
761 static PyObject * IRWinSinc_div(IRWinSinc *self, PyObject *arg) { DIV };
762 static PyObject * IRWinSinc_inplace_div(IRWinSinc *self, PyObject *arg) { INPLACE_DIV };
763
764 static PyObject * IRWinSinc_setFreq(IRWinSinc *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
765 static PyObject * IRWinSinc_setBandwidth(IRWinSinc *self, PyObject *arg) { SET_PARAM(self->bandwidth, self->bandwidth_stream, 3); }
766
767 static PyObject *
768 IRWinSinc_setType(IRWinSinc *self, PyObject *arg)
769 {
770 ASSERT_ARG_NOT_NULL
771
772 if (PyLong_Check(arg))
773 {
774 self->filtertype = PyLong_AsLong(arg);
775 self->changed = 1;
776 }
777
778 Py_RETURN_NONE;
779 }
780
781 static PyMemberDef IRWinSinc_members[] =
782 {
783 {"server", T_OBJECT_EX, offsetof(IRWinSinc, server), 0, "Pyo server."},
784 {"stream", T_OBJECT_EX, offsetof(IRWinSinc, stream), 0, "Stream object."},
785 {"input", T_OBJECT_EX, offsetof(IRWinSinc, input), 0, "Input sound object."},
786 {"freq", T_OBJECT_EX, offsetof(IRWinSinc, freq), 0, "Cutoff or center frequency."},
787 {"bandwidth", T_OBJECT_EX, offsetof(IRWinSinc, bandwidth), 0, "Bandwidth, in Hz, for bandreject and bandpass filters."},
788 {"mul", T_OBJECT_EX, offsetof(IRWinSinc, mul), 0, "Mul factor."},
789 {"add", T_OBJECT_EX, offsetof(IRWinSinc, add), 0, "Add factor."},
790 {NULL} /* Sentinel */
791 };
792
793 static PyMethodDef IRWinSinc_methods[] =
794 {
795 {"getServer", (PyCFunction)IRWinSinc_getServer, METH_NOARGS, "Returns server object."},
796 {"_getStream", (PyCFunction)IRWinSinc_getStream, METH_NOARGS, "Returns stream object."},
797 {"play", (PyCFunction)IRWinSinc_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
798 {"out", (PyCFunction)IRWinSinc_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
799 {"stop", (PyCFunction)IRWinSinc_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
800 {"setFreq", (PyCFunction)IRWinSinc_setFreq, METH_O, "Sets center/cutoff frequency."},
801 {"setBandwidth", (PyCFunction)IRWinSinc_setBandwidth, METH_O, "Sets bandwidth in Hz."},
802 {"setType", (PyCFunction)IRWinSinc_setType, METH_O, "Sets filter type factor."},
803 {"setMul", (PyCFunction)IRWinSinc_setMul, METH_O, "Sets mul factor."},
804 {"setAdd", (PyCFunction)IRWinSinc_setAdd, METH_O, "Sets add factor."},
805 {"setSub", (PyCFunction)IRWinSinc_setSub, METH_O, "Sets inverse add factor."},
806 {"setDiv", (PyCFunction)IRWinSinc_setDiv, METH_O, "Sets inverse mul factor."},
807 {NULL} /* Sentinel */
808 };
809
810 static PyNumberMethods IRWinSinc_as_number =
811 {
812 (binaryfunc)IRWinSinc_add, /*nb_add*/
813 (binaryfunc)IRWinSinc_sub, /*nb_subtract*/
814 (binaryfunc)IRWinSinc_multiply, /*nb_multiply*/
815 0, /*nb_remainder*/
816 0, /*nb_divmod*/
817 0, /*nb_power*/
818 0, /*nb_neg*/
819 0, /*nb_pos*/
820 0, /*(unaryfunc)array_abs,*/
821 0, /*nb_nonzero*/
822 0, /*nb_invert*/
823 0, /*nb_lshift*/
824 0, /*nb_rshift*/
825 0, /*nb_and*/
826 0, /*nb_xor*/
827 0, /*nb_or*/
828 0, /*nb_int*/
829 0, /*nb_long*/
830 0, /*nb_float*/
831 (binaryfunc)IRWinSinc_inplace_add, /*inplace_add*/
832 (binaryfunc)IRWinSinc_inplace_sub, /*inplace_subtract*/
833 (binaryfunc)IRWinSinc_inplace_multiply, /*inplace_multiply*/
834 0, /*inplace_remainder*/
835 0, /*inplace_power*/
836 0, /*inplace_lshift*/
837 0, /*inplace_rshift*/
838 0, /*inplace_and*/
839 0, /*inplace_xor*/
840 0, /*inplace_or*/
841 0, /*nb_floor_divide*/
842 (binaryfunc)IRWinSinc_div, /*nb_true_divide*/
843 0, /*nb_inplace_floor_divide*/
844 (binaryfunc)IRWinSinc_inplace_div, /*nb_inplace_true_divide*/
845 0, /* nb_index */
846 };
847
848 PyTypeObject IRWinSincType =
849 {
850 PyVarObject_HEAD_INIT(NULL, 0)
851 "_pyo.IRWinSinc_base", /*tp_name*/
852 sizeof(IRWinSinc), /*tp_basicsize*/
853 0, /*tp_itemsize*/
854 (destructor)IRWinSinc_dealloc, /*tp_dealloc*/
855 0, /*tp_print*/
856 0, /*tp_getattr*/
857 0, /*tp_setattr*/
858 0, /*tp_as_async (tp_compare in Python 2)*/
859 0, /*tp_repr*/
860 &IRWinSinc_as_number, /*tp_as_number*/
861 0, /*tp_as_sequence*/
862 0, /*tp_as_mapping*/
863 0, /*tp_hash */
864 0, /*tp_call*/
865 0, /*tp_str*/
866 0, /*tp_getattro*/
867 0, /*tp_setattro*/
868 0, /*tp_as_buffer*/
869 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
870 "IRWinSinc objects. Windowed-sinc filter.", /* tp_doc */
871 (traverseproc)IRWinSinc_traverse, /* tp_traverse */
872 (inquiry)IRWinSinc_clear, /* tp_clear */
873 0, /* tp_richcompare */
874 0, /* tp_weaklistoffset */
875 0, /* tp_iter */
876 0, /* tp_iternext */
877 IRWinSinc_methods, /* tp_methods */
878 IRWinSinc_members, /* tp_members */
879 0, /* tp_getset */
880 0, /* tp_base */
881 0, /* tp_dict */
882 0, /* tp_descr_get */
883 0, /* tp_descr_set */
884 0, /* tp_dictoffset */
885 0, /* tp_init */
886 0, /* tp_alloc */
887 IRWinSinc_new, /* tp_new */
888 };
889
890 /************/
891 /* IRAverage */
892 /************/
893 typedef struct
894 {
895 pyo_audio_HEAD
896 PyObject *input;
897 Stream *input_stream;
898 int modebuffer[2]; // need at least 2 slots for mul & add
899 MYFLT *impulse;
900 MYFLT *input_tmp;
901 int count;
902 int order;
903 int size;
904 } IRAverage;
905
906 static void
907 IRAverage_alloc_memory(IRAverage *self)
908 {
909 int i;
910 MYFLT val, sum;
911
912 if ((self->order % 2) != 0)
913 self->order += 1;
914
915 self->size = self->order + 1;
916
917 self->input_tmp = (MYFLT *)PyMem_RawRealloc(self->input_tmp, self->size * sizeof(MYFLT));
918 self->impulse = (MYFLT *)PyMem_RawRealloc(self->impulse, self->size * sizeof(MYFLT));
919
920 sum = 0.0;
921
922 for (i = 0; i < self->size; i++)
923 {
924 self->input_tmp[i] = 0.0;
925 val = 0.42 - 0.5 * MYCOS(TWOPI * i / self->order) + 0.08 * MYCOS(2.0 * TWOPI * i / self->order);
926 self->impulse[i] = val;
927 sum += val;
928 }
929
930 for (i = 0; i < self->size; i++)
931 {
932 self->impulse[i] /= sum;
933 }
934 }
935
936 static void
937 IRAverage_filters(IRAverage *self)
938 {
939 int i, j, tmp_count;
940
941 MYFLT *in = Stream_getData((Stream *)self->input_stream);
942
943 for (i = 0; i < self->bufsize; i++)
944 {
945 self->data[i] = 0.0;
946 tmp_count = self->count;
947
948 for (j = 0; j < self->size; j++)
949 {
950 if (tmp_count < 0)
951 tmp_count += self->size;
952
953 self->data[i] += self->input_tmp[tmp_count--] * self->impulse[j];
954 }
955
956 self->count++;
957
958 if (self->count == self->size)
959 self->count = 0;
960
961 self->input_tmp[self->count] = in[i];
962 }
963 }
964
965 static void IRAverage_postprocessing_ii(IRAverage *self) { POST_PROCESSING_II };
966 static void IRAverage_postprocessing_ai(IRAverage *self) { POST_PROCESSING_AI };
967 static void IRAverage_postprocessing_ia(IRAverage *self) { POST_PROCESSING_IA };
968 static void IRAverage_postprocessing_aa(IRAverage *self) { POST_PROCESSING_AA };
969 static void IRAverage_postprocessing_ireva(IRAverage *self) { POST_PROCESSING_IREVA };
970 static void IRAverage_postprocessing_areva(IRAverage *self) { POST_PROCESSING_AREVA };
971 static void IRAverage_postprocessing_revai(IRAverage *self) { POST_PROCESSING_REVAI };
972 static void IRAverage_postprocessing_revaa(IRAverage *self) { POST_PROCESSING_REVAA };
973 static void IRAverage_postprocessing_revareva(IRAverage *self) { POST_PROCESSING_REVAREVA };
974
975 static void
976 IRAverage_setProcMode(IRAverage *self)
977 {
978 int muladdmode;
979 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
980
981 self->proc_func_ptr = IRAverage_filters;
982
983 switch (muladdmode)
984 {
985 case 0:
986 self->muladd_func_ptr = IRAverage_postprocessing_ii;
987 break;
988
989 case 1:
990 self->muladd_func_ptr = IRAverage_postprocessing_ai;
991 break;
992
993 case 2:
994 self->muladd_func_ptr = IRAverage_postprocessing_revai;
995 break;
996
997 case 10:
998 self->muladd_func_ptr = IRAverage_postprocessing_ia;
999 break;
1000
1001 case 11:
1002 self->muladd_func_ptr = IRAverage_postprocessing_aa;
1003 break;
1004
1005 case 12:
1006 self->muladd_func_ptr = IRAverage_postprocessing_revaa;
1007 break;
1008
1009 case 20:
1010 self->muladd_func_ptr = IRAverage_postprocessing_ireva;
1011 break;
1012
1013 case 21:
1014 self->muladd_func_ptr = IRAverage_postprocessing_areva;
1015 break;
1016
1017 case 22:
1018 self->muladd_func_ptr = IRAverage_postprocessing_revareva;
1019 break;
1020 }
1021 }
1022
1023 static void
1024 IRAverage_compute_next_data_frame(IRAverage *self)
1025 {
1026 (*self->proc_func_ptr)(self);
1027 (*self->muladd_func_ptr)(self);
1028 }
1029
1030 static int
1031 IRAverage_traverse(IRAverage *self, visitproc visit, void *arg)
1032 {
1033 pyo_VISIT
1034 Py_VISIT(self->input);
1035 return 0;
1036 }
1037
1038 static int
1039 IRAverage_clear(IRAverage *self)
1040 {
1041 pyo_CLEAR
1042 Py_CLEAR(self->input);
1043 return 0;
1044 }
1045
1046 static void
1047 IRAverage_dealloc(IRAverage* self)
1048 {
1049 pyo_DEALLOC
1050 PyMem_RawFree(self->input_tmp);
1051 PyMem_RawFree(self->impulse);
1052 IRAverage_clear(self);
1053 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1054 Py_TYPE(self)->tp_free((PyObject*)self);
1055 }
1056
1057 static PyObject *
1058 IRAverage_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1059 {
1060 int i;
1061 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
1062 IRAverage *self;
1063 self = (IRAverage *)type->tp_alloc(type, 0);
1064
1065 self->order = 32;
1066 self->count = 0;
1067 self->modebuffer[0] = 0;
1068 self->modebuffer[1] = 0;
1069
1070 INIT_OBJECT_COMMON
1071 Stream_setFunctionPtr(self->stream, IRAverage_compute_next_data_frame);
1072 self->mode_func_ptr = IRAverage_setProcMode;
1073
1074 static char *kwlist[] = {"input", "order", "mul", "add", NULL};
1075
1076 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &inputtmp, &self->order, &multmp, &addtmp))
1077 Py_RETURN_NONE;
1078
1079 INIT_INPUT_STREAM
1080
1081 if (multmp)
1082 {
1083 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1084 }
1085
1086 if (addtmp)
1087 {
1088 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1089 }
1090
1091 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1092
1093 IRAverage_alloc_memory(self);
1094
1095 (*self->mode_func_ptr)(self);
1096
1097 return (PyObject *)self;
1098 }
1099
1100 static PyObject * IRAverage_getServer(IRAverage* self) { GET_SERVER };
1101 static PyObject * IRAverage_getStream(IRAverage* self) { GET_STREAM };
1102 static PyObject * IRAverage_setMul(IRAverage *self, PyObject *arg) { SET_MUL };
1103 static PyObject * IRAverage_setAdd(IRAverage *self, PyObject *arg) { SET_ADD };
1104 static PyObject * IRAverage_setSub(IRAverage *self, PyObject *arg) { SET_SUB };
1105 static PyObject * IRAverage_setDiv(IRAverage *self, PyObject *arg) { SET_DIV };
1106
1107 static PyObject * IRAverage_play(IRAverage *self, PyObject *args, PyObject *kwds) { PLAY };
1108 static PyObject * IRAverage_out(IRAverage *self, PyObject *args, PyObject *kwds) { OUT };
1109 static PyObject * IRAverage_stop(IRAverage *self, PyObject *args, PyObject *kwds) { STOP };
1110
1111 static PyObject * IRAverage_multiply(IRAverage *self, PyObject *arg) { MULTIPLY };
1112 static PyObject * IRAverage_inplace_multiply(IRAverage *self, PyObject *arg) { INPLACE_MULTIPLY };
1113 static PyObject * IRAverage_add(IRAverage *self, PyObject *arg) { ADD };
1114 static PyObject * IRAverage_inplace_add(IRAverage *self, PyObject *arg) { INPLACE_ADD };
1115 static PyObject * IRAverage_sub(IRAverage *self, PyObject *arg) { SUB };
1116 static PyObject * IRAverage_inplace_sub(IRAverage *self, PyObject *arg) { INPLACE_SUB };
1117 static PyObject * IRAverage_div(IRAverage *self, PyObject *arg) { DIV };
1118 static PyObject * IRAverage_inplace_div(IRAverage *self, PyObject *arg) { INPLACE_DIV };
1119
1120 static PyMemberDef IRAverage_members[] =
1121 {
1122 {"server", T_OBJECT_EX, offsetof(IRAverage, server), 0, "Pyo server."},
1123 {"stream", T_OBJECT_EX, offsetof(IRAverage, stream), 0, "Stream object."},
1124 {"input", T_OBJECT_EX, offsetof(IRAverage, input), 0, "Input sound object."},
1125 {"mul", T_OBJECT_EX, offsetof(IRAverage, mul), 0, "Mul factor."},
1126 {"add", T_OBJECT_EX, offsetof(IRAverage, add), 0, "Add factor."},
1127 {NULL} /* Sentinel */
1128 };
1129
1130 static PyMethodDef IRAverage_methods[] =
1131 {
1132 {"getServer", (PyCFunction)IRAverage_getServer, METH_NOARGS, "Returns server object."},
1133 {"_getStream", (PyCFunction)IRAverage_getStream, METH_NOARGS, "Returns stream object."},
1134 {"play", (PyCFunction)IRAverage_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1135 {"out", (PyCFunction)IRAverage_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1136 {"stop", (PyCFunction)IRAverage_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1137 {"setMul", (PyCFunction)IRAverage_setMul, METH_O, "Sets mul factor."},
1138 {"setAdd", (PyCFunction)IRAverage_setAdd, METH_O, "Sets add factor."},
1139 {"setSub", (PyCFunction)IRAverage_setSub, METH_O, "Sets inverse add factor."},
1140 {"setDiv", (PyCFunction)IRAverage_setDiv, METH_O, "Sets inverse mul factor."},
1141 {NULL} /* Sentinel */
1142 };
1143
1144 static PyNumberMethods IRAverage_as_number =
1145 {
1146 (binaryfunc)IRAverage_add, /*nb_add*/
1147 (binaryfunc)IRAverage_sub, /*nb_subtract*/
1148 (binaryfunc)IRAverage_multiply, /*nb_multiply*/
1149 0, /*nb_remainder*/
1150 0, /*nb_divmod*/
1151 0, /*nb_power*/
1152 0, /*nb_neg*/
1153 0, /*nb_pos*/
1154 0, /*(unaryfunc)array_abs,*/
1155 0, /*nb_nonzero*/
1156 0, /*nb_invert*/
1157 0, /*nb_lshift*/
1158 0, /*nb_rshift*/
1159 0, /*nb_and*/
1160 0, /*nb_xor*/
1161 0, /*nb_or*/
1162 0, /*nb_int*/
1163 0, /*nb_long*/
1164 0, /*nb_float*/
1165 (binaryfunc)IRAverage_inplace_add, /*inplace_add*/
1166 (binaryfunc)IRAverage_inplace_sub, /*inplace_subtract*/
1167 (binaryfunc)IRAverage_inplace_multiply, /*inplace_multiply*/
1168 0, /*inplace_remainder*/
1169 0, /*inplace_power*/
1170 0, /*inplace_lshift*/
1171 0, /*inplace_rshift*/
1172 0, /*inplace_and*/
1173 0, /*inplace_xor*/
1174 0, /*inplace_or*/
1175 0, /*nb_floor_divide*/
1176 (binaryfunc)IRAverage_div, /*nb_true_divide*/
1177 0, /*nb_inplace_floor_divide*/
1178 (binaryfunc)IRAverage_inplace_div, /*nb_inplace_true_divide*/
1179 0, /* nb_index */
1180 };
1181
1182 PyTypeObject IRAverageType =
1183 {
1184 PyVarObject_HEAD_INIT(NULL, 0)
1185 "_pyo.IRAverage_base", /*tp_name*/
1186 sizeof(IRAverage), /*tp_basicsize*/
1187 0, /*tp_itemsize*/
1188 (destructor)IRAverage_dealloc, /*tp_dealloc*/
1189 0, /*tp_print*/
1190 0, /*tp_getattr*/
1191 0, /*tp_setattr*/
1192 0, /*tp_as_async (tp_compare in Python 2)*/
1193 0, /*tp_repr*/
1194 &IRAverage_as_number, /*tp_as_number*/
1195 0, /*tp_as_sequence*/
1196 0, /*tp_as_mapping*/
1197 0, /*tp_hash */
1198 0, /*tp_call*/
1199 0, /*tp_str*/
1200 0, /*tp_getattro*/
1201 0, /*tp_setattro*/
1202 0, /*tp_as_buffer*/
1203 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1204 "IRAverage objects. Moving average FIR filter.", /* tp_doc */
1205 (traverseproc)IRAverage_traverse, /* tp_traverse */
1206 (inquiry)IRAverage_clear, /* tp_clear */
1207 0, /* tp_richcompare */
1208 0, /* tp_weaklistoffset */
1209 0, /* tp_iter */
1210 0, /* tp_iternext */
1211 IRAverage_methods, /* tp_methods */
1212 IRAverage_members, /* tp_members */
1213 0, /* tp_getset */
1214 0, /* tp_base */
1215 0, /* tp_dict */
1216 0, /* tp_descr_get */
1217 0, /* tp_descr_set */
1218 0, /* tp_dictoffset */
1219 0, /* tp_init */
1220 0, /* tp_alloc */
1221 IRAverage_new, /* tp_new */
1222 };
1223
1224 /************/
1225 /* IRPulse */
1226 /************/
1227 typedef struct
1228 {
1229 pyo_audio_HEAD
1230 PyObject *input;
1231 Stream *input_stream;
1232 PyObject *freq;
1233 Stream *freq_stream;
1234 PyObject *bandwidth;
1235 Stream *bandwidth_stream;
1236 int modebuffer[4]; // need at least 2 slots for mul & add
1237 MYFLT *impulse;
1238 MYFLT *input_tmp;
1239 int count;
1240 int filtertype;
1241 int order;
1242 int size;
1243 int changed;
1244 MYFLT last_freq;
1245 MYFLT last_bandwidth;
1246 } IRPulse;
1247
1248 static void
1249 IRPulse_alloc_memory(IRPulse *self)
1250 {
1251 int i;
1252
1253 if ((self->order % 2) != 0)
1254 self->order += 1;
1255
1256 self->size = self->order + 1;
1257
1258 self->input_tmp = (MYFLT *)PyMem_RawRealloc(self->input_tmp, self->size * sizeof(MYFLT));
1259 self->impulse = (MYFLT *)PyMem_RawRealloc(self->impulse, self->size * sizeof(MYFLT));
1260
1261 for (i = 0; i < self->size; i++)
1262 {
1263 self->input_tmp[i] = self->impulse[i] = 0.0;
1264 }
1265 }
1266
1267 static void
1268 IRPulse_create_impulse(IRPulse *self, MYFLT freq, MYFLT bandwidth)
1269 {
1270 int i, n, w, bw, dir, gate;
1271 MYFLT val, sum;
1272
1273 sum = 0.0;
1274
1275 if (freq < 1)
1276 freq = 1.0;
1277 else if (freq > (self->sr * 0.5))
1278 freq = self->sr * 0.5;
1279
1280 if (bandwidth < 1)
1281 bandwidth = 1.0;
1282 else if (bandwidth > (self->sr * 0.5))
1283 bandwidth = self->sr * 0.5;
1284
1285 switch (self->filtertype)
1286 {
1287 case 0:
1288 /* PULSE */
1289 w = (int)(self->sr / freq);
1290 bw = (int)(self->sr / bandwidth);
1291
1292 for (i = 0; i < self->size; i++)
1293 {
1294 if ((i % w) <= bw)
1295 {
1296 val = 1.0;
1297 self->impulse[i] = val;
1298 sum += val;
1299 }
1300 else
1301 {
1302 self->impulse[i] = 0.0;
1303 }
1304
1305 }
1306
1307 for (i = 0; i < self->size; i++)
1308 {
1309 self->impulse[i] /= sum;
1310 }
1311
1312 break;
1313
1314 case 1:
1315 /* PULSELP */
1316 w = (int)(self->sr / freq);
1317 bw = (int)(self->sr / bandwidth);
1318
1319 for (i = 0; i < self->size; i++)
1320 {
1321 if ((i % w) <= bw)
1322 {
1323 n = i % w;
1324 val = 0.5 * (1.0 - MYCOS(TWOPI * n / (bw - 1)));
1325 self->impulse[i] = val;
1326 sum += val;
1327 }
1328 else
1329 {
1330 self->impulse[i] = 0.0;
1331 }
1332
1333 }
1334
1335 for (i = 0; i < self->size; i++)
1336 {
1337 self->impulse[i] /= sum;
1338 }
1339
1340 break;
1341
1342 case 2:
1343 /* PULSEODD */
1344 w = (int)(self->sr / (freq * 2));
1345 bw = (int)(self->sr / bandwidth);
1346 dir = 0;
1347 gate = 0;
1348
1349 for (i = 0; i < self->size; i++)
1350 {
1351 if ((i % w) <= bw)
1352 {
1353 if (gate == 1)
1354 {
1355 dir++;
1356 gate = 0;
1357 }
1358
1359 if ((dir % 2) == 0)
1360 val = 1.0;
1361 else
1362 val = -1.0;
1363
1364 self->impulse[i] = val;
1365 sum += MYFABS(val);
1366 }
1367 else
1368 {
1369 gate = 1;
1370 self->impulse[i] = 0.0;
1371 }
1372 }
1373
1374 for (i = 0; i < self->size; i++)
1375 {
1376 self->impulse[i] /= sum;
1377 }
1378
1379 break;
1380
1381 case 3:
1382 /* PULSEODDLP */
1383 w = (int)(self->sr / (freq * 2));
1384 bw = (int)(self->sr / bandwidth);
1385 dir = 0;
1386 gate = 0;
1387
1388 for (i = 0; i < self->size; i++)
1389 {
1390 if ((i % w) <= bw)
1391 {
1392 n = i % w;
1393 val = 0.5 * (1.0 - MYCOS(TWOPI * n / (bw - 1)));
1394
1395 if (gate == 1)
1396 {
1397 dir++;
1398 gate = 0;
1399 }
1400
1401 if ((dir % 2) == 1)
1402 val = -val;
1403
1404 self->impulse[i] = val;
1405 sum += MYFABS(val);
1406 }
1407 else
1408 {
1409 gate = 1;
1410 self->impulse[i] = 0.0;
1411 }
1412 }
1413
1414 for (i = 0; i < self->size; i++)
1415 {
1416 self->impulse[i] /= sum;
1417 }
1418
1419 break;
1420 }
1421 }
1422
1423 static void
1424 IRPulse_filters(IRPulse *self)
1425 {
1426 int i, j, tmp_count;
1427 MYFLT freq, bw;
1428
1429 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1430
1431 if (self->modebuffer[2] == 0)
1432 freq = PyFloat_AS_DOUBLE(self->freq);
1433 else
1434 {
1435 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1436 freq = fr[0];
1437 }
1438
1439 if (self->modebuffer[3] == 0)
1440 bw = PyFloat_AS_DOUBLE(self->bandwidth);
1441 else
1442 {
1443 MYFLT *band = Stream_getData((Stream *)self->bandwidth_stream);
1444 bw = band[0];
1445 }
1446
1447 if (freq != self->last_freq || bw != self->last_bandwidth || self->changed == 1)
1448 {
1449 IRPulse_create_impulse(self, freq, bw);
1450 self->last_freq = freq;
1451 self->last_bandwidth = bw;
1452 self->changed = 0;
1453 }
1454
1455 for (i = 0; i < self->bufsize; i++)
1456 {
1457 self->data[i] = 0.0;
1458 tmp_count = self->count;
1459
1460 for (j = 0; j < self->size; j++)
1461 {
1462 if (tmp_count < 0)
1463 tmp_count += self->size;
1464
1465 self->data[i] += self->input_tmp[tmp_count--] * self->impulse[j];
1466 }
1467
1468 self->count++;
1469
1470 if (self->count == self->size)
1471 self->count = 0;
1472
1473 self->input_tmp[self->count] = in[i];
1474 }
1475 }
1476
1477 static void IRPulse_postprocessing_ii(IRPulse *self) { POST_PROCESSING_II };
1478 static void IRPulse_postprocessing_ai(IRPulse *self) { POST_PROCESSING_AI };
1479 static void IRPulse_postprocessing_ia(IRPulse *self) { POST_PROCESSING_IA };
1480 static void IRPulse_postprocessing_aa(IRPulse *self) { POST_PROCESSING_AA };
1481 static void IRPulse_postprocessing_ireva(IRPulse *self) { POST_PROCESSING_IREVA };
1482 static void IRPulse_postprocessing_areva(IRPulse *self) { POST_PROCESSING_AREVA };
1483 static void IRPulse_postprocessing_revai(IRPulse *self) { POST_PROCESSING_REVAI };
1484 static void IRPulse_postprocessing_revaa(IRPulse *self) { POST_PROCESSING_REVAA };
1485 static void IRPulse_postprocessing_revareva(IRPulse *self) { POST_PROCESSING_REVAREVA };
1486
1487 static void
1488 IRPulse_setProcMode(IRPulse *self)
1489 {
1490 int muladdmode;
1491 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1492
1493 self->proc_func_ptr = IRPulse_filters;
1494
1495 switch (muladdmode)
1496 {
1497 case 0:
1498 self->muladd_func_ptr = IRPulse_postprocessing_ii;
1499 break;
1500
1501 case 1:
1502 self->muladd_func_ptr = IRPulse_postprocessing_ai;
1503 break;
1504
1505 case 2:
1506 self->muladd_func_ptr = IRPulse_postprocessing_revai;
1507 break;
1508
1509 case 10:
1510 self->muladd_func_ptr = IRPulse_postprocessing_ia;
1511 break;
1512
1513 case 11:
1514 self->muladd_func_ptr = IRPulse_postprocessing_aa;
1515 break;
1516
1517 case 12:
1518 self->muladd_func_ptr = IRPulse_postprocessing_revaa;
1519 break;
1520
1521 case 20:
1522 self->muladd_func_ptr = IRPulse_postprocessing_ireva;
1523 break;
1524
1525 case 21:
1526 self->muladd_func_ptr = IRPulse_postprocessing_areva;
1527 break;
1528
1529 case 22:
1530 self->muladd_func_ptr = IRPulse_postprocessing_revareva;
1531 break;
1532 }
1533 }
1534
1535 static void
1536 IRPulse_compute_next_data_frame(IRPulse *self)
1537 {
1538 (*self->proc_func_ptr)(self);
1539 (*self->muladd_func_ptr)(self);
1540 }
1541
1542 static int
1543 IRPulse_traverse(IRPulse *self, visitproc visit, void *arg)
1544 {
1545 pyo_VISIT
1546 Py_VISIT(self->input);
1547 Py_VISIT(self->freq);
1548 Py_VISIT(self->bandwidth);
1549 return 0;
1550 }
1551
1552 static int
1553 IRPulse_clear(IRPulse *self)
1554 {
1555 pyo_CLEAR
1556 Py_CLEAR(self->input);
1557 Py_CLEAR(self->freq);
1558 Py_CLEAR(self->bandwidth);
1559 return 0;
1560 }
1561
1562 static void
1563 IRPulse_dealloc(IRPulse* self)
1564 {
1565 pyo_DEALLOC
1566 PyMem_RawFree(self->input_tmp);
1567 PyMem_RawFree(self->impulse);
1568 IRPulse_clear(self);
1569 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1570 Py_TYPE(self)->tp_free((PyObject*)self);
1571 }
1572
1573 static PyObject *
1574 IRPulse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1575 {
1576 int i;
1577 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *bandwidthtmp = NULL, *multmp = NULL, *addtmp = NULL;
1578 IRPulse *self;
1579 self = (IRPulse *)type->tp_alloc(type, 0);
1580
1581 self->last_freq = -1.0;
1582 self->last_bandwidth = -1.0;
1583 self->freq = PyFloat_FromDouble(500.0);
1584 self->bandwidth = PyFloat_FromDouble(2500.0);
1585 self->filtertype = 0;
1586 self->order = 256;
1587 self->count = 0;
1588 self->changed = 0;
1589 self->modebuffer[0] = 0;
1590 self->modebuffer[1] = 0;
1591 self->modebuffer[2] = 0;
1592 self->modebuffer[3] = 0;
1593
1594 INIT_OBJECT_COMMON
1595 Stream_setFunctionPtr(self->stream, IRPulse_compute_next_data_frame);
1596 self->mode_func_ptr = IRPulse_setProcMode;
1597
1598 static char *kwlist[] = {"input", "freq", "bw", "type", "order", "mul", "add", NULL};
1599
1600 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOiiOO", kwlist, &inputtmp, &freqtmp, &bandwidthtmp, &self->filtertype, &self->order, &multmp, &addtmp))
1601 Py_RETURN_NONE;
1602
1603 INIT_INPUT_STREAM
1604
1605 if (freqtmp)
1606 {
1607 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1608 }
1609
1610 if (bandwidthtmp)
1611 {
1612 PyObject_CallMethod((PyObject *)self, "setBandwidth", "O", bandwidthtmp);
1613 }
1614
1615 if (multmp)
1616 {
1617 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1618 }
1619
1620 if (addtmp)
1621 {
1622 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1623 }
1624
1625 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1626
1627 IRPulse_alloc_memory(self);
1628
1629 (*self->mode_func_ptr)(self);
1630
1631 return (PyObject *)self;
1632 }
1633
1634 static PyObject * IRPulse_getServer(IRPulse* self) { GET_SERVER };
1635 static PyObject * IRPulse_getStream(IRPulse* self) { GET_STREAM };
1636 static PyObject * IRPulse_setMul(IRPulse *self, PyObject *arg) { SET_MUL };
1637 static PyObject * IRPulse_setAdd(IRPulse *self, PyObject *arg) { SET_ADD };
1638 static PyObject * IRPulse_setSub(IRPulse *self, PyObject *arg) { SET_SUB };
1639 static PyObject * IRPulse_setDiv(IRPulse *self, PyObject *arg) { SET_DIV };
1640
1641 static PyObject * IRPulse_play(IRPulse *self, PyObject *args, PyObject *kwds) { PLAY };
1642 static PyObject * IRPulse_out(IRPulse *self, PyObject *args, PyObject *kwds) { OUT };
1643 static PyObject * IRPulse_stop(IRPulse *self, PyObject *args, PyObject *kwds) { STOP };
1644
1645 static PyObject * IRPulse_multiply(IRPulse *self, PyObject *arg) { MULTIPLY };
1646 static PyObject * IRPulse_inplace_multiply(IRPulse *self, PyObject *arg) { INPLACE_MULTIPLY };
1647 static PyObject * IRPulse_add(IRPulse *self, PyObject *arg) { ADD };
1648 static PyObject * IRPulse_inplace_add(IRPulse *self, PyObject *arg) { INPLACE_ADD };
1649 static PyObject * IRPulse_sub(IRPulse *self, PyObject *arg) { SUB };
1650 static PyObject * IRPulse_inplace_sub(IRPulse *self, PyObject *arg) { INPLACE_SUB };
1651 static PyObject * IRPulse_div(IRPulse *self, PyObject *arg) { DIV };
1652 static PyObject * IRPulse_inplace_div(IRPulse *self, PyObject *arg) { INPLACE_DIV };
1653
1654 static PyObject * IRPulse_setFreq(IRPulse *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
1655 static PyObject * IRPulse_setBandwidth(IRPulse *self, PyObject *arg) { SET_PARAM(self->bandwidth, self->bandwidth_stream, 3); }
1656
1657 static PyObject *
1658 IRPulse_setType(IRPulse *self, PyObject *arg)
1659 {
1660
1661 ASSERT_ARG_NOT_NULL
1662
1663 if (PyLong_Check(arg))
1664 {
1665 self->filtertype = PyLong_AsLong(arg);
1666 self->changed = 1;
1667 }
1668
1669 Py_RETURN_NONE;
1670 }
1671
1672 static PyMemberDef IRPulse_members[] =
1673 {
1674 {"server", T_OBJECT_EX, offsetof(IRPulse, server), 0, "Pyo server."},
1675 {"stream", T_OBJECT_EX, offsetof(IRPulse, stream), 0, "Stream object."},
1676 {"input", T_OBJECT_EX, offsetof(IRPulse, input), 0, "Input sound object."},
1677 {"freq", T_OBJECT_EX, offsetof(IRPulse, freq), 0, "Cutoff or center frequency."},
1678 {"bandwidth", T_OBJECT_EX, offsetof(IRPulse, bandwidth), 0, "Bandwidth, in Hz, for bandreject and bandpass filters."},
1679 {"mul", T_OBJECT_EX, offsetof(IRPulse, mul), 0, "Mul factor."},
1680 {"add", T_OBJECT_EX, offsetof(IRPulse, add), 0, "Add factor."},
1681 {NULL} /* Sentinel */
1682 };
1683
1684 static PyMethodDef IRPulse_methods[] =
1685 {
1686 {"getServer", (PyCFunction)IRPulse_getServer, METH_NOARGS, "Returns server object."},
1687 {"_getStream", (PyCFunction)IRPulse_getStream, METH_NOARGS, "Returns stream object."},
1688 {"play", (PyCFunction)IRPulse_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1689 {"out", (PyCFunction)IRPulse_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1690 {"stop", (PyCFunction)IRPulse_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1691 {"setFreq", (PyCFunction)IRPulse_setFreq, METH_O, "Sets center/cutoff frequency."},
1692 {"setBandwidth", (PyCFunction)IRPulse_setBandwidth, METH_O, "Sets bandwidth in Hz."},
1693 {"setType", (PyCFunction)IRPulse_setType, METH_O, "Sets filter type factor."},
1694 {"setMul", (PyCFunction)IRPulse_setMul, METH_O, "Sets mul factor."},
1695 {"setAdd", (PyCFunction)IRPulse_setAdd, METH_O, "Sets add factor."},
1696 {"setSub", (PyCFunction)IRPulse_setSub, METH_O, "Sets inverse add factor."},
1697 {"setDiv", (PyCFunction)IRPulse_setDiv, METH_O, "Sets inverse mul factor."},
1698 {NULL} /* Sentinel */
1699 };
1700
1701 static PyNumberMethods IRPulse_as_number =
1702 {
1703 (binaryfunc)IRPulse_add, /*nb_add*/
1704 (binaryfunc)IRPulse_sub, /*nb_subtract*/
1705 (binaryfunc)IRPulse_multiply, /*nb_multiply*/
1706 0, /*nb_remainder*/
1707 0, /*nb_divmod*/
1708 0, /*nb_power*/
1709 0, /*nb_neg*/
1710 0, /*nb_pos*/
1711 0, /*(unaryfunc)array_abs,*/
1712 0, /*nb_nonzero*/
1713 0, /*nb_invert*/
1714 0, /*nb_lshift*/
1715 0, /*nb_rshift*/
1716 0, /*nb_and*/
1717 0, /*nb_xor*/
1718 0, /*nb_or*/
1719 0, /*nb_int*/
1720 0, /*nb_long*/
1721 0, /*nb_float*/
1722 (binaryfunc)IRPulse_inplace_add, /*inplace_add*/
1723 (binaryfunc)IRPulse_inplace_sub, /*inplace_subtract*/
1724 (binaryfunc)IRPulse_inplace_multiply, /*inplace_multiply*/
1725 0, /*inplace_remainder*/
1726 0, /*inplace_power*/
1727 0, /*inplace_lshift*/
1728 0, /*inplace_rshift*/
1729 0, /*inplace_and*/
1730 0, /*inplace_xor*/
1731 0, /*inplace_or*/
1732 0, /*nb_floor_divide*/
1733 (binaryfunc)IRPulse_div, /*nb_true_divide*/
1734 0, /*nb_inplace_floor_divide*/
1735 (binaryfunc)IRPulse_inplace_div, /*nb_inplace_true_divide*/
1736 0, /* nb_index */
1737 };
1738
1739 PyTypeObject IRPulseType =
1740 {
1741 PyVarObject_HEAD_INIT(NULL, 0)
1742 "_pyo.IRPulse_base", /*tp_name*/
1743 sizeof(IRPulse), /*tp_basicsize*/
1744 0, /*tp_itemsize*/
1745 (destructor)IRPulse_dealloc, /*tp_dealloc*/
1746 0, /*tp_print*/
1747 0, /*tp_getattr*/
1748 0, /*tp_setattr*/
1749 0, /*tp_as_async (tp_compare in Python 2)*/
1750 0, /*tp_repr*/
1751 &IRPulse_as_number, /*tp_as_number*/
1752 0, /*tp_as_sequence*/
1753 0, /*tp_as_mapping*/
1754 0, /*tp_hash */
1755 0, /*tp_call*/
1756 0, /*tp_str*/
1757 0, /*tp_getattro*/
1758 0, /*tp_setattro*/
1759 0, /*tp_as_buffer*/
1760 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1761 "IRPulse objects. Windowed-sinc filter.", /* tp_doc */
1762 (traverseproc)IRPulse_traverse, /* tp_traverse */
1763 (inquiry)IRPulse_clear, /* tp_clear */
1764 0, /* tp_richcompare */
1765 0, /* tp_weaklistoffset */
1766 0, /* tp_iter */
1767 0, /* tp_iternext */
1768 IRPulse_methods, /* tp_methods */
1769 IRPulse_members, /* tp_members */
1770 0, /* tp_getset */
1771 0, /* tp_base */
1772 0, /* tp_dict */
1773 0, /* tp_descr_get */
1774 0, /* tp_descr_set */
1775 0, /* tp_dictoffset */
1776 0, /* tp_init */
1777 0, /* tp_alloc */
1778 IRPulse_new, /* tp_new */
1779 };
1780
1781 /************/
1782 /* IRFM */
1783 /************/
1784 typedef struct
1785 {
1786 pyo_audio_HEAD
1787 PyObject *input;
1788 Stream *input_stream;
1789 PyObject *carrier;
1790 Stream *carrier_stream;
1791 PyObject *ratio;
1792 Stream *ratio_stream;
1793 PyObject *index;
1794 Stream *index_stream;
1795 int modebuffer[5]; // need at least 2 slots for mul & add
1796 MYFLT *impulse;
1797 MYFLT *input_tmp;
1798 int count;
1799 int order;
1800 int size;
1801 MYFLT last_carrier;
1802 MYFLT last_ratio;
1803 MYFLT last_index;
1804 } IRFM;
1805
1806 static void
1807 IRFM_alloc_memory(IRFM *self)
1808 {
1809 int i;
1810
1811 if ((self->order % 2) != 0)
1812 self->order += 1;
1813
1814 self->size = self->order + 1;
1815
1816 self->input_tmp = (MYFLT *)PyMem_RawRealloc(self->input_tmp, self->size * sizeof(MYFLT));
1817 self->impulse = (MYFLT *)PyMem_RawRealloc(self->impulse, self->size * sizeof(MYFLT));
1818
1819 for (i = 0; i < self->size; i++)
1820 {
1821 self->input_tmp[i] = self->impulse[i] = 0.0;
1822 }
1823 }
1824
1825 static void
1826 IRFM_create_impulse(IRFM *self, MYFLT carrier, MYFLT ratio, MYFLT index)
1827 {
1828 int i;
1829 MYFLT val, sum, invSum, env, mod, fc, bw, modamp;
1830
1831 if (carrier < 1)
1832 carrier = 1.0;
1833 else if (carrier > (self->sr * 0.5))
1834 carrier = self->sr * 0.5;
1835
1836 if (ratio < 0.0001)
1837 ratio = 0.0001;
1838 else if (ratio > (self->sr * 0.5))
1839 ratio = self->sr * 0.5;
1840
1841 if (index < 0)
1842 index = 0.0;
1843
1844 fc = carrier / self->sr * self->order;
1845 bw = carrier * ratio / self->sr * self->order;
1846 modamp = index * TWOPI * bw / self->order;
1847 sum = 0.0;
1848
1849 for (i = 0; i < self->size; i++)
1850 {
1851 env = 0.5 * (1.0 - MYCOS(TWOPI * i / self->order));
1852 mod = modamp * MYSIN(TWOPI * bw * i / self->order);
1853 val = MYSIN(TWOPI * (fc + mod) * i / self->order) * env;
1854 sum += MYFABS(val);
1855 self->impulse[i] = val;
1856 }
1857
1858 invSum = 1.0 / sum;
1859
1860 for (i = 0; i < self->size; i++)
1861 {
1862 self->impulse[i] *= invSum;
1863 }
1864 }
1865
1866 static void
1867 IRFM_filters(IRFM *self)
1868 {
1869 int i, j, tmp_count;
1870 MYFLT carrier, ratio, index;
1871
1872 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1873
1874 if (self->modebuffer[2] == 0)
1875 carrier = PyFloat_AS_DOUBLE(self->carrier);
1876 else
1877 {
1878 MYFLT *car = Stream_getData((Stream *)self->carrier_stream);
1879 carrier = car[0];
1880 }
1881
1882 if (self->modebuffer[3] == 0)
1883 ratio = PyFloat_AS_DOUBLE(self->ratio);
1884 else
1885 {
1886 MYFLT *rat = Stream_getData((Stream *)self->ratio_stream);
1887 ratio = rat[0];
1888 }
1889
1890 if (self->modebuffer[4] == 0)
1891 index = PyFloat_AS_DOUBLE(self->index);
1892 else
1893 {
1894 MYFLT *ind = Stream_getData((Stream *)self->index_stream);
1895 index = ind[0];
1896 }
1897
1898 if (carrier != self->last_carrier || ratio != self->last_ratio || index != self->last_index)
1899 {
1900 IRFM_create_impulse(self, carrier, ratio, index);
1901 self->last_carrier = carrier;
1902 self->last_ratio = ratio;
1903 self->last_index = index;
1904 }
1905
1906 for (i = 0; i < self->bufsize; i++)
1907 {
1908 self->data[i] = 0.0;
1909 tmp_count = self->count;
1910
1911 for (j = 0; j < self->size; j++)
1912 {
1913 if (tmp_count < 0)
1914 tmp_count += self->size;
1915
1916 self->data[i] += self->input_tmp[tmp_count--] * self->impulse[j];
1917 }
1918
1919 self->count++;
1920
1921 if (self->count == self->size)
1922 self->count = 0;
1923
1924 self->input_tmp[self->count] = in[i];
1925 }
1926 }
1927
1928 static void IRFM_postprocessing_ii(IRFM *self) { POST_PROCESSING_II };
1929 static void IRFM_postprocessing_ai(IRFM *self) { POST_PROCESSING_AI };
1930 static void IRFM_postprocessing_ia(IRFM *self) { POST_PROCESSING_IA };
1931 static void IRFM_postprocessing_aa(IRFM *self) { POST_PROCESSING_AA };
1932 static void IRFM_postprocessing_ireva(IRFM *self) { POST_PROCESSING_IREVA };
1933 static void IRFM_postprocessing_areva(IRFM *self) { POST_PROCESSING_AREVA };
1934 static void IRFM_postprocessing_revai(IRFM *self) { POST_PROCESSING_REVAI };
1935 static void IRFM_postprocessing_revaa(IRFM *self) { POST_PROCESSING_REVAA };
1936 static void IRFM_postprocessing_revareva(IRFM *self) { POST_PROCESSING_REVAREVA };
1937
1938 static void
1939 IRFM_setProcMode(IRFM *self)
1940 {
1941 int muladdmode;
1942 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1943
1944 self->proc_func_ptr = IRFM_filters;
1945
1946 switch (muladdmode)
1947 {
1948 case 0:
1949 self->muladd_func_ptr = IRFM_postprocessing_ii;
1950 break;
1951
1952 case 1:
1953 self->muladd_func_ptr = IRFM_postprocessing_ai;
1954 break;
1955
1956 case 2:
1957 self->muladd_func_ptr = IRFM_postprocessing_revai;
1958 break;
1959
1960 case 10:
1961 self->muladd_func_ptr = IRFM_postprocessing_ia;
1962 break;
1963
1964 case 11:
1965 self->muladd_func_ptr = IRFM_postprocessing_aa;
1966 break;
1967
1968 case 12:
1969 self->muladd_func_ptr = IRFM_postprocessing_revaa;
1970 break;
1971
1972 case 20:
1973 self->muladd_func_ptr = IRFM_postprocessing_ireva;
1974 break;
1975
1976 case 21:
1977 self->muladd_func_ptr = IRFM_postprocessing_areva;
1978 break;
1979
1980 case 22:
1981 self->muladd_func_ptr = IRFM_postprocessing_revareva;
1982 break;
1983 }
1984 }
1985
1986 static void
1987 IRFM_compute_next_data_frame(IRFM *self)
1988 {
1989 (*self->proc_func_ptr)(self);
1990 (*self->muladd_func_ptr)(self);
1991 }
1992
1993 static int
1994 IRFM_traverse(IRFM *self, visitproc visit, void *arg)
1995 {
1996 pyo_VISIT
1997 Py_VISIT(self->input);
1998 Py_VISIT(self->carrier);
1999 Py_VISIT(self->ratio);
2000 Py_VISIT(self->index);
2001 return 0;
2002 }
2003
2004 static int
2005 IRFM_clear(IRFM *self)
2006 {
2007 pyo_CLEAR
2008 Py_CLEAR(self->input);
2009 Py_CLEAR(self->carrier);
2010 Py_CLEAR(self->ratio);
2011 Py_CLEAR(self->index);
2012 return 0;
2013 }
2014
2015 static void
2016 IRFM_dealloc(IRFM* self)
2017 {
2018 pyo_DEALLOC
2019 PyMem_RawFree(self->input_tmp);
2020 PyMem_RawFree(self->impulse);
2021 IRFM_clear(self);
2022 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2023 Py_TYPE(self)->tp_free((PyObject*)self);
2024 }
2025
2026 static PyObject *
2027 IRFM_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2028 {
2029 int i;
2030 PyObject *inputtmp, *input_streamtmp, *carriertmp = NULL, *ratiotmp = NULL, *indextmp = NULL, *multmp = NULL, *addtmp = NULL;
2031 IRFM *self;
2032 self = (IRFM *)type->tp_alloc(type, 0);
2033
2034 self->last_carrier = -1.0;
2035 self->last_ratio = -1.0;
2036 self->last_index = -1.0;
2037 self->carrier = PyFloat_FromDouble(1000.0);
2038 self->ratio = PyFloat_FromDouble(0.5);
2039 self->index = PyFloat_FromDouble(3.0);
2040 self->order = 256;
2041 self->count = 0;
2042 self->modebuffer[0] = 0;
2043 self->modebuffer[1] = 0;
2044 self->modebuffer[2] = 0;
2045 self->modebuffer[3] = 0;
2046 self->modebuffer[4] = 0;
2047
2048 INIT_OBJECT_COMMON
2049 Stream_setFunctionPtr(self->stream, IRFM_compute_next_data_frame);
2050 self->mode_func_ptr = IRFM_setProcMode;
2051
2052 static char *kwlist[] = {"input", "carrier", "ratio", "index", "order", "mul", "add", NULL};
2053
2054 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOiOO", kwlist, &inputtmp, &carriertmp, &ratiotmp, &indextmp, &self->order, &multmp, &addtmp))
2055 Py_RETURN_NONE;
2056
2057 INIT_INPUT_STREAM
2058
2059 if (carriertmp)
2060 {
2061 PyObject_CallMethod((PyObject *)self, "setCarrier", "O", carriertmp);
2062 }
2063
2064 if (ratiotmp)
2065 {
2066 PyObject_CallMethod((PyObject *)self, "setRatio", "O", ratiotmp);
2067 }
2068
2069 if (indextmp)
2070 {
2071 PyObject_CallMethod((PyObject *)self, "setIndex", "O", indextmp);
2072 }
2073
2074 if (multmp)
2075 {
2076 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2077 }
2078
2079 if (addtmp)
2080 {
2081 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2082 }
2083
2084 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2085
2086 IRFM_alloc_memory(self);
2087
2088 (*self->mode_func_ptr)(self);
2089
2090 return (PyObject *)self;
2091 }
2092
2093 static PyObject * IRFM_getServer(IRFM* self) { GET_SERVER };
2094 static PyObject * IRFM_getStream(IRFM* self) { GET_STREAM };
2095 static PyObject * IRFM_setMul(IRFM *self, PyObject *arg) { SET_MUL };
2096 static PyObject * IRFM_setAdd(IRFM *self, PyObject *arg) { SET_ADD };
2097 static PyObject * IRFM_setSub(IRFM *self, PyObject *arg) { SET_SUB };
2098 static PyObject * IRFM_setDiv(IRFM *self, PyObject *arg) { SET_DIV };
2099
2100 static PyObject * IRFM_play(IRFM *self, PyObject *args, PyObject *kwds) { PLAY };
2101 static PyObject * IRFM_out(IRFM *self, PyObject *args, PyObject *kwds) { OUT };
2102 static PyObject * IRFM_stop(IRFM *self, PyObject *args, PyObject *kwds) { STOP };
2103
2104 static PyObject * IRFM_multiply(IRFM *self, PyObject *arg) { MULTIPLY };
2105 static PyObject * IRFM_inplace_multiply(IRFM *self, PyObject *arg) { INPLACE_MULTIPLY };
2106 static PyObject * IRFM_add(IRFM *self, PyObject *arg) { ADD };
2107 static PyObject * IRFM_inplace_add(IRFM *self, PyObject *arg) { INPLACE_ADD };
2108 static PyObject * IRFM_sub(IRFM *self, PyObject *arg) { SUB };
2109 static PyObject * IRFM_inplace_sub(IRFM *self, PyObject *arg) { INPLACE_SUB };
2110 static PyObject * IRFM_div(IRFM *self, PyObject *arg) { DIV };
2111 static PyObject * IRFM_inplace_div(IRFM *self, PyObject *arg) { INPLACE_DIV };
2112
2113 static PyObject * IRFM_setCarrier(IRFM *self, PyObject *arg) { SET_PARAM(self->carrier, self->carrier_stream, 2); }
2114 static PyObject * IRFM_setRatio(IRFM *self, PyObject *arg) { SET_PARAM(self->ratio, self->ratio_stream, 3); }
2115 static PyObject * IRFM_setIndex(IRFM *self, PyObject *arg) { SET_PARAM(self->index, self->index_stream, 4); }
2116
2117 static PyMemberDef IRFM_members[] =
2118 {
2119 {"server", T_OBJECT_EX, offsetof(IRFM, server), 0, "Pyo server."},
2120 {"stream", T_OBJECT_EX, offsetof(IRFM, stream), 0, "Stream object."},
2121 {"input", T_OBJECT_EX, offsetof(IRFM, input), 0, "Input sound object."},
2122 {"carrier", T_OBJECT_EX, offsetof(IRFM, carrier), 0, "Carrier frequency."},
2123 {"ratio", T_OBJECT_EX, offsetof(IRFM, ratio), 0, "Modulator / carrier ratio."},
2124 {"index", T_OBJECT_EX, offsetof(IRFM, index), 0, "Modulation index."},
2125 {"mul", T_OBJECT_EX, offsetof(IRFM, mul), 0, "Mul factor."},
2126 {"add", T_OBJECT_EX, offsetof(IRFM, add), 0, "Add factor."},
2127 {NULL} /* Sentinel */
2128 };
2129
2130 static PyMethodDef IRFM_methods[] =
2131 {
2132 {"getServer", (PyCFunction)IRFM_getServer, METH_NOARGS, "Returns server object."},
2133 {"_getStream", (PyCFunction)IRFM_getStream, METH_NOARGS, "Returns stream object."},
2134 {"play", (PyCFunction)IRFM_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2135 {"out", (PyCFunction)IRFM_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2136 {"stop", (PyCFunction)IRFM_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2137 {"setCarrier", (PyCFunction)IRFM_setCarrier, METH_O, "Sets carrier frequency."},
2138 {"setRatio", (PyCFunction)IRFM_setRatio, METH_O, "Sets ratio."},
2139 {"setIndex", (PyCFunction)IRFM_setIndex, METH_O, "Sets the modulation index."},
2140 {"setMul", (PyCFunction)IRFM_setMul, METH_O, "Sets mul factor."},
2141 {"setAdd", (PyCFunction)IRFM_setAdd, METH_O, "Sets add factor."},
2142 {"setSub", (PyCFunction)IRFM_setSub, METH_O, "Sets inverse add factor."},
2143 {"setDiv", (PyCFunction)IRFM_setDiv, METH_O, "Sets inverse mul factor."},
2144 {NULL} /* Sentinel */
2145 };
2146
2147 static PyNumberMethods IRFM_as_number =
2148 {
2149 (binaryfunc)IRFM_add, /*nb_add*/
2150 (binaryfunc)IRFM_sub, /*nb_subtract*/
2151 (binaryfunc)IRFM_multiply, /*nb_multiply*/
2152 0, /*nb_remainder*/
2153 0, /*nb_divmod*/
2154 0, /*nb_power*/
2155 0, /*nb_neg*/
2156 0, /*nb_pos*/
2157 0, /*(unaryfunc)array_abs,*/
2158 0, /*nb_nonzero*/
2159 0, /*nb_invert*/
2160 0, /*nb_lshift*/
2161 0, /*nb_rshift*/
2162 0, /*nb_and*/
2163 0, /*nb_xor*/
2164 0, /*nb_or*/
2165 0, /*nb_int*/
2166 0, /*nb_long*/
2167 0, /*nb_float*/
2168 (binaryfunc)IRFM_inplace_add, /*inplace_add*/
2169 (binaryfunc)IRFM_inplace_sub, /*inplace_subtract*/
2170 (binaryfunc)IRFM_inplace_multiply, /*inplace_multiply*/
2171 0, /*inplace_remainder*/
2172 0, /*inplace_power*/
2173 0, /*inplace_lshift*/
2174 0, /*inplace_rshift*/
2175 0, /*inplace_and*/
2176 0, /*inplace_xor*/
2177 0, /*inplace_or*/
2178 0, /*nb_floor_divide*/
2179 (binaryfunc)IRFM_div, /*nb_true_divide*/
2180 0, /*nb_inplace_floor_divide*/
2181 (binaryfunc)IRFM_inplace_div, /*nb_inplace_true_divide*/
2182 0, /* nb_index */
2183 };
2184
2185 PyTypeObject IRFMType =
2186 {
2187 PyVarObject_HEAD_INIT(NULL, 0)
2188 "_pyo.IRFM_base", /*tp_name*/
2189 sizeof(IRFM), /*tp_basicsize*/
2190 0, /*tp_itemsize*/
2191 (destructor)IRFM_dealloc, /*tp_dealloc*/
2192 0, /*tp_print*/
2193 0, /*tp_getattr*/
2194 0, /*tp_setattr*/
2195 0, /*tp_as_async (tp_compare in Python 2)*/
2196 0, /*tp_repr*/
2197 &IRFM_as_number, /*tp_as_number*/
2198 0, /*tp_as_sequence*/
2199 0, /*tp_as_mapping*/
2200 0, /*tp_hash */
2201 0, /*tp_call*/
2202 0, /*tp_str*/
2203 0, /*tp_getattro*/
2204 0, /*tp_setattro*/
2205 0, /*tp_as_buffer*/
2206 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2207 "IRFM objects. Convolve an input signal with a frequency modulation spectrum.", /* tp_doc */
2208 (traverseproc)IRFM_traverse, /* tp_traverse */
2209 (inquiry)IRFM_clear, /* tp_clear */
2210 0, /* tp_richcompare */
2211 0, /* tp_weaklistoffset */
2212 0, /* tp_iter */
2213 0, /* tp_iternext */
2214 IRFM_methods, /* tp_methods */
2215 IRFM_members, /* tp_members */
2216 0, /* tp_getset */
2217 0, /* tp_base */
2218 0, /* tp_dict */
2219 0, /* tp_descr_get */
2220 0, /* tp_descr_set */
2221 0, /* tp_dictoffset */
2222 0, /* tp_init */
2223 0, /* tp_alloc */
2224 IRFM_new, /* tp_new */
2225 };