Debian Codegraph @

src/objects/exprmodule.c

raw
1 /**************************************************************************
2 * Copyright 2009-2015 Olivier Belanger *
3 * *
4 * This file is part of pyo, a python module to help digital signal *
5 * processing script creation. *
6 * *
7 * pyo is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #include <Python.h>
22 #include "structmember.h"
23 #include <math.h>
24 #include "pyomodule.h"
25 #include "streammodule.h"
26 #include "servermodule.h"
27 #include "dummymodule.h"
28
29 // arithmetic operators
30 #define OP_ADD 0
31 #define OP_SUB 1
32 #define OP_MUL 2
33 #define OP_DIV 3
34 #define OP_EXP 4
35 #define OP_MOD 5
36 #define OP_NEG 6
37
38 // moving phase operators
39 #define OP_INC 7
40 #define OP_DEC 8
41 #define OP_PHS 9
42
43 // trigonometric functions
44 #define OP_SIN 10
45 #define OP_COS 11
46 #define OP_TAN 12
47 #define OP_TANH 13
48 #define OP_ATAN 14
49 #define OP_ATAN2 15
50
51 // conditional operators
52 #define OP_LT 40
53 #define OP_LE 41
54 #define OP_GT 42
55 #define OP_GE 43
56 #define OP_EQ 44
57 #define OP_NE 45
58 #define OP_IF 46
59 #define OP_AND 47
60 #define OP_OR 48
61
62 // power and logarithmic functions
63 #define OP_SQRT 50
64 #define OP_LOG 51
65 #define OP_LOG2 52
66 #define OP_LOG10 53
67 #define OP_POW 54
68 #define OP_FABS 55
69 #define OP_FLOOR 56
70 #define OP_CEIL 57
71 #define OP_EEXP 58
72 #define OP_ROUND 59
73 #define OP_MIN 60
74 #define OP_MAX 61
75 #define OP_WRAP 62
76
77 // filters
78 #define OP_RPOLE 70
79 #define OP_RZERO 71
80 #define OP_DELAY 72
81 #define OP_CPOLE 73
82 #define OP_CZERO 74
83
84 // random functions
85 #define OP_RANDF 80
86 #define OP_RANDI 81
87
88 // sample-and-hold functions
89 #define OP_SAH 90
90
91 // math constants
92 #define OP_CONST 99
93 #define OP_PI 100
94 #define OP_TWOPI 101
95 #define OP_E 102
96 #define OP_SR 103
97
98 // complex number
99 #define OP_COMPLEX 120
100 #define OP_REAL 121
101 #define OP_IMAG 122
102
103 // multi-output
104 #define OP_OUT 200
105
106 typedef struct t_expr
107 {
108 int type_op;
109 int num;
110 int *nodes;
111 int *vars;
112 int *input;
113 int *inchnls;
114 int *output;
115 int *outchnls;
116 MYFLT *values;
117 MYFLT *previous;
118 MYFLT result;
119 MYFLT result2;
120 } expr;
121
122 typedef struct
123 {
124 pyo_audio_HEAD
125 PyObject *input;
126 PyObject *variables;
127 int count;
128 int chnls;
129 MYFLT oneOverSr;
130 MYFLT *input_buffer;
131 MYFLT *output_buffer;
132 expr lexp[1024];
133 } Exprer;
134
135 void
136 clearexpr(expr ex)
137 {
138 if (ex.nodes) { PyMem_RawFree(ex.nodes); }
139
140 if (ex.vars) { PyMem_RawFree(ex.vars); }
141
142 if (ex.input) { PyMem_RawFree(ex.input); }
143
144 if (ex.inchnls) { PyMem_RawFree(ex.inchnls); }
145
146 if (ex.output) { PyMem_RawFree(ex.output); }
147
148 if (ex.outchnls) { PyMem_RawFree(ex.outchnls); }
149
150 if (ex.values) { PyMem_RawFree(ex.values); }
151
152 if (ex.previous) { PyMem_RawFree(ex.previous); }
153 }
154
155 expr
156 initexpr(const char *op, int size)
157 {
158 expr ex;
159 int i, value = -1, num = 0;
160
161 if (strcmp(op, "+") == 0) { value = OP_ADD; num = 2; }
162 else if (strcmp(op, "-") == 0) { value = OP_SUB; num = 2; }
163 else if (strcmp(op, "*") == 0) {value = OP_MUL; num = 2; }
164 else if (strcmp(op, "/") == 0) {value = OP_DIV; num = 2; }
165 else if (strcmp(op, "^") == 0) {value = OP_EXP; num = 2; }
166 else if (strcmp(op, "%") == 0) {value = OP_MOD; num = 2; }
167 else if (strcmp(op, "neg") == 0) {value = OP_NEG; num = 1; }
168 else if (strcmp(op, "++") == 0) {value = OP_INC; num = 2; }
169 else if (strcmp(op, "--") == 0) {value = OP_DEC; num = 2; }
170 else if (strcmp(op, "~") == 0) {value = OP_PHS; num = 2; }
171 else if (strcmp(op, "sin") == 0) {value = OP_SIN; num = 1; }
172 else if (strcmp(op, "cos") == 0) {value = OP_COS; num = 1; }
173 else if (strcmp(op, "tan") == 0) {value = OP_TAN; num = 1; }
174 else if (strcmp(op, "tanh") == 0) {value = OP_TANH; num = 1; }
175 else if (strcmp(op, "atan") == 0) {value = OP_ATAN; num = 1; }
176 else if (strcmp(op, "atan2") == 0) {value = OP_ATAN2; num = 2; }
177 else if (strcmp(op, "<") == 0) {value = OP_LT; num = 2; }
178 else if (strcmp(op, "<=") == 0) {value = OP_LE; num = 2; }
179 else if (strcmp(op, ">") == 0) {value = OP_GT; num = 2; }
180 else if (strcmp(op, ">=") == 0) {value = OP_GE; num = 2; }
181 else if (strcmp(op, "==") == 0) {value = OP_EQ; num = 2; }
182 else if (strcmp(op, "!=") == 0) {value = OP_NE; num = 2; }
183 else if (strcmp(op, "if") == 0) {value = OP_IF; num = 3; }
184 else if (strcmp(op, "and") == 0) {value = OP_AND; num = 2; }
185 else if (strcmp(op, "or") == 0) {value = OP_OR; num = 2; }
186 else if (strcmp(op, "sqrt") == 0) {value = OP_SQRT; num = 1; }
187 else if (strcmp(op, "log") == 0) {value = OP_LOG; num = 1; }
188 else if (strcmp(op, "log2") == 0) {value = OP_LOG2; num = 1; }
189 else if (strcmp(op, "log10") == 0) {value = OP_LOG10; num = 1; }
190 else if (strcmp(op, "pow") == 0) {value = OP_POW; num = 2; }
191 else if (strcmp(op, "abs") == 0) {value = OP_FABS; num = 1; }
192 else if (strcmp(op, "floor") == 0) {value = OP_FLOOR; num = 1; }
193 else if (strcmp(op, "ceil") == 0) {value = OP_CEIL; num = 1; }
194 else if (strcmp(op, "exp") == 0) {value = OP_EEXP; num = 1; }
195 else if (strcmp(op, "round") == 0) {value = OP_ROUND; num = 1; }
196 else if (strcmp(op, "min") == 0) {value = OP_MIN; num = 2; }
197 else if (strcmp(op, "max") == 0) {value = OP_MAX; num = 2; }
198 else if (strcmp(op, "wrap") == 0) {value = OP_WRAP; num = 1; }
199 else if (strcmp(op, "randf") == 0) {value = OP_RANDF; num = 2; }
200 else if (strcmp(op, "randi") == 0) {value = OP_RANDI; num = 2; }
201 else if (strcmp(op, "sah") == 0) {value = OP_SAH; num = 2; } // stream, trigger
202 else if (strcmp(op, "rpole") == 0) {value = OP_RPOLE; num = 2; }
203 else if (strcmp(op, "rzero") == 0) {value = OP_RZERO; num = 2; }
204 else if (strcmp(op, "delay") == 0) {value = OP_DELAY; num = 1; }
205 else if (strcmp(op, "cpole") == 0) {value = OP_CPOLE; num = 2; }
206 else if (strcmp(op, "czero") == 0) {value = OP_CZERO; num = 2; }
207
208 else if (strcmp(op, "complex") == 0) {value = OP_COMPLEX; num = 2; }
209 else if (strcmp(op, "real") == 0) {value = OP_REAL; num = 1; }
210 else if (strcmp(op, "imag") == 0) {value = OP_IMAG; num = 1; }
211
212 else if (strcmp(op, "out") == 0) {value = OP_OUT; num = 2; }
213
214 else if (strcmp(op, "const") == 0) {value = OP_CONST; num = 1; }
215 else if (strcmp(op, "pi") == 0) {value = OP_PI; num = 0; }
216 else if (strcmp(op, "twopi") == 0) {value = OP_TWOPI; num = 0; }
217 else if (strcmp(op, "e") == 0) {value = OP_E; num = 0; }
218 else if (strcmp(op, "sr") == 0) {value = OP_SR; num = 0; }
219 else if (size == 1) {value = OP_CONST; num = 1; }
220
221 ex.type_op = value;
222 ex.num = num;
223 // Allocate an additional space for constant in order to handle complex numbers.
224 if (value == OP_CONST)
225 num = 2;
226 ex.nodes = (int *)PyMem_RawMalloc(num * sizeof(int));
227 ex.vars = (int *)PyMem_RawMalloc(num * sizeof(int));
228 ex.input = (int *)PyMem_RawMalloc(num * sizeof(int));
229 ex.inchnls = (int *)PyMem_RawMalloc(num * sizeof(int));
230 ex.output = (int *)PyMem_RawMalloc(num * sizeof(int));
231 ex.outchnls = (int *)PyMem_RawMalloc(num * sizeof(int));
232 ex.values = (MYFLT *)PyMem_RawMalloc(num * sizeof(MYFLT));
233 ex.previous = (MYFLT *)PyMem_RawMalloc(num * sizeof(MYFLT));
234
235 for (i = 0; i < num; i++)
236 {
237 ex.nodes[i] = ex.vars[i] = ex.inchnls[i] = ex.outchnls[i] = -1;
238 ex.input[i] = ex.output[i] = 1;
239 ex.values[i] = ex.previous[i] = 0.0;
240 }
241
242 ex.result = 0.0;
243 ex.result2 = 0.0;
244 return ex;
245 }
246
247 void
248 print_expr(expr ex, int node)
249 {
250 int i;
251 PySys_WriteStdout("=== Node # %d ===\n", node);
252 PySys_WriteStdout("Operator: %d\nNodes: ", ex.type_op);
253
254 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.nodes[i]); }
255
256 PySys_WriteStdout("\nVars: ");
257
258 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.vars[i]); }
259
260 PySys_WriteStdout("\nInputs: ");
261
262 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.input[i]); }
263
264 PySys_WriteStdout("\nInput channels: ");
265
266 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.inchnls[i]); }
267
268 PySys_WriteStdout("\nOutputs: ");
269
270 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.output[i]); }
271
272 PySys_WriteStdout("\nOutput channels: ");
273
274 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%d, ", ex.outchnls[i]); }
275
276 PySys_WriteStdout("\nValues: ");
277
278 for (i = 0; i < ex.num; i++) { PySys_WriteStdout("%f, ", ex.values[i]); }
279
280 PySys_WriteStdout("\n\n");
281 }
282
283 static void
284 Exprer_process(Exprer *self)
285 {
286 int i, j, k, l, pos = 0, chnl = 0, outpos = 0;
287 MYFLT tmp = 0.0, result = 0.0;
288 MYFLT nextre = 0.0, lastre = 0.0, nextim = 0.0, lastim = 0.0, coefre = 0.0, coefim = 0.0;
289 PyObject *stream;
290 MYFLT *in;
291
292 int inputsize = PyList_Size(self->input);
293
294 if (self->count == 0)
295 {
296 for (i = 0; i < self->bufsize; i++)
297 {
298 self->data[i] = 0.0;
299 }
300 }
301 else
302 {
303 for (i = 0; i < self->bufsize; i++)
304 {
305 for (l = 0; l < inputsize; l++)
306 {
307 stream = PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->input, l), "_getStream", NULL);
308 in = Stream_getData((Stream *)stream);
309 self->input_buffer[l * self->bufsize + i] = in[i];
310 }
311
312 for (j = 0; j < self->count; j++)
313 {
314 for (k = 0; k < self->lexp[j].num; k++)
315 {
316 if (self->lexp[j].nodes[k] != -1)
317 {
318 self->lexp[j].values[k] = self->lexp[self->lexp[j].nodes[k]].result;
319
320 // in case let statement try to store a complex number, we save the imag part.
321 if (k == 0 &&
322 self->lexp[j].type_op == OP_CONST &&
323 (self->lexp[self->lexp[j].nodes[k]].type_op == OP_CPOLE ||
324 self->lexp[self->lexp[j].nodes[k]].type_op == OP_CZERO ||
325 self->lexp[self->lexp[j].nodes[k]].type_op == OP_COMPLEX ||
326 (
327 self->lexp[self->lexp[j].nodes[k]].type_op == OP_CONST &&
328 self->lexp[self->lexp[j].nodes[k]].num == 2
329 ))
330 )
331 {
332 self->lexp[j].values[k + 1] = self->lexp[self->lexp[j].nodes[k]].result2;
333 self->lexp[j].num = 2;
334 }
335 }
336 else if (self->lexp[j].vars[k] != -1)
337 {
338 self->lexp[j].values[k] = self->lexp[self->lexp[j].vars[k]].result;
339
340 // in case var statement try to store a complex number, we save the imag part.
341 if (k == 0 &&
342 self->lexp[j].type_op == OP_CONST &&
343 (self->lexp[self->lexp[j].vars[k]].type_op == OP_CPOLE ||
344 self->lexp[self->lexp[j].vars[k]].type_op == OP_CZERO ||
345 self->lexp[self->lexp[j].vars[k]].type_op == OP_COMPLEX))
346 {
347 self->lexp[j].values[k + 1] = self->lexp[self->lexp[j].vars[k]].result2;
348 self->lexp[j].num = 2;
349 }
350 }
351 else if (self->lexp[j].input[k] < 1)
352 {
353 chnl = self->lexp[j].inchnls[k];
354 pos = i + self->lexp[j].input[k];
355
356 if (pos < 0)
357 pos += self->bufsize;
358
359 self->lexp[j].values[k] = self->input_buffer[chnl * self->bufsize + pos];
360 }
361 else if (self->lexp[j].output[k] < 0)
362 {
363 chnl = self->lexp[j].outchnls[k];
364 pos = i + self->lexp[j].output[k];
365
366 if (pos < 0)
367 pos += self->bufsize;
368
369 self->lexp[j].values[k] = self->output_buffer[chnl * self->bufsize + pos];
370 }
371 }
372
373 switch (self->lexp[j].type_op)
374 {
375 case OP_ADD:
376 self->lexp[j].result = self->lexp[j].values[0] + self->lexp[j].values[1];
377 break;
378
379 case OP_SUB:
380 self->lexp[j].result = self->lexp[j].values[0] - self->lexp[j].values[1];
381 break;
382
383 case OP_MUL:
384 self->lexp[j].result = self->lexp[j].values[0] * self->lexp[j].values[1];
385 break;
386
387 case OP_DIV:
388 self->lexp[j].result = self->lexp[j].values[0] / self->lexp[j].values[1];
389 break;
390
391 case OP_EXP:
392 self->lexp[j].result = MYPOW(self->lexp[j].values[0], self->lexp[j].values[1]);
393 break;
394
395 case OP_MOD:
396 self->lexp[j].result = MYFMOD(self->lexp[j].values[0], self->lexp[j].values[1]);
397 break;
398
399 case OP_NEG:
400 self->lexp[j].result = -self->lexp[j].values[0];
401 break;
402
403 case OP_INC:
404 self->lexp[j].result = self->lexp[j].previous[0];
405 self->lexp[j].previous[0] = MYFMOD(self->lexp[j].previous[0] + self->lexp[j].values[0], self->lexp[j].values[1]);
406 break;
407
408 case OP_DEC:
409 self->lexp[j].result -= self->lexp[j].values[0];
410
411 if (self->lexp[j].result < 0) { self->lexp[j].result += self->lexp[j].values[1]; }
412
413 break;
414
415 case OP_PHS:
416 tmp = self->lexp[j].previous[0] + self->lexp[j].values[1];
417
418 if (tmp >= 1) { tmp -= 1.0; }
419
420 self->lexp[j].result = tmp;
421 self->lexp[j].previous[0] += (self->lexp[j].values[0] * self->oneOverSr);
422
423 if (self->lexp[j].previous[0] >= 1) { self->lexp[j].previous[0] -= 1.0; }
424
425 break;
426
427 case OP_SIN:
428 self->lexp[j].result = MYSIN(self->lexp[j].values[0]);
429 break;
430
431 case OP_COS:
432 self->lexp[j].result = MYCOS(self->lexp[j].values[0]);
433 break;
434
435 case OP_TAN:
436 self->lexp[j].result = MYTAN(self->lexp[j].values[0]);
437 break;
438
439 case OP_TANH:
440 self->lexp[j].result = MYTANH(self->lexp[j].values[0]);
441 break;
442
443 case OP_ATAN:
444 self->lexp[j].result = MYATAN(self->lexp[j].values[0]);
445 break;
446
447 case OP_ATAN2:
448 self->lexp[j].result = MYATAN2(self->lexp[j].values[0], self->lexp[j].values[1]);
449 break;
450
451 case OP_LT:
452 self->lexp[j].result = self->lexp[j].values[0] < self->lexp[j].values[1] ? 1.0 : 0.0;
453 break;
454
455 case OP_LE:
456 self->lexp[j].result = self->lexp[j].values[0] <= self->lexp[j].values[1] ? 1.0 : 0.0;
457 break;
458
459 case OP_GT:
460 self->lexp[j].result = self->lexp[j].values[0] > self->lexp[j].values[1] ? 1.0 : 0.0;
461 break;
462
463 case OP_GE:
464 self->lexp[j].result = self->lexp[j].values[0] >= self->lexp[j].values[1] ? 1.0 : 0.0;
465 break;
466
467 case OP_EQ:
468 self->lexp[j].result = self->lexp[j].values[0] == self->lexp[j].values[1] ? 1.0 : 0.0;
469 break;
470
471 case OP_NE:
472 self->lexp[j].result = self->lexp[j].values[0] != self->lexp[j].values[1] ? 1.0 : 0.0;
473 break;
474
475 case OP_IF:
476 self->lexp[j].result = self->lexp[j].values[0] != 0 ? self->lexp[j].values[1] : self->lexp[j].values[2];
477 break;
478
479 case OP_AND:
480 self->lexp[j].result = self->lexp[j].values[0] != 0 && self->lexp[j].values[1] != 0 ? 1.0 : 0.0;
481 break;
482
483 case OP_OR:
484 self->lexp[j].result = self->lexp[j].values[0] != 0 || self->lexp[j].values[1] != 0 ? 1.0 : 0.0;
485 break;
486
487 case OP_SQRT:
488 self->lexp[j].result = MYSQRT(self->lexp[j].values[0]);
489 break;
490
491 case OP_LOG:
492 self->lexp[j].result = MYLOG(self->lexp[j].values[0]);
493 break;
494
495 case OP_LOG2:
496 self->lexp[j].result = MYLOG2(self->lexp[j].values[0]);
497 break;
498
499 case OP_LOG10:
500 self->lexp[j].result = MYLOG10(self->lexp[j].values[0]);
501 break;
502
503 case OP_POW:
504 self->lexp[j].result = MYPOW(self->lexp[j].values[0], self->lexp[j].values[1]);
505 break;
506
507 case OP_FABS:
508 self->lexp[j].result = MYFABS(self->lexp[j].values[0]);
509 break;
510
511 case OP_FLOOR:
512 self->lexp[j].result = MYFLOOR(self->lexp[j].values[0]);
513 break;
514
515 case OP_CEIL:
516 self->lexp[j].result = MYCEIL(self->lexp[j].values[0]);
517 break;
518
519 case OP_EEXP:
520 self->lexp[j].result = MYEXP(self->lexp[j].values[0]);
521 break;
522
523 case OP_ROUND:
524 self->lexp[j].result = MYROUND(self->lexp[j].values[0]);
525 break;
526
527 case OP_MIN:
528 self->lexp[j].result = self->lexp[j].values[0] < self->lexp[j].values[1] ? self->lexp[j].values[0] : self->lexp[j].values[1];
529 break;
530
531 case OP_MAX:
532 self->lexp[j].result = self->lexp[j].values[0] > self->lexp[j].values[1] ? self->lexp[j].values[0] : self->lexp[j].values[1];
533 break;
534
535 case OP_WRAP:
536 tmp = self->lexp[j].values[0];
537
538 while (tmp < 0.0) {tmp += 1.0; }
539
540 while (tmp >= 1.0) {tmp -= 1.0; }
541
542 self->lexp[j].result = tmp;
543 break;
544
545 case OP_RANDF:
546 self->lexp[j].result = RANDOM_UNIFORM * (self->lexp[j].values[1] - self->lexp[j].values[0]) + self->lexp[j].values[0];
547 break;
548
549 case OP_RANDI:
550 self->lexp[j].result = MYFLOOR(RANDOM_UNIFORM * (self->lexp[j].values[1] - self->lexp[j].values[0]) + self->lexp[j].values[0]);
551 break;
552
553 case OP_SAH:
554 self->lexp[j].result = self->lexp[j].values[1] < self->lexp[j].previous[1] ? self->lexp[j].values[0] : self->lexp[j].result;
555 self->lexp[j].previous[1] = self->lexp[j].values[1];
556 break;
557
558 case OP_RPOLE:
559 self->lexp[j].result = self->lexp[j].values[0] + self->lexp[j].result * self->lexp[j].values[1];
560 break;
561
562 case OP_RZERO:
563 self->lexp[j].result = self->lexp[j].values[0] - self->lexp[j].previous[0] * self->lexp[j].values[1];
564 self->lexp[j].previous[0] = self->lexp[j].values[0];
565 break;
566
567 case OP_DELAY:
568 self->lexp[j].result = self->lexp[j].previous[0];
569 self->lexp[j].previous[0] = self->lexp[j].values[0];
570 break;
571
572 case OP_CZERO:
573 if (self->lexp[j].nodes[0] != -1)
574 {
575 nextre = self->lexp[self->lexp[j].nodes[0]].result;
576 nextim = self->lexp[self->lexp[j].nodes[0]].result2;
577 }
578 else
579 {
580 nextre = 0.0;
581 nextim = 0.0;
582 }
583
584 if (self->lexp[j].nodes[1] != -1)
585 {
586 coefre = self->lexp[self->lexp[j].nodes[1]].result;
587 coefim = self->lexp[self->lexp[j].nodes[1]].result2;
588 }
589 else
590 {
591 coefre = 0.0;
592 coefim = 0.0;
593 }
594
595 lastre = self->lexp[j].previous[0];
596 lastim = self->lexp[j].previous[1];
597 self->lexp[j].result = nextre - lastre * coefre + lastim * coefim;
598 self->lexp[j].result2 = nextim - lastre * coefim - lastim * coefre;
599 self->lexp[j].previous[0] = nextre;
600 self->lexp[j].previous[1] = nextim;
601 break;
602
603 case OP_CPOLE:
604 if (self->lexp[j].nodes[0] != -1)
605 {
606 nextre = self->lexp[self->lexp[j].nodes[0]].result;
607 nextim = self->lexp[self->lexp[j].nodes[0]].result2;
608 }
609 else
610 {
611 nextre = 0.0;
612 nextim = 0.0;
613 }
614
615 if (self->lexp[j].nodes[1] != -1)
616 {
617 coefre = self->lexp[self->lexp[j].nodes[1]].result;
618 coefim = self->lexp[self->lexp[j].nodes[1]].result2;
619 }
620 else
621 {
622 coefre = 0.0;
623 coefim = 0.0;
624 }
625
626 lastre = self->lexp[j].previous[0];
627 lastim = self->lexp[j].previous[1];
628 tmp = self->lexp[j].result = nextre + lastre * coefre - lastim * coefim;
629 self->lexp[j].previous[1] = self->lexp[j].result2 = nextim + lastre * coefim + lastim * coefre;
630 self->lexp[j].previous[0] = tmp;
631 break;
632
633 case OP_COMPLEX:
634 self->lexp[j].result = self->lexp[j].values[0];
635 self->lexp[j].result2 = self->lexp[j].values[1];
636 break;
637
638 case OP_REAL:
639 if (self->lexp[j].nodes[0] != -1)
640 {
641 self->lexp[j].result = self->lexp[self->lexp[j].nodes[0]].result;
642 }
643 else if (self->lexp[j].vars[0] != -1)
644 {
645 self->lexp[j].result = self->lexp[self->lexp[j].vars[0]].result;
646 }
647 else
648 {
649 self->lexp[j].result = self->lexp[j].result;
650 }
651
652 break;
653
654 case OP_IMAG:
655 if (self->lexp[j].nodes[0] != -1)
656 {
657 self->lexp[j].result = self->lexp[self->lexp[j].nodes[0]].result2;
658 }
659 else if (self->lexp[j].vars[0] != -1)
660 {
661 self->lexp[j].result = self->lexp[self->lexp[j].vars[0]].result2;
662 }
663 else
664 {
665 self->lexp[j].result = self->lexp[j].result2;
666 }
667
668 break;
669
670 case OP_CONST:
671 self->lexp[j].result = self->lexp[j].values[0];
672 // in case let (which becomes a const) statement try to store a complex number, we save the imag part.
673 self->lexp[j].result2 = self->lexp[j].values[1];
674 break;
675
676 case OP_PI:
677 self->lexp[j].result = PI;
678 break;
679
680 case OP_TWOPI:
681 self->lexp[j].result = TWOPI;
682 break;
683
684 case OP_E:
685 self->lexp[j].result = E;
686 break;
687
688 case OP_SR:
689 self->lexp[j].result = self->sr;
690 break;
691
692 case OP_OUT:
693 if ((int)self->lexp[j].values[0] >= self->chnls)
694 {
695 outpos = i;
696 }
697 else
698 {
699 outpos = (int)self->lexp[j].values[0] * self->bufsize + i;
700 }
701
702 self->output_buffer[outpos] = self->lexp[j].values[1];
703 break;
704 }
705
706 result = self->lexp[j].result;
707 }
708
709 if (self->chnls == 1)
710 {
711 self->output_buffer[i] = result;
712 }
713 }
714 }
715 }
716
717 MYFLT *
718 Exprer_getSamplesBuffer(Exprer *self)
719 {
720 return (MYFLT *)self->output_buffer;
721 }
722
723 static void
724 Exprer_setProcMode(Exprer *self)
725 {
726 self->proc_func_ptr = Exprer_process;
727 }
728
729 static void
730 Exprer_compute_next_data_frame(Exprer *self)
731 {
732 (*self->proc_func_ptr)(self);
733 }
734
735 static int
736 Exprer_traverse(Exprer *self, visitproc visit, void *arg)
737 {
738 pyo_VISIT
739 Py_VISIT(self->input);
740 Py_VISIT(self->variables);
741 return 0;
742 }
743
744 static int
745 Exprer_clear(Exprer *self)
746 {
747 pyo_CLEAR
748 Py_CLEAR(self->input);
749 Py_CLEAR(self->variables);
750 return 0;
751 }
752
753 static void
754 Exprer_dealloc(Exprer* self)
755 {
756 int i;
757 pyo_DEALLOC
758
759 for (i = 0; i < self->count; i++)
760 {
761 clearexpr(self->lexp[i]);
762 }
763
764 PyMem_RawFree(self->input_buffer);
765 PyMem_RawFree(self->output_buffer);
766 Exprer_clear(self);
767 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
768 Py_TYPE(self)->tp_free((PyObject*)self);
769 }
770
771 static PyObject *
772 Exprer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
773 {
774 int i;
775 MYFLT initout = 0.0;
776 PyObject *inputtmp, *exprtmp = NULL;
777 Exprer *self;
778 self = (Exprer *)type->tp_alloc(type, 0);
779
780 INIT_OBJECT_COMMON
781 Stream_setFunctionPtr(self->stream, Exprer_compute_next_data_frame);
782 self->mode_func_ptr = Exprer_setProcMode;
783
784 self->oneOverSr = 1.0 / self->sr;
785
786 self->variables = PyDict_New();
787
788 static char *kwlist[] = {"input", "expr", "outs", "initout", NULL};
789
790 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_OIF, kwlist, &inputtmp, &exprtmp, &self->chnls, &initout))
791 Py_RETURN_NONE;
792
793 self->input = inputtmp;
794 Py_INCREF(self->input);
795
796 if (exprtmp)
797 {
798 PyObject_CallMethod((PyObject *)self, "setExpr", "O", exprtmp);
799 }
800
801 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
802
803 if (self->chnls < 1)
804 self->chnls = 1;
805
806 self->input_buffer = (MYFLT *)PyMem_RawRealloc(self->input_buffer, PyList_Size(self->input) * self->bufsize * sizeof(MYFLT));
807 self->output_buffer = (MYFLT *)PyMem_RawRealloc(self->output_buffer, self->chnls * self->bufsize * sizeof(MYFLT));
808
809 for (i = 0; i < (PyList_Size(self->input) * self->bufsize); i++)
810 {
811 self->input_buffer[i] = 0.0;
812 }
813
814 for (i = 0; i < (self->chnls * self->bufsize); i++)
815 {
816 self->output_buffer[i] = initout;
817 }
818
819 (*self->mode_func_ptr)(self);
820
821 return (PyObject *)self;
822 }
823
824 static PyObject * Exprer_getServer(Exprer* self) { GET_SERVER };
825 static PyObject * Exprer_getStream(Exprer* self) { GET_STREAM };
826
827 static PyObject * Exprer_play(Exprer *self, PyObject *args, PyObject *kwds) { PLAY };
828 static PyObject * Exprer_stop(Exprer *self, PyObject *args, PyObject *kwds) { STOP };
829
830 static PyObject *
831 Exprer_setExpr(Exprer *self, PyObject *arg)
832 {
833 int i, j = 0, count = 0;
834 PyObject *sentence = NULL, *explist = NULL, *tmpchnl = NULL, *waitingList = NULL;
835 Py_ssize_t len, start, end, chpos = 0;
836
837 if (PyUnicode_Check(arg))
838 {
839 Py_INCREF(arg);
840 Py_XDECREF(sentence);
841 sentence = arg;
842 len = PyUnicode_GetLength(sentence);
843
844 if (len == 0)
845 {
846 Py_RETURN_NONE;
847 }
848
849 PyDict_Clear(self->variables);
850 PyObject *varDict = PyDict_New();
851 PyObject *waitingDict = PyDict_New();
852
853 PyObject *braceOpenToken = PyUnicode_FromString("(");
854 PyObject *braceCloseToken = PyUnicode_FromString(")");
855 PyObject *xargToken = PyUnicode_FromString("$x");
856 PyObject *yargToken = PyUnicode_FromString("$y");
857 PyObject *constToken = PyUnicode_FromString("const");
858
859 if (PyUnicode_Count(sentence, braceCloseToken, 0, len) != PyUnicode_Count(sentence, braceOpenToken, 0, len))
860 {
861 PySys_WriteStdout("Expr: mismatched brackets, expression bypassed.\n");
862 Py_RETURN_NONE;
863 }
864
865 for (i = 0; i < self->count; i++)
866 {
867 clearexpr(self->lexp[i]);
868 }
869
870 self->count = 0;
871
872 while (PyUnicode_Find(sentence, braceCloseToken, 0, len, 1) != -1)
873 {
874 end = PyUnicode_Find(sentence, braceCloseToken, 0, len, 1) + 1;
875 start = PyUnicode_Find(sentence, braceOpenToken, 0, end, -1);
876 PyObject *exp = PySequence_GetSlice(sentence, start, end);
877
878 PyObject *let_Token = PyUnicode_FromString("let ");
879 PyObject *var_Token = PyUnicode_FromString("var ");
880 if (PyUnicode_Contains(exp, let_Token) || PyUnicode_Contains(exp, var_Token))
881 {
882 PyObject *slice1 = PySequence_GetSlice(sentence, 0, start);
883 PyObject *slice2 = PySequence_GetSlice(sentence, end, len);
884 PyObject *new_sentence = PyUnicode_Concat(slice1, slice2);
885 Py_DECREF(sentence);
886 sentence = new_sentence;
887 Py_DECREF(slice1);
888 Py_DECREF(slice2);
889 }
890 else
891 {
892 PyObject *placeHolder = PyUnicode_FromString("_%d");
893 PyObject *placeHolderFormat = PyUnicode_Format(placeHolder, PyLong_FromLong(self->count));
894 PyObject* new_sentence = PyUnicode_Replace(sentence, exp, placeHolderFormat, 1);
895 Py_DECREF(sentence);
896 sentence = new_sentence;
897 Py_DECREF(placeHolder);
898 Py_DECREF(placeHolderFormat);
899 }
900 Py_DECREF(let_Token);
901 Py_DECREF(var_Token);
902
903 PyObject *new_exp = PyUnicode_Replace(exp, braceOpenToken, PyUnicode_FromString(""), -1);
904 PyObject *new_exp2 = PyUnicode_Replace(new_exp, braceCloseToken, PyUnicode_FromString(""), -1);
905 explist = PyUnicode_Split(new_exp2, NULL, -1);
906 Py_DECREF(new_exp);
907 Py_DECREF(new_exp2);
908
909 // Prepare variable from "var" function
910 PyObject *varToken = PyUnicode_FromString("var");
911 if (PyUnicode_Compare(PyList_GetItem(explist, 0), varToken) == 0)
912 {
913 Py_INCREF(constToken);
914 PyList_SetItem(explist, 0, constToken);
915 PyDict_SetItem(self->variables, PyList_GetItem(explist, 1), PyLong_FromLong(self->count));
916 PySequence_DelItem(explist, 1);
917 }
918 Py_DECREF(varToken);
919
920 double initval = 0.0;
921
922 // Prepare variable from "let" function
923 PyObject *letToken = PyUnicode_FromString("let");
924 if (PyUnicode_Compare(PyList_GetItem(explist, 0), letToken) == 0)
925 {
926 Py_INCREF(constToken);
927 PyList_SetItem(explist, 0, constToken);
928
929 if (PyList_Size(explist) == 4)
930 {
931 PyObject *valueAsFloat = PyFloat_FromString(PyList_GetItem(explist, 3));
932 initval = PyFloat_AsDouble(valueAsFloat);
933 PySequence_DelItem(explist, 3);
934 Py_DECREF(valueAsFloat);
935 }
936
937 if (PyDict_GetItem(waitingDict, PyList_GetItem(explist, 1)) != NULL)
938 {
939 waitingList = PyDict_GetItem(waitingDict, PyList_GetItem(explist, 1));
940
941 for (j = 0; j < PyList_Size(waitingList); j++)
942 {
943 count = PyLong_AsLong(PyTuple_GetItem(PyList_GetItem(waitingList, j), 0));
944 i = PyLong_AsLong(PyTuple_GetItem(PyList_GetItem(waitingList, j), 1));
945 self->lexp[count].vars[i] = self->count;
946 }
947
948 PyDict_DelItem(waitingDict, PyList_GetItem(explist, 1));
949 }
950
951 PyDict_SetItem(varDict, PyList_GetItem(explist, 1), PyLong_FromLong(self->count));
952 PySequence_DelItem(explist, 1);
953 }
954 Py_DECREF(letToken);
955
956 // Initialize expression node
957 self->lexp[self->count] = initexpr(PyUnicode_AsUTF8(PyList_GetItem(explist, 0)), PyList_Size(explist));
958
959 if (initval != 0.0)
960 self->lexp[self->count].result = initval;
961
962 if (PyList_Size(explist) == 1 && self->lexp[self->count].type_op == OP_CONST)
963 {
964 PyList_Insert(explist, 0, constToken);
965 }
966
967 while (PyList_Size(explist) < (self->lexp[self->count].num + 1))
968 {
969 PyList_Append(explist, PyUnicode_FromString("0.0"));
970 }
971
972 for (i = 0; i < self->lexp[self->count].num; i++)
973 {
974 if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), PyUnicode_FromString("_")))
975 {
976 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), PyUnicode_FromString("_"), PyUnicode_FromString(""), -1);
977 self->lexp[self->count].nodes[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(tmpstr), NULL, 0));
978 Py_DECREF(tmpstr);
979 }
980 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), PyUnicode_FromString("#")))
981 {
982 if (PyDict_GetItem(self->variables, PyList_GetItem(explist, i + 1)) != NULL)
983 {
984 self->lexp[self->count].vars[i] = PyLong_AsLong(PyDict_GetItem(self->variables, PyList_GetItem(explist, i + 1)));
985 }
986 else if (PyDict_GetItem(varDict, PyList_GetItem(explist, i + 1)) != NULL)
987 {
988 self->lexp[self->count].vars[i] = PyLong_AsLong(PyDict_GetItem(varDict, PyList_GetItem(explist, i + 1)));
989 }
990 else
991 {
992 if (PyDict_GetItem(waitingDict, PyList_GetItem(explist, i + 1)) == NULL)
993 waitingList = PyList_New(0);
994 else
995 waitingList = PyDict_GetItem(waitingDict, PyList_GetItem(explist, i + 1));
996
997 PyObject *tmptup = PyTuple_Pack(2, PyLong_FromLong(self->count), PyLong_FromLong(i));
998 PyList_Append(waitingList, tmptup);
999 PyDict_SetItem(waitingDict, PyList_GetItem(explist, i + 1), waitingList);
1000 Py_DECREF(tmptup);
1001 }
1002 }
1003 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), xargToken))
1004 {
1005 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), xargToken, PyUnicode_FromString(""), -1);
1006 chpos = PyUnicode_Find(tmpstr, PyUnicode_FromString("["), 0, PyUnicode_GetLength(tmpstr), 1);
1007
1008 if (chpos == 0)
1009 {
1010 self->lexp[self->count].inchnls[i] = 0;
1011 }
1012 else
1013 {
1014 self->lexp[self->count].inchnls[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(PyUnicode_Substring(tmpstr, 0, chpos)), NULL, 0));
1015 PyObject *substr = PyUnicode_Substring(tmpstr, chpos, PyUnicode_GetLength(tmpstr));
1016 Py_DECREF(tmpstr);
1017 tmpstr = substr;
1018 }
1019
1020 PyObject *new_tmpstr = PyUnicode_Replace(tmpstr, PyUnicode_FromString("["), PyUnicode_FromString(""), -1);
1021 PyObject *new_tmpstr2 = PyUnicode_Replace(new_tmpstr, PyUnicode_FromString("]"), PyUnicode_FromString(""), -1);
1022 self->lexp[self->count].input[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(new_tmpstr2), NULL, 0));
1023 Py_DECREF(tmpstr);
1024 Py_DECREF(new_tmpstr);
1025 Py_DECREF(new_tmpstr2);
1026 }
1027 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), yargToken))
1028 {
1029 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), yargToken, PyUnicode_FromString(""), -1);
1030 chpos = PyUnicode_Find(tmpstr, PyUnicode_FromString("["), 0, PyUnicode_GetLength(tmpstr), 1);
1031
1032 if (chpos == 0)
1033 {
1034 self->lexp[self->count].outchnls[i] = 0;
1035 }
1036 else
1037 {
1038 self->lexp[self->count].outchnls[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(PyUnicode_Substring(tmpstr, 0, chpos)), NULL, 0));
1039 PyObject *substr = PyUnicode_Substring(tmpstr, chpos, PyUnicode_GetLength(tmpstr));
1040 Py_DECREF(tmpstr);
1041 tmpstr = substr;
1042 }
1043
1044 PyObject *new_tmpstr = PyUnicode_Replace(tmpstr, PyUnicode_FromString("["), PyUnicode_FromString(""), -1);
1045 PyObject *new_tmpstr2 = PyUnicode_Replace(new_tmpstr, PyUnicode_FromString("]"), PyUnicode_FromString(""), -1);
1046 self->lexp[self->count].output[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(new_tmpstr2), NULL, 0));
1047 Py_DECREF(tmpstr);
1048 Py_DECREF(new_tmpstr);
1049 Py_DECREF(new_tmpstr2);
1050 }
1051 else
1052 {
1053 PyObject *valueAsFloat = PyFloat_FromString(PyList_GetItem(explist, i + 1));
1054 self->lexp[self->count].values[i] = PyFloat_AsDouble(valueAsFloat);
1055 Py_DECREF(valueAsFloat);
1056 }
1057 }
1058
1059 Py_DECREF(exp);
1060
1061 len = PyUnicode_GetLength(sentence);
1062 self->count++;
1063 }
1064
1065 explist = PyUnicode_Split(sentence, NULL, -1);
1066
1067 if (PyList_Size(explist) == 1)
1068 PyList_Insert(explist, 0, constToken);
1069
1070 // Initialize last expression node
1071 self->lexp[self->count] = initexpr(PyUnicode_AsUTF8(PyList_GetItem(explist, 0)), PyList_Size(explist));
1072
1073 for (i = 0; i < self->lexp[self->count].num; i++)
1074 {
1075 if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), PyUnicode_FromString("_")))
1076 {
1077 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), PyUnicode_FromString("_"), PyUnicode_FromString(""), -1);
1078 self->lexp[self->count].nodes[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(tmpstr), NULL, 0));
1079 Py_DECREF(tmpstr);
1080 }
1081 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), PyUnicode_FromString("#")))
1082 {
1083 self->lexp[self->count].vars[i] = PyLong_AsLong(PyDict_GetItem(varDict, PyList_GetItem(explist, i + 1)));
1084 }
1085 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), xargToken))
1086 {
1087 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), xargToken, PyUnicode_FromString(""), -1);
1088 chpos = PyUnicode_Find(tmpstr, PyUnicode_FromString("["), 0, PyUnicode_GetLength(tmpstr), 1);
1089
1090 if (chpos == 0)
1091 {
1092 self->lexp[self->count].inchnls[i] = 0;
1093 }
1094 else
1095 {
1096 self->lexp[self->count].inchnls[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(PyUnicode_Substring(tmpstr, 0, chpos)), NULL, 0));
1097 PyObject *substr = PyUnicode_Substring(tmpstr, chpos, PyUnicode_GetLength(tmpstr));
1098 Py_DECREF(tmpstr);
1099 tmpstr = substr;
1100 }
1101
1102 PyObject *new_tmpstr = PyUnicode_Replace(tmpstr, PyUnicode_FromString("["), PyUnicode_FromString(""), -1);
1103 PyObject *new_tmpstr2 = PyUnicode_Replace(new_tmpstr, PyUnicode_FromString("]"), PyUnicode_FromString(""), -1);
1104 self->lexp[self->count].input[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(new_tmpstr2), NULL, 0));
1105 Py_DECREF(tmpstr);
1106 Py_DECREF(new_tmpstr);
1107 Py_DECREF(new_tmpstr2);
1108 }
1109 else if (PyUnicode_Contains(PyList_GetItem(explist, i + 1), yargToken))
1110 {
1111 PyObject *tmpstr = PyUnicode_Replace(PyList_GetItem(explist, i + 1), yargToken, PyUnicode_FromString(""), -1);
1112 chpos = PyUnicode_Find(tmpstr, PyUnicode_FromString("["), 0, PyUnicode_GetLength(tmpstr), 1);
1113
1114 if (chpos == 0)
1115 {
1116 self->lexp[self->count].outchnls[i] = 0;
1117 }
1118 else
1119 {
1120 self->lexp[self->count].outchnls[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(PyUnicode_Substring(tmpstr, 0, chpos)), NULL, 0));
1121 PyObject *substr = PyUnicode_Substring(tmpstr, chpos, PyUnicode_GetLength(tmpstr));
1122 Py_DECREF(tmpstr);
1123 tmpstr = substr;
1124 }
1125
1126 PyObject *new_tmpstr = PyUnicode_Replace(tmpstr, PyUnicode_FromString("["), PyUnicode_FromString(""), -1);
1127 PyObject *new_tmpstr2 = PyUnicode_Replace(new_tmpstr, PyUnicode_FromString("]"), PyUnicode_FromString(""), -1);
1128 self->lexp[self->count].output[i] = PyLong_AsLong(PyLong_FromString(PyUnicode_AsUTF8(new_tmpstr2), NULL, 0));
1129 Py_DECREF(tmpstr);
1130 Py_DECREF(new_tmpstr);
1131 Py_DECREF(new_tmpstr2);
1132 }
1133 else
1134 {
1135 PyObject *valueAsFloat = PyFloat_FromString(PyList_GetItem(explist, i + 1));
1136 self->lexp[self->count].values[i] = PyFloat_AsDouble(valueAsFloat);
1137 Py_DECREF(valueAsFloat);
1138 }
1139 }
1140
1141 Py_DECREF(braceOpenToken);
1142 Py_DECREF(braceCloseToken);
1143 Py_DECREF(xargToken);
1144 Py_DECREF(yargToken);
1145 Py_DECREF(constToken);
1146 Py_DECREF(varDict);
1147 Py_DECREF(waitingDict);
1148
1149 self->count++;
1150 }
1151
1152 Py_XDECREF(sentence);
1153 Py_XDECREF(explist);
1154 Py_XDECREF(waitingList);
1155 Py_XDECREF(tmpchnl);
1156
1157 Py_RETURN_NONE;
1158 }
1159
1160 static PyObject *
1161 Exprer_setVar(Exprer *self, PyObject *args, PyObject *kwds)
1162 {
1163 int index;
1164 PyObject *varname = NULL, *value = NULL;
1165 static char *kwlist[] = {"varname", "value", NULL};
1166
1167 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &varname, &value))
1168 Py_RETURN_NONE;
1169
1170 if (PyDict_GetItem(self->variables, varname))
1171 {
1172 index = PyLong_AsLong(PyDict_GetItem(self->variables, varname));
1173 self->lexp[index].values[0] = PyFloat_AsDouble(value);
1174 }
1175
1176 Py_RETURN_NONE;
1177 }
1178
1179 static PyObject *
1180 Exprer_printNodes(Exprer *self)
1181 {
1182 int i;
1183
1184 for (i = 0; i < self->count; i++)
1185 {
1186 print_expr(self->lexp[i], i);
1187 }
1188
1189 Py_RETURN_NONE;
1190 };
1191
1192 static PyMemberDef Exprer_members[] =
1193 {
1194 {"server", T_OBJECT_EX, offsetof(Exprer, server), 0, "Pyo server."},
1195 {"stream", T_OBJECT_EX, offsetof(Exprer, stream), 0, "Stream object."},
1196 {"input", T_OBJECT_EX, offsetof(Exprer, input), 0, "Input sound object."},
1197 {NULL} /* Sentinel */
1198 };
1199
1200 static PyMethodDef Exprer_methods[] =
1201 {
1202 {"getServer", (PyCFunction)Exprer_getServer, METH_NOARGS, "Returns server object."},
1203 {"_getStream", (PyCFunction)Exprer_getStream, METH_NOARGS, "Returns stream object."},
1204 {"play", (PyCFunction)Exprer_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1205 {"stop", (PyCFunction)Exprer_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1206 {"printNodes", (PyCFunction)Exprer_printNodes, METH_NOARGS, "Print the list of nodes."},
1207 {"setVar", (PyCFunction)Exprer_setVar, METH_VARARGS | METH_KEYWORDS, "Sets a variable value."},
1208 {"setExpr", (PyCFunction)Exprer_setExpr, METH_O, "Sets a new expression."},
1209 {NULL} /* Sentinel */
1210 };
1211
1212 PyTypeObject ExprerType =
1213 {
1214 PyVarObject_HEAD_INIT(NULL, 0)
1215 "_pyo.Exprer_base", /*tp_name*/
1216 sizeof(Exprer), /*tp_basicsize*/
1217 0, /*tp_itemsize*/
1218 (destructor)Exprer_dealloc, /*tp_dealloc*/
1219 0, /*tp_print*/
1220 0, /*tp_getattr*/
1221 0, /*tp_setattr*/
1222 0, /*tp_as_async (tp_compare in Python 2)*/
1223 0, /*tp_repr*/
1224 0, /*tp_as_number*/
1225 0, /*tp_as_sequence*/
1226 0, /*tp_as_mapping*/
1227 0, /*tp_hash */
1228 0, /*tp_call*/
1229 0, /*tp_str*/
1230 0, /*tp_getattro*/
1231 0, /*tp_setattro*/
1232 0, /*tp_as_buffer*/
1233 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1234 "Exprer objects. Resolve a prefix notation sentence at audio rate.", /* tp_doc */
1235 (traverseproc)Exprer_traverse, /* tp_traverse */
1236 (inquiry)Exprer_clear, /* tp_clear */
1237 0, /* tp_richcompare */
1238 0, /* tp_weaklistoffset */
1239 0, /* tp_iter */
1240 0, /* tp_iternext */
1241 Exprer_methods, /* tp_methods */
1242 Exprer_members, /* tp_members */
1243 0, /* tp_getset */
1244 0, /* tp_base */
1245 0, /* tp_dict */
1246 0, /* tp_descr_get */
1247 0, /* tp_descr_set */
1248 0, /* tp_dictoffset */
1249 0, /* tp_init */
1250 0, /* tp_alloc */
1251 Exprer_new, /* tp_new */
1252 };
1253
1254 /************************************************************************************************/
1255 /* Expr streamer object */
1256 /************************************************************************************************/
1257 typedef struct
1258 {
1259 pyo_audio_HEAD
1260 Exprer *mainSplitter;
1261 int modebuffer[2];
1262 int chnl; // Exprning order
1263 } Expr;
1264
1265 static void Expr_postprocessing_ii(Expr *self) { POST_PROCESSING_II };
1266 static void Expr_postprocessing_ai(Expr *self) { POST_PROCESSING_AI };
1267 static void Expr_postprocessing_ia(Expr *self) { POST_PROCESSING_IA };
1268 static void Expr_postprocessing_aa(Expr *self) { POST_PROCESSING_AA };
1269 static void Expr_postprocessing_ireva(Expr *self) { POST_PROCESSING_IREVA };
1270 static void Expr_postprocessing_areva(Expr *self) { POST_PROCESSING_AREVA };
1271 static void Expr_postprocessing_revai(Expr *self) { POST_PROCESSING_REVAI };
1272 static void Expr_postprocessing_revaa(Expr *self) { POST_PROCESSING_REVAA };
1273 static void Expr_postprocessing_revareva(Expr *self) { POST_PROCESSING_REVAREVA };
1274
1275 static void
1276 Expr_setProcMode(Expr *self)
1277 {
1278 int muladdmode;
1279 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1280
1281 switch (muladdmode)
1282 {
1283 case 0:
1284 self->muladd_func_ptr = Expr_postprocessing_ii;
1285 break;
1286
1287 case 1:
1288 self->muladd_func_ptr = Expr_postprocessing_ai;
1289 break;
1290
1291 case 2:
1292 self->muladd_func_ptr = Expr_postprocessing_revai;
1293 break;
1294
1295 case 10:
1296 self->muladd_func_ptr = Expr_postprocessing_ia;
1297 break;
1298
1299 case 11:
1300 self->muladd_func_ptr = Expr_postprocessing_aa;
1301 break;
1302
1303 case 12:
1304 self->muladd_func_ptr = Expr_postprocessing_revaa;
1305 break;
1306
1307 case 20:
1308 self->muladd_func_ptr = Expr_postprocessing_ireva;
1309 break;
1310
1311 case 21:
1312 self->muladd_func_ptr = Expr_postprocessing_areva;
1313 break;
1314
1315 case 22:
1316 self->muladd_func_ptr = Expr_postprocessing_revareva;
1317 break;
1318 }
1319 }
1320
1321 static void
1322 Expr_compute_next_data_frame(Expr *self)
1323 {
1324 int i;
1325 MYFLT *tmp;
1326 int offset = self->chnl * self->bufsize;
1327 tmp = Exprer_getSamplesBuffer((Exprer *)self->mainSplitter);
1328
1329 for (i = 0; i < self->bufsize; i++)
1330 {
1331 self->data[i] = tmp[i + offset];
1332 }
1333
1334 (*self->muladd_func_ptr)(self);
1335 }
1336
1337 static int
1338 Expr_traverse(Expr *self, visitproc visit, void *arg)
1339 {
1340 pyo_VISIT
1341 Py_VISIT(self->mainSplitter);
1342 return 0;
1343 }
1344
1345 static int
1346 Expr_clear(Expr *self)
1347 {
1348 pyo_CLEAR
1349 Py_CLEAR(self->mainSplitter);
1350 return 0;
1351 }
1352
1353 static void
1354 Expr_dealloc(Expr* self)
1355 {
1356 pyo_DEALLOC
1357 Expr_clear(self);
1358 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1359 Py_TYPE(self)->tp_free((PyObject*)self);
1360 }
1361
1362 static PyObject *
1363 Expr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1364 {
1365 int i;
1366 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1367 Expr *self;
1368 self = (Expr *)type->tp_alloc(type, 0);
1369
1370 self->modebuffer[0] = 0;
1371 self->modebuffer[1] = 0;
1372
1373 INIT_OBJECT_COMMON
1374 Stream_setFunctionPtr(self->stream, Expr_compute_next_data_frame);
1375 self->mode_func_ptr = Expr_setProcMode;
1376
1377 static char *kwlist[] = {"mainSplitter", "chnl", "mul", "add", NULL};
1378
1379 if (! PyArg_ParseTupleAndKeywords(args, kwds, "Oi|OO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1380 Py_RETURN_NONE;
1381
1382 self->mainSplitter = (Exprer *)maintmp;
1383 Py_INCREF(self->mainSplitter);
1384
1385 if (multmp)
1386 {
1387 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1388 }
1389
1390 if (addtmp)
1391 {
1392 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1393 }
1394
1395 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1396
1397 (*self->mode_func_ptr)(self);
1398
1399 return (PyObject *)self;
1400 }
1401
1402 static PyObject * Expr_getServer(Expr* self) { GET_SERVER };
1403 static PyObject * Expr_getStream(Expr* self) { GET_STREAM };
1404 static PyObject * Expr_setMul(Expr *self, PyObject *arg) { SET_MUL };
1405 static PyObject * Expr_setAdd(Expr *self, PyObject *arg) { SET_ADD };
1406 static PyObject * Expr_setSub(Expr *self, PyObject *arg) { SET_SUB };
1407 static PyObject * Expr_setDiv(Expr *self, PyObject *arg) { SET_DIV };
1408
1409 static PyObject * Expr_play(Expr *self, PyObject *args, PyObject *kwds) { PLAY };
1410 static PyObject * Expr_out(Expr *self, PyObject *args, PyObject *kwds) { OUT };
1411 static PyObject * Expr_stop(Expr *self, PyObject *args, PyObject *kwds) { STOP };
1412
1413 static PyObject * Expr_multiply(Expr *self, PyObject *arg) { MULTIPLY };
1414 static PyObject * Expr_inplace_multiply(Expr *self, PyObject *arg) { INPLACE_MULTIPLY };
1415 static PyObject * Expr_add(Expr *self, PyObject *arg) { ADD };
1416 static PyObject * Expr_inplace_add(Expr *self, PyObject *arg) { INPLACE_ADD };
1417 static PyObject * Expr_sub(Expr *self, PyObject *arg) { SUB };
1418 static PyObject * Expr_inplace_sub(Expr *self, PyObject *arg) { INPLACE_SUB };
1419 static PyObject * Expr_div(Expr *self, PyObject *arg) { DIV };
1420 static PyObject * Expr_inplace_div(Expr *self, PyObject *arg) { INPLACE_DIV };
1421
1422 static PyMemberDef Expr_members[] =
1423 {
1424 {"server", T_OBJECT_EX, offsetof(Expr, server), 0, "Pyo server."},
1425 {"stream", T_OBJECT_EX, offsetof(Expr, stream), 0, "Stream object."},
1426 {"mul", T_OBJECT_EX, offsetof(Expr, mul), 0, "Mul factor."},
1427 {"add", T_OBJECT_EX, offsetof(Expr, add), 0, "Add factor."},
1428 {NULL} /* Sentinel */
1429 };
1430
1431 static PyMethodDef Expr_methods[] =
1432 {
1433 {"getServer", (PyCFunction)Expr_getServer, METH_NOARGS, "Returns server object."},
1434 {"_getStream", (PyCFunction)Expr_getStream, METH_NOARGS, "Returns stream object."},
1435 {"play", (PyCFunction)Expr_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1436 {"out", (PyCFunction)Expr_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1437 {"stop", (PyCFunction)Expr_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1438 {"setMul", (PyCFunction)Expr_setMul, METH_O, "Sets Expr mul factor."},
1439 {"setAdd", (PyCFunction)Expr_setAdd, METH_O, "Sets Expr add factor."},
1440 {"setSub", (PyCFunction)Expr_setSub, METH_O, "Sets inverse add factor."},
1441 {"setDiv", (PyCFunction)Expr_setDiv, METH_O, "Sets inverse mul factor."},
1442 {NULL} /* Sentinel */
1443 };
1444
1445 static PyNumberMethods Expr_as_number =
1446 {
1447 (binaryfunc)Expr_add, /*nb_add*/
1448 (binaryfunc)Expr_sub, /*nb_subtract*/
1449 (binaryfunc)Expr_multiply, /*nb_multiply*/
1450 0, /*nb_remainder*/
1451 0, /*nb_divmod*/
1452 0, /*nb_power*/
1453 0, /*nb_neg*/
1454 0, /*nb_pos*/
1455 0, /*(unaryfunc)array_abs,*/
1456 0, /*nb_nonzero*/
1457 0, /*nb_invert*/
1458 0, /*nb_lshift*/
1459 0, /*nb_rshift*/
1460 0, /*nb_and*/
1461 0, /*nb_xor*/
1462 0, /*nb_or*/
1463 0, /*nb_int*/
1464 0, /*nb_long*/
1465 0, /*nb_float*/
1466 (binaryfunc)Expr_inplace_add, /*inplace_add*/
1467 (binaryfunc)Expr_inplace_sub, /*inplace_subtract*/
1468 (binaryfunc)Expr_inplace_multiply, /*inplace_multiply*/
1469 0, /*inplace_remainder*/
1470 0, /*inplace_power*/
1471 0, /*inplace_lshift*/
1472 0, /*inplace_rshift*/
1473 0, /*inplace_and*/
1474 0, /*inplace_xor*/
1475 0, /*inplace_or*/
1476 0, /*nb_floor_divide*/
1477 (binaryfunc)Expr_div, /*nb_true_divide*/
1478 0, /*nb_inplace_floor_divide*/
1479 (binaryfunc)Expr_inplace_div, /*nb_inplace_true_divide*/
1480 0, /* nb_index */
1481 };
1482
1483 PyTypeObject ExprType =
1484 {
1485 PyVarObject_HEAD_INIT(NULL, 0)
1486 "_pyo.Expr_base", /*tp_name*/
1487 sizeof(Expr), /*tp_basicsize*/
1488 0, /*tp_itemsize*/
1489 (destructor)Expr_dealloc, /*tp_dealloc*/
1490 0, /*tp_print*/
1491 0, /*tp_getattr*/
1492 0, /*tp_setattr*/
1493 0, /*tp_as_async (tp_compare in Python 2)*/
1494 0, /*tp_repr*/
1495 &Expr_as_number, /*tp_as_number*/
1496 0, /*tp_as_sequence*/
1497 0, /*tp_as_mapping*/
1498 0, /*tp_hash */
1499 0, /*tp_call*/
1500 0, /*tp_str*/
1501 0, /*tp_getattro*/
1502 0, /*tp_setattro*/
1503 0, /*tp_as_buffer*/
1504 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1505 "Expr objects. Reads one channel from a Exprer.", /* tp_doc */
1506 (traverseproc)Expr_traverse, /* tp_traverse */
1507 (inquiry)Expr_clear, /* tp_clear */
1508 0, /* tp_richcompare */
1509 0, /* tp_weaklistoffset */
1510 0, /* tp_iter */
1511 0, /* tp_iternext */
1512 Expr_methods, /* tp_methods */
1513 Expr_members, /* tp_members */
1514 0, /* tp_getset */
1515 0, /* tp_base */
1516 0, /* tp_dict */
1517 0, /* tp_descr_get */
1518 0, /* tp_descr_set */
1519 0, /* tp_dictoffset */
1520 0, /* tp_init */
1521 0, /* tp_alloc */
1522 Expr_new, /* tp_new */
1523 };