Debian Codegraph @

src/objects/randommodule.c

raw ◀ ▶
1 Debian patch touch this file
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #include <Python.h>
22 #include <math.h>
23 #include "structmember.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 *min;
33 PyObject *max;
34 PyObject *freq;
35 Stream *min_stream;
36 Stream *max_stream;
37 Stream *freq_stream;
38 MYFLT value;
39 MYFLT oldValue;
40 MYFLT diff;
41 MYFLT time;
42 int modebuffer[5]; // need at least 2 slots for mul & add
43 } Randi;
44
45 static void
46 Randi_generate_iii(Randi *self)
47 {
48 int i;
49 MYFLT inc;
50 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
51 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
52 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
53 MYFLT range = ma - mi;
54 inc = fr / self->sr;
55
56 for (i = 0; i < self->bufsize; i++)
57 {
58 self->time += inc;
59
60 if (self->time < 0.0)
61 self->time += 1.0;
62 else if (self->time >= 1.0)
63 {
64 self->time -= 1.0;
65 self->oldValue = self->value;
66 self->value = range * RANDOM_UNIFORM + mi;
67 self->diff = self->value - self->oldValue;
68 }
69
70 self->data[i] = self->oldValue + self->diff * self->time;
71 }
72 }
73
74 static void
75 Randi_generate_aii(Randi *self)
76 {
77 int i;
78 MYFLT inc, range;
79 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
80 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
81 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
82 inc = fr / self->sr;
83
84 for (i = 0; i < self->bufsize; i++)
85 {
86 range = ma - mi[i];
87 self->time += inc;
88
89 if (self->time < 0.0)
90 self->time += 1.0;
91 else if (self->time >= 1.0)
92 {
93 self->time -= 1.0;
94 self->oldValue = self->value;
95 self->value = range * RANDOM_UNIFORM + mi[i];
96 self->diff = self->value - self->oldValue;
97 }
98
99 self->data[i] = self->oldValue + self->diff * self->time;
100 }
101 }
102
103 static void
104 Randi_generate_iai(Randi *self)
105 {
106 int i;
107 MYFLT inc, range;
108 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
109 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
110 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
111 inc = fr / self->sr;
112
113 for (i = 0; i < self->bufsize; i++)
114 {
115 range = ma[i] - mi;
116 self->time += inc;
117
118 if (self->time < 0.0)
119 self->time += 1.0;
120 else if (self->time >= 1.0)
121 {
122 self->time -= 1.0;
123 self->oldValue = self->value;
124 self->value = range * RANDOM_UNIFORM + mi;
125 self->diff = self->value - self->oldValue;
126 }
127
128 self->data[i] = self->oldValue + self->diff * self->time;
129 }
130 }
131
132 static void
133 Randi_generate_aai(Randi *self)
134 {
135 int i;
136 MYFLT inc, range;
137 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
138 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
139 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
140 inc = fr / self->sr;
141
142 for (i = 0; i < self->bufsize; i++)
143 {
144 range = ma[i] - mi[i];
145 self->time += inc;
146
147 if (self->time < 0.0)
148 self->time += 1.0;
149 else if (self->time >= 1.0)
150 {
151 self->time -= 1.0;
152 self->oldValue = self->value;
153 self->value = range * RANDOM_UNIFORM + mi[i];
154 self->diff = self->value - self->oldValue;
155 }
156
157 self->data[i] = self->oldValue + self->diff * self->time;
158 }
159 }
160
161 static void
162 Randi_generate_iia(Randi *self)
163 {
164 int i;
165 MYFLT inc;
166 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
167 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
168 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
169 MYFLT range = ma - mi;
170
171 for (i = 0; i < self->bufsize; i++)
172 {
173 inc = fr[i] / self->sr;
174 self->time += inc;
175
176 if (self->time < 0.0)
177 self->time += 1.0;
178 else if (self->time >= 1.0)
179 {
180 self->time -= 1.0;
181 self->oldValue = self->value;
182 self->value = range * RANDOM_UNIFORM + mi;
183 self->diff = self->value - self->oldValue;
184 }
185
186 self->data[i] = self->oldValue + self->diff * self->time;
187 }
188 }
189
190 static void
191 Randi_generate_aia(Randi *self)
192 {
193 int i;
194 MYFLT inc, range;
195 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
196 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
197 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
198
199 for (i = 0; i < self->bufsize; i++)
200 {
201 inc = fr[i] / self->sr;
202 range = ma - mi[i];
203 self->time += inc;
204
205 if (self->time < 0.0)
206 self->time += 1.0;
207 else if (self->time >= 1.0)
208 {
209 self->time -= 1.0;
210 self->oldValue = self->value;
211 self->value = range * RANDOM_UNIFORM + mi[i];
212 self->diff = self->value - self->oldValue;
213 }
214
215 self->data[i] = self->oldValue + self->diff * self->time;
216 }
217 }
218
219 static void
220 Randi_generate_iaa(Randi *self)
221 {
222 int i;
223 MYFLT inc, range;
224 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
225 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
226 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
227
228 for (i = 0; i < self->bufsize; i++)
229 {
230 inc = fr[i] / self->sr;
231 range = ma[i] - mi;
232 self->time += inc;
233
234 if (self->time < 0.0)
235 self->time += 1.0;
236 else if (self->time >= 1.0)
237 {
238 self->time -= 1.0;
239 self->oldValue = self->value;
240 self->value = range * RANDOM_UNIFORM + mi;
241 self->diff = self->value - self->oldValue;
242 }
243
244 self->data[i] = self->oldValue + self->diff * self->time;
245 }
246 }
247
248 static void
249 Randi_generate_aaa(Randi *self)
250 {
251 int i;
252 MYFLT inc, range;
253 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
254 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
255 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
256
257 for (i = 0; i < self->bufsize; i++)
258 {
259 inc = fr[i] / self->sr;
260 range = ma[i] - mi[i];
261 self->time += inc;
262
263 if (self->time < 0.0)
264 self->time += 1.0;
265 else if (self->time >= 1.0)
266 {
267 self->time -= 1.0;
268 self->oldValue = self->value;
269 self->value = range * RANDOM_UNIFORM + mi[i];
270 self->diff = self->value - self->oldValue;
271 }
272
273 self->data[i] = self->oldValue + self->diff * self->time;
274 }
275 }
276
277 static void Randi_postprocessing_ii(Randi *self) { POST_PROCESSING_II };
278 static void Randi_postprocessing_ai(Randi *self) { POST_PROCESSING_AI };
279 static void Randi_postprocessing_ia(Randi *self) { POST_PROCESSING_IA };
280 static void Randi_postprocessing_aa(Randi *self) { POST_PROCESSING_AA };
281 static void Randi_postprocessing_ireva(Randi *self) { POST_PROCESSING_IREVA };
282 static void Randi_postprocessing_areva(Randi *self) { POST_PROCESSING_AREVA };
283 static void Randi_postprocessing_revai(Randi *self) { POST_PROCESSING_REVAI };
284 static void Randi_postprocessing_revaa(Randi *self) { POST_PROCESSING_REVAA };
285 static void Randi_postprocessing_revareva(Randi *self) { POST_PROCESSING_REVAREVA };
286
287 static void
288 Randi_setProcMode(Randi *self)
289 {
290 int procmode, muladdmode;
291 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
292 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
293
294 switch (procmode)
295 {
296 case 0:
297 self->proc_func_ptr = Randi_generate_iii;
298 break;
299
300 case 1:
301 self->proc_func_ptr = Randi_generate_aii;
302 break;
303
304 case 10:
305 self->proc_func_ptr = Randi_generate_iai;
306 break;
307
308 case 11:
309 self->proc_func_ptr = Randi_generate_aai;
310 break;
311
312 case 100:
313 self->proc_func_ptr = Randi_generate_iia;
314 break;
315
316 case 101:
317 self->proc_func_ptr = Randi_generate_aia;
318 break;
319
320 case 110:
321 self->proc_func_ptr = Randi_generate_iaa;
322 break;
323
324 case 111:
325 self->proc_func_ptr = Randi_generate_aaa;
326 break;
327 }
328
329 switch (muladdmode)
330 {
331 case 0:
332 self->muladd_func_ptr = Randi_postprocessing_ii;
333 break;
334
335 case 1:
336 self->muladd_func_ptr = Randi_postprocessing_ai;
337 break;
338
339 case 2:
340 self->muladd_func_ptr = Randi_postprocessing_revai;
341 break;
342
343 case 10:
344 self->muladd_func_ptr = Randi_postprocessing_ia;
345 break;
346
347 case 11:
348 self->muladd_func_ptr = Randi_postprocessing_aa;
349 break;
350
351 case 12:
352 self->muladd_func_ptr = Randi_postprocessing_revaa;
353 break;
354
355 case 20:
356 self->muladd_func_ptr = Randi_postprocessing_ireva;
357 break;
358
359 case 21:
360 self->muladd_func_ptr = Randi_postprocessing_areva;
361 break;
362
363 case 22:
364 self->muladd_func_ptr = Randi_postprocessing_revareva;
365 break;
366 }
367 }
368
369 static void
370 Randi_compute_next_data_frame(Randi *self)
371 {
372 (*self->proc_func_ptr)(self);
373 (*self->muladd_func_ptr)(self);
374 }
375
376 static int
377 Randi_traverse(Randi *self, visitproc visit, void *arg)
378 {
379 pyo_VISIT
380 Py_VISIT(self->freq);
381 Py_VISIT(self->min);
382 Py_VISIT(self->max);
383 return 0;
384 }
385
386 static int
387 Randi_clear(Randi *self)
388 {
389 pyo_CLEAR
390 Py_CLEAR(self->freq);
391 Py_CLEAR(self->min);
392 Py_CLEAR(self->max);
393 return 0;
394 }
395
396 static void
397 Randi_dealloc(Randi* self)
398 {
399 pyo_DEALLOC
400 Randi_clear(self);
401 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
402 Py_TYPE(self)->tp_free((PyObject*)self);
403 }
404
405 static PyObject *
406 Randi_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
407 {
408 int i;
409 MYFLT mi, ma;
410 PyObject *mintmp = NULL, *maxtmp = NULL, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
411 Randi *self;
412 self = (Randi *)type->tp_alloc(type, 0);
413
414 self->min = PyFloat_FromDouble(0.);
415 self->max = PyFloat_FromDouble(1.);
416 self->freq = PyFloat_FromDouble(1.);
417 self->value = self->oldValue = self->diff = 0.0;
418 self->time = 1.0;
419 self->modebuffer[0] = 0;
420 self->modebuffer[1] = 0;
421 self->modebuffer[2] = 0;
422 self->modebuffer[3] = 0;
423 self->modebuffer[4] = 0;
424
425 INIT_OBJECT_COMMON
426 Stream_setFunctionPtr(self->stream, Randi_compute_next_data_frame);
427 self->mode_func_ptr = Randi_setProcMode;
428
429 static char *kwlist[] = {"min", "max", "freq", "mul", "add", NULL};
430
431 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &mintmp, &maxtmp, &freqtmp, &multmp, &addtmp))
432 Py_RETURN_NONE;
433
434 if (mintmp)
435 {
436 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
437 }
438
439 if (maxtmp)
440 {
441 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
442 }
443
444 if (freqtmp)
445 {
446 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
447 }
448
449 if (multmp)
450 {
451 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
452 }
453
454 if (addtmp)
455 {
456 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
457 }
458
459 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
460
461 Server_generateSeed((Server *)self->server, RANDI_ID);
462
463 if (self->modebuffer[2] == 0)
464 mi = PyFloat_AS_DOUBLE(self->min);
465 else
466 mi = Stream_getData((Stream *)self->min_stream)[0];
467
468 if (self->modebuffer[3] == 0)
469 ma = PyFloat_AS_DOUBLE(self->max);
470 else
471 ma = Stream_getData((Stream *)self->max_stream)[0];
472
473 self->value = self->oldValue = (mi + ma) * 0.5;
474
475 (*self->mode_func_ptr)(self);
476
477 return (PyObject *)self;
478 }
479
480 static PyObject * Randi_getServer(Randi* self) { GET_SERVER };
481 static PyObject * Randi_getStream(Randi* self) { GET_STREAM };
482 static PyObject * Randi_setMul(Randi *self, PyObject *arg) { SET_MUL };
483 static PyObject * Randi_setAdd(Randi *self, PyObject *arg) { SET_ADD };
484 static PyObject * Randi_setSub(Randi *self, PyObject *arg) { SET_SUB };
485 static PyObject * Randi_setDiv(Randi *self, PyObject *arg) { SET_DIV };
486
487 static PyObject * Randi_play(Randi *self, PyObject *args, PyObject *kwds) { PLAY };
488 static PyObject * Randi_out(Randi *self, PyObject *args, PyObject *kwds) { OUT };
489 static PyObject * Randi_stop(Randi *self, PyObject *args, PyObject *kwds) { STOP };
490
491 static PyObject * Randi_multiply(Randi *self, PyObject *arg) { MULTIPLY };
492 static PyObject * Randi_inplace_multiply(Randi *self, PyObject *arg) { INPLACE_MULTIPLY };
493 static PyObject * Randi_add(Randi *self, PyObject *arg) { ADD };
494 static PyObject * Randi_inplace_add(Randi *self, PyObject *arg) { INPLACE_ADD };
495 static PyObject * Randi_sub(Randi *self, PyObject *arg) { SUB };
496 static PyObject * Randi_inplace_sub(Randi *self, PyObject *arg) { INPLACE_SUB };
497 static PyObject * Randi_div(Randi *self, PyObject *arg) { DIV };
498 static PyObject * Randi_inplace_div(Randi *self, PyObject *arg) { INPLACE_DIV };
499
500 static PyObject * Randi_setMin(Randi *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
501 static PyObject * Randi_setMax(Randi *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
502 static PyObject * Randi_setFreq(Randi *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 4); }
503
504 static PyMemberDef Randi_members[] =
505 {
506 {"server", T_OBJECT_EX, offsetof(Randi, server), 0, "Pyo server."},
507 {"stream", T_OBJECT_EX, offsetof(Randi, stream), 0, "Stream object."},
508 {"min", T_OBJECT_EX, offsetof(Randi, min), 0, "Minimum possible value."},
509 {"max", T_OBJECT_EX, offsetof(Randi, max), 0, "Maximum possible value."},
510 {"freq", T_OBJECT_EX, offsetof(Randi, freq), 0, "Polling frequency."},
511 {"mul", T_OBJECT_EX, offsetof(Randi, mul), 0, "Mul factor."},
512 {"add", T_OBJECT_EX, offsetof(Randi, add), 0, "Add factor."},
513 {NULL} /* Sentinel */
514 };
515
516 static PyMethodDef Randi_methods[] =
517 {
518 {"getServer", (PyCFunction)Randi_getServer, METH_NOARGS, "Returns server object."},
519 {"_getStream", (PyCFunction)Randi_getStream, METH_NOARGS, "Returns stream object."},
520 {"play", (PyCFunction)Randi_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
521 {"out", (PyCFunction)Randi_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
522 {"stop", (PyCFunction)Randi_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
523 {"setMin", (PyCFunction)Randi_setMin, METH_O, "Sets minimum possible value."},
524 {"setMax", (PyCFunction)Randi_setMax, METH_O, "Sets maximum possible value."},
525 {"setFreq", (PyCFunction)Randi_setFreq, METH_O, "Sets polling frequency."},
526 {"setMul", (PyCFunction)Randi_setMul, METH_O, "Sets oscillator mul factor."},
527 {"setAdd", (PyCFunction)Randi_setAdd, METH_O, "Sets oscillator add factor."},
528 {"setSub", (PyCFunction)Randi_setSub, METH_O, "Sets inverse add factor."},
529 {"setDiv", (PyCFunction)Randi_setDiv, METH_O, "Sets inverse mul factor."},
530 {NULL} /* Sentinel */
531 };
532
533 static PyNumberMethods Randi_as_number =
534 {
535 (binaryfunc)Randi_add, /*nb_add*/
536 (binaryfunc)Randi_sub, /*nb_subtract*/
537 (binaryfunc)Randi_multiply, /*nb_multiply*/
538 0, /*nb_remainder*/
539 0, /*nb_divmod*/
540 0, /*nb_power*/
541 0, /*nb_neg*/
542 0, /*nb_pos*/
543 0, /*(unaryfunc)array_abs,*/
544 0, /*nb_nonzero*/
545 0, /*nb_invert*/
546 0, /*nb_lshift*/
547 0, /*nb_rshift*/
548 0, /*nb_and*/
549 0, /*nb_xor*/
550 0, /*nb_or*/
551 0, /*nb_int*/
552 0, /*nb_long*/
553 0, /*nb_float*/
554 (binaryfunc)Randi_inplace_add, /*inplace_add*/
555 (binaryfunc)Randi_inplace_sub, /*inplace_subtract*/
556 (binaryfunc)Randi_inplace_multiply, /*inplace_multiply*/
557 0, /*inplace_remainder*/
558 0, /*inplace_power*/
559 0, /*inplace_lshift*/
560 0, /*inplace_rshift*/
561 0, /*inplace_and*/
562 0, /*inplace_xor*/
563 0, /*inplace_or*/
564 0, /*nb_floor_divide*/
565 (binaryfunc)Randi_div, /*nb_true_divide*/
566 0, /*nb_inplace_floor_divide*/
567 (binaryfunc)Randi_inplace_div, /*nb_inplace_true_divide*/
568 0, /* nb_index */
569 };
570
571 PyTypeObject RandiType =
572 {
573 PyVarObject_HEAD_INIT(NULL, 0)
574 "_pyo.Randi_base", /*tp_name*/
575 sizeof(Randi), /*tp_basicsize*/
576 0, /*tp_itemsize*/
577 (destructor)Randi_dealloc, /*tp_dealloc*/
578 0, /*tp_print*/
579 0, /*tp_getattr*/
580 0, /*tp_setattr*/
581 0, /*tp_as_async (tp_compare in Python 2)*/
582 0, /*tp_repr*/
583 &Randi_as_number, /*tp_as_number*/
584 0, /*tp_as_sequence*/
585 0, /*tp_as_mapping*/
586 0, /*tp_hash */
587 0, /*tp_call*/
588 0, /*tp_str*/
589 0, /*tp_getattro*/
590 0, /*tp_setattro*/
591 0, /*tp_as_buffer*/
592 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
593 "Randi objects. Periodically generates a new random value with interpolation.", /* tp_doc */
594 (traverseproc)Randi_traverse, /* tp_traverse */
595 (inquiry)Randi_clear, /* tp_clear */
596 0, /* tp_richcompare */
597 0, /* tp_weaklistoffset */
598 0, /* tp_iter */
599 0, /* tp_iternext */
600 Randi_methods, /* tp_methods */
601 Randi_members, /* tp_members */
602 0, /* tp_getset */
603 0, /* tp_base */
604 0, /* tp_dict */
605 0, /* tp_descr_get */
606 0, /* tp_descr_set */
607 0, /* tp_dictoffset */
608 0, /* tp_init */
609 0, /* tp_alloc */
610 Randi_new, /* tp_new */
611 };
612
613 /****************/
614 /**** Randh *****/
615 /****************/
616 typedef struct
617 {
618 pyo_audio_HEAD
619 PyObject *min;
620 PyObject *max;
621 PyObject *freq;
622 Stream *min_stream;
623 Stream *max_stream;
624 Stream *freq_stream;
625 MYFLT value;
626 MYFLT time;
627 int modebuffer[5]; // need at least 2 slots for mul & add
628 } Randh;
629
630 static void
631 Randh_generate_iii(Randh *self)
632 {
633 int i;
634 MYFLT inc;
635 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
636 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
637 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
638 MYFLT range = ma - mi;
639 inc = fr / self->sr;
640
641 for (i = 0; i < self->bufsize; i++)
642 {
643 self->time += inc;
644
645 if (self->time < 0.0)
646 self->time += 1.0;
647 else if (self->time >= 1.0)
648 {
649 self->time -= 1.0;
650 self->value = range * RANDOM_UNIFORM + mi;
651 }
652
653 self->data[i] = self->value;
654 }
655 }
656
657 static void
658 Randh_generate_aii(Randh *self)
659 {
660 int i;
661 MYFLT inc, range;
662 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
663 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
664 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
665 inc = fr / self->sr;
666
667 for (i = 0; i < self->bufsize; i++)
668 {
669 range = ma - mi[i];
670 self->time += inc;
671
672 if (self->time < 0.0)
673 self->time += 1.0;
674 else if (self->time >= 1.0)
675 {
676 self->time -= 1.0;
677 self->value = range * RANDOM_UNIFORM + mi[i];
678 }
679
680 self->data[i] = self->value;
681 }
682 }
683
684 static void
685 Randh_generate_iai(Randh *self)
686 {
687 int i;
688 MYFLT inc, range;
689 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
690 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
691 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
692 inc = fr / self->sr;
693
694 for (i = 0; i < self->bufsize; i++)
695 {
696 range = ma[i] - mi;
697 self->time += inc;
698
699 if (self->time < 0.0)
700 self->time += 1.0;
701 else if (self->time >= 1.0)
702 {
703 self->time -= 1.0;
704 self->value = range * RANDOM_UNIFORM + mi;
705 }
706
707 self->data[i] = self->value;
708 }
709 }
710
711 static void
712 Randh_generate_aai(Randh *self)
713 {
714 int i;
715 MYFLT inc, range;
716 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
717 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
718 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
719 inc = fr / self->sr;
720
721 for (i = 0; i < self->bufsize; i++)
722 {
723 range = ma[i] - mi[i];
724 self->time += inc;
725
726 if (self->time < 0.0)
727 self->time += 1.0;
728 else if (self->time >= 1.0)
729 {
730 self->time -= 1.0;
731 self->value = range * RANDOM_UNIFORM + mi[i];
732 }
733
734 self->data[i] = self->value;
735 }
736 }
737
738 static void
739 Randh_generate_iia(Randh *self)
740 {
741 int i;
742 MYFLT inc;
743 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
744 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
745 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
746 MYFLT range = ma - mi;
747
748 for (i = 0; i < self->bufsize; i++)
749 {
750 inc = fr[i] / self->sr;
751 self->time += inc;
752
753 if (self->time < 0.0)
754 self->time += 1.0;
755 else if (self->time >= 1.0)
756 {
757 self->time -= 1.0;
758 self->value = range * RANDOM_UNIFORM + mi;
759 }
760
761 self->data[i] = self->value;
762 }
763 }
764
765 static void
766 Randh_generate_aia(Randh *self)
767 {
768 int i;
769 MYFLT inc, range;
770 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
771 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
772 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
773
774 for (i = 0; i < self->bufsize; i++)
775 {
776 inc = fr[i] / self->sr;
777 range = ma - mi[i];
778 self->time += inc;
779
780 if (self->time < 0.0)
781 self->time += 1.0;
782 else if (self->time >= 1.0)
783 {
784 self->time -= 1.0;
785 self->value = range * RANDOM_UNIFORM + mi[i];
786 }
787
788 self->data[i] = self->value;
789 }
790 }
791
792 static void
793 Randh_generate_iaa(Randh *self)
794 {
795 int i;
796 MYFLT inc, range;
797 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
798 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
799 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
800
801 for (i = 0; i < self->bufsize; i++)
802 {
803 inc = fr[i] / self->sr;
804 range = ma[i] - mi;
805 self->time += inc;
806
807 if (self->time < 0.0)
808 self->time += 1.0;
809 else if (self->time >= 1.0)
810 {
811 self->time -= 1.0;
812 self->value = range * RANDOM_UNIFORM + mi;
813 }
814
815 self->data[i] = self->value;
816 }
817 }
818
819 static void
820 Randh_generate_aaa(Randh *self)
821 {
822 int i;
823 MYFLT inc, range;
824 MYFLT *mi = Stream_getData((Stream *)self->min_stream);
825 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
826 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
827
828 for (i = 0; i < self->bufsize; i++)
829 {
830 inc = fr[i] / self->sr;
831 range = ma[i] - mi[i];
832 self->time += inc;
833
834 if (self->time < 0.0)
835 self->time += 1.0;
836 else if (self->time >= 1.0)
837 {
838 self->time -= 1.0;
839 self->value = range * RANDOM_UNIFORM + mi[i];
840 }
841
842 self->data[i] = self->value;
843 }
844 }
845
846 static void Randh_postprocessing_ii(Randh *self) { POST_PROCESSING_II };
847 static void Randh_postprocessing_ai(Randh *self) { POST_PROCESSING_AI };
848 static void Randh_postprocessing_ia(Randh *self) { POST_PROCESSING_IA };
849 static void Randh_postprocessing_aa(Randh *self) { POST_PROCESSING_AA };
850 static void Randh_postprocessing_ireva(Randh *self) { POST_PROCESSING_IREVA };
851 static void Randh_postprocessing_areva(Randh *self) { POST_PROCESSING_AREVA };
852 static void Randh_postprocessing_revai(Randh *self) { POST_PROCESSING_REVAI };
853 static void Randh_postprocessing_revaa(Randh *self) { POST_PROCESSING_REVAA };
854 static void Randh_postprocessing_revareva(Randh *self) { POST_PROCESSING_REVAREVA };
855
856 static void
857 Randh_setProcMode(Randh *self)
858 {
859 int procmode, muladdmode;
860 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
861 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
862
863 switch (procmode)
864 {
865 case 0:
866 self->proc_func_ptr = Randh_generate_iii;
867 break;
868
869 case 1:
870 self->proc_func_ptr = Randh_generate_aii;
871 break;
872
873 case 10:
874 self->proc_func_ptr = Randh_generate_iai;
875 break;
876
877 case 11:
878 self->proc_func_ptr = Randh_generate_aai;
879 break;
880
881 case 100:
882 self->proc_func_ptr = Randh_generate_iia;
883 break;
884
885 case 101:
886 self->proc_func_ptr = Randh_generate_aia;
887 break;
888
889 case 110:
890 self->proc_func_ptr = Randh_generate_iaa;
891 break;
892
893 case 111:
894 self->proc_func_ptr = Randh_generate_aaa;
895 break;
896 }
897
898 switch (muladdmode)
899 {
900 case 0:
901 self->muladd_func_ptr = Randh_postprocessing_ii;
902 break;
903
904 case 1:
905 self->muladd_func_ptr = Randh_postprocessing_ai;
906 break;
907
908 case 2:
909 self->muladd_func_ptr = Randh_postprocessing_revai;
910 break;
911
912 case 10:
913 self->muladd_func_ptr = Randh_postprocessing_ia;
914 break;
915
916 case 11:
917 self->muladd_func_ptr = Randh_postprocessing_aa;
918 break;
919
920 case 12:
921 self->muladd_func_ptr = Randh_postprocessing_revaa;
922 break;
923
924 case 20:
925 self->muladd_func_ptr = Randh_postprocessing_ireva;
926 break;
927
928 case 21:
929 self->muladd_func_ptr = Randh_postprocessing_areva;
930 break;
931
932 case 22:
933 self->muladd_func_ptr = Randh_postprocessing_revareva;
934 break;
935 }
936 }
937
938 static void
939 Randh_compute_next_data_frame(Randh *self)
940 {
941 (*self->proc_func_ptr)(self);
942 (*self->muladd_func_ptr)(self);
943 }
944
945 static int
946 Randh_traverse(Randh *self, visitproc visit, void *arg)
947 {
948 pyo_VISIT
949 Py_VISIT(self->freq);
950 Py_VISIT(self->min);
951 Py_VISIT(self->max);
952 return 0;
953 }
954
955 static int
956 Randh_clear(Randh *self)
957 {
958 pyo_CLEAR
959 Py_CLEAR(self->freq);
960 Py_CLEAR(self->min);
961 Py_CLEAR(self->max);
962 return 0;
963 }
964
965 static void
966 Randh_dealloc(Randh* self)
967 {
968 pyo_DEALLOC
969 Randh_clear(self);
970 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
971 Py_TYPE(self)->tp_free((PyObject*)self);
972 }
973
974 static PyObject *
975 Randh_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
976 {
977 int i;
978 MYFLT mi, ma;
979 PyObject *mintmp = NULL, *maxtmp = NULL, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
980 Randh *self;
981 self = (Randh *)type->tp_alloc(type, 0);
982
983 self->min = PyFloat_FromDouble(0.);
984 self->max = PyFloat_FromDouble(1.);
985 self->freq = PyFloat_FromDouble(1.);
986 self->value = 0.0;
987 self->time = 1.0;
988 self->modebuffer[0] = 0;
989 self->modebuffer[1] = 0;
990 self->modebuffer[2] = 0;
991 self->modebuffer[3] = 0;
992 self->modebuffer[4] = 0;
993
994 INIT_OBJECT_COMMON
995 Stream_setFunctionPtr(self->stream, Randh_compute_next_data_frame);
996 self->mode_func_ptr = Randh_setProcMode;
997
998 static char *kwlist[] = {"min", "max", "freq", "mul", "add", NULL};
999
1000 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &mintmp, &maxtmp, &freqtmp, &multmp, &addtmp))
1001 Py_RETURN_NONE;
1002
1003 if (mintmp)
1004 {
1005 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
1006 }
1007
1008 if (maxtmp)
1009 {
1010 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
1011 }
1012
1013 if (freqtmp)
1014 {
1015 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1016 }
1017
1018 if (multmp)
1019 {
1020 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1021 }
1022
1023 if (addtmp)
1024 {
1025 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1026 }
1027
1028 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1029
1030 Server_generateSeed((Server *)self->server, RANDH_ID);
1031
1032 if (self->modebuffer[2] == 0)
1033 mi = PyFloat_AS_DOUBLE(self->min);
1034 else
1035 mi = Stream_getData((Stream *)self->min_stream)[0];
1036
1037 if (self->modebuffer[3] == 0)
1038 ma = PyFloat_AS_DOUBLE(self->max);
1039 else
1040 ma = Stream_getData((Stream *)self->max_stream)[0];
1041
1042 self->value = (mi + ma) * 0.5;
1043
1044 (*self->mode_func_ptr)(self);
1045
1046 return (PyObject *)self;
1047 }
1048
1049 static PyObject * Randh_getServer(Randh* self) { GET_SERVER };
1050 static PyObject * Randh_getStream(Randh* self) { GET_STREAM };
1051 static PyObject * Randh_setMul(Randh *self, PyObject *arg) { SET_MUL };
1052 static PyObject * Randh_setAdd(Randh *self, PyObject *arg) { SET_ADD };
1053 static PyObject * Randh_setSub(Randh *self, PyObject *arg) { SET_SUB };
1054 static PyObject * Randh_setDiv(Randh *self, PyObject *arg) { SET_DIV };
1055
1056 static PyObject * Randh_play(Randh *self, PyObject *args, PyObject *kwds) { PLAY };
1057 static PyObject * Randh_out(Randh *self, PyObject *args, PyObject *kwds) { OUT };
1058 static PyObject * Randh_stop(Randh *self, PyObject *args, PyObject *kwds) { STOP };
1059
1060 static PyObject * Randh_multiply(Randh *self, PyObject *arg) { MULTIPLY };
1061 static PyObject * Randh_inplace_multiply(Randh *self, PyObject *arg) { INPLACE_MULTIPLY };
1062 static PyObject * Randh_add(Randh *self, PyObject *arg) { ADD };
1063 static PyObject * Randh_inplace_add(Randh *self, PyObject *arg) { INPLACE_ADD };
1064 static PyObject * Randh_sub(Randh *self, PyObject *arg) { SUB };
1065 static PyObject * Randh_inplace_sub(Randh *self, PyObject *arg) { INPLACE_SUB };
1066 static PyObject * Randh_div(Randh *self, PyObject *arg) { DIV };
1067 static PyObject * Randh_inplace_div(Randh *self, PyObject *arg) { INPLACE_DIV };
1068
1069 static PyObject * Randh_setMin(Randh *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
1070 static PyObject * Randh_setMax(Randh *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
1071 static PyObject * Randh_setFreq(Randh *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 4); }
1072
1073 static PyMemberDef Randh_members[] =
1074 {
1075 {"server", T_OBJECT_EX, offsetof(Randh, server), 0, "Pyo server."},
1076 {"stream", T_OBJECT_EX, offsetof(Randh, stream), 0, "Stream object."},
1077 {"min", T_OBJECT_EX, offsetof(Randh, min), 0, "Minimum possible value."},
1078 {"max", T_OBJECT_EX, offsetof(Randh, max), 0, "Maximum possible value."},
1079 {"freq", T_OBJECT_EX, offsetof(Randh, freq), 0, "Polling frequency."},
1080 {"mul", T_OBJECT_EX, offsetof(Randh, mul), 0, "Mul factor."},
1081 {"add", T_OBJECT_EX, offsetof(Randh, add), 0, "Add factor."},
1082 {NULL} /* Sentinel */
1083 };
1084
1085 static PyMethodDef Randh_methods[] =
1086 {
1087 {"getServer", (PyCFunction)Randh_getServer, METH_NOARGS, "Returns server object."},
1088 {"_getStream", (PyCFunction)Randh_getStream, METH_NOARGS, "Returns stream object."},
1089 {"play", (PyCFunction)Randh_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1090 {"out", (PyCFunction)Randh_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1091 {"stop", (PyCFunction)Randh_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1092 {"setMin", (PyCFunction)Randh_setMin, METH_O, "Sets minimum possible value."},
1093 {"setMax", (PyCFunction)Randh_setMax, METH_O, "Sets maximum possible value."},
1094 {"setFreq", (PyCFunction)Randh_setFreq, METH_O, "Sets polling frequency."},
1095 {"setMul", (PyCFunction)Randh_setMul, METH_O, "Sets oscillator mul factor."},
1096 {"setAdd", (PyCFunction)Randh_setAdd, METH_O, "Sets oscillator add factor."},
1097 {"setSub", (PyCFunction)Randh_setSub, METH_O, "Sets inverse add factor."},
1098 {"setDiv", (PyCFunction)Randh_setDiv, METH_O, "Sets inverse mul factor."},
1099 {NULL} /* Sentinel */
1100 };
1101
1102 static PyNumberMethods Randh_as_number =
1103 {
1104 (binaryfunc)Randh_add, /*nb_add*/
1105 (binaryfunc)Randh_sub, /*nb_subtract*/
1106 (binaryfunc)Randh_multiply, /*nb_multiply*/
1107 0, /*nb_remainder*/
1108 0, /*nb_divmod*/
1109 0, /*nb_power*/
1110 0, /*nb_neg*/
1111 0, /*nb_pos*/
1112 0, /*(unaryfunc)array_abs,*/
1113 0, /*nb_nonzero*/
1114 0, /*nb_invert*/
1115 0, /*nb_lshift*/
1116 0, /*nb_rshift*/
1117 0, /*nb_and*/
1118 0, /*nb_xor*/
1119 0, /*nb_or*/
1120 0, /*nb_int*/
1121 0, /*nb_long*/
1122 0, /*nb_float*/
1123 (binaryfunc)Randh_inplace_add, /*inplace_add*/
1124 (binaryfunc)Randh_inplace_sub, /*inplace_subtract*/
1125 (binaryfunc)Randh_inplace_multiply, /*inplace_multiply*/
1126 0, /*inplace_remainder*/
1127 0, /*inplace_power*/
1128 0, /*inplace_lshift*/
1129 0, /*inplace_rshift*/
1130 0, /*inplace_and*/
1131 0, /*inplace_xor*/
1132 0, /*inplace_or*/
1133 0, /*nb_floor_divide*/
1134 (binaryfunc)Randh_div, /*nb_true_divide*/
1135 0, /*nb_inplace_floor_divide*/
1136 (binaryfunc)Randh_inplace_div, /*nb_inplace_true_divide*/
1137 0, /* nb_index */
1138 };
1139
1140 PyTypeObject RandhType =
1141 {
1142 PyVarObject_HEAD_INIT(NULL, 0)
1143 "_pyo.Randh_base", /*tp_name*/
1144 sizeof(Randh), /*tp_basicsize*/
1145 0, /*tp_itemsize*/
1146 (destructor)Randh_dealloc, /*tp_dealloc*/
1147 0, /*tp_print*/
1148 0, /*tp_getattr*/
1149 0, /*tp_setattr*/
1150 0, /*tp_as_async (tp_compare in Python 2)*/
1151 0, /*tp_repr*/
1152 &Randh_as_number, /*tp_as_number*/
1153 0, /*tp_as_sequence*/
1154 0, /*tp_as_mapping*/
1155 0, /*tp_hash */
1156 0, /*tp_call*/
1157 0, /*tp_str*/
1158 0, /*tp_getattro*/
1159 0, /*tp_setattro*/
1160 0, /*tp_as_buffer*/
1161 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1162 "Randh objects. Periodically generates a new random value.", /* tp_doc */
1163 (traverseproc)Randh_traverse, /* tp_traverse */
1164 (inquiry)Randh_clear, /* tp_clear */
1165 0, /* tp_richcompare */
1166 0, /* tp_weaklistoffset */
1167 0, /* tp_iter */
1168 0, /* tp_iternext */
1169 Randh_methods, /* tp_methods */
1170 Randh_members, /* tp_members */
1171 0, /* tp_getset */
1172 0, /* tp_base */
1173 0, /* tp_dict */
1174 0, /* tp_descr_get */
1175 0, /* tp_descr_set */
1176 0, /* tp_dictoffset */
1177 0, /* tp_init */
1178 0, /* tp_alloc */
1179 Randh_new, /* tp_new */
1180 };
1181
1182 /****************/
1183 /**** Choice *****/
1184 /****************/
1185 typedef struct
1186 {
1187 pyo_audio_HEAD
1188 PyObject *freq;
1189 Stream *freq_stream;
1190 int chSize;
1191 MYFLT *choice;
1192 MYFLT value;
1193 MYFLT time;
1194 int modebuffer[3]; // need at least 2 slots for mul & add
1195 } Choice;
1196
1197 static void
1198 Choice_generate_i(Choice *self)
1199 {
1200 int i;
1201 MYFLT inc;
1202 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
1203 inc = fr / self->sr;
1204
1205 for (i = 0; i < self->bufsize; i++)
1206 {
1207 self->time += inc;
1208
1209 if (self->time < 0.0)
1210 self->time += 1.0;
1211 else if (self->time >= 1.0)
1212 {
1213 self->time -= 1.0;
1214 self->value = self->choice[(int)(RANDOM_UNIFORM * self->chSize)];
1215 }
1216
1217 self->data[i] = self->value;
1218 }
1219 }
1220
1221 static void
1222 Choice_generate_a(Choice *self)
1223 {
1224 int i;
1225 MYFLT inc;
1226 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1227
1228 for (i = 0; i < self->bufsize; i++)
1229 {
1230 inc = fr[i] / self->sr;
1231 self->time += inc;
1232
1233 if (self->time < 0.0)
1234 self->time += 1.0;
1235 else if (self->time >= 1.0)
1236 {
1237 self->time -= 1.0;
1238 self->value = self->choice[(int)(RANDOM_UNIFORM * self->chSize)];
1239 }
1240
1241 self->data[i] = self->value;
1242 }
1243 }
1244
1245 static void Choice_postprocessing_ii(Choice *self) { POST_PROCESSING_II };
1246 static void Choice_postprocessing_ai(Choice *self) { POST_PROCESSING_AI };
1247 static void Choice_postprocessing_ia(Choice *self) { POST_PROCESSING_IA };
1248 static void Choice_postprocessing_aa(Choice *self) { POST_PROCESSING_AA };
1249 static void Choice_postprocessing_ireva(Choice *self) { POST_PROCESSING_IREVA };
1250 static void Choice_postprocessing_areva(Choice *self) { POST_PROCESSING_AREVA };
1251 static void Choice_postprocessing_revai(Choice *self) { POST_PROCESSING_REVAI };
1252 static void Choice_postprocessing_revaa(Choice *self) { POST_PROCESSING_REVAA };
1253 static void Choice_postprocessing_revareva(Choice *self) { POST_PROCESSING_REVAREVA };
1254
1255 static void
1256 Choice_setProcMode(Choice *self)
1257 {
1258 int procmode, muladdmode;
1259 procmode = self->modebuffer[2];
1260 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1261
1262 switch (procmode)
1263 {
1264 case 0:
1265 self->proc_func_ptr = Choice_generate_i;
1266 break;
1267
1268 case 1:
1269 self->proc_func_ptr = Choice_generate_a;
1270 break;
1271 }
1272
1273 switch (muladdmode)
1274 {
1275 case 0:
1276 self->muladd_func_ptr = Choice_postprocessing_ii;
1277 break;
1278
1279 case 1:
1280 self->muladd_func_ptr = Choice_postprocessing_ai;
1281 break;
1282
1283 case 2:
1284 self->muladd_func_ptr = Choice_postprocessing_revai;
1285 break;
1286
1287 case 10:
1288 self->muladd_func_ptr = Choice_postprocessing_ia;
1289 break;
1290
1291 case 11:
1292 self->muladd_func_ptr = Choice_postprocessing_aa;
1293 break;
1294
1295 case 12:
1296 self->muladd_func_ptr = Choice_postprocessing_revaa;
1297 break;
1298
1299 case 20:
1300 self->muladd_func_ptr = Choice_postprocessing_ireva;
1301 break;
1302
1303 case 21:
1304 self->muladd_func_ptr = Choice_postprocessing_areva;
1305 break;
1306
1307 case 22:
1308 self->muladd_func_ptr = Choice_postprocessing_revareva;
1309 break;
1310 }
1311 }
1312
1313 static void
1314 Choice_compute_next_data_frame(Choice *self)
1315 {
1316 (*self->proc_func_ptr)(self);
1317 (*self->muladd_func_ptr)(self);
1318 }
1319
1320 static int
1321 Choice_traverse(Choice *self, visitproc visit, void *arg)
1322 {
1323 pyo_VISIT
1324 Py_VISIT(self->freq);
1325 return 0;
1326 }
1327
1328 static int
1329 Choice_clear(Choice *self)
1330 {
1331 pyo_CLEAR
1332 Py_CLEAR(self->freq);
1333 return 0;
1334 }
1335
1336 static void
1337 Choice_dealloc(Choice* self)
1338 {
1339 pyo_DEALLOC
1340 PyMem_RawFree(self->choice);
1341 Choice_clear(self);
1342 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1343 Py_TYPE(self)->tp_free((PyObject*)self);
1344 }
1345
1346 static PyObject *
1347 Choice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1348 {
1349 int i;
1350 PyObject *choicetmp = NULL, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
1351 Choice *self;
1352 self = (Choice *)type->tp_alloc(type, 0);
1353
1354 self->freq = PyFloat_FromDouble(1.);
1355 self->value = 0.0;
1356 self->time = 1.0;
1357 self->modebuffer[0] = 0;
1358 self->modebuffer[1] = 0;
1359 self->modebuffer[2] = 0;
1360
1361 INIT_OBJECT_COMMON
1362 Stream_setFunctionPtr(self->stream, Choice_compute_next_data_frame);
1363 self->mode_func_ptr = Choice_setProcMode;
1364
1365 static char *kwlist[] = {"choice", "freq", "mul", "add", NULL};
1366
1367 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &choicetmp, &freqtmp, &multmp, &addtmp))
1368 Py_RETURN_NONE;
1369
1370 if (choicetmp)
1371 {
1372 PyObject_CallMethod((PyObject *)self, "setChoice", "O", choicetmp);
1373 }
1374
1375 if (freqtmp)
1376 {
1377 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1378 }
1379
1380 if (multmp)
1381 {
1382 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1383 }
1384
1385 if (addtmp)
1386 {
1387 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1388 }
1389
1390 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1391
1392 Server_generateSeed((Server *)self->server, CHOICE_ID);
1393
1394 (*self->mode_func_ptr)(self);
1395
1396 return (PyObject *)self;
1397 }
1398
1399 static PyObject * Choice_getServer(Choice* self) { GET_SERVER };
1400 static PyObject * Choice_getStream(Choice* self) { GET_STREAM };
1401 static PyObject * Choice_setMul(Choice *self, PyObject *arg) { SET_MUL };
1402 static PyObject * Choice_setAdd(Choice *self, PyObject *arg) { SET_ADD };
1403 static PyObject * Choice_setSub(Choice *self, PyObject *arg) { SET_SUB };
1404 static PyObject * Choice_setDiv(Choice *self, PyObject *arg) { SET_DIV };
1405
1406 static PyObject * Choice_play(Choice *self, PyObject *args, PyObject *kwds) { PLAY };
1407 static PyObject * Choice_out(Choice *self, PyObject *args, PyObject *kwds) { OUT };
1408 static PyObject * Choice_stop(Choice *self, PyObject *args, PyObject *kwds) { STOP };
1409
1410 static PyObject * Choice_multiply(Choice *self, PyObject *arg) { MULTIPLY };
1411 static PyObject * Choice_inplace_multiply(Choice *self, PyObject *arg) { INPLACE_MULTIPLY };
1412 static PyObject * Choice_add(Choice *self, PyObject *arg) { ADD };
1413 static PyObject * Choice_inplace_add(Choice *self, PyObject *arg) { INPLACE_ADD };
1414 static PyObject * Choice_sub(Choice *self, PyObject *arg) { SUB };
1415 static PyObject * Choice_inplace_sub(Choice *self, PyObject *arg) { INPLACE_SUB };
1416 static PyObject * Choice_div(Choice *self, PyObject *arg) { DIV };
1417 static PyObject * Choice_inplace_div(Choice *self, PyObject *arg) { INPLACE_DIV };
1418
1419 static PyObject *
1420 Choice_setChoice(Choice *self, PyObject *arg)
1421 {
1422 int i;
1423
1424 if (! PyList_Check(arg))
1425 {
1426 PyErr_SetString(PyExc_TypeError, "The choice attribute must be a list.");
1427 Py_RETURN_NONE;
1428 }
1429
1430 self->chSize = PyList_Size(arg);
1431 self->choice = (MYFLT *)PyMem_RawRealloc(self->choice, self->chSize * sizeof(MYFLT));
1432
1433 for (i = 0; i < self->chSize; i++)
1434 {
1435 self->choice[i] = PyFloat_AsDouble(PyList_GET_ITEM(arg, i));
1436 }
1437
1438 (*self->mode_func_ptr)(self);
1439
1440 Py_RETURN_NONE;
1441 }
1442
1443 static PyObject * Choice_setFreq(Choice *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
1444
1445 static PyMemberDef Choice_members[] =
1446 {
1447 {"server", T_OBJECT_EX, offsetof(Choice, server), 0, "Pyo server."},
1448 {"stream", T_OBJECT_EX, offsetof(Choice, stream), 0, "Stream object."},
1449 {"freq", T_OBJECT_EX, offsetof(Choice, freq), 0, "Polling frequency."},
1450 {"mul", T_OBJECT_EX, offsetof(Choice, mul), 0, "Mul factor."},
1451 {"add", T_OBJECT_EX, offsetof(Choice, add), 0, "Add factor."},
1452 {NULL} /* Sentinel */
1453 };
1454
1455 static PyMethodDef Choice_methods[] =
1456 {
1457 {"getServer", (PyCFunction)Choice_getServer, METH_NOARGS, "Returns server object."},
1458 {"_getStream", (PyCFunction)Choice_getStream, METH_NOARGS, "Returns stream object."},
1459 {"play", (PyCFunction)Choice_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1460 {"out", (PyCFunction)Choice_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1461 {"stop", (PyCFunction)Choice_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1462 {"setChoice", (PyCFunction)Choice_setChoice, METH_O, "Sets list of possible floats."},
1463 {"setFreq", (PyCFunction)Choice_setFreq, METH_O, "Sets polling frequency."},
1464 {"setMul", (PyCFunction)Choice_setMul, METH_O, "Sets oscillator mul factor."},
1465 {"setAdd", (PyCFunction)Choice_setAdd, METH_O, "Sets oscillator add factor."},
1466 {"setSub", (PyCFunction)Choice_setSub, METH_O, "Sets inverse add factor."},
1467 {"setDiv", (PyCFunction)Choice_setDiv, METH_O, "Sets inverse mul factor."},
1468 {NULL} /* Sentinel */
1469 };
1470
1471 static PyNumberMethods Choice_as_number =
1472 {
1473 (binaryfunc)Choice_add, /*nb_add*/
1474 (binaryfunc)Choice_sub, /*nb_subtract*/
1475 (binaryfunc)Choice_multiply, /*nb_multiply*/
1476 0, /*nb_remainder*/
1477 0, /*nb_divmod*/
1478 0, /*nb_power*/
1479 0, /*nb_neg*/
1480 0, /*nb_pos*/
1481 0, /*(unaryfunc)array_abs,*/
1482 0, /*nb_nonzero*/
1483 0, /*nb_invert*/
1484 0, /*nb_lshift*/
1485 0, /*nb_rshift*/
1486 0, /*nb_and*/
1487 0, /*nb_xor*/
1488 0, /*nb_or*/
1489 0, /*nb_int*/
1490 0, /*nb_long*/
1491 0, /*nb_float*/
1492 (binaryfunc)Choice_inplace_add, /*inplace_add*/
1493 (binaryfunc)Choice_inplace_sub, /*inplace_subtract*/
1494 (binaryfunc)Choice_inplace_multiply, /*inplace_multiply*/
1495 0, /*inplace_remainder*/
1496 0, /*inplace_power*/
1497 0, /*inplace_lshift*/
1498 0, /*inplace_rshift*/
1499 0, /*inplace_and*/
1500 0, /*inplace_xor*/
1501 0, /*inplace_or*/
1502 0, /*nb_floor_divide*/
1503 (binaryfunc)Choice_div, /*nb_true_divide*/
1504 0, /*nb_inplace_floor_divide*/
1505 (binaryfunc)Choice_inplace_div, /*nb_inplace_true_divide*/
1506 0, /* nb_index */
1507 };
1508
1509 PyTypeObject ChoiceType =
1510 {
1511 PyVarObject_HEAD_INIT(NULL, 0)
1512 "_pyo.Choice_base", /*tp_name*/
1513 sizeof(Choice), /*tp_basicsize*/
1514 0, /*tp_itemsize*/
1515 (destructor)Choice_dealloc, /*tp_dealloc*/
1516 0, /*tp_print*/
1517 0, /*tp_getattr*/
1518 0, /*tp_setattr*/
1519 0, /*tp_as_async (tp_compare in Python 2)*/
1520 0, /*tp_repr*/
1521 &Choice_as_number, /*tp_as_number*/
1522 0, /*tp_as_sequence*/
1523 0, /*tp_as_mapping*/
1524 0, /*tp_hash */
1525 0, /*tp_call*/
1526 0, /*tp_str*/
1527 0, /*tp_getattro*/
1528 0, /*tp_setattro*/
1529 0, /*tp_as_buffer*/
1530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1531 "Choice objects. Periodically generates a new random value from a list.", /* tp_doc */
1532 (traverseproc)Choice_traverse, /* tp_traverse */
1533 (inquiry)Choice_clear, /* tp_clear */
1534 0, /* tp_richcompare */
1535 0, /* tp_weaklistoffset */
1536 0, /* tp_iter */
1537 0, /* tp_iternext */
1538 Choice_methods, /* tp_methods */
1539 Choice_members, /* tp_members */
1540 0, /* tp_getset */
1541 0, /* tp_base */
1542 0, /* tp_dict */
1543 0, /* tp_descr_get */
1544 0, /* tp_descr_set */
1545 0, /* tp_dictoffset */
1546 0, /* tp_init */
1547 0, /* tp_alloc */
1548 Choice_new, /* tp_new */
1549 };
1550
1551 /****************/
1552 /**** RandInt ***/
1553 /****************/
1554 typedef struct
1555 {
1556 pyo_audio_HEAD
1557 PyObject *max;
1558 PyObject *freq;
1559 Stream *max_stream;
1560 Stream *freq_stream;
1561 MYFLT value;
1562 MYFLT time;
1563 int modebuffer[4]; // need at least 2 slots for mul & add
1564 } RandInt;
1565
1566 static void
1567 RandInt_generate_ii(RandInt *self)
1568 {
1569 int i;
1570 MYFLT inc;
1571 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
1572 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
1573 inc = fr / self->sr;
1574
1575 for (i = 0; i < self->bufsize; i++)
1576 {
1577 self->time += inc;
1578
1579 if (self->time < 0.0)
1580 self->time += 1.0;
1581 else if (self->time >= 1.0)
1582 {
1583 self->time -= 1.0;
1584 self->value = (MYFLT)((int)(RANDOM_UNIFORM * ma));
1585 }
1586
1587 self->data[i] = self->value;
1588 }
1589 }
1590
1591 static void
1592 RandInt_generate_ai(RandInt *self)
1593 {
1594 int i;
1595 MYFLT inc;
1596 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
1597 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
1598 inc = fr / self->sr;
1599
1600 for (i = 0; i < self->bufsize; i++)
1601 {
1602 self->time += inc;
1603
1604 if (self->time < 0.0)
1605 self->time += 1.0;
1606 else if (self->time >= 1.0)
1607 {
1608 self->time -= 1.0;
1609 self->value = (MYFLT)((int)(RANDOM_UNIFORM * ma[i]));
1610 }
1611
1612 self->data[i] = self->value;
1613 }
1614 }
1615
1616 static void
1617 RandInt_generate_ia(RandInt *self)
1618 {
1619 int i;
1620 MYFLT inc;
1621 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
1622 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1623
1624 for (i = 0; i < self->bufsize; i++)
1625 {
1626 inc = fr[i] / self->sr;
1627 self->time += inc;
1628
1629 if (self->time < 0.0)
1630 self->time += 1.0;
1631 else if (self->time >= 1.0)
1632 {
1633 self->time -= 1.0;
1634 self->value = (MYFLT)((int)(RANDOM_UNIFORM * ma));
1635 }
1636
1637 self->data[i] = self->value;
1638 }
1639 }
1640
1641 static void
1642 RandInt_generate_aa(RandInt *self)
1643 {
1644 int i;
1645 MYFLT inc;
1646 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
1647 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1648
1649 for (i = 0; i < self->bufsize; i++)
1650 {
1651 inc = fr[i] / self->sr;
1652 self->time += inc;
1653
1654 if (self->time < 0.0)
1655 self->time += 1.0;
1656 else if (self->time >= 1.0)
1657 {
1658 self->time -= 1.0;
1659 self->value = (MYFLT)((int)(RANDOM_UNIFORM * ma[i]));
1660 }
1661
1662 self->data[i] = self->value;
1663 }
1664 }
1665
1666 static void RandInt_postprocessing_ii(RandInt *self) { POST_PROCESSING_II };
1667 static void RandInt_postprocessing_ai(RandInt *self) { POST_PROCESSING_AI };
1668 static void RandInt_postprocessing_ia(RandInt *self) { POST_PROCESSING_IA };
1669 static void RandInt_postprocessing_aa(RandInt *self) { POST_PROCESSING_AA };
1670 static void RandInt_postprocessing_ireva(RandInt *self) { POST_PROCESSING_IREVA };
1671 static void RandInt_postprocessing_areva(RandInt *self) { POST_PROCESSING_AREVA };
1672 static void RandInt_postprocessing_revai(RandInt *self) { POST_PROCESSING_REVAI };
1673 static void RandInt_postprocessing_revaa(RandInt *self) { POST_PROCESSING_REVAA };
1674 static void RandInt_postprocessing_revareva(RandInt *self) { POST_PROCESSING_REVAREVA };
1675
1676 static void
1677 RandInt_setProcMode(RandInt *self)
1678 {
1679 int procmode, muladdmode;
1680 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
1681 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1682
1683 switch (procmode)
1684 {
1685 case 0:
1686 self->proc_func_ptr = RandInt_generate_ii;
1687 break;
1688
1689 case 1:
1690 self->proc_func_ptr = RandInt_generate_ai;
1691 break;
1692
1693 case 10:
1694 self->proc_func_ptr = RandInt_generate_ia;
1695 break;
1696
1697 case 11:
1698 self->proc_func_ptr = RandInt_generate_aa;
1699 break;
1700 }
1701
1702 switch (muladdmode)
1703 {
1704 case 0:
1705 self->muladd_func_ptr = RandInt_postprocessing_ii;
1706 break;
1707
1708 case 1:
1709 self->muladd_func_ptr = RandInt_postprocessing_ai;
1710 break;
1711
1712 case 2:
1713 self->muladd_func_ptr = RandInt_postprocessing_revai;
1714 break;
1715
1716 case 10:
1717 self->muladd_func_ptr = RandInt_postprocessing_ia;
1718 break;
1719
1720 case 11:
1721 self->muladd_func_ptr = RandInt_postprocessing_aa;
1722 break;
1723
1724 case 12:
1725 self->muladd_func_ptr = RandInt_postprocessing_revaa;
1726 break;
1727
1728 case 20:
1729 self->muladd_func_ptr = RandInt_postprocessing_ireva;
1730 break;
1731
1732 case 21:
1733 self->muladd_func_ptr = RandInt_postprocessing_areva;
1734 break;
1735
1736 case 22:
1737 self->muladd_func_ptr = RandInt_postprocessing_revareva;
1738 break;
1739 }
1740 }
1741
1742 static void
1743 RandInt_compute_next_data_frame(RandInt *self)
1744 {
1745 (*self->proc_func_ptr)(self);
1746 (*self->muladd_func_ptr)(self);
1747 }
1748
1749 static int
1750 RandInt_traverse(RandInt *self, visitproc visit, void *arg)
1751 {
1752 pyo_VISIT
1753 Py_VISIT(self->freq);
1754 Py_VISIT(self->max);
1755 return 0;
1756 }
1757
1758 static int
1759 RandInt_clear(RandInt *self)
1760 {
1761 pyo_CLEAR
1762 Py_CLEAR(self->freq);
1763 Py_CLEAR(self->max);
1764 return 0;
1765 }
1766
1767 static void
1768 RandInt_dealloc(RandInt* self)
1769 {
1770 pyo_DEALLOC
1771 RandInt_clear(self);
1772 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1773 Py_TYPE(self)->tp_free((PyObject*)self);
1774 }
1775
1776 static PyObject *
1777 RandInt_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1778 {
1779 int i;
1780 PyObject *maxtmp = NULL, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
1781 RandInt *self;
1782 self = (RandInt *)type->tp_alloc(type, 0);
1783
1784 self->max = PyFloat_FromDouble(100.);
1785 self->freq = PyFloat_FromDouble(1.);
1786 self->value = 0.0;
1787 self->time = 1.0;
1788 self->modebuffer[0] = 0;
1789 self->modebuffer[1] = 0;
1790 self->modebuffer[2] = 0;
1791 self->modebuffer[3] = 0;
1792
1793 INIT_OBJECT_COMMON
1794 Stream_setFunctionPtr(self->stream, RandInt_compute_next_data_frame);
1795 self->mode_func_ptr = RandInt_setProcMode;
1796
1797 static char *kwlist[] = {"max", "freq", "mul", "add", NULL};
1798
1799 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, &maxtmp, &freqtmp, &multmp, &addtmp))
1800 Py_RETURN_NONE;
1801
1802 if (maxtmp)
1803 {
1804 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
1805 }
1806
1807 if (freqtmp)
1808 {
1809 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1810 }
1811
1812 if (multmp)
1813 {
1814 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1815 }
1816
1817 if (addtmp)
1818 {
1819 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1820 }
1821
1822 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1823
1824 Server_generateSeed((Server *)self->server, RANDINT_ID);
1825
1826 (*self->mode_func_ptr)(self);
1827
1828 return (PyObject *)self;
1829 }
1830
1831 static PyObject * RandInt_getServer(RandInt* self) { GET_SERVER };
1832 static PyObject * RandInt_getStream(RandInt* self) { GET_STREAM };
1833 static PyObject * RandInt_setMul(RandInt *self, PyObject *arg) { SET_MUL };
1834 static PyObject * RandInt_setAdd(RandInt *self, PyObject *arg) { SET_ADD };
1835 static PyObject * RandInt_setSub(RandInt *self, PyObject *arg) { SET_SUB };
1836 static PyObject * RandInt_setDiv(RandInt *self, PyObject *arg) { SET_DIV };
1837
1838 static PyObject * RandInt_play(RandInt *self, PyObject *args, PyObject *kwds) { PLAY };
1839 static PyObject * RandInt_out(RandInt *self, PyObject *args, PyObject *kwds) { OUT };
1840 static PyObject * RandInt_stop(RandInt *self, PyObject *args, PyObject *kwds) { STOP };
1841
1842 static PyObject * RandInt_multiply(RandInt *self, PyObject *arg) { MULTIPLY };
1843 static PyObject * RandInt_inplace_multiply(RandInt *self, PyObject *arg) { INPLACE_MULTIPLY };
1844 static PyObject * RandInt_add(RandInt *self, PyObject *arg) { ADD };
1845 static PyObject * RandInt_inplace_add(RandInt *self, PyObject *arg) { INPLACE_ADD };
1846 static PyObject * RandInt_sub(RandInt *self, PyObject *arg) { SUB };
1847 static PyObject * RandInt_inplace_sub(RandInt *self, PyObject *arg) { INPLACE_SUB };
1848 static PyObject * RandInt_div(RandInt *self, PyObject *arg) { DIV };
1849 static PyObject * RandInt_inplace_div(RandInt *self, PyObject *arg) { INPLACE_DIV };
1850
1851 static PyObject * RandInt_setMax(RandInt *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 2); }
1852 static PyObject * RandInt_setFreq(RandInt *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 3); }
1853
1854 static PyMemberDef RandInt_members[] =
1855 {
1856 {"server", T_OBJECT_EX, offsetof(RandInt, server), 0, "Pyo server."},
1857 {"stream", T_OBJECT_EX, offsetof(RandInt, stream), 0, "Stream object."},
1858 {"max", T_OBJECT_EX, offsetof(RandInt, max), 0, "Maximum possible value."},
1859 {"freq", T_OBJECT_EX, offsetof(RandInt, freq), 0, "Polling frequency."},
1860 {"mul", T_OBJECT_EX, offsetof(RandInt, mul), 0, "Mul factor."},
1861 {"add", T_OBJECT_EX, offsetof(RandInt, add), 0, "Add factor."},
1862 {NULL} /* Sentinel */
1863 };
1864
1865 static PyMethodDef RandInt_methods[] =
1866 {
1867 {"getServer", (PyCFunction)RandInt_getServer, METH_NOARGS, "Returns server object."},
1868 {"_getStream", (PyCFunction)RandInt_getStream, METH_NOARGS, "Returns stream object."},
1869 {"play", (PyCFunction)RandInt_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1870 {"out", (PyCFunction)RandInt_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1871 {"stop", (PyCFunction)RandInt_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1872 {"setMax", (PyCFunction)RandInt_setMax, METH_O, "Sets maximum possible value."},
1873 {"setFreq", (PyCFunction)RandInt_setFreq, METH_O, "Sets polling frequency."},
1874 {"setMul", (PyCFunction)RandInt_setMul, METH_O, "Sets oscillator mul factor."},
1875 {"setAdd", (PyCFunction)RandInt_setAdd, METH_O, "Sets oscillator add factor."},
1876 {"setSub", (PyCFunction)RandInt_setSub, METH_O, "Sets inverse add factor."},
1877 {"setDiv", (PyCFunction)RandInt_setDiv, METH_O, "Sets inverse mul factor."},
1878 {NULL} /* Sentinel */
1879 };
1880
1881 static PyNumberMethods RandInt_as_number =
1882 {
1883 (binaryfunc)RandInt_add, /*nb_add*/
1884 (binaryfunc)RandInt_sub, /*nb_subtract*/
1885 (binaryfunc)RandInt_multiply, /*nb_multiply*/
1886 0, /*nb_remainder*/
1887 0, /*nb_divmod*/
1888 0, /*nb_power*/
1889 0, /*nb_neg*/
1890 0, /*nb_pos*/
1891 0, /*(unaryfunc)array_abs,*/
1892 0, /*nb_nonzero*/
1893 0, /*nb_invert*/
1894 0, /*nb_lshift*/
1895 0, /*nb_rshift*/
1896 0, /*nb_and*/
1897 0, /*nb_xor*/
1898 0, /*nb_or*/
1899 0, /*nb_int*/
1900 0, /*nb_long*/
1901 0, /*nb_float*/
1902 (binaryfunc)RandInt_inplace_add, /*inplace_add*/
1903 (binaryfunc)RandInt_inplace_sub, /*inplace_subtract*/
1904 (binaryfunc)RandInt_inplace_multiply, /*inplace_multiply*/
1905 0, /*inplace_remainder*/
1906 0, /*inplace_power*/
1907 0, /*inplace_lshift*/
1908 0, /*inplace_rshift*/
1909 0, /*inplace_and*/
1910 0, /*inplace_xor*/
1911 0, /*inplace_or*/
1912 0, /*nb_floor_divide*/
1913 (binaryfunc)RandInt_div, /*nb_true_divide*/
1914 0, /*nb_inplace_floor_divide*/
1915 (binaryfunc)RandInt_inplace_div, /*nb_inplace_true_divide*/
1916 0, /* nb_index */
1917 };
1918
1919 PyTypeObject RandIntType =
1920 {
1921 PyVarObject_HEAD_INIT(NULL, 0)
1922 "_pyo.RandInt_base", /*tp_name*/
1923 sizeof(RandInt), /*tp_basicsize*/
1924 0, /*tp_itemsize*/
1925 (destructor)RandInt_dealloc, /*tp_dealloc*/
1926 0, /*tp_print*/
1927 0, /*tp_getattr*/
1928 0, /*tp_setattr*/
1929 0, /*tp_as_async (tp_compare in Python 2)*/
1930 0, /*tp_repr*/
1931 &RandInt_as_number, /*tp_as_number*/
1932 0, /*tp_as_sequence*/
1933 0, /*tp_as_mapping*/
1934 0, /*tp_hash */
1935 0, /*tp_call*/
1936 0, /*tp_str*/
1937 0, /*tp_getattro*/
1938 0, /*tp_setattro*/
1939 0, /*tp_as_buffer*/
1940 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1941 "RandInt objects. Periodically generates a new integer random value.", /* tp_doc */
1942 (traverseproc)RandInt_traverse, /* tp_traverse */
1943 (inquiry)RandInt_clear, /* tp_clear */
1944 0, /* tp_richcompare */
1945 0, /* tp_weaklistoffset */
1946 0, /* tp_iter */
1947 0, /* tp_iternext */
1948 RandInt_methods, /* tp_methods */
1949 RandInt_members, /* tp_members */
1950 0, /* tp_getset */
1951 0, /* tp_base */
1952 0, /* tp_dict */
1953 0, /* tp_descr_get */
1954 0, /* tp_descr_set */
1955 0, /* tp_dictoffset */
1956 0, /* tp_init */
1957 0, /* tp_alloc */
1958 RandInt_new, /* tp_new */
1959 };
1960
1961 /*****************/
1962 /**** RandDur ****/
1963 /*****************/
1964 typedef struct
1965 {
1966 pyo_audio_HEAD
1967 PyObject *min;
1968 PyObject *max;
1969 Stream *min_stream;
1970 Stream *max_stream;
1971 MYFLT value;
1972 MYFLT time;
1973 MYFLT inc;
1974 int modebuffer[4]; // need at least 2 slots for mul & add
1975 } RandDur;
1976
1977 static void
1978 RandDur_generate_ii(RandDur *self)
1979 {
1980 int i;
1981 MYFLT range;
1982 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
1983 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
1984
1985 for (i = 0; i < self->bufsize; i++)
1986 {
1987 self->time += self->inc;
1988
1989 if (self->time < 0.0)
1990 self->time += 1.0;
1991 else if (self->time >= 1.0)
1992 {
1993 self->time -= 1.0;
1994
1995 if (mi < 0.0)
1996 mi = 0.0;
1997
1998 range = ma - mi;
1999
2000 if (range < 0.0)
2001 range = 0.0;
2002
2003 self->value = range * RANDOM_UNIFORM + mi;
2004 self->inc = (1.0 / self->value) / self->sr;
2005 }
2006
2007 self->data[i] = self->value;
2008 }
2009 }
2010
2011 static void
2012 RandDur_generate_ai(RandDur *self)
2013 {
2014 int i;
2015 MYFLT range, mi;
2016 MYFLT *min = Stream_getData((Stream *)self->min_stream);
2017 MYFLT ma = PyFloat_AS_DOUBLE(self->max);
2018
2019 for (i = 0; i < self->bufsize; i++)
2020 {
2021 self->time += self->inc;
2022
2023 if (self->time < 0.0)
2024 self->time += 1.0;
2025 else if (self->time >= 1.0)
2026 {
2027 self->time -= 1.0;
2028 mi = min[i];
2029
2030 if (mi < 0.0)
2031 mi = 0.0;
2032
2033 range = ma - mi;
2034
2035 if (range < 0.0)
2036 range = 0.0;
2037
2038 self->value = range * RANDOM_UNIFORM + mi;
2039 self->inc = (1.0 / self->value) / self->sr;
2040 }
2041
2042 self->data[i] = self->value;
2043 }
2044 }
2045
2046 static void
2047 RandDur_generate_ia(RandDur *self)
2048 {
2049 int i;
2050 MYFLT range;
2051 MYFLT mi = PyFloat_AS_DOUBLE(self->min);
2052 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
2053
2054 for (i = 0; i < self->bufsize; i++)
2055 {
2056 self->time += self->inc;
2057
2058 if (self->time < 0.0)
2059 self->time += 1.0;
2060 else if (self->time >= 1.0)
2061 {
2062 self->time -= 1.0;
2063
2064 if (mi < 0.0)
2065 mi = 0.0;
2066
2067 range = ma[i] - mi;
2068
2069 if (range < 0.0)
2070 range = 0.0;
2071
2072 self->value = range * RANDOM_UNIFORM + mi;
2073 self->inc = (1.0 / self->value) / self->sr;
2074 }
2075
2076 self->data[i] = self->value;
2077 }
2078 }
2079
2080 static void
2081 RandDur_generate_aa(RandDur *self)
2082 {
2083 int i;
2084 MYFLT range, mi;
2085 MYFLT *min = Stream_getData((Stream *)self->min_stream);
2086 MYFLT *ma = Stream_getData((Stream *)self->max_stream);
2087
2088 for (i = 0; i < self->bufsize; i++)
2089 {
2090 self->time += self->inc;
2091
2092 if (self->time < 0.0)
2093 self->time += 1.0;
2094 else if (self->time >= 1.0)
2095 {
2096 self->time -= 1.0;
2097 mi = min[i];
2098
2099 if (mi < 0.0)
2100 mi = 0.0;
2101
2102 range = ma[i] - mi;
2103
2104 if (range < 0.0)
2105 range = 0.0;
2106
2107 self->value = range * RANDOM_UNIFORM + mi;
2108 self->inc = (1.0 / self->value) / self->sr;
2109 }
2110
2111 self->data[i] = self->value;
2112 }
2113 }
2114
2115 static void RandDur_postprocessing_ii(RandDur *self) { POST_PROCESSING_II };
2116 static void RandDur_postprocessing_ai(RandDur *self) { POST_PROCESSING_AI };
2117 static void RandDur_postprocessing_ia(RandDur *self) { POST_PROCESSING_IA };
2118 static void RandDur_postprocessing_aa(RandDur *self) { POST_PROCESSING_AA };
2119 static void RandDur_postprocessing_ireva(RandDur *self) { POST_PROCESSING_IREVA };
2120 static void RandDur_postprocessing_areva(RandDur *self) { POST_PROCESSING_AREVA };
2121 static void RandDur_postprocessing_revai(RandDur *self) { POST_PROCESSING_REVAI };
2122 static void RandDur_postprocessing_revaa(RandDur *self) { POST_PROCESSING_REVAA };
2123 static void RandDur_postprocessing_revareva(RandDur *self) { POST_PROCESSING_REVAREVA };
2124
2125 static void
2126 RandDur_setProcMode(RandDur *self)
2127 {
2128 int procmode, muladdmode;
2129 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
2130 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2131
2132 switch (procmode)
2133 {
2134 case 0:
2135 self->proc_func_ptr = RandDur_generate_ii;
2136 break;
2137
2138 case 1:
2139 self->proc_func_ptr = RandDur_generate_ai;
2140 break;
2141
2142 case 10:
2143 self->proc_func_ptr = RandDur_generate_ia;
2144 break;
2145
2146 case 11:
2147 self->proc_func_ptr = RandDur_generate_aa;
2148 break;
2149 }
2150
2151 switch (muladdmode)
2152 {
2153 case 0:
2154 self->muladd_func_ptr = RandDur_postprocessing_ii;
2155 break;
2156
2157 case 1:
2158 self->muladd_func_ptr = RandDur_postprocessing_ai;
2159 break;
2160
2161 case 2:
2162 self->muladd_func_ptr = RandDur_postprocessing_revai;
2163 break;
2164
2165 case 10:
2166 self->muladd_func_ptr = RandDur_postprocessing_ia;
2167 break;
2168
2169 case 11:
2170 self->muladd_func_ptr = RandDur_postprocessing_aa;
2171 break;
2172
2173 case 12:
2174 self->muladd_func_ptr = RandDur_postprocessing_revaa;
2175 break;
2176
2177 case 20:
2178 self->muladd_func_ptr = RandDur_postprocessing_ireva;
2179 break;
2180
2181 case 21:
2182 self->muladd_func_ptr = RandDur_postprocessing_areva;
2183 break;
2184
2185 case 22:
2186 self->muladd_func_ptr = RandDur_postprocessing_revareva;
2187 break;
2188 }
2189 }
2190
2191 static void
2192 RandDur_compute_next_data_frame(RandDur *self)
2193 {
2194 (*self->proc_func_ptr)(self);
2195 (*self->muladd_func_ptr)(self);
2196 }
2197
2198 static int
2199 RandDur_traverse(RandDur *self, visitproc visit, void *arg)
2200 {
2201 pyo_VISIT
2202 Py_VISIT(self->min);
2203 Py_VISIT(self->max);
2204 return 0;
2205 }
2206
2207 static int
2208 RandDur_clear(RandDur *self)
2209 {
2210 pyo_CLEAR
2211 Py_CLEAR(self->min);
2212 Py_CLEAR(self->max);
2213 return 0;
2214 }
2215
2216 static void
2217 RandDur_dealloc(RandDur* self)
2218 {
2219 pyo_DEALLOC
2220 RandDur_clear(self);
2221 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2222 Py_TYPE(self)->tp_free((PyObject*)self);
2223 }
2224
2225 static PyObject *
2226 RandDur_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2227 {
2228 int i;
2229 MYFLT mi, ma;
2230 PyObject *mintmp = NULL, *maxtmp = NULL, *multmp = NULL, *addtmp = NULL;
2231 RandDur *self;
2232 self = (RandDur *)type->tp_alloc(type, 0);
2233
2234 self->min = PyFloat_FromDouble(0.01);
2235 self->max = PyFloat_FromDouble(1.);
2236 self->value = self->inc = 0.0;
2237 self->time = 1.0;
2238 self->modebuffer[0] = 0;
2239 self->modebuffer[1] = 0;
2240 self->modebuffer[2] = 0;
2241 self->modebuffer[3] = 0;
2242
2243 INIT_OBJECT_COMMON
2244 Stream_setFunctionPtr(self->stream, RandDur_compute_next_data_frame);
2245 self->mode_func_ptr = RandDur_setProcMode;
2246
2247 static char *kwlist[] = {"min", "max", "mul", "add", NULL};
2248
2249 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, &mintmp, &maxtmp, &multmp, &addtmp))
2250 Py_RETURN_NONE;
2251
2252 if (mintmp)
2253 {
2254 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
2255 }
2256
2257 if (maxtmp)
2258 {
2259 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
2260 }
2261
2262 if (multmp)
2263 {
2264 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2265 }
2266
2267 if (addtmp)
2268 {
2269 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2270 }
2271
2272 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2273
2274 Server_generateSeed((Server *)self->server, RANDDUR_ID);
2275
2276 if (self->modebuffer[2] == 0)
2277 mi = PyFloat_AS_DOUBLE(self->min);
2278 else
2279 mi = Stream_getData((Stream *)self->min_stream)[0];
2280
2281 if (self->modebuffer[3] == 0)
2282 ma = PyFloat_AS_DOUBLE(self->max);
2283 else
2284 ma = Stream_getData((Stream *)self->max_stream)[0];
2285
2286 self->value = (mi + ma) * 0.5;
2287
2288 if (self->value == 0.0)
2289 self->inc = 0.0;
2290 else
2291 self->inc = (1.0 / self->value) / self->sr;
2292
2293 (*self->mode_func_ptr)(self);
2294
2295 return (PyObject *)self;
2296 }
2297
2298 static PyObject * RandDur_getServer(RandDur* self) { GET_SERVER };
2299 static PyObject * RandDur_getStream(RandDur* self) { GET_STREAM };
2300 static PyObject * RandDur_setMul(RandDur *self, PyObject *arg) { SET_MUL };
2301 static PyObject * RandDur_setAdd(RandDur *self, PyObject *arg) { SET_ADD };
2302 static PyObject * RandDur_setSub(RandDur *self, PyObject *arg) { SET_SUB };
2303 static PyObject * RandDur_setDiv(RandDur *self, PyObject *arg) { SET_DIV };
2304
2305 static PyObject * RandDur_play(RandDur *self, PyObject *args, PyObject *kwds) { PLAY };
2306 static PyObject * RandDur_out(RandDur *self, PyObject *args, PyObject *kwds) { OUT };
2307 static PyObject * RandDur_stop(RandDur *self, PyObject *args, PyObject *kwds) { STOP };
2308
2309 static PyObject * RandDur_multiply(RandDur *self, PyObject *arg) { MULTIPLY };
2310 static PyObject * RandDur_inplace_multiply(RandDur *self, PyObject *arg) { INPLACE_MULTIPLY };
2311 static PyObject * RandDur_add(RandDur *self, PyObject *arg) { ADD };
2312 static PyObject * RandDur_inplace_add(RandDur *self, PyObject *arg) { INPLACE_ADD };
2313 static PyObject * RandDur_sub(RandDur *self, PyObject *arg) { SUB };
2314 static PyObject * RandDur_inplace_sub(RandDur *self, PyObject *arg) { INPLACE_SUB };
2315 static PyObject * RandDur_div(RandDur *self, PyObject *arg) { DIV };
2316 static PyObject * RandDur_inplace_div(RandDur *self, PyObject *arg) { INPLACE_DIV };
2317
2318 static PyObject * RandDur_setMin(RandDur *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 2); }
2319 static PyObject * RandDur_setMax(RandDur *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 3); }
2320
2321 static PyMemberDef RandDur_members[] =
2322 {
2323 {"server", T_OBJECT_EX, offsetof(RandDur, server), 0, "Pyo server."},
2324 {"stream", T_OBJECT_EX, offsetof(RandDur, stream), 0, "Stream object."},
2325 {"min", T_OBJECT_EX, offsetof(RandDur, min), 0, "Minimum possible value."},
2326 {"max", T_OBJECT_EX, offsetof(RandDur, max), 0, "Maximum possible value."},
2327 {"mul", T_OBJECT_EX, offsetof(RandDur, mul), 0, "Mul factor."},
2328 {"add", T_OBJECT_EX, offsetof(RandDur, add), 0, "Add factor."},
2329 {NULL} /* Sentinel */
2330 };
2331
2332 static PyMethodDef RandDur_methods[] =
2333 {
2334 {"getServer", (PyCFunction)RandDur_getServer, METH_NOARGS, "Returns server object."},
2335 {"_getStream", (PyCFunction)RandDur_getStream, METH_NOARGS, "Returns stream object."},
2336 {"play", (PyCFunction)RandDur_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2337 {"out", (PyCFunction)RandDur_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2338 {"stop", (PyCFunction)RandDur_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2339 {"setMin", (PyCFunction)RandDur_setMin, METH_O, "Sets minimum possible value."},
2340 {"setMax", (PyCFunction)RandDur_setMax, METH_O, "Sets maximum possible value."},
2341 {"setMul", (PyCFunction)RandDur_setMul, METH_O, "Sets oscillator mul factor."},
2342 {"setAdd", (PyCFunction)RandDur_setAdd, METH_O, "Sets oscillator add factor."},
2343 {"setSub", (PyCFunction)RandDur_setSub, METH_O, "Sets inverse add factor."},
2344 {"setDiv", (PyCFunction)RandDur_setDiv, METH_O, "Sets inverse mul factor."},
2345 {NULL} /* Sentinel */
2346 };
2347
2348 static PyNumberMethods RandDur_as_number =
2349 {
2350 (binaryfunc)RandDur_add, /*nb_add*/
2351 (binaryfunc)RandDur_sub, /*nb_subtract*/
2352 (binaryfunc)RandDur_multiply, /*nb_multiply*/
2353 0, /*nb_remainder*/
2354 0, /*nb_divmod*/
2355 0, /*nb_power*/
2356 0, /*nb_neg*/
2357 0, /*nb_pos*/
2358 0, /*(unaryfunc)array_abs,*/
2359 0, /*nb_nonzero*/
2360 0, /*nb_invert*/
2361 0, /*nb_lshift*/
2362 0, /*nb_rshift*/
2363 0, /*nb_and*/
2364 0, /*nb_xor*/
2365 0, /*nb_or*/
2366 0, /*nb_int*/
2367 0, /*nb_long*/
2368 0, /*nb_float*/
2369 (binaryfunc)RandDur_inplace_add, /*inplace_add*/
2370 (binaryfunc)RandDur_inplace_sub, /*inplace_subtract*/
2371 (binaryfunc)RandDur_inplace_multiply, /*inplace_multiply*/
2372 0, /*inplace_remainder*/
2373 0, /*inplace_power*/
2374 0, /*inplace_lshift*/
2375 0, /*inplace_rshift*/
2376 0, /*inplace_and*/
2377 0, /*inplace_xor*/
2378 0, /*inplace_or*/
2379 0, /*nb_floor_divide*/
2380 (binaryfunc)RandDur_div, /*nb_true_divide*/
2381 0, /*nb_inplace_floor_divide*/
2382 (binaryfunc)RandDur_inplace_div, /*nb_inplace_true_divide*/
2383 0, /* nb_index */
2384 };
2385
2386 PyTypeObject RandDurType =
2387 {
2388 PyVarObject_HEAD_INIT(NULL, 0)
2389 "_pyo.RandDur_base", /*tp_name*/
2390 sizeof(RandDur), /*tp_basicsize*/
2391 0, /*tp_itemsize*/
2392 (destructor)RandDur_dealloc, /*tp_dealloc*/
2393 0, /*tp_print*/
2394 0, /*tp_getattr*/
2395 0, /*tp_setattr*/
2396 0, /*tp_as_async (tp_compare in Python 2)*/
2397 0, /*tp_repr*/
2398 &RandDur_as_number, /*tp_as_number*/
2399 0, /*tp_as_sequence*/
2400 0, /*tp_as_mapping*/
2401 0, /*tp_hash */
2402 0, /*tp_call*/
2403 0, /*tp_str*/
2404 0, /*tp_getattro*/
2405 0, /*tp_setattro*/
2406 0, /*tp_as_buffer*/
2407 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2408 "RandDur objects. Recursive time varying generation of random values.", /* tp_doc */
2409 (traverseproc)RandDur_traverse, /* tp_traverse */
2410 (inquiry)RandDur_clear, /* tp_clear */
2411 0, /* tp_richcompare */
2412 0, /* tp_weaklistoffset */
2413 0, /* tp_iter */
2414 0, /* tp_iternext */
2415 RandDur_methods, /* tp_methods */
2416 RandDur_members, /* tp_members */
2417 0, /* tp_getset */
2418 0, /* tp_base */
2419 0, /* tp_dict */
2420 0, /* tp_descr_get */
2421 0, /* tp_descr_set */
2422 0, /* tp_dictoffset */
2423 0, /* tp_init */
2424 0, /* tp_alloc */
2425 RandDur_new, /* tp_new */
2426 };
2427
2428 /****************/
2429 /**** Xnoise *****/
2430 /****************/
2431 typedef struct
2432 {
2433 pyo_audio_HEAD
2434 PyObject *x1;
2435 PyObject *x2;
2436 PyObject *freq;
2437 Stream *x1_stream;
2438 Stream *x2_stream;
2439 Stream *freq_stream;
•2440 MYFLT (*type_func_ptr)(void *);
2441 MYFLT xx1;
2442 MYFLT xx2;
2443 int type;
2444 MYFLT value;
2445 MYFLT time;
2446 MYFLT lastPoissonX1;
2447 int poisson_tab;
2448 MYFLT poisson_buffer[2000];
2449 MYFLT walkerValue;
2450 MYFLT loop_buffer[15];
2451 int loopChoice;
2452 int loopCountPlay;
2453 int loopTime;
2454 int loopCountRec;
2455 int loopLen;
2456 int loopStop;
2457 int modebuffer[5]; // need at least 2 slots for mul & add
2458 } Xnoise;
2459
2460 // no parameter
2461 static MYFLT
2462 Xnoise_uniform(Xnoise *self)
2463 {
2464 return RANDOM_UNIFORM;
2465 }
2466
2467 static MYFLT
2468 Xnoise_linear_min(Xnoise *self)
2469 {
2470 MYFLT a = RANDOM_UNIFORM;
2471 MYFLT b = RANDOM_UNIFORM;
2472
2473 if (a < b) return a;
2474 else return b;
2475 }
2476
2477 static MYFLT
2478 Xnoise_linear_max(Xnoise *self)
2479 {
2480 MYFLT a = RANDOM_UNIFORM;
2481 MYFLT b = RANDOM_UNIFORM;
2482
2483 if (a > b) return a;
2484 else return b;
2485 }
2486
2487 static MYFLT
2488 Xnoise_triangle(Xnoise *self)
2489 {
2490 MYFLT a = RANDOM_UNIFORM;
2491 MYFLT b = RANDOM_UNIFORM;
2492 return ((a + b) * 0.5);
2493 }
2494
2495 // x1 = slope
2496 static MYFLT
2497 Xnoise_expon_min(Xnoise *self)
2498 {
2499 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
2500
2501 MYFLT val = -MYLOG(RANDOM_UNIFORM) / self->xx1;
2502
2503 if (val < 0.0) return 0.0;
2504 else if (val > 1.0) return 1.0;
2505 else return val;
2506 }
2507
2508 static MYFLT
2509 Xnoise_expon_max(Xnoise *self)
2510 {
2511 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
2512
2513 MYFLT val = 1.0 - (-MYLOG(RANDOM_UNIFORM) / self->xx1);
2514
2515 if (val < 0.0) return 0.0;
2516 else if (val > 1.0) return 1.0;
2517 else return val;
2518 }
2519
2520 // x1 = bandwidth
2521 static MYFLT
2522 Xnoise_biexpon(Xnoise *self)
2523 {
2524 MYFLT polar, val;
2525
2526 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
2527
2528 MYFLT sum = RANDOM_UNIFORM * 2.0;
2529
2530 if (sum > 1.0)
2531 {
2532 polar = -1;
2533 sum = 2.0 - sum;
2534 }
2535 else
2536 polar = 1;
2537
2538 val = 0.5 * (polar * MYLOG(sum) / self->xx1) + 0.5;
2539
2540 if (val < 0.0) return 0.0;
2541 else if (val > 1.0) return 1.0;
2542 else return val;
2543 }
2544
2545 static MYFLT
2546 Xnoise_cauchy(Xnoise *self)
2547 {
2548 MYFLT rnd, val, dir;
2549
2550 do
2551 {
2552 rnd = RANDOM_UNIFORM;
2553 }
2554 while (rnd == 0.5);
2555
2556 if (pyorand() < (PYO_RAND_MAX / 2))
2557 dir = -1;
2558 else
2559 dir = 1;
2560
2561 val = 0.5 * (MYTAN(rnd) * self->xx1 * dir) + 0.5;
2562
2563 if (val < 0.0) return 0.0;
2564 else if (val > 1.0) return 1.0;
2565 else return val;
2566 }
2567
2568 // x1 = locator, x2 = shape
2569 static MYFLT
2570 Xnoise_weibull(Xnoise *self)
2571 {
2572 MYFLT rnd, val;
2573
2574 if (self->xx2 <= 0.0) self->xx2 = 0.00001;
2575
2576 rnd = 1.0 / (1.0 - RANDOM_UNIFORM);
2577 val = self->xx1 * MYPOW(MYLOG(rnd), (1.0 / self->xx2));
2578
2579 if (val < 0.0) return 0.0;
2580 else if (val > 1.0) return 1.0;
2581 else return val;
2582 }
2583
2584 // x1 = locator, x2 = bandwidth
2585 static MYFLT
2586 Xnoise_gaussian(Xnoise *self)
2587 {
2588 MYFLT rnd, val;
2589
2590 rnd = (RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM);
2591 val = (self->xx2 * (rnd - 3.0) * 0.33 + self->xx1);
2592
2593 if (val < 0.0) return 0.0;
2594 else if (val > 1.0) return 1.0;
2595 else return val;
2596 }
2597
2598 // x1 = gravity center, x2 = compress/expand
2599 static MYFLT
2600 Xnoise_poisson(Xnoise *self)
2601 {
2602 int i, j, factorial;
2603 long tot;
2604 MYFLT val;
2605
2606 if (self->xx1 < 0.1) self->xx1 = 0.1;
2607
2608 if (self->xx2 < 0.1) self->xx2 = 0.1;
2609
2610 if (self->xx1 != self->lastPoissonX1)
2611 {
2612 self->lastPoissonX1 = self->xx1;
2613 self->poisson_tab = 0;
2614 factorial = 1;
2615
2616 for (i = 1; i < 12; i++)
2617 {
2618 factorial *= i;
2619 tot = (long)(1000.0 * (MYPOW(2.7182818, -self->xx1) * MYPOW(self->xx1, i) / factorial));
2620
2621 for (j = 0; j < tot; j++)
2622 {
2623 self->poisson_buffer[self->poisson_tab] = i;
2624 self->poisson_tab++;
2625 }
2626 }
2627 }
2628
2629 val = self->poisson_buffer[pyorand() % self->poisson_tab] / 12.0 * self->xx2;
2630
2631 if (val < 0.0) return 0.0;
2632 else if (val > 1.0) return 1.0;
2633 else return val;
2634 }
2635
2636 // x1 = max value, x2 = max step
2637 static MYFLT
2638 Xnoise_walker(Xnoise *self)
2639 {
2640 unsigned int modulo, dir;
2641
2642 if (self->xx2 < 0.002) self->xx2 = 0.002;
2643
2644 modulo = (unsigned int)(self->xx2 * 1000.0);
2645 dir = pyorand() % 100;
2646
2647 if (dir < 50)
2648 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
2649 else
2650 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
2651
2652 if (self->walkerValue > self->xx1)
2653 self->walkerValue = self->xx1;
2654 else if (self->walkerValue < 0.0)
2655 self->walkerValue = 0.0;
2656
2657 return self->walkerValue;
2658 }
2659
2660 // x1 = max value, x2 = max step
2661 static MYFLT
2662 Xnoise_loopseg(Xnoise *self)
2663 {
2664 unsigned int modulo, dir;
2665
2666 if (self->loopChoice == 0)
2667 {
2668
2669 self->loopCountPlay = self->loopTime = 0;
2670
2671 if (self->xx2 < 0.002) self->xx2 = 0.002;
2672
2673 modulo = (unsigned int)(self->xx2 * 1000.0);
2674 dir = pyorand() % 100;
2675
2676 if (dir < 50)
2677 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
2678 else
2679 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
2680
2681 if (self->walkerValue > self->xx1)
2682 self->walkerValue = self->xx1;
2683 else if (self->walkerValue < 0.0)
2684 self->walkerValue = 0.0;
2685
2686 self->loop_buffer[self->loopCountRec++] = self->walkerValue;
2687
2688 if (self->loopCountRec < self->loopLen)
2689 self->loopChoice = 0;
2690 else
2691 {
2692 self->loopChoice = 1;
2693 self->loopStop = (pyorand() % 4) + 1;
2694 }
2695 }
2696 else
2697 {
2698 self->loopCountRec = 0;
2699
2700 self->walkerValue = self->loop_buffer[self->loopCountPlay++];
2701
2702 if (self->loopCountPlay < self->loopLen)
2703 self->loopChoice = 1;
2704 else
2705 {
2706 self->loopCountPlay = 0;
2707 self->loopTime++;
2708 }
2709
2710 if (self->loopTime == self->loopStop)
2711 {
2712 self->loopChoice = 0;
2713 self->loopLen = (pyorand() % 10) + 3;
2714 }
2715 }
2716
2717 return self->walkerValue;
2718 }
2719
2720 static void
2721 Xnoise_generate_iii(Xnoise *self)
2722 {
2723 int i;
2724 MYFLT inc;
2725 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
2726 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
2727 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
2728 inc = fr / self->sr;
2729
2730 for (i = 0; i < self->bufsize; i++)
2731 {
2732 self->time += inc;
2733
2734 if (self->time < 0.0)
2735 self->time += 1.0;
2736 else if (self->time >= 1.0)
2737 {
2738 self->time -= 1.0;
2739 self->value = (*self->type_func_ptr)(self);
2740 }
2741
2742 self->data[i] = self->value;
2743 }
2744 }
2745
2746 static void
2747 Xnoise_generate_aii(Xnoise *self)
2748 {
2749 int i;
2750 MYFLT inc;
2751 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
2752 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
2753 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
2754 inc = fr / self->sr;
2755
2756 for (i = 0; i < self->bufsize; i++)
2757 {
2758 self->time += inc;
2759
2760 if (self->time < 0.0)
2761 self->time += 1.0;
2762 else if (self->time >= 1.0)
2763 {
2764 self->time -= 1.0;
2765 self->xx1 = x1[i];
2766 self->value = (*self->type_func_ptr)(self);
2767 }
2768
2769 self->data[i] = self->value;
2770 }
2771 }
2772
2773 static void
2774 Xnoise_generate_iai(Xnoise *self)
2775 {
2776 int i;
2777 MYFLT inc;
2778 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
2779 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
2780 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
2781 inc = fr / self->sr;
2782
2783 for (i = 0; i < self->bufsize; i++)
2784 {
2785 self->time += inc;
2786
2787 if (self->time < 0.0)
2788 self->time += 1.0;
2789 else if (self->time >= 1.0)
2790 {
2791 self->time -= 1.0;
2792 self->xx2 = x2[i];
2793 self->value = (*self->type_func_ptr)(self);
2794 }
2795
2796 self->data[i] = self->value;
2797 }
2798 }
2799
2800 static void
2801 Xnoise_generate_aai(Xnoise *self)
2802 {
2803 int i;
2804 MYFLT inc;
2805 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
2806 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
2807 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
2808 inc = fr / self->sr;
2809
2810 for (i = 0; i < self->bufsize; i++)
2811 {
2812 self->time += inc;
2813
2814 if (self->time < 0.0)
2815 self->time += 1.0;
2816 else if (self->time >= 1.0)
2817 {
2818 self->time -= 1.0;
2819 self->xx1 = x1[i];
2820 self->xx2 = x2[i];
2821 self->value = (*self->type_func_ptr)(self);
2822 }
2823
2824 self->data[i] = self->value;
2825 }
2826 }
2827
2828 static void
2829 Xnoise_generate_iia(Xnoise *self)
2830 {
2831 int i;
2832 MYFLT inc;
2833 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
2834 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
2835 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
2836
2837 for (i = 0; i < self->bufsize; i++)
2838 {
2839 inc = fr[i] / self->sr;
2840 self->time += inc;
2841
2842 if (self->time < 0.0)
2843 self->time += 1.0;
2844 else if (self->time >= 1.0)
2845 {
2846 self->time -= 1.0;
2847 self->value = (*self->type_func_ptr)(self);
2848 }
2849
2850 self->data[i] = self->value;
2851 }
2852 }
2853
2854 static void
2855 Xnoise_generate_aia(Xnoise *self)
2856 {
2857 int i;
2858 MYFLT inc;
2859 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
2860 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
2861 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
2862
2863 for (i = 0; i < self->bufsize; i++)
2864 {
2865 inc = fr[i] / self->sr;
2866 self->time += inc;
2867
2868 if (self->time < 0.0)
2869 self->time += 1.0;
2870 else if (self->time >= 1.0)
2871 {
2872 self->time -= 1.0;
2873 self->xx1 = x1[i];
2874 self->value = (*self->type_func_ptr)(self);
2875 }
2876
2877 self->data[i] = self->value;
2878 }
2879 }
2880
2881 static void
2882 Xnoise_generate_iaa(Xnoise *self)
2883 {
2884 int i;
2885 MYFLT inc;
2886 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
2887 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
2888 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
2889
2890 for (i = 0; i < self->bufsize; i++)
2891 {
2892 inc = fr[i] / self->sr;
2893 self->time += inc;
2894
2895 if (self->time < 0.0)
2896 self->time += 1.0;
2897 else if (self->time >= 1.0)
2898 {
2899 self->time -= 1.0;
2900 self->xx2 = x2[i];
2901 self->value = (*self->type_func_ptr)(self);
2902 }
2903
2904 self->data[i] = self->value;
2905 }
2906 }
2907
2908 static void
2909 Xnoise_generate_aaa(Xnoise *self)
2910 {
2911 int i;
2912 MYFLT inc;
2913 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
2914 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
2915 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
2916
2917 for (i = 0; i < self->bufsize; i++)
2918 {
2919 inc = fr[i] / self->sr;
2920 self->time += inc;
2921
2922 if (self->time < 0.0)
2923 self->time += 1.0;
2924 else if (self->time >= 1.0)
2925 {
2926 self->time -= 1.0;
2927 self->xx1 = x1[i];
2928 self->xx2 = x2[i];
2929 self->value = (*self->type_func_ptr)(self);
2930 }
2931
2932 self->data[i] = self->value;
2933 }
2934 }
2935
2936 static void Xnoise_postprocessing_ii(Xnoise *self) { POST_PROCESSING_II };
2937 static void Xnoise_postprocessing_ai(Xnoise *self) { POST_PROCESSING_AI };
2938 static void Xnoise_postprocessing_ia(Xnoise *self) { POST_PROCESSING_IA };
2939 static void Xnoise_postprocessing_aa(Xnoise *self) { POST_PROCESSING_AA };
2940 static void Xnoise_postprocessing_ireva(Xnoise *self) { POST_PROCESSING_IREVA };
2941 static void Xnoise_postprocessing_areva(Xnoise *self) { POST_PROCESSING_AREVA };
2942 static void Xnoise_postprocessing_revai(Xnoise *self) { POST_PROCESSING_REVAI };
2943 static void Xnoise_postprocessing_revaa(Xnoise *self) { POST_PROCESSING_REVAA };
2944 static void Xnoise_postprocessing_revareva(Xnoise *self) { POST_PROCESSING_REVAREVA };
2945
2946 static void
2947 Xnoise_setRandomType(Xnoise *self)
2948 {
2949
2950 switch (self->type)
2951 {
2952 case 0:
2953 self->type_func_ptr = Xnoise_uniform;
2954 break;
2955
2956 case 1:
2957 self->type_func_ptr = Xnoise_linear_min;
2958 break;
2959
2960 case 2:
2961 self->type_func_ptr = Xnoise_linear_max;
2962 break;
2963
2964 case 3:
2965 self->type_func_ptr = Xnoise_triangle;
2966 break;
2967
2968 case 4:
2969 self->type_func_ptr = Xnoise_expon_min;
2970 break;
2971
2972 case 5:
2973 self->type_func_ptr = Xnoise_expon_max;
2974 break;
2975
2976 case 6:
2977 self->type_func_ptr = Xnoise_biexpon;
2978 break;
2979
2980 case 7:
2981 self->type_func_ptr = Xnoise_cauchy;
2982 break;
2983
2984 case 8:
2985 self->type_func_ptr = Xnoise_weibull;
2986 break;
2987
2988 case 9:
2989 self->type_func_ptr = Xnoise_gaussian;
2990 break;
2991
2992 case 10:
2993 self->type_func_ptr = Xnoise_poisson;
2994 break;
2995
2996 case 11:
2997 self->type_func_ptr = Xnoise_walker;
2998 break;
2999
3000 case 12:
3001 self->type_func_ptr = Xnoise_loopseg;
3002 break;
3003 }
3004 }
3005
3006 static void
3007 Xnoise_setProcMode(Xnoise *self)
3008 {
3009 int procmode, muladdmode;
3010 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
3011 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3012
3013 switch (procmode)
3014 {
3015 case 0:
3016 self->proc_func_ptr = Xnoise_generate_iii;
3017 break;
3018
3019 case 1:
3020 self->proc_func_ptr = Xnoise_generate_aii;
3021 break;
3022
3023 case 10:
3024 self->proc_func_ptr = Xnoise_generate_iai;
3025 break;
3026
3027 case 11:
3028 self->proc_func_ptr = Xnoise_generate_aai;
3029 break;
3030
3031 case 100:
3032 self->proc_func_ptr = Xnoise_generate_iia;
3033 break;
3034
3035 case 101:
3036 self->proc_func_ptr = Xnoise_generate_aia;
3037 break;
3038
3039 case 110:
3040 self->proc_func_ptr = Xnoise_generate_iaa;
3041 break;
3042
3043 case 111:
3044 self->proc_func_ptr = Xnoise_generate_aaa;
3045 break;
3046 }
3047
3048 switch (muladdmode)
3049 {
3050 case 0:
3051 self->muladd_func_ptr = Xnoise_postprocessing_ii;
3052 break;
3053
3054 case 1:
3055 self->muladd_func_ptr = Xnoise_postprocessing_ai;
3056 break;
3057
3058 case 2:
3059 self->muladd_func_ptr = Xnoise_postprocessing_revai;
3060 break;
3061
3062 case 10:
3063 self->muladd_func_ptr = Xnoise_postprocessing_ia;
3064 break;
3065
3066 case 11:
3067 self->muladd_func_ptr = Xnoise_postprocessing_aa;
3068 break;
3069
3070 case 12:
3071 self->muladd_func_ptr = Xnoise_postprocessing_revaa;
3072 break;
3073
3074 case 20:
3075 self->muladd_func_ptr = Xnoise_postprocessing_ireva;
3076 break;
3077
3078 case 21:
3079 self->muladd_func_ptr = Xnoise_postprocessing_areva;
3080 break;
3081
3082 case 22:
3083 self->muladd_func_ptr = Xnoise_postprocessing_revareva;
3084 break;
3085 }
3086 }
3087
3088 static void
3089 Xnoise_compute_next_data_frame(Xnoise *self)
3090 {
3091 (*self->proc_func_ptr)(self);
3092 (*self->muladd_func_ptr)(self);
3093 }
3094
3095 static int
3096 Xnoise_traverse(Xnoise *self, visitproc visit, void *arg)
3097 {
3098 pyo_VISIT
3099 Py_VISIT(self->freq);
3100 Py_VISIT(self->x1);
3101 Py_VISIT(self->x2);
3102 return 0;
3103 }
3104
3105 static int
3106 Xnoise_clear(Xnoise *self)
3107 {
3108 pyo_CLEAR
3109 Py_CLEAR(self->freq);
3110 Py_CLEAR(self->x1);
3111 Py_CLEAR(self->x2);
3112 return 0;
3113 }
3114
3115 static void
3116 Xnoise_dealloc(Xnoise* self)
3117 {
3118 pyo_DEALLOC
3119 Xnoise_clear(self);
3120 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3121 Py_TYPE(self)->tp_free((PyObject*)self);
3122 }
3123
3124 static PyObject *
3125 Xnoise_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3126 {
3127 int i;
3128 PyObject *freqtmp = NULL, *x1tmp = NULL, *x2tmp = NULL, *multmp = NULL, *addtmp = NULL;
3129 Xnoise *self;
3130 self = (Xnoise *)type->tp_alloc(type, 0);
3131
3132 self->x1 = PyFloat_FromDouble(0.5);
3133 self->x2 = PyFloat_FromDouble(0.5);
3134 self->freq = PyFloat_FromDouble(1.);
3135 self->xx1 = self->xx2 = self->walkerValue = 0.5;
3136 self->value = 0.0;
3137 self->time = 1.0;
3138 self->modebuffer[0] = 0;
3139 self->modebuffer[1] = 0;
3140 self->modebuffer[2] = 0;
3141 self->modebuffer[3] = 0;
3142 self->modebuffer[4] = 0;
3143
3144 INIT_OBJECT_COMMON
3145
3146 Server_generateSeed((Server *)self->server, XNOISE_ID);
3147
3148 self->poisson_tab = 0;
3149 self->lastPoissonX1 = -99.0;
3150
3151 for (i = 0; i < 2000; i++)
3152 {
3153 self->poisson_buffer[i] = 0.0;
3154 }
3155
3156 for (i = 0; i < 15; i++)
3157 {
3158 self->loop_buffer[i] = 0.0;
3159 }
3160
3161 self->loopChoice = self->loopCountPlay = self->loopTime = self->loopCountRec = self->loopStop = 0;
3162 self->loopLen = (pyorand() % 10) + 3;
3163
3164 Stream_setFunctionPtr(self->stream, Xnoise_compute_next_data_frame);
3165 self->mode_func_ptr = Xnoise_setProcMode;
3166
3167 static char *kwlist[] = {"type", "freq", "x1", "x2", "mul", "add", NULL};
3168
3169 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iOOOOO", kwlist, &self->type, &freqtmp, &x1tmp, &x2tmp, &multmp, &addtmp))
3170 Py_RETURN_NONE;
3171
3172 if (x1tmp)
3173 {
3174 PyObject_CallMethod((PyObject *)self, "setX1", "O", x1tmp);
3175 }
3176
3177 if (x2tmp)
3178 {
3179 PyObject_CallMethod((PyObject *)self, "setX2", "O", x2tmp);
3180 }
3181
3182 if (freqtmp)
3183 {
3184 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
3185 }
3186
3187 if (multmp)
3188 {
3189 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3190 }
3191
3192 if (addtmp)
3193 {
3194 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3195 }
3196
3197 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3198
3199 Xnoise_setRandomType(self);
3200
3201 (*self->mode_func_ptr)(self);
3202
3203 return (PyObject *)self;
3204 }
3205
3206 static PyObject * Xnoise_getServer(Xnoise* self) { GET_SERVER };
3207 static PyObject * Xnoise_getStream(Xnoise* self) { GET_STREAM };
3208 static PyObject * Xnoise_setMul(Xnoise *self, PyObject *arg) { SET_MUL };
3209 static PyObject * Xnoise_setAdd(Xnoise *self, PyObject *arg) { SET_ADD };
3210 static PyObject * Xnoise_setSub(Xnoise *self, PyObject *arg) { SET_SUB };
3211 static PyObject * Xnoise_setDiv(Xnoise *self, PyObject *arg) { SET_DIV };
3212
3213 static PyObject * Xnoise_play(Xnoise *self, PyObject *args, PyObject *kwds) { PLAY };
3214 static PyObject * Xnoise_out(Xnoise *self, PyObject *args, PyObject *kwds) { OUT };
3215 static PyObject * Xnoise_stop(Xnoise *self, PyObject *args, PyObject *kwds) { STOP };
3216
3217 static PyObject * Xnoise_multiply(Xnoise *self, PyObject *arg) { MULTIPLY };
3218 static PyObject * Xnoise_inplace_multiply(Xnoise *self, PyObject *arg) { INPLACE_MULTIPLY };
3219 static PyObject * Xnoise_add(Xnoise *self, PyObject *arg) { ADD };
3220 static PyObject * Xnoise_inplace_add(Xnoise *self, PyObject *arg) { INPLACE_ADD };
3221 static PyObject * Xnoise_sub(Xnoise *self, PyObject *arg) { SUB };
3222 static PyObject * Xnoise_inplace_sub(Xnoise *self, PyObject *arg) { INPLACE_SUB };
3223 static PyObject * Xnoise_div(Xnoise *self, PyObject *arg) { DIV };
3224 static PyObject * Xnoise_inplace_div(Xnoise *self, PyObject *arg) { INPLACE_DIV };
3225
3226 static PyObject *
3227 Xnoise_setType(Xnoise *self, PyObject *arg)
3228 {
3229 ASSERT_ARG_NOT_NULL
3230
3231 if (PyLong_Check(arg))
3232 {
3233 self->type = PyLong_AsLong(arg);
3234 Xnoise_setRandomType(self);
3235 }
3236
3237 Py_RETURN_NONE;
3238 }
3239
3240 static PyObject * Xnoise_setX1(Xnoise *self, PyObject *arg) { SET_PARAM(self->x1, self->x1_stream, 2); }
3241 static PyObject * Xnoise_setX2(Xnoise *self, PyObject *arg) { SET_PARAM(self->x2, self->x2_stream, 3); }
3242 static PyObject * Xnoise_setFreq(Xnoise *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 4); }
3243
3244 static PyMemberDef Xnoise_members[] =
3245 {
3246 {"server", T_OBJECT_EX, offsetof(Xnoise, server), 0, "Pyo server."},
3247 {"stream", T_OBJECT_EX, offsetof(Xnoise, stream), 0, "Stream object."},
3248 {"x1", T_OBJECT_EX, offsetof(Xnoise, x1), 0, "first param."},
3249 {"x2", T_OBJECT_EX, offsetof(Xnoise, x2), 0, "second param."},
3250 {"freq", T_OBJECT_EX, offsetof(Xnoise, freq), 0, "Polling frequency."},
3251 {"mul", T_OBJECT_EX, offsetof(Xnoise, mul), 0, "Mul factor."},
3252 {"add", T_OBJECT_EX, offsetof(Xnoise, add), 0, "Add factor."},
3253 {NULL} /* Sentinel */
3254 };
3255
3256 static PyMethodDef Xnoise_methods[] =
3257 {
3258 {"getServer", (PyCFunction)Xnoise_getServer, METH_NOARGS, "Returns server object."},
3259 {"_getStream", (PyCFunction)Xnoise_getStream, METH_NOARGS, "Returns stream object."},
3260 {"play", (PyCFunction)Xnoise_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3261 {"out", (PyCFunction)Xnoise_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3262 {"stop", (PyCFunction)Xnoise_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3263 {"setType", (PyCFunction)Xnoise_setType, METH_O, "Sets distribution type."},
3264 {"setX1", (PyCFunction)Xnoise_setX1, METH_O, "Sets first param."},
3265 {"setX2", (PyCFunction)Xnoise_setX2, METH_O, "Sets second param."},
3266 {"setFreq", (PyCFunction)Xnoise_setFreq, METH_O, "Sets polling frequency."},
3267 {"setMul", (PyCFunction)Xnoise_setMul, METH_O, "Sets oscillator mul factor."},
3268 {"setAdd", (PyCFunction)Xnoise_setAdd, METH_O, "Sets oscillator add factor."},
3269 {"setSub", (PyCFunction)Xnoise_setSub, METH_O, "Sets inverse add factor."},
3270 {"setDiv", (PyCFunction)Xnoise_setDiv, METH_O, "Sets inverse mul factor."},
3271 {NULL} /* Sentinel */
3272 };
3273
3274 static PyNumberMethods Xnoise_as_number =
3275 {
3276 (binaryfunc)Xnoise_add, /*nb_add*/
3277 (binaryfunc)Xnoise_sub, /*nb_subtract*/
3278 (binaryfunc)Xnoise_multiply, /*nb_multiply*/
3279 0, /*nb_remainder*/
3280 0, /*nb_divmod*/
3281 0, /*nb_power*/
3282 0, /*nb_neg*/
3283 0, /*nb_pos*/
3284 0, /*(unaryfunc)array_abs,*/
3285 0, /*nb_nonzero*/
3286 0, /*nb_invert*/
3287 0, /*nb_lshift*/
3288 0, /*nb_rshift*/
3289 0, /*nb_and*/
3290 0, /*nb_xor*/
3291 0, /*nb_or*/
3292 0, /*nb_int*/
3293 0, /*nb_long*/
3294 0, /*nb_float*/
3295 (binaryfunc)Xnoise_inplace_add, /*inplace_add*/
3296 (binaryfunc)Xnoise_inplace_sub, /*inplace_subtract*/
3297 (binaryfunc)Xnoise_inplace_multiply, /*inplace_multiply*/
3298 0, /*inplace_remainder*/
3299 0, /*inplace_power*/
3300 0, /*inplace_lshift*/
3301 0, /*inplace_rshift*/
3302 0, /*inplace_and*/
3303 0, /*inplace_xor*/
3304 0, /*inplace_or*/
3305 0, /*nb_floor_divide*/
3306 (binaryfunc)Xnoise_div, /*nb_true_divide*/
3307 0, /*nb_inplace_floor_divide*/
3308 (binaryfunc)Xnoise_inplace_div, /*nb_inplace_true_divide*/
3309 0, /* nb_index */
3310 };
3311
3312 PyTypeObject XnoiseType =
3313 {
3314 PyVarObject_HEAD_INIT(NULL, 0)
3315 "_pyo.Xnoise_base", /*tp_name*/
3316 sizeof(Xnoise), /*tp_basicsize*/
3317 0, /*tp_itemsize*/
3318 (destructor)Xnoise_dealloc, /*tp_dealloc*/
3319 0, /*tp_print*/
3320 0, /*tp_getattr*/
3321 0, /*tp_setattr*/
3322 0, /*tp_as_async (tp_compare in Python 2)*/
3323 0, /*tp_repr*/
3324 &Xnoise_as_number, /*tp_as_number*/
3325 0, /*tp_as_sequence*/
3326 0, /*tp_as_mapping*/
3327 0, /*tp_hash */
3328 0, /*tp_call*/
3329 0, /*tp_str*/
3330 0, /*tp_getattro*/
3331 0, /*tp_setattro*/
3332 0, /*tp_as_buffer*/
3333 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3334 "Xnoise objects. Periodically generates a new random value.", /* tp_doc */
3335 (traverseproc)Xnoise_traverse, /* tp_traverse */
3336 (inquiry)Xnoise_clear, /* tp_clear */
3337 0, /* tp_richcompare */
3338 0, /* tp_weaklistoffset */
3339 0, /* tp_iter */
3340 0, /* tp_iternext */
3341 Xnoise_methods, /* tp_methods */
3342 Xnoise_members, /* tp_members */
3343 0, /* tp_getset */
3344 0, /* tp_base */
3345 0, /* tp_dict */
3346 0, /* tp_descr_get */
3347 0, /* tp_descr_set */
3348 0, /* tp_dictoffset */
3349 0, /* tp_init */
3350 0, /* tp_alloc */
3351 Xnoise_new, /* tp_new */
3352 };
3353
3354 /****************/
3355 /**** XnoiseMidi *****/
3356 /****************/
3357 typedef struct
3358 {
3359 pyo_audio_HEAD
3360 PyObject *x1;
3361 PyObject *x2;
3362 PyObject *freq;
3363 Stream *x1_stream;
3364 Stream *x2_stream;
3365 Stream *freq_stream;
•3366 MYFLT (*type_func_ptr)(void *);
3367 int scale; // 0 = Midi, 1 = frequency, 2 = transpo
3368 MYFLT xx1;
3369 MYFLT xx2;
3370 int range_min;
3371 int range_max;
3372 int centralkey;
3373 int type;
3374 MYFLT value;
3375 MYFLT time;
3376 MYFLT lastPoissonX1;
3377 int poisson_tab;
3378 MYFLT poisson_buffer[2000];
3379 MYFLT walkerValue;
3380 MYFLT loop_buffer[15];
3381 int loopChoice;
3382 int loopCountPlay;
3383 int loopTime;
3384 int loopCountRec;
3385 int loopLen;
3386 int loopStop;
3387 int modebuffer[5]; // need at least 2 slots for mul & add
3388 } XnoiseMidi;
3389
3390 static MYFLT
3391 XnoiseMidi_convert(XnoiseMidi *self)
3392 {
3393 int midival;
3394 MYFLT val;
3395
3396 midival = (int)((self->value * (self->range_max - self->range_min)) + self->range_min);
3397
3398 if (midival < 0)
3399 midival = 0;
3400 else if (midival > 127)
3401 midival = 127;
3402
3403 if (self->scale == 0)
3404 val = (MYFLT)midival;
3405 else if (self->scale == 1)
3406 val = 8.1757989156437 * MYPOW(1.0594630943593, midival);
3407 else if (self->scale == 2)
3408 val = MYPOW(1.0594630943593, midival - self->centralkey);
3409 else
3410 val = midival;
3411
3412 return val;
3413 }
3414
3415 // no parameter
3416 static MYFLT
3417 XnoiseMidi_uniform(XnoiseMidi *self)
3418 {
3419 return RANDOM_UNIFORM;
3420 }
3421
3422 static MYFLT
3423 XnoiseMidi_linear_min(XnoiseMidi *self)
3424 {
3425 MYFLT a = RANDOM_UNIFORM;
3426 MYFLT b = RANDOM_UNIFORM;
3427
3428 if (a < b) return a;
3429 else return b;
3430 }
3431
3432 static MYFLT
3433 XnoiseMidi_linear_max(XnoiseMidi *self)
3434 {
3435 MYFLT a = RANDOM_UNIFORM;
3436 MYFLT b = RANDOM_UNIFORM;
3437
3438 if (a > b) return a;
3439 else return b;
3440 }
3441
3442 static MYFLT
3443 XnoiseMidi_triangle(XnoiseMidi *self)
3444 {
3445 MYFLT a = RANDOM_UNIFORM;
3446 MYFLT b = RANDOM_UNIFORM;
3447 return ((a + b) * 0.5);
3448 }
3449
3450 // x1 = slope
3451 static MYFLT
3452 XnoiseMidi_expon_min(XnoiseMidi *self)
3453 {
3454 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
3455
3456 MYFLT val = -MYLOG(RANDOM_UNIFORM) / self->xx1;
3457
3458 if (val < 0.0) return 0.0;
3459 else if (val > 1.0) return 1.0;
3460 else return val;
3461 }
3462
3463 static MYFLT
3464 XnoiseMidi_expon_max(XnoiseMidi *self)
3465 {
3466 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
3467
3468 MYFLT val = 1.0 - (-MYLOG(RANDOM_UNIFORM) / self->xx1);
3469
3470 if (val < 0.0) return 0.0;
3471 else if (val > 1.0) return 1.0;
3472 else return val;
3473 }
3474
3475 // x1 = bandwidth
3476 static MYFLT
3477 XnoiseMidi_biexpon(XnoiseMidi *self)
3478 {
3479 MYFLT polar, val;
3480
3481 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
3482
3483 MYFLT sum = RANDOM_UNIFORM * 2.0;
3484
3485 if (sum > 1.0)
3486 {
3487 polar = -1;
3488 sum = 2.0 - sum;
3489 }
3490 else
3491 polar = 1;
3492
3493 val = 0.5 * (polar * MYLOG(sum) / self->xx1) + 0.5;
3494
3495 if (val < 0.0) return 0.0;
3496 else if (val > 1.0) return 1.0;
3497 else return val;
3498 }
3499
3500 static MYFLT
3501 XnoiseMidi_cauchy(XnoiseMidi *self)
3502 {
3503 MYFLT rnd, val, dir;
3504
3505 do
3506 {
3507 rnd = RANDOM_UNIFORM;
3508 }
3509 while (rnd == 0.5);
3510
3511 if (pyorand() < (PYO_RAND_MAX / 2))
3512 dir = -1;
3513 else
3514 dir = 1;
3515
3516 val = 0.5 * (MYTAN(rnd) * self->xx1 * dir) + 0.5;
3517
3518 if (val < 0.0) return 0.0;
3519 else if (val > 1.0) return 1.0;
3520 else return val;
3521 }
3522
3523 // x1 = locator, x2 = shape
3524 static MYFLT
3525 XnoiseMidi_weibull(XnoiseMidi *self)
3526 {
3527 MYFLT rnd, val;
3528
3529 if (self->xx2 <= 0.0) self->xx2 = 0.00001;
3530
3531 rnd = 1.0 / (1.0 - RANDOM_UNIFORM);
3532 val = self->xx1 * MYPOW(MYLOG(rnd), (1.0 / self->xx2));
3533
3534 if (val < 0.0) return 0.0;
3535 else if (val > 1.0) return 1.0;
3536 else return val;
3537 }
3538
3539 // x1 = locator, x2 = bandwidth
3540 static MYFLT
3541 XnoiseMidi_gaussian(XnoiseMidi *self)
3542 {
3543 MYFLT rnd, val;
3544
3545 rnd = (RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM);
3546 val = (self->xx2 * (rnd - 3.0) * 0.33 + self->xx1);
3547
3548 if (val < 0.0) return 0.0;
3549 else if (val > 1.0) return 1.0;
3550 else return val;
3551 }
3552
3553 // x1 = gravity center, x2 = compress/expand
3554 static MYFLT
3555 XnoiseMidi_poisson(XnoiseMidi *self)
3556 {
3557 int i, j, factorial;
3558 long tot;
3559 MYFLT val;
3560
3561 if (self->xx1 < 0.1) self->xx1 = 0.1;
3562
3563 if (self->xx2 < 0.1) self->xx2 = 0.1;
3564
3565 if (self->xx1 != self->lastPoissonX1)
3566 {
3567 self->lastPoissonX1 = self->xx1;
3568 self->poisson_tab = 0;
3569 factorial = 1;
3570
3571 for (i = 1; i < 12; i++)
3572 {
3573 factorial *= i;
3574 tot = (long)(1000.0 * (MYPOW(2.7182818, -self->xx1) * MYPOW(self->xx1, i) / factorial));
3575
3576 for (j = 0; j < tot; j++)
3577 {
3578 self->poisson_buffer[self->poisson_tab] = i;
3579 self->poisson_tab++;
3580 }
3581 }
3582 }
3583
3584 val = self->poisson_buffer[pyorand() % self->poisson_tab] / 12.0 * self->xx2;
3585
3586 if (val < 0.0) return 0.0;
3587 else if (val > 1.0) return 1.0;
3588 else return val;
3589 }
3590
3591 // x1 = max value, x2 = max step
3592 static MYFLT
3593 XnoiseMidi_walker(XnoiseMidi *self)
3594 {
3595 unsigned int modulo, dir;
3596
3597 if (self->xx2 < 0.002) self->xx2 = 0.002;
3598
3599 modulo = (unsigned int)(self->xx2 * 1000.0);
3600 dir = pyorand() % 100;
3601
3602 if (dir < 50)
3603 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
3604 else
3605 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
3606
3607 if (self->walkerValue > self->xx1)
3608 self->walkerValue = self->xx1;
3609 else if (self->walkerValue < 0.0)
3610 self->walkerValue = 0.0;
3611
3612 return self->walkerValue;
3613 }
3614
3615 // x1 = max value, x2 = max step
3616 static MYFLT
3617 XnoiseMidi_loopseg(XnoiseMidi *self)
3618 {
3619 unsigned int modulo, dir;
3620
3621 if (self->loopChoice == 0)
3622 {
3623
3624 self->loopCountPlay = self->loopTime = 0;
3625
3626 if (self->xx2 < 0.002) self->xx2 = 0.002;
3627
3628 modulo = (unsigned int)(self->xx2 * 1000.0);
3629 dir = pyorand() % 100;
3630
3631 if (dir < 50)
3632 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
3633 else
3634 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
3635
3636 if (self->walkerValue > self->xx1)
3637 self->walkerValue = self->xx1;
3638 else if (self->walkerValue < 0.0)
3639 self->walkerValue = 0.0;
3640
3641 self->loop_buffer[self->loopCountRec++] = self->walkerValue;
3642
3643 if (self->loopCountRec < self->loopLen)
3644 self->loopChoice = 0;
3645 else
3646 {
3647 self->loopChoice = 1;
3648 self->loopStop = (pyorand() % 4) + 1;
3649 }
3650 }
3651 else
3652 {
3653 self->loopCountRec = 0;
3654
3655 self->walkerValue = self->loop_buffer[self->loopCountPlay++];
3656
3657 if (self->loopCountPlay < self->loopLen)
3658 self->loopChoice = 1;
3659 else
3660 {
3661 self->loopCountPlay = 0;
3662 self->loopTime++;
3663 }
3664
3665 if (self->loopTime == self->loopStop)
3666 {
3667 self->loopChoice = 0;
3668 self->loopLen = (pyorand() % 10) + 3;
3669 }
3670 }
3671
3672 return self->walkerValue;
3673 }
3674
3675 static void
3676 XnoiseMidi_generate_iii(XnoiseMidi *self)
3677 {
3678 int i;
3679 MYFLT inc;
3680 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
3681 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
3682 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
3683 inc = fr / self->sr;
3684
3685 for (i = 0; i < self->bufsize; i++)
3686 {
3687 self->time += inc;
3688
3689 if (self->time < 0.0)
3690 self->time += 1.0;
3691 else if (self->time >= 1.0)
3692 {
3693 self->time -= 1.0;
3694 self->value = (*self->type_func_ptr)(self);
3695 self->value = XnoiseMidi_convert(self);
3696 }
3697
3698 self->data[i] = self->value;
3699 }
3700 }
3701
3702 static void
3703 XnoiseMidi_generate_aii(XnoiseMidi *self)
3704 {
3705 int i;
3706 MYFLT inc;
3707 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
3708 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
3709 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
3710 inc = fr / self->sr;
3711
3712 for (i = 0; i < self->bufsize; i++)
3713 {
3714 self->time += inc;
3715
3716 if (self->time < 0.0)
3717 self->time += 1.0;
3718 else if (self->time >= 1.0)
3719 {
3720 self->time -= 1.0;
3721 self->xx1 = x1[i];
3722 self->value = (*self->type_func_ptr)(self);
3723 self->value = XnoiseMidi_convert(self);
3724 }
3725
3726 self->data[i] = self->value;
3727 }
3728 }
3729
3730 static void
3731 XnoiseMidi_generate_iai(XnoiseMidi *self)
3732 {
3733 int i;
3734 MYFLT inc;
3735 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
3736 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
3737 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
3738 inc = fr / self->sr;
3739
3740 for (i = 0; i < self->bufsize; i++)
3741 {
3742 self->time += inc;
3743
3744 if (self->time < 0.0)
3745 self->time += 1.0;
3746 else if (self->time >= 1.0)
3747 {
3748 self->time -= 1.0;
3749 self->xx2 = x2[i];
3750 self->value = (*self->type_func_ptr)(self);
3751 self->value = XnoiseMidi_convert(self);
3752 }
3753
3754 self->data[i] = self->value;
3755 }
3756 }
3757
3758 static void
3759 XnoiseMidi_generate_aai(XnoiseMidi *self)
3760 {
3761 int i;
3762 MYFLT inc;
3763 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
3764 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
3765 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
3766 inc = fr / self->sr;
3767
3768 for (i = 0; i < self->bufsize; i++)
3769 {
3770 self->time += inc;
3771
3772 if (self->time < 0.0)
3773 self->time += 1.0;
3774 else if (self->time >= 1.0)
3775 {
3776 self->time -= 1.0;
3777 self->xx1 = x1[i];
3778 self->xx2 = x2[i];
3779 self->value = (*self->type_func_ptr)(self);
3780 self->value = XnoiseMidi_convert(self);
3781 }
3782
3783 self->data[i] = self->value;
3784 }
3785 }
3786
3787 static void
3788 XnoiseMidi_generate_iia(XnoiseMidi *self)
3789 {
3790 int i;
3791 MYFLT inc;
3792 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
3793 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
3794 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
3795
3796 for (i = 0; i < self->bufsize; i++)
3797 {
3798 inc = fr[i] / self->sr;
3799 self->time += inc;
3800
3801 if (self->time < 0.0)
3802 self->time += 1.0;
3803 else if (self->time >= 1.0)
3804 {
3805 self->time -= 1.0;
3806 self->value = (*self->type_func_ptr)(self);
3807 self->value = XnoiseMidi_convert(self);
3808 }
3809
3810 self->data[i] = self->value;
3811 }
3812 }
3813
3814 static void
3815 XnoiseMidi_generate_aia(XnoiseMidi *self)
3816 {
3817 int i;
3818 MYFLT inc;
3819 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
3820 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
3821 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
3822
3823 for (i = 0; i < self->bufsize; i++)
3824 {
3825 inc = fr[i] / self->sr;
3826 self->time += inc;
3827
3828 if (self->time < 0.0)
3829 self->time += 1.0;
3830 else if (self->time >= 1.0)
3831 {
3832 self->time -= 1.0;
3833 self->xx1 = x1[i];
3834 self->value = (*self->type_func_ptr)(self);
3835 self->value = XnoiseMidi_convert(self);
3836 }
3837
3838 self->data[i] = self->value;
3839 }
3840 }
3841
3842 static void
3843 XnoiseMidi_generate_iaa(XnoiseMidi *self)
3844 {
3845 int i;
3846 MYFLT inc;
3847 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
3848 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
3849 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
3850
3851 for (i = 0; i < self->bufsize; i++)
3852 {
3853 inc = fr[i] / self->sr;
3854 self->time += inc;
3855
3856 if (self->time < 0.0)
3857 self->time += 1.0;
3858 else if (self->time >= 1.0)
3859 {
3860 self->time -= 1.0;
3861 self->xx2 = x2[i];
3862 self->value = (*self->type_func_ptr)(self);
3863 self->value = XnoiseMidi_convert(self);
3864 }
3865
3866 self->data[i] = self->value;
3867 }
3868 }
3869
3870 static void
3871 XnoiseMidi_generate_aaa(XnoiseMidi *self)
3872 {
3873 int i;
3874 MYFLT inc;
3875 MYFLT *x1 = Stream_getData((Stream *)self->x1_stream);
3876 MYFLT *x2 = Stream_getData((Stream *)self->x2_stream);
3877 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
3878
3879 for (i = 0; i < self->bufsize; i++)
3880 {
3881 inc = fr[i] / self->sr;
3882 self->time += inc;
3883
3884 if (self->time < 0.0)
3885 self->time += 1.0;
3886 else if (self->time >= 1.0)
3887 {
3888 self->time -= 1.0;
3889 self->xx1 = x1[i];
3890 self->xx2 = x2[i];
3891 self->value = (*self->type_func_ptr)(self);
3892 self->value = XnoiseMidi_convert(self);
3893 }
3894
3895 self->data[i] = self->value;
3896 }
3897 }
3898
3899 static void XnoiseMidi_postprocessing_ii(XnoiseMidi *self) { POST_PROCESSING_II };
3900 static void XnoiseMidi_postprocessing_ai(XnoiseMidi *self) { POST_PROCESSING_AI };
3901 static void XnoiseMidi_postprocessing_ia(XnoiseMidi *self) { POST_PROCESSING_IA };
3902 static void XnoiseMidi_postprocessing_aa(XnoiseMidi *self) { POST_PROCESSING_AA };
3903 static void XnoiseMidi_postprocessing_ireva(XnoiseMidi *self) { POST_PROCESSING_IREVA };
3904 static void XnoiseMidi_postprocessing_areva(XnoiseMidi *self) { POST_PROCESSING_AREVA };
3905 static void XnoiseMidi_postprocessing_revai(XnoiseMidi *self) { POST_PROCESSING_REVAI };
3906 static void XnoiseMidi_postprocessing_revaa(XnoiseMidi *self) { POST_PROCESSING_REVAA };
3907 static void XnoiseMidi_postprocessing_revareva(XnoiseMidi *self) { POST_PROCESSING_REVAREVA };
3908
3909 static void
3910 XnoiseMidi_setRandomType(XnoiseMidi *self)
3911 {
3912
3913 switch (self->type)
3914 {
3915 case 0:
3916 self->type_func_ptr = XnoiseMidi_uniform;
3917 break;
3918
3919 case 1:
3920 self->type_func_ptr = XnoiseMidi_linear_min;
3921 break;
3922
3923 case 2:
3924 self->type_func_ptr = XnoiseMidi_linear_max;
3925 break;
3926
3927 case 3:
3928 self->type_func_ptr = XnoiseMidi_triangle;
3929 break;
3930
3931 case 4:
3932 self->type_func_ptr = XnoiseMidi_expon_min;
3933 break;
3934
3935 case 5:
3936 self->type_func_ptr = XnoiseMidi_expon_max;
3937 break;
3938
3939 case 6:
3940 self->type_func_ptr = XnoiseMidi_biexpon;
3941 break;
3942
3943 case 7:
3944 self->type_func_ptr = XnoiseMidi_cauchy;
3945 break;
3946
3947 case 8:
3948 self->type_func_ptr = XnoiseMidi_weibull;
3949 break;
3950
3951 case 9:
3952 self->type_func_ptr = XnoiseMidi_gaussian;
3953 break;
3954
3955 case 10:
3956 self->type_func_ptr = XnoiseMidi_poisson;
3957 break;
3958
3959 case 11:
3960 self->type_func_ptr = XnoiseMidi_walker;
3961 break;
3962
3963 case 12:
3964 self->type_func_ptr = XnoiseMidi_loopseg;
3965 break;
3966 }
3967 }
3968
3969 static void
3970 XnoiseMidi_setProcMode(XnoiseMidi *self)
3971 {
3972 int procmode, muladdmode;
3973 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
3974 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3975
3976 switch (procmode)
3977 {
3978 case 0:
3979 self->proc_func_ptr = XnoiseMidi_generate_iii;
3980 break;
3981
3982 case 1:
3983 self->proc_func_ptr = XnoiseMidi_generate_aii;
3984 break;
3985
3986 case 10:
3987 self->proc_func_ptr = XnoiseMidi_generate_iai;
3988 break;
3989
3990 case 11:
3991 self->proc_func_ptr = XnoiseMidi_generate_aai;
3992 break;
3993
3994 case 100:
3995 self->proc_func_ptr = XnoiseMidi_generate_iia;
3996 break;
3997
3998 case 101:
3999 self->proc_func_ptr = XnoiseMidi_generate_aia;
4000 break;
4001
4002 case 110:
4003 self->proc_func_ptr = XnoiseMidi_generate_iaa;
4004 break;
4005
4006 case 111:
4007 self->proc_func_ptr = XnoiseMidi_generate_aaa;
4008 break;
4009 }
4010
4011 switch (muladdmode)
4012 {
4013 case 0:
4014 self->muladd_func_ptr = XnoiseMidi_postprocessing_ii;
4015 break;
4016
4017 case 1:
4018 self->muladd_func_ptr = XnoiseMidi_postprocessing_ai;
4019 break;
4020
4021 case 2:
4022 self->muladd_func_ptr = XnoiseMidi_postprocessing_revai;
4023 break;
4024
4025 case 10:
4026 self->muladd_func_ptr = XnoiseMidi_postprocessing_ia;
4027 break;
4028
4029 case 11:
4030 self->muladd_func_ptr = XnoiseMidi_postprocessing_aa;
4031 break;
4032
4033 case 12:
4034 self->muladd_func_ptr = XnoiseMidi_postprocessing_revaa;
4035 break;
4036
4037 case 20:
4038 self->muladd_func_ptr = XnoiseMidi_postprocessing_ireva;
4039 break;
4040
4041 case 21:
4042 self->muladd_func_ptr = XnoiseMidi_postprocessing_areva;
4043 break;
4044
4045 case 22:
4046 self->muladd_func_ptr = XnoiseMidi_postprocessing_revareva;
4047 break;
4048 }
4049 }
4050
4051 static void
4052 XnoiseMidi_compute_next_data_frame(XnoiseMidi *self)
4053 {
4054 (*self->proc_func_ptr)(self);
4055 (*self->muladd_func_ptr)(self);
4056 }
4057
4058 static int
4059 XnoiseMidi_traverse(XnoiseMidi *self, visitproc visit, void *arg)
4060 {
4061 pyo_VISIT
4062 Py_VISIT(self->freq);
4063 Py_VISIT(self->x1);
4064 Py_VISIT(self->x2);
4065 return 0;
4066 }
4067
4068 static int
4069 XnoiseMidi_clear(XnoiseMidi *self)
4070 {
4071 pyo_CLEAR
4072 Py_CLEAR(self->freq);
4073 Py_CLEAR(self->x1);
4074 Py_CLEAR(self->x2);
4075 return 0;
4076 }
4077
4078 static void
4079 XnoiseMidi_dealloc(XnoiseMidi* self)
4080 {
4081 pyo_DEALLOC
4082 XnoiseMidi_clear(self);
4083 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4084 Py_TYPE(self)->tp_free((PyObject*)self);
4085 }
4086
4087 static PyObject *
4088 XnoiseMidi_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4089 {
4090 int i;
4091 PyObject *freqtmp = NULL, *x1tmp = NULL, *x2tmp = NULL, *rangetmp = NULL, *multmp = NULL, *addtmp = NULL;
4092 XnoiseMidi *self;
4093 self = (XnoiseMidi *)type->tp_alloc(type, 0);
4094
4095 self->x1 = PyFloat_FromDouble(0.5);
4096 self->x2 = PyFloat_FromDouble(0.5);
4097 self->freq = PyFloat_FromDouble(1.);
4098 self->xx1 = self->xx2 = self->walkerValue = 0.5;
4099 self->value = 0.0;
4100 self->time = 1.0;
4101 self->scale = 0;
4102 self->range_min = 0;
4103 self->range_max = 127;
4104 self->centralkey = 64;
4105 self->modebuffer[0] = 0;
4106 self->modebuffer[1] = 0;
4107 self->modebuffer[2] = 0;
4108 self->modebuffer[3] = 0;
4109 self->modebuffer[4] = 0;
4110
4111 INIT_OBJECT_COMMON
4112
4113 Server_generateSeed((Server *)self->server, XNOISEMIDI_ID);
4114
4115 self->poisson_tab = 0;
4116 self->lastPoissonX1 = -99.0;
4117
4118 for (i = 0; i < 2000; i++)
4119 {
4120 self->poisson_buffer[i] = 0.0;
4121 }
4122
4123 for (i = 0; i < 15; i++)
4124 {
4125 self->loop_buffer[i] = 0.0;
4126 }
4127
4128 self->loopChoice = self->loopCountPlay = self->loopTime = self->loopCountRec = self->loopStop = 0;
4129 self->loopLen = (pyorand() % 10) + 3;
4130
4131 Stream_setFunctionPtr(self->stream, XnoiseMidi_compute_next_data_frame);
4132 self->mode_func_ptr = XnoiseMidi_setProcMode;
4133
4134 static char *kwlist[] = {"type", "freq", "x1", "x2", "scale", "range", "mul", "add", NULL};
4135
4136 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iOOOiOOO", kwlist, &self->type, &freqtmp, &x1tmp, &x2tmp, &self->scale, &rangetmp, &multmp, &addtmp))
4137 Py_RETURN_NONE;
4138
4139 if (x1tmp)
4140 {
4141 PyObject_CallMethod((PyObject *)self, "setX1", "O", x1tmp);
4142 }
4143
4144 if (x2tmp)
4145 {
4146 PyObject_CallMethod((PyObject *)self, "setX2", "O", x2tmp);
4147 }
4148
4149 if (freqtmp)
4150 {
4151 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
4152 }
4153
4154 if (rangetmp)
4155 {
4156 PyObject_CallMethod((PyObject *)self, "setRange", "O", rangetmp);
4157 }
4158
4159 if (multmp)
4160 {
4161 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4162 }
4163
4164 if (addtmp)
4165 {
4166 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4167 }
4168
4169 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4170
4171 XnoiseMidi_setRandomType(self);
4172
4173 (*self->mode_func_ptr)(self);
4174
4175 return (PyObject *)self;
4176 }
4177
4178 static PyObject * XnoiseMidi_getServer(XnoiseMidi* self) { GET_SERVER };
4179 static PyObject * XnoiseMidi_getStream(XnoiseMidi* self) { GET_STREAM };
4180 static PyObject * XnoiseMidi_setMul(XnoiseMidi *self, PyObject *arg) { SET_MUL };
4181 static PyObject * XnoiseMidi_setAdd(XnoiseMidi *self, PyObject *arg) { SET_ADD };
4182 static PyObject * XnoiseMidi_setSub(XnoiseMidi *self, PyObject *arg) { SET_SUB };
4183 static PyObject * XnoiseMidi_setDiv(XnoiseMidi *self, PyObject *arg) { SET_DIV };
4184
4185 static PyObject * XnoiseMidi_play(XnoiseMidi *self, PyObject *args, PyObject *kwds) { PLAY };
4186 static PyObject * XnoiseMidi_out(XnoiseMidi *self, PyObject *args, PyObject *kwds) { OUT };
4187 static PyObject * XnoiseMidi_stop(XnoiseMidi *self, PyObject *args, PyObject *kwds) { STOP };
4188
4189 static PyObject * XnoiseMidi_multiply(XnoiseMidi *self, PyObject *arg) { MULTIPLY };
4190 static PyObject * XnoiseMidi_inplace_multiply(XnoiseMidi *self, PyObject *arg) { INPLACE_MULTIPLY };
4191 static PyObject * XnoiseMidi_add(XnoiseMidi *self, PyObject *arg) { ADD };
4192 static PyObject * XnoiseMidi_inplace_add(XnoiseMidi *self, PyObject *arg) { INPLACE_ADD };
4193 static PyObject * XnoiseMidi_sub(XnoiseMidi *self, PyObject *arg) { SUB };
4194 static PyObject * XnoiseMidi_inplace_sub(XnoiseMidi *self, PyObject *arg) { INPLACE_SUB };
4195 static PyObject * XnoiseMidi_div(XnoiseMidi *self, PyObject *arg) { DIV };
4196 static PyObject * XnoiseMidi_inplace_div(XnoiseMidi *self, PyObject *arg) { INPLACE_DIV };
4197
4198 static PyObject *
4199 XnoiseMidi_setType(XnoiseMidi *self, PyObject *arg)
4200 {
4201 ASSERT_ARG_NOT_NULL
4202
4203 if (PyLong_Check(arg))
4204 {
4205 self->type = PyLong_AsLong(arg);
4206 XnoiseMidi_setRandomType(self);
4207 }
4208
4209 Py_RETURN_NONE;
4210 }
4211
4212 static PyObject *
4213 XnoiseMidi_setScale(XnoiseMidi *self, PyObject *arg)
4214 {
4215 int tmp;
4216
4217 ASSERT_ARG_NOT_NULL
4218
4219 if (PyLong_Check(arg))
4220 {
4221 tmp = PyLong_AsLong(arg);
4222
4223 if (tmp >= 0 && tmp <= 2)
4224 self->scale = tmp;
4225 else
4226 PySys_WriteStdout("XnoiseMidi: scale attribute must be an integer {0, 1, 2}\n");
4227 }
4228
4229 Py_RETURN_NONE;
4230 }
4231
4232 static PyObject *
4233 XnoiseMidi_setRange(XnoiseMidi *self, PyObject *args)
4234 {
4235 if (args == NULL)
4236 {
4237 Py_RETURN_NONE;
4238 }
4239
4240 int isTuple = PyTuple_Check(args);
4241
4242 if (isTuple == 1)
4243 {
4244 self->range_min = PyLong_AsLong(PyTuple_GET_ITEM(args, 0));
4245 self->range_max = PyLong_AsLong(PyTuple_GET_ITEM(args, 1));
4246 self->centralkey = (int)((self->range_max + self->range_min) / 2);
4247 }
4248
4249 Py_RETURN_NONE;
4250 }
4251
4252 static PyObject * XnoiseMidi_setX1(XnoiseMidi *self, PyObject *arg) { SET_PARAM(self->x1, self->x1_stream, 2); }
4253 static PyObject * XnoiseMidi_setX2(XnoiseMidi *self, PyObject *arg) { SET_PARAM(self->x2, self->x2_stream, 3); }
4254 static PyObject * XnoiseMidi_setFreq(XnoiseMidi *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 4); }
4255
4256 static PyMemberDef XnoiseMidi_members[] =
4257 {
4258 {"server", T_OBJECT_EX, offsetof(XnoiseMidi, server), 0, "Pyo server."},
4259 {"stream", T_OBJECT_EX, offsetof(XnoiseMidi, stream), 0, "Stream object."},
4260 {"x1", T_OBJECT_EX, offsetof(XnoiseMidi, x1), 0, "first param."},
4261 {"x2", T_OBJECT_EX, offsetof(XnoiseMidi, x2), 0, "second param."},
4262 {"freq", T_OBJECT_EX, offsetof(XnoiseMidi, freq), 0, "Polling frequency."},
4263 {"mul", T_OBJECT_EX, offsetof(XnoiseMidi, mul), 0, "Mul factor."},
4264 {"add", T_OBJECT_EX, offsetof(XnoiseMidi, add), 0, "Add factor."},
4265 {NULL} /* Sentinel */
4266 };
4267
4268 static PyMethodDef XnoiseMidi_methods[] =
4269 {
4270 {"getServer", (PyCFunction)XnoiseMidi_getServer, METH_NOARGS, "Returns server object."},
4271 {"_getStream", (PyCFunction)XnoiseMidi_getStream, METH_NOARGS, "Returns stream object."},
4272 {"play", (PyCFunction)XnoiseMidi_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4273 {"out", (PyCFunction)XnoiseMidi_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
4274 {"stop", (PyCFunction)XnoiseMidi_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4275 {"setType", (PyCFunction)XnoiseMidi_setType, METH_O, "Sets distribution type."},
4276 {"setScale", (PyCFunction)XnoiseMidi_setScale, METH_O, "Sets output scale."},
4277 {"setRange", (PyCFunction)XnoiseMidi_setRange, METH_VARARGS, "Sets range in midi notes (min, max)."},
4278 {"setX1", (PyCFunction)XnoiseMidi_setX1, METH_O, "Sets first param."},
4279 {"setX2", (PyCFunction)XnoiseMidi_setX2, METH_O, "Sets second param."},
4280 {"setFreq", (PyCFunction)XnoiseMidi_setFreq, METH_O, "Sets polling frequency."},
4281 {"setMul", (PyCFunction)XnoiseMidi_setMul, METH_O, "Sets oscillator mul factor."},
4282 {"setAdd", (PyCFunction)XnoiseMidi_setAdd, METH_O, "Sets oscillator add factor."},
4283 {"setSub", (PyCFunction)XnoiseMidi_setSub, METH_O, "Sets inverse add factor."},
4284 {"setDiv", (PyCFunction)XnoiseMidi_setDiv, METH_O, "Sets inverse mul factor."},
4285 {NULL} /* Sentinel */
4286 };
4287
4288 static PyNumberMethods XnoiseMidi_as_number =
4289 {
4290 (binaryfunc)XnoiseMidi_add, /*nb_add*/
4291 (binaryfunc)XnoiseMidi_sub, /*nb_subtract*/
4292 (binaryfunc)XnoiseMidi_multiply, /*nb_multiply*/
4293 0, /*nb_remainder*/
4294 0, /*nb_divmod*/
4295 0, /*nb_power*/
4296 0, /*nb_neg*/
4297 0, /*nb_pos*/
4298 0, /*(unaryfunc)array_abs,*/
4299 0, /*nb_nonzero*/
4300 0, /*nb_invert*/
4301 0, /*nb_lshift*/
4302 0, /*nb_rshift*/
4303 0, /*nb_and*/
4304 0, /*nb_xor*/
4305 0, /*nb_or*/
4306 0, /*nb_int*/
4307 0, /*nb_long*/
4308 0, /*nb_float*/
4309 (binaryfunc)XnoiseMidi_inplace_add, /*inplace_add*/
4310 (binaryfunc)XnoiseMidi_inplace_sub, /*inplace_subtract*/
4311 (binaryfunc)XnoiseMidi_inplace_multiply, /*inplace_multiply*/
4312 0, /*inplace_remainder*/
4313 0, /*inplace_power*/
4314 0, /*inplace_lshift*/
4315 0, /*inplace_rshift*/
4316 0, /*inplace_and*/
4317 0, /*inplace_xor*/
4318 0, /*inplace_or*/
4319 0, /*nb_floor_divide*/
4320 (binaryfunc)XnoiseMidi_div, /*nb_true_divide*/
4321 0, /*nb_inplace_floor_divide*/
4322 (binaryfunc)XnoiseMidi_inplace_div, /*nb_inplace_true_divide*/
4323 0, /* nb_index */
4324 };
4325
4326 PyTypeObject XnoiseMidiType =
4327 {
4328 PyVarObject_HEAD_INIT(NULL, 0)
4329 "_pyo.XnoiseMidi_base", /*tp_name*/
4330 sizeof(XnoiseMidi), /*tp_basicsize*/
4331 0, /*tp_itemsize*/
4332 (destructor)XnoiseMidi_dealloc, /*tp_dealloc*/
4333 0, /*tp_print*/
4334 0, /*tp_getattr*/
4335 0, /*tp_setattr*/
4336 0, /*tp_as_async (tp_compare in Python 2)*/
4337 0, /*tp_repr*/
4338 &XnoiseMidi_as_number, /*tp_as_number*/
4339 0, /*tp_as_sequence*/
4340 0, /*tp_as_mapping*/
4341 0, /*tp_hash */
4342 0, /*tp_call*/
4343 0, /*tp_str*/
4344 0, /*tp_getattro*/
4345 0, /*tp_setattro*/
4346 0, /*tp_as_buffer*/
4347 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4348 "XnoiseMidi objects. Periodically generates a new random value.", /* tp_doc */
4349 (traverseproc)XnoiseMidi_traverse, /* tp_traverse */
4350 (inquiry)XnoiseMidi_clear, /* tp_clear */
4351 0, /* tp_richcompare */
4352 0, /* tp_weaklistoffset */
4353 0, /* tp_iter */
4354 0, /* tp_iternext */
4355 XnoiseMidi_methods, /* tp_methods */
4356 XnoiseMidi_members, /* tp_members */
4357 0, /* tp_getset */
4358 0, /* tp_base */
4359 0, /* tp_dict */
4360 0, /* tp_descr_get */
4361 0, /* tp_descr_set */
4362 0, /* tp_dictoffset */
4363 0, /* tp_init */
4364 0, /* tp_alloc */
4365 XnoiseMidi_new, /* tp_new */
4366 };
4367
4368 /****************/
4369 /**** XnoiseDur *****/
4370 /****************/
4371 typedef struct
4372 {
4373 pyo_audio_HEAD
4374 PyObject *x1;
4375 PyObject *x2;
4376 PyObject *min;
4377 PyObject *max;
4378 Stream *x1_stream;
4379 Stream *x2_stream;
4380 Stream *min_stream;
4381 Stream *max_stream;
•4382 MYFLT (*type_func_ptr)(void *);
4383 MYFLT xx1;
4384 MYFLT xx2;
4385 int type;
4386 MYFLT value;
4387 MYFLT time;
4388 MYFLT inc;
4389 MYFLT lastPoissonX1;
4390 int poisson_tab;
4391 MYFLT poisson_buffer[2000];
4392 MYFLT walkerValue;
4393 MYFLT loop_buffer[15];
4394 int loopChoice;
4395 int loopCountPlay;
4396 int loopTime;
4397 int loopCountRec;
4398 int loopLen;
4399 int loopStop;
4400 int modebuffer[6]; // need at least 2 slots for mul & add
4401 } XnoiseDur;
4402
4403 // no parameter
4404 static MYFLT
4405 XnoiseDur_uniform(XnoiseDur *self)
4406 {
4407 return RANDOM_UNIFORM;
4408 }
4409
4410 static MYFLT
4411 XnoiseDur_linear_min(XnoiseDur *self)
4412 {
4413 MYFLT a = RANDOM_UNIFORM;
4414 MYFLT b = RANDOM_UNIFORM;
4415
4416 if (a < b) return a;
4417 else return b;
4418 }
4419
4420 static MYFLT
4421 XnoiseDur_linear_max(XnoiseDur *self)
4422 {
4423 MYFLT a = RANDOM_UNIFORM;
4424 MYFLT b = RANDOM_UNIFORM;
4425
4426 if (a > b) return a;
4427 else return b;
4428 }
4429
4430 static MYFLT
4431 XnoiseDur_triangle(XnoiseDur *self)
4432 {
4433 MYFLT a = RANDOM_UNIFORM;
4434 MYFLT b = RANDOM_UNIFORM;
4435 return ((a + b) * 0.5);
4436 }
4437
4438 // x1 = slope
4439 static MYFLT
4440 XnoiseDur_expon_min(XnoiseDur *self)
4441 {
4442 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
4443
4444 MYFLT val = -MYLOG(RANDOM_UNIFORM) / self->xx1;
4445
4446 if (val < 0.0) return 0.0;
4447 else if (val > 1.0) return 1.0;
4448 else return val;
4449 }
4450
4451 static MYFLT
4452 XnoiseDur_expon_max(XnoiseDur *self)
4453 {
4454 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
4455
4456 MYFLT val = 1.0 - (-MYLOG(RANDOM_UNIFORM) / self->xx1);
4457
4458 if (val < 0.0) return 0.0;
4459 else if (val > 1.0) return 1.0;
4460 else return val;
4461 }
4462
4463 // x1 = bandwidth
4464 static MYFLT
4465 XnoiseDur_biexpon(XnoiseDur *self)
4466 {
4467 MYFLT polar, val;
4468
4469 if (self->xx1 <= 0.0) self->xx1 = 0.00001;
4470
4471 MYFLT sum = RANDOM_UNIFORM * 2.0;
4472
4473 if (sum > 1.0)
4474 {
4475 polar = -1;
4476 sum = 2.0 - sum;
4477 }
4478 else
4479 polar = 1;
4480
4481 val = 0.5 * (polar * MYLOG(sum) / self->xx1) + 0.5;
4482
4483 if (val < 0.0) return 0.0;
4484 else if (val > 1.0) return 1.0;
4485 else return val;
4486 }
4487
4488 static MYFLT
4489 XnoiseDur_cauchy(XnoiseDur *self)
4490 {
4491 MYFLT rnd, val, dir;
4492
4493 do
4494 {
4495 rnd = RANDOM_UNIFORM;
4496 }
4497 while (rnd == 0.5);
4498
4499 if (pyorand() < (PYO_RAND_MAX / 2))
4500 dir = -1;
4501 else
4502 dir = 1;
4503
4504 val = 0.5 * (MYTAN(rnd) * self->xx1 * dir) + 0.5;
4505
4506 if (val < 0.0) return 0.0;
4507 else if (val > 1.0) return 1.0;
4508 else return val;
4509 }
4510
4511 // x1 = locator, x2 = shape
4512 static MYFLT
4513 XnoiseDur_weibull(XnoiseDur *self)
4514 {
4515 MYFLT rnd, val;
4516
4517 if (self->xx2 <= 0.0) self->xx2 = 0.00001;
4518
4519 rnd = 1.0 / (1.0 - RANDOM_UNIFORM);
4520 val = self->xx1 * MYPOW(MYLOG(rnd), (1.0 / self->xx2));
4521
4522 if (val < 0.0) return 0.0;
4523 else if (val > 1.0) return 1.0;
4524 else return val;
4525 }
4526
4527 // x1 = locator, x2 = bandwidth
4528 static MYFLT
4529 XnoiseDur_gaussian(XnoiseDur *self)
4530 {
4531 MYFLT rnd, val;
4532
4533 rnd = (RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM);
4534 val = (self->xx2 * (rnd - 3.0) * 0.33 + self->xx1);
4535
4536 if (val < 0.0) return 0.0;
4537 else if (val > 1.0) return 1.0;
4538 else return val;
4539 }
4540
4541 // x1 = gravity center, x2 = compress/expand
4542 static MYFLT
4543 XnoiseDur_poisson(XnoiseDur *self)
4544 {
4545 int i, j, factorial;
4546 long tot;
4547 MYFLT val;
4548
4549 if (self->xx1 < 0.1) self->xx1 = 0.1;
4550
4551 if (self->xx2 < 0.1) self->xx2 = 0.1;
4552
4553 if (self->xx1 != self->lastPoissonX1)
4554 {
4555 self->lastPoissonX1 = self->xx1;
4556 self->poisson_tab = 0;
4557 factorial = 1;
4558
4559 for (i = 1; i < 12; i++)
4560 {
4561 factorial *= i;
4562 tot = (long)(1000.0 * (MYPOW(2.7182818, -self->xx1) * MYPOW(self->xx1, i) / factorial));
4563
4564 for (j = 0; j < tot; j++)
4565 {
4566 self->poisson_buffer[self->poisson_tab] = i;
4567 self->poisson_tab++;
4568 }
4569 }
4570 }
4571
4572 val = self->poisson_buffer[pyorand() % self->poisson_tab] / 12.0 * self->xx2;
4573
4574 if (val < 0.0) return 0.0;
4575 else if (val > 1.0) return 1.0;
4576 else return val;
4577 }
4578
4579 // x1 = max value, x2 = max step
4580 static MYFLT
4581 XnoiseDur_walker(XnoiseDur *self)
4582 {
4583 unsigned int modulo, dir;
4584
4585 if (self->xx2 < 0.002) self->xx2 = 0.002;
4586
4587 modulo = (unsigned int)(self->xx2 * 1000.0);
4588 dir = pyorand() % 100;
4589
4590 if (dir < 50)
4591 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
4592 else
4593 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
4594
4595 if (self->walkerValue > self->xx1)
4596 self->walkerValue = self->xx1;
4597 else if (self->walkerValue < 0.0)
4598 self->walkerValue = 0.0;
4599
4600 return self->walkerValue;
4601 }
4602
4603 // x1 = max value, x2 = max step
4604 static MYFLT
4605 XnoiseDur_loopseg(XnoiseDur *self)
4606 {
4607 unsigned int modulo, dir;
4608
4609 if (self->loopChoice == 0)
4610 {
4611
4612 self->loopCountPlay = self->loopTime = 0;
4613
4614 if (self->xx2 < 0.002) self->xx2 = 0.002;
4615
4616 modulo = (unsigned int)(self->xx2 * 1000.0);
4617 dir = pyorand() % 100;
4618
4619 if (dir < 50)
4620 self->walkerValue += ((MYFLT)(pyorand() % modulo) * 0.001);
4621 else
4622 self->walkerValue -= ((MYFLT)(pyorand() % modulo) * 0.001);
4623
4624 if (self->walkerValue > self->xx1)
4625 self->walkerValue = self->xx1;
4626 else if (self->walkerValue < 0.0)
4627 self->walkerValue = 0.0;
4628
4629 self->loop_buffer[self->loopCountRec++] = self->walkerValue;
4630
4631 if (self->loopCountRec < self->loopLen)
4632 self->loopChoice = 0;
4633 else
4634 {
4635 self->loopChoice = 1;
4636 self->loopStop = (pyorand() % 4) + 1;
4637 }
4638 }
4639 else
4640 {
4641 self->loopCountRec = 0;
4642
4643 self->walkerValue = self->loop_buffer[self->loopCountPlay++];
4644
4645 if (self->loopCountPlay < self->loopLen)
4646 self->loopChoice = 1;
4647 else
4648 {
4649 self->loopCountPlay = 0;
4650 self->loopTime++;
4651 }
4652
4653 if (self->loopTime == self->loopStop)
4654 {
4655 self->loopChoice = 0;
4656 self->loopLen = (pyorand() % 10) + 3;
4657 }
4658 }
4659
4660 return self->walkerValue;
4661 }
4662
4663 static void
4664 XnoiseDur_generate(XnoiseDur *self)
4665 {
4666 int i;
4667 MYFLT min, max;
4668
4669 for (i = 0; i < self->bufsize; i++)
4670 {
4671 self->time += self->inc;
4672
4673 if (self->time < 0.0)
4674 self->time += 1.0;
4675 else if (self->time >= 1.0)
4676 {
4677 self->time -= 1.0;
4678
4679 if (self->modebuffer[2] == 0)
4680 self->xx1 = PyFloat_AS_DOUBLE(self->x1);
4681 else
4682 self->xx1 = Stream_getData((Stream *)self->x1_stream)[i];
4683
4684 if (self->modebuffer[3] == 0)
4685 self->xx2 = PyFloat_AS_DOUBLE(self->x2);
4686 else
4687 self->xx2 = Stream_getData((Stream *)self->x2_stream)[i];
4688
4689 if (self->modebuffer[4] == 0)
4690 min = PyFloat_AS_DOUBLE(self->min);
4691 else
4692 min = Stream_getData((Stream *)self->min_stream)[i];
4693
4694 if (self->modebuffer[5] == 0)
4695 max = PyFloat_AS_DOUBLE(self->max);
4696 else
4697 max = Stream_getData((Stream *)self->max_stream)[i];
4698
4699 if (min > max)
4700 max = min;
4701
4702 self->value = (*self->type_func_ptr)(self) * (max - min) + min;
4703
4704 if (self->value == 0.0)
4705 self->inc = 0.0;
4706 else
4707 self->inc = (1.0 / self->value) / self->sr;
4708 }
4709
4710 self->data[i] = self->value;
4711 }
4712 }
4713
4714 static void XnoiseDur_postprocessing_ii(XnoiseDur *self) { POST_PROCESSING_II };
4715 static void XnoiseDur_postprocessing_ai(XnoiseDur *self) { POST_PROCESSING_AI };
4716 static void XnoiseDur_postprocessing_ia(XnoiseDur *self) { POST_PROCESSING_IA };
4717 static void XnoiseDur_postprocessing_aa(XnoiseDur *self) { POST_PROCESSING_AA };
4718 static void XnoiseDur_postprocessing_ireva(XnoiseDur *self) { POST_PROCESSING_IREVA };
4719 static void XnoiseDur_postprocessing_areva(XnoiseDur *self) { POST_PROCESSING_AREVA };
4720 static void XnoiseDur_postprocessing_revai(XnoiseDur *self) { POST_PROCESSING_REVAI };
4721 static void XnoiseDur_postprocessing_revaa(XnoiseDur *self) { POST_PROCESSING_REVAA };
4722 static void XnoiseDur_postprocessing_revareva(XnoiseDur *self) { POST_PROCESSING_REVAREVA };
4723
4724 static void
4725 XnoiseDur_setRandomType(XnoiseDur *self)
4726 {
4727
4728 switch (self->type)
4729 {
4730 case 0:
4731 self->type_func_ptr = XnoiseDur_uniform;
4732 break;
4733
4734 case 1:
4735 self->type_func_ptr = XnoiseDur_linear_min;
4736 break;
4737
4738 case 2:
4739 self->type_func_ptr = XnoiseDur_linear_max;
4740 break;
4741
4742 case 3:
4743 self->type_func_ptr = XnoiseDur_triangle;
4744 break;
4745
4746 case 4:
4747 self->type_func_ptr = XnoiseDur_expon_min;
4748 break;
4749
4750 case 5:
4751 self->type_func_ptr = XnoiseDur_expon_max;
4752 break;
4753
4754 case 6:
4755 self->type_func_ptr = XnoiseDur_biexpon;
4756 break;
4757
4758 case 7:
4759 self->type_func_ptr = XnoiseDur_cauchy;
4760 break;
4761
4762 case 8:
4763 self->type_func_ptr = XnoiseDur_weibull;
4764 break;
4765
4766 case 9:
4767 self->type_func_ptr = XnoiseDur_gaussian;
4768 break;
4769
4770 case 10:
4771 self->type_func_ptr = XnoiseDur_poisson;
4772 break;
4773
4774 case 11:
4775 self->type_func_ptr = XnoiseDur_walker;
4776 break;
4777
4778 case 12:
4779 self->type_func_ptr = XnoiseDur_loopseg;
4780 break;
4781 }
4782 }
4783
4784 static void
4785 XnoiseDur_setProcMode(XnoiseDur *self)
4786 {
4787 int muladdmode;
4788 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
4789
4790 self->proc_func_ptr = XnoiseDur_generate;
4791
4792 switch (muladdmode)
4793 {
4794 case 0:
4795 self->muladd_func_ptr = XnoiseDur_postprocessing_ii;
4796 break;
4797
4798 case 1:
4799 self->muladd_func_ptr = XnoiseDur_postprocessing_ai;
4800 break;
4801
4802 case 2:
4803 self->muladd_func_ptr = XnoiseDur_postprocessing_revai;
4804 break;
4805
4806 case 10:
4807 self->muladd_func_ptr = XnoiseDur_postprocessing_ia;
4808 break;
4809
4810 case 11:
4811 self->muladd_func_ptr = XnoiseDur_postprocessing_aa;
4812 break;
4813
4814 case 12:
4815 self->muladd_func_ptr = XnoiseDur_postprocessing_revaa;
4816 break;
4817
4818 case 20:
4819 self->muladd_func_ptr = XnoiseDur_postprocessing_ireva;
4820 break;
4821
4822 case 21:
4823 self->muladd_func_ptr = XnoiseDur_postprocessing_areva;
4824 break;
4825
4826 case 22:
4827 self->muladd_func_ptr = XnoiseDur_postprocessing_revareva;
4828 break;
4829 }
4830 }
4831
4832 static void
4833 XnoiseDur_compute_next_data_frame(XnoiseDur *self)
4834 {
4835 (*self->proc_func_ptr)(self);
4836 (*self->muladd_func_ptr)(self);
4837 }
4838
4839 static int
4840 XnoiseDur_traverse(XnoiseDur *self, visitproc visit, void *arg)
4841 {
4842 pyo_VISIT
4843 Py_VISIT(self->min);
4844 Py_VISIT(self->max);
4845 Py_VISIT(self->x1);
4846 Py_VISIT(self->x2);
4847 return 0;
4848 }
4849
4850 static int
4851 XnoiseDur_clear(XnoiseDur *self)
4852 {
4853 pyo_CLEAR
4854 Py_CLEAR(self->min);
4855 Py_CLEAR(self->max);
4856 Py_CLEAR(self->x1);
4857 Py_CLEAR(self->x2);
4858 return 0;
4859 }
4860
4861 static void
4862 XnoiseDur_dealloc(XnoiseDur* self)
4863 {
4864 pyo_DEALLOC
4865 XnoiseDur_clear(self);
4866 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4867 Py_TYPE(self)->tp_free((PyObject*)self);
4868 }
4869
4870 static PyObject *
4871 XnoiseDur_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4872 {
4873 int i;
4874 MYFLT mi, ma;
4875 PyObject *mintmp = NULL, *maxtmp = NULL, *x1tmp = NULL, *x2tmp = NULL, *multmp = NULL, *addtmp = NULL;
4876 XnoiseDur *self;
4877 self = (XnoiseDur *)type->tp_alloc(type, 0);
4878
4879 self->x1 = PyFloat_FromDouble(0.5);
4880 self->x2 = PyFloat_FromDouble(0.5);
4881 self->min = PyFloat_FromDouble(0.0);
4882 self->max = PyFloat_FromDouble(1.0);
4883 self->xx1 = self->xx2 = self->walkerValue = 0.5;
4884 self->time = 1.0;
4885 self->modebuffer[0] = 0;
4886 self->modebuffer[1] = 0;
4887 self->modebuffer[2] = 0;
4888 self->modebuffer[3] = 0;
4889 self->modebuffer[4] = 0;
4890 self->modebuffer[5] = 0;
4891
4892 INIT_OBJECT_COMMON
4893
4894 Server_generateSeed((Server *)self->server, XNOISEDUR_ID);
4895
4896 self->poisson_tab = 0;
4897 self->lastPoissonX1 = -99.0;
4898
4899 for (i = 0; i < 2000; i++)
4900 {
4901 self->poisson_buffer[i] = 0.0;
4902 }
4903
4904 for (i = 0; i < 15; i++)
4905 {
4906 self->loop_buffer[i] = 0.0;
4907 }
4908
4909 self->loopChoice = self->loopCountPlay = self->loopTime = self->loopCountRec = self->loopStop = 0;
4910 self->loopLen = (pyorand() % 10) + 3;
4911
4912 Stream_setFunctionPtr(self->stream, XnoiseDur_compute_next_data_frame);
4913 self->mode_func_ptr = XnoiseDur_setProcMode;
4914
4915 static char *kwlist[] = {"type", "min", "max", "x1", "x2", "mul", "add", NULL};
4916
4917 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iOOOOOO", kwlist, &self->type, &mintmp, &maxtmp, &x1tmp, &x2tmp, &multmp, &addtmp))
4918 Py_RETURN_NONE;
4919
4920 if (x1tmp)
4921 {
4922 PyObject_CallMethod((PyObject *)self, "setX1", "O", x1tmp);
4923 }
4924
4925 if (x2tmp)
4926 {
4927 PyObject_CallMethod((PyObject *)self, "setX2", "O", x2tmp);
4928 }
4929
4930 if (mintmp)
4931 {
4932 PyObject_CallMethod((PyObject *)self, "setMin", "O", mintmp);
4933 }
4934
4935 if (maxtmp)
4936 {
4937 PyObject_CallMethod((PyObject *)self, "setMax", "O", maxtmp);
4938 }
4939
4940 if (multmp)
4941 {
4942 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4943 }
4944
4945 if (addtmp)
4946 {
4947 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4948 }
4949
4950 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4951
4952 if (self->modebuffer[2] == 0)
4953 mi = PyFloat_AS_DOUBLE(self->min);
4954 else
4955 mi = Stream_getData((Stream *)self->min_stream)[0];
4956
4957 if (self->modebuffer[3] == 0)
4958 ma = PyFloat_AS_DOUBLE(self->max);
4959 else
4960 ma = Stream_getData((Stream *)self->max_stream)[0];
4961
4962 self->value = (mi + ma) * 0.5;
4963
4964 if (self->value == 0.0)
4965 self->inc = 0.0;
4966 else
4967 self->inc = (1.0 / self->value) / self->sr;
4968
4969 XnoiseDur_setRandomType(self);
4970
4971 (*self->mode_func_ptr)(self);
4972
4973 return (PyObject *)self;
4974 }
4975
4976 static PyObject * XnoiseDur_getServer(XnoiseDur* self) { GET_SERVER };
4977 static PyObject * XnoiseDur_getStream(XnoiseDur* self) { GET_STREAM };
4978 static PyObject * XnoiseDur_setMul(XnoiseDur *self, PyObject *arg) { SET_MUL };
4979 static PyObject * XnoiseDur_setAdd(XnoiseDur *self, PyObject *arg) { SET_ADD };
4980 static PyObject * XnoiseDur_setSub(XnoiseDur *self, PyObject *arg) { SET_SUB };
4981 static PyObject * XnoiseDur_setDiv(XnoiseDur *self, PyObject *arg) { SET_DIV };
4982
4983 static PyObject * XnoiseDur_play(XnoiseDur *self, PyObject *args, PyObject *kwds) { PLAY };
4984 static PyObject * XnoiseDur_out(XnoiseDur *self, PyObject *args, PyObject *kwds) { OUT };
4985 static PyObject * XnoiseDur_stop(XnoiseDur *self, PyObject *args, PyObject *kwds) { STOP };
4986
4987 static PyObject * XnoiseDur_multiply(XnoiseDur *self, PyObject *arg) { MULTIPLY };
4988 static PyObject * XnoiseDur_inplace_multiply(XnoiseDur *self, PyObject *arg) { INPLACE_MULTIPLY };
4989 static PyObject * XnoiseDur_add(XnoiseDur *self, PyObject *arg) { ADD };
4990 static PyObject * XnoiseDur_inplace_add(XnoiseDur *self, PyObject *arg) { INPLACE_ADD };
4991 static PyObject * XnoiseDur_sub(XnoiseDur *self, PyObject *arg) { SUB };
4992 static PyObject * XnoiseDur_inplace_sub(XnoiseDur *self, PyObject *arg) { INPLACE_SUB };
4993 static PyObject * XnoiseDur_div(XnoiseDur *self, PyObject *arg) { DIV };
4994 static PyObject * XnoiseDur_inplace_div(XnoiseDur *self, PyObject *arg) { INPLACE_DIV };
4995
4996 static PyObject *
4997 XnoiseDur_setType(XnoiseDur *self, PyObject *arg)
4998 {
4999 ASSERT_ARG_NOT_NULL
5000
5001 if (PyLong_Check(arg))
5002 {
5003 self->type = PyLong_AsLong(arg);
5004 XnoiseDur_setRandomType(self);
5005 }
5006
5007 Py_RETURN_NONE;
5008 }
5009
5010 static PyObject * XnoiseDur_setX1(XnoiseDur *self, PyObject *arg) { SET_PARAM(self->x1, self->x1_stream, 2); }
5011 static PyObject * XnoiseDur_setX2(XnoiseDur *self, PyObject *arg) { SET_PARAM(self->x2, self->x2_stream, 3); }
5012 static PyObject * XnoiseDur_setMin(XnoiseDur *self, PyObject *arg) { SET_PARAM(self->min, self->min_stream, 4); }
5013 static PyObject * XnoiseDur_setMax(XnoiseDur *self, PyObject *arg) { SET_PARAM(self->max, self->max_stream, 5); }
5014
5015 static PyMemberDef XnoiseDur_members[] =
5016 {
5017 {"server", T_OBJECT_EX, offsetof(XnoiseDur, server), 0, "Pyo server."},
5018 {"stream", T_OBJECT_EX, offsetof(XnoiseDur, stream), 0, "Stream object."},
5019 {"x1", T_OBJECT_EX, offsetof(XnoiseDur, x1), 0, "first param."},
5020 {"x2", T_OBJECT_EX, offsetof(XnoiseDur, x2), 0, "second param."},
5021 {"min", T_OBJECT_EX, offsetof(XnoiseDur, min), 0, "Minimum value."},
5022 {"max", T_OBJECT_EX, offsetof(XnoiseDur, max), 0, "Maximum value."},
5023 {"mul", T_OBJECT_EX, offsetof(XnoiseDur, mul), 0, "Mul factor."},
5024 {"add", T_OBJECT_EX, offsetof(XnoiseDur, add), 0, "Add factor."},
5025 {NULL} /* Sentinel */
5026 };
5027
5028 static PyMethodDef XnoiseDur_methods[] =
5029 {
5030 {"getServer", (PyCFunction)XnoiseDur_getServer, METH_NOARGS, "Returns server object."},
5031 {"_getStream", (PyCFunction)XnoiseDur_getStream, METH_NOARGS, "Returns stream object."},
5032 {"play", (PyCFunction)XnoiseDur_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
5033 {"out", (PyCFunction)XnoiseDur_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
5034 {"stop", (PyCFunction)XnoiseDur_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
5035 {"setType", (PyCFunction)XnoiseDur_setType, METH_O, "Sets distribution type."},
5036 {"setX1", (PyCFunction)XnoiseDur_setX1, METH_O, "Sets first param."},
5037 {"setX2", (PyCFunction)XnoiseDur_setX2, METH_O, "Sets second param."},
5038 {"setMin", (PyCFunction)XnoiseDur_setMin, METH_O, "Sets minimum value."},
5039 {"setMax", (PyCFunction)XnoiseDur_setMax, METH_O, "Sets maximum value."},
5040 {"setMul", (PyCFunction)XnoiseDur_setMul, METH_O, "Sets oscillator mul factor."},
5041 {"setAdd", (PyCFunction)XnoiseDur_setAdd, METH_O, "Sets oscillator add factor."},
5042 {"setSub", (PyCFunction)XnoiseDur_setSub, METH_O, "Sets inverse add factor."},
5043 {"setDiv", (PyCFunction)XnoiseDur_setDiv, METH_O, "Sets inverse mul factor."},
5044 {NULL} /* Sentinel */
5045 };
5046
5047 static PyNumberMethods XnoiseDur_as_number =
5048 {
5049 (binaryfunc)XnoiseDur_add, /*nb_add*/
5050 (binaryfunc)XnoiseDur_sub, /*nb_subtract*/
5051 (binaryfunc)XnoiseDur_multiply, /*nb_multiply*/
5052 0, /*nb_remainder*/
5053 0, /*nb_divmod*/
5054 0, /*nb_power*/
5055 0, /*nb_neg*/
5056 0, /*nb_pos*/
5057 0, /*(unaryfunc)array_abs,*/
5058 0, /*nb_nonzero*/
5059 0, /*nb_invert*/
5060 0, /*nb_lshift*/
5061 0, /*nb_rshift*/
5062 0, /*nb_and*/
5063 0, /*nb_xor*/
5064 0, /*nb_or*/
5065 0, /*nb_int*/
5066 0, /*nb_long*/
5067 0, /*nb_float*/
5068 (binaryfunc)XnoiseDur_inplace_add, /*inplace_add*/
5069 (binaryfunc)XnoiseDur_inplace_sub, /*inplace_subtract*/
5070 (binaryfunc)XnoiseDur_inplace_multiply, /*inplace_multiply*/
5071 0, /*inplace_remainder*/
5072 0, /*inplace_power*/
5073 0, /*inplace_lshift*/
5074 0, /*inplace_rshift*/
5075 0, /*inplace_and*/
5076 0, /*inplace_xor*/
5077 0, /*inplace_or*/
5078 0, /*nb_floor_divide*/
5079 (binaryfunc)XnoiseDur_div, /*nb_true_divide*/
5080 0, /*nb_inplace_floor_divide*/
5081 (binaryfunc)XnoiseDur_inplace_div, /*nb_inplace_true_divide*/
5082 0, /* nb_index */
5083 };
5084
5085 PyTypeObject XnoiseDurType =
5086 {
5087 PyVarObject_HEAD_INIT(NULL, 0)
5088 "_pyo.XnoiseDur_base", /*tp_name*/
5089 sizeof(XnoiseDur), /*tp_basicsize*/
5090 0, /*tp_itemsize*/
5091 (destructor)XnoiseDur_dealloc, /*tp_dealloc*/
5092 0, /*tp_print*/
5093 0, /*tp_getattr*/
5094 0, /*tp_setattr*/
5095 0, /*tp_as_async (tp_compare in Python 2)*/
5096 0, /*tp_repr*/
5097 &XnoiseDur_as_number, /*tp_as_number*/
5098 0, /*tp_as_sequence*/
5099 0, /*tp_as_mapping*/
5100 0, /*tp_hash */
5101 0, /*tp_call*/
5102 0, /*tp_str*/
5103 0, /*tp_getattro*/
5104 0, /*tp_setattro*/
5105 0, /*tp_as_buffer*/
5106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5107 "XnoiseDur objects. Generates a random value and uses this value as time base for the next generation.", /* tp_doc */
5108 (traverseproc)XnoiseDur_traverse, /* tp_traverse */
5109 (inquiry)XnoiseDur_clear, /* tp_clear */
5110 0, /* tp_richcompare */
5111 0, /* tp_weaklistoffset */
5112 0, /* tp_iter */
5113 0, /* tp_iternext */
5114 XnoiseDur_methods, /* tp_methods */
5115 XnoiseDur_members, /* tp_members */
5116 0, /* tp_getset */
5117 0, /* tp_base */
5118 0, /* tp_dict */
5119 0, /* tp_descr_get */
5120 0, /* tp_descr_set */
5121 0, /* tp_dictoffset */
5122 0, /* tp_init */
5123 0, /* tp_alloc */
5124 XnoiseDur_new, /* tp_new */
5125 };
5126
5127 /****************/
5128 /****** Urn *****/
5129 /****************/
5130 typedef struct
5131 {
5132 pyo_audio_HEAD
5133 PyObject *freq;
5134 Stream *freq_stream;
5135 int *list;
5136 int max;
5137 int length;
5138 int lastvalue;
5139 MYFLT value;
5140 MYFLT time;
5141 MYFLT *trigsBuffer;
5142 TriggerStream *trig_stream;
5143 int modebuffer[3]; // need at least 2 slots for mul & add
5144 } Urn;
5145
5146 static void
5147 Urn_reset(Urn *self)
5148 {
5149 int i;
5150 self->lastvalue = (int)self->value;
5151 self->length = self->max;
5152 self->list = (int *)PyMem_RawRealloc(self->list, self->max * sizeof(int));
5153
5154 for (i = 0; i < self->max; i++)
5155 {
5156 self->list[i] = i;
5157 }
5158 }
5159
5160 static int
5161 Urn_choose(Urn *self)
5162 {
5163 int x = 0;
5164 int value = 0;
5165 int i, pick;
5166
5167 pick = pyorand() % self->length;
5168
5169 while (pick == self->lastvalue)
5170 pick = pyorand() % self->length;
5171
5172 for (i = 0; i < self->length; i++)
5173 {
5174 if (i != pick)
5175 self->list[x++] = self->list[i];
5176 else
5177 value = self->list[i];
5178 }
5179
5180 self->length = x;
5181 self->lastvalue = -1;
5182
5183 return value;
5184 }
5185
5186 static void
5187 Urn_generate_i(Urn *self)
5188 {
5189 int i;
5190 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
5191 MYFLT inc = fr / self->sr;
5192
5193 for (i = 0; i < self->bufsize; i++)
5194 {
5195 self->trigsBuffer[i] = 0.0;
5196 self->time += inc;
5197
5198 if (self->time < 0.0)
5199 self->time += 1.0;
5200 else if (self->time >= 1.0)
5201 {
5202 self->time -= 1.0;
5203 self->value = (MYFLT)Urn_choose(self);
5204
5205 if (self->length == 0)
5206 {
5207 self->trigsBuffer[i] = 1.0;
5208 Urn_reset(self);
5209 }
5210 }
5211
5212 self->data[i] = self->value;
5213 }
5214 }
5215
5216 static void
5217 Urn_generate_a(Urn *self)
5218 {
5219 int i;
5220 MYFLT inc;
5221 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
5222
5223 for (i = 0; i < self->bufsize; i++)
5224 {
5225 self->trigsBuffer[i] = 0.0;
5226 inc = fr[i] / self->sr;
5227 self->time += inc;
5228
5229 if (self->time < 0.0)
5230 self->time += 1.0;
5231 else if (self->time >= 1.0)
5232 {
5233 self->time -= 1.0;
5234 self->value = (MYFLT)Urn_choose(self);
5235
5236 if (self->length == 0)
5237 {
5238 self->trigsBuffer[i] = 1.0;
5239 Urn_reset(self);
5240 }
5241 }
5242
5243 self->data[i] = self->value;
5244 }
5245 }
5246
5247 static void Urn_postprocessing_ii(Urn *self) { POST_PROCESSING_II };
5248 static void Urn_postprocessing_ai(Urn *self) { POST_PROCESSING_AI };
5249 static void Urn_postprocessing_ia(Urn *self) { POST_PROCESSING_IA };
5250 static void Urn_postprocessing_aa(Urn *self) { POST_PROCESSING_AA };
5251 static void Urn_postprocessing_ireva(Urn *self) { POST_PROCESSING_IREVA };
5252 static void Urn_postprocessing_areva(Urn *self) { POST_PROCESSING_AREVA };
5253 static void Urn_postprocessing_revai(Urn *self) { POST_PROCESSING_REVAI };
5254 static void Urn_postprocessing_revaa(Urn *self) { POST_PROCESSING_REVAA };
5255 static void Urn_postprocessing_revareva(Urn *self) { POST_PROCESSING_REVAREVA };
5256
5257 static void
5258 Urn_setProcMode(Urn *self)
5259 {
5260 int procmode, muladdmode;
5261 procmode = self->modebuffer[2];
5262 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
5263
5264 switch (procmode)
5265 {
5266 case 0:
5267 self->proc_func_ptr = Urn_generate_i;
5268 break;
5269
5270 case 1:
5271 self->proc_func_ptr = Urn_generate_a;
5272 break;
5273 }
5274
5275 switch (muladdmode)
5276 {
5277 case 0:
5278 self->muladd_func_ptr = Urn_postprocessing_ii;
5279 break;
5280
5281 case 1:
5282 self->muladd_func_ptr = Urn_postprocessing_ai;
5283 break;
5284
5285 case 2:
5286 self->muladd_func_ptr = Urn_postprocessing_revai;
5287 break;
5288
5289 case 10:
5290 self->muladd_func_ptr = Urn_postprocessing_ia;
5291 break;
5292
5293 case 11:
5294 self->muladd_func_ptr = Urn_postprocessing_aa;
5295 break;
5296
5297 case 12:
5298 self->muladd_func_ptr = Urn_postprocessing_revaa;
5299 break;
5300
5301 case 20:
5302 self->muladd_func_ptr = Urn_postprocessing_ireva;
5303 break;
5304
5305 case 21:
5306 self->muladd_func_ptr = Urn_postprocessing_areva;
5307 break;
5308
5309 case 22:
5310 self->muladd_func_ptr = Urn_postprocessing_revareva;
5311 break;
5312 }
5313 }
5314
5315 static void
5316 Urn_compute_next_data_frame(Urn *self)
5317 {
5318 (*self->proc_func_ptr)(self);
5319 (*self->muladd_func_ptr)(self);
5320 }
5321
5322 static int
5323 Urn_traverse(Urn *self, visitproc visit, void *arg)
5324 {
5325 pyo_VISIT
5326 Py_VISIT(self->freq);
5327 return 0;
5328 }
5329
5330 static int
5331 Urn_clear(Urn *self)
5332 {
5333 pyo_CLEAR
5334 Py_CLEAR(self->freq);
5335 return 0;
5336 }
5337
5338 static void
5339 Urn_dealloc(Urn* self)
5340 {
5341 pyo_DEALLOC
5342 PyMem_RawFree(self->list);
5343 PyMem_RawFree(self->trigsBuffer);
5344 Urn_clear(self);
5345 Py_TYPE(self->trig_stream)->tp_free((PyObject*)self->trig_stream);
5346 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
5347 Py_TYPE(self)->tp_free((PyObject*)self);
5348 }
5349
5350 static PyObject *
5351 Urn_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5352 {
5353 int i;
5354 PyObject *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
5355 Urn *self;
5356 self = (Urn *)type->tp_alloc(type, 0);
5357
5358 self->freq = PyFloat_FromDouble(1.);
5359 self->max = 100;
5360 self->length = 0;
5361 self->lastvalue = -1;
5362 self->value = 0.0;
5363 self->time = 1.0;
5364 self->modebuffer[0] = 0;
5365 self->modebuffer[1] = 0;
5366 self->modebuffer[2] = 0;
5367
5368 INIT_OBJECT_COMMON
5369 Stream_setFunctionPtr(self->stream, Urn_compute_next_data_frame);
5370 self->mode_func_ptr = Urn_setProcMode;
5371
5372 static char *kwlist[] = {"max", "freq", "mul", "add", NULL};
5373
5374 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iOOO", kwlist, &self->max, &freqtmp, &multmp, &addtmp))
5375 Py_RETURN_NONE;
5376
5377 if (freqtmp)
5378 {
5379 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
5380 }
5381
5382 if (multmp)
5383 {
5384 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
5385 }
5386
5387 if (addtmp)
5388 {
5389 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
5390 }
5391
5392 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
5393
5394 self->trigsBuffer = (MYFLT *)PyMem_RawRealloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT));
5395
5396 for (i = 0; i < self->bufsize; i++)
5397 {
5398 self->trigsBuffer[i] = 0.0;
5399 }
5400
5401 MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL);
5402 TriggerStream_setData(self->trig_stream, self->trigsBuffer);
5403
5404 Urn_reset(self);
5405
5406 Server_generateSeed((Server *)self->server, URN_ID);
5407
5408 (*self->mode_func_ptr)(self);
5409
5410 return (PyObject *)self;
5411 }
5412
5413 static PyObject * Urn_getServer(Urn* self) { GET_SERVER };
5414 static PyObject * Urn_getStream(Urn* self) { GET_STREAM };
5415 static PyObject * Urn_getTriggerStream(Urn* self) { GET_TRIGGER_STREAM };
5416 static PyObject * Urn_setMul(Urn *self, PyObject *arg) { SET_MUL };
5417 static PyObject * Urn_setAdd(Urn *self, PyObject *arg) { SET_ADD };
5418 static PyObject * Urn_setSub(Urn *self, PyObject *arg) { SET_SUB };
5419 static PyObject * Urn_setDiv(Urn *self, PyObject *arg) { SET_DIV };
5420
5421 static PyObject * Urn_play(Urn *self, PyObject *args, PyObject *kwds) { PLAY };
5422 static PyObject * Urn_out(Urn *self, PyObject *args, PyObject *kwds) { OUT };
5423 static PyObject * Urn_stop(Urn *self, PyObject *args, PyObject *kwds) { STOP };
5424
5425 static PyObject * Urn_multiply(Urn *self, PyObject *arg) { MULTIPLY };
5426 static PyObject * Urn_inplace_multiply(Urn *self, PyObject *arg) { INPLACE_MULTIPLY };
5427 static PyObject * Urn_add(Urn *self, PyObject *arg) { ADD };
5428 static PyObject * Urn_inplace_add(Urn *self, PyObject *arg) { INPLACE_ADD };
5429 static PyObject * Urn_sub(Urn *self, PyObject *arg) { SUB };
5430 static PyObject * Urn_inplace_sub(Urn *self, PyObject *arg) { INPLACE_SUB };
5431 static PyObject * Urn_div(Urn *self, PyObject *arg) { DIV };
5432 static PyObject * Urn_inplace_div(Urn *self, PyObject *arg) { INPLACE_DIV };
5433
5434 static PyObject *
5435 Urn_setMax(Urn *self, PyObject *arg)
5436 {
5437 if (PyNumber_Check(arg) == 1)
5438 self->max = PyLong_AsLong(arg);
5439
5440 Urn_reset(self);
5441
5442 Py_RETURN_NONE;
5443 }
5444
5445 static PyObject * Urn_setFreq(Urn *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
5446
5447 static PyMemberDef Urn_members[] =
5448 {
5449 {"server", T_OBJECT_EX, offsetof(Urn, server), 0, "Pyo server."},
5450 {"stream", T_OBJECT_EX, offsetof(Urn, stream), 0, "Stream object."},
5451 {"trig_stream", T_OBJECT_EX, offsetof(Urn, trig_stream), 0, "Trigger Stream object."},
5452 {"freq", T_OBJECT_EX, offsetof(Urn, freq), 0, "Polling frequency."},
5453 {"mul", T_OBJECT_EX, offsetof(Urn, mul), 0, "Mul factor."},
5454 {"add", T_OBJECT_EX, offsetof(Urn, add), 0, "Add factor."},
5455 {NULL} /* Sentinel */
5456 };
5457
5458 static PyMethodDef Urn_methods[] =
5459 {
5460 {"getServer", (PyCFunction)Urn_getServer, METH_NOARGS, "Returns server object."},
5461 {"_getStream", (PyCFunction)Urn_getStream, METH_NOARGS, "Returns stream object."},
5462 {"_getTriggerStream", (PyCFunction)Urn_getTriggerStream, METH_NOARGS, "Returns trigger stream object."},
5463 {"play", (PyCFunction)Urn_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
5464 {"out", (PyCFunction)Urn_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
5465 {"stop", (PyCFunction)Urn_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
5466 {"setMax", (PyCFunction)Urn_setMax, METH_O, "Sets maximum possible value."},
5467 {"setFreq", (PyCFunction)Urn_setFreq, METH_O, "Sets polling frequency."},
5468 {"setMul", (PyCFunction)Urn_setMul, METH_O, "Sets oscillator mul factor."},
5469 {"setAdd", (PyCFunction)Urn_setAdd, METH_O, "Sets oscillator add factor."},
5470 {"setSub", (PyCFunction)Urn_setSub, METH_O, "Sets inverse add factor."},
5471 {"setDiv", (PyCFunction)Urn_setDiv, METH_O, "Sets inverse mul factor."},
5472 {NULL} /* Sentinel */
5473 };
5474
5475 static PyNumberMethods Urn_as_number =
5476 {
5477 (binaryfunc)Urn_add, /*nb_add*/
5478 (binaryfunc)Urn_sub, /*nb_subtract*/
5479 (binaryfunc)Urn_multiply, /*nb_multiply*/
5480 0, /*nb_remainder*/
5481 0, /*nb_divmod*/
5482 0, /*nb_power*/
5483 0, /*nb_neg*/
5484 0, /*nb_pos*/
5485 0, /*(unaryfunc)array_abs,*/
5486 0, /*nb_nonzero*/
5487 0, /*nb_invert*/
5488 0, /*nb_lshift*/
5489 0, /*nb_rshift*/
5490 0, /*nb_and*/
5491 0, /*nb_xor*/
5492 0, /*nb_or*/
5493 0, /*nb_int*/
5494 0, /*nb_long*/
5495 0, /*nb_float*/
5496 (binaryfunc)Urn_inplace_add, /*inplace_add*/
5497 (binaryfunc)Urn_inplace_sub, /*inplace_subtract*/
5498 (binaryfunc)Urn_inplace_multiply, /*inplace_multiply*/
5499 0, /*inplace_remainder*/
5500 0, /*inplace_power*/
5501 0, /*inplace_lshift*/
5502 0, /*inplace_rshift*/
5503 0, /*inplace_and*/
5504 0, /*inplace_xor*/
5505 0, /*inplace_or*/
5506 0, /*nb_floor_divide*/
5507 (binaryfunc)Urn_div, /*nb_true_divide*/
5508 0, /*nb_inplace_floor_divide*/
5509 (binaryfunc)Urn_inplace_div, /*nb_inplace_true_divide*/
5510 0, /* nb_index */
5511 };
5512
5513 PyTypeObject UrnType =
5514 {
5515 PyVarObject_HEAD_INIT(NULL, 0)
5516 "_pyo.Urn_base", /*tp_name*/
5517 sizeof(Urn), /*tp_basicsize*/
5518 0, /*tp_itemsize*/
5519 (destructor)Urn_dealloc, /*tp_dealloc*/
5520 0, /*tp_print*/
5521 0, /*tp_getattr*/
5522 0, /*tp_setattr*/
5523 0, /*tp_as_async (tp_compare in Python 2)*/
5524 0, /*tp_repr*/
5525 &Urn_as_number, /*tp_as_number*/
5526 0, /*tp_as_sequence*/
5527 0, /*tp_as_mapping*/
5528 0, /*tp_hash */
5529 0, /*tp_call*/
5530 0, /*tp_str*/
5531 0, /*tp_getattro*/
5532 0, /*tp_setattro*/
5533 0, /*tp_as_buffer*/
5534 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5535 "Urn objects. Periodically generates a new integer random value without repetition.", /* tp_doc */
5536 (traverseproc)Urn_traverse, /* tp_traverse */
5537 (inquiry)Urn_clear, /* tp_clear */
5538 0, /* tp_richcompare */
5539 0, /* tp_weaklistoffset */
5540 0, /* tp_iter */
5541 0, /* tp_iternext */
5542 Urn_methods, /* tp_methods */
5543 Urn_members, /* tp_members */
5544 0, /* tp_getset */
5545 0, /* tp_base */
5546 0, /* tp_dict */
5547 0, /* tp_descr_get */
5548 0, /* tp_descr_set */
5549 0, /* tp_dictoffset */
5550 0, /* tp_init */
5551 0, /* tp_alloc */
5552 Urn_new, /* tp_new */
5553 };
5554
5555 /****************/
5556 /**** LogiMap ***/
5557 /****************/
5558 typedef struct
5559 {
5560 pyo_audio_HEAD
5561 PyObject *chaos;
5562 PyObject *freq;
5563 Stream *chaos_stream;
5564 Stream *freq_stream;
5565 MYFLT init;
5566 MYFLT value;
5567 MYFLT time;
5568 int modebuffer[4]; // need at least 2 slots for mul & add
5569 } LogiMap;
5570
5571 static void
5572 LogiMap_generate_ii(LogiMap *self)
5573 {
5574 int i;
5575 MYFLT inc;
5576 MYFLT ch = PyFloat_AS_DOUBLE(self->chaos);
5577 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
5578 inc = fr / self->sr;
5579
5580 if (ch <= 0.0)
5581 ch = 0.001;
5582 else if (ch >= 1.0)
5583 ch = 0.999;
5584
5585 for (i = 0; i < self->bufsize; i++)
5586 {
5587 self->time += inc;
5588
5589 if (self->time >= 1.0)
5590 {
5591 self->time -= 1.0;
5592 self->value = (ch + 3) * self->value * (1.0 - self->value);
5593 }
5594
5595 self->data[i] = self->value;
5596 }
5597 }
5598
5599 static void
5600 LogiMap_generate_ai(LogiMap *self)
5601 {
5602 int i;
5603 MYFLT inc, ch = 0.0;
5604 MYFLT *chaos = Stream_getData((Stream *)self->chaos_stream);
5605 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
5606 inc = fr / self->sr;
5607
5608 for (i = 0; i < self->bufsize; i++)
5609 {
5610 self->time += inc;
5611
5612 if (self->time >= 1.0)
5613 {
5614 self->time -= 1.0;
5615 ch = chaos[i];
5616
5617 if (ch <= 0.0)
5618 ch = 0.001;
5619 else if (ch >= 1.0)
5620 ch = 0.999;
5621
5622 self->value = (ch + 3) * self->value * (1.0 - self->value);
5623 }
5624
5625 self->data[i] = self->value;
5626 }
5627 }
5628
5629 static void
5630 LogiMap_generate_ia(LogiMap *self)
5631 {
5632 int i;
5633 MYFLT inc;
5634 MYFLT ch = PyFloat_AS_DOUBLE(self->chaos);
5635 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
5636
5637 if (ch <= 0.0)
5638 ch = 0.001;
5639 else if (ch >= 1.0)
5640 ch = 0.999;
5641
5642 for (i = 0; i < self->bufsize; i++)
5643 {
5644 inc = fr[i] / self->sr;
5645 self->time += inc;
5646
5647 if (self->time >= 1.0)
5648 {
5649 self->time -= 1.0;
5650 self->value = (ch + 3) * self->value * (1.0 - self->value);
5651 }
5652
5653 self->data[i] = self->value;
5654 }
5655 }
5656
5657 static void
5658 LogiMap_generate_aa(LogiMap *self)
5659 {
5660 int i;
5661 MYFLT inc, ch = 0.0;
5662 MYFLT *chaos = Stream_getData((Stream *)self->chaos_stream);
5663 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
5664
5665 for (i = 0; i < self->bufsize; i++)
5666 {
5667 inc = fr[i] / self->sr;
5668 self->time += inc;
5669
5670 if (self->time >= 1.0)
5671 {
5672 self->time -= 1.0;
5673 ch = chaos[i];
5674
5675 if (ch <= 0.0)
5676 ch = 0.001;
5677 else if (ch >= 1.0)
5678 ch = 0.999;
5679
5680 self->value = (ch + 3) * self->value * (1.0 - self->value);
5681 }
5682
5683 self->data[i] = self->value;
5684 }
5685 }
5686
5687 static void LogiMap_postprocessing_ii(LogiMap *self) { POST_PROCESSING_II };
5688 static void LogiMap_postprocessing_ai(LogiMap *self) { POST_PROCESSING_AI };
5689 static void LogiMap_postprocessing_ia(LogiMap *self) { POST_PROCESSING_IA };
5690 static void LogiMap_postprocessing_aa(LogiMap *self) { POST_PROCESSING_AA };
5691 static void LogiMap_postprocessing_ireva(LogiMap *self) { POST_PROCESSING_IREVA };
5692 static void LogiMap_postprocessing_areva(LogiMap *self) { POST_PROCESSING_AREVA };
5693 static void LogiMap_postprocessing_revai(LogiMap *self) { POST_PROCESSING_REVAI };
5694 static void LogiMap_postprocessing_revaa(LogiMap *self) { POST_PROCESSING_REVAA };
5695 static void LogiMap_postprocessing_revareva(LogiMap *self) { POST_PROCESSING_REVAREVA };
5696
5697 static void
5698 LogiMap_setProcMode(LogiMap *self)
5699 {
5700 int procmode, muladdmode;
5701 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
5702 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
5703
5704 switch (procmode)
5705 {
5706 case 0:
5707 self->proc_func_ptr = LogiMap_generate_ii;
5708 break;
5709
5710 case 1:
5711 self->proc_func_ptr = LogiMap_generate_ai;
5712 break;
5713
5714 case 10:
5715 self->proc_func_ptr = LogiMap_generate_ia;
5716 break;
5717
5718 case 11:
5719 self->proc_func_ptr = LogiMap_generate_aa;
5720 break;
5721 }
5722
5723 switch (muladdmode)
5724 {
5725 case 0:
5726 self->muladd_func_ptr = LogiMap_postprocessing_ii;
5727 break;
5728
5729 case 1:
5730 self->muladd_func_ptr = LogiMap_postprocessing_ai;
5731 break;
5732
5733 case 2:
5734 self->muladd_func_ptr = LogiMap_postprocessing_revai;
5735 break;
5736
5737 case 10:
5738 self->muladd_func_ptr = LogiMap_postprocessing_ia;
5739 break;
5740
5741 case 11:
5742 self->muladd_func_ptr = LogiMap_postprocessing_aa;
5743 break;
5744
5745 case 12:
5746 self->muladd_func_ptr = LogiMap_postprocessing_revaa;
5747 break;
5748
5749 case 20:
5750 self->muladd_func_ptr = LogiMap_postprocessing_ireva;
5751 break;
5752
5753 case 21:
5754 self->muladd_func_ptr = LogiMap_postprocessing_areva;
5755 break;
5756
5757 case 22:
5758 self->muladd_func_ptr = LogiMap_postprocessing_revareva;
5759 break;
5760 }
5761 }
5762
5763 static void
5764 LogiMap_compute_next_data_frame(LogiMap *self)
5765 {
5766 (*self->proc_func_ptr)(self);
5767 (*self->muladd_func_ptr)(self);
5768 }
5769
5770 static int
5771 LogiMap_traverse(LogiMap *self, visitproc visit, void *arg)
5772 {
5773 pyo_VISIT
5774 Py_VISIT(self->freq);
5775 Py_VISIT(self->chaos);
5776 return 0;
5777 }
5778
5779 static int
5780 LogiMap_clear(LogiMap *self)
5781 {
5782 pyo_CLEAR
5783 Py_CLEAR(self->freq);
5784 Py_CLEAR(self->chaos);
5785 return 0;
5786 }
5787
5788 static void
5789 LogiMap_dealloc(LogiMap* self)
5790 {
5791 pyo_DEALLOC
5792 LogiMap_clear(self);
5793 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
5794 Py_TYPE(self)->tp_free((PyObject*)self);
5795 }
5796
5797 static PyObject *
5798 LogiMap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5799 {
5800 int i;
5801 MYFLT init;
5802 PyObject *chaostmp = NULL, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
5803 LogiMap *self;
5804 self = (LogiMap *)type->tp_alloc(type, 0);
5805
5806 self->chaos = PyFloat_FromDouble(0.6);
5807 self->freq = PyFloat_FromDouble(1.);
5808 self->time = 1.0;
5809 self->modebuffer[0] = 0;
5810 self->modebuffer[1] = 0;
5811 self->modebuffer[2] = 0;
5812 self->modebuffer[3] = 0;
5813
5814 INIT_OBJECT_COMMON
5815 Stream_setFunctionPtr(self->stream, LogiMap_compute_next_data_frame);
5816 self->mode_func_ptr = LogiMap_setProcMode;
5817
5818 static char *kwlist[] = {"chaos", "freq", "init", "mul", "add", NULL};
5819
5820 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE__OOFOO, kwlist, &chaostmp, &freqtmp, &init, &multmp, &addtmp))
5821 Py_RETURN_NONE;
5822
5823 if (chaostmp)
5824 {
5825 PyObject_CallMethod((PyObject *)self, "setChaos", "O", chaostmp);
5826 }
5827
5828 if (freqtmp)
5829 {
5830 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
5831 }
5832
5833 if (multmp)
5834 {
5835 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
5836 }
5837
5838 if (addtmp)
5839 {
5840 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
5841 }
5842
5843 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
5844
5845 if (init <= 0)
5846 init = 0.001;
5847 else if (init >= 1.0)
5848 init = 0.999;
5849
5850 self->value = self->init = init;
5851
5852 (*self->mode_func_ptr)(self);
5853
5854 return (PyObject *)self;
5855 }
5856
5857 static PyObject * LogiMap_getServer(LogiMap* self) { GET_SERVER };
5858 static PyObject * LogiMap_getStream(LogiMap* self) { GET_STREAM };
5859 static PyObject * LogiMap_setMul(LogiMap *self, PyObject *arg) { SET_MUL };
5860 static PyObject * LogiMap_setAdd(LogiMap *self, PyObject *arg) { SET_ADD };
5861 static PyObject * LogiMap_setSub(LogiMap *self, PyObject *arg) { SET_SUB };
5862 static PyObject * LogiMap_setDiv(LogiMap *self, PyObject *arg) { SET_DIV };
5863
5864 static PyObject * LogiMap_play(LogiMap *self, PyObject *args, PyObject *kwds)
5865 {
5866 self->value = self->init;
5867 PLAY
5868 };
5869 static PyObject * LogiMap_out(LogiMap *self, PyObject *args, PyObject *kwds) { OUT };
5870 static PyObject * LogiMap_stop(LogiMap *self, PyObject *args, PyObject *kwds) { STOP };
5871
5872 static PyObject * LogiMap_multiply(LogiMap *self, PyObject *arg) { MULTIPLY };
5873 static PyObject * LogiMap_inplace_multiply(LogiMap *self, PyObject *arg) { INPLACE_MULTIPLY };
5874 static PyObject * LogiMap_add(LogiMap *self, PyObject *arg) { ADD };
5875 static PyObject * LogiMap_inplace_add(LogiMap *self, PyObject *arg) { INPLACE_ADD };
5876 static PyObject * LogiMap_sub(LogiMap *self, PyObject *arg) { SUB };
5877 static PyObject * LogiMap_inplace_sub(LogiMap *self, PyObject *arg) { INPLACE_SUB };
5878 static PyObject * LogiMap_div(LogiMap *self, PyObject *arg) { DIV };
5879 static PyObject * LogiMap_inplace_div(LogiMap *self, PyObject *arg) { INPLACE_DIV };
5880
5881 static PyObject * LogiMap_setChaos(LogiMap *self, PyObject *arg) { SET_PARAM(self->chaos, self->chaos_stream, 2); }
5882 static PyObject * LogiMap_setFreq(LogiMap *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 3); }
5883
5884 static PyMemberDef LogiMap_members[] =
5885 {
5886 {"server", T_OBJECT_EX, offsetof(LogiMap, server), 0, "Pyo server."},
5887 {"stream", T_OBJECT_EX, offsetof(LogiMap, stream), 0, "Stream object."},
5888 {"chaos", T_OBJECT_EX, offsetof(LogiMap, chaos), 0, "chaosimum possible value."},
5889 {"freq", T_OBJECT_EX, offsetof(LogiMap, freq), 0, "Polling frequency."},
5890 {"mul", T_OBJECT_EX, offsetof(LogiMap, mul), 0, "Mul factor."},
5891 {"add", T_OBJECT_EX, offsetof(LogiMap, add), 0, "Add factor."},
5892 {NULL} /* Sentinel */
5893 };
5894
5895 static PyMethodDef LogiMap_methods[] =
5896 {
5897 {"getServer", (PyCFunction)LogiMap_getServer, METH_NOARGS, "Returns server object."},
5898 {"_getStream", (PyCFunction)LogiMap_getStream, METH_NOARGS, "Returns stream object."},
5899 {"play", (PyCFunction)LogiMap_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
5900 {"out", (PyCFunction)LogiMap_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
5901 {"stop", (PyCFunction)LogiMap_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
5902 {"setChaos", (PyCFunction)LogiMap_setChaos, METH_O, "Sets the randomization degree."},
5903 {"setFreq", (PyCFunction)LogiMap_setFreq, METH_O, "Sets polling frequency."},
5904 {"setMul", (PyCFunction)LogiMap_setMul, METH_O, "Sets oscillator mul factor."},
5905 {"setAdd", (PyCFunction)LogiMap_setAdd, METH_O, "Sets oscillator add factor."},
5906 {"setSub", (PyCFunction)LogiMap_setSub, METH_O, "Sets inverse add factor."},
5907 {"setDiv", (PyCFunction)LogiMap_setDiv, METH_O, "Sets inverse mul factor."},
5908 {NULL} /* Sentinel */
5909 };
5910
5911 static PyNumberMethods LogiMap_as_number =
5912 {
5913 (binaryfunc)LogiMap_add, /*nb_add*/
5914 (binaryfunc)LogiMap_sub, /*nb_subtract*/
5915 (binaryfunc)LogiMap_multiply, /*nb_multiply*/
5916 0, /*nb_remainder*/
5917 0, /*nb_divmod*/
5918 0, /*nb_power*/
5919 0, /*nb_neg*/
5920 0, /*nb_pos*/
5921 0, /*(unaryfunc)array_abs,*/
5922 0, /*nb_nonzero*/
5923 0, /*nb_invert*/
5924 0, /*nb_lshift*/
5925 0, /*nb_rshift*/
5926 0, /*nb_and*/
5927 0, /*nb_xor*/
5928 0, /*nb_or*/
5929 0, /*nb_int*/
5930 0, /*nb_long*/
5931 0, /*nb_float*/
5932 (binaryfunc)LogiMap_inplace_add, /*inplace_add*/
5933 (binaryfunc)LogiMap_inplace_sub, /*inplace_subtract*/
5934 (binaryfunc)LogiMap_inplace_multiply, /*inplace_multiply*/
5935 0, /*inplace_remainder*/
5936 0, /*inplace_power*/
5937 0, /*inplace_lshift*/
5938 0, /*inplace_rshift*/
5939 0, /*inplace_and*/
5940 0, /*inplace_xor*/
5941 0, /*inplace_or*/
5942 0, /*nb_floor_divide*/
5943 (binaryfunc)LogiMap_div, /*nb_true_divide*/
5944 0, /*nb_inplace_floor_divide*/
5945 (binaryfunc)LogiMap_inplace_div, /*nb_inplace_true_divide*/
5946 0, /* nb_index */
5947 };
5948
5949 PyTypeObject LogiMapType =
5950 {
5951 PyVarObject_HEAD_INIT(NULL, 0)
5952 "_pyo.LogiMap_base", /*tp_name*/
5953 sizeof(LogiMap), /*tp_basicsize*/
5954 0, /*tp_itemsize*/
5955 (destructor)LogiMap_dealloc, /*tp_dealloc*/
5956 0, /*tp_print*/
5957 0, /*tp_getattr*/
5958 0, /*tp_setattr*/
5959 0, /*tp_as_async (tp_compare in Python 2)*/
5960 0, /*tp_repr*/
5961 &LogiMap_as_number, /*tp_as_number*/
5962 0, /*tp_as_sequence*/
5963 0, /*tp_as_mapping*/
5964 0, /*tp_hash */
5965 0, /*tp_call*/
5966 0, /*tp_str*/
5967 0, /*tp_getattro*/
5968 0, /*tp_setattro*/
5969 0, /*tp_as_buffer*/
5970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5971 "LogiMap objects. Random generator based on the logistic equation.", /* tp_doc */
5972 (traverseproc)LogiMap_traverse, /* tp_traverse */
5973 (inquiry)LogiMap_clear, /* tp_clear */
5974 0, /* tp_richcompare */
5975 0, /* tp_weaklistoffset */
5976 0, /* tp_iter */
5977 0, /* tp_iternext */
5978 LogiMap_methods, /* tp_methods */
5979 LogiMap_members, /* tp_members */
5980 0, /* tp_getset */
5981 0, /* tp_base */
5982 0, /* tp_dict */
5983 0, /* tp_descr_get */
5984 0, /* tp_descr_set */
5985 0, /* tp_dictoffset */
5986 0, /* tp_init */
5987 0, /* tp_alloc */
5988 LogiMap_new, /* tp_new */
5989 };