Debian Codegraph @

src/objects/filtremodule.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 "structmember.h"
23 #include <math.h>
24 #include "pyomodule.h"
25 #include "streammodule.h"
26 #include "servermodule.h"
27 #include "dummymodule.h"
28
29 static MYFLT HALF_COS_ARRAY[513] = {1.0, 0.99998110153278696, 0.99992440684545181, 0.99982991808087995, 0.99969763881045715, 0.99952757403393411, 0.99931973017923825, 0.99907411510222999, 0.99879073808640628, 0.99846960984254973, 0.99811074250832332, 0.99771414964781235, 0.99727984625101107, 0.99680784873325645, 0.99629817493460782, 0.99575084411917214, 0.99516587697437664, 0.99454329561018584, 0.99388312355826691, 0.9931853857710996, 0.99245010862103322, 0.99167731989928998, 0.99086704881491472, 0.99001932599367026, 0.98913418347688054, 0.98821165472021921, 0.9872517745924454, 0.98625457937408512, 0.98522010675606064, 0.98414839583826585, 0.98303948712808786, 0.98189342253887657, 0.98071024538836005, 0.97949000039700762, 0.97823273368633901, 0.9769384927771817, 0.97560732658787452, 0.97423928543241856, 0.97283442101857576, 0.97139278644591409, 0.96991443620380113, 0.96839942616934394, 0.96684781360527761, 0.96525965715780015, 0.96363501685435693, 0.96197395410137099, 0.96027653168192206, 0.95854281375337425, 0.95677286584495025, 0.95496675485525528, 0.95312454904974775, 0.95124631805815985, 0.94933213287186513, 0.94738206584119555, 0.94539619067270686, 0.9433745824263926, 0.94131731751284708, 0.9392244736903772, 0.93709613006206383, 0.9349323670727715, 0.93273326650610799, 0.93049891148133324, 0.92822938645021758, 0.92592477719384991, 0.92358517081939495, 0.92121065575680161, 0.91880132175545981, 0.91635725988080907, 0.91387856251089561, 0.91136532333288145, 0.90881763733950294, 0.9062356008254806, 0.90361931138387919, 0.90096886790241915, 0.89828437055973898, 0.89556592082160869, 0.89281362143709486, 0.89002757643467667, 0.88720789111831455, 0.8843546720634694, 0.88146802711307481, 0.87854806537346075, 0.87559489721022943, 0.8726086342440843, 0.86958938934661101, 0.86653727663601088, 0.86345241147278784, 0.86033491045538835, 0.85718489141579368, 0.85400247341506719, 0.8507877767388532, 0.84754092289283123, 0.8442620345981231, 0.84095123578665476, 0.8376086515964718, 0.83423440836700968, 0.83082863363431847, 0.82739145612624232, 0.82392300575755428, 0.82042341362504534, 0.81689281200256991, 0.81333133433604599, 0.80973911523841147, 0.80611629048453592, 0.80246299700608914, 0.79877937288636502, 0.7950655573550629, 0.79132169078302494, 0.78754791467693042, 0.78374437167394739, 0.77991120553634141, 0.77604856114604148, 0.77215658449916424, 0.76823542270049605, 0.76428522395793219, 0.7603061375768756, 0.75629831395459302, 0.75226190457453135, 0.74819706200059122, 0.7441039398713607, 0.73998269289430851, 0.73583347683993672, 0.73165644853589207, 0.72745176586103977, 0.72321958773949491, 0.71896007413461649, 0.71467338604296105, 0.71035968548819706, 0.70601913551498185, 0.70165190018279788, 0.69725814455975277, 0.69283803471633953, 0.68839173771916018, 0.68391942162461061, 0.6794212554725293, 0.67489740927980701, 0.67034805403396192, 0.66577336168667567, 0.66117350514729512, 0.65654865827629605, 0.65189899587871258, 0.64722469369752944, 0.6425259284070397, 0.63780287760616672, 0.63305571981175202, 0.62828463445180749, 0.62348980185873359, 0.61867140326250347, 0.61382962078381298, 0.60896463742719675, 0.60407663707411186, 0.59916580447598711, 0.59423232524724023, 0.58927638585826192, 0.58429817362836856, 0.57929787671872113, 0.57427568412521424, 0.56923178567133192, 0.56416637200097319, 0.55907963457124654, 0.55397176564523298, 0.5488429582847193, 0.5436934063429012, 0.53852330445705543, 0.53333284804118442, 0.52812223327862839, 0.52289165711465235, 0.51764131724900009, 0.51237141212842374, 0.50708214093918114, 0.50177370359950879, 0.49644630075206486, 0.49110013375634509, 0.48573540468107329, 0.48035231629656205, 0.47495107206705045, 0.46953187614301212, 0.46409493335344021, 0.45864044919810504, 0.45316862983978612, 0.44767968209648135, 0.44217381343358825, 0.43665123195606403, 0.43111214640055828, 0.42555676612752463, 0.41998530111330729, 0.41439796194220363, 0.40879495979850627, 0.40317650645851943, 0.39754281428255606, 0.3918940962069094, 0.38623056573580644, 0.38055243693333718, 0.3748599244153632, 0.36915324334140731, 0.36343260940651945, 0.35769823883312568, 0.35195034836285416, 0.34618915524834432, 0.34041487724503472, 0.33462773260293199, 0.32882794005836308, 0.32301571882570607, 0.31719128858910622, 0.31135486949417079, 0.30550668213964982, 0.29964694756909749, 0.29377588726251663, 0.28789372312798917, 0.28200067749328667, 0.27609697309746906, 0.27018283308246382, 0.26425848098463345, 0.25832414072632598, 0.25238003660741054, 0.24642639329680122, 0.24046343582396335, 0.23449138957040974, 0.22851048026118126, 0.22252093395631445, 0.21652297704229864, 0.21051683622351761, 0.20450273851368242, 0.19848091122724945, 0.19245158197082995, 0.18641497863458675, 0.1803713293836198, 0.17432086264934399, 0.16826380712085329, 0.16220039173627876, 0.15613084567413366, 0.1500553983446527, 0.14397427938112045, 0.13788771863119115, 0.13179594614820278, 0.12569919218247999, 0.11959768717263308, 0.11349166173684638, 0.10738134666416307, 0.10126697290576155, 0.095148771566225324, 0.089026973894809708, 0.082901811276699419, 0.076773515224264705, 0.070642317368309157, 0.064508449449316344, 0.058372143308689985, 0.052233630879990445, 0.046093144180169916, 0.039950915300801082, 0.033807176399306589, 0.027662159690182372, 0.021516097436222258, 0.01536922193973846, 0.0092217655337806046, 0.0030739605733557966, -0.0030739605733554522, -0.0092217655337804832, -0.015369221939738116, -0.021516097436222133, -0.027662159690182025, -0.033807176399306464, -0.039950915300800735, -0.046093144180169791, -0.052233630879990098, -0.05837214330868986, -0.064508449449316232, -0.07064231736830906, -0.076773515224264371, -0.082901811276699308, -0.089026973894809375, -0.095148771566225213, -0.10126697290576121, -0.10738134666416296, -0.11349166173684605, -0.11959768717263299, -0.12569919218247966, -0.13179594614820267, -0.13788771863119104, -0.14397427938112034, -0.15005539834465259, -0.15613084567413354, -0.16220039173627843, -0.16826380712085318, -0.17432086264934366, -0.18037132938361969, -0.18641497863458642, -0.19245158197082984, -0.19848091122724912, -0.20450273851368231, -0.21051683622351727, -0.21652297704229853, -0.22252093395631434, -0.22851048026118118, -0.23449138957040966, -0.24046343582396323, -0.24642639329680088, -0.25238003660741043, -0.25832414072632565, -0.26425848098463334, -0.27018283308246349, -0.27609697309746895, -0.28200067749328633, -0.28789372312798905, -0.2937758872625163, -0.29964694756909738, -0.30550668213964971, -0.31135486949417068, -0.31719128858910589, -0.32301571882570601, -0.32882794005836274, -0.33462773260293188, -0.34041487724503444, -0.3461891552483442, -0.35195034836285388, -0.35769823883312557, -0.36343260940651911, -0.3691532433414072, -0.37485992441536287, -0.38055243693333707, -0.38623056573580633, -0.39189409620690935, -0.39754281428255578, -0.40317650645851938, -0.408794959798506, -0.41439796194220352, -0.41998530111330723, -0.42555676612752458, -0.43111214640055795, -0.43665123195606392, -0.44217381343358819, -0.44767968209648107, -0.45316862983978584, -0.45864044919810493, -0.46409493335344015, -0.46953187614301223, -0.47495107206704995, -0.48035231629656183, -0.4857354046810729, -0.49110013375634509, -0.4964463007520647, -0.50177370359950857, -0.5070821409391808, -0.51237141212842352, -0.51764131724899998, -0.52289165711465191, -0.52812223327862795, -0.53333284804118419, -0.53852330445705532, -0.5436934063429012, -0.54884295828471885, -0.55397176564523276, -0.55907963457124621, -0.56416637200097308, -0.5692317856713317, -0.57427568412521401, -0.57929787671872079, -0.58429817362836844, -0.5892763858582617, -0.5942323252472399, -0.59916580447598666, -0.60407663707411174, -0.60896463742719653, -0.61382962078381298, -0.61867140326250303, -0.62348980185873337, -0.62828463445180716, -0.6330557198117519, -0.6378028776061665, -0.64252592840703937, -0.64722469369752911, -0.65189899587871247, -0.65654865827629583, -0.66117350514729478, -0.66577336168667522, -0.67034805403396169, -0.67489740927980679, -0.6794212554725293, -0.68391942162461028, -0.68839173771915996, -0.6928380347163392, -0.69725814455975266, -0.70165190018279777, -0.70601913551498163, -0.71035968548819683, -0.71467338604296105, -0.71896007413461638, -0.72321958773949468, -0.72745176586103955, -0.73165644853589207, -0.73583347683993661, -0.73998269289430874, -0.74410393987136036, -0.74819706200059111, -0.75226190457453113, -0.75629831395459302, -0.76030613757687548, -0.76428522395793208, -0.76823542270049594, -0.77215658449916424, -0.77604856114604126, -0.77991120553634119, -0.78374437167394717, -0.78754791467693031, -0.79132169078302472, -0.7950655573550629, -0.79877937288636469, -0.80246299700608903, -0.80611629048453581, -0.80973911523841147, -0.81333133433604599, -0.8168928120025698, -0.82042341362504512, -0.82392300575755417, -0.82739145612624221, -0.83082863363431825, -0.83423440836700946, -0.8376086515964718, -0.84095123578665465, -0.8442620345981231, -0.84754092289283089, -0.85078777673885309, -0.85400247341506696, -0.85718489141579368, -0.86033491045538824, -0.86345241147278773, -0.86653727663601066, -0.86958938934661101, -0.87260863424408419, -0.87559489721022921, -0.87854806537346053, -0.88146802711307481, -0.88435467206346929, -0.88720789111831455, -0.89002757643467667, -0.89281362143709475, -0.89556592082160857, -0.89828437055973898, -0.90096886790241903, -0.90361931138387908, -0.90623560082548038, -0.90881763733950294, -0.91136532333288134, -0.9138785625108955, -0.91635725988080885, -0.91880132175545981, -0.92121065575680139, -0.92358517081939495, -0.9259247771938498, -0.92822938645021758, -0.93049891148133312, -0.93273326650610799, -0.9349323670727715, -0.93709613006206383, -0.93922447369037709, -0.94131731751284708, -0.9433745824263926, -0.94539619067270697, -0.94738206584119544, -0.94933213287186502, -0.95124631805815973, -0.95312454904974775, -0.95496675485525517, -0.95677286584495025, -0.95854281375337413, -0.96027653168192206, -0.96197395410137099, -0.96363501685435693, -0.96525965715780004, -0.9668478136052775, -0.96839942616934394, -0.96991443620380113, -0.97139278644591398, -0.97283442101857565, -0.97423928543241844, -0.97560732658787452, -0.9769384927771817, -0.9782327336863389, -0.97949000039700751, -0.98071024538836005, -0.98189342253887657, -0.98303948712808775, -0.98414839583826574, -0.98522010675606064, -0.98625457937408501, -0.9872517745924454, -0.98821165472021921, -0.98913418347688054, -0.99001932599367015, -0.99086704881491472, -0.99167731989928998, -0.99245010862103311, -0.99318538577109949, -0.99388312355826691, -0.99454329561018584, -0.99516587697437653, -0.99575084411917214, -0.99629817493460782, -0.99680784873325645, -0.99727984625101107, -0.99771414964781235, -0.99811074250832332, -0.99846960984254973, -0.99879073808640628, -0.99907411510222999, -0.99931973017923825, -0.99952757403393411, -0.99969763881045715, -0.99982991808087995, -0.99992440684545181, -0.99998110153278685, -1.0, -1.0};
30
31 typedef struct
32 {
33 pyo_audio_HEAD
34 PyObject *input;
35 Stream *input_stream;
36 PyObject *freq;
37 Stream *freq_stream;
38 PyObject *q;
39 Stream *q_stream;
•40 void (*coeffs_func_ptr)(void *);
41 int init;
42 int modebuffer[4]; // need at least 2 slots for mul & add
43 int filtertype;
44 MYFLT nyquist;
45 MYFLT twoPiOverSr;
46 // sample memories
47 MYFLT x1;
48 MYFLT x2;
49 MYFLT y1;
50 MYFLT y2;
51 // variables
52 MYFLT c;
53 MYFLT w0;
54 MYFLT alpha;
55 // coefficients
56 MYFLT b0;
57 MYFLT b1;
58 MYFLT b2;
59 MYFLT a0;
60 MYFLT a1;
61 MYFLT a2;
62 } Biquad;
63
64 static void
65 Biquad_compute_coeffs_lp(Biquad *self)
66 {
67 self->b0 = self->b2 = (1 - self->c) / 2;
68 self->b1 = 1 - self->c;
69 self->a0 = 1.0 / (1 + self->alpha);
70 self->a1 = -2 * self->c;
71 self->a2 = 1 - self->alpha;
72 }
73
74 static void
75 Biquad_compute_coeffs_hp(Biquad *self)
76 {
77 self->b0 = (1 + self->c) / 2;
78 self->b1 = -(1 + self->c);
79 self->b2 = self->b0;
80 self->a0 = 1.0 / (1 + self->alpha);
81 self->a1 = -2 * self->c;
82 self->a2 = 1 - self->alpha;
83 }
84
85 static void
86 Biquad_compute_coeffs_bp(Biquad *self)
87 {
88 self->b0 = self->alpha;
89 self->b1 = 0;
90 self->b2 = -self->alpha;
91 self->a0 = 1.0 / (1 + self->alpha);
92 self->a1 = -2 * self->c;
93 self->a2 = 1 - self->alpha;
94 }
95
96 static void
97 Biquad_compute_coeffs_bs(Biquad *self)
98 {
99 self->b0 = 1;
100 self->b1 = self->a1 = -2 * self->c;
101 self->b2 = 1;
102 self->a0 = 1.0 / (1 + self->alpha);
103 self->a2 = 1 - self->alpha;
104 }
105
106 static void
107 Biquad_compute_coeffs_ap(Biquad *self)
108 {
109 self->b0 = self->a2 = 1 - self->alpha;
110 self->b1 = self->a1 = -2 * self->c;
111 self->b2 = 1 + self->alpha;
112 self->a0 = 1.0 / (1 + self->alpha);
113 }
114
115 static void
116 Biquad_compute_variables(Biquad *self, MYFLT freq, MYFLT q)
117 {
118 if (freq <= 1)
119 freq = 1;
120 else if (freq >= self->nyquist)
121 freq = self->nyquist;
122
123 if (q < 0.1)
124 q = 0.1;
125
126 self->w0 = freq * self->twoPiOverSr;
127 self->c = MYCOS(self->w0);
128 self->alpha = MYSIN(self->w0) / (2 * q);
129 (*self->coeffs_func_ptr)(self);
130 }
131
132 static void
133 Biquad_filters_ii(Biquad *self)
134 {
135 MYFLT val;
136 int i;
137 MYFLT *in = Stream_getData((Stream *)self->input_stream);
138
139 if (self->init == 1)
140 {
141 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
142 self->init = 0;
143 }
144
145 for (i = 0; i < self->bufsize; i++)
146 {
147 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
148 self->y2 = self->y1;
149 self->data[i] = self->y1 = val;
150 self->x2 = self->x1;
151 self->x1 = in[i];
152 }
153 }
154
155 static void
156 Biquad_filters_ai(Biquad *self)
157 {
158 MYFLT val, q;
159 int i;
160 MYFLT *in = Stream_getData((Stream *)self->input_stream);
161
162 if (self->init == 1)
163 {
164 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
165 self->init = 0;
166 }
167
168 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
169 q = PyFloat_AS_DOUBLE(self->q);
170
171 for (i = 0; i < self->bufsize; i++)
172 {
173 Biquad_compute_variables(self, fr[i], q);
174 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
175 self->y2 = self->y1;
176 self->data[i] = self->y1 = val;
177 self->x2 = self->x1;
178 self->x1 = in[i];
179 }
180 }
181
182 static void
183 Biquad_filters_ia(Biquad *self)
184 {
185 MYFLT val, fr;
186 int i;
187 MYFLT *in = Stream_getData((Stream *)self->input_stream);
188
189 if (self->init == 1)
190 {
191 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
192 self->init = 0;
193 }
194
195 fr = PyFloat_AS_DOUBLE(self->freq);
196 MYFLT *q = Stream_getData((Stream *)self->q_stream);
197
198 for (i = 0; i < self->bufsize; i++)
199 {
200 Biquad_compute_variables(self, fr, q[i]);
201 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
202 self->y2 = self->y1;
203 self->data[i] = self->y1 = val;
204 self->x2 = self->x1;
205 self->x1 = in[i];
206 }
207 }
208
209 static void
210 Biquad_filters_aa(Biquad *self)
211 {
212 MYFLT val;
213 int i;
214 MYFLT *in = Stream_getData((Stream *)self->input_stream);
215
216 if (self->init == 1)
217 {
218 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
219 self->init = 0;
220 }
221
222 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
223 MYFLT *q = Stream_getData((Stream *)self->q_stream);
224
225 for (i = 0; i < self->bufsize; i++)
226 {
227 Biquad_compute_variables(self, fr[i], q[i]);
228 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
229 self->y2 = self->y1;
230 self->data[i] = self->y1 = val;
231 self->x2 = self->x1;
232 self->x1 = in[i];
233 }
234 }
235
236 static void Biquad_postprocessing_ii(Biquad *self) { POST_PROCESSING_II };
237 static void Biquad_postprocessing_ai(Biquad *self) { POST_PROCESSING_AI };
238 static void Biquad_postprocessing_ia(Biquad *self) { POST_PROCESSING_IA };
239 static void Biquad_postprocessing_aa(Biquad *self) { POST_PROCESSING_AA };
240 static void Biquad_postprocessing_ireva(Biquad *self) { POST_PROCESSING_IREVA };
241 static void Biquad_postprocessing_areva(Biquad *self) { POST_PROCESSING_AREVA };
242 static void Biquad_postprocessing_revai(Biquad *self) { POST_PROCESSING_REVAI };
243 static void Biquad_postprocessing_revaa(Biquad *self) { POST_PROCESSING_REVAA };
244 static void Biquad_postprocessing_revareva(Biquad *self) { POST_PROCESSING_REVAREVA };
245
246 static void
247 Biquad_setProcMode(Biquad *self)
248 {
249 int procmode, muladdmode;
250 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
251 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
252
253 switch (self->filtertype)
254 {
255 case 0:
256 self->coeffs_func_ptr = Biquad_compute_coeffs_lp;
257 break;
258
259 case 1:
260 self->coeffs_func_ptr = Biquad_compute_coeffs_hp;
261 break;
262
263 case 2:
264 self->coeffs_func_ptr = Biquad_compute_coeffs_bp;
265 break;
266
267 case 3:
268 self->coeffs_func_ptr = Biquad_compute_coeffs_bs;
269 break;
270
271 case 4:
272 self->coeffs_func_ptr = Biquad_compute_coeffs_ap;
273 break;
274 }
275
276 switch (procmode)
277 {
278 case 0:
279 Biquad_compute_variables(self, PyFloat_AS_DOUBLE(self->freq), PyFloat_AS_DOUBLE(self->q));
280 self->proc_func_ptr = Biquad_filters_ii;
281 break;
282
283 case 1:
284 self->proc_func_ptr = Biquad_filters_ai;
285 break;
286
287 case 10:
288 self->proc_func_ptr = Biquad_filters_ia;
289 break;
290
291 case 11:
292 self->proc_func_ptr = Biquad_filters_aa;
293 break;
294 }
295
296 switch (muladdmode)
297 {
298 case 0:
299 self->muladd_func_ptr = Biquad_postprocessing_ii;
300 break;
301
302 case 1:
303 self->muladd_func_ptr = Biquad_postprocessing_ai;
304 break;
305
306 case 2:
307 self->muladd_func_ptr = Biquad_postprocessing_revai;
308 break;
309
310 case 10:
311 self->muladd_func_ptr = Biquad_postprocessing_ia;
312 break;
313
314 case 11:
315 self->muladd_func_ptr = Biquad_postprocessing_aa;
316 break;
317
318 case 12:
319 self->muladd_func_ptr = Biquad_postprocessing_revaa;
320 break;
321
322 case 20:
323 self->muladd_func_ptr = Biquad_postprocessing_ireva;
324 break;
325
326 case 21:
327 self->muladd_func_ptr = Biquad_postprocessing_areva;
328 break;
329
330 case 22:
331 self->muladd_func_ptr = Biquad_postprocessing_revareva;
332 break;
333 }
334 }
335
336 static void
337 Biquad_compute_next_data_frame(Biquad *self)
338 {
339 (*self->proc_func_ptr)(self);
340 (*self->muladd_func_ptr)(self);
341 }
342
343 static int
344 Biquad_traverse(Biquad *self, visitproc visit, void *arg)
345 {
346 pyo_VISIT
347 Py_VISIT(self->input);
348 Py_VISIT(self->freq);
349 Py_VISIT(self->q);
350 return 0;
351 }
352
353 static int
354 Biquad_clear(Biquad *self)
355 {
356 pyo_CLEAR
357 Py_CLEAR(self->input);
358 Py_CLEAR(self->freq);
359 Py_CLEAR(self->q);
360 return 0;
361 }
362
363 static void
364 Biquad_dealloc(Biquad* self)
365 {
366 pyo_DEALLOC
367 Biquad_clear(self);
368 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
369 Py_TYPE(self)->tp_free((PyObject*)self);
370 }
371
372 static PyObject *
373 Biquad_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
374 {
375 int i;
376 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
377 Biquad *self;
378 self = (Biquad *)type->tp_alloc(type, 0);
379
380 self->freq = PyFloat_FromDouble(1000);
381 self->q = PyFloat_FromDouble(1);
382 self->filtertype = 0;
383 self->modebuffer[0] = 0;
384 self->modebuffer[1] = 0;
385 self->modebuffer[2] = 0;
386 self->modebuffer[3] = 0;
387 self->init = 1;
388
389 INIT_OBJECT_COMMON
390
391 self->nyquist = (MYFLT)self->sr * 0.49;
392 self->twoPiOverSr = TWOPI / (MYFLT)self->sr;
393
394 Stream_setFunctionPtr(self->stream, Biquad_compute_next_data_frame);
395 self->mode_func_ptr = Biquad_setProcMode;
396
397 static char *kwlist[] = {"input", "freq", "q", "type", "mul", "add", NULL};
398
399 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOiOO", kwlist, &inputtmp, &freqtmp, &qtmp, &self->filtertype, &multmp, &addtmp))
400 Py_RETURN_NONE;
401
402 INIT_INPUT_STREAM
403
404 if (freqtmp)
405 {
406 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
407 }
408
409 if (qtmp)
410 {
411 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
412 }
413
414 if (multmp)
415 {
416 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
417 }
418
419 if (addtmp)
420 {
421 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
422 }
423
424 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
425
426 (*self->mode_func_ptr)(self);
427
428 return (PyObject *)self;
429 }
430
431 static PyObject * Biquad_getServer(Biquad* self) { GET_SERVER };
432 static PyObject * Biquad_getStream(Biquad* self) { GET_STREAM };
433 static PyObject * Biquad_setMul(Biquad *self, PyObject *arg) { SET_MUL };
434 static PyObject * Biquad_setAdd(Biquad *self, PyObject *arg) { SET_ADD };
435 static PyObject * Biquad_setSub(Biquad *self, PyObject *arg) { SET_SUB };
436 static PyObject * Biquad_setDiv(Biquad *self, PyObject *arg) { SET_DIV };
437
438 static PyObject * Biquad_play(Biquad *self, PyObject *args, PyObject *kwds) { PLAY };
439 static PyObject * Biquad_out(Biquad *self, PyObject *args, PyObject *kwds) { OUT };
440 static PyObject * Biquad_stop(Biquad *self, PyObject *args, PyObject *kwds) { STOP };
441
442 static PyObject * Biquad_multiply(Biquad *self, PyObject *arg) { MULTIPLY };
443 static PyObject * Biquad_inplace_multiply(Biquad *self, PyObject *arg) { INPLACE_MULTIPLY };
444 static PyObject * Biquad_add(Biquad *self, PyObject *arg) { ADD };
445 static PyObject * Biquad_inplace_add(Biquad *self, PyObject *arg) { INPLACE_ADD };
446 static PyObject * Biquad_sub(Biquad *self, PyObject *arg) { SUB };
447 static PyObject * Biquad_inplace_sub(Biquad *self, PyObject *arg) { INPLACE_SUB };
448 static PyObject * Biquad_div(Biquad *self, PyObject *arg) { DIV };
449 static PyObject * Biquad_inplace_div(Biquad *self, PyObject *arg) { INPLACE_DIV };
450
451 static PyObject * Biquad_setFreq(Biquad *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
452 static PyObject * Biquad_setQ(Biquad *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
453
454 static PyObject *
455 Biquad_setType(Biquad *self, PyObject *arg)
456 {
457 ASSERT_ARG_NOT_NULL
458
459 if (PyLong_Check(arg))
460 {
461 self->filtertype = PyLong_AsLong(arg);
462 }
463
464 (*self->mode_func_ptr)(self);
465
466 Py_RETURN_NONE;
467 }
468
469 static PyMemberDef Biquad_members[] =
470 {
471 {"server", T_OBJECT_EX, offsetof(Biquad, server), 0, "Pyo server."},
472 {"stream", T_OBJECT_EX, offsetof(Biquad, stream), 0, "Stream object."},
473 {"input", T_OBJECT_EX, offsetof(Biquad, input), 0, "Input sound object."},
474 {"freq", T_OBJECT_EX, offsetof(Biquad, freq), 0, "Cutoff frequency in cycle per second."},
475 {"q", T_OBJECT_EX, offsetof(Biquad, q), 0, "Q factor."},
476 {"mul", T_OBJECT_EX, offsetof(Biquad, mul), 0, "Mul factor."},
477 {"add", T_OBJECT_EX, offsetof(Biquad, add), 0, "Add factor."},
478 {NULL} /* Sentinel */
479 };
480
481 static PyMethodDef Biquad_methods[] =
482 {
483 {"getServer", (PyCFunction)Biquad_getServer, METH_NOARGS, "Returns server object."},
484 {"_getStream", (PyCFunction)Biquad_getStream, METH_NOARGS, "Returns stream object."},
485 {"play", (PyCFunction)Biquad_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
486 {"out", (PyCFunction)Biquad_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
487 {"stop", (PyCFunction)Biquad_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
488 {"setFreq", (PyCFunction)Biquad_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
489 {"setQ", (PyCFunction)Biquad_setQ, METH_O, "Sets filter Q factor."},
490 {"setType", (PyCFunction)Biquad_setType, METH_O, "Sets filter type factor."},
491 {"setMul", (PyCFunction)Biquad_setMul, METH_O, "Sets oscillator mul factor."},
492 {"setAdd", (PyCFunction)Biquad_setAdd, METH_O, "Sets oscillator add factor."},
493 {"setSub", (PyCFunction)Biquad_setSub, METH_O, "Sets inverse add factor."},
494 {"setDiv", (PyCFunction)Biquad_setDiv, METH_O, "Sets inverse mul factor."},
495 {NULL} /* Sentinel */
496 };
497
498 static PyNumberMethods Biquad_as_number =
499 {
500 (binaryfunc)Biquad_add, /*nb_add*/
501 (binaryfunc)Biquad_sub, /*nb_subtract*/
502 (binaryfunc)Biquad_multiply, /*nb_multiply*/
503 0, /*nb_remainder*/
504 0, /*nb_divmod*/
505 0, /*nb_power*/
506 0, /*nb_neg*/
507 0, /*nb_pos*/
508 0, /*(unaryfunc)array_abs,*/
509 0, /*nb_nonzero*/
510 0, /*nb_invert*/
511 0, /*nb_lshift*/
512 0, /*nb_rshift*/
513 0, /*nb_and*/
514 0, /*nb_xor*/
515 0, /*nb_or*/
516 0, /*nb_int*/
517 0, /*nb_long*/
518 0, /*nb_float*/
519 (binaryfunc)Biquad_inplace_add, /*inplace_add*/
520 (binaryfunc)Biquad_inplace_sub, /*inplace_subtract*/
521 (binaryfunc)Biquad_inplace_multiply, /*inplace_multiply*/
522 0, /*inplace_remainder*/
523 0, /*inplace_power*/
524 0, /*inplace_lshift*/
525 0, /*inplace_rshift*/
526 0, /*inplace_and*/
527 0, /*inplace_xor*/
528 0, /*inplace_or*/
529 0, /*nb_floor_divide*/
530 (binaryfunc)Biquad_div, /*nb_true_divide*/
531 0, /*nb_inplace_floor_divide*/
532 (binaryfunc)Biquad_inplace_div, /*nb_inplace_true_divide*/
533 0, /* nb_index */
534 };
535
536 PyTypeObject BiquadType =
537 {
538 PyVarObject_HEAD_INIT(NULL, 0)
539 "_pyo.Biquad_base", /*tp_name*/
540 sizeof(Biquad), /*tp_basicsize*/
541 0, /*tp_itemsize*/
542 (destructor)Biquad_dealloc, /*tp_dealloc*/
543 0, /*tp_print*/
544 0, /*tp_getattr*/
545 0, /*tp_setattr*/
546 0, /*tp_as_async (tp_compare in Python 2)*/
547 0, /*tp_repr*/
548 &Biquad_as_number, /*tp_as_number*/
549 0, /*tp_as_sequence*/
550 0, /*tp_as_mapping*/
551 0, /*tp_hash */
552 0, /*tp_call*/
553 0, /*tp_str*/
554 0, /*tp_getattro*/
555 0, /*tp_setattro*/
556 0, /*tp_as_buffer*/
557 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
558 "Biquad objects. Generates a biquadratic filter.", /* tp_doc */
559 (traverseproc)Biquad_traverse, /* tp_traverse */
560 (inquiry)Biquad_clear, /* tp_clear */
561 0, /* tp_richcompare */
562 0, /* tp_weaklistoffset */
563 0, /* tp_iter */
564 0, /* tp_iternext */
565 Biquad_methods, /* tp_methods */
566 Biquad_members, /* tp_members */
567 0, /* tp_getset */
568 0, /* tp_base */
569 0, /* tp_dict */
570 0, /* tp_descr_get */
571 0, /* tp_descr_set */
572 0, /* tp_dictoffset */
573 0, /* tp_init */
574 0, /* tp_alloc */
575 Biquad_new, /* tp_new */
576 };
577
578 typedef struct
579 {
580 pyo_audio_HEAD
581 PyObject *input;
582 Stream *input_stream;
583 PyObject *freq;
584 Stream *freq_stream;
585 PyObject *q;
586 Stream *q_stream;
•587 void (*coeffs_func_ptr)(void *);
588 int init;
589 int modebuffer[4]; // need at least 2 slots for mul & add
590 int filtertype;
591 int stages;
592 MYFLT nyquist;
593 // sample memories
594 MYFLT *x1;
595 MYFLT *x2;
596 MYFLT *y1;
597 MYFLT *y2;
598 // variables
599 MYFLT c;
600 MYFLT w0;
601 MYFLT alpha;
602 // coefficients
603 MYFLT b0;
604 MYFLT b1;
605 MYFLT b2;
606 MYFLT a0;
607 MYFLT a1;
608 MYFLT a2;
609 } Biquadx;
610
611 static void
612 Biquadx_allocate_memories(Biquadx *self)
613 {
614 self->x1 = (MYFLT *)PyMem_RawRealloc(self->x1, self->stages * sizeof(MYFLT));
615 self->x2 = (MYFLT *)PyMem_RawRealloc(self->x2, self->stages * sizeof(MYFLT));
616 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, self->stages * sizeof(MYFLT));
617 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, self->stages * sizeof(MYFLT));
618 self->init = 1;
619 }
620
621 static void
622 Biquadx_compute_coeffs_lp(Biquadx *self)
623 {
624 self->b0 = self->b2 = (1 - self->c) / 2;
625 self->b1 = 1 - self->c;
626 self->a0 = 1.0 / (1 + self->alpha);
627 self->a1 = -2 * self->c;
628 self->a2 = 1 - self->alpha;
629 }
630
631 static void
632 Biquadx_compute_coeffs_hp(Biquadx *self)
633 {
634 self->b0 = (1 + self->c) / 2;
635 self->b1 = -(1 + self->c);
636 self->b2 = self->b0;
637 self->a0 = 1.0 / (1 + self->alpha);
638 self->a1 = -2 * self->c;
639 self->a2 = 1 - self->alpha;
640 }
641
642 static void
643 Biquadx_compute_coeffs_bp(Biquadx *self)
644 {
645 self->b0 = self->alpha;
646 self->b1 = 0;
647 self->b2 = -self->alpha;
648 self->a0 = 1.0 / (1 + self->alpha);
649 self->a1 = -2 * self->c;
650 self->a2 = 1 - self->alpha;
651 }
652
653 static void
654 Biquadx_compute_coeffs_bs(Biquadx *self)
655 {
656 self->b0 = 1;
657 self->b1 = self->a1 = -2 * self->c;
658 self->b2 = 1;
659 self->a0 = 1.0 / (1 + self->alpha);
660 self->a2 = 1 - self->alpha;
661 }
662
663 static void
664 Biquadx_compute_coeffs_ap(Biquadx *self)
665 {
666 self->b0 = self->a2 = 1 - self->alpha;
667 self->b1 = self->a1 = -2 * self->c;
668 self->b2 = 1 + self->alpha;
669 self->a0 = 1.0 / (1 + self->alpha);
670 }
671
672 static void
673 Biquadx_compute_variables(Biquadx *self, MYFLT freq, MYFLT q)
674 {
675 if (freq <= 1)
676 freq = 1;
677 else if (freq >= self->nyquist)
678 freq = self->nyquist;
679
680 if (q < 0.1)
681 q = 0.1;
682
683 self->w0 = TWOPI * freq / self->sr;
684 self->c = MYCOS(self->w0);
685 self->alpha = MYSIN(self->w0) / (2 * q);
686 (*self->coeffs_func_ptr)(self);
687 }
688
689 static void
690 Biquadx_filters_ii(Biquadx *self)
691 {
692 MYFLT vin, vout;
693 int i, j;
694 MYFLT *in = Stream_getData((Stream *)self->input_stream);
695
696 if (self->init == 1)
697 {
698 for (i = 0; i < self->stages; i++)
699 {
700 self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = in[0];
701 }
702
703 self->init = 0;
704 }
705
706 vout = 0.0;
707
708 for (i = 0; i < self->bufsize; i++)
709 {
710 vin = in[i];
711
712 for (j = 0; j < self->stages; j++)
713 {
714 vout = ( (self->b0 * vin) + (self->b1 * self->x1[j]) + (self->b2 * self->x2[j]) - (self->a1 * self->y1[j]) - (self->a2 * self->y2[j]) ) * self->a0;
715 self->x2[j] = self->x1[j];
716 self->x1[j] = vin;
717 self->y2[j] = self->y1[j];
718 self->y1[j] = vin = vout;
719 }
720
721 self->data[i] = vout;
722 }
723 }
724
725 static void
726 Biquadx_filters_ai(Biquadx *self)
727 {
728 MYFLT vin, vout, q;
729 int i, j;
730 MYFLT *in = Stream_getData((Stream *)self->input_stream);
731
732 if (self->init == 1)
733 {
734 for (i = 0; i < self->stages; i++)
735 {
736 self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = in[0];
737 }
738
739 self->init = 0;
740 }
741
742 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
743 q = PyFloat_AS_DOUBLE(self->q);
744
745 vout = 0.0;
746
747 for (i = 0; i < self->bufsize; i++)
748 {
749 Biquadx_compute_variables(self, fr[i], q);
750 vin = in[i];
751
752 for (j = 0; j < self->stages; j++)
753 {
754 vout = ( (self->b0 * vin) + (self->b1 * self->x1[j]) + (self->b2 * self->x2[j]) - (self->a1 * self->y1[j]) - (self->a2 * self->y2[j]) ) * self->a0;
755 self->x2[j] = self->x1[j];
756 self->x1[j] = vin;
757 self->y2[j] = self->y1[j];
758 self->y1[j] = vin = vout;
759 }
760
761 self->data[i] = vout;
762 }
763 }
764
765 static void
766 Biquadx_filters_ia(Biquadx *self)
767 {
768 MYFLT vin, vout, fr;
769 int i, j;
770 MYFLT *in = Stream_getData((Stream *)self->input_stream);
771
772 if (self->init == 1)
773 {
774 for (i = 0; i < self->stages; i++)
775 {
776 self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = in[0];
777 }
778
779 self->init = 0;
780 }
781
782 fr = PyFloat_AS_DOUBLE(self->freq);
783 MYFLT *q = Stream_getData((Stream *)self->q_stream);
784
785 vout = 0.0;
786
787 for (i = 0; i < self->bufsize; i++)
788 {
789 Biquadx_compute_variables(self, fr, q[i]);
790 vin = in[i];
791
792 for (j = 0; j < self->stages; j++)
793 {
794 vout = ( (self->b0 * vin) + (self->b1 * self->x1[j]) + (self->b2 * self->x2[j]) - (self->a1 * self->y1[j]) - (self->a2 * self->y2[j]) ) * self->a0;
795 self->x2[j] = self->x1[j];
796 self->x1[j] = vin;
797 self->y2[j] = self->y1[j];
798 self->y1[j] = vin = vout;
799 }
800
801 self->data[i] = vout;
802 }
803 }
804
805 static void
806 Biquadx_filters_aa(Biquadx *self)
807 {
808 MYFLT vin, vout;
809 int i, j;
810 MYFLT *in = Stream_getData((Stream *)self->input_stream);
811
812 if (self->init == 1)
813 {
814 for (i = 0; i < self->stages; i++)
815 {
816 self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = in[0];
817 }
818
819 self->init = 0;
820 }
821
822 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
823 MYFLT *q = Stream_getData((Stream *)self->q_stream);
824
825 vout = 0.0;
826
827 for (i = 0; i < self->bufsize; i++)
828 {
829 Biquadx_compute_variables(self, fr[i], q[i]);
830 vin = in[i];
831
832 for (j = 0; j < self->stages; j++)
833 {
834 vout = ( (self->b0 * vin) + (self->b1 * self->x1[j]) + (self->b2 * self->x2[j]) - (self->a1 * self->y1[j]) - (self->a2 * self->y2[j]) ) * self->a0;
835 self->x2[j] = self->x1[j];
836 self->x1[j] = vin;
837 self->y2[j] = self->y1[j];
838 self->y1[j] = vin = vout;
839 }
840
841 self->data[i] = vout;
842 }
843 }
844
845 static void Biquadx_postprocessing_ii(Biquadx *self) { POST_PROCESSING_II };
846 static void Biquadx_postprocessing_ai(Biquadx *self) { POST_PROCESSING_AI };
847 static void Biquadx_postprocessing_ia(Biquadx *self) { POST_PROCESSING_IA };
848 static void Biquadx_postprocessing_aa(Biquadx *self) { POST_PROCESSING_AA };
849 static void Biquadx_postprocessing_ireva(Biquadx *self) { POST_PROCESSING_IREVA };
850 static void Biquadx_postprocessing_areva(Biquadx *self) { POST_PROCESSING_AREVA };
851 static void Biquadx_postprocessing_revai(Biquadx *self) { POST_PROCESSING_REVAI };
852 static void Biquadx_postprocessing_revaa(Biquadx *self) { POST_PROCESSING_REVAA };
853 static void Biquadx_postprocessing_revareva(Biquadx *self) { POST_PROCESSING_REVAREVA };
854
855 static void
856 Biquadx_setProcMode(Biquadx *self)
857 {
858 int procmode, muladdmode;
859 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
860 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
861
862 switch (self->filtertype)
863 {
864 case 0:
865 self->coeffs_func_ptr = Biquadx_compute_coeffs_lp;
866 break;
867
868 case 1:
869 self->coeffs_func_ptr = Biquadx_compute_coeffs_hp;
870 break;
871
872 case 2:
873 self->coeffs_func_ptr = Biquadx_compute_coeffs_bp;
874 break;
875
876 case 3:
877 self->coeffs_func_ptr = Biquadx_compute_coeffs_bs;
878 break;
879
880 case 4:
881 self->coeffs_func_ptr = Biquadx_compute_coeffs_ap;
882 break;
883 }
884
885 switch (procmode)
886 {
887 case 0:
888 Biquadx_compute_variables(self, PyFloat_AS_DOUBLE(self->freq), PyFloat_AS_DOUBLE(self->q));
889 self->proc_func_ptr = Biquadx_filters_ii;
890 break;
891
892 case 1:
893 self->proc_func_ptr = Biquadx_filters_ai;
894 break;
895
896 case 10:
897 self->proc_func_ptr = Biquadx_filters_ia;
898 break;
899
900 case 11:
901 self->proc_func_ptr = Biquadx_filters_aa;
902 break;
903 }
904
905 switch (muladdmode)
906 {
907 case 0:
908 self->muladd_func_ptr = Biquadx_postprocessing_ii;
909 break;
910
911 case 1:
912 self->muladd_func_ptr = Biquadx_postprocessing_ai;
913 break;
914
915 case 2:
916 self->muladd_func_ptr = Biquadx_postprocessing_revai;
917 break;
918
919 case 10:
920 self->muladd_func_ptr = Biquadx_postprocessing_ia;
921 break;
922
923 case 11:
924 self->muladd_func_ptr = Biquadx_postprocessing_aa;
925 break;
926
927 case 12:
928 self->muladd_func_ptr = Biquadx_postprocessing_revaa;
929 break;
930
931 case 20:
932 self->muladd_func_ptr = Biquadx_postprocessing_ireva;
933 break;
934
935 case 21:
936 self->muladd_func_ptr = Biquadx_postprocessing_areva;
937 break;
938
939 case 22:
940 self->muladd_func_ptr = Biquadx_postprocessing_revareva;
941 break;
942 }
943 }
944
945 static void
946 Biquadx_compute_next_data_frame(Biquadx *self)
947 {
948 (*self->proc_func_ptr)(self);
949 (*self->muladd_func_ptr)(self);
950 }
951
952 static int
953 Biquadx_traverse(Biquadx *self, visitproc visit, void *arg)
954 {
955 pyo_VISIT
956 Py_VISIT(self->input);
957 Py_VISIT(self->freq);
958 Py_VISIT(self->q);
959 return 0;
960 }
961
962 static int
963 Biquadx_clear(Biquadx *self)
964 {
965 pyo_CLEAR
966 Py_CLEAR(self->input);
967 Py_CLEAR(self->freq);
968 Py_CLEAR(self->q);
969 return 0;
970 }
971
972 static void
973 Biquadx_dealloc(Biquadx* self)
974 {
975 pyo_DEALLOC
976 PyMem_RawFree(self->x1);
977 PyMem_RawFree(self->x2);
978 PyMem_RawFree(self->y1);
979 PyMem_RawFree(self->y2);
980 Biquadx_clear(self);
981 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
982 Py_TYPE(self)->tp_free((PyObject*)self);
983 }
984
985 static PyObject *
986 Biquadx_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
987 {
988 int i;
989 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
990 Biquadx *self;
991 self = (Biquadx *)type->tp_alloc(type, 0);
992
993 self->freq = PyFloat_FromDouble(1000);
994 self->q = PyFloat_FromDouble(1);
995 self->filtertype = 0;
996 self->stages = 4;
997 self->modebuffer[0] = 0;
998 self->modebuffer[1] = 0;
999 self->modebuffer[2] = 0;
1000 self->modebuffer[3] = 0;
1001 self->init = 1;
1002
1003 INIT_OBJECT_COMMON
1004
1005 self->nyquist = (MYFLT)self->sr * 0.49;
1006
1007 Stream_setFunctionPtr(self->stream, Biquadx_compute_next_data_frame);
1008 self->mode_func_ptr = Biquadx_setProcMode;
1009
1010 static char *kwlist[] = {"input", "freq", "q", "type", "stages", "mul", "add", NULL};
1011
1012 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOiiOO", kwlist, &inputtmp, &freqtmp, &qtmp, &self->filtertype, &self->stages, &multmp, &addtmp))
1013 Py_RETURN_NONE;
1014
1015 INIT_INPUT_STREAM
1016
1017 if (freqtmp)
1018 {
1019 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
1020 }
1021
1022 if (qtmp)
1023 {
1024 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
1025 }
1026
1027 if (multmp)
1028 {
1029 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1030 }
1031
1032 if (addtmp)
1033 {
1034 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1035 }
1036
1037 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1038
1039 Biquadx_allocate_memories(self);
1040
1041 (*self->mode_func_ptr)(self);
1042
1043 return (PyObject *)self;
1044 }
1045
1046 static PyObject * Biquadx_getServer(Biquadx* self) { GET_SERVER };
1047 static PyObject * Biquadx_getStream(Biquadx* self) { GET_STREAM };
1048 static PyObject * Biquadx_setMul(Biquadx *self, PyObject *arg) { SET_MUL };
1049 static PyObject * Biquadx_setAdd(Biquadx *self, PyObject *arg) { SET_ADD };
1050 static PyObject * Biquadx_setSub(Biquadx *self, PyObject *arg) { SET_SUB };
1051 static PyObject * Biquadx_setDiv(Biquadx *self, PyObject *arg) { SET_DIV };
1052
1053 static PyObject * Biquadx_play(Biquadx *self, PyObject *args, PyObject *kwds) { PLAY };
1054 static PyObject * Biquadx_out(Biquadx *self, PyObject *args, PyObject *kwds) { OUT };
1055 static PyObject * Biquadx_stop(Biquadx *self, PyObject *args, PyObject *kwds) { STOP };
1056
1057 static PyObject * Biquadx_multiply(Biquadx *self, PyObject *arg) { MULTIPLY };
1058 static PyObject * Biquadx_inplace_multiply(Biquadx *self, PyObject *arg) { INPLACE_MULTIPLY };
1059 static PyObject * Biquadx_add(Biquadx *self, PyObject *arg) { ADD };
1060 static PyObject * Biquadx_inplace_add(Biquadx *self, PyObject *arg) { INPLACE_ADD };
1061 static PyObject * Biquadx_sub(Biquadx *self, PyObject *arg) { SUB };
1062 static PyObject * Biquadx_inplace_sub(Biquadx *self, PyObject *arg) { INPLACE_SUB };
1063 static PyObject * Biquadx_div(Biquadx *self, PyObject *arg) { DIV };
1064 static PyObject * Biquadx_inplace_div(Biquadx *self, PyObject *arg) { INPLACE_DIV };
1065
1066 static PyObject * Biquadx_setFreq(Biquadx *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
1067 static PyObject * Biquadx_setQ(Biquadx *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
1068
1069 static PyObject *
1070 Biquadx_setType(Biquadx *self, PyObject *arg)
1071 {
1072 ASSERT_ARG_NOT_NULL
1073
1074 if (PyLong_Check(arg))
1075 {
1076 self->filtertype = PyLong_AsLong(arg);
1077 }
1078
1079 (*self->mode_func_ptr)(self);
1080
1081 Py_RETURN_NONE;
1082 }
1083
1084 static PyObject *
1085 Biquadx_setStages(Biquadx *self, PyObject *arg)
1086 {
1087 ASSERT_ARG_NOT_NULL
1088
1089 if (PyLong_Check(arg))
1090 {
1091 self->stages = PyLong_AsLong(arg);
1092 Biquadx_allocate_memories(self);
1093 }
1094
1095 Py_RETURN_NONE;
1096 }
1097
1098 static PyMemberDef Biquadx_members[] =
1099 {
1100 {"server", T_OBJECT_EX, offsetof(Biquadx, server), 0, "Pyo server."},
1101 {"stream", T_OBJECT_EX, offsetof(Biquadx, stream), 0, "Stream object."},
1102 {"input", T_OBJECT_EX, offsetof(Biquadx, input), 0, "Input sound object."},
1103 {"freq", T_OBJECT_EX, offsetof(Biquadx, freq), 0, "Cutoff frequency in cycle per second."},
1104 {"q", T_OBJECT_EX, offsetof(Biquadx, q), 0, "Q factor."},
1105 {"mul", T_OBJECT_EX, offsetof(Biquadx, mul), 0, "Mul factor."},
1106 {"add", T_OBJECT_EX, offsetof(Biquadx, add), 0, "Add factor."},
1107 {NULL} /* Sentinel */
1108 };
1109
1110 static PyMethodDef Biquadx_methods[] =
1111 {
1112 {"getServer", (PyCFunction)Biquadx_getServer, METH_NOARGS, "Returns server object."},
1113 {"_getStream", (PyCFunction)Biquadx_getStream, METH_NOARGS, "Returns stream object."},
1114 {"play", (PyCFunction)Biquadx_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1115 {"out", (PyCFunction)Biquadx_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1116 {"stop", (PyCFunction)Biquadx_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1117 {"setFreq", (PyCFunction)Biquadx_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
1118 {"setQ", (PyCFunction)Biquadx_setQ, METH_O, "Sets filter Q factor."},
1119 {"setType", (PyCFunction)Biquadx_setType, METH_O, "Sets filter type factor."},
1120 {"setStages", (PyCFunction)Biquadx_setStages, METH_O, "Sets the number of filtering stages."},
1121 {"setMul", (PyCFunction)Biquadx_setMul, METH_O, "Sets oscillator mul factor."},
1122 {"setAdd", (PyCFunction)Biquadx_setAdd, METH_O, "Sets oscillator add factor."},
1123 {"setSub", (PyCFunction)Biquadx_setSub, METH_O, "Sets inverse add factor."},
1124 {"setDiv", (PyCFunction)Biquadx_setDiv, METH_O, "Sets inverse mul factor."},
1125 {NULL} /* Sentinel */
1126 };
1127
1128 static PyNumberMethods Biquadx_as_number =
1129 {
1130 (binaryfunc)Biquadx_add, /*nb_add*/
1131 (binaryfunc)Biquadx_sub, /*nb_subtract*/
1132 (binaryfunc)Biquadx_multiply, /*nb_multiply*/
1133 0, /*nb_remainder*/
1134 0, /*nb_divmod*/
1135 0, /*nb_power*/
1136 0, /*nb_neg*/
1137 0, /*nb_pos*/
1138 0, /*(unaryfunc)array_abs,*/
1139 0, /*nb_nonzero*/
1140 0, /*nb_invert*/
1141 0, /*nb_lshift*/
1142 0, /*nb_rshift*/
1143 0, /*nb_and*/
1144 0, /*nb_xor*/
1145 0, /*nb_or*/
1146 0, /*nb_int*/
1147 0, /*nb_long*/
1148 0, /*nb_float*/
1149 (binaryfunc)Biquadx_inplace_add, /*inplace_add*/
1150 (binaryfunc)Biquadx_inplace_sub, /*inplace_subtract*/
1151 (binaryfunc)Biquadx_inplace_multiply, /*inplace_multiply*/
1152 0, /*inplace_remainder*/
1153 0, /*inplace_power*/
1154 0, /*inplace_lshift*/
1155 0, /*inplace_rshift*/
1156 0, /*inplace_and*/
1157 0, /*inplace_xor*/
1158 0, /*inplace_or*/
1159 0, /*nb_floor_divide*/
1160 (binaryfunc)Biquadx_div, /*nb_true_divide*/
1161 0, /*nb_inplace_floor_divide*/
1162 (binaryfunc)Biquadx_inplace_div, /*nb_inplace_true_divide*/
1163 0, /* nb_index */
1164 };
1165
1166 PyTypeObject BiquadxType =
1167 {
1168 PyVarObject_HEAD_INIT(NULL, 0)
1169 "_pyo.Biquadx_base", /*tp_name*/
1170 sizeof(Biquadx), /*tp_basicsize*/
1171 0, /*tp_itemsize*/
1172 (destructor)Biquadx_dealloc, /*tp_dealloc*/
1173 0, /*tp_print*/
1174 0, /*tp_getattr*/
1175 0, /*tp_setattr*/
1176 0, /*tp_as_async (tp_compare in Python 2)*/
1177 0, /*tp_repr*/
1178 &Biquadx_as_number, /*tp_as_number*/
1179 0, /*tp_as_sequence*/
1180 0, /*tp_as_mapping*/
1181 0, /*tp_hash */
1182 0, /*tp_call*/
1183 0, /*tp_str*/
1184 0, /*tp_getattro*/
1185 0, /*tp_setattro*/
1186 0, /*tp_as_buffer*/
1187 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1188 "Biquadx objects. Generates a biquadratic filter.", /* tp_doc */
1189 (traverseproc)Biquadx_traverse, /* tp_traverse */
1190 (inquiry)Biquadx_clear, /* tp_clear */
1191 0, /* tp_richcompare */
1192 0, /* tp_weaklistoffset */
1193 0, /* tp_iter */
1194 0, /* tp_iternext */
1195 Biquadx_methods, /* tp_methods */
1196 Biquadx_members, /* tp_members */
1197 0, /* tp_getset */
1198 0, /* tp_base */
1199 0, /* tp_dict */
1200 0, /* tp_descr_get */
1201 0, /* tp_descr_set */
1202 0, /* tp_dictoffset */
1203 0, /* tp_init */
1204 0, /* tp_alloc */
1205 Biquadx_new, /* tp_new */
1206 };
1207
1208 /*** Biquad filter with direct coefficient control ***/
1209 typedef struct
1210 {
1211 pyo_audio_HEAD
1212 PyObject *input;
1213 Stream *input_stream;
1214 // coefficients
1215 Stream *b0_stream;
1216 Stream *b1_stream;
1217 Stream *b2_stream;
1218 Stream *a0_stream;
1219 Stream *a1_stream;
1220 Stream *a2_stream;
1221 int init;
1222 int modebuffer[2]; // need at least 2 slots for mul & add
1223 // sample memories
1224 MYFLT x1;
1225 MYFLT x2;
1226 MYFLT y1;
1227 MYFLT y2;
1228 } Biquada;
1229
1230 static void
1231 Biquada_filters(Biquada *self)
1232 {
1233 MYFLT val;
1234 int i;
1235 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1236 MYFLT *b0 = Stream_getData((Stream *)self->b0_stream);
1237 MYFLT *b1 = Stream_getData((Stream *)self->b1_stream);
1238 MYFLT *b2 = Stream_getData((Stream *)self->b2_stream);
1239 MYFLT *a0 = Stream_getData((Stream *)self->a0_stream);
1240 MYFLT *a1 = Stream_getData((Stream *)self->a1_stream);
1241 MYFLT *a2 = Stream_getData((Stream *)self->a2_stream);
1242
1243 if (self->init == 1)
1244 {
1245 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1246 self->init = 0;
1247 }
1248
1249 for (i = 0; i < self->bufsize; i++)
1250 {
1251 val = ( (b0[i] * in[i]) + (b1[i] * self->x1) + (b2[i] * self->x2) - (a1[i] * self->y1) - (a2[i] * self->y2) ) / a0[i];
1252 self->y2 = self->y1;
1253 self->data[i] = self->y1 = val;
1254 self->x2 = self->x1;
1255 self->x1 = in[i];
1256 }
1257 }
1258
1259 static void Biquada_postprocessing_ii(Biquada *self) { POST_PROCESSING_II };
1260 static void Biquada_postprocessing_ai(Biquada *self) { POST_PROCESSING_AI };
1261 static void Biquada_postprocessing_ia(Biquada *self) { POST_PROCESSING_IA };
1262 static void Biquada_postprocessing_aa(Biquada *self) { POST_PROCESSING_AA };
1263 static void Biquada_postprocessing_ireva(Biquada *self) { POST_PROCESSING_IREVA };
1264 static void Biquada_postprocessing_areva(Biquada *self) { POST_PROCESSING_AREVA };
1265 static void Biquada_postprocessing_revai(Biquada *self) { POST_PROCESSING_REVAI };
1266 static void Biquada_postprocessing_revaa(Biquada *self) { POST_PROCESSING_REVAA };
1267 static void Biquada_postprocessing_revareva(Biquada *self) { POST_PROCESSING_REVAREVA };
1268
1269 static void
1270 Biquada_setProcMode(Biquada *self)
1271 {
1272 int muladdmode;
1273 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1274
1275 self->proc_func_ptr = Biquada_filters;
1276
1277 switch (muladdmode)
1278 {
1279 case 0:
1280 self->muladd_func_ptr = Biquada_postprocessing_ii;
1281 break;
1282
1283 case 1:
1284 self->muladd_func_ptr = Biquada_postprocessing_ai;
1285 break;
1286
1287 case 2:
1288 self->muladd_func_ptr = Biquada_postprocessing_revai;
1289 break;
1290
1291 case 10:
1292 self->muladd_func_ptr = Biquada_postprocessing_ia;
1293 break;
1294
1295 case 11:
1296 self->muladd_func_ptr = Biquada_postprocessing_aa;
1297 break;
1298
1299 case 12:
1300 self->muladd_func_ptr = Biquada_postprocessing_revaa;
1301 break;
1302
1303 case 20:
1304 self->muladd_func_ptr = Biquada_postprocessing_ireva;
1305 break;
1306
1307 case 21:
1308 self->muladd_func_ptr = Biquada_postprocessing_areva;
1309 break;
1310
1311 case 22:
1312 self->muladd_func_ptr = Biquada_postprocessing_revareva;
1313 break;
1314 }
1315 }
1316
1317 static void
1318 Biquada_compute_next_data_frame(Biquada *self)
1319 {
1320 (*self->proc_func_ptr)(self);
1321 (*self->muladd_func_ptr)(self);
1322 }
1323
1324 static int
1325 Biquada_traverse(Biquada *self, visitproc visit, void *arg)
1326 {
1327 pyo_VISIT
1328 Py_VISIT(self->input);
1329 return 0;
1330 }
1331
1332 static int
1333 Biquada_clear(Biquada *self)
1334 {
1335 pyo_CLEAR
1336 Py_CLEAR(self->input);
1337 return 0;
1338 }
1339
1340 static void
1341 Biquada_dealloc(Biquada* self)
1342 {
1343 pyo_DEALLOC
1344 Biquada_clear(self);
1345 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1346 Py_TYPE(self)->tp_free((PyObject*)self);
1347 }
1348
1349 static PyObject *
1350 Biquada_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1351 {
1352 int i;
1353 PyObject *inputtmp, *input_streamtmp, *b0tmp, *b1tmp, *b2tmp, *a0tmp, *a1tmp, *a2tmp, *multmp = NULL, *addtmp = NULL;
1354 Biquada *self;
1355 self = (Biquada *)type->tp_alloc(type, 0);
1356
1357 self->modebuffer[0] = 0;
1358 self->modebuffer[1] = 0;
1359 self->init = 1;
1360
1361 INIT_OBJECT_COMMON
1362
1363 Stream_setFunctionPtr(self->stream, Biquada_compute_next_data_frame);
1364 self->mode_func_ptr = Biquada_setProcMode;
1365
1366 static char *kwlist[] = {"input", "b0", "b1", "b2", "a0", "a1", "a2", "mul", "add", NULL};
1367
1368 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOO", kwlist, &inputtmp, &b0tmp, &b1tmp, &b2tmp, &a0tmp, &a1tmp, &a2tmp, &multmp, &addtmp))
1369 Py_RETURN_NONE;
1370
1371 INIT_INPUT_STREAM
1372
1373 if (b0tmp)
1374 PyObject_CallMethod((PyObject *)self, "setB0", "O", b0tmp);
1375
1376 if (b1tmp)
1377 PyObject_CallMethod((PyObject *)self, "setB1", "O", b1tmp);
1378
1379 if (b2tmp)
1380 PyObject_CallMethod((PyObject *)self, "setB2", "O", b2tmp);
1381
1382 if (a0tmp)
1383 PyObject_CallMethod((PyObject *)self, "setA0", "O", a0tmp);
1384
1385 if (a1tmp)
1386 PyObject_CallMethod((PyObject *)self, "setA1", "O", a1tmp);
1387
1388 if (a2tmp)
1389 PyObject_CallMethod((PyObject *)self, "setA2", "O", a2tmp);
1390
1391 if (multmp)
1392 {
1393 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1394 }
1395
1396 if (addtmp)
1397 {
1398 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1399 }
1400
1401 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1402
1403 (*self->mode_func_ptr)(self);
1404
1405 return (PyObject *)self;
1406 }
1407
1408 static PyObject * Biquada_getServer(Biquada* self) { GET_SERVER };
1409 static PyObject * Biquada_getStream(Biquada* self) { GET_STREAM };
1410 static PyObject * Biquada_setMul(Biquada *self, PyObject *arg) { SET_MUL };
1411 static PyObject * Biquada_setAdd(Biquada *self, PyObject *arg) { SET_ADD };
1412 static PyObject * Biquada_setSub(Biquada *self, PyObject *arg) { SET_SUB };
1413 static PyObject * Biquada_setDiv(Biquada *self, PyObject *arg) { SET_DIV };
1414
1415 static PyObject * Biquada_play(Biquada *self, PyObject *args, PyObject *kwds) { PLAY };
1416 static PyObject * Biquada_out(Biquada *self, PyObject *args, PyObject *kwds) { OUT };
1417 static PyObject * Biquada_stop(Biquada *self, PyObject *args, PyObject *kwds) { STOP };
1418
1419 static PyObject * Biquada_multiply(Biquada *self, PyObject *arg) { MULTIPLY };
1420 static PyObject * Biquada_inplace_multiply(Biquada *self, PyObject *arg) { INPLACE_MULTIPLY };
1421 static PyObject * Biquada_add(Biquada *self, PyObject *arg) { ADD };
1422 static PyObject * Biquada_inplace_add(Biquada *self, PyObject *arg) { INPLACE_ADD };
1423 static PyObject * Biquada_sub(Biquada *self, PyObject *arg) { SUB };
1424 static PyObject * Biquada_inplace_sub(Biquada *self, PyObject *arg) { INPLACE_SUB };
1425 static PyObject * Biquada_div(Biquada *self, PyObject *arg) { DIV };
1426 static PyObject * Biquada_inplace_div(Biquada *self, PyObject *arg) { INPLACE_DIV };
1427
1428 static PyObject *
1429 Biquada_setB0(Biquada *self, PyObject *arg)
1430 {
1431 ASSERT_ARG_NOT_NULL
1432
1433 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1434 self->b0_stream = (Stream *)streamtmp;
1435 Py_INCREF(self->b0_stream);
1436
1437 Py_RETURN_NONE;
1438 }
1439
1440 static PyObject *
1441 Biquada_setB1(Biquada *self, PyObject *arg)
1442 {
1443 ASSERT_ARG_NOT_NULL
1444
1445 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1446 self->b1_stream = (Stream *)streamtmp;
1447 Py_INCREF(self->b1_stream);
1448
1449 Py_RETURN_NONE;
1450 }
1451
1452 static PyObject *
1453 Biquada_setB2(Biquada *self, PyObject *arg)
1454 {
1455 ASSERT_ARG_NOT_NULL
1456
1457 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1458 self->b2_stream = (Stream *)streamtmp;
1459 Py_INCREF(self->b2_stream);
1460
1461 Py_RETURN_NONE;
1462 }
1463
1464 static PyObject *
1465 Biquada_setA0(Biquada *self, PyObject *arg)
1466 {
1467 ASSERT_ARG_NOT_NULL
1468
1469 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1470 self->a0_stream = (Stream *)streamtmp;
1471 Py_INCREF(self->a0_stream);
1472
1473 Py_RETURN_NONE;
1474 }
1475
1476 static PyObject *
1477 Biquada_setA1(Biquada *self, PyObject *arg)
1478 {
1479 ASSERT_ARG_NOT_NULL
1480
1481 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1482 self->a1_stream = (Stream *)streamtmp;
1483 Py_INCREF(self->a1_stream);
1484
1485 Py_RETURN_NONE;
1486 }
1487
1488 static PyObject *
1489 Biquada_setA2(Biquada *self, PyObject *arg)
1490 {
1491 ASSERT_ARG_NOT_NULL
1492
1493 PyObject *streamtmp = PyObject_CallMethod((PyObject *)arg, "_getStream", NULL);
1494 self->a2_stream = (Stream *)streamtmp;
1495 Py_INCREF(self->a2_stream);
1496
1497 Py_RETURN_NONE;
1498 }
1499
1500 static PyMemberDef Biquada_members[] =
1501 {
1502 {"server", T_OBJECT_EX, offsetof(Biquada, server), 0, "Pyo server."},
1503 {"stream", T_OBJECT_EX, offsetof(Biquada, stream), 0, "Stream object."},
1504 {"input", T_OBJECT_EX, offsetof(Biquada, input), 0, "Input sound object."},
1505 {"mul", T_OBJECT_EX, offsetof(Biquada, mul), 0, "Mul factor."},
1506 {"add", T_OBJECT_EX, offsetof(Biquada, add), 0, "Add factor."},
1507 {NULL} /* Sentinel */
1508 };
1509
1510 static PyMethodDef Biquada_methods[] =
1511 {
1512 {"getServer", (PyCFunction)Biquada_getServer, METH_NOARGS, "Returns server object."},
1513 {"_getStream", (PyCFunction)Biquada_getStream, METH_NOARGS, "Returns stream object."},
1514 {"play", (PyCFunction)Biquada_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1515 {"out", (PyCFunction)Biquada_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1516 {"stop", (PyCFunction)Biquada_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1517 {"setB0", (PyCFunction)Biquada_setB0, METH_O, "Sets b0 coefficient."},
1518 {"setB1", (PyCFunction)Biquada_setB1, METH_O, "Sets b1 coefficient."},
1519 {"setB2", (PyCFunction)Biquada_setB2, METH_O, "Sets b2 coefficient."},
1520 {"setA0", (PyCFunction)Biquada_setA0, METH_O, "Sets a0 coefficient."},
1521 {"setA1", (PyCFunction)Biquada_setA1, METH_O, "Sets a1 coefficient."},
1522 {"setA2", (PyCFunction)Biquada_setA2, METH_O, "Sets a2 coefficient."},
1523 {"setMul", (PyCFunction)Biquada_setMul, METH_O, "Sets oscillator mul factor."},
1524 {"setAdd", (PyCFunction)Biquada_setAdd, METH_O, "Sets oscillator add factor."},
1525 {"setSub", (PyCFunction)Biquada_setSub, METH_O, "Sets inverse add factor."},
1526 {"setDiv", (PyCFunction)Biquada_setDiv, METH_O, "Sets inverse mul factor."},
1527 {NULL} /* Sentinel */
1528 };
1529
1530 static PyNumberMethods Biquada_as_number =
1531 {
1532 (binaryfunc)Biquada_add, /*nb_add*/
1533 (binaryfunc)Biquada_sub, /*nb_subtract*/
1534 (binaryfunc)Biquada_multiply, /*nb_multiply*/
1535 0, /*nb_remainder*/
1536 0, /*nb_divmod*/
1537 0, /*nb_power*/
1538 0, /*nb_neg*/
1539 0, /*nb_pos*/
1540 0, /*(unaryfunc)array_abs,*/
1541 0, /*nb_nonzero*/
1542 0, /*nb_invert*/
1543 0, /*nb_lshift*/
1544 0, /*nb_rshift*/
1545 0, /*nb_and*/
1546 0, /*nb_xor*/
1547 0, /*nb_or*/
1548 0, /*nb_int*/
1549 0, /*nb_long*/
1550 0, /*nb_float*/
1551 (binaryfunc)Biquada_inplace_add, /*inplace_add*/
1552 (binaryfunc)Biquada_inplace_sub, /*inplace_subtract*/
1553 (binaryfunc)Biquada_inplace_multiply, /*inplace_multiply*/
1554 0, /*inplace_remainder*/
1555 0, /*inplace_power*/
1556 0, /*inplace_lshift*/
1557 0, /*inplace_rshift*/
1558 0, /*inplace_and*/
1559 0, /*inplace_xor*/
1560 0, /*inplace_or*/
1561 0, /*nb_floor_divide*/
1562 (binaryfunc)Biquada_div, /*nb_true_divide*/
1563 0, /*nb_inplace_floor_divide*/
1564 (binaryfunc)Biquada_inplace_div, /*nb_inplace_true_divide*/
1565 0, /* nb_index */
1566 };
1567
1568 PyTypeObject BiquadaType =
1569 {
1570 PyVarObject_HEAD_INIT(NULL, 0)
1571 "_pyo.Biquada_base", /*tp_name*/
1572 sizeof(Biquada), /*tp_basicsize*/
1573 0, /*tp_itemsize*/
1574 (destructor)Biquada_dealloc, /*tp_dealloc*/
1575 0, /*tp_print*/
1576 0, /*tp_getattr*/
1577 0, /*tp_setattr*/
1578 0, /*tp_as_async (tp_compare in Python 2)*/
1579 0, /*tp_repr*/
1580 &Biquada_as_number, /*tp_as_number*/
1581 0, /*tp_as_sequence*/
1582 0, /*tp_as_mapping*/
1583 0, /*tp_hash */
1584 0, /*tp_call*/
1585 0, /*tp_str*/
1586 0, /*tp_getattro*/
1587 0, /*tp_setattro*/
1588 0, /*tp_as_buffer*/
1589 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1590 "Biquada objects. Generates a biquadratic filter with direct coefficient control.", /* tp_doc */
1591 (traverseproc)Biquada_traverse, /* tp_traverse */
1592 (inquiry)Biquada_clear, /* tp_clear */
1593 0, /* tp_richcompare */
1594 0, /* tp_weaklistoffset */
1595 0, /* tp_iter */
1596 0, /* tp_iternext */
1597 Biquada_methods, /* tp_methods */
1598 Biquada_members, /* tp_members */
1599 0, /* tp_getset */
1600 0, /* tp_base */
1601 0, /* tp_dict */
1602 0, /* tp_descr_get */
1603 0, /* tp_descr_set */
1604 0, /* tp_dictoffset */
1605 0, /* tp_init */
1606 0, /* tp_alloc */
1607 Biquada_new, /* tp_new */
1608 };
1609
1610 /*** Typical EQ filter ***/
1611 typedef struct
1612 {
1613 pyo_audio_HEAD
1614 PyObject *input;
1615 Stream *input_stream;
1616 PyObject *freq;
1617 Stream *freq_stream;
1618 PyObject *q;
1619 Stream *q_stream;
1620 PyObject *boost;
1621 Stream *boost_stream;
•1622 void (*coeffs_func_ptr)(void *);
1623 int init;
1624 int modebuffer[5]; // need at least 2 slots for mul & add
1625 int filtertype;
1626 MYFLT nyquist;
1627 MYFLT twoPiOverSr;
1628 // sample memories
1629 MYFLT x1;
1630 MYFLT x2;
1631 MYFLT y1;
1632 MYFLT y2;
1633 // variables
1634 MYFLT A;
1635 MYFLT c;
1636 MYFLT w0;
1637 MYFLT alpha;
1638 // coefficients
1639 MYFLT b0;
1640 MYFLT b1;
1641 MYFLT b2;
1642 MYFLT a0;
1643 MYFLT a1;
1644 MYFLT a2;
1645 } EQ;
1646
1647 static void
1648 EQ_compute_coeffs_peak(EQ *self)
1649 {
1650 MYFLT alphaMul = self->alpha * self->A;
1651 MYFLT alphaDiv = self->alpha / self->A;
1652
1653 self->b0 = 1.0 + alphaMul;
1654 self->b1 = self->a1 = -2.0 * self->c;
1655 self->b2 = 1.0 - alphaMul;
1656 self->a0 = 1.0 / (1.0 + alphaDiv);
1657 self->a2 = 1.0 - alphaDiv;
1658 }
1659
1660 static void
1661 EQ_compute_coeffs_lowshelf(EQ *self)
1662 {
1663 MYFLT twoSqrtAAlpha = MYSQRT(self->A * 2.0) * self->alpha;
1664 MYFLT AminOneC = (self->A - 1.0) * self->c;
1665 MYFLT AAddOneC = (self->A + 1.0) * self->c;
1666
1667 self->b0 = self->A * ((self->A + 1.0) - AminOneC + twoSqrtAAlpha);
1668 self->b1 = 2.0 * self->A * ((self->A - 1.0) - AAddOneC);
1669 self->b2 = self->A * ((self->A + 1.0) - AminOneC - twoSqrtAAlpha);
1670 self->a0 = 1.0 / ((self->A + 1.0) + AminOneC + twoSqrtAAlpha);
1671 self->a1 = -2.0 * ((self->A - 1.0) + AAddOneC);
1672 self->a2 = (self->A + 1.0) + AminOneC - twoSqrtAAlpha;
1673 }
1674
1675 static void
1676 EQ_compute_coeffs_highshelf(EQ *self)
1677 {
1678 MYFLT twoSqrtAAlpha = MYSQRT(self->A * 2.0) * self->alpha;
1679 MYFLT AminOneC = (self->A - 1.0) * self->c;
1680 MYFLT AAddOneC = (self->A + 1.0) * self->c;
1681
1682 self->b0 = self->A * ((self->A + 1.0) + AminOneC + twoSqrtAAlpha);
1683 self->b1 = -2.0 * self->A * ((self->A - 1.0) + AAddOneC);
1684 self->b2 = self->A * ((self->A + 1.0) + AminOneC - twoSqrtAAlpha);
1685 self->a0 = 1.0 / ((self->A + 1.0) - AminOneC + twoSqrtAAlpha);
1686 self->a1 = 2.0 * ((self->A - 1.0) - AAddOneC);
1687 self->a2 = (self->A + 1.0) - AminOneC - twoSqrtAAlpha;
1688 }
1689
1690 static void
1691 EQ_compute_variables(EQ *self, MYFLT freq, MYFLT q, MYFLT boost)
1692 {
1693 if (freq <= 1)
1694 freq = 1;
1695 else if (freq >= self->nyquist)
1696 freq = self->nyquist;
1697
1698 self->A = MYPOW(10.0, boost / 40.0);
1699 self->w0 = freq * self->twoPiOverSr;
1700 self->c = MYCOS(self->w0);
1701 self->alpha = MYSIN(self->w0) / (2 * q);
1702 (*self->coeffs_func_ptr)(self);
1703 }
1704
1705 static void
1706 EQ_filters_iii(EQ *self)
1707 {
1708 MYFLT val;
1709 int i;
1710 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1711
1712 if (self->init == 1)
1713 {
1714 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1715 self->init = 0;
1716 }
1717
1718 for (i = 0; i < self->bufsize; i++)
1719 {
1720 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1721 self->y2 = self->y1;
1722 self->data[i] = self->y1 = val;
1723 self->x2 = self->x1;
1724 self->x1 = in[i];
1725 }
1726 }
1727
1728 static void
1729 EQ_filters_aii(EQ *self)
1730 {
1731 MYFLT val, q, boost;
1732 int i;
1733 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1734
1735 if (self->init == 1)
1736 {
1737 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1738 self->init = 0;
1739 }
1740
1741 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1742 q = PyFloat_AS_DOUBLE(self->q);
1743 boost = PyFloat_AS_DOUBLE(self->boost);
1744
1745 for (i = 0; i < self->bufsize; i++)
1746 {
1747 EQ_compute_variables(self, fr[i], q, boost);
1748 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1749 self->y2 = self->y1;
1750 self->data[i] = self->y1 = val;
1751 self->x2 = self->x1;
1752 self->x1 = in[i];
1753 }
1754 }
1755
1756 static void
1757 EQ_filters_iai(EQ *self)
1758 {
1759 MYFLT val, fr, boost;
1760 int i;
1761 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1762
1763 if (self->init == 1)
1764 {
1765 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1766 self->init = 0;
1767 }
1768
1769 fr = PyFloat_AS_DOUBLE(self->freq);
1770 MYFLT *q = Stream_getData((Stream *)self->q_stream);
1771 boost = PyFloat_AS_DOUBLE(self->boost);
1772
1773 for (i = 0; i < self->bufsize; i++)
1774 {
1775 EQ_compute_variables(self, fr, q[i], boost);
1776 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1777 self->y2 = self->y1;
1778 self->data[i] = self->y1 = val;
1779 self->x2 = self->x1;
1780 self->x1 = in[i];
1781 }
1782 }
1783
1784 static void
1785 EQ_filters_aai(EQ *self)
1786 {
1787 MYFLT val, boost;
1788 int i;
1789 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1790
1791 if (self->init == 1)
1792 {
1793 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1794 self->init = 0;
1795 }
1796
1797 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1798 MYFLT *q = Stream_getData((Stream *)self->q_stream);
1799 boost = PyFloat_AS_DOUBLE(self->boost);
1800
1801 for (i = 0; i < self->bufsize; i++)
1802 {
1803 EQ_compute_variables(self, fr[i], q[i], boost);
1804 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1805 self->y2 = self->y1;
1806 self->data[i] = self->y1 = val;
1807 self->x2 = self->x1;
1808 self->x1 = in[i];
1809 }
1810 }
1811
1812 static void
1813 EQ_filters_iia(EQ *self)
1814 {
1815 MYFLT val, fr, q;
1816 int i;
1817 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1818
1819 if (self->init == 1)
1820 {
1821 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1822 self->init = 0;
1823 }
1824
1825 fr = PyFloat_AS_DOUBLE(self->freq);
1826 q = PyFloat_AS_DOUBLE(self->q);
1827 MYFLT *boost = Stream_getData((Stream *)self->boost_stream);
1828
1829 for (i = 0; i < self->bufsize; i++)
1830 {
1831 EQ_compute_variables(self, fr, q, boost[i]);
1832 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1833 self->y2 = self->y1;
1834 self->data[i] = self->y1 = val;
1835 self->x2 = self->x1;
1836 self->x1 = in[i];
1837 }
1838 }
1839
1840 static void
1841 EQ_filters_aia(EQ *self)
1842 {
1843 MYFLT val, q;
1844 int i;
1845 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1846
1847 if (self->init == 1)
1848 {
1849 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1850 self->init = 0;
1851 }
1852
1853 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1854 q = PyFloat_AS_DOUBLE(self->q);
1855 MYFLT *boost = Stream_getData((Stream *)self->boost_stream);
1856
1857 for (i = 0; i < self->bufsize; i++)
1858 {
1859 EQ_compute_variables(self, fr[i], q, boost[i]);
1860 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1861 self->y2 = self->y1;
1862 self->data[i] = self->y1 = val;
1863 self->x2 = self->x1;
1864 self->x1 = in[i];
1865 }
1866 }
1867
1868 static void
1869 EQ_filters_iaa(EQ *self)
1870 {
1871 MYFLT val, fr;
1872 int i;
1873 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1874
1875 if (self->init == 1)
1876 {
1877 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1878 self->init = 0;
1879 }
1880
1881 fr = PyFloat_AS_DOUBLE(self->freq);
1882 MYFLT *q = Stream_getData((Stream *)self->q_stream);
1883 MYFLT *boost = Stream_getData((Stream *)self->boost_stream);
1884
1885 for (i = 0; i < self->bufsize; i++)
1886 {
1887 EQ_compute_variables(self, fr, q[i], boost[i]);
1888 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1889 self->y2 = self->y1;
1890 self->data[i] = self->y1 = val;
1891 self->x2 = self->x1;
1892 self->x1 = in[i];
1893 }
1894 }
1895
1896 static void
1897 EQ_filters_aaa(EQ *self)
1898 {
1899 MYFLT val;
1900 int i;
1901 MYFLT *in = Stream_getData((Stream *)self->input_stream);
1902
1903 if (self->init == 1)
1904 {
1905 self->x1 = self->x2 = self->y1 = self->y2 = in[0];
1906 self->init = 0;
1907 }
1908
1909 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
1910 MYFLT *q = Stream_getData((Stream *)self->q_stream);
1911 MYFLT *boost = Stream_getData((Stream *)self->boost_stream);
1912
1913 for (i = 0; i < self->bufsize; i++)
1914 {
1915 EQ_compute_variables(self, fr[i], q[i], boost[i]);
1916 val = ( (self->b0 * in[i]) + (self->b1 * self->x1) + (self->b2 * self->x2) - (self->a1 * self->y1) - (self->a2 * self->y2) ) * self->a0;
1917 self->y2 = self->y1;
1918 self->data[i] = self->y1 = val;
1919 self->x2 = self->x1;
1920 self->x1 = in[i];
1921 }
1922 }
1923
1924 static void EQ_postprocessing_ii(EQ *self) { POST_PROCESSING_II };
1925 static void EQ_postprocessing_ai(EQ *self) { POST_PROCESSING_AI };
1926 static void EQ_postprocessing_ia(EQ *self) { POST_PROCESSING_IA };
1927 static void EQ_postprocessing_aa(EQ *self) { POST_PROCESSING_AA };
1928 static void EQ_postprocessing_ireva(EQ *self) { POST_PROCESSING_IREVA };
1929 static void EQ_postprocessing_areva(EQ *self) { POST_PROCESSING_AREVA };
1930 static void EQ_postprocessing_revai(EQ *self) { POST_PROCESSING_REVAI };
1931 static void EQ_postprocessing_revaa(EQ *self) { POST_PROCESSING_REVAA };
1932 static void EQ_postprocessing_revareva(EQ *self) { POST_PROCESSING_REVAREVA };
1933
1934 static void
1935 EQ_setProcMode(EQ *self)
1936 {
1937 int procmode, muladdmode;
1938 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
1939 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1940
1941 switch (self->filtertype)
1942 {
1943 case 0:
1944 self->coeffs_func_ptr = EQ_compute_coeffs_peak;
1945 break;
1946
1947 case 1:
1948 self->coeffs_func_ptr = EQ_compute_coeffs_lowshelf;
1949 break;
1950
1951 case 2:
1952 self->coeffs_func_ptr = EQ_compute_coeffs_highshelf;
1953 break;
1954 }
1955
1956 switch (procmode)
1957 {
1958 case 0:
1959 EQ_compute_variables(self, PyFloat_AS_DOUBLE(self->freq), PyFloat_AS_DOUBLE(self->q), PyFloat_AS_DOUBLE(self->boost));
1960 self->proc_func_ptr = EQ_filters_iii;
1961 break;
1962
1963 case 1:
1964 self->proc_func_ptr = EQ_filters_aii;
1965 break;
1966
1967 case 10:
1968 self->proc_func_ptr = EQ_filters_iai;
1969 break;
1970
1971 case 11:
1972 self->proc_func_ptr = EQ_filters_aai;
1973 break;
1974
1975 case 100:
1976 self->proc_func_ptr = EQ_filters_iia;
1977 break;
1978
1979 case 101:
1980 self->proc_func_ptr = EQ_filters_aia;
1981 break;
1982
1983 case 110:
1984 self->proc_func_ptr = EQ_filters_iaa;
1985 break;
1986
1987 case 111:
1988 self->proc_func_ptr = EQ_filters_aaa;
1989 break;
1990 }
1991
1992 switch (muladdmode)
1993 {
1994 case 0:
1995 self->muladd_func_ptr = EQ_postprocessing_ii;
1996 break;
1997
1998 case 1:
1999 self->muladd_func_ptr = EQ_postprocessing_ai;
2000 break;
2001
2002 case 2:
2003 self->muladd_func_ptr = EQ_postprocessing_revai;
2004 break;
2005
2006 case 10:
2007 self->muladd_func_ptr = EQ_postprocessing_ia;
2008 break;
2009
2010 case 11:
2011 self->muladd_func_ptr = EQ_postprocessing_aa;
2012 break;
2013
2014 case 12:
2015 self->muladd_func_ptr = EQ_postprocessing_revaa;
2016 break;
2017
2018 case 20:
2019 self->muladd_func_ptr = EQ_postprocessing_ireva;
2020 break;
2021
2022 case 21:
2023 self->muladd_func_ptr = EQ_postprocessing_areva;
2024 break;
2025
2026 case 22:
2027 self->muladd_func_ptr = EQ_postprocessing_revareva;
2028 break;
2029 }
2030 }
2031
2032 static void
2033 EQ_compute_next_data_frame(EQ *self)
2034 {
2035 (*self->proc_func_ptr)(self);
2036 (*self->muladd_func_ptr)(self);
2037 }
2038
2039 static int
2040 EQ_traverse(EQ *self, visitproc visit, void *arg)
2041 {
2042 pyo_VISIT
2043 Py_VISIT(self->input);
2044 Py_VISIT(self->freq);
2045 Py_VISIT(self->q);
2046 Py_VISIT(self->boost);
2047 return 0;
2048 }
2049
2050 static int
2051 EQ_clear(EQ *self)
2052 {
2053 pyo_CLEAR
2054 Py_CLEAR(self->input);
2055 Py_CLEAR(self->freq);
2056 Py_CLEAR(self->q);
2057 Py_CLEAR(self->boost);
2058 return 0;
2059 }
2060
2061 static void
2062 EQ_dealloc(EQ* self)
2063 {
2064 pyo_DEALLOC
2065 EQ_clear(self);
2066 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2067 Py_TYPE(self)->tp_free((PyObject*)self);
2068 }
2069
2070 static PyObject *
2071 EQ_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2072 {
2073 int i;
2074 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *boosttmp = NULL, *multmp = NULL, *addtmp = NULL;
2075 EQ *self;
2076 self = (EQ *)type->tp_alloc(type, 0);
2077
2078 self->freq = PyFloat_FromDouble(1000);
2079 self->q = PyFloat_FromDouble(1);
2080 self->boost = PyFloat_FromDouble(-3.0);
2081 self->filtertype = 0;
2082 self->modebuffer[0] = 0;
2083 self->modebuffer[1] = 0;
2084 self->modebuffer[2] = 0;
2085 self->modebuffer[3] = 0;
2086 self->modebuffer[4] = 0;
2087 self->init = 1;
2088
2089 INIT_OBJECT_COMMON
2090
2091 self->nyquist = (MYFLT)self->sr * 0.49;
2092 self->twoPiOverSr = TWOPI / (MYFLT)self->sr;
2093
2094 Stream_setFunctionPtr(self->stream, EQ_compute_next_data_frame);
2095 self->mode_func_ptr = EQ_setProcMode;
2096
2097 static char *kwlist[] = {"input", "freq", "q", "boost", "type", "mul", "add", NULL};
2098
2099 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOiOO", kwlist, &inputtmp, &freqtmp, &qtmp, &boosttmp, &self->filtertype, &multmp, &addtmp))
2100 Py_RETURN_NONE;
2101
2102 INIT_INPUT_STREAM
2103
2104 if (freqtmp)
2105 {
2106 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
2107 }
2108
2109 if (qtmp)
2110 {
2111 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
2112 }
2113
2114 if (boosttmp)
2115 {
2116 PyObject_CallMethod((PyObject *)self, "setBoost", "O", boosttmp);
2117 }
2118
2119 if (multmp)
2120 {
2121 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2122 }
2123
2124 if (addtmp)
2125 {
2126 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2127 }
2128
2129 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2130
2131 (*self->mode_func_ptr)(self);
2132
2133 return (PyObject *)self;
2134 }
2135
2136 static PyObject * EQ_getServer(EQ* self) { GET_SERVER };
2137 static PyObject * EQ_getStream(EQ* self) { GET_STREAM };
2138 static PyObject * EQ_setMul(EQ *self, PyObject *arg) { SET_MUL };
2139 static PyObject * EQ_setAdd(EQ *self, PyObject *arg) { SET_ADD };
2140 static PyObject * EQ_setSub(EQ *self, PyObject *arg) { SET_SUB };
2141 static PyObject * EQ_setDiv(EQ *self, PyObject *arg) { SET_DIV };
2142
2143 static PyObject * EQ_play(EQ *self, PyObject *args, PyObject *kwds) { PLAY };
2144 static PyObject * EQ_out(EQ *self, PyObject *args, PyObject *kwds) { OUT };
2145 static PyObject * EQ_stop(EQ *self, PyObject *args, PyObject *kwds) { STOP };
2146
2147 static PyObject * EQ_multiply(EQ *self, PyObject *arg) { MULTIPLY };
2148 static PyObject * EQ_inplace_multiply(EQ *self, PyObject *arg) { INPLACE_MULTIPLY };
2149 static PyObject * EQ_add(EQ *self, PyObject *arg) { ADD };
2150 static PyObject * EQ_inplace_add(EQ *self, PyObject *arg) { INPLACE_ADD };
2151 static PyObject * EQ_sub(EQ *self, PyObject *arg) { SUB };
2152 static PyObject * EQ_inplace_sub(EQ *self, PyObject *arg) { INPLACE_SUB };
2153 static PyObject * EQ_div(EQ *self, PyObject *arg) { DIV };
2154 static PyObject * EQ_inplace_div(EQ *self, PyObject *arg) { INPLACE_DIV };
2155
2156 static PyObject * EQ_setFreq(EQ *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
2157 static PyObject * EQ_setQ(EQ *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
2158 static PyObject * EQ_setBoost(EQ *self, PyObject *arg) { SET_PARAM(self->boost, self->boost_stream, 4); }
2159
2160 static PyObject *
2161 EQ_setType(EQ *self, PyObject *arg)
2162 {
2163 ASSERT_ARG_NOT_NULL
2164
2165 if (PyLong_Check(arg))
2166 {
2167 self->filtertype = PyLong_AsLong(arg);
2168 }
2169
2170 (*self->mode_func_ptr)(self);
2171
2172 Py_RETURN_NONE;
2173 }
2174
2175 static PyMemberDef EQ_members[] =
2176 {
2177 {"server", T_OBJECT_EX, offsetof(EQ, server), 0, "Pyo server."},
2178 {"stream", T_OBJECT_EX, offsetof(EQ, stream), 0, "Stream object."},
2179 {"input", T_OBJECT_EX, offsetof(EQ, input), 0, "Input sound object."},
2180 {"freq", T_OBJECT_EX, offsetof(EQ, freq), 0, "Cutoff frequency in cycle per second."},
2181 {"q", T_OBJECT_EX, offsetof(EQ, q), 0, "Q factor."},
2182 {"boost", T_OBJECT_EX, offsetof(EQ, boost), 0, "Boost factor."},
2183 {"mul", T_OBJECT_EX, offsetof(EQ, mul), 0, "Mul factor."},
2184 {"add", T_OBJECT_EX, offsetof(EQ, add), 0, "Add factor."},
2185 {NULL} /* Sentinel */
2186 };
2187
2188 static PyMethodDef EQ_methods[] =
2189 {
2190 {"getServer", (PyCFunction)EQ_getServer, METH_NOARGS, "Returns server object."},
2191 {"_getStream", (PyCFunction)EQ_getStream, METH_NOARGS, "Returns stream object."},
2192 {"play", (PyCFunction)EQ_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2193 {"out", (PyCFunction)EQ_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2194 {"stop", (PyCFunction)EQ_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2195 {"setFreq", (PyCFunction)EQ_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
2196 {"setQ", (PyCFunction)EQ_setQ, METH_O, "Sets filter Q factor."},
2197 {"setBoost", (PyCFunction)EQ_setBoost, METH_O, "Sets filter boost factor."},
2198 {"setType", (PyCFunction)EQ_setType, METH_O, "Sets filter type factor."},
2199 {"setMul", (PyCFunction)EQ_setMul, METH_O, "Sets oscillator mul factor."},
2200 {"setAdd", (PyCFunction)EQ_setAdd, METH_O, "Sets oscillator add factor."},
2201 {"setSub", (PyCFunction)EQ_setSub, METH_O, "Sets inverse add factor."},
2202 {"setDiv", (PyCFunction)EQ_setDiv, METH_O, "Sets inverse mul factor."},
2203 {NULL} /* Sentinel */
2204 };
2205
2206 static PyNumberMethods EQ_as_number =
2207 {
2208 (binaryfunc)EQ_add, /*nb_add*/
2209 (binaryfunc)EQ_sub, /*nb_subtract*/
2210 (binaryfunc)EQ_multiply, /*nb_multiply*/
2211 0, /*nb_remainder*/
2212 0, /*nb_divmod*/
2213 0, /*nb_power*/
2214 0, /*nb_neg*/
2215 0, /*nb_pos*/
2216 0, /*(unaryfunc)array_abs,*/
2217 0, /*nb_nonzero*/
2218 0, /*nb_invert*/
2219 0, /*nb_lshift*/
2220 0, /*nb_rshift*/
2221 0, /*nb_and*/
2222 0, /*nb_xor*/
2223 0, /*nb_or*/
2224 0, /*nb_int*/
2225 0, /*nb_long*/
2226 0, /*nb_float*/
2227 (binaryfunc)EQ_inplace_add, /*inplace_add*/
2228 (binaryfunc)EQ_inplace_sub, /*inplace_subtract*/
2229 (binaryfunc)EQ_inplace_multiply, /*inplace_multiply*/
2230 0, /*inplace_remainder*/
2231 0, /*inplace_power*/
2232 0, /*inplace_lshift*/
2233 0, /*inplace_rshift*/
2234 0, /*inplace_and*/
2235 0, /*inplace_xor*/
2236 0, /*inplace_or*/
2237 0, /*nb_floor_divide*/
2238 (binaryfunc)EQ_div, /*nb_true_divide*/
2239 0, /*nb_inplace_floor_divide*/
2240 (binaryfunc)EQ_inplace_div, /*nb_inplace_true_divide*/
2241 0, /* nb_index */
2242 };
2243
2244 PyTypeObject EQType =
2245 {
2246 PyVarObject_HEAD_INIT(NULL, 0)
2247 "_pyo.EQ_base", /*tp_name*/
2248 sizeof(EQ), /*tp_basicsize*/
2249 0, /*tp_itemsize*/
2250 (destructor)EQ_dealloc, /*tp_dealloc*/
2251 0, /*tp_print*/
2252 0, /*tp_getattr*/
2253 0, /*tp_setattr*/
2254 0, /*tp_as_async (tp_compare in Python 2)*/
2255 0, /*tp_repr*/
2256 &EQ_as_number, /*tp_as_number*/
2257 0, /*tp_as_sequence*/
2258 0, /*tp_as_mapping*/
2259 0, /*tp_hash */
2260 0, /*tp_call*/
2261 0, /*tp_str*/
2262 0, /*tp_getattro*/
2263 0, /*tp_setattro*/
2264 0, /*tp_as_buffer*/
2265 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2266 "EQ objects. Generates a biquadratic filter.", /* tp_doc */
2267 (traverseproc)EQ_traverse, /* tp_traverse */
2268 (inquiry)EQ_clear, /* tp_clear */
2269 0, /* tp_richcompare */
2270 0, /* tp_weaklistoffset */
2271 0, /* tp_iter */
2272 0, /* tp_iternext */
2273 EQ_methods, /* tp_methods */
2274 EQ_members, /* tp_members */
2275 0, /* tp_getset */
2276 0, /* tp_base */
2277 0, /* tp_dict */
2278 0, /* tp_descr_get */
2279 0, /* tp_descr_set */
2280 0, /* tp_dictoffset */
2281 0, /* tp_init */
2282 0, /* tp_alloc */
2283 EQ_new, /* tp_new */
2284 };
2285
2286 /* Performs portamento on audio signal */
2287 typedef struct
2288 {
2289 pyo_audio_HEAD
2290 PyObject *input;
2291 Stream *input_stream;
2292 PyObject *risetime;
2293 PyObject *falltime;
2294 Stream *risetime_stream;
2295 Stream *falltime_stream;
2296 int modebuffer[4]; // need at least 2 slots for mul & add
2297 MYFLT y1; // sample memory
2298 MYFLT x1;
2299 int dir;
2300 } Port;
2301
2302 // inline ?
2303 static void
2304 direction(Port *self, MYFLT val)
2305 {
2306 if (val == self->x1)
2307 return;
2308 else if (val > self->x1)
2309 self->dir = 1;
2310 else
2311 self->dir = 0;
2312
2313 self->x1 = val;
2314 }
2315
2316 static void
2317 Port_filters_ii(Port *self)
2318 {
2319 int i;
2320 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2321 MYFLT risetime = PyFloat_AS_DOUBLE(self->risetime);
2322 MYFLT falltime = PyFloat_AS_DOUBLE(self->falltime);
2323
2324 if (risetime < 0)
2325 risetime = 0.0;
2326
2327 if (falltime < 0)
2328 falltime = 0.0;
2329
2330 MYFLT risefactor = 1.0 / ((risetime + 0.00025) * self->sr);
2331 MYFLT fallfactor = 1.0 / ((falltime + 0.00025) * self->sr);
2332 MYFLT factors[2] = {fallfactor, risefactor};
2333
2334 for (i = 0; i < self->bufsize; i++)
2335 {
2336 direction(self, in[i]);
2337 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) * factors[self->dir];
2338 }
2339 }
2340
2341 static void
2342 Port_filters_ai(Port *self)
2343 {
2344 MYFLT risetime, risefactor;
2345 int i;
2346 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2347 MYFLT *rt = Stream_getData((Stream *)self->risetime_stream);
2348 MYFLT falltime = PyFloat_AS_DOUBLE(self->falltime);
2349
2350 if (falltime < 0)
2351 falltime = 0.0;
2352
2353 MYFLT fallfactor = 1.0 / ((falltime + 0.00025) * self->sr);
2354
2355 for (i = 0; i < self->bufsize; i++)
2356 {
2357 direction(self, in[i]);
2358 risetime = rt[i];
2359
2360 if (risetime < 0)
2361 risetime = 0.0;
2362
2363 risefactor = (risetime + 0.00025) * self->sr;
2364
2365 if (self->dir == 1)
2366 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) / risefactor;
2367 else
2368 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) * fallfactor;
2369 }
2370 }
2371
2372 static void
2373 Port_filters_ia(Port *self)
2374 {
2375 MYFLT falltime, fallfactor;
2376 int i;
2377 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2378 MYFLT *ft = Stream_getData((Stream *)self->falltime_stream);
2379 MYFLT risetime = PyFloat_AS_DOUBLE(self->risetime);
2380
2381 if (risetime < 0)
2382 risetime = 0.0;
2383
2384 MYFLT risefactor = 1.0 / ((risetime + 0.00025) * self->sr);
2385
2386 for (i = 0; i < self->bufsize; i++)
2387 {
2388 direction(self, in[i]);
2389 falltime = ft[i];
2390
2391 if (falltime < 0)
2392 falltime = 0.0;
2393
2394 fallfactor = (falltime + 0.00025) * self->sr;
2395
2396 if (self->dir == 1)
2397 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) * risefactor;
2398 else
2399 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) / fallfactor;
2400 }
2401 }
2402
2403 static void
2404 Port_filters_aa(Port *self)
2405 {
2406 MYFLT risetime, falltime, risefactor, fallfactor;
2407 int i;
2408 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2409 MYFLT *rt = Stream_getData((Stream *)self->risetime_stream);
2410 MYFLT *ft = Stream_getData((Stream *)self->falltime_stream);
2411
2412 for (i = 0; i < self->bufsize; i++)
2413 {
2414 direction(self, in[i]);
2415 risetime = rt[i];
2416
2417 if (risetime < 0)
2418 risetime = 0.0;
2419
2420 falltime = ft[i];
2421
2422 if (falltime < 0)
2423 falltime = 0.0;
2424
2425 risefactor = (risetime + 0.00025) * self->sr;
2426 fallfactor = (falltime + 0.00025) * self->sr;
2427
2428 if (self->dir == 1)
2429 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) / risefactor;
2430 else
2431 self->data[i] = self->y1 = self->y1 + (in[i] - self->y1) / fallfactor;
2432 }
2433 }
2434
2435 static void Port_postprocessing_ii(Port *self) { POST_PROCESSING_II };
2436 static void Port_postprocessing_ai(Port *self) { POST_PROCESSING_AI };
2437 static void Port_postprocessing_ia(Port *self) { POST_PROCESSING_IA };
2438 static void Port_postprocessing_aa(Port *self) { POST_PROCESSING_AA };
2439 static void Port_postprocessing_ireva(Port *self) { POST_PROCESSING_IREVA };
2440 static void Port_postprocessing_areva(Port *self) { POST_PROCESSING_AREVA };
2441 static void Port_postprocessing_revai(Port *self) { POST_PROCESSING_REVAI };
2442 static void Port_postprocessing_revaa(Port *self) { POST_PROCESSING_REVAA };
2443 static void Port_postprocessing_revareva(Port *self) { POST_PROCESSING_REVAREVA };
2444
2445 static void
2446 Port_setProcMode(Port *self)
2447 {
2448 int procmode, muladdmode;
2449 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
2450 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2451
2452 switch (procmode)
2453 {
2454 case 0:
2455 self->proc_func_ptr = Port_filters_ii;
2456 break;
2457
2458 case 1:
2459 self->proc_func_ptr = Port_filters_ai;
2460 break;
2461
2462 case 10:
2463 self->proc_func_ptr = Port_filters_ia;
2464 break;
2465
2466 case 11:
2467 self->proc_func_ptr = Port_filters_aa;
2468 break;
2469 }
2470
2471 switch (muladdmode)
2472 {
2473 case 0:
2474 self->muladd_func_ptr = Port_postprocessing_ii;
2475 break;
2476
2477 case 1:
2478 self->muladd_func_ptr = Port_postprocessing_ai;
2479 break;
2480
2481 case 2:
2482 self->muladd_func_ptr = Port_postprocessing_revai;
2483 break;
2484
2485 case 10:
2486 self->muladd_func_ptr = Port_postprocessing_ia;
2487 break;
2488
2489 case 11:
2490 self->muladd_func_ptr = Port_postprocessing_aa;
2491 break;
2492
2493 case 12:
2494 self->muladd_func_ptr = Port_postprocessing_revaa;
2495 break;
2496
2497 case 20:
2498 self->muladd_func_ptr = Port_postprocessing_ireva;
2499 break;
2500
2501 case 21:
2502 self->muladd_func_ptr = Port_postprocessing_areva;
2503 break;
2504
2505 case 22:
2506 self->muladd_func_ptr = Port_postprocessing_revareva;
2507 break;
2508 }
2509 }
2510
2511 static void
2512 Port_compute_next_data_frame(Port *self)
2513 {
2514 (*self->proc_func_ptr)(self);
2515 (*self->muladd_func_ptr)(self);
2516 }
2517
2518 static int
2519 Port_traverse(Port *self, visitproc visit, void *arg)
2520 {
2521 pyo_VISIT
2522 Py_VISIT(self->input);
2523 Py_VISIT(self->risetime);
2524 Py_VISIT(self->falltime);
2525 return 0;
2526 }
2527
2528 static int
2529 Port_clear(Port *self)
2530 {
2531 pyo_CLEAR
2532 Py_CLEAR(self->input);
2533 Py_CLEAR(self->risetime);
2534 Py_CLEAR(self->falltime);
2535 return 0;
2536 }
2537
2538 static void
2539 Port_dealloc(Port* self)
2540 {
2541 pyo_DEALLOC
2542 Port_clear(self);
2543 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2544 Py_TYPE(self)->tp_free((PyObject*)self);
2545 }
2546
2547 static PyObject *
2548 Port_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2549 {
2550 int i;
2551 MYFLT inittmp = 0.0;
2552 PyObject *inputtmp, *input_streamtmp, *risetimetmp = NULL, *falltimetmp = NULL, *multmp = NULL, *addtmp = NULL;
2553 Port *self;
2554 self = (Port *)type->tp_alloc(type, 0);
2555
2556 self->risetime = PyFloat_FromDouble(0.05);
2557 self->falltime = PyFloat_FromDouble(0.05);
2558 self->modebuffer[0] = 0;
2559 self->modebuffer[1] = 0;
2560 self->modebuffer[2] = 0;
2561 self->modebuffer[3] = 0;
2562 self->y1 = 0.0;
2563 self->x1 = 0.0;
2564 self->dir = 1;
2565
2566 INIT_OBJECT_COMMON
2567 Stream_setFunctionPtr(self->stream, Port_compute_next_data_frame);
2568 self->mode_func_ptr = Port_setProcMode;
2569
2570 static char *kwlist[] = {"input", "risetime", "falltime", "init", "mul", "add", NULL};
2571
2572 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_OOFOO, kwlist, &inputtmp, &risetimetmp, &falltimetmp, &inittmp, &multmp, &addtmp))
2573 Py_RETURN_NONE;
2574
2575 INIT_INPUT_STREAM
2576
2577 if (risetimetmp)
2578 {
2579 PyObject_CallMethod((PyObject *)self, "setRiseTime", "O", risetimetmp);
2580 }
2581
2582 if (falltimetmp)
2583 {
2584 PyObject_CallMethod((PyObject *)self, "setFallTime", "O", falltimetmp);
2585 }
2586
2587 if (multmp)
2588 {
2589 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2590 }
2591
2592 if (addtmp)
2593 {
2594 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2595 }
2596
2597 if (inittmp != 0.0)
2598 self->x1 = self->y1 = inittmp;
2599
2600 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2601
2602 (*self->mode_func_ptr)(self);
2603
2604 return (PyObject *)self;
2605 }
2606
2607 static PyObject * Port_getServer(Port* self) { GET_SERVER };
2608 static PyObject * Port_getStream(Port* self) { GET_STREAM };
2609 static PyObject * Port_setMul(Port *self, PyObject *arg) { SET_MUL };
2610 static PyObject * Port_setAdd(Port *self, PyObject *arg) { SET_ADD };
2611 static PyObject * Port_setSub(Port *self, PyObject *arg) { SET_SUB };
2612 static PyObject * Port_setDiv(Port *self, PyObject *arg) { SET_DIV };
2613
2614 static PyObject * Port_play(Port *self, PyObject *args, PyObject *kwds) { PLAY };
2615 static PyObject * Port_out(Port *self, PyObject *args, PyObject *kwds) { OUT };
2616 static PyObject * Port_stop(Port *self, PyObject *args, PyObject *kwds) { STOP };
2617
2618 static PyObject * Port_multiply(Port *self, PyObject *arg) { MULTIPLY };
2619 static PyObject * Port_inplace_multiply(Port *self, PyObject *arg) { INPLACE_MULTIPLY };
2620 static PyObject * Port_add(Port *self, PyObject *arg) { ADD };
2621 static PyObject * Port_inplace_add(Port *self, PyObject *arg) { INPLACE_ADD };
2622 static PyObject * Port_sub(Port *self, PyObject *arg) { SUB };
2623 static PyObject * Port_inplace_sub(Port *self, PyObject *arg) { INPLACE_SUB };
2624 static PyObject * Port_div(Port *self, PyObject *arg) { DIV };
2625 static PyObject * Port_inplace_div(Port *self, PyObject *arg) { INPLACE_DIV };
2626
2627 static PyObject * Port_setRiseTime(Port *self, PyObject *arg) { SET_PARAM(self->risetime, self->risetime_stream, 2); }
2628 static PyObject * Port_setFallTime(Port *self, PyObject *arg) { SET_PARAM(self->falltime, self->falltime_stream, 3); }
2629
2630 static PyMemberDef Port_members[] =
2631 {
2632 {"server", T_OBJECT_EX, offsetof(Port, server), 0, "Pyo server."},
2633 {"stream", T_OBJECT_EX, offsetof(Port, stream), 0, "Stream object."},
2634 {"input", T_OBJECT_EX, offsetof(Port, input), 0, "Input sound object."},
2635 {"risetime", T_OBJECT_EX, offsetof(Port, risetime), 0, "Rising portamento time in seconds."},
2636 {"falltime", T_OBJECT_EX, offsetof(Port, falltime), 0, "Falling portamento time in seconds."},
2637 {"mul", T_OBJECT_EX, offsetof(Port, mul), 0, "Mul factor."},
2638 {"add", T_OBJECT_EX, offsetof(Port, add), 0, "Add factor."},
2639 {NULL} /* Sentinel */
2640 };
2641
2642 static PyMethodDef Port_methods[] =
2643 {
2644 {"getServer", (PyCFunction)Port_getServer, METH_NOARGS, "Returns server object."},
2645 {"_getStream", (PyCFunction)Port_getStream, METH_NOARGS, "Returns stream object."},
2646 {"play", (PyCFunction)Port_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2647 {"out", (PyCFunction)Port_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2648 {"stop", (PyCFunction)Port_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2649 {"setRiseTime", (PyCFunction)Port_setRiseTime, METH_O, "Sets rising portamento time in seconds."},
2650 {"setFallTime", (PyCFunction)Port_setFallTime, METH_O, "Sets falling portamento time in seconds."},
2651 {"setMul", (PyCFunction)Port_setMul, METH_O, "Sets oscillator mul factor."},
2652 {"setAdd", (PyCFunction)Port_setAdd, METH_O, "Sets oscillator add factor."},
2653 {"setSub", (PyCFunction)Port_setSub, METH_O, "Sets inverse add factor."},
2654 {"setDiv", (PyCFunction)Port_setDiv, METH_O, "Sets inverse mul factor."},
2655 {NULL} /* Sentinel */
2656 };
2657
2658 static PyNumberMethods Port_as_number =
2659 {
2660 (binaryfunc)Port_add, /*nb_add*/
2661 (binaryfunc)Port_sub, /*nb_subtract*/
2662 (binaryfunc)Port_multiply, /*nb_multiply*/
2663 0, /*nb_remainder*/
2664 0, /*nb_divmod*/
2665 0, /*nb_power*/
2666 0, /*nb_neg*/
2667 0, /*nb_pos*/
2668 0, /*(unaryfunc)array_abs,*/
2669 0, /*nb_nonzero*/
2670 0, /*nb_invert*/
2671 0, /*nb_lshift*/
2672 0, /*nb_rshift*/
2673 0, /*nb_and*/
2674 0, /*nb_xor*/
2675 0, /*nb_or*/
2676 0, /*nb_int*/
2677 0, /*nb_long*/
2678 0, /*nb_float*/
2679 (binaryfunc)Port_inplace_add, /*inplace_add*/
2680 (binaryfunc)Port_inplace_sub, /*inplace_subtract*/
2681 (binaryfunc)Port_inplace_multiply, /*inplace_multiply*/
2682 0, /*inplace_remainder*/
2683 0, /*inplace_power*/
2684 0, /*inplace_lshift*/
2685 0, /*inplace_rshift*/
2686 0, /*inplace_and*/
2687 0, /*inplace_xor*/
2688 0, /*inplace_or*/
2689 0, /*nb_floor_divide*/
2690 (binaryfunc)Port_div, /*nb_true_divide*/
2691 0, /*nb_inplace_floor_divide*/
2692 (binaryfunc)Port_inplace_div, /*nb_inplace_true_divide*/
2693 0, /* nb_index */
2694 };
2695
2696 PyTypeObject PortType =
2697 {
2698 PyVarObject_HEAD_INIT(NULL, 0)
2699 "_pyo.Port_base", /*tp_name*/
2700 sizeof(Port), /*tp_basicsize*/
2701 0, /*tp_itemsize*/
2702 (destructor)Port_dealloc, /*tp_dealloc*/
2703 0, /*tp_print*/
2704 0, /*tp_getattr*/
2705 0, /*tp_setattr*/
2706 0, /*tp_as_async (tp_compare in Python 2)*/
2707 0, /*tp_repr*/
2708 &Port_as_number, /*tp_as_number*/
2709 0, /*tp_as_sequence*/
2710 0, /*tp_as_mapping*/
2711 0, /*tp_hash */
2712 0, /*tp_call*/
2713 0, /*tp_str*/
2714 0, /*tp_getattro*/
2715 0, /*tp_setattro*/
2716 0, /*tp_as_buffer*/
2717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2718 "Port objects. Generates a portamento filter.", /* tp_doc */
2719 (traverseproc)Port_traverse, /* tp_traverse */
2720 (inquiry)Port_clear, /* tp_clear */
2721 0, /* tp_richcompare */
2722 0, /* tp_weaklistoffset */
2723 0, /* tp_iter */
2724 0, /* tp_iternext */
2725 Port_methods, /* tp_methods */
2726 Port_members, /* tp_members */
2727 0, /* tp_getset */
2728 0, /* tp_base */
2729 0, /* tp_dict */
2730 0, /* tp_descr_get */
2731 0, /* tp_descr_set */
2732 0, /* tp_dictoffset */
2733 0, /* tp_init */
2734 0, /* tp_alloc */
2735 Port_new, /* tp_new */
2736 };
2737
2738 /************/
2739 /* Tone */
2740 /************/
2741 typedef struct
2742 {
2743 pyo_audio_HEAD
2744 PyObject *input;
2745 Stream *input_stream;
2746 PyObject *freq;
2747 Stream *freq_stream;
2748 int modebuffer[3]; // need at least 2 slots for mul & add
2749 MYFLT lastFreq;
2750 MYFLT nyquist;
2751 MYFLT mTwoPiOverSr;
2752 // sample memories
2753 MYFLT y1;
2754 // variables
2755 MYFLT c;
2756 } Tone;
2757
2758 static void
2759 Tone_filters_i(Tone *self)
2760 {
2761 int i;
2762 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2763 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
2764
2765 if (fr != self->lastFreq)
2766 {
2767 if (fr <= 0.1)
2768 fr = 0.1;
2769 else if (fr >= self->nyquist)
2770 fr = self->nyquist;
2771
2772 self->lastFreq = fr;
2773 self->c = MYEXP(self->mTwoPiOverSr * fr);
2774 }
2775
2776 for (i = 0; i < self->bufsize; i++)
2777 {
2778 self->data[i] = self->y1 = in[i] + (self->y1 - in[i]) * self->c;
2779 }
2780 }
2781
2782 static void
2783 Tone_filters_a(Tone *self)
2784 {
2785 int i;
2786 MYFLT freq;
2787 MYFLT *in = Stream_getData((Stream *)self->input_stream);
2788 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
2789
2790 for (i = 0; i < self->bufsize; i++)
2791 {
2792 freq = fr[i];
2793
2794 if (freq != self->lastFreq)
2795 {
2796 if (freq <= 0.1)
2797 freq = 0.1;
2798 else if (freq >= self->nyquist)
2799 freq = self->nyquist;
2800
2801 self->lastFreq = freq;
2802 self->c = MYEXP(self->mTwoPiOverSr * freq);
2803 }
2804
2805 self->data[i] = self->y1 = in[i] + (self->y1 - in[i]) * self->c;
2806 }
2807 }
2808
2809 static void Tone_postprocessing_ii(Tone *self) { POST_PROCESSING_II };
2810 static void Tone_postprocessing_ai(Tone *self) { POST_PROCESSING_AI };
2811 static void Tone_postprocessing_ia(Tone *self) { POST_PROCESSING_IA };
2812 static void Tone_postprocessing_aa(Tone *self) { POST_PROCESSING_AA };
2813 static void Tone_postprocessing_ireva(Tone *self) { POST_PROCESSING_IREVA };
2814 static void Tone_postprocessing_areva(Tone *self) { POST_PROCESSING_AREVA };
2815 static void Tone_postprocessing_revai(Tone *self) { POST_PROCESSING_REVAI };
2816 static void Tone_postprocessing_revaa(Tone *self) { POST_PROCESSING_REVAA };
2817 static void Tone_postprocessing_revareva(Tone *self) { POST_PROCESSING_REVAREVA };
2818
2819 static void
2820 Tone_setProcMode(Tone *self)
2821 {
2822 int procmode, muladdmode;
2823 procmode = self->modebuffer[2];
2824 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2825
2826 switch (procmode)
2827 {
2828 case 0:
2829 self->proc_func_ptr = Tone_filters_i;
2830 break;
2831
2832 case 1:
2833 self->proc_func_ptr = Tone_filters_a;
2834 break;
2835 }
2836
2837 switch (muladdmode)
2838 {
2839 case 0:
2840 self->muladd_func_ptr = Tone_postprocessing_ii;
2841 break;
2842
2843 case 1:
2844 self->muladd_func_ptr = Tone_postprocessing_ai;
2845 break;
2846
2847 case 2:
2848 self->muladd_func_ptr = Tone_postprocessing_revai;
2849 break;
2850
2851 case 10:
2852 self->muladd_func_ptr = Tone_postprocessing_ia;
2853 break;
2854
2855 case 11:
2856 self->muladd_func_ptr = Tone_postprocessing_aa;
2857 break;
2858
2859 case 12:
2860 self->muladd_func_ptr = Tone_postprocessing_revaa;
2861 break;
2862
2863 case 20:
2864 self->muladd_func_ptr = Tone_postprocessing_ireva;
2865 break;
2866
2867 case 21:
2868 self->muladd_func_ptr = Tone_postprocessing_areva;
2869 break;
2870
2871 case 22:
2872 self->muladd_func_ptr = Tone_postprocessing_revareva;
2873 break;
2874 }
2875 }
2876
2877 static void
2878 Tone_compute_next_data_frame(Tone *self)
2879 {
2880 (*self->proc_func_ptr)(self);
2881 (*self->muladd_func_ptr)(self);
2882 }
2883
2884 static int
2885 Tone_traverse(Tone *self, visitproc visit, void *arg)
2886 {
2887 pyo_VISIT
2888 Py_VISIT(self->input);
2889 Py_VISIT(self->freq);
2890 return 0;
2891 }
2892
2893 static int
2894 Tone_clear(Tone *self)
2895 {
2896 pyo_CLEAR
2897 Py_CLEAR(self->input);
2898 Py_CLEAR(self->freq);
2899 return 0;
2900 }
2901
2902 static void
2903 Tone_dealloc(Tone* self)
2904 {
2905 pyo_DEALLOC
2906 Tone_clear(self);
2907 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2908 Py_TYPE(self)->tp_free((PyObject*)self);
2909 }
2910
2911 static PyObject *
2912 Tone_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2913 {
2914 int i;
2915 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
2916 Tone *self;
2917 self = (Tone *)type->tp_alloc(type, 0);
2918
2919 self->freq = PyFloat_FromDouble(1000);
2920 self->lastFreq = -1.0;
2921 self->y1 = self->c = 0.0;
2922 self->modebuffer[0] = 0;
2923 self->modebuffer[1] = 0;
2924 self->modebuffer[2] = 0;
2925
2926 INIT_OBJECT_COMMON
2927
2928 self->nyquist = (MYFLT)self->sr * 0.49;
2929 self->mTwoPiOverSr = -TWOPI / (MYFLT)self->sr;
2930
2931 Stream_setFunctionPtr(self->stream, Tone_compute_next_data_frame);
2932 self->mode_func_ptr = Tone_setProcMode;
2933
2934 static char *kwlist[] = {"input", "freq", "mul", "add", NULL};
2935
2936 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freqtmp, &multmp, &addtmp))
2937 Py_RETURN_NONE;
2938
2939 INIT_INPUT_STREAM
2940
2941 if (freqtmp)
2942 {
2943 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
2944 }
2945
2946 if (multmp)
2947 {
2948 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2949 }
2950
2951 if (addtmp)
2952 {
2953 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2954 }
2955
2956 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2957
2958 (*self->mode_func_ptr)(self);
2959
2960 return (PyObject *)self;
2961 }
2962
2963 static PyObject * Tone_getServer(Tone* self) { GET_SERVER };
2964 static PyObject * Tone_getStream(Tone* self) { GET_STREAM };
2965 static PyObject * Tone_setMul(Tone *self, PyObject *arg) { SET_MUL };
2966 static PyObject * Tone_setAdd(Tone *self, PyObject *arg) { SET_ADD };
2967 static PyObject * Tone_setSub(Tone *self, PyObject *arg) { SET_SUB };
2968 static PyObject * Tone_setDiv(Tone *self, PyObject *arg) { SET_DIV };
2969
2970 static PyObject * Tone_play(Tone *self, PyObject *args, PyObject *kwds) { PLAY };
2971 static PyObject * Tone_out(Tone *self, PyObject *args, PyObject *kwds) { OUT };
2972 static PyObject * Tone_stop(Tone *self, PyObject *args, PyObject *kwds) { STOP };
2973
2974 static PyObject * Tone_multiply(Tone *self, PyObject *arg) { MULTIPLY };
2975 static PyObject * Tone_inplace_multiply(Tone *self, PyObject *arg) { INPLACE_MULTIPLY };
2976 static PyObject * Tone_add(Tone *self, PyObject *arg) { ADD };
2977 static PyObject * Tone_inplace_add(Tone *self, PyObject *arg) { INPLACE_ADD };
2978 static PyObject * Tone_sub(Tone *self, PyObject *arg) { SUB };
2979 static PyObject * Tone_inplace_sub(Tone *self, PyObject *arg) { INPLACE_SUB };
2980 static PyObject * Tone_div(Tone *self, PyObject *arg) { DIV };
2981 static PyObject * Tone_inplace_div(Tone *self, PyObject *arg) { INPLACE_DIV };
2982
2983 static PyObject * Tone_setFreq(Tone *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
2984
2985 static PyMemberDef Tone_members[] =
2986 {
2987 {"server", T_OBJECT_EX, offsetof(Tone, server), 0, "Pyo server."},
2988 {"stream", T_OBJECT_EX, offsetof(Tone, stream), 0, "Stream object."},
2989 {"input", T_OBJECT_EX, offsetof(Tone, input), 0, "Input sound object."},
2990 {"freq", T_OBJECT_EX, offsetof(Tone, freq), 0, "Cutoff frequency in cycle per second."},
2991 {"mul", T_OBJECT_EX, offsetof(Tone, mul), 0, "Mul factor."},
2992 {"add", T_OBJECT_EX, offsetof(Tone, add), 0, "Add factor."},
2993 {NULL} /* Sentinel */
2994 };
2995
2996 static PyMethodDef Tone_methods[] =
2997 {
2998 {"getServer", (PyCFunction)Tone_getServer, METH_NOARGS, "Returns server object."},
2999 {"_getStream", (PyCFunction)Tone_getStream, METH_NOARGS, "Returns stream object."},
3000 {"play", (PyCFunction)Tone_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3001 {"out", (PyCFunction)Tone_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3002 {"stop", (PyCFunction)Tone_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3003 {"setFreq", (PyCFunction)Tone_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
3004 {"setMul", (PyCFunction)Tone_setMul, METH_O, "Sets oscillator mul factor."},
3005 {"setAdd", (PyCFunction)Tone_setAdd, METH_O, "Sets oscillator add factor."},
3006 {"setSub", (PyCFunction)Tone_setSub, METH_O, "Sets inverse add factor."},
3007 {"setDiv", (PyCFunction)Tone_setDiv, METH_O, "Sets inverse mul factor."},
3008 {NULL} /* Sentinel */
3009 };
3010
3011 static PyNumberMethods Tone_as_number =
3012 {
3013 (binaryfunc)Tone_add, /*nb_add*/
3014 (binaryfunc)Tone_sub, /*nb_subtract*/
3015 (binaryfunc)Tone_multiply, /*nb_multiply*/
3016 0, /*nb_remainder*/
3017 0, /*nb_divmod*/
3018 0, /*nb_power*/
3019 0, /*nb_neg*/
3020 0, /*nb_pos*/
3021 0, /*(unaryfunc)array_abs,*/
3022 0, /*nb_nonzero*/
3023 0, /*nb_invert*/
3024 0, /*nb_lshift*/
3025 0, /*nb_rshift*/
3026 0, /*nb_and*/
3027 0, /*nb_xor*/
3028 0, /*nb_or*/
3029 0, /*nb_int*/
3030 0, /*nb_long*/
3031 0, /*nb_float*/
3032 (binaryfunc)Tone_inplace_add, /*inplace_add*/
3033 (binaryfunc)Tone_inplace_sub, /*inplace_subtract*/
3034 (binaryfunc)Tone_inplace_multiply, /*inplace_multiply*/
3035 0, /*inplace_remainder*/
3036 0, /*inplace_power*/
3037 0, /*inplace_lshift*/
3038 0, /*inplace_rshift*/
3039 0, /*inplace_and*/
3040 0, /*inplace_xor*/
3041 0, /*inplace_or*/
3042 0, /*nb_floor_divide*/
3043 (binaryfunc)Tone_div, /*nb_true_divide*/
3044 0, /*nb_inplace_floor_divide*/
3045 (binaryfunc)Tone_inplace_div, /*nb_inplace_true_divide*/
3046 0, /* nb_index */
3047 };
3048
3049 PyTypeObject ToneType =
3050 {
3051 PyVarObject_HEAD_INIT(NULL, 0)
3052 "_pyo.Tone_base", /*tp_name*/
3053 sizeof(Tone), /*tp_basicsize*/
3054 0, /*tp_itemsize*/
3055 (destructor)Tone_dealloc, /*tp_dealloc*/
3056 0, /*tp_print*/
3057 0, /*tp_getattr*/
3058 0, /*tp_setattr*/
3059 0, /*tp_as_async (tp_compare in Python 2)*/
3060 0, /*tp_repr*/
3061 &Tone_as_number, /*tp_as_number*/
3062 0, /*tp_as_sequence*/
3063 0, /*tp_as_mapping*/
3064 0, /*tp_hash */
3065 0, /*tp_call*/
3066 0, /*tp_str*/
3067 0, /*tp_getattro*/
3068 0, /*tp_setattro*/
3069 0, /*tp_as_buffer*/
3070 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3071 "Tone objects. One-pole recursive lowpass filter.", /* tp_doc */
3072 (traverseproc)Tone_traverse, /* tp_traverse */
3073 (inquiry)Tone_clear, /* tp_clear */
3074 0, /* tp_richcompare */
3075 0, /* tp_weaklistoffset */
3076 0, /* tp_iter */
3077 0, /* tp_iternext */
3078 Tone_methods, /* tp_methods */
3079 Tone_members, /* tp_members */
3080 0, /* tp_getset */
3081 0, /* tp_base */
3082 0, /* tp_dict */
3083 0, /* tp_descr_get */
3084 0, /* tp_descr_set */
3085 0, /* tp_dictoffset */
3086 0, /* tp_init */
3087 0, /* tp_alloc */
3088 Tone_new, /* tp_new */
3089 };
3090
3091 /************/
3092 /* Atone */
3093 /************/
3094 typedef struct
3095 {
3096 pyo_audio_HEAD
3097 PyObject *input;
3098 Stream *input_stream;
3099 PyObject *freq;
3100 Stream *freq_stream;
3101 int modebuffer[3]; // need at least 2 slots for mul & add
3102 MYFLT lastFreq;
3103 MYFLT nyquist;
3104 MYFLT mTwoPiOverSr;
3105 // sample memories
3106 MYFLT y1;
3107 // variables
3108 MYFLT c;
3109 } Atone;
3110
3111 static void
3112 Atone_filters_i(Atone *self)
3113 {
3114 int i;
3115 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3116 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
3117
3118 if (fr != self->lastFreq)
3119 {
3120 if (fr <= 0.1)
3121 fr = 0.1;
3122 else if (fr >= self->nyquist)
3123 fr = self->nyquist;
3124
3125 self->lastFreq = fr;
3126 self->c = MYEXP(self->mTwoPiOverSr * fr);
3127 }
3128
3129 for (i = 0; i < self->bufsize; i++)
3130 {
3131 self->y1 = in[i] + (self->y1 - in[i]) * self->c;
3132 self->data[i] = in[i] - self->y1;
3133 }
3134 }
3135
3136 static void
3137 Atone_filters_a(Atone *self)
3138 {
3139 int i;
3140 MYFLT freq;
3141 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3142 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
3143
3144 for (i = 0; i < self->bufsize; i++)
3145 {
3146 freq = fr[i];
3147
3148 if (freq != self->lastFreq)
3149 {
3150 if (freq <= 0.1)
3151 freq = 0.1;
3152 else if (freq >= self->nyquist)
3153 freq = self->nyquist;
3154
3155 self->lastFreq = freq;
3156 self->c = MYEXP(self->mTwoPiOverSr * freq);
3157 }
3158
3159 self->y1 = in[i] + (self->y1 - in[i]) * self->c;
3160 self->data[i] = in[i] - self->y1;
3161 }
3162 }
3163
3164 static void Atone_postprocessing_ii(Atone *self) { POST_PROCESSING_II };
3165 static void Atone_postprocessing_ai(Atone *self) { POST_PROCESSING_AI };
3166 static void Atone_postprocessing_ia(Atone *self) { POST_PROCESSING_IA };
3167 static void Atone_postprocessing_aa(Atone *self) { POST_PROCESSING_AA };
3168 static void Atone_postprocessing_ireva(Atone *self) { POST_PROCESSING_IREVA };
3169 static void Atone_postprocessing_areva(Atone *self) { POST_PROCESSING_AREVA };
3170 static void Atone_postprocessing_revai(Atone *self) { POST_PROCESSING_REVAI };
3171 static void Atone_postprocessing_revaa(Atone *self) { POST_PROCESSING_REVAA };
3172 static void Atone_postprocessing_revareva(Atone *self) { POST_PROCESSING_REVAREVA };
3173
3174 static void
3175 Atone_setProcMode(Atone *self)
3176 {
3177 int procmode, muladdmode;
3178 procmode = self->modebuffer[2];
3179 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3180
3181 switch (procmode)
3182 {
3183 case 0:
3184 self->proc_func_ptr = Atone_filters_i;
3185 break;
3186
3187 case 1:
3188 self->proc_func_ptr = Atone_filters_a;
3189 break;
3190 }
3191
3192 switch (muladdmode)
3193 {
3194 case 0:
3195 self->muladd_func_ptr = Atone_postprocessing_ii;
3196 break;
3197
3198 case 1:
3199 self->muladd_func_ptr = Atone_postprocessing_ai;
3200 break;
3201
3202 case 2:
3203 self->muladd_func_ptr = Atone_postprocessing_revai;
3204 break;
3205
3206 case 10:
3207 self->muladd_func_ptr = Atone_postprocessing_ia;
3208 break;
3209
3210 case 11:
3211 self->muladd_func_ptr = Atone_postprocessing_aa;
3212 break;
3213
3214 case 12:
3215 self->muladd_func_ptr = Atone_postprocessing_revaa;
3216 break;
3217
3218 case 20:
3219 self->muladd_func_ptr = Atone_postprocessing_ireva;
3220 break;
3221
3222 case 21:
3223 self->muladd_func_ptr = Atone_postprocessing_areva;
3224 break;
3225
3226 case 22:
3227 self->muladd_func_ptr = Atone_postprocessing_revareva;
3228 break;
3229 }
3230 }
3231
3232 static void
3233 Atone_compute_next_data_frame(Atone *self)
3234 {
3235 (*self->proc_func_ptr)(self);
3236 (*self->muladd_func_ptr)(self);
3237 }
3238
3239 static int
3240 Atone_traverse(Atone *self, visitproc visit, void *arg)
3241 {
3242 pyo_VISIT
3243 Py_VISIT(self->input);
3244 Py_VISIT(self->freq);
3245 return 0;
3246 }
3247
3248 static int
3249 Atone_clear(Atone *self)
3250 {
3251 pyo_CLEAR
3252 Py_CLEAR(self->input);
3253 Py_CLEAR(self->freq);
3254 return 0;
3255 }
3256
3257 static void
3258 Atone_dealloc(Atone* self)
3259 {
3260 pyo_DEALLOC
3261 Atone_clear(self);
3262 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3263 Py_TYPE(self)->tp_free((PyObject*)self);
3264 }
3265
3266 static PyObject *
3267 Atone_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3268 {
3269 int i;
3270 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
3271 Atone *self;
3272 self = (Atone *)type->tp_alloc(type, 0);
3273
3274 self->freq = PyFloat_FromDouble(1000);
3275 self->lastFreq = -1.0;
3276 self->y1 = self->c = 0.0;
3277 self->modebuffer[0] = 0;
3278 self->modebuffer[1] = 0;
3279 self->modebuffer[2] = 0;
3280
3281 INIT_OBJECT_COMMON
3282
3283 self->nyquist = (MYFLT)self->sr * 0.49;
3284 self->mTwoPiOverSr = -TWOPI / (MYFLT)self->sr;
3285
3286 Stream_setFunctionPtr(self->stream, Atone_compute_next_data_frame);
3287 self->mode_func_ptr = Atone_setProcMode;
3288
3289 static char *kwlist[] = {"input", "freq", "mul", "add", NULL};
3290
3291 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freqtmp, &multmp, &addtmp))
3292 Py_RETURN_NONE;
3293
3294 INIT_INPUT_STREAM
3295
3296 if (freqtmp)
3297 {
3298 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
3299 }
3300
3301 if (multmp)
3302 {
3303 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3304 }
3305
3306 if (addtmp)
3307 {
3308 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3309 }
3310
3311 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3312
3313 (*self->mode_func_ptr)(self);
3314
3315 return (PyObject *)self;
3316 }
3317
3318 static PyObject * Atone_getServer(Atone* self) { GET_SERVER };
3319 static PyObject * Atone_getStream(Atone* self) { GET_STREAM };
3320 static PyObject * Atone_setMul(Atone *self, PyObject *arg) { SET_MUL };
3321 static PyObject * Atone_setAdd(Atone *self, PyObject *arg) { SET_ADD };
3322 static PyObject * Atone_setSub(Atone *self, PyObject *arg) { SET_SUB };
3323 static PyObject * Atone_setDiv(Atone *self, PyObject *arg) { SET_DIV };
3324
3325 static PyObject * Atone_play(Atone *self, PyObject *args, PyObject *kwds) { PLAY };
3326 static PyObject * Atone_out(Atone *self, PyObject *args, PyObject *kwds) { OUT };
3327 static PyObject * Atone_stop(Atone *self, PyObject *args, PyObject *kwds) { STOP };
3328
3329 static PyObject * Atone_multiply(Atone *self, PyObject *arg) { MULTIPLY };
3330 static PyObject * Atone_inplace_multiply(Atone *self, PyObject *arg) { INPLACE_MULTIPLY };
3331 static PyObject * Atone_add(Atone *self, PyObject *arg) { ADD };
3332 static PyObject * Atone_inplace_add(Atone *self, PyObject *arg) { INPLACE_ADD };
3333 static PyObject * Atone_sub(Atone *self, PyObject *arg) { SUB };
3334 static PyObject * Atone_inplace_sub(Atone *self, PyObject *arg) { INPLACE_SUB };
3335 static PyObject * Atone_div(Atone *self, PyObject *arg) { DIV };
3336 static PyObject * Atone_inplace_div(Atone *self, PyObject *arg) { INPLACE_DIV };
3337
3338 static PyObject * Atone_setFreq(Atone *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
3339
3340 static PyMemberDef Atone_members[] =
3341 {
3342 {"server", T_OBJECT_EX, offsetof(Atone, server), 0, "Pyo server."},
3343 {"stream", T_OBJECT_EX, offsetof(Atone, stream), 0, "Stream object."},
3344 {"input", T_OBJECT_EX, offsetof(Atone, input), 0, "Input sound object."},
3345 {"freq", T_OBJECT_EX, offsetof(Atone, freq), 0, "Cutoff frequency in cycle per second."},
3346 {"mul", T_OBJECT_EX, offsetof(Atone, mul), 0, "Mul factor."},
3347 {"add", T_OBJECT_EX, offsetof(Atone, add), 0, "Add factor."},
3348 {NULL} /* Sentinel */
3349 };
3350
3351 static PyMethodDef Atone_methods[] =
3352 {
3353 {"getServer", (PyCFunction)Atone_getServer, METH_NOARGS, "Returns server object."},
3354 {"_getStream", (PyCFunction)Atone_getStream, METH_NOARGS, "Returns stream object."},
3355 {"play", (PyCFunction)Atone_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3356 {"out", (PyCFunction)Atone_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3357 {"stop", (PyCFunction)Atone_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3358 {"setFreq", (PyCFunction)Atone_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
3359 {"setMul", (PyCFunction)Atone_setMul, METH_O, "Sets oscillator mul factor."},
3360 {"setAdd", (PyCFunction)Atone_setAdd, METH_O, "Sets oscillator add factor."},
3361 {"setSub", (PyCFunction)Atone_setSub, METH_O, "Sets inverse add factor."},
3362 {"setDiv", (PyCFunction)Atone_setDiv, METH_O, "Sets inverse mul factor."},
3363 {NULL} /* Sentinel */
3364 };
3365
3366 static PyNumberMethods Atone_as_number =
3367 {
3368 (binaryfunc)Atone_add, /*nb_add*/
3369 (binaryfunc)Atone_sub, /*nb_subtract*/
3370 (binaryfunc)Atone_multiply, /*nb_multiply*/
3371 0, /*nb_remainder*/
3372 0, /*nb_divmod*/
3373 0, /*nb_power*/
3374 0, /*nb_neg*/
3375 0, /*nb_pos*/
3376 0, /*(unaryfunc)array_abs,*/
3377 0, /*nb_nonzero*/
3378 0, /*nb_invert*/
3379 0, /*nb_lshift*/
3380 0, /*nb_rshift*/
3381 0, /*nb_and*/
3382 0, /*nb_xor*/
3383 0, /*nb_or*/
3384 0, /*nb_int*/
3385 0, /*nb_long*/
3386 0, /*nb_float*/
3387 (binaryfunc)Atone_inplace_add, /*inplace_add*/
3388 (binaryfunc)Atone_inplace_sub, /*inplace_subtract*/
3389 (binaryfunc)Atone_inplace_multiply, /*inplace_multiply*/
3390 0, /*inplace_remainder*/
3391 0, /*inplace_power*/
3392 0, /*inplace_lshift*/
3393 0, /*inplace_rshift*/
3394 0, /*inplace_and*/
3395 0, /*inplace_xor*/
3396 0, /*inplace_or*/
3397 0, /*nb_floor_divide*/
3398 (binaryfunc)Atone_div, /*nb_true_divide*/
3399 0, /*nb_inplace_floor_divide*/
3400 (binaryfunc)Atone_inplace_div, /*nb_inplace_true_divide*/
3401 0, /* nb_index */
3402 };
3403
3404 PyTypeObject AtoneType =
3405 {
3406 PyVarObject_HEAD_INIT(NULL, 0)
3407 "_pyo.Atone_base", /*tp_name*/
3408 sizeof(Atone), /*tp_basicsize*/
3409 0, /*tp_itemsize*/
3410 (destructor)Atone_dealloc, /*tp_dealloc*/
3411 0, /*tp_print*/
3412 0, /*tp_getattr*/
3413 0, /*tp_setattr*/
3414 0, /*tp_as_async (tp_compare in Python 2)*/
3415 0, /*tp_repr*/
3416 &Atone_as_number, /*tp_as_number*/
3417 0, /*tp_as_sequence*/
3418 0, /*tp_as_mapping*/
3419 0, /*tp_hash */
3420 0, /*tp_call*/
3421 0, /*tp_str*/
3422 0, /*tp_getattro*/
3423 0, /*tp_setattro*/
3424 0, /*tp_as_buffer*/
3425 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3426 "Atone objects. One-pole recursive lowpass filter.", /* tp_doc */
3427 (traverseproc)Atone_traverse, /* tp_traverse */
3428 (inquiry)Atone_clear, /* tp_clear */
3429 0, /* tp_richcompare */
3430 0, /* tp_weaklistoffset */
3431 0, /* tp_iter */
3432 0, /* tp_iternext */
3433 Atone_methods, /* tp_methods */
3434 Atone_members, /* tp_members */
3435 0, /* tp_getset */
3436 0, /* tp_base */
3437 0, /* tp_dict */
3438 0, /* tp_descr_get */
3439 0, /* tp_descr_set */
3440 0, /* tp_dictoffset */
3441 0, /* tp_init */
3442 0, /* tp_alloc */
3443 Atone_new, /* tp_new */
3444 };
3445
3446 /************/
3447 /* DCBlock */
3448 /************/
3449 typedef struct
3450 {
3451 pyo_audio_HEAD
3452 PyObject *input;
3453 Stream *input_stream;
3454 int modebuffer[2]; // need at least 2 slots for mul & add
3455 MYFLT x1;
3456 MYFLT y1;
3457 } DCBlock;
3458
3459 static void
3460 DCBlock_filters(DCBlock *self)
3461 {
3462 int i;
3463 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3464
3465 for (i = 0; i < self->bufsize; i++)
3466 {
3467 self->data[i] = self->y1 = in[i] - self->x1 + 0.995 * self->y1;
3468 self->x1 = in[i];
3469 }
3470 }
3471
3472 static void DCBlock_postprocessing_ii(DCBlock *self) { POST_PROCESSING_II };
3473 static void DCBlock_postprocessing_ai(DCBlock *self) { POST_PROCESSING_AI };
3474 static void DCBlock_postprocessing_ia(DCBlock *self) { POST_PROCESSING_IA };
3475 static void DCBlock_postprocessing_aa(DCBlock *self) { POST_PROCESSING_AA };
3476 static void DCBlock_postprocessing_ireva(DCBlock *self) { POST_PROCESSING_IREVA };
3477 static void DCBlock_postprocessing_areva(DCBlock *self) { POST_PROCESSING_AREVA };
3478 static void DCBlock_postprocessing_revai(DCBlock *self) { POST_PROCESSING_REVAI };
3479 static void DCBlock_postprocessing_revaa(DCBlock *self) { POST_PROCESSING_REVAA };
3480 static void DCBlock_postprocessing_revareva(DCBlock *self) { POST_PROCESSING_REVAREVA };
3481
3482 static void
3483 DCBlock_setProcMode(DCBlock *self)
3484 {
3485 int muladdmode;
3486 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3487
3488 self->proc_func_ptr = DCBlock_filters;
3489
3490 switch (muladdmode)
3491 {
3492 case 0:
3493 self->muladd_func_ptr = DCBlock_postprocessing_ii;
3494 break;
3495
3496 case 1:
3497 self->muladd_func_ptr = DCBlock_postprocessing_ai;
3498 break;
3499
3500 case 2:
3501 self->muladd_func_ptr = DCBlock_postprocessing_revai;
3502 break;
3503
3504 case 10:
3505 self->muladd_func_ptr = DCBlock_postprocessing_ia;
3506 break;
3507
3508 case 11:
3509 self->muladd_func_ptr = DCBlock_postprocessing_aa;
3510 break;
3511
3512 case 12:
3513 self->muladd_func_ptr = DCBlock_postprocessing_revaa;
3514 break;
3515
3516 case 20:
3517 self->muladd_func_ptr = DCBlock_postprocessing_ireva;
3518 break;
3519
3520 case 21:
3521 self->muladd_func_ptr = DCBlock_postprocessing_areva;
3522 break;
3523
3524 case 22:
3525 self->muladd_func_ptr = DCBlock_postprocessing_revareva;
3526 break;
3527 }
3528 }
3529
3530 static void
3531 DCBlock_compute_next_data_frame(DCBlock *self)
3532 {
3533 (*self->proc_func_ptr)(self);
3534 (*self->muladd_func_ptr)(self);
3535 }
3536
3537 static int
3538 DCBlock_traverse(DCBlock *self, visitproc visit, void *arg)
3539 {
3540 pyo_VISIT
3541 Py_VISIT(self->input);
3542 return 0;
3543 }
3544
3545 static int
3546 DCBlock_clear(DCBlock *self)
3547 {
3548 pyo_CLEAR
3549 Py_CLEAR(self->input);
3550 return 0;
3551 }
3552
3553 static void
3554 DCBlock_dealloc(DCBlock* self)
3555 {
3556 pyo_DEALLOC
3557 DCBlock_clear(self);
3558 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
3559 Py_TYPE(self)->tp_free((PyObject*)self);
3560 }
3561
3562 static PyObject *
3563 DCBlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3564 {
3565 int i;
3566 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
3567 DCBlock *self;
3568 self = (DCBlock *)type->tp_alloc(type, 0);
3569
3570 self->x1 = self->y1 = 0.0;
3571 self->modebuffer[0] = 0;
3572 self->modebuffer[1] = 0;
3573
3574 INIT_OBJECT_COMMON
3575 Stream_setFunctionPtr(self->stream, DCBlock_compute_next_data_frame);
3576 self->mode_func_ptr = DCBlock_setProcMode;
3577
3578 static char *kwlist[] = {"input", "mul", "add", NULL};
3579
3580 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &inputtmp, &multmp, &addtmp))
3581 Py_RETURN_NONE;
3582
3583 INIT_INPUT_STREAM
3584
3585 if (multmp)
3586 {
3587 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
3588 }
3589
3590 if (addtmp)
3591 {
3592 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
3593 }
3594
3595 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
3596
3597 (*self->mode_func_ptr)(self);
3598
3599 return (PyObject *)self;
3600 }
3601
3602 static PyObject * DCBlock_getServer(DCBlock* self) { GET_SERVER };
3603 static PyObject * DCBlock_getStream(DCBlock* self) { GET_STREAM };
3604 static PyObject * DCBlock_setMul(DCBlock *self, PyObject *arg) { SET_MUL };
3605 static PyObject * DCBlock_setAdd(DCBlock *self, PyObject *arg) { SET_ADD };
3606 static PyObject * DCBlock_setSub(DCBlock *self, PyObject *arg) { SET_SUB };
3607 static PyObject * DCBlock_setDiv(DCBlock *self, PyObject *arg) { SET_DIV };
3608
3609 static PyObject * DCBlock_play(DCBlock *self, PyObject *args, PyObject *kwds) { PLAY };
3610 static PyObject * DCBlock_out(DCBlock *self, PyObject *args, PyObject *kwds) { OUT };
3611 static PyObject * DCBlock_stop(DCBlock *self, PyObject *args, PyObject *kwds) { STOP };
3612
3613 static PyObject * DCBlock_multiply(DCBlock *self, PyObject *arg) { MULTIPLY };
3614 static PyObject * DCBlock_inplace_multiply(DCBlock *self, PyObject *arg) { INPLACE_MULTIPLY };
3615 static PyObject * DCBlock_add(DCBlock *self, PyObject *arg) { ADD };
3616 static PyObject * DCBlock_inplace_add(DCBlock *self, PyObject *arg) { INPLACE_ADD };
3617 static PyObject * DCBlock_sub(DCBlock *self, PyObject *arg) { SUB };
3618 static PyObject * DCBlock_inplace_sub(DCBlock *self, PyObject *arg) { INPLACE_SUB };
3619 static PyObject * DCBlock_div(DCBlock *self, PyObject *arg) { DIV };
3620 static PyObject * DCBlock_inplace_div(DCBlock *self, PyObject *arg) { INPLACE_DIV };
3621
3622 static PyMemberDef DCBlock_members[] =
3623 {
3624 {"server", T_OBJECT_EX, offsetof(DCBlock, server), 0, "Pyo server."},
3625 {"stream", T_OBJECT_EX, offsetof(DCBlock, stream), 0, "Stream object."},
3626 {"input", T_OBJECT_EX, offsetof(DCBlock, input), 0, "Input sound object."},
3627 {"mul", T_OBJECT_EX, offsetof(DCBlock, mul), 0, "Mul factor."},
3628 {"add", T_OBJECT_EX, offsetof(DCBlock, add), 0, "Add factor."},
3629 {NULL} /* Sentinel */
3630 };
3631
3632 static PyMethodDef DCBlock_methods[] =
3633 {
3634 {"getServer", (PyCFunction)DCBlock_getServer, METH_NOARGS, "Returns server object."},
3635 {"_getStream", (PyCFunction)DCBlock_getStream, METH_NOARGS, "Returns stream object."},
3636 {"play", (PyCFunction)DCBlock_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
3637 {"out", (PyCFunction)DCBlock_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
3638 {"stop", (PyCFunction)DCBlock_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
3639 {"setMul", (PyCFunction)DCBlock_setMul, METH_O, "Sets oscillator mul factor."},
3640 {"setAdd", (PyCFunction)DCBlock_setAdd, METH_O, "Sets oscillator add factor."},
3641 {"setSub", (PyCFunction)DCBlock_setSub, METH_O, "Sets inverse add factor."},
3642 {"setDiv", (PyCFunction)DCBlock_setDiv, METH_O, "Sets inverse mul factor."},
3643 {NULL} /* Sentinel */
3644 };
3645
3646 static PyNumberMethods DCBlock_as_number =
3647 {
3648 (binaryfunc)DCBlock_add, /*nb_add*/
3649 (binaryfunc)DCBlock_sub, /*nb_subtract*/
3650 (binaryfunc)DCBlock_multiply, /*nb_multiply*/
3651 0, /*nb_remainder*/
3652 0, /*nb_divmod*/
3653 0, /*nb_power*/
3654 0, /*nb_neg*/
3655 0, /*nb_pos*/
3656 0, /*(unaryfunc)array_abs,*/
3657 0, /*nb_nonzero*/
3658 0, /*nb_invert*/
3659 0, /*nb_lshift*/
3660 0, /*nb_rshift*/
3661 0, /*nb_and*/
3662 0, /*nb_xor*/
3663 0, /*nb_or*/
3664 0, /*nb_int*/
3665 0, /*nb_long*/
3666 0, /*nb_float*/
3667 (binaryfunc)DCBlock_inplace_add, /*inplace_add*/
3668 (binaryfunc)DCBlock_inplace_sub, /*inplace_subtract*/
3669 (binaryfunc)DCBlock_inplace_multiply, /*inplace_multiply*/
3670 0, /*inplace_remainder*/
3671 0, /*inplace_power*/
3672 0, /*inplace_lshift*/
3673 0, /*inplace_rshift*/
3674 0, /*inplace_and*/
3675 0, /*inplace_xor*/
3676 0, /*inplace_or*/
3677 0, /*nb_floor_divide*/
3678 (binaryfunc)DCBlock_div, /*nb_true_divide*/
3679 0, /*nb_inplace_floor_divide*/
3680 (binaryfunc)DCBlock_inplace_div, /*nb_inplace_true_divide*/
3681 0, /* nb_index */
3682 };
3683
3684 PyTypeObject DCBlockType =
3685 {
3686 PyVarObject_HEAD_INIT(NULL, 0)
3687 "_pyo.DCBlock_base", /*tp_name*/
3688 sizeof(DCBlock), /*tp_basicsize*/
3689 0, /*tp_itemsize*/
3690 (destructor)DCBlock_dealloc, /*tp_dealloc*/
3691 0, /*tp_print*/
3692 0, /*tp_getattr*/
3693 0, /*tp_setattr*/
3694 0, /*tp_as_async (tp_compare in Python 2)*/
3695 0, /*tp_repr*/
3696 &DCBlock_as_number, /*tp_as_number*/
3697 0, /*tp_as_sequence*/
3698 0, /*tp_as_mapping*/
3699 0, /*tp_hash */
3700 0, /*tp_call*/
3701 0, /*tp_str*/
3702 0, /*tp_getattro*/
3703 0, /*tp_setattro*/
3704 0, /*tp_as_buffer*/
3705 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3706 "DCBlock objects. Implements the DC blocking filter.", /* tp_doc */
3707 (traverseproc)DCBlock_traverse, /* tp_traverse */
3708 (inquiry)DCBlock_clear, /* tp_clear */
3709 0, /* tp_richcompare */
3710 0, /* tp_weaklistoffset */
3711 0, /* tp_iter */
3712 0, /* tp_iternext */
3713 DCBlock_methods, /* tp_methods */
3714 DCBlock_members, /* tp_members */
3715 0, /* tp_getset */
3716 0, /* tp_base */
3717 0, /* tp_dict */
3718 0, /* tp_descr_get */
3719 0, /* tp_descr_set */
3720 0, /* tp_dictoffset */
3721 0, /* tp_init */
3722 0, /* tp_alloc */
3723 DCBlock_new, /* tp_new */
3724 };
3725
3726 typedef struct
3727 {
3728 pyo_audio_HEAD
3729 PyObject *input;
3730 Stream *input_stream;
3731 PyObject *delay;
3732 Stream *delay_stream;
3733 PyObject *feedback;
3734 Stream *feedback_stream;
3735 MYFLT maxDelay;
3736 long size;
3737 int in_count;
3738 int modebuffer[4];
3739 MYFLT *buffer; // samples memory
3740 } Allpass;
3741
3742 static void
3743 Allpass_process_ii(Allpass *self)
3744 {
3745 MYFLT val, xind, frac, omsqrf;
3746 int i, ind;
3747
3748 MYFLT del = PyFloat_AS_DOUBLE(self->delay);
3749 MYFLT feed = PyFloat_AS_DOUBLE(self->feedback);
3750
3751 if (del < 0.)
3752 del = 0.;
3753 else if (del > self->maxDelay)
3754 del = self->maxDelay;
3755
3756 MYFLT sampdel = del * self->sr;
3757
3758 if (feed < 0)
3759 feed = 0;
3760 else if (feed > 1)
3761 feed = 1;
3762
3763 omsqrf = 1.0 - (feed * feed);
3764
3765 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3766
3767 for (i = 0; i < self->bufsize; i++)
3768 {
3769 xind = self->in_count - sampdel;
3770
3771 if (xind < 0)
3772 xind += self->size;
3773
3774 ind = (int)xind;
3775 frac = xind - ind;
3776 val = self->buffer[ind] + (self->buffer[ind + 1] - self->buffer[ind]) * frac;
3777 self->data[i] = val * omsqrf + in[i] * -feed;
3778
3779 self->buffer[self->in_count] = in[i] + (val * feed);
3780
3781 if (self->in_count == 0)
3782 self->buffer[self->size] = self->buffer[0];
3783
3784 self->in_count++;
3785
3786 if (self->in_count >= self->size)
3787 self->in_count = 0;
3788 }
3789 }
3790
3791 static void
3792 Allpass_process_ai(Allpass *self)
3793 {
3794 MYFLT val, xind, frac, sampdel, del, omsqrf;
3795 int i, ind;
3796
3797 MYFLT *delobj = Stream_getData((Stream *)self->delay_stream);
3798 MYFLT feed = PyFloat_AS_DOUBLE(self->feedback);
3799
3800 if (feed < 0)
3801 feed = 0;
3802 else if (feed > 1)
3803 feed = 1;
3804
3805 omsqrf = 1.0 - (feed * feed);
3806
3807 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3808
3809 for (i = 0; i < self->bufsize; i++)
3810 {
3811 del = delobj[i];
3812
3813 if (del < 0.)
3814 del = 0.;
3815 else if (del > self->maxDelay)
3816 del = self->maxDelay;
3817
3818 sampdel = del * self->sr;
3819 xind = self->in_count - sampdel;
3820
3821 if (xind < 0)
3822 xind += self->size;
3823
3824 ind = (int)xind;
3825 frac = xind - ind;
3826 val = self->buffer[ind] + (self->buffer[ind + 1] - self->buffer[ind]) * frac;
3827 self->data[i] = val * omsqrf + in[i] * -feed;
3828
3829 self->buffer[self->in_count] = in[i] + (val * feed);
3830
3831 if (self->in_count == 0)
3832 self->buffer[self->size] = self->buffer[0];
3833
3834 self->in_count++;
3835
3836 if (self->in_count >= self->size)
3837 self->in_count = 0;
3838 }
3839 }
3840
3841 static void
3842 Allpass_process_ia(Allpass *self)
3843 {
3844 MYFLT val, xind, frac, feed;
3845 int i, ind;
3846
3847 MYFLT del = PyFloat_AS_DOUBLE(self->delay);
3848 MYFLT *fdb = Stream_getData((Stream *)self->feedback_stream);
3849
3850 if (del < 0.)
3851 del = 0.;
3852 else if (del > self->maxDelay)
3853 del = self->maxDelay;
3854
3855 MYFLT sampdel = del * self->sr;
3856
3857 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3858
3859 for (i = 0; i < self->bufsize; i++)
3860 {
3861 feed = fdb[i];
3862
3863 if (feed < 0)
3864 feed = 0;
3865 else if (feed > 1)
3866 feed = 1;
3867
3868 xind = self->in_count - sampdel;
3869
3870 if (xind < 0)
3871 xind += self->size;
3872
3873 ind = (int)xind;
3874 frac = xind - ind;
3875 val = self->buffer[ind] + (self->buffer[ind + 1] - self->buffer[ind]) * frac;
3876 self->data[i] = val * (1.0 - (feed * feed)) + in[i] * -feed;
3877
3878 self->buffer[self->in_count] = in[i] + (val * feed);
3879
3880 if (self->in_count == 0)
3881 self->buffer[self->size] = self->buffer[0];
3882
3883 self->in_count++;
3884
3885 if (self->in_count == self->size)
3886 self->in_count = 0;
3887 }
3888 }
3889
3890 static void
3891 Allpass_process_aa(Allpass *self)
3892 {
3893 MYFLT val, xind, frac, sampdel, feed, del;
3894 int i, ind;
3895
3896 MYFLT *delobj = Stream_getData((Stream *)self->delay_stream);
3897 MYFLT *fdb = Stream_getData((Stream *)self->feedback_stream);
3898
3899 MYFLT *in = Stream_getData((Stream *)self->input_stream);
3900
3901 for (i = 0; i < self->bufsize; i++)
3902 {
3903 del = delobj[i];
3904 feed = fdb[i];
3905
3906 if (feed < 0)
3907 feed = 0;
3908 else if (feed > 1)
3909 feed = 1;
3910
3911 if (del < 0.)
3912 del = 0.;
3913 else if (del > self->maxDelay)
3914 del = self->maxDelay;
3915
3916 sampdel = del * self->sr;
3917 xind = self->in_count - sampdel;
3918
3919 if (xind < 0)
3920 xind += self->size;
3921
3922 ind = (int)xind;
3923 frac = xind - ind;
3924 val = self->buffer[ind] + (self->buffer[ind + 1] - self->buffer[ind]) * frac;
3925 self->data[i] = val * (1.0 - (feed * feed)) + in[i] * -feed;
3926
3927 self->buffer[self->in_count] = in[i] + (val * feed);
3928
3929 if (self->in_count == 0)
3930 self->buffer[self->size] = self->buffer[0];
3931
3932 self->in_count++;
3933
3934 if (self->in_count == self->size)
3935 self->in_count = 0;
3936 }
3937 }
3938
3939 static void Allpass_postprocessing_ii(Allpass *self) { POST_PROCESSING_II };
3940 static void Allpass_postprocessing_ai(Allpass *self) { POST_PROCESSING_AI };
3941 static void Allpass_postprocessing_ia(Allpass *self) { POST_PROCESSING_IA };
3942 static void Allpass_postprocessing_aa(Allpass *self) { POST_PROCESSING_AA };
3943 static void Allpass_postprocessing_ireva(Allpass *self) { POST_PROCESSING_IREVA };
3944 static void Allpass_postprocessing_areva(Allpass *self) { POST_PROCESSING_AREVA };
3945 static void Allpass_postprocessing_revai(Allpass *self) { POST_PROCESSING_REVAI };
3946 static void Allpass_postprocessing_revaa(Allpass *self) { POST_PROCESSING_REVAA };
3947 static void Allpass_postprocessing_revareva(Allpass *self) { POST_PROCESSING_REVAREVA };
3948
3949 static void
3950 Allpass_setProcMode(Allpass *self)
3951 {
3952 int procmode, muladdmode;
3953 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
3954 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
3955
3956 switch (procmode)
3957 {
3958 case 0:
3959 self->proc_func_ptr = Allpass_process_ii;
3960 break;
3961
3962 case 1:
3963 self->proc_func_ptr = Allpass_process_ai;
3964 break;
3965
3966 case 10:
3967 self->proc_func_ptr = Allpass_process_ia;
3968 break;
3969
3970 case 11:
3971 self->proc_func_ptr = Allpass_process_aa;
3972 break;
3973 }
3974
3975 switch (muladdmode)
3976 {
3977 case 0:
3978 self->muladd_func_ptr = Allpass_postprocessing_ii;
3979 break;
3980
3981 case 1:
3982 self->muladd_func_ptr = Allpass_postprocessing_ai;
3983 break;
3984
3985 case 2:
3986 self->muladd_func_ptr = Allpass_postprocessing_revai;
3987 break;
3988
3989 case 10:
3990 self->muladd_func_ptr = Allpass_postprocessing_ia;
3991 break;
3992
3993 case 11:
3994 self->muladd_func_ptr = Allpass_postprocessing_aa;
3995 break;
3996
3997 case 12:
3998 self->muladd_func_ptr = Allpass_postprocessing_revaa;
3999 break;
4000
4001 case 20:
4002 self->muladd_func_ptr = Allpass_postprocessing_ireva;
4003 break;
4004
4005 case 21:
4006 self->muladd_func_ptr = Allpass_postprocessing_areva;
4007 break;
4008
4009 case 22:
4010 self->muladd_func_ptr = Allpass_postprocessing_revareva;
4011 break;
4012 }
4013 }
4014
4015 static void
4016 Allpass_compute_next_data_frame(Allpass *self)
4017 {
4018 (*self->proc_func_ptr)(self);
4019 (*self->muladd_func_ptr)(self);
4020 }
4021
4022 static int
4023 Allpass_traverse(Allpass *self, visitproc visit, void *arg)
4024 {
4025 pyo_VISIT
4026 Py_VISIT(self->input);
4027 Py_VISIT(self->delay);
4028 Py_VISIT(self->feedback);
4029 return 0;
4030 }
4031
4032 static int
4033 Allpass_clear(Allpass *self)
4034 {
4035 pyo_CLEAR
4036 Py_CLEAR(self->input);
4037 Py_CLEAR(self->delay);
4038 Py_CLEAR(self->feedback);
4039 return 0;
4040 }
4041
4042 static void
4043 Allpass_dealloc(Allpass* self)
4044 {
4045 pyo_DEALLOC
4046 PyMem_RawFree(self->buffer);
4047 Allpass_clear(self);
4048 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4049 Py_TYPE(self)->tp_free((PyObject*)self);
4050 }
4051
4052 static PyObject *
4053 Allpass_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4054 {
4055 int i;
4056 PyObject *inputtmp, *input_streamtmp, *delaytmp = NULL, *feedbacktmp = NULL, *multmp = NULL, *addtmp = NULL;
4057 Allpass *self;
4058 self = (Allpass *)type->tp_alloc(type, 0);
4059
4060 self->delay = PyFloat_FromDouble(0);
4061 self->feedback = PyFloat_FromDouble(0);
4062 self->maxDelay = 1;
4063 self->in_count = 0;
4064 self->modebuffer[0] = 0;
4065 self->modebuffer[1] = 0;
4066 self->modebuffer[2] = 0;
4067 self->modebuffer[3] = 0;
4068
4069 INIT_OBJECT_COMMON
4070 Stream_setFunctionPtr(self->stream, Allpass_compute_next_data_frame);
4071 self->mode_func_ptr = Allpass_setProcMode;
4072
4073 static char *kwlist[] = {"input", "delay", "feedback", "maxDelay", "mul", "add", NULL};
4074
4075 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_OOFOO, kwlist, &inputtmp, &delaytmp, &feedbacktmp, &self->maxDelay, &multmp, &addtmp))
4076 Py_RETURN_NONE;
4077
4078 INIT_INPUT_STREAM
4079
4080 if (delaytmp)
4081 {
4082 PyObject_CallMethod((PyObject *)self, "setDelay", "O", delaytmp);
4083 }
4084
4085 if (feedbacktmp)
4086 {
4087 PyObject_CallMethod((PyObject *)self, "setFeedback", "O", feedbacktmp);
4088 }
4089
4090 if (multmp)
4091 {
4092 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4093 }
4094
4095 if (addtmp)
4096 {
4097 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4098 }
4099
4100 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4101
4102 self->size = self->maxDelay * self->sr + 0.5;
4103
4104 self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, (self->size + 1) * sizeof(MYFLT));
4105
4106 for (i = 0; i < (self->size + 1); i++)
4107 {
4108 self->buffer[i] = 0.;
4109 }
4110
4111 (*self->mode_func_ptr)(self);
4112
4113 return (PyObject *)self;
4114 }
4115
4116 static PyObject * Allpass_getServer(Allpass* self) { GET_SERVER };
4117 static PyObject * Allpass_getStream(Allpass* self) { GET_STREAM };
4118 static PyObject * Allpass_setMul(Allpass *self, PyObject *arg) { SET_MUL };
4119 static PyObject * Allpass_setAdd(Allpass *self, PyObject *arg) { SET_ADD };
4120 static PyObject * Allpass_setSub(Allpass *self, PyObject *arg) { SET_SUB };
4121 static PyObject * Allpass_setDiv(Allpass *self, PyObject *arg) { SET_DIV };
4122
4123 static PyObject * Allpass_play(Allpass *self, PyObject *args, PyObject *kwds) { PLAY };
4124 static PyObject * Allpass_out(Allpass *self, PyObject *args, PyObject *kwds) { OUT };
4125 static PyObject * Allpass_stop(Allpass *self, PyObject *args, PyObject *kwds) { STOP };
4126
4127 static PyObject * Allpass_multiply(Allpass *self, PyObject *arg) { MULTIPLY };
4128 static PyObject * Allpass_inplace_multiply(Allpass *self, PyObject *arg) { INPLACE_MULTIPLY };
4129 static PyObject * Allpass_add(Allpass *self, PyObject *arg) { ADD };
4130 static PyObject * Allpass_inplace_add(Allpass *self, PyObject *arg) { INPLACE_ADD };
4131 static PyObject * Allpass_sub(Allpass *self, PyObject *arg) { SUB };
4132 static PyObject * Allpass_inplace_sub(Allpass *self, PyObject *arg) { INPLACE_SUB };
4133 static PyObject * Allpass_div(Allpass *self, PyObject *arg) { DIV };
4134 static PyObject * Allpass_inplace_div(Allpass *self, PyObject *arg) { INPLACE_DIV };
4135
4136 static PyObject * Allpass_setDelay(Allpass *self, PyObject *arg) { SET_PARAM(self->delay, self->delay_stream, 2); }
4137 static PyObject * Allpass_setFeedback(Allpass *self, PyObject *arg) { SET_PARAM(self->feedback, self->feedback_stream, 3); }
4138
4139 static PyMemberDef Allpass_members[] =
4140 {
4141 {"server", T_OBJECT_EX, offsetof(Allpass, server), 0, "Pyo server."},
4142 {"stream", T_OBJECT_EX, offsetof(Allpass, stream), 0, "Stream object."},
4143 {"input", T_OBJECT_EX, offsetof(Allpass, input), 0, "Input sound object."},
4144 {"delay", T_OBJECT_EX, offsetof(Allpass, delay), 0, "delay time in seconds."},
4145 {"feedback", T_OBJECT_EX, offsetof(Allpass, feedback), 0, "Feedback value."},
4146 {"mul", T_OBJECT_EX, offsetof(Allpass, mul), 0, "Mul factor."},
4147 {"add", T_OBJECT_EX, offsetof(Allpass, add), 0, "Add factor."},
4148 {NULL} /* Sentinel */
4149 };
4150
4151 static PyMethodDef Allpass_methods[] =
4152 {
4153 {"getServer", (PyCFunction)Allpass_getServer, METH_NOARGS, "Returns server object."},
4154 {"_getStream", (PyCFunction)Allpass_getStream, METH_NOARGS, "Returns stream object."},
4155 {"play", (PyCFunction)Allpass_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4156 {"out", (PyCFunction)Allpass_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
4157 {"stop", (PyCFunction)Allpass_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4158 {"setDelay", (PyCFunction)Allpass_setDelay, METH_O, "Sets delay time in seconds."},
4159 {"setFeedback", (PyCFunction)Allpass_setFeedback, METH_O, "Sets feedback value between 0 -> 1."},
4160 {"setMul", (PyCFunction)Allpass_setMul, METH_O, "Sets oscillator mul factor."},
4161 {"setAdd", (PyCFunction)Allpass_setAdd, METH_O, "Sets oscillator add factor."},
4162 {"setSub", (PyCFunction)Allpass_setSub, METH_O, "Sets inverse add factor."},
4163 {"setDiv", (PyCFunction)Allpass_setDiv, METH_O, "Sets inverse mul factor."},
4164 {NULL} /* Sentinel */
4165 };
4166
4167 static PyNumberMethods Allpass_as_number =
4168 {
4169 (binaryfunc)Allpass_add, /*nb_add*/
4170 (binaryfunc)Allpass_sub, /*nb_subtract*/
4171 (binaryfunc)Allpass_multiply, /*nb_multiply*/
4172 0, /*nb_remainder*/
4173 0, /*nb_divmod*/
4174 0, /*nb_power*/
4175 0, /*nb_neg*/
4176 0, /*nb_pos*/
4177 0, /*(unaryfunc)array_abs,*/
4178 0, /*nb_nonzero*/
4179 0, /*nb_invert*/
4180 0, /*nb_lshift*/
4181 0, /*nb_rshift*/
4182 0, /*nb_and*/
4183 0, /*nb_xor*/
4184 0, /*nb_or*/
4185 0, /*nb_int*/
4186 0, /*nb_long*/
4187 0, /*nb_float*/
4188 (binaryfunc)Allpass_inplace_add, /*inplace_add*/
4189 (binaryfunc)Allpass_inplace_sub, /*inplace_subtract*/
4190 (binaryfunc)Allpass_inplace_multiply, /*inplace_multiply*/
4191 0, /*inplace_remainder*/
4192 0, /*inplace_power*/
4193 0, /*inplace_lshift*/
4194 0, /*inplace_rshift*/
4195 0, /*inplace_and*/
4196 0, /*inplace_xor*/
4197 0, /*inplace_or*/
4198 0, /*nb_floor_divide*/
4199 (binaryfunc)Allpass_div, /*nb_true_divide*/
4200 0, /*nb_inplace_floor_divide*/
4201 (binaryfunc)Allpass_inplace_div, /*nb_inplace_true_divide*/
4202 0, /* nb_index */
4203 };
4204
4205 PyTypeObject AllpassType =
4206 {
4207 PyVarObject_HEAD_INIT(NULL, 0)
4208 "_pyo.Allpass_base", /*tp_name*/
4209 sizeof(Allpass), /*tp_basicsize*/
4210 0, /*tp_itemsize*/
4211 (destructor)Allpass_dealloc, /*tp_dealloc*/
4212 0, /*tp_print*/
4213 0, /*tp_getattr*/
4214 0, /*tp_setattr*/
4215 0, /*tp_as_async (tp_compare in Python 2)*/
4216 0, /*tp_repr*/
4217 &Allpass_as_number, /*tp_as_number*/
4218 0, /*tp_as_sequence*/
4219 0, /*tp_as_mapping*/
4220 0, /*tp_hash */
4221 0, /*tp_call*/
4222 0, /*tp_str*/
4223 0, /*tp_getattro*/
4224 0, /*tp_setattro*/
4225 0, /*tp_as_buffer*/
4226 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4227 "Allpass objects. Allpass signal by x samples.", /* tp_doc */
4228 (traverseproc)Allpass_traverse, /* tp_traverse */
4229 (inquiry)Allpass_clear, /* tp_clear */
4230 0, /* tp_richcompare */
4231 0, /* tp_weaklistoffset */
4232 0, /* tp_iter */
4233 0, /* tp_iternext */
4234 Allpass_methods, /* tp_methods */
4235 Allpass_members, /* tp_members */
4236 0, /* tp_getset */
4237 0, /* tp_base */
4238 0, /* tp_dict */
4239 0, /* tp_descr_get */
4240 0, /* tp_descr_set */
4241 0, /* tp_dictoffset */
4242 0, /* tp_init */
4243 0, /* tp_alloc */
4244 Allpass_new, /* tp_new */
4245 };
4246
4247 typedef struct
4248 {
4249 pyo_audio_HEAD
4250 PyObject *input;
4251 Stream *input_stream;
4252 PyObject *freq;
4253 Stream *freq_stream;
4254 PyObject *bw;
4255 Stream *bw_stream;
4256 int init;
4257 int modebuffer[4]; // need at least 2 slots for mul & add
4258 MYFLT minusPiOverSr;
4259 MYFLT twoPiOverSr;
4260 MYFLT nyquist;
4261 // sample memories
4262 MYFLT y1;
4263 MYFLT y2;
4264 // coefficients
4265 MYFLT alpha;
4266 MYFLT beta;
4267 } Allpass2;
4268
4269 static void
4270 Allpass2_compute_variables(Allpass2 *self, MYFLT freq, MYFLT bw)
4271 {
4272 MYFLT radius, angle;
4273
4274 if (freq <= 1)
4275 freq = 1;
4276 else if (freq >= self->nyquist)
4277 freq = self->nyquist;
4278
4279 radius = MYEXP(bw * self->minusPiOverSr);
4280 angle = freq * self->twoPiOverSr;
4281
4282 self->alpha = radius * radius;
4283 self->beta = -2.0 * radius * MYCOS(angle);
4284 }
4285
4286 static void
4287 Allpass2_filters_ii(Allpass2 *self)
4288 {
4289 MYFLT val;
4290 int i;
4291 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4292
4293 if (self->init == 1)
4294 {
4295 self->y1 = self->y2 = in[0];
4296 self->init = 0;
4297 }
4298
4299 for (i = 0; i < self->bufsize; i++)
4300 {
4301 val = in[i] + (self->y1 * -self->beta) + (self->y2 * -self->alpha);
4302 self->data[i] = (val * self->alpha) + (self->y1 * self->beta) + self->y2;
4303 self->y2 = self->y1;
4304 self->y1 = val;
4305 }
4306 }
4307
4308 static void
4309 Allpass2_filters_ai(Allpass2 *self)
4310 {
4311 MYFLT val, bw;
4312 int i;
4313 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4314
4315 if (self->init == 1)
4316 {
4317 self->y1 = self->y2 = in[0];
4318 self->init = 0;
4319 }
4320
4321 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
4322 bw = PyFloat_AS_DOUBLE(self->bw);
4323
4324 for (i = 0; i < self->bufsize; i++)
4325 {
4326 Allpass2_compute_variables(self, fr[i], bw);
4327 val = in[i] + (self->y1 * -self->beta) + (self->y2 * -self->alpha);
4328 self->data[i] = (val * self->alpha) + (self->y1 * self->beta) + self->y2;
4329 self->y2 = self->y1;
4330 self->y1 = val;
4331 }
4332 }
4333
4334 static void
4335 Allpass2_filters_ia(Allpass2 *self)
4336 {
4337 MYFLT val, fr;
4338 int i;
4339 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4340
4341 if (self->init == 1)
4342 {
4343 self->y1 = self->y2 = in[0];
4344 self->init = 0;
4345 }
4346
4347 fr = PyFloat_AS_DOUBLE(self->freq);
4348 MYFLT *bw = Stream_getData((Stream *)self->bw_stream);
4349
4350 for (i = 0; i < self->bufsize; i++)
4351 {
4352 Allpass2_compute_variables(self, fr, bw[i]);
4353 val = in[i] + (self->y1 * -self->beta) + (self->y2 * -self->alpha);
4354 self->data[i] = (val * self->alpha) + (self->y1 * self->beta) + self->y2;
4355 self->y2 = self->y1;
4356 self->y1 = val;
4357 }
4358 }
4359
4360 static void
4361 Allpass2_filters_aa(Allpass2 *self)
4362 {
4363 MYFLT val;
4364 int i;
4365 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4366
4367 if (self->init == 1)
4368 {
4369 self->y1 = self->y2 = in[0];
4370 self->init = 0;
4371 }
4372
4373 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
4374 MYFLT *bw = Stream_getData((Stream *)self->bw_stream);
4375
4376 for (i = 0; i < self->bufsize; i++)
4377 {
4378 Allpass2_compute_variables(self, fr[i], bw[i]);
4379 val = in[i] + (self->y1 * -self->beta) + (self->y2 * -self->alpha);
4380 self->data[i] = (val * self->alpha) + (self->y1 * self->beta) + self->y2;
4381 self->y2 = self->y1;
4382 self->y1 = val;
4383 }
4384 }
4385
4386 static void Allpass2_postprocessing_ii(Allpass2 *self) { POST_PROCESSING_II };
4387 static void Allpass2_postprocessing_ai(Allpass2 *self) { POST_PROCESSING_AI };
4388 static void Allpass2_postprocessing_ia(Allpass2 *self) { POST_PROCESSING_IA };
4389 static void Allpass2_postprocessing_aa(Allpass2 *self) { POST_PROCESSING_AA };
4390 static void Allpass2_postprocessing_ireva(Allpass2 *self) { POST_PROCESSING_IREVA };
4391 static void Allpass2_postprocessing_areva(Allpass2 *self) { POST_PROCESSING_AREVA };
4392 static void Allpass2_postprocessing_revai(Allpass2 *self) { POST_PROCESSING_REVAI };
4393 static void Allpass2_postprocessing_revaa(Allpass2 *self) { POST_PROCESSING_REVAA };
4394 static void Allpass2_postprocessing_revareva(Allpass2 *self) { POST_PROCESSING_REVAREVA };
4395
4396 static void
4397 Allpass2_setProcMode(Allpass2 *self)
4398 {
4399 int procmode, muladdmode;
4400 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
4401 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
4402
4403 switch (procmode)
4404 {
4405 case 0:
4406 Allpass2_compute_variables(self, PyFloat_AS_DOUBLE(self->freq), PyFloat_AS_DOUBLE(self->bw));
4407 self->proc_func_ptr = Allpass2_filters_ii;
4408 break;
4409
4410 case 1:
4411 self->proc_func_ptr = Allpass2_filters_ai;
4412 break;
4413
4414 case 10:
4415 self->proc_func_ptr = Allpass2_filters_ia;
4416 break;
4417
4418 case 11:
4419 self->proc_func_ptr = Allpass2_filters_aa;
4420 break;
4421 }
4422
4423 switch (muladdmode)
4424 {
4425 case 0:
4426 self->muladd_func_ptr = Allpass2_postprocessing_ii;
4427 break;
4428
4429 case 1:
4430 self->muladd_func_ptr = Allpass2_postprocessing_ai;
4431 break;
4432
4433 case 2:
4434 self->muladd_func_ptr = Allpass2_postprocessing_revai;
4435 break;
4436
4437 case 10:
4438 self->muladd_func_ptr = Allpass2_postprocessing_ia;
4439 break;
4440
4441 case 11:
4442 self->muladd_func_ptr = Allpass2_postprocessing_aa;
4443 break;
4444
4445 case 12:
4446 self->muladd_func_ptr = Allpass2_postprocessing_revaa;
4447 break;
4448
4449 case 20:
4450 self->muladd_func_ptr = Allpass2_postprocessing_ireva;
4451 break;
4452
4453 case 21:
4454 self->muladd_func_ptr = Allpass2_postprocessing_areva;
4455 break;
4456
4457 case 22:
4458 self->muladd_func_ptr = Allpass2_postprocessing_revareva;
4459 break;
4460 }
4461 }
4462
4463 static void
4464 Allpass2_compute_next_data_frame(Allpass2 *self)
4465 {
4466 (*self->proc_func_ptr)(self);
4467 (*self->muladd_func_ptr)(self);
4468 }
4469
4470 static int
4471 Allpass2_traverse(Allpass2 *self, visitproc visit, void *arg)
4472 {
4473 pyo_VISIT
4474 Py_VISIT(self->input);
4475 Py_VISIT(self->freq);
4476 Py_VISIT(self->bw);
4477 return 0;
4478 }
4479
4480 static int
4481 Allpass2_clear(Allpass2 *self)
4482 {
4483 pyo_CLEAR
4484 Py_CLEAR(self->input);
4485 Py_CLEAR(self->freq);
4486 Py_CLEAR(self->bw);
4487 return 0;
4488 }
4489
4490 static void
4491 Allpass2_dealloc(Allpass2* self)
4492 {
4493 pyo_DEALLOC
4494 Allpass2_clear(self);
4495 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
4496 Py_TYPE(self)->tp_free((PyObject*)self);
4497 }
4498
4499 static PyObject *
4500 Allpass2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4501 {
4502 int i;
4503 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *bwtmp = NULL, *multmp = NULL, *addtmp = NULL;
4504 Allpass2 *self;
4505 self = (Allpass2 *)type->tp_alloc(type, 0);
4506
4507 self->freq = PyFloat_FromDouble(1000);
4508 self->bw = PyFloat_FromDouble(100);
4509 self->modebuffer[0] = 0;
4510 self->modebuffer[1] = 0;
4511 self->modebuffer[2] = 0;
4512 self->modebuffer[3] = 0;
4513 self->init = 1;
4514
4515 INIT_OBJECT_COMMON
4516
4517 self->minusPiOverSr = -PI / self->sr;
4518 self->twoPiOverSr = TWOPI / self->sr;
4519 self->nyquist = (MYFLT)self->sr * 0.49;
4520
4521 Stream_setFunctionPtr(self->stream, Allpass2_compute_next_data_frame);
4522 self->mode_func_ptr = Allpass2_setProcMode;
4523
4524 static char *kwlist[] = {"input", "freq", "bw", "mul", "add", NULL};
4525
4526 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &bwtmp, &multmp, &addtmp))
4527 Py_RETURN_NONE;
4528
4529 INIT_INPUT_STREAM
4530
4531 if (freqtmp)
4532 {
4533 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
4534 }
4535
4536 if (bwtmp)
4537 {
4538 PyObject_CallMethod((PyObject *)self, "setBw", "O", bwtmp);
4539 }
4540
4541 if (multmp)
4542 {
4543 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
4544 }
4545
4546 if (addtmp)
4547 {
4548 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
4549 }
4550
4551 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
4552
4553 (*self->mode_func_ptr)(self);
4554
4555 return (PyObject *)self;
4556 }
4557
4558 static PyObject * Allpass2_getServer(Allpass2* self) { GET_SERVER };
4559 static PyObject * Allpass2_getStream(Allpass2* self) { GET_STREAM };
4560 static PyObject * Allpass2_setMul(Allpass2 *self, PyObject *arg) { SET_MUL };
4561 static PyObject * Allpass2_setAdd(Allpass2 *self, PyObject *arg) { SET_ADD };
4562 static PyObject * Allpass2_setSub(Allpass2 *self, PyObject *arg) { SET_SUB };
4563 static PyObject * Allpass2_setDiv(Allpass2 *self, PyObject *arg) { SET_DIV };
4564
4565 static PyObject * Allpass2_play(Allpass2 *self, PyObject *args, PyObject *kwds) { PLAY };
4566 static PyObject * Allpass2_out(Allpass2 *self, PyObject *args, PyObject *kwds) { OUT };
4567 static PyObject * Allpass2_stop(Allpass2 *self, PyObject *args, PyObject *kwds) { STOP };
4568
4569 static PyObject * Allpass2_multiply(Allpass2 *self, PyObject *arg) { MULTIPLY };
4570 static PyObject * Allpass2_inplace_multiply(Allpass2 *self, PyObject *arg) { INPLACE_MULTIPLY };
4571 static PyObject * Allpass2_add(Allpass2 *self, PyObject *arg) { ADD };
4572 static PyObject * Allpass2_inplace_add(Allpass2 *self, PyObject *arg) { INPLACE_ADD };
4573 static PyObject * Allpass2_sub(Allpass2 *self, PyObject *arg) { SUB };
4574 static PyObject * Allpass2_inplace_sub(Allpass2 *self, PyObject *arg) { INPLACE_SUB };
4575 static PyObject * Allpass2_div(Allpass2 *self, PyObject *arg) { DIV };
4576 static PyObject * Allpass2_inplace_div(Allpass2 *self, PyObject *arg) { INPLACE_DIV };
4577
4578 static PyObject * Allpass2_setFreq(Allpass2 *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
4579 static PyObject * Allpass2_setBw(Allpass2 *self, PyObject *arg) { SET_PARAM(self->bw, self->bw_stream, 3); }
4580
4581 static PyMemberDef Allpass2_members[] =
4582 {
4583 {"server", T_OBJECT_EX, offsetof(Allpass2, server), 0, "Pyo server."},
4584 {"stream", T_OBJECT_EX, offsetof(Allpass2, stream), 0, "Stream object."},
4585 {"input", T_OBJECT_EX, offsetof(Allpass2, input), 0, "Input sound object."},
4586 {"freq", T_OBJECT_EX, offsetof(Allpass2, freq), 0, "Cutoff frequency in cycle per second."},
4587 {"bw", T_OBJECT_EX, offsetof(Allpass2, bw), 0, "Bandwidth."},
4588 {"mul", T_OBJECT_EX, offsetof(Allpass2, mul), 0, "Mul factor."},
4589 {"add", T_OBJECT_EX, offsetof(Allpass2, add), 0, "Add factor."},
4590 {NULL} /* Sentinel */
4591 };
4592
4593 static PyMethodDef Allpass2_methods[] =
4594 {
4595 {"getServer", (PyCFunction)Allpass2_getServer, METH_NOARGS, "Returns server object."},
4596 {"_getStream", (PyCFunction)Allpass2_getStream, METH_NOARGS, "Returns stream object."},
4597 {"play", (PyCFunction)Allpass2_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
4598 {"out", (PyCFunction)Allpass2_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
4599 {"stop", (PyCFunction)Allpass2_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
4600 {"setFreq", (PyCFunction)Allpass2_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
4601 {"setBw", (PyCFunction)Allpass2_setBw, METH_O, "Sets filter bandwidth."},
4602 {"setMul", (PyCFunction)Allpass2_setMul, METH_O, "Sets oscillator mul factor."},
4603 {"setAdd", (PyCFunction)Allpass2_setAdd, METH_O, "Sets oscillator add factor."},
4604 {"setSub", (PyCFunction)Allpass2_setSub, METH_O, "Sets inverse add factor."},
4605 {"setDiv", (PyCFunction)Allpass2_setDiv, METH_O, "Sets inverse mul factor."},
4606 {NULL} /* Sentinel */
4607 };
4608
4609 static PyNumberMethods Allpass2_as_number =
4610 {
4611 (binaryfunc)Allpass2_add, /*nb_add*/
4612 (binaryfunc)Allpass2_sub, /*nb_subtract*/
4613 (binaryfunc)Allpass2_multiply, /*nb_multiply*/
4614 0, /*nb_remainder*/
4615 0, /*nb_divmod*/
4616 0, /*nb_power*/
4617 0, /*nb_neg*/
4618 0, /*nb_pos*/
4619 0, /*(unaryfunc)array_abs,*/
4620 0, /*nb_nonzero*/
4621 0, /*nb_invert*/
4622 0, /*nb_lshift*/
4623 0, /*nb_rshift*/
4624 0, /*nb_and*/
4625 0, /*nb_xor*/
4626 0, /*nb_or*/
4627 0, /*nb_int*/
4628 0, /*nb_long*/
4629 0, /*nb_float*/
4630 (binaryfunc)Allpass2_inplace_add, /*inplace_add*/
4631 (binaryfunc)Allpass2_inplace_sub, /*inplace_subtract*/
4632 (binaryfunc)Allpass2_inplace_multiply, /*inplace_multiply*/
4633 0, /*inplace_remainder*/
4634 0, /*inplace_power*/
4635 0, /*inplace_lshift*/
4636 0, /*inplace_rshift*/
4637 0, /*inplace_and*/
4638 0, /*inplace_xor*/
4639 0, /*inplace_or*/
4640 0, /*nb_floor_divide*/
4641 (binaryfunc)Allpass2_div, /*nb_true_divide*/
4642 0, /*nb_inplace_floor_divide*/
4643 (binaryfunc)Allpass2_inplace_div, /*nb_inplace_true_divide*/
4644 0, /* nb_index */
4645 };
4646
4647 PyTypeObject Allpass2Type =
4648 {
4649 PyVarObject_HEAD_INIT(NULL, 0)
4650 "_pyo.Allpass2_base", /*tp_name*/
4651 sizeof(Allpass2), /*tp_basicsize*/
4652 0, /*tp_itemsize*/
4653 (destructor)Allpass2_dealloc, /*tp_dealloc*/
4654 0, /*tp_print*/
4655 0, /*tp_getattr*/
4656 0, /*tp_setattr*/
4657 0, /*tp_as_async (tp_compare in Python 2)*/
4658 0, /*tp_repr*/
4659 &Allpass2_as_number, /*tp_as_number*/
4660 0, /*tp_as_sequence*/
4661 0, /*tp_as_mapping*/
4662 0, /*tp_hash */
4663 0, /*tp_call*/
4664 0, /*tp_str*/
4665 0, /*tp_getattro*/
4666 0, /*tp_setattro*/
4667 0, /*tp_as_buffer*/
4668 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
4669 "Allpass2 objects. Second order allpass filter.", /* tp_doc */
4670 (traverseproc)Allpass2_traverse, /* tp_traverse */
4671 (inquiry)Allpass2_clear, /* tp_clear */
4672 0, /* tp_richcompare */
4673 0, /* tp_weaklistoffset */
4674 0, /* tp_iter */
4675 0, /* tp_iternext */
4676 Allpass2_methods, /* tp_methods */
4677 Allpass2_members, /* tp_members */
4678 0, /* tp_getset */
4679 0, /* tp_base */
4680 0, /* tp_dict */
4681 0, /* tp_descr_get */
4682 0, /* tp_descr_set */
4683 0, /* tp_dictoffset */
4684 0, /* tp_init */
4685 0, /* tp_alloc */
4686 Allpass2_new, /* tp_new */
4687 };
4688
4689 /*******************/
4690 /***** Phaser ******/
4691 /*******************/
4692 typedef struct
4693 {
4694 pyo_audio_HEAD
4695 PyObject *input;
4696 Stream *input_stream;
4697 PyObject *freq;
4698 Stream *freq_stream;
4699 PyObject *spread;
4700 Stream *spread_stream;
4701 PyObject *q;
4702 Stream *q_stream;
4703 PyObject *feedback;
4704 Stream *feedback_stream;
4705 int stages;
4706 int modebuffer[6]; // need at least 2 slots for mul & add
4707 MYFLT halfSr;
4708 MYFLT minusPiOnSr;
4709 MYFLT twoPiOnSr;
4710 MYFLT norm_arr_pos;
4711 MYFLT tmp;
4712 // sample memories
4713 MYFLT *y1;
4714 MYFLT *y2;
4715 // coefficients
4716 MYFLT *alpha;
4717 MYFLT *beta;
4718 } Phaser;
4719
4720 static MYFLT
4721 Phaser_clip(MYFLT x)
4722 {
4723 if (x < -1.0)
4724 return -1.0;
4725 else if (x > 1.0)
4726 return 1.0;
4727 else
4728 return x;
4729 }
4730
4731 static void
4732 Phaser_compute_variables(Phaser *self, MYFLT freq, MYFLT spread, MYFLT q)
4733 {
4734 int i, ipart;
4735 MYFLT radius, angle, fr, qfactor, pos, fpart;
4736
4737 qfactor = 1.0 / q * self->minusPiOnSr;
4738 fr = freq;
4739
4740 for (i = 0; i < self->stages; i++)
4741 {
4742 if (fr <= 20)
4743 fr = 20;
4744 else if (fr >= self->halfSr)
4745 fr = self->halfSr;
4746
4747 radius = MYEXP(fr * qfactor);
4748 angle = fr * self->twoPiOnSr;
4749
4750 self->alpha[i] = radius * radius;
4751
4752 pos = angle * self->norm_arr_pos;
4753 ipart = (int)pos;
4754 fpart = pos - ipart;
4755 self->beta[i] = -2.0 * radius * (HALF_COS_ARRAY[i] + (HALF_COS_ARRAY[i + 1] - HALF_COS_ARRAY[i]) * fpart);
4756 fr *= spread;
4757 }
4758 }
4759
4760 static void
4761 Phaser_filters_iii(Phaser *self)
4762 {
4763 MYFLT val;
4764 int i, j;
4765 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4766
4767 if (self->modebuffer[5] == 0)
4768 {
4769 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
4770
4771 for (i = 0; i < self->bufsize; i++)
4772 {
4773 self->tmp = in[i] + self->tmp * feed;
4774
4775 for (j = 0; j < self->stages; j++)
4776 {
4777 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4778 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4779 self->y2[j] = self->y1[j];
4780 self->y1[j] = val;
4781 }
4782
4783 self->data[i] = self->tmp;
4784 }
4785 }
4786 else
4787 {
4788 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
4789
4790 for (i = 0; i < self->bufsize; i++)
4791 {
4792 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
4793
4794 for (j = 0; j < self->stages; j++)
4795 {
4796 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4797 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4798 self->y2[j] = self->y1[j];
4799 self->y1[j] = val;
4800 }
4801
4802 self->data[i] = self->tmp;
4803 }
4804 }
4805 }
4806
4807 static void
4808 Phaser_filters_aii(Phaser *self)
4809 {
4810 MYFLT val;
4811 int i, j;
4812 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4813 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
4814 MYFLT spread = PyFloat_AS_DOUBLE(self->spread);
4815 MYFLT q = PyFloat_AS_DOUBLE(self->q);
4816
4817 if (self->modebuffer[5] == 0)
4818 {
4819 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
4820
4821 for (i = 0; i < self->bufsize; i++)
4822 {
4823 Phaser_compute_variables(self, freq[i], spread, q);
4824 self->tmp = in[i] + self->tmp * feed;
4825
4826 for (j = 0; j < self->stages; j++)
4827 {
4828 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4829 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4830 self->y2[j] = self->y1[j];
4831 self->y1[j] = val;
4832 }
4833
4834 self->data[i] = self->tmp;
4835 }
4836 }
4837 else
4838 {
4839 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
4840
4841 for (i = 0; i < self->bufsize; i++)
4842 {
4843 Phaser_compute_variables(self, freq[i], spread, q);
4844 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
4845
4846 for (j = 0; j < self->stages; j++)
4847 {
4848 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4849 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4850 self->y2[j] = self->y1[j];
4851 self->y1[j] = val;
4852 }
4853
4854 self->data[i] = self->tmp;
4855 }
4856 }
4857 }
4858
4859 static void
4860 Phaser_filters_iai(Phaser *self)
4861 {
4862 MYFLT val;
4863 int i, j;
4864 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4865 MYFLT freq = PyFloat_AS_DOUBLE(self->freq);
4866 MYFLT *spread = Stream_getData((Stream *)self->spread_stream);
4867 MYFLT q = PyFloat_AS_DOUBLE(self->q);
4868
4869 if (self->modebuffer[5] == 0)
4870 {
4871 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
4872
4873 for (i = 0; i < self->bufsize; i++)
4874 {
4875 Phaser_compute_variables(self, freq, spread[i], q);
4876 self->tmp = in[i] + self->tmp * feed;
4877
4878 for (j = 0; j < self->stages; j++)
4879 {
4880 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4881 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4882 self->y2[j] = self->y1[j];
4883 self->y1[j] = val;
4884 }
4885
4886 self->data[i] = self->tmp;
4887 }
4888 }
4889 else
4890 {
4891 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
4892
4893 for (i = 0; i < self->bufsize; i++)
4894 {
4895 Phaser_compute_variables(self, freq, spread[i], q);
4896 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
4897
4898 for (j = 0; j < self->stages; j++)
4899 {
4900 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4901 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4902 self->y2[j] = self->y1[j];
4903 self->y1[j] = val;
4904 }
4905
4906 self->data[i] = self->tmp;
4907 }
4908 }
4909 }
4910
4911 static void
4912 Phaser_filters_aai(Phaser *self)
4913 {
4914 MYFLT val;
4915 int i, j;
4916 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4917 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
4918 MYFLT *spread = Stream_getData((Stream *)self->spread_stream);
4919 MYFLT q = PyFloat_AS_DOUBLE(self->q);
4920
4921 if (self->modebuffer[5] == 0)
4922 {
4923 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
4924
4925 for (i = 0; i < self->bufsize; i++)
4926 {
4927 Phaser_compute_variables(self, freq[i], spread[i], q);
4928 self->tmp = in[i] + self->tmp * feed;
4929
4930 for (j = 0; j < self->stages; j++)
4931 {
4932 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4933 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4934 self->y2[j] = self->y1[j];
4935 self->y1[j] = val;
4936 }
4937
4938 self->data[i] = self->tmp;
4939 }
4940 }
4941 else
4942 {
4943 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
4944
4945 for (i = 0; i < self->bufsize; i++)
4946 {
4947 Phaser_compute_variables(self, freq[i], spread[i], q);
4948 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
4949
4950 for (j = 0; j < self->stages; j++)
4951 {
4952 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4953 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4954 self->y2[j] = self->y1[j];
4955 self->y1[j] = val;
4956 }
4957
4958 self->data[i] = self->tmp;
4959 }
4960 }
4961 }
4962
4963 static void
4964 Phaser_filters_iia(Phaser *self)
4965 {
4966 MYFLT val;
4967 int i, j;
4968 MYFLT *in = Stream_getData((Stream *)self->input_stream);
4969 MYFLT freq = PyFloat_AS_DOUBLE(self->freq);
4970 MYFLT spread = PyFloat_AS_DOUBLE(self->spread);
4971 MYFLT *q = Stream_getData((Stream *)self->q_stream);
4972
4973 if (self->modebuffer[5] == 0)
4974 {
4975 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
4976
4977 for (i = 0; i < self->bufsize; i++)
4978 {
4979 Phaser_compute_variables(self, freq, spread, q[i]);
4980 self->tmp = in[i] + self->tmp * feed;
4981
4982 for (j = 0; j < self->stages; j++)
4983 {
4984 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
4985 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
4986 self->y2[j] = self->y1[j];
4987 self->y1[j] = val;
4988 }
4989
4990 self->data[i] = self->tmp;
4991 }
4992 }
4993 else
4994 {
4995 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
4996
4997 for (i = 0; i < self->bufsize; i++)
4998 {
4999 Phaser_compute_variables(self, freq, spread, q[i]);
5000 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
5001
5002 for (j = 0; j < self->stages; j++)
5003 {
5004 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5005 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5006 self->y2[j] = self->y1[j];
5007 self->y1[j] = val;
5008 }
5009
5010 self->data[i] = self->tmp;
5011 }
5012 }
5013 }
5014
5015 static void
5016 Phaser_filters_aia(Phaser *self)
5017 {
5018 MYFLT val;
5019 int i, j;
5020 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5021 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
5022 MYFLT spread = PyFloat_AS_DOUBLE(self->spread);
5023 MYFLT *q = Stream_getData((Stream *)self->q_stream);
5024
5025 if (self->modebuffer[5] == 0)
5026 {
5027 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
5028
5029 for (i = 0; i < self->bufsize; i++)
5030 {
5031 Phaser_compute_variables(self, freq[i], spread, q[i]);
5032 self->tmp = in[i] + self->tmp * feed;
5033
5034 for (j = 0; j < self->stages; j++)
5035 {
5036 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5037 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5038 self->y2[j] = self->y1[j];
5039 self->y1[j] = val;
5040 }
5041
5042 self->data[i] = self->tmp;
5043 }
5044 }
5045 else
5046 {
5047 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
5048
5049 for (i = 0; i < self->bufsize; i++)
5050 {
5051 Phaser_compute_variables(self, freq[i], spread, q[i]);
5052 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
5053
5054 for (j = 0; j < self->stages; j++)
5055 {
5056 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5057 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5058 self->y2[j] = self->y1[j];
5059 self->y1[j] = val;
5060 }
5061
5062 self->data[i] = self->tmp;
5063 }
5064 }
5065 }
5066
5067 static void
5068 Phaser_filters_iaa(Phaser *self)
5069 {
5070 MYFLT val;
5071 int i, j;
5072 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5073 MYFLT freq = PyFloat_AS_DOUBLE(self->freq);
5074 MYFLT *spread = Stream_getData((Stream *)self->spread_stream);
5075 MYFLT *q = Stream_getData((Stream *)self->q_stream);
5076
5077 if (self->modebuffer[5] == 0)
5078 {
5079 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
5080
5081 for (i = 0; i < self->bufsize; i++)
5082 {
5083 Phaser_compute_variables(self, freq, spread[i], q[i]);
5084 self->tmp = in[i] + self->tmp * feed;
5085
5086 for (j = 0; j < self->stages; j++)
5087 {
5088 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5089 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5090 self->y2[j] = self->y1[j];
5091 self->y1[j] = val;
5092 }
5093
5094 self->data[i] = self->tmp;
5095 }
5096 }
5097 else
5098 {
5099 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
5100
5101 for (i = 0; i < self->bufsize; i++)
5102 {
5103 Phaser_compute_variables(self, freq, spread[i], q[i]);
5104 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
5105
5106 for (j = 0; j < self->stages; j++)
5107 {
5108 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5109 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5110 self->y2[j] = self->y1[j];
5111 self->y1[j] = val;
5112 }
5113
5114 self->data[i] = self->tmp;
5115 }
5116 }
5117 }
5118
5119 static void
5120 Phaser_filters_aaa(Phaser *self)
5121 {
5122 MYFLT val;
5123 int i, j;
5124 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5125 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
5126 MYFLT *spread = Stream_getData((Stream *)self->spread_stream);
5127 MYFLT *q = Stream_getData((Stream *)self->q_stream);
5128
5129 if (self->modebuffer[5] == 0)
5130 {
5131 MYFLT feed = Phaser_clip(PyFloat_AS_DOUBLE(self->feedback));
5132
5133 for (i = 0; i < self->bufsize; i++)
5134 {
5135 Phaser_compute_variables(self, freq[i], spread[i], q[i]);
5136 self->tmp = in[i] + self->tmp * feed;
5137
5138 for (j = 0; j < self->stages; j++)
5139 {
5140 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5141 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5142 self->y2[j] = self->y1[j];
5143 self->y1[j] = val;
5144 }
5145
5146 self->data[i] = self->tmp;
5147 }
5148 }
5149 else
5150 {
5151 MYFLT *feed = Stream_getData((Stream *)self->feedback_stream);
5152
5153 for (i = 0; i < self->bufsize; i++)
5154 {
5155 Phaser_compute_variables(self, freq[i], spread[i], q[i]);
5156 self->tmp = in[i] + self->tmp * Phaser_clip(feed[i]);
5157
5158 for (j = 0; j < self->stages; j++)
5159 {
5160 val = self->tmp + (self->y1[j] * -self->beta[j]) + (self->y2[j] * -self->alpha[j]);
5161 self->tmp = (val * self->alpha[j]) + (self->y1[j] * self->beta[j]) + self->y2[j];
5162 self->y2[j] = self->y1[j];
5163 self->y1[j] = val;
5164 }
5165
5166 self->data[i] = self->tmp;
5167 }
5168 }
5169 }
5170
5171 static void Phaser_postprocessing_ii(Phaser *self) { POST_PROCESSING_II };
5172 static void Phaser_postprocessing_ai(Phaser *self) { POST_PROCESSING_AI };
5173 static void Phaser_postprocessing_ia(Phaser *self) { POST_PROCESSING_IA };
5174 static void Phaser_postprocessing_aa(Phaser *self) { POST_PROCESSING_AA };
5175 static void Phaser_postprocessing_ireva(Phaser *self) { POST_PROCESSING_IREVA };
5176 static void Phaser_postprocessing_areva(Phaser *self) { POST_PROCESSING_AREVA };
5177 static void Phaser_postprocessing_revai(Phaser *self) { POST_PROCESSING_REVAI };
5178 static void Phaser_postprocessing_revaa(Phaser *self) { POST_PROCESSING_REVAA };
5179 static void Phaser_postprocessing_revareva(Phaser *self) { POST_PROCESSING_REVAREVA };
5180
5181 static void
5182 Phaser_setProcMode(Phaser *self)
5183 {
5184 int procmode, muladdmode;
5185 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
5186 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
5187
5188 switch (procmode)
5189 {
5190 case 0:
5191 Phaser_compute_variables(self, PyFloat_AS_DOUBLE(self->freq), PyFloat_AS_DOUBLE(self->spread), PyFloat_AS_DOUBLE(self->q));
5192 self->proc_func_ptr = Phaser_filters_iii;
5193 break;
5194
5195 case 1:
5196 self->proc_func_ptr = Phaser_filters_aii;
5197 break;
5198
5199 case 10:
5200 self->proc_func_ptr = Phaser_filters_iai;
5201 break;
5202
5203 case 11:
5204 self->proc_func_ptr = Phaser_filters_aai;
5205 break;
5206
5207 case 100:
5208 self->proc_func_ptr = Phaser_filters_iia;
5209 break;
5210
5211 case 101:
5212 self->proc_func_ptr = Phaser_filters_aia;
5213 break;
5214
5215 case 110:
5216 self->proc_func_ptr = Phaser_filters_iaa;
5217 break;
5218
5219 case 111:
5220 self->proc_func_ptr = Phaser_filters_aaa;
5221 break;
5222 }
5223
5224 switch (muladdmode)
5225 {
5226 case 0:
5227 self->muladd_func_ptr = Phaser_postprocessing_ii;
5228 break;
5229
5230 case 1:
5231 self->muladd_func_ptr = Phaser_postprocessing_ai;
5232 break;
5233
5234 case 2:
5235 self->muladd_func_ptr = Phaser_postprocessing_revai;
5236 break;
5237
5238 case 10:
5239 self->muladd_func_ptr = Phaser_postprocessing_ia;
5240 break;
5241
5242 case 11:
5243 self->muladd_func_ptr = Phaser_postprocessing_aa;
5244 break;
5245
5246 case 12:
5247 self->muladd_func_ptr = Phaser_postprocessing_revaa;
5248 break;
5249
5250 case 20:
5251 self->muladd_func_ptr = Phaser_postprocessing_ireva;
5252 break;
5253
5254 case 21:
5255 self->muladd_func_ptr = Phaser_postprocessing_areva;
5256 break;
5257
5258 case 22:
5259 self->muladd_func_ptr = Phaser_postprocessing_revareva;
5260 break;
5261 }
5262 }
5263
5264 static void
5265 Phaser_compute_next_data_frame(Phaser *self)
5266 {
5267 (*self->proc_func_ptr)(self);
5268 (*self->muladd_func_ptr)(self);
5269 }
5270
5271 static int
5272 Phaser_traverse(Phaser *self, visitproc visit, void *arg)
5273 {
5274 pyo_VISIT
5275 Py_VISIT(self->input);
5276 Py_VISIT(self->freq);
5277 Py_VISIT(self->spread);
5278 Py_VISIT(self->q);
5279 Py_VISIT(self->feedback);
5280 return 0;
5281 }
5282
5283 static int
5284 Phaser_clear(Phaser *self)
5285 {
5286 pyo_CLEAR
5287 Py_CLEAR(self->input);
5288 Py_CLEAR(self->freq);
5289 Py_CLEAR(self->spread);
5290 Py_CLEAR(self->q);
5291 Py_CLEAR(self->feedback);
5292 return 0;
5293 }
5294
5295 static void
5296 Phaser_dealloc(Phaser* self)
5297 {
5298 pyo_DEALLOC
5299 PyMem_RawFree(self->y1);
5300 PyMem_RawFree(self->y2);
5301 PyMem_RawFree(self->alpha);
5302 PyMem_RawFree(self->beta);
5303 Phaser_clear(self);
5304 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
5305 Py_TYPE(self)->tp_free((PyObject*)self);
5306 }
5307
5308 static PyObject *
5309 Phaser_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5310 {
5311 int i;
5312 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *spreadtmp = NULL, *qtmp = NULL, *feedbacktmp = NULL, *multmp = NULL, *addtmp = NULL;
5313 Phaser *self;
5314 self = (Phaser *)type->tp_alloc(type, 0);
5315
5316 self->freq = PyFloat_FromDouble(1000.0);
5317 self->spread = PyFloat_FromDouble(1.0);
5318 self->q = PyFloat_FromDouble(10.0);
5319 self->feedback = PyFloat_FromDouble(0.0);
5320 self->tmp = 0.0;
5321 self->stages = 8;
5322 self->modebuffer[0] = 0;
5323 self->modebuffer[1] = 0;
5324 self->modebuffer[2] = 0;
5325 self->modebuffer[3] = 0;
5326 self->modebuffer[4] = 0;
5327 self->modebuffer[5] = 0;
5328
5329 INIT_OBJECT_COMMON
5330
5331 self->halfSr = (MYFLT)self->sr * 0.49;
5332 self->minusPiOnSr = -PI / self->sr;
5333 self->twoPiOnSr = TWOPI / self->sr;
5334 self->norm_arr_pos = 1.0 / PI * 512.0;
5335
5336 Stream_setFunctionPtr(self->stream, Phaser_compute_next_data_frame);
5337 self->mode_func_ptr = Phaser_setProcMode;
5338
5339 static char *kwlist[] = {"input", "freq", "spread", "q", "feedback", "num", "mul", "add", NULL};
5340
5341 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOiOO", kwlist, &inputtmp, &freqtmp, &spreadtmp, &qtmp, &feedbacktmp, &self->stages, &multmp, &addtmp))
5342 Py_RETURN_NONE;
5343
5344 INIT_INPUT_STREAM
5345
5346 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, self->stages * sizeof(MYFLT));
5347 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, self->stages * sizeof(MYFLT));
5348 self->alpha = (MYFLT *)PyMem_RawRealloc(self->alpha, self->stages * sizeof(MYFLT));
5349 self->beta = (MYFLT *)PyMem_RawRealloc(self->beta, self->stages * sizeof(MYFLT));
5350
5351
5352 if (freqtmp)
5353 {
5354 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
5355 }
5356
5357 if (spreadtmp)
5358 {
5359 PyObject_CallMethod((PyObject *)self, "setSpread", "O", spreadtmp);
5360 }
5361
5362 if (qtmp)
5363 {
5364 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
5365 }
5366
5367 if (feedbacktmp)
5368 {
5369 PyObject_CallMethod((PyObject *)self, "setFeedback", "O", feedbacktmp);
5370 }
5371
5372 if (multmp)
5373 {
5374 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
5375 }
5376
5377 if (addtmp)
5378 {
5379 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
5380 }
5381
5382 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
5383
5384 (*self->mode_func_ptr)(self);
5385
5386 for (i = 0; i < self->stages; i++)
5387 {
5388 self->y1[i] = self->y2[i] = 0.0;
5389 }
5390
5391 return (PyObject *)self;
5392 }
5393
5394 static PyObject * Phaser_getServer(Phaser* self) { GET_SERVER };
5395 static PyObject * Phaser_getStream(Phaser* self) { GET_STREAM };
5396 static PyObject * Phaser_setMul(Phaser *self, PyObject *arg) { SET_MUL };
5397 static PyObject * Phaser_setAdd(Phaser *self, PyObject *arg) { SET_ADD };
5398 static PyObject * Phaser_setSub(Phaser *self, PyObject *arg) { SET_SUB };
5399 static PyObject * Phaser_setDiv(Phaser *self, PyObject *arg) { SET_DIV };
5400
5401 static PyObject * Phaser_play(Phaser *self, PyObject *args, PyObject *kwds) { PLAY };
5402 static PyObject * Phaser_out(Phaser *self, PyObject *args, PyObject *kwds) { OUT };
5403 static PyObject * Phaser_stop(Phaser *self, PyObject *args, PyObject *kwds) { STOP };
5404
5405 static PyObject * Phaser_multiply(Phaser *self, PyObject *arg) { MULTIPLY };
5406 static PyObject * Phaser_inplace_multiply(Phaser *self, PyObject *arg) { INPLACE_MULTIPLY };
5407 static PyObject * Phaser_add(Phaser *self, PyObject *arg) { ADD };
5408 static PyObject * Phaser_inplace_add(Phaser *self, PyObject *arg) { INPLACE_ADD };
5409 static PyObject * Phaser_sub(Phaser *self, PyObject *arg) { SUB };
5410 static PyObject * Phaser_inplace_sub(Phaser *self, PyObject *arg) { INPLACE_SUB };
5411 static PyObject * Phaser_div(Phaser *self, PyObject *arg) { DIV };
5412 static PyObject * Phaser_inplace_div(Phaser *self, PyObject *arg) { INPLACE_DIV };
5413
5414 static PyObject * Phaser_setFreq(Phaser *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
5415 static PyObject * Phaser_setSpread(Phaser *self, PyObject *arg) { SET_PARAM(self->spread, self->spread_stream, 3); }
5416 static PyObject * Phaser_setQ(Phaser *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 4); }
5417 static PyObject * Phaser_setFeedback(Phaser *self, PyObject *arg) { SET_PARAM(self->feedback, self->feedback_stream, 5); }
5418
5419 static PyMemberDef Phaser_members[] =
5420 {
5421 {"server", T_OBJECT_EX, offsetof(Phaser, server), 0, "Pyo server."},
5422 {"stream", T_OBJECT_EX, offsetof(Phaser, stream), 0, "Stream object."},
5423 {"input", T_OBJECT_EX, offsetof(Phaser, input), 0, "Input sound object."},
5424 {"freq", T_OBJECT_EX, offsetof(Phaser, freq), 0, "Base frequency in Hertz."},
5425 {"spread", T_OBJECT_EX, offsetof(Phaser, spread), 0, "Frequencies spreading factor."},
5426 {"q", T_OBJECT_EX, offsetof(Phaser, q), 0, "Q factor."},
5427 {"feedback", T_OBJECT_EX, offsetof(Phaser, feedback), 0, "Feedback factor."},
5428 {"mul", T_OBJECT_EX, offsetof(Phaser, mul), 0, "Mul factor."},
5429 {"add", T_OBJECT_EX, offsetof(Phaser, add), 0, "Add factor."},
5430 {NULL} /* Sentinel */
5431 };
5432
5433 static PyMethodDef Phaser_methods[] =
5434 {
5435 {"getServer", (PyCFunction)Phaser_getServer, METH_NOARGS, "Returns server object."},
5436 {"_getStream", (PyCFunction)Phaser_getStream, METH_NOARGS, "Returns stream object."},
5437 {"play", (PyCFunction)Phaser_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
5438 {"out", (PyCFunction)Phaser_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
5439 {"stop", (PyCFunction)Phaser_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
5440 {"setFreq", (PyCFunction)Phaser_setFreq, METH_O, "Sets base frequency in Hertz."},
5441 {"setSpread", (PyCFunction)Phaser_setSpread, METH_O, "Sets spreading factor."},
5442 {"setQ", (PyCFunction)Phaser_setQ, METH_O, "Sets filter Q factor."},
5443 {"setFeedback", (PyCFunction)Phaser_setFeedback, METH_O, "Sets filter Feedback factor."},
5444 {"setMul", (PyCFunction)Phaser_setMul, METH_O, "Sets oscillator mul factor."},
5445 {"setAdd", (PyCFunction)Phaser_setAdd, METH_O, "Sets oscillator add factor."},
5446 {"setSub", (PyCFunction)Phaser_setSub, METH_O, "Sets inverse add factor."},
5447 {"setDiv", (PyCFunction)Phaser_setDiv, METH_O, "Sets inverse mul factor."},
5448 {NULL} /* Sentinel */
5449 };
5450
5451 static PyNumberMethods Phaser_as_number =
5452 {
5453 (binaryfunc)Phaser_add, /*nb_add*/
5454 (binaryfunc)Phaser_sub, /*nb_subtract*/
5455 (binaryfunc)Phaser_multiply, /*nb_multiply*/
5456 0, /*nb_remainder*/
5457 0, /*nb_divmod*/
5458 0, /*nb_power*/
5459 0, /*nb_neg*/
5460 0, /*nb_pos*/
5461 0, /*(unaryfunc)array_abs,*/
5462 0, /*nb_nonzero*/
5463 0, /*nb_invert*/
5464 0, /*nb_lshift*/
5465 0, /*nb_rshift*/
5466 0, /*nb_and*/
5467 0, /*nb_xor*/
5468 0, /*nb_or*/
5469 0, /*nb_int*/
5470 0, /*nb_long*/
5471 0, /*nb_float*/
5472 (binaryfunc)Phaser_inplace_add, /*inplace_add*/
5473 (binaryfunc)Phaser_inplace_sub, /*inplace_subtract*/
5474 (binaryfunc)Phaser_inplace_multiply, /*inplace_multiply*/
5475 0, /*inplace_remainder*/
5476 0, /*inplace_power*/
5477 0, /*inplace_lshift*/
5478 0, /*inplace_rshift*/
5479 0, /*inplace_and*/
5480 0, /*inplace_xor*/
5481 0, /*inplace_or*/
5482 0, /*nb_floor_divide*/
5483 (binaryfunc)Phaser_div, /*nb_true_divide*/
5484 0, /*nb_inplace_floor_divide*/
5485 (binaryfunc)Phaser_inplace_div, /*nb_inplace_true_divide*/
5486 0, /* nb_index */
5487 };
5488
5489 PyTypeObject PhaserType =
5490 {
5491 PyVarObject_HEAD_INIT(NULL, 0)
5492 "_pyo.Phaser_base", /*tp_name*/
5493 sizeof(Phaser), /*tp_basicsize*/
5494 0, /*tp_itemsize*/
5495 (destructor)Phaser_dealloc, /*tp_dealloc*/
5496 0, /*tp_print*/
5497 0, /*tp_getattr*/
5498 0, /*tp_setattr*/
5499 0, /*tp_as_async (tp_compare in Python 2)*/
5500 0, /*tp_repr*/
5501 &Phaser_as_number, /*tp_as_number*/
5502 0, /*tp_as_sequence*/
5503 0, /*tp_as_mapping*/
5504 0, /*tp_hash */
5505 0, /*tp_call*/
5506 0, /*tp_str*/
5507 0, /*tp_getattro*/
5508 0, /*tp_setattro*/
5509 0, /*tp_as_buffer*/
5510 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
5511 "Phaser objects. Multi-stages second order allpass filters.", /* tp_doc */
5512 (traverseproc)Phaser_traverse, /* tp_traverse */
5513 (inquiry)Phaser_clear, /* tp_clear */
5514 0, /* tp_richcompare */
5515 0, /* tp_weaklistoffset */
5516 0, /* tp_iter */
5517 0, /* tp_iternext */
5518 Phaser_methods, /* tp_methods */
5519 Phaser_members, /* tp_members */
5520 0, /* tp_getset */
5521 0, /* tp_base */
5522 0, /* tp_dict */
5523 0, /* tp_descr_get */
5524 0, /* tp_descr_set */
5525 0, /* tp_dictoffset */
5526 0, /* tp_init */
5527 0, /* tp_alloc */
5528 Phaser_new, /* tp_new */
5529 };
5530
5531 typedef struct
5532 {
5533 pyo_audio_HEAD
5534 PyObject *input;
5535 Stream *input_stream;
5536 PyObject *input2;
5537 Stream *input2_stream;
5538 PyObject *freq;
5539 Stream *freq_stream;
5540 PyObject *spread;
5541 Stream *spread_stream;
5542 PyObject *q;
5543 Stream *q_stream;
5544 PyObject *slope;
5545 Stream *slope_stream;
5546 MYFLT last_freq;
5547 MYFLT last_spread;
5548 MYFLT last_q;
5549 MYFLT last_slope;
5550 MYFLT factor;
5551 int stages;
5552 int last_stages;
5553 int flag;
5554 MYFLT nyquist;
5555 MYFLT twoPiOnSr;
5556 int modebuffer[6]; // need at least 2 slots for mul & add
5557 // sample memories
5558 MYFLT *y1;
5559 MYFLT *y2;
5560 MYFLT *yy1;
5561 MYFLT *yy2;
5562 // follower memories
5563 MYFLT *follow;
5564 // coefficients
5565 MYFLT *b0;
5566 MYFLT *b2;
5567 MYFLT *a0;
5568 MYFLT *a1;
5569 MYFLT *a2;
5570 } Vocoder;
5571
5572 static void
5573 Vocoder_allocate_memories(Vocoder *self)
5574 {
5575 int i, i2j, j;
5576 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, self->stages * 2 * sizeof(MYFLT));
5577 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, self->stages * 2 * sizeof(MYFLT));
5578 self->yy1 = (MYFLT *)PyMem_RawRealloc(self->yy1, self->stages * 2 * sizeof(MYFLT));
5579 self->yy2 = (MYFLT *)PyMem_RawRealloc(self->yy2, self->stages * 2 * sizeof(MYFLT));
5580 self->b0 = (MYFLT *)PyMem_RawRealloc(self->b0, self->stages * sizeof(MYFLT));
5581 self->b2 = (MYFLT *)PyMem_RawRealloc(self->b2, self->stages * sizeof(MYFLT));
5582 self->a0 = (MYFLT *)PyMem_RawRealloc(self->a0, self->stages * sizeof(MYFLT));
5583 self->a1 = (MYFLT *)PyMem_RawRealloc(self->a1, self->stages * sizeof(MYFLT));
5584 self->a2 = (MYFLT *)PyMem_RawRealloc(self->a2, self->stages * sizeof(MYFLT));
5585 self->follow = (MYFLT *)PyMem_RawRealloc(self->follow, self->stages * sizeof(MYFLT));
5586
5587 for (i = 0; i < self->stages; i++)
5588 {
5589 self->b0[i] = self->b2[i] = self->a0[i] = self->a1[i] = self->a2[i] = self->follow[i] = 0.0;
5590
5591 for (j = 0; j < 2; j++)
5592 {
5593 i2j = i * 2 + j;
5594 self->yy1[i2j] = self->yy2[i2j] = self->y1[i2j] = self->y2[i2j] = 0.0;
5595 }
5596 }
5597
5598 self->flag = 1;
5599 }
5600
5601 static void
5602 Vocoder_compute_variables(Vocoder *self, MYFLT base, MYFLT spread, MYFLT q)
5603 {
5604 int i;
5605 MYFLT w0, c, alpha, freq, invqfac;
5606
5607 invqfac = 1.0 / (2.0 * q);
5608
5609 for (i = 0; i < self->stages; i++)
5610 {
5611 freq = base * MYPOW(i + 1, spread);
5612
5613 if (freq <= 10)
5614 freq = 10.0;
5615 else if (freq >= self->nyquist)
5616 freq = self->nyquist;
5617
5618 w0 = self->twoPiOnSr * freq;
5619 c = MYCOS(w0);
5620 alpha = MYSIN(w0) * invqfac;
5621 self->b0[i] = alpha;
5622 self->b2[i] = -alpha;
5623 self->a0[i] = 1.0 / (1.0 + alpha); /* Inversed to multiply */
5624 self->a1[i] = -2.0 * c;
5625 self->a2[i] = 1.0 - alpha;
5626 }
5627 }
5628
5629 static void
5630 Vocoder_filters_iii(Vocoder *self)
5631 {
5632 int i, j, j2;
5633 MYFLT vin, vout, vin2, vout2, w, w2, freq, spread, q, slope, output, amp;
5634 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5635 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
5636
5637 freq = PyFloat_AS_DOUBLE(self->freq);
5638 spread = PyFloat_AS_DOUBLE(self->spread);
5639 q = PyFloat_AS_DOUBLE(self->q);
5640
5641 if (q < 0.1)
5642 q = 0.1;
5643
5644 amp = q * 10.0;
5645
5646 if (self->modebuffer[5] == 0)
5647 slope = PyFloat_AS_DOUBLE(self->slope);
5648 else
5649 slope = Stream_getData((Stream *)self->slope_stream)[0];
5650
5651 if (slope < 0.0)
5652 slope = 0.0;
5653 else if (slope > 1.0)
5654 slope = 1.0;
5655
5656 if (slope != self->last_slope)
5657 {
5658 self->last_slope = slope;
5659 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
5660 }
5661
5662 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
5663 {
5664 self->last_freq = freq;
5665 self->last_spread = spread;
5666 self->last_q = q;
5667 self->last_stages = self->stages;
5668 self->flag = 0;
5669 Vocoder_compute_variables(self, freq, spread, q);
5670 }
5671
5672 for (i = 0; i < self->bufsize; i++)
5673 {
5674 output = 0.0;
5675 vin = in[i];
5676 vin2 = in2[i];
5677
5678 for (j = 0; j < self->stages; j++)
5679 {
5680 j2 = j * 2;
5681 /* Analysis part filter 1 */
5682 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5683 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5684 self->y2[j2] = self->y1[j2];
5685 self->y1[j2] = w;
5686
5687 /* Exciter part filter 1 */
5688 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5689 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5690 self->yy2[j2] = self->yy1[j2];
5691 self->yy1[j2] = w2;
5692
5693 j2++;
5694 /* Analysis part filter 2 */
5695 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5696 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5697 self->y2[j2] = self->y1[j2];
5698 self->y1[j2] = w;
5699
5700 /* Exciter part filter 2 */
5701 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5702 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5703 self->yy2[j2] = self->yy1[j2];
5704 self->yy1[j2] = w2;
5705
5706 /* Follower */
5707 if (vout < 0.0)
5708 vout = -vout;
5709
5710 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
5711 output += vout2 * self->follow[j];
5712 }
5713
5714 self->data[i] = output * amp;
5715 }
5716 }
5717
5718 static void
5719 Vocoder_filters_aii(Vocoder *self)
5720 {
5721 int i, j, j2;
5722 int count = 0, maxcount = self->bufsize / 4;
5723 MYFLT vin, vout, vin2, vout2, w, w2, spread, q, slope, output, amp;
5724 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5725 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
5726
5727 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
5728 MYFLT freq = fr[0];
5729 spread = PyFloat_AS_DOUBLE(self->spread);
5730 q = PyFloat_AS_DOUBLE(self->q);
5731
5732 if (q < 0.1)
5733 q = 0.1;
5734
5735 amp = q * 10.0;
5736
5737 if (self->modebuffer[5] == 0)
5738 slope = PyFloat_AS_DOUBLE(self->slope);
5739 else
5740 slope = Stream_getData((Stream *)self->slope_stream)[0];
5741
5742 if (slope < 0.0)
5743 slope = 0.0;
5744 else if (slope > 1.0)
5745 slope = 1.0;
5746
5747 if (slope != self->last_slope)
5748 {
5749 self->last_slope = slope;
5750 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
5751 }
5752
5753 for (i = 0; i < self->bufsize; i++)
5754 {
5755 if (count == 0)
5756 freq = fr[i];
5757 else if (count >= maxcount)
5758 count = 0;
5759
5760 count++;
5761
5762 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
5763 {
5764 self->last_freq = freq;
5765 self->last_spread = spread;
5766 self->last_q = q;
5767 self->last_stages = self->stages;
5768 self->flag = 0;
5769 Vocoder_compute_variables(self, freq, spread, q);
5770 }
5771
5772 output = 0.0;
5773 vin = in[i];
5774 vin2 = in2[i];
5775
5776 for (j = 0; j < self->stages; j++)
5777 {
5778 j2 = j * 2;
5779 /* Analysis part filter 1 */
5780 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5781 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5782 self->y2[j2] = self->y1[j2];
5783 self->y1[j2] = w;
5784
5785 /* Exciter part filter 1 */
5786 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5787 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5788 self->yy2[j2] = self->yy1[j2];
5789 self->yy1[j2] = w2;
5790
5791 j2++;
5792 /* Analysis part filter 2 */
5793 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5794 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5795 self->y2[j2] = self->y1[j2];
5796 self->y1[j2] = w;
5797
5798 /* Exciter part filter 2 */
5799 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5800 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5801 self->yy2[j2] = self->yy1[j2];
5802 self->yy1[j2] = w2;
5803
5804 /* Follower */
5805 if (vout < 0.0)
5806 vout = -vout;
5807
5808 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
5809 output += vout2 * self->follow[j];
5810 }
5811
5812 self->data[i] = output * amp;
5813 }
5814 }
5815
5816 static void
5817 Vocoder_filters_iai(Vocoder *self)
5818 {
5819 int i, j, j2;
5820 int count = 0, maxcount = self->bufsize / 4;
5821 MYFLT vin, vout, vin2, vout2, w, w2, freq, q, slope, output, amp;
5822 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5823 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
5824
5825 freq = PyFloat_AS_DOUBLE(self->freq);
5826 MYFLT *sprd = Stream_getData((Stream *)self->spread_stream);
5827 MYFLT spread = sprd[0];
5828 q = PyFloat_AS_DOUBLE(self->q);
5829
5830 if (q < 0.1)
5831 q = 0.1;
5832
5833 amp = q * 10.0;
5834
5835 if (self->modebuffer[5] == 0)
5836 slope = PyFloat_AS_DOUBLE(self->slope);
5837 else
5838 slope = Stream_getData((Stream *)self->slope_stream)[0];
5839
5840 if (slope < 0.0)
5841 slope = 0.0;
5842 else if (slope > 1.0)
5843 slope = 1.0;
5844
5845 if (slope != self->last_slope)
5846 {
5847 self->last_slope = slope;
5848 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
5849 }
5850
5851 for (i = 0; i < self->bufsize; i++)
5852 {
5853 if (count == 0)
5854 spread = sprd[i];
5855 else if (count >= maxcount)
5856 count = 0;
5857
5858 count++;
5859
5860 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
5861 {
5862 self->last_freq = freq;
5863 self->last_spread = spread;
5864 self->last_q = q;
5865 self->last_stages = self->stages;
5866 self->flag = 0;
5867 Vocoder_compute_variables(self, freq, spread, q);
5868 }
5869
5870 output = 0.0;
5871 vin = in[i];
5872 vin2 = in2[i];
5873
5874 for (j = 0; j < self->stages; j++)
5875 {
5876 j2 = j * 2;
5877 /* Analysis part filter 1 */
5878 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5879 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5880 self->y2[j2] = self->y1[j2];
5881 self->y1[j2] = w;
5882
5883 /* Exciter part filter 1 */
5884 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5885 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5886 self->yy2[j2] = self->yy1[j2];
5887 self->yy1[j2] = w2;
5888
5889 j2++;
5890 /* Analysis part filter 2 */
5891 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5892 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5893 self->y2[j2] = self->y1[j2];
5894 self->y1[j2] = w;
5895
5896 /* Exciter part filter 2 */
5897 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5898 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5899 self->yy2[j2] = self->yy1[j2];
5900 self->yy1[j2] = w2;
5901
5902 /* Follower */
5903 if (vout < 0.0)
5904 vout = -vout;
5905
5906 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
5907 output += vout2 * self->follow[j];
5908 }
5909
5910 self->data[i] = output * amp;
5911 }
5912 }
5913
5914 static void
5915 Vocoder_filters_aai(Vocoder *self)
5916 {
5917 int i, j, j2;
5918 int count = 0, maxcount = self->bufsize / 4;
5919 MYFLT vin, vout, vin2, vout2, w, w2, q, slope, output, amp;
5920 MYFLT *in = Stream_getData((Stream *)self->input_stream);
5921 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
5922
5923 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
5924 MYFLT freq = fr[0];
5925 MYFLT *sprd = Stream_getData((Stream *)self->spread_stream);
5926 MYFLT spread = sprd[0];
5927 q = PyFloat_AS_DOUBLE(self->q);
5928
5929 if (q < 0.1)
5930 q = 0.1;
5931
5932 amp = q * 10.0;
5933
5934 if (self->modebuffer[5] == 0)
5935 slope = PyFloat_AS_DOUBLE(self->slope);
5936 else
5937 slope = Stream_getData((Stream *)self->slope_stream)[0];
5938
5939 if (slope < 0.0)
5940 slope = 0.0;
5941 else if (slope > 1.0)
5942 slope = 1.0;
5943
5944 if (slope != self->last_slope)
5945 {
5946 self->last_slope = slope;
5947 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
5948 }
5949
5950 for (i = 0; i < self->bufsize; i++)
5951 {
5952 if (count == 0)
5953 {
5954 freq = fr[i];
5955 spread = sprd[i];
5956 }
5957 else if (count >= maxcount)
5958 count = 0;
5959
5960 count++;
5961
5962 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
5963 {
5964 self->last_freq = freq;
5965 self->last_spread = spread;
5966 self->last_q = q;
5967 self->last_stages = self->stages;
5968 self->flag = 0;
5969 Vocoder_compute_variables(self, freq, spread, q);
5970 }
5971
5972 output = 0.0;
5973 vin = in[i];
5974 vin2 = in2[i];
5975
5976 for (j = 0; j < self->stages; j++)
5977 {
5978 j2 = j * 2;
5979 /* Analysis part filter 1 */
5980 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5981 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5982 self->y2[j2] = self->y1[j2];
5983 self->y1[j2] = w;
5984
5985 /* Exciter part filter 1 */
5986 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
5987 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
5988 self->yy2[j2] = self->yy1[j2];
5989 self->yy1[j2] = w2;
5990
5991 j2++;
5992 /* Analysis part filter 2 */
5993 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
5994 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
5995 self->y2[j2] = self->y1[j2];
5996 self->y1[j2] = w;
5997
5998 /* Exciter part filter 2 */
5999 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6000 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6001 self->yy2[j2] = self->yy1[j2];
6002 self->yy1[j2] = w2;
6003
6004 /* Follower */
6005 if (vout < 0.0)
6006 vout = -vout;
6007
6008 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
6009 output += vout2 * self->follow[j];
6010 }
6011
6012 self->data[i] = output * amp;
6013 }
6014 }
6015
6016 static void
6017 Vocoder_filters_iia(Vocoder *self)
6018 {
6019 int i, j, j2;
6020 int count = 0, maxcount = self->bufsize / 4;
6021 MYFLT vin, vout, vin2, vout2, w, w2, freq, spread, slope, output, amp = 1.0;
6022 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6023 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
6024
6025 freq = PyFloat_AS_DOUBLE(self->freq);
6026 spread = PyFloat_AS_DOUBLE(self->spread);
6027 MYFLT *qstr = Stream_getData((Stream *)self->q_stream);
6028 MYFLT q = qstr[0];
6029
6030 if (self->modebuffer[5] == 0)
6031 slope = PyFloat_AS_DOUBLE(self->slope);
6032 else
6033 slope = Stream_getData((Stream *)self->slope_stream)[0];
6034
6035 if (slope < 0.0)
6036 slope = 0.0;
6037 else if (slope > 1.0)
6038 slope = 1.0;
6039
6040 if (slope != self->last_slope)
6041 {
6042 self->last_slope = slope;
6043 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
6044 }
6045
6046 for (i = 0; i < self->bufsize; i++)
6047 {
6048 if (count == 0)
6049 {
6050 q = qstr[i];
6051
6052 if (q < 0.1)
6053 q = 0.1;
6054
6055 amp = q * 10.0;
6056 }
6057 else if (count >= maxcount)
6058 count = 0;
6059
6060 count++;
6061
6062 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
6063 {
6064 self->last_freq = freq;
6065 self->last_spread = spread;
6066 self->last_q = q;
6067 self->last_stages = self->stages;
6068 self->flag = 0;
6069 Vocoder_compute_variables(self, freq, spread, q);
6070 }
6071
6072 output = 0.0;
6073 vin = in[i];
6074 vin2 = in2[i];
6075
6076 for (j = 0; j < self->stages; j++)
6077 {
6078 j2 = j * 2;
6079 /* Analysis part filter 1 */
6080 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6081 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6082 self->y2[j2] = self->y1[j2];
6083 self->y1[j2] = w;
6084
6085 /* Exciter part filter 1 */
6086 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6087 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6088 self->yy2[j2] = self->yy1[j2];
6089 self->yy1[j2] = w2;
6090
6091 j2++;
6092 /* Analysis part filter 2 */
6093 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6094 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6095 self->y2[j2] = self->y1[j2];
6096 self->y1[j2] = w;
6097
6098 /* Exciter part filter 2 */
6099 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6100 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6101 self->yy2[j2] = self->yy1[j2];
6102 self->yy1[j2] = w2;
6103
6104 /* Follower */
6105 if (vout < 0.0)
6106 vout = -vout;
6107
6108 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
6109 output += vout2 * self->follow[j];
6110 }
6111
6112 self->data[i] = output * amp;
6113 }
6114 }
6115
6116 static void
6117 Vocoder_filters_aia(Vocoder *self)
6118 {
6119 int i, j, j2;
6120 int count = 0, maxcount = self->bufsize / 4;
6121 MYFLT vin, vout, vin2, vout2, w, w2, spread, slope, output, amp = 1.0;
6122 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6123 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
6124
6125 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
6126 MYFLT freq = fr[0];
6127 spread = PyFloat_AS_DOUBLE(self->spread);
6128 MYFLT *qstr = Stream_getData((Stream *)self->q_stream);
6129 MYFLT q = qstr[0];
6130
6131 if (self->modebuffer[5] == 0)
6132 slope = PyFloat_AS_DOUBLE(self->slope);
6133 else
6134 slope = Stream_getData((Stream *)self->slope_stream)[0];
6135
6136 if (slope < 0.0)
6137 slope = 0.0;
6138 else if (slope > 1.0)
6139 slope = 1.0;
6140
6141 if (slope != self->last_slope)
6142 {
6143 self->last_slope = slope;
6144 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
6145 }
6146
6147 for (i = 0; i < self->bufsize; i++)
6148 {
6149 if (count == 0)
6150 {
6151 freq = fr[i];
6152 q = qstr[i];
6153
6154 if (q < 0.1)
6155 q = 0.1;
6156
6157 amp = q * 10.0;
6158 }
6159 else if (count >= maxcount)
6160 count = 0;
6161
6162 count++;
6163
6164 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
6165 {
6166 self->last_freq = freq;
6167 self->last_spread = spread;
6168 self->last_q = q;
6169 self->last_stages = self->stages;
6170 self->flag = 0;
6171 Vocoder_compute_variables(self, freq, spread, q);
6172 }
6173
6174 output = 0.0;
6175 vin = in[i];
6176 vin2 = in2[i];
6177
6178 for (j = 0; j < self->stages; j++)
6179 {
6180 j2 = j * 2;
6181 /* Analysis part filter 1 */
6182 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6183 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6184 self->y2[j2] = self->y1[j2];
6185 self->y1[j2] = w;
6186
6187 /* Exciter part filter 1 */
6188 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6189 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6190 self->yy2[j2] = self->yy1[j2];
6191 self->yy1[j2] = w2;
6192
6193 j2++;
6194 /* Analysis part filter 2 */
6195 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6196 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6197 self->y2[j2] = self->y1[j2];
6198 self->y1[j2] = w;
6199
6200 /* Exciter part filter 2 */
6201 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6202 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6203 self->yy2[j2] = self->yy1[j2];
6204 self->yy1[j2] = w2;
6205
6206 /* Follower */
6207 if (vout < 0.0)
6208 vout = -vout;
6209
6210 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
6211 output += vout2 * self->follow[j];
6212 }
6213
6214 self->data[i] = output * amp;
6215 }
6216 }
6217
6218 static void
6219 Vocoder_filters_iaa(Vocoder *self)
6220 {
6221 int i, j, j2;
6222 int count = 0, maxcount = self->bufsize / 4;
6223 MYFLT vin, vout, vin2, vout2, w, w2, freq, slope, output, amp = 1.0;
6224 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6225 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
6226
6227 freq = PyFloat_AS_DOUBLE(self->freq);
6228 MYFLT *sprd = Stream_getData((Stream *)self->spread_stream);
6229 MYFLT spread = sprd[0];
6230 MYFLT *qstr = Stream_getData((Stream *)self->q_stream);
6231 MYFLT q = qstr[0];
6232
6233 if (self->modebuffer[5] == 0)
6234 slope = PyFloat_AS_DOUBLE(self->slope);
6235 else
6236 slope = Stream_getData((Stream *)self->slope_stream)[0];
6237
6238 if (slope < 0.0)
6239 slope = 0.0;
6240 else if (slope > 1.0)
6241 slope = 1.0;
6242
6243 if (slope != self->last_slope)
6244 {
6245 self->last_slope = slope;
6246 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 48.0) + 2.0)));
6247 }
6248
6249 for (i = 0; i < self->bufsize; i++)
6250 {
6251 if (count == 0)
6252 {
6253 spread = sprd[i];
6254 q = qstr[i];
6255
6256 if (q < 0.1)
6257 q = 0.1;
6258
6259 amp = q * 10.0;
6260 }
6261 else if (count >= maxcount)
6262 count = 0;
6263
6264 count++;
6265
6266 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
6267 {
6268 self->last_freq = freq;
6269 self->last_spread = spread;
6270 self->last_q = q;
6271 self->last_stages = self->stages;
6272 self->flag = 0;
6273 Vocoder_compute_variables(self, freq, spread, q);
6274 }
6275
6276 output = 0.0;
6277 vin = in[i];
6278 vin2 = in2[i];
6279
6280 for (j = 0; j < self->stages; j++)
6281 {
6282 j2 = j * 2;
6283 /* Analysis part filter 1 */
6284 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6285 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6286 self->y2[j2] = self->y1[j2];
6287 self->y1[j2] = w;
6288
6289 /* Exciter part filter 1 */
6290 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6291 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6292 self->yy2[j2] = self->yy1[j2];
6293 self->yy1[j2] = w2;
6294
6295 j2++;
6296 /* Analysis part filter 2 */
6297 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6298 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6299 self->y2[j2] = self->y1[j2];
6300 self->y1[j2] = w;
6301
6302 /* Exciter part filter 2 */
6303 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6304 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6305 self->yy2[j2] = self->yy1[j2];
6306 self->yy1[j2] = w2;
6307
6308 /* Follower */
6309 if (vout < 0.0)
6310 vout = -vout;
6311
6312 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
6313 output += vout2 * self->follow[j];
6314 }
6315
6316 self->data[i] = output * amp;
6317 }
6318 }
6319
6320 static void
6321 Vocoder_filters_aaa(Vocoder *self)
6322 {
6323 int i, j, j2;
6324 int count = 0, maxcount = self->bufsize / 4;
6325 MYFLT vin, vout, vin2, vout2, w, w2, slope, output, amp = 1.0;
6326 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6327 MYFLT *in2 = Stream_getData((Stream *)self->input2_stream);
6328
6329 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
6330 MYFLT freq = fr[0];
6331 MYFLT *sprd = Stream_getData((Stream *)self->spread_stream);
6332 MYFLT spread = sprd[0];
6333 MYFLT *qstr = Stream_getData((Stream *)self->q_stream);
6334 MYFLT q = qstr[0];
6335
6336 if (self->modebuffer[5] == 0)
6337 slope = PyFloat_AS_DOUBLE(self->slope);
6338 else
6339 slope = Stream_getData((Stream *)self->slope_stream)[0];
6340
6341 if (slope < 0.0)
6342 slope = 0.0;
6343 else if (slope > 1.0)
6344 slope = 1.0;
6345
6346 if (slope != self->last_slope)
6347 {
6348 self->last_slope = slope;
6349 self->factor = MYEXP(-1.0 / (self->sr / ((slope * 99.0) + 1.0)));
6350 }
6351
6352 for (i = 0; i < self->bufsize; i++)
6353 {
6354 if (count == 0)
6355 {
6356 freq = fr[i];
6357 spread = sprd[i];
6358 q = qstr[i];
6359
6360 if (q < 0.1)
6361 q = 0.1;
6362
6363 amp = q * 10.0;
6364 }
6365 else if (count >= maxcount)
6366 count = 0;
6367
6368 count++;
6369
6370 if (freq != self->last_freq || spread != self->last_spread || q != self->last_q || self->stages != self->last_stages || self->flag)
6371 {
6372 self->last_freq = freq;
6373 self->last_spread = spread;
6374 self->last_q = q;
6375 self->last_stages = self->stages;
6376 self->flag = 0;
6377 Vocoder_compute_variables(self, freq, spread, q);
6378 }
6379
6380 output = 0.0;
6381 vin = in[i];
6382 vin2 = in2[i];
6383
6384 for (j = 0; j < self->stages; j++)
6385 {
6386 j2 = j * 2;
6387 /* Analysis part filter 1 */
6388 w = ( vin - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6389 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6390 self->y2[j2] = self->y1[j2];
6391 self->y1[j2] = w;
6392
6393 /* Exciter part filter 1 */
6394 w2 = ( vin2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6395 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6396 self->yy2[j2] = self->yy1[j2];
6397 self->yy1[j2] = w2;
6398
6399 j2++;
6400 /* Analysis part filter 2 */
6401 w = ( vout - (self->a1[j] * self->y1[j2]) - (self->a2[j] * self->y2[j2]) ) * self->a0[j];
6402 vout = (self->b0[j] * w) + (self->b2[j] * self->y2[j2]);
6403 self->y2[j2] = self->y1[j2];
6404 self->y1[j2] = w;
6405
6406 /* Exciter part filter 2 */
6407 w2 = ( vout2 - (self->a1[j] * self->yy1[j2]) - (self->a2[j] * self->yy2[j2]) ) * self->a0[j];
6408 vout2 = (self->b0[j] * w2) + (self->b2[j] * self->yy2[j2]);
6409 self->yy2[j2] = self->yy1[j2];
6410 self->yy1[j2] = w2;
6411
6412 /* Follower */
6413 if (vout < 0.0)
6414 vout = -vout;
6415
6416 self->follow[j] = vout + self->factor * (self->follow[j] - vout);
6417 output += vout2 * self->follow[j];
6418 }
6419
6420 self->data[i] = output * amp;
6421 }
6422 }
6423
6424 static void Vocoder_postprocessing_ii(Vocoder *self) { POST_PROCESSING_II };
6425 static void Vocoder_postprocessing_ai(Vocoder *self) { POST_PROCESSING_AI };
6426 static void Vocoder_postprocessing_ia(Vocoder *self) { POST_PROCESSING_IA };
6427 static void Vocoder_postprocessing_aa(Vocoder *self) { POST_PROCESSING_AA };
6428 static void Vocoder_postprocessing_ireva(Vocoder *self) { POST_PROCESSING_IREVA };
6429 static void Vocoder_postprocessing_areva(Vocoder *self) { POST_PROCESSING_AREVA };
6430 static void Vocoder_postprocessing_revai(Vocoder *self) { POST_PROCESSING_REVAI };
6431 static void Vocoder_postprocessing_revaa(Vocoder *self) { POST_PROCESSING_REVAA };
6432 static void Vocoder_postprocessing_revareva(Vocoder *self) { POST_PROCESSING_REVAREVA };
6433
6434 static void
6435 Vocoder_setProcMode(Vocoder *self)
6436 {
6437 int procmode, muladdmode;
6438 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
6439 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
6440
6441 switch (procmode)
6442 {
6443 case 0:
6444 self->proc_func_ptr = Vocoder_filters_iii;
6445 break;
6446
6447 case 1:
6448 self->proc_func_ptr = Vocoder_filters_aii;
6449 break;
6450
6451 case 10:
6452 self->proc_func_ptr = Vocoder_filters_iai;
6453 break;
6454
6455 case 11:
6456 self->proc_func_ptr = Vocoder_filters_aai;
6457 break;
6458
6459 case 100:
6460 self->proc_func_ptr = Vocoder_filters_iia;
6461 break;
6462
6463 case 101:
6464 self->proc_func_ptr = Vocoder_filters_aia;
6465 break;
6466
6467 case 110:
6468 self->proc_func_ptr = Vocoder_filters_iaa;
6469 break;
6470
6471 case 111:
6472 self->proc_func_ptr = Vocoder_filters_aaa;
6473 break;
6474 }
6475
6476 switch (muladdmode)
6477 {
6478 case 0:
6479 self->muladd_func_ptr = Vocoder_postprocessing_ii;
6480 break;
6481
6482 case 1:
6483 self->muladd_func_ptr = Vocoder_postprocessing_ai;
6484 break;
6485
6486 case 2:
6487 self->muladd_func_ptr = Vocoder_postprocessing_revai;
6488 break;
6489
6490 case 10:
6491 self->muladd_func_ptr = Vocoder_postprocessing_ia;
6492 break;
6493
6494 case 11:
6495 self->muladd_func_ptr = Vocoder_postprocessing_aa;
6496 break;
6497
6498 case 12:
6499 self->muladd_func_ptr = Vocoder_postprocessing_revaa;
6500 break;
6501
6502 case 20:
6503 self->muladd_func_ptr = Vocoder_postprocessing_ireva;
6504 break;
6505
6506 case 21:
6507 self->muladd_func_ptr = Vocoder_postprocessing_areva;
6508 break;
6509
6510 case 22:
6511 self->muladd_func_ptr = Vocoder_postprocessing_revareva;
6512 break;
6513 }
6514 }
6515
6516 static void
6517 Vocoder_compute_next_data_frame(Vocoder *self)
6518 {
6519 (*self->proc_func_ptr)(self);
6520 (*self->muladd_func_ptr)(self);
6521 }
6522
6523 static int
6524 Vocoder_traverse(Vocoder *self, visitproc visit, void *arg)
6525 {
6526 pyo_VISIT
6527 Py_VISIT(self->input);
6528 Py_VISIT(self->input2);
6529 Py_VISIT(self->freq);
6530 Py_VISIT(self->spread);
6531 Py_VISIT(self->q);
6532 Py_VISIT(self->slope);
6533 return 0;
6534 }
6535
6536 static int
6537 Vocoder_clear(Vocoder *self)
6538 {
6539 pyo_CLEAR
6540 Py_CLEAR(self->input);
6541 Py_CLEAR(self->input2);
6542 Py_CLEAR(self->freq);
6543 Py_CLEAR(self->spread);
6544 Py_CLEAR(self->q);
6545 Py_CLEAR(self->slope);
6546 return 0;
6547 }
6548
6549 static void
6550 Vocoder_dealloc(Vocoder* self)
6551 {
6552 pyo_DEALLOC
6553 PyMem_RawFree(self->y1);
6554 PyMem_RawFree(self->y2);
6555 PyMem_RawFree(self->yy1);
6556 PyMem_RawFree(self->yy2);
6557 PyMem_RawFree(self->b0);
6558 PyMem_RawFree(self->b2);
6559 PyMem_RawFree(self->a0);
6560 PyMem_RawFree(self->a1);
6561 PyMem_RawFree(self->a2);
6562 PyMem_RawFree(self->follow);
6563 Vocoder_clear(self);
6564 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
6565 Py_TYPE(self)->tp_free((PyObject*)self);
6566 }
6567
6568 static PyObject *
6569 Vocoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6570 {
6571 int i;
6572 PyObject *inputtmp, *input_streamtmp, *input2tmp, *input2_streamtmp, *freqtmp = NULL, *spreadtmp = NULL, *qtmp = NULL, *slopetmp = NULL, *multmp = NULL, *addtmp = NULL;
6573 Vocoder *self;
6574 self = (Vocoder *)type->tp_alloc(type, 0);
6575
6576 self->freq = PyFloat_FromDouble(60);
6577 self->spread = PyFloat_FromDouble(1.25);
6578 self->q = PyFloat_FromDouble(20);
6579 self->slope = PyFloat_FromDouble(0.5);
6580 self->last_freq = self->last_spread = self->last_q = self->last_slope = -1.0;
6581 self->factor = 0.99;
6582 self->stages = 24;
6583 self->last_stages = -1;
6584 self->flag = 0;
6585 self->modebuffer[0] = 0;
6586 self->modebuffer[1] = 0;
6587 self->modebuffer[2] = 0;
6588 self->modebuffer[3] = 0;
6589 self->modebuffer[4] = 0;
6590 self->modebuffer[5] = 0;
6591
6592 INIT_OBJECT_COMMON
6593
6594 self->nyquist = (MYFLT)self->sr * 0.49;
6595 self->twoPiOnSr = (MYFLT)(TWOPI / self->sr);
6596
6597 Stream_setFunctionPtr(self->stream, Vocoder_compute_next_data_frame);
6598 self->mode_func_ptr = Vocoder_setProcMode;
6599
6600 static char *kwlist[] = {"input", "input2", "freq", "spread", "q", "slope", "stages", "mul", "add", NULL};
6601
6602 if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OOOOiOO", kwlist, &inputtmp, &input2tmp, &freqtmp, &spreadtmp, &qtmp, &slopetmp, &self->stages, &multmp, &addtmp))
6603 Py_RETURN_NONE;
6604
6605 INIT_INPUT_STREAM
6606
6607 if ( PyObject_HasAttrString((PyObject *)input2tmp, "server") == 0 )
6608 {
6609 PyErr_SetString(PyExc_TypeError, "\"input2\" argument of Vocoder must be a PyoObject.\n");
6610 Py_RETURN_NONE;
6611 }
6612
6613 self->input2 = input2tmp;
6614 Py_INCREF(self->input2);
6615 input2_streamtmp = PyObject_CallMethod((PyObject *)self->input2, "_getStream", NULL);
6616 self->input2_stream = (Stream *)input2_streamtmp;
6617 Py_INCREF(self->input2_stream);
6618
6619 if (freqtmp)
6620 {
6621 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
6622 }
6623
6624 if (spreadtmp)
6625 {
6626 PyObject_CallMethod((PyObject *)self, "setSpread", "O", spreadtmp);
6627 }
6628
6629 if (qtmp)
6630 {
6631 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
6632 }
6633
6634 if (slopetmp)
6635 {
6636 PyObject_CallMethod((PyObject *)self, "setSlope", "O", slopetmp);
6637 }
6638
6639 if (multmp)
6640 {
6641 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
6642 }
6643
6644 if (addtmp)
6645 {
6646 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
6647 }
6648
6649 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
6650
6651 Vocoder_allocate_memories(self);
6652
6653 (*self->mode_func_ptr)(self);
6654
6655 return (PyObject *)self;
6656 }
6657
6658 static PyObject * Vocoder_getServer(Vocoder* self) { GET_SERVER };
6659 static PyObject * Vocoder_getStream(Vocoder* self) { GET_STREAM };
6660 static PyObject * Vocoder_setMul(Vocoder *self, PyObject *arg) { SET_MUL };
6661 static PyObject * Vocoder_setAdd(Vocoder *self, PyObject *arg) { SET_ADD };
6662 static PyObject * Vocoder_setSub(Vocoder *self, PyObject *arg) { SET_SUB };
6663 static PyObject * Vocoder_setDiv(Vocoder *self, PyObject *arg) { SET_DIV };
6664
6665 static PyObject * Vocoder_play(Vocoder *self, PyObject *args, PyObject *kwds) { PLAY };
6666 static PyObject * Vocoder_out(Vocoder *self, PyObject *args, PyObject *kwds) { OUT };
6667 static PyObject * Vocoder_stop(Vocoder *self, PyObject *args, PyObject *kwds) { STOP };
6668
6669 static PyObject * Vocoder_multiply(Vocoder *self, PyObject *arg) { MULTIPLY };
6670 static PyObject * Vocoder_inplace_multiply(Vocoder *self, PyObject *arg) { INPLACE_MULTIPLY };
6671 static PyObject * Vocoder_add(Vocoder *self, PyObject *arg) { ADD };
6672 static PyObject * Vocoder_inplace_add(Vocoder *self, PyObject *arg) { INPLACE_ADD };
6673 static PyObject * Vocoder_sub(Vocoder *self, PyObject *arg) { SUB };
6674 static PyObject * Vocoder_inplace_sub(Vocoder *self, PyObject *arg) { INPLACE_SUB };
6675 static PyObject * Vocoder_div(Vocoder *self, PyObject *arg) { DIV };
6676 static PyObject * Vocoder_inplace_div(Vocoder *self, PyObject *arg) { INPLACE_DIV };
6677
6678 static PyObject * Vocoder_setFreq(Vocoder *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
6679 static PyObject * Vocoder_setSpread(Vocoder *self, PyObject *arg) { SET_PARAM(self->spread, self->spread_stream, 3); }
6680 static PyObject * Vocoder_setQ(Vocoder *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 4); }
6681 static PyObject * Vocoder_setSlope(Vocoder *self, PyObject *arg) { SET_PARAM(self->slope, self->slope_stream, 5); }
6682
6683 static PyObject *
6684 Vocoder_setStages(Vocoder *self, PyObject *arg)
6685 {
6686 ASSERT_ARG_NOT_NULL
6687
6688 if (PyLong_Check(arg))
6689 {
6690 self->stages = PyLong_AsLong(arg);
6691 Vocoder_allocate_memories(self);
6692 }
6693
6694 Py_RETURN_NONE;
6695 }
6696
6697 static PyMemberDef Vocoder_members[] =
6698 {
6699 {"server", T_OBJECT_EX, offsetof(Vocoder, server), 0, "Pyo server."},
6700 {"stream", T_OBJECT_EX, offsetof(Vocoder, stream), 0, "Stream object."},
6701 {"input", T_OBJECT_EX, offsetof(Vocoder, input), 0, "Spectral envelope signal."},
6702 {"input2", T_OBJECT_EX, offsetof(Vocoder, input2), 0, "Exciter signal."},
6703 {"freq", T_OBJECT_EX, offsetof(Vocoder, freq), 0, "Base frequency in cycle per second."},
6704 {"spread", T_OBJECT_EX, offsetof(Vocoder, spread), 0, "Frequency expansion factor."},
6705 {"q", T_OBJECT_EX, offsetof(Vocoder, q), 0, "Q factor."},
6706 {"slope", T_OBJECT_EX, offsetof(Vocoder, slope), 0, "Responsiveness of the follower lowpass filter."},
6707 {"mul", T_OBJECT_EX, offsetof(Vocoder, mul), 0, "Mul factor."},
6708 {"add", T_OBJECT_EX, offsetof(Vocoder, add), 0, "Add factor."},
6709 {NULL} /* Sentinel */
6710 };
6711
6712 static PyMethodDef Vocoder_methods[] =
6713 {
6714 {"getServer", (PyCFunction)Vocoder_getServer, METH_NOARGS, "Returns server object."},
6715 {"_getStream", (PyCFunction)Vocoder_getStream, METH_NOARGS, "Returns stream object."},
6716 {"play", (PyCFunction)Vocoder_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
6717 {"out", (PyCFunction)Vocoder_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
6718 {"stop", (PyCFunction)Vocoder_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
6719 {"setFreq", (PyCFunction)Vocoder_setFreq, METH_O, "Sets filter base frequency in cycle per second."},
6720 {"setSpread", (PyCFunction)Vocoder_setSpread, METH_O, "Sets frequency expansion factor."},
6721 {"setQ", (PyCFunction)Vocoder_setQ, METH_O, "Sets filter Q factor."},
6722 {"setSlope", (PyCFunction)Vocoder_setSlope, METH_O, "Sets responsiveness of the follower."},
6723 {"setStages", (PyCFunction)Vocoder_setStages, METH_O, "Sets the number of filtering stages."},
6724 {"setMul", (PyCFunction)Vocoder_setMul, METH_O, "Sets oscillator mul factor."},
6725 {"setAdd", (PyCFunction)Vocoder_setAdd, METH_O, "Sets oscillator add factor."},
6726 {"setSub", (PyCFunction)Vocoder_setSub, METH_O, "Sets inverse add factor."},
6727 {"setDiv", (PyCFunction)Vocoder_setDiv, METH_O, "Sets inverse mul factor."},
6728 {NULL} /* Sentinel */
6729 };
6730
6731 static PyNumberMethods Vocoder_as_number =
6732 {
6733 (binaryfunc)Vocoder_add, /*nb_add*/
6734 (binaryfunc)Vocoder_sub, /*nb_subtract*/
6735 (binaryfunc)Vocoder_multiply, /*nb_multiply*/
6736 0, /*nb_remainder*/
6737 0, /*nb_divmod*/
6738 0, /*nb_power*/
6739 0, /*nb_neg*/
6740 0, /*nb_pos*/
6741 0, /*(unaryfunc)array_abs,*/
6742 0, /*nb_nonzero*/
6743 0, /*nb_invert*/
6744 0, /*nb_lshift*/
6745 0, /*nb_rshift*/
6746 0, /*nb_and*/
6747 0, /*nb_xor*/
6748 0, /*nb_or*/
6749 0, /*nb_int*/
6750 0, /*nb_long*/
6751 0, /*nb_float*/
6752 (binaryfunc)Vocoder_inplace_add, /*inplace_add*/
6753 (binaryfunc)Vocoder_inplace_sub, /*inplace_subtract*/
6754 (binaryfunc)Vocoder_inplace_multiply, /*inplace_multiply*/
6755 0, /*inplace_remainder*/
6756 0, /*inplace_power*/
6757 0, /*inplace_lshift*/
6758 0, /*inplace_rshift*/
6759 0, /*inplace_and*/
6760 0, /*inplace_xor*/
6761 0, /*inplace_or*/
6762 0, /*nb_floor_divide*/
6763 (binaryfunc)Vocoder_div, /*nb_true_divide*/
6764 0, /*nb_inplace_floor_divide*/
6765 (binaryfunc)Vocoder_inplace_div, /*nb_inplace_true_divide*/
6766 0, /* nb_index */
6767 };
6768
6769 PyTypeObject VocoderType =
6770 {
6771 PyVarObject_HEAD_INIT(NULL, 0)
6772 "_pyo.Vocoder_base", /*tp_name*/
6773 sizeof(Vocoder), /*tp_basicsize*/
6774 0, /*tp_itemsize*/
6775 (destructor)Vocoder_dealloc, /*tp_dealloc*/
6776 0, /*tp_print*/
6777 0, /*tp_getattr*/
6778 0, /*tp_setattr*/
6779 0, /*tp_as_async (tp_compare in Python 2)*/
6780 0, /*tp_repr*/
6781 &Vocoder_as_number, /*tp_as_number*/
6782 0, /*tp_as_sequence*/
6783 0, /*tp_as_mapping*/
6784 0, /*tp_hash */
6785 0, /*tp_call*/
6786 0, /*tp_str*/
6787 0, /*tp_getattro*/
6788 0, /*tp_setattro*/
6789 0, /*tp_as_buffer*/
6790 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
6791 "Vocoder objects. Bank of bandpass filters implementing the vocoder effect.", /* tp_doc */
6792 (traverseproc)Vocoder_traverse, /* tp_traverse */
6793 (inquiry)Vocoder_clear, /* tp_clear */
6794 0, /* tp_richcompare */
6795 0, /* tp_weaklistoffset */
6796 0, /* tp_iter */
6797 0, /* tp_iternext */
6798 Vocoder_methods, /* tp_methods */
6799 Vocoder_members, /* tp_members */
6800 0, /* tp_getset */
6801 0, /* tp_base */
6802 0, /* tp_dict */
6803 0, /* tp_descr_get */
6804 0, /* tp_descr_set */
6805 0, /* tp_dictoffset */
6806 0, /* tp_init */
6807 0, /* tp_alloc */
6808 Vocoder_new, /* tp_new */
6809 };
6810
6811 typedef struct
6812 {
6813 pyo_audio_HEAD
6814 PyObject *input;
6815 Stream *input_stream;
6816 PyObject *freq;
6817 Stream *freq_stream;
6818 PyObject *q;
6819 Stream *q_stream;
6820 PyObject *type;
6821 Stream *type_stream;
6822 int modebuffer[5]; // need at least 2 slots for mul & add
6823 MYFLT srOverSix;
6824 MYFLT last_freq;
6825 MYFLT piOverSr;
6826 // sample memories
6827 MYFLT y1;
6828 MYFLT y2;
6829 MYFLT y3;
6830 MYFLT y4;
6831 // variables
6832 MYFLT w;
6833 } SVF;
6834
6835 static void
6836 SVF_filters_iii(SVF *self)
6837 {
6838 int i;
6839 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
6840 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6841 freq = PyFloat_AS_DOUBLE(self->freq);
6842 q = PyFloat_AS_DOUBLE(self->q);
6843 type = PyFloat_AS_DOUBLE(self->type);
6844
6845 if (freq < 0.1)
6846 freq = 0.1;
6847 else if (freq > self->srOverSix)
6848 freq = self->srOverSix;
6849
6850 if (freq != self->last_freq)
6851 {
6852 self->last_freq = freq;
6853 self->w = 2.0 * MYSIN(freq * self->piOverSr);
6854 }
6855
6856 if (q < 0.5)
6857 q = 0.5;
6858
6859 q1 = 1.0 / q;
6860
6861 if (type < 0.0)
6862 type = 0.0;
6863 else if (type > 1.0)
6864 type = 1.0;
6865
6866 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
6867 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
6868 bandgain = (type <= 0.5) ? type : (1.0 - type);
6869
6870 for (i = 0; i < self->bufsize; i++)
6871 {
6872 low = self->y2 + self->w * self->y1;
6873 high = in[i] - low - q1 * self->y1;
6874 band = self->w * high + self->y1;
6875 self->y1 = band;
6876 self->y2 = low;
6877 val = low * lowgain + high * highgain + band * bandgain;
6878 low = self->y4 + self->w * self->y3;
6879 high = val - low - q1 * self->y3;
6880 band = self->w * high + self->y3;
6881 self->y3 = band;
6882 self->y4 = low;
6883 self->data[i] = low * lowgain + high * highgain + band * bandgain;
6884 }
6885 }
6886
6887 static void
6888 SVF_filters_aii(SVF *self)
6889 {
6890 int i;
6891 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
6892 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6893 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
6894 q = PyFloat_AS_DOUBLE(self->q);
6895 type = PyFloat_AS_DOUBLE(self->type);
6896
6897 if (q < 0.5)
6898 q = 0.5;
6899
6900 q1 = 1.0 / q;
6901
6902 if (type < 0.0)
6903 type = 0.0;
6904 else if (type > 1.0)
6905 type = 1.0;
6906
6907 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
6908 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
6909 bandgain = (type <= 0.5) ? type : (1.0 - type);
6910
6911 for (i = 0; i < self->bufsize; i++)
6912 {
6913 freq = fr[i];
6914
6915 if (freq < 0.1)
6916 freq = 0.1;
6917 else if (freq > self->srOverSix)
6918 freq = self->srOverSix;
6919
6920 if (freq != self->last_freq)
6921 {
6922 self->last_freq = freq;
6923 self->w = 2.0 * MYSIN(freq * self->piOverSr);
6924 }
6925
6926 low = self->y2 + self->w * self->y1;
6927 high = in[i] - low - q1 * self->y1;
6928 band = self->w * high + self->y1;
6929 self->y1 = band;
6930 self->y2 = low;
6931 val = low * lowgain + high * highgain + band * bandgain;
6932 low = self->y4 + self->w * self->y3;
6933 high = val - low - q1 * self->y3;
6934 band = self->w * high + self->y3;
6935 self->y3 = band;
6936 self->y4 = low;
6937 self->data[i] = low * lowgain + high * highgain + band * bandgain;
6938 }
6939 }
6940
6941 static void
6942 SVF_filters_iai(SVF *self)
6943 {
6944 int i;
6945 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
6946 MYFLT *in = Stream_getData((Stream *)self->input_stream);
6947 freq = PyFloat_AS_DOUBLE(self->freq);
6948 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
6949 type = PyFloat_AS_DOUBLE(self->type);
6950
6951 if (freq < 0.1)
6952 freq = 0.1;
6953 else if (freq > self->srOverSix)
6954 freq = self->srOverSix;
6955
6956 if (freq != self->last_freq)
6957 {
6958 self->last_freq = freq;
6959 self->w = 2.0 * MYSIN(freq * self->piOverSr);
6960 }
6961
6962 if (type < 0.0)
6963 type = 0.0;
6964 else if (type > 1.0)
6965 type = 1.0;
6966
6967 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
6968 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
6969 bandgain = (type <= 0.5) ? type : (1.0 - type);
6970
6971 for (i = 0; i < self->bufsize; i++)
6972 {
6973 q = qst[i];
6974
6975 if (q < 0.5)
6976 q = 0.5;
6977
6978 q1 = 1.0 / q;
6979 low = self->y2 + self->w * self->y1;
6980 high = in[i] - low - q1 * self->y1;
6981 band = self->w * high + self->y1;
6982 self->y1 = band;
6983 self->y2 = low;
6984 val = low * lowgain + high * highgain + band * bandgain;
6985 low = self->y4 + self->w * self->y3;
6986 high = val - low - q1 * self->y3;
6987 band = self->w * high + self->y3;
6988 self->y3 = band;
6989 self->y4 = low;
6990 self->data[i] = low * lowgain + high * highgain + band * bandgain;
6991 }
6992 }
6993
6994 static void
6995 SVF_filters_aai(SVF *self)
6996 {
6997 int i;
6998 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
6999 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7000 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
7001 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
7002 type = PyFloat_AS_DOUBLE(self->type);
7003
7004 if (type < 0.0)
7005 type = 0.0;
7006 else if (type > 1.0)
7007 type = 1.0;
7008
7009 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
7010 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
7011 bandgain = (type <= 0.5) ? type : (1.0 - type);
7012
7013 for (i = 0; i < self->bufsize; i++)
7014 {
7015 freq = fr[i];
7016 q = qst[i];
7017
7018 if (freq < 0.1)
7019 freq = 0.1;
7020 else if (freq > self->srOverSix)
7021 freq = self->srOverSix;
7022
7023 if (freq != self->last_freq)
7024 {
7025 self->last_freq = freq;
7026 self->w = 2.0 * MYSIN(freq * self->piOverSr);
7027 }
7028
7029 if (q < 0.5)
7030 q = 0.5;
7031
7032 q1 = 1.0 / q;
7033 low = self->y2 + self->w * self->y1;
7034 high = in[i] - low - q1 * self->y1;
7035 band = self->w * high + self->y1;
7036 self->y1 = band;
7037 self->y2 = low;
7038 val = low * lowgain + high * highgain + band * bandgain;
7039 low = self->y4 + self->w * self->y3;
7040 high = val - low - q1 * self->y3;
7041 band = self->w * high + self->y3;
7042 self->y3 = band;
7043 self->y4 = low;
7044 self->data[i] = low * lowgain + high * highgain + band * bandgain;
7045 }
7046 }
7047
7048 static void
7049 SVF_filters_iia(SVF *self)
7050 {
7051 int i;
7052 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
7053 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7054 freq = PyFloat_AS_DOUBLE(self->freq);
7055 q = PyFloat_AS_DOUBLE(self->q);
7056 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7057
7058 if (freq < 0.1)
7059 freq = 0.1;
7060 else if (freq > self->srOverSix)
7061 freq = self->srOverSix;
7062
7063 if (freq != self->last_freq)
7064 {
7065 self->last_freq = freq;
7066 self->w = 2.0 * MYSIN(freq * self->piOverSr);
7067 }
7068
7069 if (q < 0.5)
7070 q = 0.5;
7071
7072 q1 = 1.0 / q;
7073
7074 for (i = 0; i < self->bufsize; i++)
7075 {
7076 type = tp[i];
7077
7078 if (type < 0.0)
7079 type = 0.0;
7080 else if (type > 1.0)
7081 type = 1.0;
7082
7083 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
7084 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
7085 bandgain = (type <= 0.5) ? type : (1.0 - type);
7086 low = self->y2 + self->w * self->y1;
7087 high = in[i] - low - q1 * self->y1;
7088 band = self->w * high + self->y1;
7089 self->y1 = band;
7090 self->y2 = low;
7091 val = low * lowgain + high * highgain + band * bandgain;
7092 low = self->y4 + self->w * self->y3;
7093 high = val - low - q1 * self->y3;
7094 band = self->w * high + self->y3;
7095 self->y3 = band;
7096 self->y4 = low;
7097 self->data[i] = low * lowgain + high * highgain + band * bandgain;
7098 }
7099 }
7100
7101 static void
7102 SVF_filters_aia(SVF *self)
7103 {
7104 int i;
7105 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
7106 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7107 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
7108 q = PyFloat_AS_DOUBLE(self->q);
7109 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7110
7111 if (q < 0.5)
7112 q = 0.5;
7113
7114 q1 = 1.0 / q;
7115
7116 for (i = 0; i < self->bufsize; i++)
7117 {
7118 freq = fr[i];
7119 type = tp[i];
7120
7121 if (freq < 0.1)
7122 freq = 0.1;
7123 else if (freq > self->srOverSix)
7124 freq = self->srOverSix;
7125
7126 if (freq != self->last_freq)
7127 {
7128 self->last_freq = freq;
7129 self->w = 2.0 * MYSIN(freq * self->piOverSr);
7130 }
7131
7132 if (type < 0.0)
7133 type = 0.0;
7134 else if (type > 1.0)
7135 type = 1.0;
7136
7137 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
7138 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
7139 bandgain = (type <= 0.5) ? type : (1.0 - type);
7140 low = self->y2 + self->w * self->y1;
7141 high = in[i] - low - q1 * self->y1;
7142 band = self->w * high + self->y1;
7143 self->y1 = band;
7144 self->y2 = low;
7145 val = low * lowgain + high * highgain + band * bandgain;
7146 low = self->y4 + self->w * self->y3;
7147 high = val - low - q1 * self->y3;
7148 band = self->w * high + self->y3;
7149 self->y3 = band;
7150 self->y4 = low;
7151 self->data[i] = low * lowgain + high * highgain + band * bandgain;
7152 }
7153 }
7154
7155 static void
7156 SVF_filters_iaa(SVF *self)
7157 {
7158 int i;
7159 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
7160 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7161 freq = PyFloat_AS_DOUBLE(self->freq);
7162 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
7163 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7164
7165 if (freq < 0.1)
7166 freq = 0.1;
7167 else if (freq > self->srOverSix)
7168 freq = self->srOverSix;
7169
7170 if (freq != self->last_freq)
7171 {
7172 self->last_freq = freq;
7173 self->w = 2.0 * MYSIN(freq * self->piOverSr);
7174 }
7175
7176 for (i = 0; i < self->bufsize; i++)
7177 {
7178 q = qst[i];
7179 type = tp[i];
7180
7181 if (q < 0.5)
7182 q = 0.5;
7183
7184 q1 = 1.0 / q;
7185
7186 if (type < 0.0)
7187 type = 0.0;
7188 else if (type > 1.0)
7189 type = 1.0;
7190
7191 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
7192 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
7193 bandgain = (type <= 0.5) ? type : (1.0 - type);
7194 low = self->y2 + self->w * self->y1;
7195 high = in[i] - low - q1 * self->y1;
7196 band = self->w * high + self->y1;
7197 self->y1 = band;
7198 self->y2 = low;
7199 val = low * lowgain + high * highgain + band * bandgain;
7200 low = self->y4 + self->w * self->y3;
7201 high = val - low - q1 * self->y3;
7202 band = self->w * high + self->y3;
7203 self->y3 = band;
7204 self->y4 = low;
7205 self->data[i] = low * lowgain + high * highgain + band * bandgain;
7206 }
7207 }
7208
7209 static void
7210 SVF_filters_aaa(SVF *self)
7211 {
7212 int i;
7213 MYFLT val, freq, q, type, q1, low, high, band, lowgain, highgain, bandgain;
7214 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7215 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
7216 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
7217 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7218
7219 for (i = 0; i < self->bufsize; i++)
7220 {
7221 freq = fr[i];
7222 q = qst[i];
7223 type = tp[i];
7224
7225 if (freq < 0.1)
7226 freq = 0.1;
7227 else if (freq > self->srOverSix)
7228 freq = self->srOverSix;
7229
7230 if (freq != self->last_freq)
7231 {
7232 self->last_freq = freq;
7233 self->w = 2.0 * MYSIN(freq * self->piOverSr);
7234 }
7235
7236 if (q < 0.5)
7237 q = 0.5;
7238
7239 q1 = 1.0 / q;
7240
7241 if (type < 0.0)
7242 type = 0.0;
7243 else if (type > 1.0)
7244 type = 1.0;
7245
7246 lowgain = (type <= 0.5) ? (0.5 - type) : 0.0;
7247 highgain = (type >= 0.5) ? (type - 0.5) : 0.0;
7248 bandgain = (type <= 0.5) ? type : (1.0 - type);
7249 low = self->y2 + self->w * self->y1;
7250 high = in[i] - low - q1 * self->y1;
7251 band = self->w * high + self->y1;
7252 self->y1 = band;
7253 self->y2 = low;
7254 val = low * lowgain + high * highgain + band * bandgain;
7255 low = self->y4 + self->w * self->y3;
7256 high = val - low - q1 * self->y3;
7257 band = self->w * high + self->y3;
7258 self->y3 = band;
7259 self->y4 = low;
7260 self->data[i] = low * lowgain + high * highgain + band * bandgain;
7261 }
7262 }
7263
7264 static void SVF_postprocessing_ii(SVF *self) { POST_PROCESSING_II };
7265 static void SVF_postprocessing_ai(SVF *self) { POST_PROCESSING_AI };
7266 static void SVF_postprocessing_ia(SVF *self) { POST_PROCESSING_IA };
7267 static void SVF_postprocessing_aa(SVF *self) { POST_PROCESSING_AA };
7268 static void SVF_postprocessing_ireva(SVF *self) { POST_PROCESSING_IREVA };
7269 static void SVF_postprocessing_areva(SVF *self) { POST_PROCESSING_AREVA };
7270 static void SVF_postprocessing_revai(SVF *self) { POST_PROCESSING_REVAI };
7271 static void SVF_postprocessing_revaa(SVF *self) { POST_PROCESSING_REVAA };
7272 static void SVF_postprocessing_revareva(SVF *self) { POST_PROCESSING_REVAREVA };
7273
7274 static void
7275 SVF_setProcMode(SVF *self)
7276 {
7277 int procmode, muladdmode;
7278 procmode = self->modebuffer[2] + self->modebuffer[3] * 10 + self->modebuffer[4] * 100;
7279 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
7280
7281 switch (procmode)
7282 {
7283 case 0:
7284 self->proc_func_ptr = SVF_filters_iii;
7285 break;
7286
7287 case 1:
7288 self->proc_func_ptr = SVF_filters_aii;
7289 break;
7290
7291 case 10:
7292 self->proc_func_ptr = SVF_filters_iai;
7293 break;
7294
7295 case 11:
7296 self->proc_func_ptr = SVF_filters_aai;
7297 break;
7298
7299 case 100:
7300 self->proc_func_ptr = SVF_filters_iia;
7301 break;
7302
7303 case 101:
7304 self->proc_func_ptr = SVF_filters_aia;
7305 break;
7306
7307 case 110:
7308 self->proc_func_ptr = SVF_filters_iaa;
7309 break;
7310
7311 case 111:
7312 self->proc_func_ptr = SVF_filters_aaa;
7313 break;
7314 }
7315
7316 switch (muladdmode)
7317 {
7318 case 0:
7319 self->muladd_func_ptr = SVF_postprocessing_ii;
7320 break;
7321
7322 case 1:
7323 self->muladd_func_ptr = SVF_postprocessing_ai;
7324 break;
7325
7326 case 2:
7327 self->muladd_func_ptr = SVF_postprocessing_revai;
7328 break;
7329
7330 case 10:
7331 self->muladd_func_ptr = SVF_postprocessing_ia;
7332 break;
7333
7334 case 11:
7335 self->muladd_func_ptr = SVF_postprocessing_aa;
7336 break;
7337
7338 case 12:
7339 self->muladd_func_ptr = SVF_postprocessing_revaa;
7340 break;
7341
7342 case 20:
7343 self->muladd_func_ptr = SVF_postprocessing_ireva;
7344 break;
7345
7346 case 21:
7347 self->muladd_func_ptr = SVF_postprocessing_areva;
7348 break;
7349
7350 case 22:
7351 self->muladd_func_ptr = SVF_postprocessing_revareva;
7352 break;
7353 }
7354 }
7355
7356 static void
7357 SVF_compute_next_data_frame(SVF *self)
7358 {
7359 (*self->proc_func_ptr)(self);
7360 (*self->muladd_func_ptr)(self);
7361 }
7362
7363 static int
7364 SVF_traverse(SVF *self, visitproc visit, void *arg)
7365 {
7366 pyo_VISIT
7367 Py_VISIT(self->input);
7368 Py_VISIT(self->freq);
7369 Py_VISIT(self->q);
7370 Py_VISIT(self->type);
7371 return 0;
7372 }
7373
7374 static int
7375 SVF_clear(SVF *self)
7376 {
7377 pyo_CLEAR
7378 Py_CLEAR(self->input);
7379 Py_CLEAR(self->freq);
7380 Py_CLEAR(self->q);
7381 Py_CLEAR(self->type);
7382 return 0;
7383 }
7384
7385 static void
7386 SVF_dealloc(SVF* self)
7387 {
7388 pyo_DEALLOC
7389 SVF_clear(self);
7390 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
7391 Py_TYPE(self)->tp_free((PyObject*)self);
7392 }
7393
7394 static PyObject *
7395 SVF_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
7396 {
7397 int i;
7398 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *typetmp = NULL, *multmp = NULL, *addtmp = NULL;
7399 SVF *self;
7400 self = (SVF *)type->tp_alloc(type, 0);
7401
7402 self->freq = PyFloat_FromDouble(1000);
7403 self->q = PyFloat_FromDouble(1);
7404 self->type = PyFloat_FromDouble(0);
7405 self->modebuffer[0] = 0;
7406 self->modebuffer[1] = 0;
7407 self->modebuffer[2] = 0;
7408 self->modebuffer[3] = 0;
7409 self->modebuffer[4] = 0;
7410 self->y1 = self->y2 = self->y3 = self->y4 = self->w = 0.0;
7411 self->last_freq = -1.0;
7412
7413 INIT_OBJECT_COMMON
7414
7415 self->srOverSix = (MYFLT)self->sr / 6.0;
7416 self->piOverSr = PI / self->sr;
7417
7418 Stream_setFunctionPtr(self->stream, SVF_compute_next_data_frame);
7419 self->mode_func_ptr = SVF_setProcMode;
7420
7421 static char *kwlist[] = {"input", "freq", "q", "type", "mul", "add", NULL};
7422
7423 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOO", kwlist, &inputtmp, &freqtmp, &qtmp, &typetmp, &multmp, &addtmp))
7424 Py_RETURN_NONE;
7425
7426 INIT_INPUT_STREAM
7427
7428 if (freqtmp)
7429 {
7430 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
7431 }
7432
7433 if (qtmp)
7434 {
7435 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
7436 }
7437
7438 if (typetmp)
7439 {
7440 PyObject_CallMethod((PyObject *)self, "setType", "O", typetmp);
7441 }
7442
7443 if (multmp)
7444 {
7445 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
7446 }
7447
7448 if (addtmp)
7449 {
7450 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
7451 }
7452
7453 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
7454
7455 (*self->mode_func_ptr)(self);
7456
7457 return (PyObject *)self;
7458 }
7459
7460 static PyObject * SVF_getServer(SVF* self) { GET_SERVER };
7461 static PyObject * SVF_getStream(SVF* self) { GET_STREAM };
7462 static PyObject * SVF_setMul(SVF *self, PyObject *arg) { SET_MUL };
7463 static PyObject * SVF_setAdd(SVF *self, PyObject *arg) { SET_ADD };
7464 static PyObject * SVF_setSub(SVF *self, PyObject *arg) { SET_SUB };
7465 static PyObject * SVF_setDiv(SVF *self, PyObject *arg) { SET_DIV };
7466
7467 static PyObject * SVF_play(SVF *self, PyObject *args, PyObject *kwds) { PLAY };
7468 static PyObject * SVF_out(SVF *self, PyObject *args, PyObject *kwds) { OUT };
7469 static PyObject * SVF_stop(SVF *self, PyObject *args, PyObject *kwds) { STOP };
7470
7471 static PyObject * SVF_multiply(SVF *self, PyObject *arg) { MULTIPLY };
7472 static PyObject * SVF_inplace_multiply(SVF *self, PyObject *arg) { INPLACE_MULTIPLY };
7473 static PyObject * SVF_add(SVF *self, PyObject *arg) { ADD };
7474 static PyObject * SVF_inplace_add(SVF *self, PyObject *arg) { INPLACE_ADD };
7475 static PyObject * SVF_sub(SVF *self, PyObject *arg) { SUB };
7476 static PyObject * SVF_inplace_sub(SVF *self, PyObject *arg) { INPLACE_SUB };
7477 static PyObject * SVF_div(SVF *self, PyObject *arg) { DIV };
7478 static PyObject * SVF_inplace_div(SVF *self, PyObject *arg) { INPLACE_DIV };
7479
7480 static PyObject * SVF_setFreq(SVF *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
7481 static PyObject * SVF_setQ(SVF *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
7482 static PyObject * SVF_setType(SVF *self, PyObject *arg) { SET_PARAM(self->type, self->type_stream, 4); }
7483
7484 static PyMemberDef SVF_members[] =
7485 {
7486 {"server", T_OBJECT_EX, offsetof(SVF, server), 0, "Pyo server."},
7487 {"stream", T_OBJECT_EX, offsetof(SVF, stream), 0, "Stream object."},
7488 {"input", T_OBJECT_EX, offsetof(SVF, input), 0, "Input sound object."},
7489 {"freq", T_OBJECT_EX, offsetof(SVF, freq), 0, "Cutoff frequency in cycle per second."},
7490 {"q", T_OBJECT_EX, offsetof(SVF, q), 0, "Q factor."},
7491 {"type", T_OBJECT_EX, offsetof(SVF, type), 0, "Filter type."},
7492 {"mul", T_OBJECT_EX, offsetof(SVF, mul), 0, "Mul factor."},
7493 {"add", T_OBJECT_EX, offsetof(SVF, add), 0, "Add factor."},
7494 {NULL} /* Sentinel */
7495 };
7496
7497 static PyMethodDef SVF_methods[] =
7498 {
7499 {"getServer", (PyCFunction)SVF_getServer, METH_NOARGS, "Returns server object."},
7500 {"_getStream", (PyCFunction)SVF_getStream, METH_NOARGS, "Returns stream object."},
7501 {"play", (PyCFunction)SVF_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
7502 {"out", (PyCFunction)SVF_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
7503 {"stop", (PyCFunction)SVF_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
7504 {"setFreq", (PyCFunction)SVF_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
7505 {"setQ", (PyCFunction)SVF_setQ, METH_O, "Sets filter Q factor."},
7506 {"setType", (PyCFunction)SVF_setType, METH_O, "Sets filter type factor."},
7507 {"setMul", (PyCFunction)SVF_setMul, METH_O, "Sets mul factor."},
7508 {"setAdd", (PyCFunction)SVF_setAdd, METH_O, "Sets add factor."},
7509 {"setSub", (PyCFunction)SVF_setSub, METH_O, "Sets inverse add factor."},
7510 {"setDiv", (PyCFunction)SVF_setDiv, METH_O, "Sets inverse mul factor."},
7511 {NULL} /* Sentinel */
7512 };
7513
7514 static PyNumberMethods SVF_as_number =
7515 {
7516 (binaryfunc)SVF_add, /*nb_add*/
7517 (binaryfunc)SVF_sub, /*nb_subtract*/
7518 (binaryfunc)SVF_multiply, /*nb_multiply*/
7519 0, /*nb_remainder*/
7520 0, /*nb_divmod*/
7521 0, /*nb_power*/
7522 0, /*nb_neg*/
7523 0, /*nb_pos*/
7524 0, /*(unaryfunc)array_abs,*/
7525 0, /*nb_nonzero*/
7526 0, /*nb_invert*/
7527 0, /*nb_lshift*/
7528 0, /*nb_rshift*/
7529 0, /*nb_and*/
7530 0, /*nb_xor*/
7531 0, /*nb_or*/
7532 0, /*nb_int*/
7533 0, /*nb_long*/
7534 0, /*nb_float*/
7535 (binaryfunc)SVF_inplace_add, /*inplace_add*/
7536 (binaryfunc)SVF_inplace_sub, /*inplace_subtract*/
7537 (binaryfunc)SVF_inplace_multiply, /*inplace_multiply*/
7538 0, /*inplace_remainder*/
7539 0, /*inplace_power*/
7540 0, /*inplace_lshift*/
7541 0, /*inplace_rshift*/
7542 0, /*inplace_and*/
7543 0, /*inplace_xor*/
7544 0, /*inplace_or*/
7545 0, /*nb_floor_divide*/
7546 (binaryfunc)SVF_div, /*nb_true_divide*/
7547 0, /*nb_inplace_floor_divide*/
7548 (binaryfunc)SVF_inplace_div, /*nb_inplace_true_divide*/
7549 0, /* nb_index */
7550 };
7551
7552 PyTypeObject SVFType =
7553 {
7554 PyVarObject_HEAD_INIT(NULL, 0)
7555 "_pyo.SVF_base", /*tp_name*/
7556 sizeof(SVF), /*tp_basicsize*/
7557 0, /*tp_itemsize*/
7558 (destructor)SVF_dealloc, /*tp_dealloc*/
7559 0, /*tp_print*/
7560 0, /*tp_getattr*/
7561 0, /*tp_setattr*/
7562 0, /*tp_as_async (tp_compare in Python 2)*/
7563 0, /*tp_repr*/
7564 &SVF_as_number, /*tp_as_number*/
7565 0, /*tp_as_sequence*/
7566 0, /*tp_as_mapping*/
7567 0, /*tp_hash */
7568 0, /*tp_call*/
7569 0, /*tp_str*/
7570 0, /*tp_getattro*/
7571 0, /*tp_setattro*/
7572 0, /*tp_as_buffer*/
7573 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
7574 "SVF objects. Generates a state variable filter.", /* tp_doc */
7575 (traverseproc)SVF_traverse, /* tp_traverse */
7576 (inquiry)SVF_clear, /* tp_clear */
7577 0, /* tp_richcompare */
7578 0, /* tp_weaklistoffset */
7579 0, /* tp_iter */
7580 0, /* tp_iternext */
7581 SVF_methods, /* tp_methods */
7582 SVF_members, /* tp_members */
7583 0, /* tp_getset */
7584 0, /* tp_base */
7585 0, /* tp_dict */
7586 0, /* tp_descr_get */
7587 0, /* tp_descr_set */
7588 0, /* tp_dictoffset */
7589 0, /* tp_init */
7590 0, /* tp_alloc */
7591 SVF_new, /* tp_new */
7592 };
7593
7594 typedef struct
7595 {
7596 pyo_audio_HEAD
7597 PyObject *input;
7598 Stream *input_stream;
7599 PyObject *freq;
7600 Stream *freq_stream;
7601 PyObject *shelf;
7602 Stream *shelf_stream;
7603 PyObject *q;
7604 Stream *q_stream;
7605 PyObject *type;
7606 Stream *type_stream;
7607 int modebuffer[6]; // need at least 2 slots for mul & add
7608 MYFLT oneOverSr;
7609 MYFLT last_freq;
7610 MYFLT last_shelf;
7611 MYFLT last_q;
7612 // sample memories
7613 MYFLT y1;
7614 MYFLT y2;
7615 // variables
7616 MYFLT g;
7617 MYFLT r;
7618 MYFLT k;
7619 int order[10];
7620 } SVF2;
7621
7622 static void
7623 SVF2_filters_ii(SVF2 *self)
7624 {
7625 int i, j, ipart;
7626 MYFLT freq, q, shelf, type, wd, wa, fpart;
7627 MYFLT hp, lp, bp, ubp, bs, notch, peak, ap, ls, hs;
7628 MYFLT g_d, r_d, k_d;
7629 MYFLT deflt[11], interp[11];
7630
7631 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7632
7633 freq = PyFloat_AS_DOUBLE(self->freq);
7634
7635 if (self->modebuffer[3] == 0)
7636 q = PyFloat_AS_DOUBLE(self->q);
7637 else
7638 q = Stream_getData((Stream *)self->q_stream)[0];
7639
7640 if (self->modebuffer[4] == 0)
7641 shelf = PyFloat_AS_DOUBLE(self->shelf);
7642 else
7643 shelf = Stream_getData((Stream *)self->shelf_stream)[0];
7644
7645 type = PyFloat_AS_DOUBLE(self->type);
7646
7647 g_d = r_d = k_d = 0.0;
7648
7649 if (freq < 0.1)
7650 freq = 0.1;
7651 else if (freq > self->sr / 2.0)
7652 freq = self->sr / 2.0;
7653
7654 if (freq != self->last_freq)
7655 {
7656 self->last_freq = freq;
7657 wd = freq * TWOPI;
7658 wa = (2.0 / self->oneOverSr) * MYTAN(wd * self->oneOverSr / 2.0);
7659 g_d = ((wa * self->oneOverSr / 2.0) - self->g) / self->bufsize;
7660 }
7661
7662 if (q < 0.5)
7663 q = 0.5;
7664
7665 if (q != self->last_q)
7666 {
7667 self->last_q = q;
7668 r_d = ((1.0 / (2.0 * q)) - self->r) / self->bufsize;
7669 }
7670
7671 if (shelf < -24.0)
7672 shelf = -24.0;
7673 else if (shelf > 24.0)
7674 shelf = 24.0;
7675
7676 if (shelf != self->last_shelf)
7677 {
7678 self->last_shelf = shelf;
7679 k_d = ((MYPOW(10.0, shelf * 0.05) - 1.0) - self->k) / self->bufsize;
7680 }
7681
7682 if (type < 0.0)
7683 type = 0.0;
7684 else if (type >= 10.0)
7685 type = 10.0;
7686
7687 for (i = 0; i < self->bufsize; i++)
7688 {
7689 hp = (in[i] - (2.0 * self->r + self->g) * self->y1 - self->y2) / (1.0 + (2.0 * self->r * self->g) + self->g * self->g);
7690 bp = hp * self->g + self->y1;
7691 lp = bp * self->g + self->y2;
7692 ubp = 2.0 * self->r * bp;
7693 bs = in[i] + ubp * self->k;
7694 ls = in[i] + lp * self->k;
7695 hs = in[i] + hp * self->k;
7696 notch = in[i] - ubp;
7697 peak = lp - hp;
7698 ap = in[i] - (4.0 * self->r * bp);
7699 self->y1 = self->g * hp + bp;
7700 self->y2 = self->g * bp + lp;
7701
7702 self->g += g_d;
7703 self->r += r_d;
7704 self->k += k_d;
7705
7706 // default: lp - bp - hp - hs - bs - ls - notch - peak - ap - ubp
7707 deflt[0] = lp;
7708 deflt[1] = bp;
7709 deflt[2] = hp;
7710 deflt[3] = hs;
7711 deflt[4] = bs;
7712 deflt[5] = ls;
7713 deflt[6] = notch;
7714 deflt[7] = peak;
7715 deflt[8] = ap;
7716 deflt[9] = ubp;
7717 deflt[10] = lp;
7718
7719 for (j = 0; j < 10; j++)
7720 {
7721 interp[j] = deflt[self->order[j]];
7722 }
7723 interp[10] = interp[0];
7724
7725 ipart = (int)type;
7726 fpart = type - ipart;
7727 self->data[i] = interp[ipart] + (interp[ipart + 1] - interp[ipart]) * fpart;
7728 }
7729 }
7730
7731 static void
7732 SVF2_filters_ai(SVF2 *self)
7733 {
7734 int i, j, ipart;
7735 MYFLT freq, q, shelf, type, wd, wa, fpart;
7736 MYFLT hp, lp, bp, ubp, bs, notch, peak, ap, ls, hs;
7737 MYFLT g_d, r_d, k_d;
7738 MYFLT deflt[11], interp[11];
7739
7740 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7741
7742 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
7743
7744 if (self->modebuffer[3] == 0)
7745 q = PyFloat_AS_DOUBLE(self->q);
7746 else
7747 q = Stream_getData((Stream *)self->q_stream)[0];
7748
7749 if (self->modebuffer[4] == 0)
7750 shelf = PyFloat_AS_DOUBLE(self->shelf);
7751 else
7752 shelf = Stream_getData((Stream *)self->shelf_stream)[0];
7753
7754 type = PyFloat_AS_DOUBLE(self->type);
7755
7756 g_d = r_d = k_d = 0.0;
7757
7758 if (q < 0.5)
7759 q = 0.5;
7760
7761 if (q != self->last_q)
7762 {
7763 self->last_q = q;
7764 r_d = ((1.0 / (2.0 * q)) - self->r) / self->bufsize;
7765 }
7766
7767 if (shelf < -24.0)
7768 shelf = -24.0;
7769 else if (shelf > 24.0)
7770 shelf = 24.0;
7771
7772 if (shelf != self->last_shelf)
7773 {
7774 self->last_shelf = shelf;
7775 k_d = ((MYPOW(10.0, shelf * 0.05) - 1.0) - self->k) / self->bufsize;
7776 }
7777
7778 if (type < 0.0)
7779 type = 0.0;
7780 else if (type >= 10.0)
7781 type = 10.0;
7782
7783 for (i = 0; i < self->bufsize; i++)
7784 {
7785 freq = fr[i];
7786
7787 if (freq < 0.1)
7788 freq = 0.1;
7789 else if (freq > self->sr / 2.0)
7790 freq = self->sr / 2.0;
7791
7792 if (freq != self->last_freq)
7793 {
7794 self->last_freq = freq;
7795 wd = freq * TWOPI;
7796 wa = (2.0 / self->oneOverSr) * MYTAN(wd * self->oneOverSr / 2.0);
7797 g_d = ((wa * self->oneOverSr / 2.0) - self->g) / self->bufsize;
7798 }
7799
7800 hp = (in[i] - (2.0 * self->r + self->g) * self->y1 - self->y2) / (1.0 + (2.0 * self->r * self->g) + self->g * self->g);
7801 bp = hp * self->g + self->y1;
7802 lp = bp * self->g + self->y2;
7803 ubp = 2.0 * self->r * bp;
7804 bs = in[i] + ubp * self->k;
7805 ls = in[i] + lp * self->k;
7806 hs = in[i] + hp * self->k;
7807 notch = in[i] - ubp;
7808 peak = lp - hp;
7809 ap = in[i] - (4.0 * self->r * bp);
7810 self->y1 = self->g * hp + bp;
7811 self->y2 = self->g * bp + lp;
7812
7813 self->g += g_d;
7814 self->r += r_d;
7815 self->k += k_d;
7816
7817 // default: lp - bp - hp - hs - bs - ls - notch - peak - ap - ubp
7818 deflt[0] = lp;
7819 deflt[1] = bp;
7820 deflt[2] = hp;
7821 deflt[3] = hs;
7822 deflt[4] = bs;
7823 deflt[5] = ls;
7824 deflt[6] = notch;
7825 deflt[7] = peak;
7826 deflt[8] = ap;
7827 deflt[9] = ubp;
7828 deflt[10] = lp;
7829
7830 for (j = 0; j < 10; j++)
7831 {
7832 interp[j] = deflt[self->order[j]];
7833 }
7834 interp[10] = interp[0];
7835
7836 ipart = (int)type;
7837 fpart = type - ipart;
7838 self->data[i] = interp[ipart] + (interp[ipart + 1] - interp[ipart]) * fpart;
7839 }
7840 }
7841
7842 static void
7843 SVF2_filters_ia(SVF2 *self)
7844 {
7845 int i, j, ipart;
7846 MYFLT freq, q, shelf, type, wd, wa, fpart;
7847 MYFLT hp, lp, bp, ubp, bs, notch, peak, ap, ls, hs;
7848 MYFLT g_d, r_d, k_d;
7849 MYFLT deflt[11], interp[11];
7850
7851 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7852
7853 freq = PyFloat_AS_DOUBLE(self->freq);
7854
7855 if (self->modebuffer[3] == 0)
7856 q = PyFloat_AS_DOUBLE(self->q);
7857 else
7858 q = Stream_getData((Stream *)self->q_stream)[0];
7859
7860 if (self->modebuffer[4] == 0)
7861 shelf = PyFloat_AS_DOUBLE(self->shelf);
7862 else
7863 shelf = Stream_getData((Stream *)self->shelf_stream)[0];
7864
7865 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7866
7867 g_d = r_d = k_d = 0.0;
7868
7869 if (freq < 0.1)
7870 freq = 0.1;
7871 else if (freq > self->sr / 2.0)
7872 freq = self->sr / 2.0;
7873
7874 if (freq != self->last_freq)
7875 {
7876 self->last_freq = freq;
7877 wd = freq * TWOPI;
7878 wa = (2.0 / self->oneOverSr) * MYTAN(wd * self->oneOverSr / 2.0);
7879 g_d = ((wa * self->oneOverSr / 2.0) - self->g) / self->bufsize;
7880 }
7881
7882 if (q < 0.5)
7883 q = 0.5;
7884
7885 if (q != self->last_q)
7886 {
7887 self->last_q = q;
7888 r_d = ((1.0 / (2.0 * q)) - self->r) / self->bufsize;
7889 }
7890
7891 if (shelf < -24.0)
7892 shelf = -24.0;
7893 else if (shelf > 24.0)
7894 shelf = 24.0;
7895
7896 if (shelf != self->last_shelf)
7897 {
7898 self->last_shelf = shelf;
7899 k_d = ((MYPOW(10.0, shelf * 0.05) - 1.0) - self->k) / self->bufsize;
7900 }
7901
7902 for (i = 0; i < self->bufsize; i++)
7903 {
7904 hp = (in[i] - (2.0 * self->r + self->g) * self->y1 - self->y2) / (1.0 + (2.0 * self->r * self->g) + self->g * self->g);
7905 bp = hp * self->g + self->y1;
7906 lp = bp * self->g + self->y2;
7907 ubp = 2.0 * self->r * bp;
7908 bs = in[i] + ubp * self->k;
7909 ls = in[i] + lp * self->k;
7910 hs = in[i] + hp * self->k;
7911 notch = in[i] - ubp;
7912 peak = lp - hp;
7913 ap = in[i] - (4.0 * self->r * bp);
7914 self->y1 = self->g * hp + bp;
7915 self->y2 = self->g * bp + lp;
7916
7917 self->g += g_d;
7918 self->r += r_d;
7919 self->k += k_d;
7920
7921 // default: lp - bp - hp - hs - bs - ls - notch - peak - ap - ubp
7922 deflt[0] = lp;
7923 deflt[1] = bp;
7924 deflt[2] = hp;
7925 deflt[3] = hs;
7926 deflt[4] = bs;
7927 deflt[5] = ls;
7928 deflt[6] = notch;
7929 deflt[7] = peak;
7930 deflt[8] = ap;
7931 deflt[9] = ubp;
7932 deflt[10] = lp;
7933
7934 for (j = 0; j < 10; j++)
7935 {
7936 interp[j] = deflt[self->order[j]];
7937 }
7938 interp[10] = interp[0];
7939
7940 type = tp[i];
7941
7942 if (type < 0.0)
7943 type = 0.0;
7944 else if (type >= 10.0)
7945 type = 10.0;
7946
7947 ipart = (int)type;
7948 fpart = type - ipart;
7949 self->data[i] = interp[ipart] + (interp[ipart + 1] - interp[ipart]) * fpart;
7950 }
7951 }
7952
7953 static void
7954 SVF2_filters_aa(SVF2 *self)
7955 {
7956 int i, j, ipart;
7957 MYFLT freq, q, shelf, type, wd, wa, fpart;
7958 MYFLT hp, lp, bp, ubp, bs, notch, peak, ap, ls, hs;
7959 MYFLT g_d, r_d, k_d;
7960 MYFLT deflt[11], interp[11];
7961
7962 MYFLT *in = Stream_getData((Stream *)self->input_stream);
7963
7964 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
7965
7966 if (self->modebuffer[3] == 0)
7967 q = PyFloat_AS_DOUBLE(self->q);
7968 else
7969 q = Stream_getData((Stream *)self->q_stream)[0];
7970
7971 if (self->modebuffer[4] == 0)
7972 shelf = PyFloat_AS_DOUBLE(self->shelf);
7973 else
7974 shelf = Stream_getData((Stream *)self->shelf_stream)[0];
7975
7976 MYFLT *tp = Stream_getData((Stream *)self->type_stream);
7977
7978 g_d = r_d = k_d = 0.0;
7979
7980 if (q < 0.5)
7981 q = 0.5;
7982
7983 if (q != self->last_q)
7984 {
7985 self->last_q = q;
7986 r_d = ((1.0 / (2.0 * q)) - self->r) / self->bufsize;
7987 }
7988
7989 if (shelf < -24.0)
7990 shelf = -24.0;
7991 else if (shelf > 24.0)
7992 shelf = 24.0;
7993
7994 if (shelf != self->last_shelf)
7995 {
7996 self->last_shelf = shelf;
7997 k_d = ((MYPOW(10.0, shelf * 0.05) - 1.0) - self->k) / self->bufsize;
7998 }
7999
8000 for (i = 0; i < self->bufsize; i++)
8001 {
8002 freq = fr[i];
8003
8004 if (freq < 0.1)
8005 freq = 0.1;
8006 else if (freq > self->sr / 2.0)
8007 freq = self->sr / 2.0;
8008
8009 if (freq != self->last_freq)
8010 {
8011 self->last_freq = freq;
8012 wd = freq * TWOPI;
8013 wa = (2.0 / self->oneOverSr) * MYTAN(wd * self->oneOverSr / 2.0);
8014 g_d = ((wa * self->oneOverSr / 2.0) - self->g) / self->bufsize;
8015 }
8016
8017 hp = (in[i] - (2.0 * self->r + self->g) * self->y1 - self->y2) / (1.0 + (2.0 * self->r * self->g) + self->g * self->g);
8018 bp = hp * self->g + self->y1;
8019 lp = bp * self->g + self->y2;
8020 ubp = 2.0 * self->r * bp;
8021 bs = in[i] + ubp * self->k;
8022 ls = in[i] + lp * self->k;
8023 hs = in[i] + hp * self->k;
8024 notch = in[i] - ubp;
8025 peak = lp - hp;
8026 ap = in[i] - (4.0 * self->r * bp);
8027 self->y1 = self->g * hp + bp;
8028 self->y2 = self->g * bp + lp;
8029
8030 self->g += g_d;
8031 self->r += r_d;
8032 self->k += k_d;
8033
8034 // default: lp - bp - hp - hs - bs - ls - notch - peak - ap - ubp
8035 deflt[0] = lp;
8036 deflt[1] = bp;
8037 deflt[2] = hp;
8038 deflt[3] = hs;
8039 deflt[4] = bs;
8040 deflt[5] = ls;
8041 deflt[6] = notch;
8042 deflt[7] = peak;
8043 deflt[8] = ap;
8044 deflt[9] = ubp;
8045 deflt[10] = lp;
8046
8047 for (j = 0; j < 10; j++)
8048 {
8049 interp[j] = deflt[self->order[j]];
8050 }
8051 interp[10] = interp[0];
8052
8053 type = tp[i];
8054
8055 if (type < 0.0)
8056 type = 0.0;
8057 else if (type >= 10.0)
8058 type = 10.0;
8059
8060 ipart = (int)type;
8061 fpart = type - ipart;
8062 self->data[i] = interp[ipart] + (interp[ipart + 1] - interp[ipart]) * fpart;
8063 }
8064 }
8065
8066 static void SVF2_postprocessing_ii(SVF2 *self) { POST_PROCESSING_II };
8067 static void SVF2_postprocessing_ai(SVF2 *self) { POST_PROCESSING_AI };
8068 static void SVF2_postprocessing_ia(SVF2 *self) { POST_PROCESSING_IA };
8069 static void SVF2_postprocessing_aa(SVF2 *self) { POST_PROCESSING_AA };
8070 static void SVF2_postprocessing_ireva(SVF2 *self) { POST_PROCESSING_IREVA };
8071 static void SVF2_postprocessing_areva(SVF2 *self) { POST_PROCESSING_AREVA };
8072 static void SVF2_postprocessing_revai(SVF2 *self) { POST_PROCESSING_REVAI };
8073 static void SVF2_postprocessing_revaa(SVF2 *self) { POST_PROCESSING_REVAA };
8074 static void SVF2_postprocessing_revareva(SVF2 *self) { POST_PROCESSING_REVAREVA };
8075
8076 static void
8077 SVF2_setProcMode(SVF2 *self)
8078 {
8079 int procmode, muladdmode;
8080 procmode = self->modebuffer[2] + self->modebuffer[5] * 10;
8081 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
8082
8083 switch (procmode)
8084 {
8085 case 0:
8086 self->proc_func_ptr = SVF2_filters_ii;
8087 break;
8088
8089 case 1:
8090 self->proc_func_ptr = SVF2_filters_ai;
8091 break;
8092
8093 case 10:
8094 self->proc_func_ptr = SVF2_filters_ia;
8095 break;
8096
8097 case 11:
8098 self->proc_func_ptr = SVF2_filters_aa;
8099 break;
8100 }
8101
8102 switch (muladdmode)
8103 {
8104 case 0:
8105 self->muladd_func_ptr = SVF2_postprocessing_ii;
8106 break;
8107
8108 case 1:
8109 self->muladd_func_ptr = SVF2_postprocessing_ai;
8110 break;
8111
8112 case 2:
8113 self->muladd_func_ptr = SVF2_postprocessing_revai;
8114 break;
8115
8116 case 10:
8117 self->muladd_func_ptr = SVF2_postprocessing_ia;
8118 break;
8119
8120 case 11:
8121 self->muladd_func_ptr = SVF2_postprocessing_aa;
8122 break;
8123
8124 case 12:
8125 self->muladd_func_ptr = SVF2_postprocessing_revaa;
8126 break;
8127
8128 case 20:
8129 self->muladd_func_ptr = SVF2_postprocessing_ireva;
8130 break;
8131
8132 case 21:
8133 self->muladd_func_ptr = SVF2_postprocessing_areva;
8134 break;
8135
8136 case 22:
8137 self->muladd_func_ptr = SVF2_postprocessing_revareva;
8138 break;
8139 }
8140 }
8141
8142 static void
8143 SVF2_compute_next_data_frame(SVF2 *self)
8144 {
8145 (*self->proc_func_ptr)(self);
8146 (*self->muladd_func_ptr)(self);
8147 }
8148
8149 static int
8150 SVF2_traverse(SVF2 *self, visitproc visit, void *arg)
8151 {
8152 pyo_VISIT
8153 Py_VISIT(self->input);
8154 Py_VISIT(self->freq);
8155 Py_VISIT(self->shelf);
8156 Py_VISIT(self->q);
8157 Py_VISIT(self->type);
8158 return 0;
8159 }
8160
8161 static int
8162 SVF2_clear(SVF2 *self)
8163 {
8164 pyo_CLEAR
8165 Py_CLEAR(self->input);
8166 Py_CLEAR(self->freq);
8167 Py_CLEAR(self->shelf);
8168 Py_CLEAR(self->q);
8169 Py_CLEAR(self->type);
8170 return 0;
8171 }
8172
8173 static void
8174 SVF2_dealloc(SVF2* self)
8175 {
8176 pyo_DEALLOC
8177 SVF2_clear(self);
8178 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
8179 Py_TYPE(self)->tp_free((PyObject*)self);
8180 }
8181
8182 static PyObject *
8183 SVF2_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
8184 {
8185 int i;
8186 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *shelftmp = NULL, *qtmp = NULL, *typetmp = NULL, *multmp = NULL, *addtmp = NULL;
8187 SVF2 *self;
8188 self = (SVF2 *)type->tp_alloc(type, 0);
8189
8190 self->freq = PyFloat_FromDouble(1000);
8191 self->shelf = PyFloat_FromDouble(-3.0);
8192 self->q = PyFloat_FromDouble(1);
8193 self->type = PyFloat_FromDouble(0);
8194 self->modebuffer[0] = 0;
8195 self->modebuffer[1] = 0;
8196 self->modebuffer[2] = 0;
8197 self->modebuffer[3] = 0;
8198 self->modebuffer[4] = 0;
8199 self->modebuffer[5] = 0;
8200 self->y1 = self->y2 = 0.0;
8201 self->g = self->r = 0.0;
8202 self->k = 0.0;
8203 self->last_freq = self->last_q = self->last_shelf = -1.0;
8204
8205 for (i = 0; i < 10; i++)
8206 {
8207 self->order[i] = i;
8208 }
8209
8210 INIT_OBJECT_COMMON
8211
8212 self->oneOverSr = 1.0 / (MYFLT)self->sr;
8213
8214 Stream_setFunctionPtr(self->stream, SVF2_compute_next_data_frame);
8215 self->mode_func_ptr = SVF2_setProcMode;
8216
8217 static char *kwlist[] = {"input", "freq", "q", "shelf", "type", "mul", "add", NULL};
8218
8219 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOOOO", kwlist, &inputtmp, &freqtmp, &qtmp, &shelftmp, &typetmp, &multmp, &addtmp))
8220 Py_RETURN_NONE;
8221
8222 INIT_INPUT_STREAM
8223
8224 if (freqtmp)
8225 {
8226 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
8227 }
8228
8229 if (qtmp)
8230 {
8231 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
8232 }
8233
8234 if (shelftmp)
8235 {
8236 PyObject_CallMethod((PyObject *)self, "setShelf", "O", shelftmp);
8237 }
8238
8239 if (typetmp)
8240 {
8241 PyObject_CallMethod((PyObject *)self, "setType", "O", typetmp);
8242 }
8243
8244 if (multmp)
8245 {
8246 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
8247 }
8248
8249 if (addtmp)
8250 {
8251 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
8252 }
8253
8254 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
8255
8256 (*self->mode_func_ptr)(self);
8257
8258 return (PyObject *)self;
8259 }
8260
8261 static PyObject * SVF2_getServer(SVF2* self) { GET_SERVER };
8262 static PyObject * SVF2_getStream(SVF2* self) { GET_STREAM };
8263 static PyObject * SVF2_setMul(SVF2 *self, PyObject *arg) { SET_MUL };
8264 static PyObject * SVF2_setAdd(SVF2 *self, PyObject *arg) { SET_ADD };
8265 static PyObject * SVF2_setSub(SVF2 *self, PyObject *arg) { SET_SUB };
8266 static PyObject * SVF2_setDiv(SVF2 *self, PyObject *arg) { SET_DIV };
8267
8268 static PyObject * SVF2_play(SVF2 *self, PyObject *args, PyObject *kwds) { PLAY };
8269 static PyObject * SVF2_out(SVF2 *self, PyObject *args, PyObject *kwds) { OUT };
8270 static PyObject * SVF2_stop(SVF2 *self, PyObject *args, PyObject *kwds) { STOP };
8271
8272 static PyObject * SVF2_multiply(SVF2 *self, PyObject *arg) { MULTIPLY };
8273 static PyObject * SVF2_inplace_multiply(SVF2 *self, PyObject *arg) { INPLACE_MULTIPLY };
8274 static PyObject * SVF2_add(SVF2 *self, PyObject *arg) { ADD };
8275 static PyObject * SVF2_inplace_add(SVF2 *self, PyObject *arg) { INPLACE_ADD };
8276 static PyObject * SVF2_sub(SVF2 *self, PyObject *arg) { SUB };
8277 static PyObject * SVF2_inplace_sub(SVF2 *self, PyObject *arg) { INPLACE_SUB };
8278 static PyObject * SVF2_div(SVF2 *self, PyObject *arg) { DIV };
8279 static PyObject * SVF2_inplace_div(SVF2 *self, PyObject *arg) { INPLACE_DIV };
8280
8281 static PyObject * SVF2_setFreq(SVF2 *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
8282 static PyObject * SVF2_setQ(SVF2 *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
8283 static PyObject * SVF2_setShelf(SVF2 *self, PyObject *arg) { SET_PARAM(self->shelf, self->shelf_stream, 4); }
8284 static PyObject * SVF2_setType(SVF2 *self, PyObject *arg) {SET_PARAM(self->type, self->type_stream, 5); }
8285
8286 static PyObject *
8287 SVF2_setOrder(SVF2 *self, PyObject *arg)
8288 {
8289 int i, size;
8290
8291 if (PyList_Check(arg))
8292 {
8293 size = PyList_Size(arg);
8294
8295 if (size > 10)
8296 size = 10;
8297
8298 for (i = 0; i < size; i++)
8299 {
8300 self->order[i] = PyLong_AsLong(PyList_GetItem(arg, i));
8301 }
8302
8303 self->order[size] = self->order[0];
8304 }
8305
8306 Py_RETURN_NONE;
8307 }
8308
8309 static PyMemberDef SVF2_members[] =
8310 {
8311 {"server", T_OBJECT_EX, offsetof(SVF2, server), 0, "Pyo server."},
8312 {"stream", T_OBJECT_EX, offsetof(SVF2, stream), 0, "Stream object."},
8313 {"input", T_OBJECT_EX, offsetof(SVF2, input), 0, "Input sound object."},
8314 {"freq", T_OBJECT_EX, offsetof(SVF2, freq), 0, "Cutoff frequency in cycle per second."},
8315 {"q", T_OBJECT_EX, offsetof(SVF2, q), 0, "Q factor."},
8316 {"type", T_OBJECT_EX, offsetof(SVF2, type), 0, "Filter type."},
8317 {"mul", T_OBJECT_EX, offsetof(SVF2, mul), 0, "Mul factor."},
8318 {"add", T_OBJECT_EX, offsetof(SVF2, add), 0, "Add factor."},
8319 {NULL} /* Sentinel */
8320 };
8321
8322 static PyMethodDef SVF2_methods[] =
8323 {
8324 {"getServer", (PyCFunction)SVF2_getServer, METH_NOARGS, "Returns server object."},
8325 {"_getStream", (PyCFunction)SVF2_getStream, METH_NOARGS, "Returns stream object."},
8326 {"play", (PyCFunction)SVF2_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
8327 {"out", (PyCFunction)SVF2_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
8328 {"stop", (PyCFunction)SVF2_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
8329 {"setFreq", (PyCFunction)SVF2_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
8330 {"setQ", (PyCFunction)SVF2_setQ, METH_O, "Sets filter Q factor."},
8331 {"setShelf", (PyCFunction)SVF2_setShelf, METH_O, "Sets filter shelving gain."},
8332 {"setType", (PyCFunction)SVF2_setType, METH_O, "Sets filter type factor."},
8333 {"setOrder", (PyCFunction)SVF2_setOrder, METH_O, "Sets filter type ordering."},
8334 {"setMul", (PyCFunction)SVF2_setMul, METH_O, "Sets mul factor."},
8335 {"setAdd", (PyCFunction)SVF2_setAdd, METH_O, "Sets add factor."},
8336 {"setSub", (PyCFunction)SVF2_setSub, METH_O, "Sets inverse add factor."},
8337 {"setDiv", (PyCFunction)SVF2_setDiv, METH_O, "Sets inverse mul factor."},
8338 {NULL} /* Sentinel */
8339 };
8340
8341 static PyNumberMethods SVF2_as_number =
8342 {
8343 (binaryfunc)SVF2_add, /*nb_add*/
8344 (binaryfunc)SVF2_sub, /*nb_subtract*/
8345 (binaryfunc)SVF2_multiply, /*nb_multiply*/
8346 0, /*nb_remainder*/
8347 0, /*nb_divmod*/
8348 0, /*nb_power*/
8349 0, /*nb_neg*/
8350 0, /*nb_pos*/
8351 0, /*(unaryfunc)array_abs,*/
8352 0, /*nb_nonzero*/
8353 0, /*nb_invert*/
8354 0, /*nb_lshift*/
8355 0, /*nb_rshift*/
8356 0, /*nb_and*/
8357 0, /*nb_xor*/
8358 0, /*nb_or*/
8359 0, /*nb_int*/
8360 0, /*nb_long*/
8361 0, /*nb_float*/
8362 (binaryfunc)SVF2_inplace_add, /*inplace_add*/
8363 (binaryfunc)SVF2_inplace_sub, /*inplace_subtract*/
8364 (binaryfunc)SVF2_inplace_multiply, /*inplace_multiply*/
8365 0, /*inplace_remainder*/
8366 0, /*inplace_power*/
8367 0, /*inplace_lshift*/
8368 0, /*inplace_rshift*/
8369 0, /*inplace_and*/
8370 0, /*inplace_xor*/
8371 0, /*inplace_or*/
8372 0, /*nb_floor_divide*/
8373 (binaryfunc)SVF2_div, /*nb_true_divide*/
8374 0, /*nb_inplace_floor_divide*/
8375 (binaryfunc)SVF2_inplace_div, /*nb_inplace_true_divide*/
8376 0, /* nb_index */
8377 };
8378
8379 PyTypeObject SVF2Type =
8380 {
8381 PyVarObject_HEAD_INIT(NULL, 0)
8382 "_pyo.SVF2_base", /*tp_name*/
8383 sizeof(SVF2), /*tp_basicsize*/
8384 0, /*tp_itemsize*/
8385 (destructor)SVF2_dealloc, /*tp_dealloc*/
8386 0, /*tp_print*/
8387 0, /*tp_getattr*/
8388 0, /*tp_setattr*/
8389 0, /*tp_as_async (tp_compare in Python 2)*/
8390 0, /*tp_repr*/
8391 &SVF2_as_number, /*tp_as_number*/
8392 0, /*tp_as_sequence*/
8393 0, /*tp_as_mapping*/
8394 0, /*tp_hash */
8395 0, /*tp_call*/
8396 0, /*tp_str*/
8397 0, /*tp_getattro*/
8398 0, /*tp_setattro*/
8399 0, /*tp_as_buffer*/
8400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
8401 "SVF2 objects. Generates a state variable filter.", /* tp_doc */
8402 (traverseproc)SVF2_traverse, /* tp_traverse */
8403 (inquiry)SVF2_clear, /* tp_clear */
8404 0, /* tp_richcompare */
8405 0, /* tp_weaklistoffset */
8406 0, /* tp_iter */
8407 0, /* tp_iternext */
8408 SVF2_methods, /* tp_methods */
8409 SVF2_members, /* tp_members */
8410 0, /* tp_getset */
8411 0, /* tp_base */
8412 0, /* tp_dict */
8413 0, /* tp_descr_get */
8414 0, /* tp_descr_set */
8415 0, /* tp_dictoffset */
8416 0, /* tp_init */
8417 0, /* tp_alloc */
8418 SVF2_new, /* tp_new */
8419 };
8420
8421 typedef struct
8422 {
8423 pyo_audio_HEAD
8424 PyObject *input;
8425 Stream *input_stream;
8426 int size;
8427 int halfSize;
8428 int in_count;
8429 int init;
8430 double currentValue;
8431 double oneOnSize;
8432 int modebuffer[2];
8433 MYFLT *buffer; // samples memory
8434 } Average;
8435
8436 static void
8437 Average_process_i(Average *self)
8438 {
8439 int i;
8440
8441 MYFLT *in = Stream_getData((Stream *)self->input_stream);
8442
8443 if (self->init)
8444 {
8445 for (i = 0; i < self->bufsize; i++)
8446 {
8447 if (self->init)
8448 {
8449 self->buffer[self->in_count] = in[i];
8450 self->currentValue += (double)in[i];
8451 self->in_count++;
8452
8453 if (self->in_count < self->halfSize)
8454 self->data[i] = 0.0;
8455 else
8456 self->data[i] = (MYFLT)(self->currentValue * self->oneOnSize);
8457
8458 if (self->in_count >= self->size)
8459 {
8460 self->in_count = 0;
8461 self->init = 0;
8462 }
8463 }
8464 else
8465 {
8466 self->buffer[self->in_count] = in[i];
8467 self->currentValue += (double)in[i];
8468 self->in_count++;
8469
8470 if (self->in_count >= self->size)
8471 self->in_count = 0;
8472
8473 self->currentValue -= (double)self->buffer[self->in_count];
8474 self->data[i] = (MYFLT)(self->currentValue * self->oneOnSize);
8475 }
8476 }
8477 }
8478 else
8479 {
8480 for (i = 0; i < self->bufsize; i++)
8481 {
8482 self->buffer[self->in_count] = in[i];
8483 self->currentValue += (double)in[i];
8484 self->in_count++;
8485
8486 if (self->in_count >= self->size)
8487 self->in_count = 0;
8488
8489 self->currentValue -= (double)self->buffer[self->in_count];
8490 self->data[i] = (MYFLT)(self->currentValue * self->oneOnSize);
8491 }
8492 }
8493 }
8494
8495 static void Average_postprocessing_ii(Average *self) { POST_PROCESSING_II };
8496 static void Average_postprocessing_ai(Average *self) { POST_PROCESSING_AI };
8497 static void Average_postprocessing_ia(Average *self) { POST_PROCESSING_IA };
8498 static void Average_postprocessing_aa(Average *self) { POST_PROCESSING_AA };
8499 static void Average_postprocessing_ireva(Average *self) { POST_PROCESSING_IREVA };
8500 static void Average_postprocessing_areva(Average *self) { POST_PROCESSING_AREVA };
8501 static void Average_postprocessing_revai(Average *self) { POST_PROCESSING_REVAI };
8502 static void Average_postprocessing_revaa(Average *self) { POST_PROCESSING_REVAA };
8503 static void Average_postprocessing_revareva(Average *self) { POST_PROCESSING_REVAREVA };
8504
8505 static void
8506 Average_setProcMode(Average *self)
8507 {
8508 int muladdmode;
8509 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
8510
8511 self->proc_func_ptr = Average_process_i;
8512
8513 switch (muladdmode)
8514 {
8515 case 0:
8516 self->muladd_func_ptr = Average_postprocessing_ii;
8517 break;
8518
8519 case 1:
8520 self->muladd_func_ptr = Average_postprocessing_ai;
8521 break;
8522
8523 case 2:
8524 self->muladd_func_ptr = Average_postprocessing_revai;
8525 break;
8526
8527 case 10:
8528 self->muladd_func_ptr = Average_postprocessing_ia;
8529 break;
8530
8531 case 11:
8532 self->muladd_func_ptr = Average_postprocessing_aa;
8533 break;
8534
8535 case 12:
8536 self->muladd_func_ptr = Average_postprocessing_revaa;
8537 break;
8538
8539 case 20:
8540 self->muladd_func_ptr = Average_postprocessing_ireva;
8541 break;
8542
8543 case 21:
8544 self->muladd_func_ptr = Average_postprocessing_areva;
8545 break;
8546
8547 case 22:
8548 self->muladd_func_ptr = Average_postprocessing_revareva;
8549 break;
8550 }
8551 }
8552
8553 static void
8554 Average_compute_next_data_frame(Average *self)
8555 {
8556 (*self->proc_func_ptr)(self);
8557 (*self->muladd_func_ptr)(self);
8558 }
8559
8560 static int
8561 Average_traverse(Average *self, visitproc visit, void *arg)
8562 {
8563 pyo_VISIT
8564 Py_VISIT(self->input);
8565 return 0;
8566 }
8567
8568 static int
8569 Average_clear(Average *self)
8570 {
8571 pyo_CLEAR
8572 Py_CLEAR(self->input);
8573 return 0;
8574 }
8575
8576 static void
8577 Average_dealloc(Average* self)
8578 {
8579 pyo_DEALLOC
8580 PyMem_RawFree(self->buffer);
8581 Average_clear(self);
8582 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
8583 Py_TYPE(self)->tp_free((PyObject*)self);
8584 }
8585
8586 static PyObject *
8587 Average_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
8588 {
8589 int i;
8590 PyObject *inputtmp, *input_streamtmp, *multmp = NULL, *addtmp = NULL;
8591 Average *self;
8592 self = (Average *)type->tp_alloc(type, 0);
8593
8594 self->size = 10;
8595 self->init = 1;
8596 self->in_count = 0;
8597 self->currentValue = 0.0;
8598 self->modebuffer[0] = 0;
8599 self->modebuffer[1] = 0;
8600
8601 INIT_OBJECT_COMMON
8602 Stream_setFunctionPtr(self->stream, Average_compute_next_data_frame);
8603 self->mode_func_ptr = Average_setProcMode;
8604
8605 static char *kwlist[] = {"input", "size", "mul", "add", NULL};
8606
8607 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &inputtmp, &self->size, &multmp, &addtmp))
8608 Py_RETURN_NONE;
8609
8610 INIT_INPUT_STREAM
8611
8612 if (multmp)
8613 {
8614 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
8615 }
8616
8617 if (addtmp)
8618 {
8619 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
8620 }
8621
8622 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
8623
8624 self->halfSize = (int)(self->size / 2);
8625 self->oneOnSize = 1.0 / (double)self->size;
8626
8627 self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, (self->size) * sizeof(MYFLT));
8628
8629 for (i = 0; i < (self->size); i++)
8630 {
8631 self->buffer[i] = 0.;
8632 }
8633
8634 (*self->mode_func_ptr)(self);
8635
8636 return (PyObject *)self;
8637 }
8638
8639 static PyObject * Average_getServer(Average* self) { GET_SERVER };
8640 static PyObject * Average_getStream(Average* self) { GET_STREAM };
8641 static PyObject * Average_setMul(Average *self, PyObject *arg) { SET_MUL };
8642 static PyObject * Average_setAdd(Average *self, PyObject *arg) { SET_ADD };
8643 static PyObject * Average_setSub(Average *self, PyObject *arg) { SET_SUB };
8644 static PyObject * Average_setDiv(Average *self, PyObject *arg) { SET_DIV };
8645
8646 static PyObject * Average_play(Average *self, PyObject *args, PyObject *kwds) { PLAY };
8647 static PyObject * Average_out(Average *self, PyObject *args, PyObject *kwds) { OUT };
8648 static PyObject * Average_stop(Average *self, PyObject *args, PyObject *kwds) { STOP };
8649
8650 static PyObject * Average_multiply(Average *self, PyObject *arg) { MULTIPLY };
8651 static PyObject * Average_inplace_multiply(Average *self, PyObject *arg) { INPLACE_MULTIPLY };
8652 static PyObject * Average_add(Average *self, PyObject *arg) { ADD };
8653 static PyObject * Average_inplace_add(Average *self, PyObject *arg) { INPLACE_ADD };
8654 static PyObject * Average_sub(Average *self, PyObject *arg) { SUB };
8655 static PyObject * Average_inplace_sub(Average *self, PyObject *arg) { INPLACE_SUB };
8656 static PyObject * Average_div(Average *self, PyObject *arg) { DIV };
8657 static PyObject * Average_inplace_div(Average *self, PyObject *arg) { INPLACE_DIV };
8658
8659 static PyObject *
8660 Average_setSize(Average *self, PyObject *arg)
8661 {
8662 int i;
8663
8664 ASSERT_ARG_NOT_NULL
8665
8666 if (PyLong_Check(arg))
8667 {
8668 self->size = PyLong_AsLong(arg);
8669 self->halfSize = (int)(self->size / 2);
8670 self->oneOnSize = 1.0 / (double)self->size;
8671 self->init = 1;
8672 self->in_count = 0;
8673 self->currentValue = 0.0;
8674 self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, (self->size) * sizeof(MYFLT));
8675
8676 for (i = 0; i < (self->size); i++)
8677 {
8678 self->buffer[i] = 0.;
8679 }
8680 }
8681
8682 Py_RETURN_NONE;
8683 }
8684
8685 static PyMemberDef Average_members[] =
8686 {
8687 {"server", T_OBJECT_EX, offsetof(Average, server), 0, "Pyo server."},
8688 {"stream", T_OBJECT_EX, offsetof(Average, stream), 0, "Stream object."},
8689 {"input", T_OBJECT_EX, offsetof(Average, input), 0, "Input sound object."},
8690 {"mul", T_OBJECT_EX, offsetof(Average, mul), 0, "Mul factor."},
8691 {"add", T_OBJECT_EX, offsetof(Average, add), 0, "Add factor."},
8692 {NULL} /* Sentinel */
8693 };
8694
8695 static PyMethodDef Average_methods[] =
8696 {
8697 {"getServer", (PyCFunction)Average_getServer, METH_NOARGS, "Returns server object."},
8698 {"_getStream", (PyCFunction)Average_getStream, METH_NOARGS, "Returns stream object."},
8699 {"play", (PyCFunction)Average_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
8700 {"out", (PyCFunction)Average_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
8701 {"stop", (PyCFunction)Average_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
8702 {"setSize", (PyCFunction)Average_setSize, METH_O, "Sets filter kernel size."},
8703 {"setMul", (PyCFunction)Average_setMul, METH_O, "Sets oscillator mul factor."},
8704 {"setAdd", (PyCFunction)Average_setAdd, METH_O, "Sets oscillator add factor."},
8705 {"setSub", (PyCFunction)Average_setSub, METH_O, "Sets inverse add factor."},
8706 {"setDiv", (PyCFunction)Average_setDiv, METH_O, "Sets inverse mul factor."},
8707 {NULL} /* Sentinel */
8708 };
8709
8710 static PyNumberMethods Average_as_number =
8711 {
8712 (binaryfunc)Average_add, /*nb_add*/
8713 (binaryfunc)Average_sub, /*nb_subtract*/
8714 (binaryfunc)Average_multiply, /*nb_multiply*/
8715 0, /*nb_remainder*/
8716 0, /*nb_divmod*/
8717 0, /*nb_power*/
8718 0, /*nb_neg*/
8719 0, /*nb_pos*/
8720 0, /*(unaryfunc)array_abs,*/
8721 0, /*nb_nonzero*/
8722 0, /*nb_invert*/
8723 0, /*nb_lshift*/
8724 0, /*nb_rshift*/
8725 0, /*nb_and*/
8726 0, /*nb_xor*/
8727 0, /*nb_or*/
8728 0, /*nb_int*/
8729 0, /*nb_long*/
8730 0, /*nb_float*/
8731 (binaryfunc)Average_inplace_add, /*inplace_add*/
8732 (binaryfunc)Average_inplace_sub, /*inplace_subtract*/
8733 (binaryfunc)Average_inplace_multiply, /*inplace_multiply*/
8734 0, /*inplace_remainder*/
8735 0, /*inplace_power*/
8736 0, /*inplace_lshift*/
8737 0, /*inplace_rshift*/
8738 0, /*inplace_and*/
8739 0, /*inplace_xor*/
8740 0, /*inplace_or*/
8741 0, /*nb_floor_divide*/
8742 (binaryfunc)Average_div, /*nb_true_divide*/
8743 0, /*nb_inplace_floor_divide*/
8744 (binaryfunc)Average_inplace_div, /*nb_inplace_true_divide*/
8745 0, /* nb_index */
8746 };
8747
8748 PyTypeObject AverageType =
8749 {
8750 PyVarObject_HEAD_INIT(NULL, 0)
8751 "_pyo.Average_base", /*tp_name*/
8752 sizeof(Average), /*tp_basicsize*/
8753 0, /*tp_itemsize*/
8754 (destructor)Average_dealloc, /*tp_dealloc*/
8755 0, /*tp_print*/
8756 0, /*tp_getattr*/
8757 0, /*tp_setattr*/
8758 0, /*tp_as_async (tp_compare in Python 2)*/
8759 0, /*tp_repr*/
8760 &Average_as_number, /*tp_as_number*/
8761 0, /*tp_as_sequence*/
8762 0, /*tp_as_mapping*/
8763 0, /*tp_hash */
8764 0, /*tp_call*/
8765 0, /*tp_str*/
8766 0, /*tp_getattro*/
8767 0, /*tp_setattro*/
8768 0, /*tp_as_buffer*/
8769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
8770 "Average objects. Moving average filter.", /* tp_doc */
8771 (traverseproc)Average_traverse, /* tp_traverse */
8772 (inquiry)Average_clear, /* tp_clear */
8773 0, /* tp_richcompare */
8774 0, /* tp_weaklistoffset */
8775 0, /* tp_iter */
8776 0, /* tp_iternext */
8777 Average_methods, /* tp_methods */
8778 Average_members, /* tp_members */
8779 0, /* tp_getset */
8780 0, /* tp_base */
8781 0, /* tp_dict */
8782 0, /* tp_descr_get */
8783 0, /* tp_descr_set */
8784 0, /* tp_dictoffset */
8785 0, /* tp_init */
8786 0, /* tp_alloc */
8787 Average_new, /* tp_new */
8788 };
8789
8790 typedef struct
8791 {
8792 pyo_audio_HEAD
8793 PyObject *input;
8794 Stream *input_stream;
8795 PyObject *freq;
8796 Stream *freq_stream;
8797 PyObject *q;
8798 Stream *q_stream;
8799 int modebuffer[4]; // need at least 2 slots for mul & add
8800 MYFLT nyquist;
8801 MYFLT last_freq;
8802 MYFLT last_q;
8803 MYFLT twopiOverSr;
8804 // sample memories
8805 MYFLT x1;
8806 MYFLT x2;
8807 MYFLT y1;
8808 MYFLT y2;
8809 // coefficients
8810 MYFLT b1;
8811 MYFLT b2;
8812 MYFLT a;
8813 } Reson;
8814
8815 static void
8816 Reson_compute_coeffs(Reson *self, MYFLT freq, MYFLT q)
8817 {
8818 MYFLT bw;
8819
8820 if (freq < 0.1)
8821 freq = 0.1;
8822 else if (freq > self->nyquist)
8823 freq = self->nyquist;
8824
8825 if (q < 0.1)
8826 q = 0.1;
8827
8828 bw = freq / q;
8829
8830 self->b2 = MYEXP(-self->twopiOverSr * bw);
8831 self->b1 = (-4.0 * self->b2) / (1.0 + self->b2) * MYCOS(freq * self->twopiOverSr);
8832 self->a = 1.0 - MYSQRT(self->b2);
8833 }
8834
8835 static void
8836 Reson_filters_ii(Reson *self)
8837 {
8838 MYFLT val, fr, q;
8839 int i;
8840 MYFLT *in = Stream_getData((Stream *)self->input_stream);
8841 fr = PyFloat_AS_DOUBLE(self->freq);
8842 q = PyFloat_AS_DOUBLE(self->q);
8843
8844 if (fr != self->last_freq || q != self->last_q)
8845 {
8846 self->last_freq = fr;
8847 self->last_q = q;
8848 Reson_compute_coeffs(self, fr, q);
8849 }
8850
8851 for (i = 0; i < self->bufsize; i++)
8852 {
8853 val = self->a * (in[i] - self->x2) - (self->b1 * self->y1) - (self->b2 * self->y2);
8854 self->y2 = self->y1;
8855 self->data[i] = self->y1 = val;
8856 self->x2 = self->x1;
8857 self->x1 = in[i];
8858 }
8859 }
8860
8861 static void
8862 Reson_filters_ai(Reson *self)
8863 {
8864 MYFLT val, fr, q;
8865 int i;
8866 MYFLT *in = Stream_getData((Stream *)self->input_stream);
8867 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
8868 q = PyFloat_AS_DOUBLE(self->q);
8869
8870 for (i = 0; i < self->bufsize; i++)
8871 {
8872 fr = freq[i];
8873
8874 if (fr != self->last_freq || q != self->last_q)
8875 {
8876 self->last_freq = fr;
8877 self->last_q = q;
8878 Reson_compute_coeffs(self, fr, q);
8879 }
8880
8881 val = self->a * (in[i] - self->x2) - (self->b1 * self->y1) - (self->b2 * self->y2);
8882 self->y2 = self->y1;
8883 self->data[i] = self->y1 = val;
8884 self->x2 = self->x1;
8885 self->x1 = in[i];
8886 }
8887 }
8888
8889 static void
8890 Reson_filters_ia(Reson *self)
8891 {
8892 MYFLT val, fr, q;
8893 int i;
8894 MYFLT *in = Stream_getData((Stream *)self->input_stream);
8895 fr = PyFloat_AS_DOUBLE(self->freq);
8896 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
8897
8898 for (i = 0; i < self->bufsize; i++)
8899 {
8900 q = qst[i];
8901
8902 if (fr != self->last_freq || q != self->last_q)
8903 {
8904 self->last_freq = fr;
8905 self->last_q = q;
8906 Reson_compute_coeffs(self, fr, q);
8907 }
8908
8909 val = self->a * (in[i] - self->x2) - (self->b1 * self->y1) - (self->b2 * self->y2);
8910 self->y2 = self->y1;
8911 self->data[i] = self->y1 = val;
8912 self->x2 = self->x1;
8913 self->x1 = in[i];
8914 }
8915 }
8916
8917 static void
8918 Reson_filters_aa(Reson *self)
8919 {
8920 MYFLT val, fr, q;
8921 int i;
8922 MYFLT *in = Stream_getData((Stream *)self->input_stream);
8923 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
8924 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
8925
8926 for (i = 0; i < self->bufsize; i++)
8927 {
8928 fr = freq[i];
8929 q = qst[i];
8930
8931 if (fr != self->last_freq || q != self->last_q)
8932 {
8933 self->last_freq = fr;
8934 self->last_q = q;
8935 Reson_compute_coeffs(self, fr, q);
8936 }
8937
8938 val = self->a * (in[i] - self->x2) - (self->b1 * self->y1) - (self->b2 * self->y2);
8939 self->y2 = self->y1;
8940 self->data[i] = self->y1 = val;
8941 self->x2 = self->x1;
8942 self->x1 = in[i];
8943 }
8944 }
8945
8946 static void Reson_postprocessing_ii(Reson *self) { POST_PROCESSING_II };
8947 static void Reson_postprocessing_ai(Reson *self) { POST_PROCESSING_AI };
8948 static void Reson_postprocessing_ia(Reson *self) { POST_PROCESSING_IA };
8949 static void Reson_postprocessing_aa(Reson *self) { POST_PROCESSING_AA };
8950 static void Reson_postprocessing_ireva(Reson *self) { POST_PROCESSING_IREVA };
8951 static void Reson_postprocessing_areva(Reson *self) { POST_PROCESSING_AREVA };
8952 static void Reson_postprocessing_revai(Reson *self) { POST_PROCESSING_REVAI };
8953 static void Reson_postprocessing_revaa(Reson *self) { POST_PROCESSING_REVAA };
8954 static void Reson_postprocessing_revareva(Reson *self) { POST_PROCESSING_REVAREVA };
8955
8956 static void
8957 Reson_setProcMode(Reson *self)
8958 {
8959 int procmode, muladdmode;
8960 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
8961 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
8962
8963 switch (procmode)
8964 {
8965 case 0:
8966 self->proc_func_ptr = Reson_filters_ii;
8967 break;
8968
8969 case 1:
8970 self->proc_func_ptr = Reson_filters_ai;
8971 break;
8972
8973 case 10:
8974 self->proc_func_ptr = Reson_filters_ia;
8975 break;
8976
8977 case 11:
8978 self->proc_func_ptr = Reson_filters_aa;
8979 break;
8980 }
8981
8982 switch (muladdmode)
8983 {
8984 case 0:
8985 self->muladd_func_ptr = Reson_postprocessing_ii;
8986 break;
8987
8988 case 1:
8989 self->muladd_func_ptr = Reson_postprocessing_ai;
8990 break;
8991
8992 case 2:
8993 self->muladd_func_ptr = Reson_postprocessing_revai;
8994 break;
8995
8996 case 10:
8997 self->muladd_func_ptr = Reson_postprocessing_ia;
8998 break;
8999
9000 case 11:
9001 self->muladd_func_ptr = Reson_postprocessing_aa;
9002 break;
9003
9004 case 12:
9005 self->muladd_func_ptr = Reson_postprocessing_revaa;
9006 break;
9007
9008 case 20:
9009 self->muladd_func_ptr = Reson_postprocessing_ireva;
9010 break;
9011
9012 case 21:
9013 self->muladd_func_ptr = Reson_postprocessing_areva;
9014 break;
9015
9016 case 22:
9017 self->muladd_func_ptr = Reson_postprocessing_revareva;
9018 break;
9019 }
9020 }
9021
9022 static void
9023 Reson_compute_next_data_frame(Reson *self)
9024 {
9025 (*self->proc_func_ptr)(self);
9026 (*self->muladd_func_ptr)(self);
9027 }
9028
9029 static int
9030 Reson_traverse(Reson *self, visitproc visit, void *arg)
9031 {
9032 pyo_VISIT
9033 Py_VISIT(self->input);
9034 Py_VISIT(self->freq);
9035 Py_VISIT(self->q);
9036 return 0;
9037 }
9038
9039 static int
9040 Reson_clear(Reson *self)
9041 {
9042 pyo_CLEAR
9043 Py_CLEAR(self->input);
9044 Py_CLEAR(self->freq);
9045 Py_CLEAR(self->q);
9046 return 0;
9047 }
9048
9049 static void
9050 Reson_dealloc(Reson* self)
9051 {
9052 pyo_DEALLOC
9053 Reson_clear(self);
9054 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
9055 Py_TYPE(self)->tp_free((PyObject*)self);
9056 }
9057
9058 static PyObject *
9059 Reson_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
9060 {
9061 int i;
9062 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
9063 Reson *self;
9064 self = (Reson *)type->tp_alloc(type, 0);
9065
9066 self->freq = PyFloat_FromDouble(1000);
9067 self->q = PyFloat_FromDouble(1);
9068 self->last_freq = self->last_q = -1.0;
9069 self->x1 = self->x2 = self->y1 = self->y2 = 0.0;
9070 self->a = self->b1 = self->b2 = 0.0;
9071 self->modebuffer[0] = 0;
9072 self->modebuffer[1] = 0;
9073 self->modebuffer[2] = 0;
9074 self->modebuffer[3] = 0;
9075
9076 INIT_OBJECT_COMMON
9077
9078 self->nyquist = (MYFLT)self->sr * 0.49;
9079 self->twopiOverSr = TWOPI / (MYFLT)self->sr;
9080
9081 Stream_setFunctionPtr(self->stream, Reson_compute_next_data_frame);
9082 self->mode_func_ptr = Reson_setProcMode;
9083
9084 static char *kwlist[] = {"input", "freq", "q", "mul", "add", NULL};
9085
9086 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &qtmp, &multmp, &addtmp))
9087 Py_RETURN_NONE;
9088
9089 INIT_INPUT_STREAM
9090
9091 if (freqtmp)
9092 {
9093 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
9094 }
9095
9096 if (qtmp)
9097 {
9098 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
9099 }
9100
9101 if (multmp)
9102 {
9103 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
9104 }
9105
9106 if (addtmp)
9107 {
9108 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
9109 }
9110
9111 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
9112
9113 (*self->mode_func_ptr)(self);
9114
9115 return (PyObject *)self;
9116 }
9117
9118 static PyObject * Reson_getServer(Reson* self) { GET_SERVER };
9119 static PyObject * Reson_getStream(Reson* self) { GET_STREAM };
9120 static PyObject * Reson_setMul(Reson *self, PyObject *arg) { SET_MUL };
9121 static PyObject * Reson_setAdd(Reson *self, PyObject *arg) { SET_ADD };
9122 static PyObject * Reson_setSub(Reson *self, PyObject *arg) { SET_SUB };
9123 static PyObject * Reson_setDiv(Reson *self, PyObject *arg) { SET_DIV };
9124
9125 static PyObject * Reson_play(Reson *self, PyObject *args, PyObject *kwds) { PLAY };
9126 static PyObject * Reson_out(Reson *self, PyObject *args, PyObject *kwds) { OUT };
9127 static PyObject * Reson_stop(Reson *self, PyObject *args, PyObject *kwds) { STOP };
9128
9129 static PyObject * Reson_multiply(Reson *self, PyObject *arg) { MULTIPLY };
9130 static PyObject * Reson_inplace_multiply(Reson *self, PyObject *arg) { INPLACE_MULTIPLY };
9131 static PyObject * Reson_add(Reson *self, PyObject *arg) { ADD };
9132 static PyObject * Reson_inplace_add(Reson *self, PyObject *arg) { INPLACE_ADD };
9133 static PyObject * Reson_sub(Reson *self, PyObject *arg) { SUB };
9134 static PyObject * Reson_inplace_sub(Reson *self, PyObject *arg) { INPLACE_SUB };
9135 static PyObject * Reson_div(Reson *self, PyObject *arg) { DIV };
9136 static PyObject * Reson_inplace_div(Reson *self, PyObject *arg) { INPLACE_DIV };
9137
9138 static PyObject * Reson_setFreq(Reson *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
9139 static PyObject * Reson_setQ(Reson *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
9140
9141 static PyMemberDef Reson_members[] =
9142 {
9143 {"server", T_OBJECT_EX, offsetof(Reson, server), 0, "Pyo server."},
9144 {"stream", T_OBJECT_EX, offsetof(Reson, stream), 0, "Stream object."},
9145 {"input", T_OBJECT_EX, offsetof(Reson, input), 0, "Input sound object."},
9146 {"freq", T_OBJECT_EX, offsetof(Reson, freq), 0, "Cutoff frequency in cycle per second."},
9147 {"q", T_OBJECT_EX, offsetof(Reson, q), 0, "Q factor."},
9148 {"mul", T_OBJECT_EX, offsetof(Reson, mul), 0, "Mul factor."},
9149 {"add", T_OBJECT_EX, offsetof(Reson, add), 0, "Add factor."},
9150 {NULL} /* Sentinel */
9151 };
9152
9153 static PyMethodDef Reson_methods[] =
9154 {
9155 {"getServer", (PyCFunction)Reson_getServer, METH_NOARGS, "Returns server object."},
9156 {"_getStream", (PyCFunction)Reson_getStream, METH_NOARGS, "Returns stream object."},
9157 {"play", (PyCFunction)Reson_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
9158 {"out", (PyCFunction)Reson_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
9159 {"stop", (PyCFunction)Reson_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
9160 {"setFreq", (PyCFunction)Reson_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
9161 {"setQ", (PyCFunction)Reson_setQ, METH_O, "Sets filter Q factor."},
9162 {"setMul", (PyCFunction)Reson_setMul, METH_O, "Sets oscillator mul factor."},
9163 {"setAdd", (PyCFunction)Reson_setAdd, METH_O, "Sets oscillator add factor."},
9164 {"setSub", (PyCFunction)Reson_setSub, METH_O, "Sets inverse add factor."},
9165 {"setDiv", (PyCFunction)Reson_setDiv, METH_O, "Sets inverse mul factor."},
9166 {NULL} /* Sentinel */
9167 };
9168
9169 static PyNumberMethods Reson_as_number =
9170 {
9171 (binaryfunc)Reson_add, /*nb_add*/
9172 (binaryfunc)Reson_sub, /*nb_subtract*/
9173 (binaryfunc)Reson_multiply, /*nb_multiply*/
9174 0, /*nb_remainder*/
9175 0, /*nb_divmod*/
9176 0, /*nb_power*/
9177 0, /*nb_neg*/
9178 0, /*nb_pos*/
9179 0, /*(unaryfunc)array_abs,*/
9180 0, /*nb_nonzero*/
9181 0, /*nb_invert*/
9182 0, /*nb_lshift*/
9183 0, /*nb_rshift*/
9184 0, /*nb_and*/
9185 0, /*nb_xor*/
9186 0, /*nb_or*/
9187 0, /*nb_int*/
9188 0, /*nb_long*/
9189 0, /*nb_float*/
9190 (binaryfunc)Reson_inplace_add, /*inplace_add*/
9191 (binaryfunc)Reson_inplace_sub, /*inplace_subtract*/
9192 (binaryfunc)Reson_inplace_multiply, /*inplace_multiply*/
9193 0, /*inplace_remainder*/
9194 0, /*inplace_power*/
9195 0, /*inplace_lshift*/
9196 0, /*inplace_rshift*/
9197 0, /*inplace_and*/
9198 0, /*inplace_xor*/
9199 0, /*inplace_or*/
9200 0, /*nb_floor_divide*/
9201 (binaryfunc)Reson_div, /*nb_true_divide*/
9202 0, /*nb_inplace_floor_divide*/
9203 (binaryfunc)Reson_inplace_div, /*nb_inplace_true_divide*/
9204 0, /* nb_index */
9205 };
9206
9207 PyTypeObject ResonType =
9208 {
9209 PyVarObject_HEAD_INIT(NULL, 0)
9210 "_pyo.Reson_base", /*tp_name*/
9211 sizeof(Reson), /*tp_basicsize*/
9212 0, /*tp_itemsize*/
9213 (destructor)Reson_dealloc, /*tp_dealloc*/
9214 0, /*tp_print*/
9215 0, /*tp_getattr*/
9216 0, /*tp_setattr*/
9217 0, /*tp_as_async (tp_compare in Python 2)*/
9218 0, /*tp_repr*/
9219 &Reson_as_number, /*tp_as_number*/
9220 0, /*tp_as_sequence*/
9221 0, /*tp_as_mapping*/
9222 0, /*tp_hash */
9223 0, /*tp_call*/
9224 0, /*tp_str*/
9225 0, /*tp_getattro*/
9226 0, /*tp_setattro*/
9227 0, /*tp_as_buffer*/
9228 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
9229 "Reson objects. Second-order resonant bandpass filter.", /* tp_doc */
9230 (traverseproc)Reson_traverse, /* tp_traverse */
9231 (inquiry)Reson_clear, /* tp_clear */
9232 0, /* tp_richcompare */
9233 0, /* tp_weaklistoffset */
9234 0, /* tp_iter */
9235 0, /* tp_iternext */
9236 Reson_methods, /* tp_methods */
9237 Reson_members, /* tp_members */
9238 0, /* tp_getset */
9239 0, /* tp_base */
9240 0, /* tp_dict */
9241 0, /* tp_descr_get */
9242 0, /* tp_descr_set */
9243 0, /* tp_dictoffset */
9244 0, /* tp_init */
9245 0, /* tp_alloc */
9246 Reson_new, /* tp_new */
9247 };
9248
9249 typedef struct
9250 {
9251 pyo_audio_HEAD
9252 PyObject *input;
9253 Stream *input_stream;
9254 PyObject *freq;
9255 Stream *freq_stream;
9256 PyObject *q;
9257 Stream *q_stream;
9258 int modebuffer[4]; // need at least 2 slots for mul & add
9259 int stages;
9260 MYFLT nyquist;
9261 MYFLT last_freq;
9262 MYFLT last_q;
9263 MYFLT twopiOverSr;
9264 // sample memories
9265 MYFLT *x1;
9266 MYFLT *x2;
9267 MYFLT *y1;
9268 MYFLT *y2;
9269 // coefficients
9270 MYFLT b1;
9271 MYFLT b2;
9272 MYFLT a;
9273 } Resonx;
9274
9275 static void
9276 Resonx_allocate_memories(Resonx *self)
9277 {
9278 int i;
9279 self->x1 = (MYFLT *)PyMem_RawRealloc(self->x1, self->stages * sizeof(MYFLT));
9280 self->x2 = (MYFLT *)PyMem_RawRealloc(self->x2, self->stages * sizeof(MYFLT));
9281 self->y1 = (MYFLT *)PyMem_RawRealloc(self->y1, self->stages * sizeof(MYFLT));
9282 self->y2 = (MYFLT *)PyMem_RawRealloc(self->y2, self->stages * sizeof(MYFLT));
9283
9284 for (i = 0; i < self->stages; i++)
9285 {
9286 self->x1[i] = self->x2[i] = self->y1[i] = self->y2[i] = 0.0;
9287 }
9288 }
9289
9290 static void
9291 Resonx_compute_coeffs(Resonx *self, MYFLT freq, MYFLT q)
9292 {
9293 MYFLT bw;
9294
9295 if (freq < 0.1)
9296 freq = 0.1;
9297 else if (freq > self->nyquist)
9298 freq = self->nyquist;
9299
9300 if (q < 0.1)
9301 q = 0.1;
9302
9303 bw = freq / q;
9304
9305 self->b2 = MYEXP(-self->twopiOverSr * bw);
9306 self->b1 = (-4.0 * self->b2) / (1.0 + self->b2) * MYCOS(freq * self->twopiOverSr);
9307 self->a = 1.0 - MYSQRT(self->b2);
9308 }
9309
9310 static void
9311 Resonx_filters_ii(Resonx *self)
9312 {
9313 MYFLT vin, vout, fr, q;
9314 int i, j;
9315 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9316 fr = PyFloat_AS_DOUBLE(self->freq);
9317 q = PyFloat_AS_DOUBLE(self->q);
9318
9319 if (fr != self->last_freq || q != self->last_q)
9320 {
9321 self->last_freq = fr;
9322 self->last_q = q;
9323 Resonx_compute_coeffs(self, fr, q);
9324 }
9325
9326 vout = 0.0;
9327
9328 for (i = 0; i < self->bufsize; i++)
9329 {
9330 vin = in[i];
9331
9332 for (j = 0; j < self->stages; j++)
9333 {
9334 vout = self->a * (vin - self->x2[j]) - (self->b1 * self->y1[j]) - (self->b2 * self->y2[j]);
9335 self->x2[j] = self->x1[j];
9336 self->x1[j] = vin;
9337 self->y2[j] = self->y1[j];
9338 self->y1[j] = vin = vout;
9339 }
9340
9341 self->data[i] = vout;
9342 }
9343 }
9344
9345 static void
9346 Resonx_filters_ai(Resonx *self)
9347 {
9348 MYFLT vin, vout, fr, q;
9349 int i, j;
9350 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9351 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
9352 q = PyFloat_AS_DOUBLE(self->q);
9353
9354 vout = 0.0;
9355
9356 for (i = 0; i < self->bufsize; i++)
9357 {
9358 vin = in[i];
9359 fr = freq[i];
9360
9361 if (fr != self->last_freq || q != self->last_q)
9362 {
9363 self->last_freq = fr;
9364 self->last_q = q;
9365 Resonx_compute_coeffs(self, fr, q);
9366 }
9367
9368 for (j = 0; j < self->stages; j++)
9369 {
9370 vout = self->a * (vin - self->x2[j]) - (self->b1 * self->y1[j]) - (self->b2 * self->y2[j]);
9371 self->x2[j] = self->x1[j];
9372 self->x1[j] = vin;
9373 self->y2[j] = self->y1[j];
9374 self->y1[j] = vin = vout;
9375 }
9376
9377 self->data[i] = vout;
9378 }
9379 }
9380
9381 static void
9382 Resonx_filters_ia(Resonx *self)
9383 {
9384 MYFLT vin, vout, fr, q;
9385 int i, j;
9386 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9387 fr = PyFloat_AS_DOUBLE(self->freq);
9388 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
9389
9390 vout = 0.0;
9391
9392 for (i = 0; i < self->bufsize; i++)
9393 {
9394 vin = in[i];
9395 q = qst[i];
9396
9397 if (fr != self->last_freq || q != self->last_q)
9398 {
9399 self->last_freq = fr;
9400 self->last_q = q;
9401 Resonx_compute_coeffs(self, fr, q);
9402 }
9403
9404 for (j = 0; j < self->stages; j++)
9405 {
9406 vout = self->a * (vin - self->x2[j]) - (self->b1 * self->y1[j]) - (self->b2 * self->y2[j]);
9407 self->x2[j] = self->x1[j];
9408 self->x1[j] = vin;
9409 self->y2[j] = self->y1[j];
9410 self->y1[j] = vin = vout;
9411 }
9412
9413 self->data[i] = vout;
9414 }
9415 }
9416
9417 static void
9418 Resonx_filters_aa(Resonx *self)
9419 {
9420 MYFLT vin, vout, fr, q;
9421 int i, j;
9422 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9423 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
9424 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
9425
9426 vout = 0.0;
9427
9428 for (i = 0; i < self->bufsize; i++)
9429 {
9430 vin = in[i];
9431 fr = freq[i];
9432 q = qst[i];
9433
9434 if (fr != self->last_freq || q != self->last_q)
9435 {
9436 self->last_freq = fr;
9437 self->last_q = q;
9438 Resonx_compute_coeffs(self, fr, q);
9439 }
9440
9441 for (j = 0; j < self->stages; j++)
9442 {
9443 vout = self->a * (vin - self->x2[j]) - (self->b1 * self->y1[j]) - (self->b2 * self->y2[j]);
9444 self->x2[j] = self->x1[j];
9445 self->x1[j] = vin;
9446 self->y2[j] = self->y1[j];
9447 self->y1[j] = vin = vout;
9448 }
9449
9450 self->data[i] = vout;
9451 }
9452 }
9453
9454 static void Resonx_postprocessing_ii(Resonx *self) { POST_PROCESSING_II };
9455 static void Resonx_postprocessing_ai(Resonx *self) { POST_PROCESSING_AI };
9456 static void Resonx_postprocessing_ia(Resonx *self) { POST_PROCESSING_IA };
9457 static void Resonx_postprocessing_aa(Resonx *self) { POST_PROCESSING_AA };
9458 static void Resonx_postprocessing_ireva(Resonx *self) { POST_PROCESSING_IREVA };
9459 static void Resonx_postprocessing_areva(Resonx *self) { POST_PROCESSING_AREVA };
9460 static void Resonx_postprocessing_revai(Resonx *self) { POST_PROCESSING_REVAI };
9461 static void Resonx_postprocessing_revaa(Resonx *self) { POST_PROCESSING_REVAA };
9462 static void Resonx_postprocessing_revareva(Resonx *self) { POST_PROCESSING_REVAREVA };
9463
9464 static void
9465 Resonx_setProcMode(Resonx *self)
9466 {
9467 int procmode, muladdmode;
9468 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
9469 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
9470
9471 switch (procmode)
9472 {
9473 case 0:
9474 self->proc_func_ptr = Resonx_filters_ii;
9475 break;
9476
9477 case 1:
9478 self->proc_func_ptr = Resonx_filters_ai;
9479 break;
9480
9481 case 10:
9482 self->proc_func_ptr = Resonx_filters_ia;
9483 break;
9484
9485 case 11:
9486 self->proc_func_ptr = Resonx_filters_aa;
9487 break;
9488 }
9489
9490 switch (muladdmode)
9491 {
9492 case 0:
9493 self->muladd_func_ptr = Resonx_postprocessing_ii;
9494 break;
9495
9496 case 1:
9497 self->muladd_func_ptr = Resonx_postprocessing_ai;
9498 break;
9499
9500 case 2:
9501 self->muladd_func_ptr = Resonx_postprocessing_revai;
9502 break;
9503
9504 case 10:
9505 self->muladd_func_ptr = Resonx_postprocessing_ia;
9506 break;
9507
9508 case 11:
9509 self->muladd_func_ptr = Resonx_postprocessing_aa;
9510 break;
9511
9512 case 12:
9513 self->muladd_func_ptr = Resonx_postprocessing_revaa;
9514 break;
9515
9516 case 20:
9517 self->muladd_func_ptr = Resonx_postprocessing_ireva;
9518 break;
9519
9520 case 21:
9521 self->muladd_func_ptr = Resonx_postprocessing_areva;
9522 break;
9523
9524 case 22:
9525 self->muladd_func_ptr = Resonx_postprocessing_revareva;
9526 break;
9527 }
9528 }
9529
9530 static void
9531 Resonx_compute_next_data_frame(Resonx *self)
9532 {
9533 (*self->proc_func_ptr)(self);
9534 (*self->muladd_func_ptr)(self);
9535 }
9536
9537 static int
9538 Resonx_traverse(Resonx *self, visitproc visit, void *arg)
9539 {
9540 pyo_VISIT
9541 Py_VISIT(self->input);
9542 Py_VISIT(self->freq);
9543 Py_VISIT(self->q);
9544 return 0;
9545 }
9546
9547 static int
9548 Resonx_clear(Resonx *self)
9549 {
9550 pyo_CLEAR
9551 Py_CLEAR(self->input);
9552 Py_CLEAR(self->freq);
9553 Py_CLEAR(self->q);
9554 return 0;
9555 }
9556
9557 static void
9558 Resonx_dealloc(Resonx* self)
9559 {
9560 pyo_DEALLOC
9561 PyMem_RawFree(self->x1);
9562 PyMem_RawFree(self->x2);
9563 PyMem_RawFree(self->y1);
9564 PyMem_RawFree(self->y2);
9565 Resonx_clear(self);
9566 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
9567 Py_TYPE(self)->tp_free((PyObject*)self);
9568 }
9569
9570 static PyObject *
9571 Resonx_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
9572 {
9573 int i;
9574 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
9575 Resonx *self;
9576 self = (Resonx *)type->tp_alloc(type, 0);
9577
9578 self->freq = PyFloat_FromDouble(1000);
9579 self->q = PyFloat_FromDouble(1);
9580 self->last_freq = self->last_q = -1.0;
9581 self->stages = 4;
9582 self->a = self->b1 = self->b2 = 0.0;
9583 self->modebuffer[0] = 0;
9584 self->modebuffer[1] = 0;
9585 self->modebuffer[2] = 0;
9586 self->modebuffer[3] = 0;
9587
9588 INIT_OBJECT_COMMON
9589
9590 self->nyquist = (MYFLT)self->sr * 0.49;
9591 self->twopiOverSr = TWOPI / (MYFLT)self->sr;
9592
9593 Stream_setFunctionPtr(self->stream, Resonx_compute_next_data_frame);
9594 self->mode_func_ptr = Resonx_setProcMode;
9595
9596 static char *kwlist[] = {"input", "freq", "q", "stages", "mul", "add", NULL};
9597
9598 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOiOO", kwlist, &inputtmp, &freqtmp, &qtmp, &self->stages, &multmp, &addtmp))
9599 Py_RETURN_NONE;
9600
9601 INIT_INPUT_STREAM
9602
9603 if (freqtmp)
9604 {
9605 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
9606 }
9607
9608 if (qtmp)
9609 {
9610 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
9611 }
9612
9613 if (multmp)
9614 {
9615 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
9616 }
9617
9618 if (addtmp)
9619 {
9620 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
9621 }
9622
9623 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
9624
9625 Resonx_allocate_memories(self);
9626
9627 (*self->mode_func_ptr)(self);
9628
9629 return (PyObject *)self;
9630 }
9631
9632 static PyObject * Resonx_getServer(Resonx* self) { GET_SERVER };
9633 static PyObject * Resonx_getStream(Resonx* self) { GET_STREAM };
9634 static PyObject * Resonx_setMul(Resonx *self, PyObject *arg) { SET_MUL };
9635 static PyObject * Resonx_setAdd(Resonx *self, PyObject *arg) { SET_ADD };
9636 static PyObject * Resonx_setSub(Resonx *self, PyObject *arg) { SET_SUB };
9637 static PyObject * Resonx_setDiv(Resonx *self, PyObject *arg) { SET_DIV };
9638
9639 static PyObject * Resonx_play(Resonx *self, PyObject *args, PyObject *kwds) { PLAY };
9640 static PyObject * Resonx_out(Resonx *self, PyObject *args, PyObject *kwds) { OUT };
9641 static PyObject * Resonx_stop(Resonx *self, PyObject *args, PyObject *kwds) { STOP };
9642
9643 static PyObject * Resonx_multiply(Resonx *self, PyObject *arg) { MULTIPLY };
9644 static PyObject * Resonx_inplace_multiply(Resonx *self, PyObject *arg) { INPLACE_MULTIPLY };
9645 static PyObject * Resonx_add(Resonx *self, PyObject *arg) { ADD };
9646 static PyObject * Resonx_inplace_add(Resonx *self, PyObject *arg) { INPLACE_ADD };
9647 static PyObject * Resonx_sub(Resonx *self, PyObject *arg) { SUB };
9648 static PyObject * Resonx_inplace_sub(Resonx *self, PyObject *arg) { INPLACE_SUB };
9649 static PyObject * Resonx_div(Resonx *self, PyObject *arg) { DIV };
9650 static PyObject * Resonx_inplace_div(Resonx *self, PyObject *arg) { INPLACE_DIV };
9651
9652 static PyObject * Resonx_setFreq(Resonx *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
9653 static PyObject * Resonx_setQ(Resonx *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
9654
9655 static PyObject *
9656 Resonx_setStages(Resonx *self, PyObject *arg)
9657 {
9658 ASSERT_ARG_NOT_NULL
9659
9660 if (PyLong_Check(arg))
9661 {
9662 self->stages = PyLong_AsLong(arg);
9663 Resonx_allocate_memories(self);
9664 }
9665
9666 Py_RETURN_NONE;
9667 }
9668
9669 static PyMemberDef Resonx_members[] =
9670 {
9671 {"server", T_OBJECT_EX, offsetof(Resonx, server), 0, "Pyo server."},
9672 {"stream", T_OBJECT_EX, offsetof(Resonx, stream), 0, "Stream object."},
9673 {"input", T_OBJECT_EX, offsetof(Resonx, input), 0, "Input sound object."},
9674 {"freq", T_OBJECT_EX, offsetof(Resonx, freq), 0, "Cutoff frequency in cycle per second."},
9675 {"q", T_OBJECT_EX, offsetof(Resonx, q), 0, "Q factor."},
9676 {"mul", T_OBJECT_EX, offsetof(Resonx, mul), 0, "Mul factor."},
9677 {"add", T_OBJECT_EX, offsetof(Resonx, add), 0, "Add factor."},
9678 {NULL} /* Sentinel */
9679 };
9680
9681 static PyMethodDef Resonx_methods[] =
9682 {
9683 {"getServer", (PyCFunction)Resonx_getServer, METH_NOARGS, "Returns server object."},
9684 {"_getStream", (PyCFunction)Resonx_getStream, METH_NOARGS, "Returns stream object."},
9685 {"play", (PyCFunction)Resonx_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
9686 {"out", (PyCFunction)Resonx_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
9687 {"stop", (PyCFunction)Resonx_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
9688 {"setFreq", (PyCFunction)Resonx_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
9689 {"setQ", (PyCFunction)Resonx_setQ, METH_O, "Sets filter Q factor."},
9690 {"setStages", (PyCFunction)Resonx_setStages, METH_O, "Sets the number of stages of the filter."},
9691 {"setMul", (PyCFunction)Resonx_setMul, METH_O, "Sets oscillator mul factor."},
9692 {"setAdd", (PyCFunction)Resonx_setAdd, METH_O, "Sets oscillator add factor."},
9693 {"setSub", (PyCFunction)Resonx_setSub, METH_O, "Sets inverse add factor."},
9694 {"setDiv", (PyCFunction)Resonx_setDiv, METH_O, "Sets inverse mul factor."},
9695 {NULL} /* Sentinel */
9696 };
9697
9698 static PyNumberMethods Resonx_as_number =
9699 {
9700 (binaryfunc)Resonx_add, /*nb_add*/
9701 (binaryfunc)Resonx_sub, /*nb_subtract*/
9702 (binaryfunc)Resonx_multiply, /*nb_multiply*/
9703 0, /*nb_remainder*/
9704 0, /*nb_divmod*/
9705 0, /*nb_power*/
9706 0, /*nb_neg*/
9707 0, /*nb_pos*/
9708 0, /*(unaryfunc)array_abs,*/
9709 0, /*nb_nonzero*/
9710 0, /*nb_invert*/
9711 0, /*nb_lshift*/
9712 0, /*nb_rshift*/
9713 0, /*nb_and*/
9714 0, /*nb_xor*/
9715 0, /*nb_or*/
9716 0, /*nb_int*/
9717 0, /*nb_long*/
9718 0, /*nb_float*/
9719 (binaryfunc)Resonx_inplace_add, /*inplace_add*/
9720 (binaryfunc)Resonx_inplace_sub, /*inplace_subtract*/
9721 (binaryfunc)Resonx_inplace_multiply, /*inplace_multiply*/
9722 0, /*inplace_remainder*/
9723 0, /*inplace_power*/
9724 0, /*inplace_lshift*/
9725 0, /*inplace_rshift*/
9726 0, /*inplace_and*/
9727 0, /*inplace_xor*/
9728 0, /*inplace_or*/
9729 0, /*nb_floor_divide*/
9730 (binaryfunc)Resonx_div, /*nb_true_divide*/
9731 0, /*nb_inplace_floor_divide*/
9732 (binaryfunc)Resonx_inplace_div, /*nb_inplace_true_divide*/
9733 0, /* nb_index */
9734 };
9735
9736 PyTypeObject ResonxType =
9737 {
9738 PyVarObject_HEAD_INIT(NULL, 0)
9739 "_pyo.Resonx_base", /*tp_name*/
9740 sizeof(Resonx), /*tp_basicsize*/
9741 0, /*tp_itemsize*/
9742 (destructor)Resonx_dealloc, /*tp_dealloc*/
9743 0, /*tp_print*/
9744 0, /*tp_getattr*/
9745 0, /*tp_setattr*/
9746 0, /*tp_as_async (tp_compare in Python 2)*/
9747 0, /*tp_repr*/
9748 &Resonx_as_number, /*tp_as_number*/
9749 0, /*tp_as_sequence*/
9750 0, /*tp_as_mapping*/
9751 0, /*tp_hash */
9752 0, /*tp_call*/
9753 0, /*tp_str*/
9754 0, /*tp_getattro*/
9755 0, /*tp_setattro*/
9756 0, /*tp_as_buffer*/
9757 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
9758 "Resonx objects. Cascade of second-order Resonant bandpass filter.", /* tp_doc */
9759 (traverseproc)Resonx_traverse, /* tp_traverse */
9760 (inquiry)Resonx_clear, /* tp_clear */
9761 0, /* tp_richcompare */
9762 0, /* tp_weaklistoffset */
9763 0, /* tp_iter */
9764 0, /* tp_iternext */
9765 Resonx_methods, /* tp_methods */
9766 Resonx_members, /* tp_members */
9767 0, /* tp_getset */
9768 0, /* tp_base */
9769 0, /* tp_dict */
9770 0, /* tp_descr_get */
9771 0, /* tp_descr_set */
9772 0, /* tp_dictoffset */
9773 0, /* tp_init */
9774 0, /* tp_alloc */
9775 Resonx_new, /* tp_new */
9776 };
9777
9778 /************/
9779 /* ButLP */
9780 /************/
9781 typedef struct
9782 {
9783 pyo_audio_HEAD
9784 PyObject *input;
9785 Stream *input_stream;
9786 PyObject *freq;
9787 Stream *freq_stream;
9788 int modebuffer[3]; // need at least 2 slots for mul & add
9789 MYFLT lastFreq;
9790 MYFLT nyquist;
9791 MYFLT piOnSr;
9792 MYFLT sqrt2;
9793 // sample memories
9794 MYFLT x1;
9795 MYFLT x2;
9796 MYFLT y1;
9797 MYFLT y2;
9798 // variables
9799 MYFLT a0;
9800 MYFLT a1;
9801 MYFLT a2;
9802 MYFLT b1;
9803 MYFLT b2;
9804 } ButLP;
9805
9806 static void
9807 ButLP_filters_i(ButLP *self)
9808 {
9809 MYFLT val, c, c2;
9810 int i;
9811 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9812 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
9813
9814 if (fr != self->lastFreq)
9815 {
9816 if (fr < 0.1)
9817 fr = 0.1;
9818 else if (fr >= self->nyquist)
9819 fr = self->nyquist;
9820
9821 self->lastFreq = fr;
9822 c = 1.0 / MYTAN(self->piOnSr * fr);
9823 c2 = c * c;
9824 self->a0 = self->a2 = 1.0 / (1.0 + self->sqrt2 * c + c2);
9825 self->a1 = 2.0 * self->a0;
9826 self->b1 = self->a1 * (1.0 - c2);
9827 self->b2 = self->a0 * (1.0 - self->sqrt2 * c + c2);
9828 }
9829
9830 for (i = 0; i < self->bufsize; i++)
9831 {
9832 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
9833 self->x2 = self->x1;
9834 self->x1 = in[i];
9835 self->y2 = self->y1;
9836 self->data[i] = self->y1 = val;
9837 }
9838 }
9839
9840 static void
9841 ButLP_filters_a(ButLP *self)
9842 {
9843 MYFLT val, fr, c, c2;
9844 int i;
9845 MYFLT *in = Stream_getData((Stream *)self->input_stream);
9846 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
9847
9848 for (i = 0; i < self->bufsize; i++)
9849 {
9850 fr = freq[i];
9851
9852 if (fr != self->lastFreq)
9853 {
9854 if (fr < 0.1)
9855 fr = 0.1;
9856 else if (fr >= self->nyquist)
9857 fr = self->nyquist;
9858
9859 self->lastFreq = fr;
9860 c = 1.0 / MYTAN(self->piOnSr * fr);
9861 c2 = c * c;
9862 self->a0 = self->a2 = 1.0 / (1.0 + self->sqrt2 * c + c2);
9863 self->a1 = 2.0 * self->a0;
9864 self->b1 = self->a1 * (1.0 - c2);
9865 self->b2 = self->a0 * (1.0 - self->sqrt2 * c + c2);
9866 }
9867
9868 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
9869 self->x2 = self->x1;
9870 self->x1 = in[i];
9871 self->y2 = self->y1;
9872 self->data[i] = self->y1 = val;
9873 }
9874 }
9875
9876 static void ButLP_postprocessing_ii(ButLP *self) { POST_PROCESSING_II };
9877 static void ButLP_postprocessing_ai(ButLP *self) { POST_PROCESSING_AI };
9878 static void ButLP_postprocessing_ia(ButLP *self) { POST_PROCESSING_IA };
9879 static void ButLP_postprocessing_aa(ButLP *self) { POST_PROCESSING_AA };
9880 static void ButLP_postprocessing_ireva(ButLP *self) { POST_PROCESSING_IREVA };
9881 static void ButLP_postprocessing_areva(ButLP *self) { POST_PROCESSING_AREVA };
9882 static void ButLP_postprocessing_revai(ButLP *self) { POST_PROCESSING_REVAI };
9883 static void ButLP_postprocessing_revaa(ButLP *self) { POST_PROCESSING_REVAA };
9884 static void ButLP_postprocessing_revareva(ButLP *self) { POST_PROCESSING_REVAREVA };
9885
9886 static void
9887 ButLP_setProcMode(ButLP *self)
9888 {
9889 int procmode, muladdmode;
9890 procmode = self->modebuffer[2];
9891 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
9892
9893 switch (procmode)
9894 {
9895 case 0:
9896 self->proc_func_ptr = ButLP_filters_i;
9897 break;
9898
9899 case 1:
9900 self->proc_func_ptr = ButLP_filters_a;
9901 break;
9902 }
9903
9904 switch (muladdmode)
9905 {
9906 case 0:
9907 self->muladd_func_ptr = ButLP_postprocessing_ii;
9908 break;
9909
9910 case 1:
9911 self->muladd_func_ptr = ButLP_postprocessing_ai;
9912 break;
9913
9914 case 2:
9915 self->muladd_func_ptr = ButLP_postprocessing_revai;
9916 break;
9917
9918 case 10:
9919 self->muladd_func_ptr = ButLP_postprocessing_ia;
9920 break;
9921
9922 case 11:
9923 self->muladd_func_ptr = ButLP_postprocessing_aa;
9924 break;
9925
9926 case 12:
9927 self->muladd_func_ptr = ButLP_postprocessing_revaa;
9928 break;
9929
9930 case 20:
9931 self->muladd_func_ptr = ButLP_postprocessing_ireva;
9932 break;
9933
9934 case 21:
9935 self->muladd_func_ptr = ButLP_postprocessing_areva;
9936 break;
9937
9938 case 22:
9939 self->muladd_func_ptr = ButLP_postprocessing_revareva;
9940 break;
9941 }
9942 }
9943
9944 static void
9945 ButLP_compute_next_data_frame(ButLP *self)
9946 {
9947 (*self->proc_func_ptr)(self);
9948 (*self->muladd_func_ptr)(self);
9949 }
9950
9951 static int
9952 ButLP_traverse(ButLP *self, visitproc visit, void *arg)
9953 {
9954 pyo_VISIT
9955 Py_VISIT(self->input);
9956 Py_VISIT(self->freq);
9957 return 0;
9958 }
9959
9960 static int
9961 ButLP_clear(ButLP *self)
9962 {
9963 pyo_CLEAR
9964 Py_CLEAR(self->input);
9965 Py_CLEAR(self->freq);
9966 return 0;
9967 }
9968
9969 static void
9970 ButLP_dealloc(ButLP* self)
9971 {
9972 pyo_DEALLOC
9973 ButLP_clear(self);
9974 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
9975 Py_TYPE(self)->tp_free((PyObject*)self);
9976 }
9977
9978 static PyObject *
9979 ButLP_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
9980 {
9981 int i;
9982 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
9983 ButLP *self;
9984 self = (ButLP *)type->tp_alloc(type, 0);
9985
9986 self->freq = PyFloat_FromDouble(1000);
9987 self->lastFreq = -1.0;
9988 self->x1 = self->x2 = self->y1 = self->y2 = self->a0 = self->a1 = self->a2 = self->b1 = self->b2 = 0.0;
9989 self->modebuffer[0] = 0;
9990 self->modebuffer[1] = 0;
9991 self->modebuffer[2] = 0;
9992
9993 INIT_OBJECT_COMMON
9994
9995 self->nyquist = (MYFLT)self->sr * 0.49;
9996 self->piOnSr = PI / (MYFLT)self->sr;
9997 self->sqrt2 = MYSQRT(2.0);
9998
9999 Stream_setFunctionPtr(self->stream, ButLP_compute_next_data_frame);
10000 self->mode_func_ptr = ButLP_setProcMode;
10001
10002 static char *kwlist[] = {"input", "freq", "mul", "add", NULL};
10003
10004 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freqtmp, &multmp, &addtmp))
10005 Py_RETURN_NONE;
10006
10007 INIT_INPUT_STREAM
10008
10009 if (freqtmp)
10010 {
10011 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
10012 }
10013
10014 if (multmp)
10015 {
10016 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
10017 }
10018
10019 if (addtmp)
10020 {
10021 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
10022 }
10023
10024 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
10025
10026 (*self->mode_func_ptr)(self);
10027
10028 return (PyObject *)self;
10029 }
10030
10031 static PyObject * ButLP_getServer(ButLP* self) { GET_SERVER };
10032 static PyObject * ButLP_getStream(ButLP* self) { GET_STREAM };
10033 static PyObject * ButLP_setMul(ButLP *self, PyObject *arg) { SET_MUL };
10034 static PyObject * ButLP_setAdd(ButLP *self, PyObject *arg) { SET_ADD };
10035 static PyObject * ButLP_setSub(ButLP *self, PyObject *arg) { SET_SUB };
10036 static PyObject * ButLP_setDiv(ButLP *self, PyObject *arg) { SET_DIV };
10037
10038 static PyObject * ButLP_play(ButLP *self, PyObject *args, PyObject *kwds) { PLAY };
10039 static PyObject * ButLP_out(ButLP *self, PyObject *args, PyObject *kwds) { OUT };
10040 static PyObject * ButLP_stop(ButLP *self, PyObject *args, PyObject *kwds) { STOP };
10041
10042 static PyObject * ButLP_multiply(ButLP *self, PyObject *arg) { MULTIPLY };
10043 static PyObject * ButLP_inplace_multiply(ButLP *self, PyObject *arg) { INPLACE_MULTIPLY };
10044 static PyObject * ButLP_add(ButLP *self, PyObject *arg) { ADD };
10045 static PyObject * ButLP_inplace_add(ButLP *self, PyObject *arg) { INPLACE_ADD };
10046 static PyObject * ButLP_sub(ButLP *self, PyObject *arg) { SUB };
10047 static PyObject * ButLP_inplace_sub(ButLP *self, PyObject *arg) { INPLACE_SUB };
10048 static PyObject * ButLP_div(ButLP *self, PyObject *arg) { DIV };
10049 static PyObject * ButLP_inplace_div(ButLP *self, PyObject *arg) { INPLACE_DIV };
10050
10051 static PyObject * ButLP_setFreq(ButLP *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
10052
10053 static PyMemberDef ButLP_members[] =
10054 {
10055 {"server", T_OBJECT_EX, offsetof(ButLP, server), 0, "Pyo server."},
10056 {"stream", T_OBJECT_EX, offsetof(ButLP, stream), 0, "Stream object."},
10057 {"input", T_OBJECT_EX, offsetof(ButLP, input), 0, "Input sound object."},
10058 {"freq", T_OBJECT_EX, offsetof(ButLP, freq), 0, "Cutoff frequency in cycle per second."},
10059 {"mul", T_OBJECT_EX, offsetof(ButLP, mul), 0, "Mul factor."},
10060 {"add", T_OBJECT_EX, offsetof(ButLP, add), 0, "Add factor."},
10061 {NULL} /* Sentinel */
10062 };
10063
10064 static PyMethodDef ButLP_methods[] =
10065 {
10066 {"getServer", (PyCFunction)ButLP_getServer, METH_NOARGS, "Returns server object."},
10067 {"_getStream", (PyCFunction)ButLP_getStream, METH_NOARGS, "Returns stream object."},
10068 {"play", (PyCFunction)ButLP_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
10069 {"out", (PyCFunction)ButLP_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
10070 {"stop", (PyCFunction)ButLP_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
10071 {"setFreq", (PyCFunction)ButLP_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
10072 {"setMul", (PyCFunction)ButLP_setMul, METH_O, "Sets oscillator mul factor."},
10073 {"setAdd", (PyCFunction)ButLP_setAdd, METH_O, "Sets oscillator add factor."},
10074 {"setSub", (PyCFunction)ButLP_setSub, METH_O, "Sets inverse add factor."},
10075 {"setDiv", (PyCFunction)ButLP_setDiv, METH_O, "Sets inverse mul factor."},
10076 {NULL} /* Sentinel */
10077 };
10078
10079 static PyNumberMethods ButLP_as_number =
10080 {
10081 (binaryfunc)ButLP_add, /*nb_add*/
10082 (binaryfunc)ButLP_sub, /*nb_subtract*/
10083 (binaryfunc)ButLP_multiply, /*nb_multiply*/
10084 0, /*nb_remainder*/
10085 0, /*nb_divmod*/
10086 0, /*nb_power*/
10087 0, /*nb_neg*/
10088 0, /*nb_pos*/
10089 0, /*(unaryfunc)array_abs,*/
10090 0, /*nb_nonzero*/
10091 0, /*nb_invert*/
10092 0, /*nb_lshift*/
10093 0, /*nb_rshift*/
10094 0, /*nb_and*/
10095 0, /*nb_xor*/
10096 0, /*nb_or*/
10097 0, /*nb_int*/
10098 0, /*nb_long*/
10099 0, /*nb_float*/
10100 (binaryfunc)ButLP_inplace_add, /*inplace_add*/
10101 (binaryfunc)ButLP_inplace_sub, /*inplace_subtract*/
10102 (binaryfunc)ButLP_inplace_multiply, /*inplace_multiply*/
10103 0, /*inplace_remainder*/
10104 0, /*inplace_power*/
10105 0, /*inplace_lshift*/
10106 0, /*inplace_rshift*/
10107 0, /*inplace_and*/
10108 0, /*inplace_xor*/
10109 0, /*inplace_or*/
10110 0, /*nb_floor_divide*/
10111 (binaryfunc)ButLP_div, /*nb_true_divide*/
10112 0, /*nb_inplace_floor_divide*/
10113 (binaryfunc)ButLP_inplace_div, /*nb_inplace_true_divide*/
10114 0, /* nb_index */
10115 };
10116
10117 PyTypeObject ButLPType =
10118 {
10119 PyVarObject_HEAD_INIT(NULL, 0)
10120 "_pyo.ButLP_base", /*tp_name*/
10121 sizeof(ButLP), /*tp_basicsize*/
10122 0, /*tp_itemsize*/
10123 (destructor)ButLP_dealloc, /*tp_dealloc*/
10124 0, /*tp_print*/
10125 0, /*tp_getattr*/
10126 0, /*tp_setattr*/
10127 0, /*tp_as_async (tp_compare in Python 2)*/
10128 0, /*tp_repr*/
10129 &ButLP_as_number, /*tp_as_number*/
10130 0, /*tp_as_sequence*/
10131 0, /*tp_as_mapping*/
10132 0, /*tp_hash */
10133 0, /*tp_call*/
10134 0, /*tp_str*/
10135 0, /*tp_getattro*/
10136 0, /*tp_setattro*/
10137 0, /*tp_as_buffer*/
10138 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
10139 "ButLP objects. Second-order Butterworth lowpass filter.", /* tp_doc */
10140 (traverseproc)ButLP_traverse, /* tp_traverse */
10141 (inquiry)ButLP_clear, /* tp_clear */
10142 0, /* tp_richcompare */
10143 0, /* tp_weaklistoffset */
10144 0, /* tp_iter */
10145 0, /* tp_iternext */
10146 ButLP_methods, /* tp_methods */
10147 ButLP_members, /* tp_members */
10148 0, /* tp_getset */
10149 0, /* tp_base */
10150 0, /* tp_dict */
10151 0, /* tp_descr_get */
10152 0, /* tp_descr_set */
10153 0, /* tp_dictoffset */
10154 0, /* tp_init */
10155 0, /* tp_alloc */
10156 ButLP_new, /* tp_new */
10157 };
10158
10159 /************/
10160 /* ButHP */
10161 /************/
10162 typedef struct
10163 {
10164 pyo_audio_HEAD
10165 PyObject *input;
10166 Stream *input_stream;
10167 PyObject *freq;
10168 Stream *freq_stream;
10169 int modebuffer[3]; // need at least 2 slots for mul & add
10170 MYFLT lastFreq;
10171 MYFLT nyquist;
10172 MYFLT piOnSr;
10173 MYFLT sqrt2;
10174 // sample memories
10175 MYFLT x1;
10176 MYFLT x2;
10177 MYFLT y1;
10178 MYFLT y2;
10179 // variables
10180 MYFLT a0;
10181 MYFLT a1;
10182 MYFLT a2;
10183 MYFLT b1;
10184 MYFLT b2;
10185 } ButHP;
10186
10187 static void
10188 ButHP_filters_i(ButHP *self)
10189 {
10190 MYFLT val, c, c2;
10191 int i;
10192 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10193 MYFLT fr = PyFloat_AS_DOUBLE(self->freq);
10194
10195 if (fr != self->lastFreq)
10196 {
10197 if (fr < 0.1)
10198 fr = 0.1;
10199 else if (fr >= self->nyquist)
10200 fr = self->nyquist;
10201
10202 self->lastFreq = fr;
10203 c = MYTAN(self->piOnSr * fr);
10204 c2 = c * c;
10205 self->a0 = self->a2 = 1.0 / (1.0 + self->sqrt2 * c + c2);
10206 self->a1 = -2.0 * self->a0;
10207 self->b1 = 2.0 * self->a0 * (c2 - 1.0);
10208 self->b2 = self->a0 * (1.0 - self->sqrt2 * c + c2);
10209 }
10210
10211 for (i = 0; i < self->bufsize; i++)
10212 {
10213 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10214 self->x2 = self->x1;
10215 self->x1 = in[i];
10216 self->y2 = self->y1;
10217 self->data[i] = self->y1 = val;
10218 }
10219 }
10220
10221 static void
10222 ButHP_filters_a(ButHP *self)
10223 {
10224 MYFLT val, fr, c, c2;
10225 int i;
10226 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10227 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
10228
10229 for (i = 0; i < self->bufsize; i++)
10230 {
10231 fr = freq[i];
10232
10233 if (fr != self->lastFreq)
10234 {
10235 if (fr < 0.1)
10236 fr = 0.1;
10237 else if (fr >= self->nyquist)
10238 fr = self->nyquist;
10239
10240 self->lastFreq = fr;
10241 c = MYTAN(self->piOnSr * fr);
10242 c2 = c * c;
10243 self->a0 = self->a2 = 1.0 / (1.0 + self->sqrt2 * c + c2);
10244 self->a1 = -2.0 * self->a0;
10245 self->b1 = 2.0 * self->a0 * (c2 - 1.0);
10246 self->b2 = self->a0 * (1.0 - self->sqrt2 * c + c2);
10247 }
10248
10249 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10250 self->x2 = self->x1;
10251 self->x1 = in[i];
10252 self->y2 = self->y1;
10253 self->data[i] = self->y1 = val;
10254 }
10255 }
10256
10257 static void ButHP_postprocessing_ii(ButHP *self) { POST_PROCESSING_II };
10258 static void ButHP_postprocessing_ai(ButHP *self) { POST_PROCESSING_AI };
10259 static void ButHP_postprocessing_ia(ButHP *self) { POST_PROCESSING_IA };
10260 static void ButHP_postprocessing_aa(ButHP *self) { POST_PROCESSING_AA };
10261 static void ButHP_postprocessing_ireva(ButHP *self) { POST_PROCESSING_IREVA };
10262 static void ButHP_postprocessing_areva(ButHP *self) { POST_PROCESSING_AREVA };
10263 static void ButHP_postprocessing_revai(ButHP *self) { POST_PROCESSING_REVAI };
10264 static void ButHP_postprocessing_revaa(ButHP *self) { POST_PROCESSING_REVAA };
10265 static void ButHP_postprocessing_revareva(ButHP *self) { POST_PROCESSING_REVAREVA };
10266
10267 static void
10268 ButHP_setProcMode(ButHP *self)
10269 {
10270 int procmode, muladdmode;
10271 procmode = self->modebuffer[2];
10272 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
10273
10274 switch (procmode)
10275 {
10276 case 0:
10277 self->proc_func_ptr = ButHP_filters_i;
10278 break;
10279
10280 case 1:
10281 self->proc_func_ptr = ButHP_filters_a;
10282 break;
10283 }
10284
10285 switch (muladdmode)
10286 {
10287 case 0:
10288 self->muladd_func_ptr = ButHP_postprocessing_ii;
10289 break;
10290
10291 case 1:
10292 self->muladd_func_ptr = ButHP_postprocessing_ai;
10293 break;
10294
10295 case 2:
10296 self->muladd_func_ptr = ButHP_postprocessing_revai;
10297 break;
10298
10299 case 10:
10300 self->muladd_func_ptr = ButHP_postprocessing_ia;
10301 break;
10302
10303 case 11:
10304 self->muladd_func_ptr = ButHP_postprocessing_aa;
10305 break;
10306
10307 case 12:
10308 self->muladd_func_ptr = ButHP_postprocessing_revaa;
10309 break;
10310
10311 case 20:
10312 self->muladd_func_ptr = ButHP_postprocessing_ireva;
10313 break;
10314
10315 case 21:
10316 self->muladd_func_ptr = ButHP_postprocessing_areva;
10317 break;
10318
10319 case 22:
10320 self->muladd_func_ptr = ButHP_postprocessing_revareva;
10321 break;
10322 }
10323 }
10324
10325 static void
10326 ButHP_compute_next_data_frame(ButHP *self)
10327 {
10328 (*self->proc_func_ptr)(self);
10329 (*self->muladd_func_ptr)(self);
10330 }
10331
10332 static int
10333 ButHP_traverse(ButHP *self, visitproc visit, void *arg)
10334 {
10335 pyo_VISIT
10336 Py_VISIT(self->input);
10337 Py_VISIT(self->freq);
10338 return 0;
10339 }
10340
10341 static int
10342 ButHP_clear(ButHP *self)
10343 {
10344 pyo_CLEAR
10345 Py_CLEAR(self->input);
10346 Py_CLEAR(self->freq);
10347 return 0;
10348 }
10349
10350 static void
10351 ButHP_dealloc(ButHP* self)
10352 {
10353 pyo_DEALLOC
10354 ButHP_clear(self);
10355 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
10356 Py_TYPE(self)->tp_free((PyObject*)self);
10357 }
10358
10359 static PyObject *
10360 ButHP_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10361 {
10362 int i;
10363 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *multmp = NULL, *addtmp = NULL;
10364 ButHP *self;
10365 self = (ButHP *)type->tp_alloc(type, 0);
10366
10367 self->freq = PyFloat_FromDouble(1000);
10368 self->lastFreq = -1.0;
10369 self->x1 = self->x2 = self->y1 = self->y2 = self->a0 = self->a1 = self->a2 = self->b1 = self->b2 = 0.0;
10370 self->modebuffer[0] = 0;
10371 self->modebuffer[1] = 0;
10372 self->modebuffer[2] = 0;
10373
10374 INIT_OBJECT_COMMON
10375
10376 self->nyquist = (MYFLT)self->sr * 0.49;
10377 self->piOnSr = PI / (MYFLT)self->sr;
10378 self->sqrt2 = MYSQRT(2.0);
10379
10380 Stream_setFunctionPtr(self->stream, ButHP_compute_next_data_frame);
10381 self->mode_func_ptr = ButHP_setProcMode;
10382
10383 static char *kwlist[] = {"input", "freq", "mul", "add", NULL};
10384
10385 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &inputtmp, &freqtmp, &multmp, &addtmp))
10386 Py_RETURN_NONE;
10387
10388 INIT_INPUT_STREAM
10389
10390 if (freqtmp)
10391 {
10392 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
10393 }
10394
10395 if (multmp)
10396 {
10397 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
10398 }
10399
10400 if (addtmp)
10401 {
10402 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
10403 }
10404
10405 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
10406
10407 (*self->mode_func_ptr)(self);
10408
10409 return (PyObject *)self;
10410 }
10411
10412 static PyObject * ButHP_getServer(ButHP* self) { GET_SERVER };
10413 static PyObject * ButHP_getStream(ButHP* self) { GET_STREAM };
10414 static PyObject * ButHP_setMul(ButHP *self, PyObject *arg) { SET_MUL };
10415 static PyObject * ButHP_setAdd(ButHP *self, PyObject *arg) { SET_ADD };
10416 static PyObject * ButHP_setSub(ButHP *self, PyObject *arg) { SET_SUB };
10417 static PyObject * ButHP_setDiv(ButHP *self, PyObject *arg) { SET_DIV };
10418
10419 static PyObject * ButHP_play(ButHP *self, PyObject *args, PyObject *kwds) { PLAY };
10420 static PyObject * ButHP_out(ButHP *self, PyObject *args, PyObject *kwds) { OUT };
10421 static PyObject * ButHP_stop(ButHP *self, PyObject *args, PyObject *kwds) { STOP };
10422
10423 static PyObject * ButHP_multiply(ButHP *self, PyObject *arg) { MULTIPLY };
10424 static PyObject * ButHP_inplace_multiply(ButHP *self, PyObject *arg) { INPLACE_MULTIPLY };
10425 static PyObject * ButHP_add(ButHP *self, PyObject *arg) { ADD };
10426 static PyObject * ButHP_inplace_add(ButHP *self, PyObject *arg) { INPLACE_ADD };
10427 static PyObject * ButHP_sub(ButHP *self, PyObject *arg) { SUB };
10428 static PyObject * ButHP_inplace_sub(ButHP *self, PyObject *arg) { INPLACE_SUB };
10429 static PyObject * ButHP_div(ButHP *self, PyObject *arg) { DIV };
10430 static PyObject * ButHP_inplace_div(ButHP *self, PyObject *arg) { INPLACE_DIV };
10431
10432 static PyObject * ButHP_setFreq(ButHP *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
10433
10434 static PyMemberDef ButHP_members[] =
10435 {
10436 {"server", T_OBJECT_EX, offsetof(ButHP, server), 0, "Pyo server."},
10437 {"stream", T_OBJECT_EX, offsetof(ButHP, stream), 0, "Stream object."},
10438 {"input", T_OBJECT_EX, offsetof(ButHP, input), 0, "Input sound object."},
10439 {"freq", T_OBJECT_EX, offsetof(ButHP, freq), 0, "Cutoff frequency in cycle per second."},
10440 {"mul", T_OBJECT_EX, offsetof(ButHP, mul), 0, "Mul factor."},
10441 {"add", T_OBJECT_EX, offsetof(ButHP, add), 0, "Add factor."},
10442 {NULL} /* Sentinel */
10443 };
10444
10445 static PyMethodDef ButHP_methods[] =
10446 {
10447 {"getServer", (PyCFunction)ButHP_getServer, METH_NOARGS, "Returns server object."},
10448 {"_getStream", (PyCFunction)ButHP_getStream, METH_NOARGS, "Returns stream object."},
10449 {"play", (PyCFunction)ButHP_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
10450 {"out", (PyCFunction)ButHP_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
10451 {"stop", (PyCFunction)ButHP_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
10452 {"setFreq", (PyCFunction)ButHP_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
10453 {"setMul", (PyCFunction)ButHP_setMul, METH_O, "Sets oscillator mul factor."},
10454 {"setAdd", (PyCFunction)ButHP_setAdd, METH_O, "Sets oscillator add factor."},
10455 {"setSub", (PyCFunction)ButHP_setSub, METH_O, "Sets inverse add factor."},
10456 {"setDiv", (PyCFunction)ButHP_setDiv, METH_O, "Sets inverse mul factor."},
10457 {NULL} /* Sentinel */
10458 };
10459
10460 static PyNumberMethods ButHP_as_number =
10461 {
10462 (binaryfunc)ButHP_add, /*nb_add*/
10463 (binaryfunc)ButHP_sub, /*nb_subtract*/
10464 (binaryfunc)ButHP_multiply, /*nb_multiply*/
10465 0, /*nb_remainder*/
10466 0, /*nb_divmod*/
10467 0, /*nb_power*/
10468 0, /*nb_neg*/
10469 0, /*nb_pos*/
10470 0, /*(unaryfunc)array_abs,*/
10471 0, /*nb_nonzero*/
10472 0, /*nb_invert*/
10473 0, /*nb_lshift*/
10474 0, /*nb_rshift*/
10475 0, /*nb_and*/
10476 0, /*nb_xor*/
10477 0, /*nb_or*/
10478 0, /*nb_int*/
10479 0, /*nb_long*/
10480 0, /*nb_float*/
10481 (binaryfunc)ButHP_inplace_add, /*inplace_add*/
10482 (binaryfunc)ButHP_inplace_sub, /*inplace_subtract*/
10483 (binaryfunc)ButHP_inplace_multiply, /*inplace_multiply*/
10484 0, /*inplace_remainder*/
10485 0, /*inplace_power*/
10486 0, /*inplace_lshift*/
10487 0, /*inplace_rshift*/
10488 0, /*inplace_and*/
10489 0, /*inplace_xor*/
10490 0, /*inplace_or*/
10491 0, /*nb_floor_divide*/
10492 (binaryfunc)ButHP_div, /*nb_true_divide*/
10493 0, /*nb_inplace_floor_divide*/
10494 (binaryfunc)ButHP_inplace_div, /*nb_inplace_true_divide*/
10495 0, /* nb_index */
10496 };
10497
10498 PyTypeObject ButHPType =
10499 {
10500 PyVarObject_HEAD_INIT(NULL, 0)
10501 "_pyo.ButHP_base", /*tp_name*/
10502 sizeof(ButHP), /*tp_basicsize*/
10503 0, /*tp_itemsize*/
10504 (destructor)ButHP_dealloc, /*tp_dealloc*/
10505 0, /*tp_print*/
10506 0, /*tp_getattr*/
10507 0, /*tp_setattr*/
10508 0, /*tp_as_async (tp_compare in Python 2)*/
10509 0, /*tp_repr*/
10510 &ButHP_as_number, /*tp_as_number*/
10511 0, /*tp_as_sequence*/
10512 0, /*tp_as_mapping*/
10513 0, /*tp_hash */
10514 0, /*tp_call*/
10515 0, /*tp_str*/
10516 0, /*tp_getattro*/
10517 0, /*tp_setattro*/
10518 0, /*tp_as_buffer*/
10519 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
10520 "ButHP objects. Second-order Butterworth highpass filter.", /* tp_doc */
10521 (traverseproc)ButHP_traverse, /* tp_traverse */
10522 (inquiry)ButHP_clear, /* tp_clear */
10523 0, /* tp_richcompare */
10524 0, /* tp_weaklistoffset */
10525 0, /* tp_iter */
10526 0, /* tp_iternext */
10527 ButHP_methods, /* tp_methods */
10528 ButHP_members, /* tp_members */
10529 0, /* tp_getset */
10530 0, /* tp_base */
10531 0, /* tp_dict */
10532 0, /* tp_descr_get */
10533 0, /* tp_descr_set */
10534 0, /* tp_dictoffset */
10535 0, /* tp_init */
10536 0, /* tp_alloc */
10537 ButHP_new, /* tp_new */
10538 };
10539
10540 /************/
10541 /* ButBP */
10542 /************/
10543 typedef struct
10544 {
10545 pyo_audio_HEAD
10546 PyObject *input;
10547 Stream *input_stream;
10548 PyObject *freq;
10549 Stream *freq_stream;
10550 PyObject *q;
10551 Stream *q_stream;
10552 int modebuffer[4]; // need at least 2 slots for mul & add
10553 MYFLT nyquist;
10554 MYFLT last_freq;
10555 MYFLT last_q;
10556 MYFLT piOnSr;
10557 // sample memories
10558 MYFLT x1;
10559 MYFLT x2;
10560 MYFLT y1;
10561 MYFLT y2;
10562 // coefficients
10563 MYFLT a0;
10564 MYFLT a2;
10565 MYFLT b1;
10566 MYFLT b2;
10567 } ButBP;
10568
10569 static void
10570 ButBP_compute_coeffs(ButBP *self, MYFLT freq, MYFLT q)
10571 {
10572 MYFLT bw, c, d;
10573
10574 if (freq < 1.0)
10575 freq = 1.0;
10576 else if (freq > self->nyquist)
10577 freq = self->nyquist;
10578
10579 if (q < 1.0)
10580 q = 1.0;
10581
10582 bw = freq / q;
10583 c = 1.0 / MYTAN(self->piOnSr * bw);
10584 d = 2.0 * MYCOS(2.0 * self->piOnSr * freq);
10585
10586 self->a0 = 1.0 / (1.0 + c);
10587 self->a2 = -self->a0;
10588 self->b1 = self->a2 * c * d;
10589 self->b2 = self->a0 * (c - 1.0);
10590 }
10591
10592 static void
10593 ButBP_filters_ii(ButBP *self)
10594 {
10595 MYFLT val, fr, q;
10596 int i;
10597 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10598 fr = PyFloat_AS_DOUBLE(self->freq);
10599 q = PyFloat_AS_DOUBLE(self->q);
10600
10601 if (fr != self->last_freq || q != self->last_q)
10602 {
10603 self->last_freq = fr;
10604 self->last_q = q;
10605 ButBP_compute_coeffs(self, fr, q);
10606 }
10607
10608 for (i = 0; i < self->bufsize; i++)
10609 {
10610 val = self->a0 * in[i] + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10611 self->x2 = self->x1;
10612 self->x1 = in[i];
10613 self->y2 = self->y1;
10614 self->data[i] = self->y1 = val;
10615 }
10616 }
10617
10618 static void
10619 ButBP_filters_ai(ButBP *self)
10620 {
10621 MYFLT val, fr, q;
10622 int i;
10623 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10624 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
10625 q = PyFloat_AS_DOUBLE(self->q);
10626
10627 for (i = 0; i < self->bufsize; i++)
10628 {
10629 fr = freq[i];
10630
10631 if (fr != self->last_freq || q != self->last_q)
10632 {
10633 self->last_freq = fr;
10634 self->last_q = q;
10635 ButBP_compute_coeffs(self, fr, q);
10636 }
10637
10638 val = self->a0 * in[i] + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10639 self->x2 = self->x1;
10640 self->x1 = in[i];
10641 self->y2 = self->y1;
10642 self->data[i] = self->y1 = val;
10643 }
10644 }
10645
10646 static void
10647 ButBP_filters_ia(ButBP *self)
10648 {
10649 MYFLT val, fr, q;
10650 int i;
10651 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10652 fr = PyFloat_AS_DOUBLE(self->freq);
10653 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
10654
10655 for (i = 0; i < self->bufsize; i++)
10656 {
10657 q = qst[i];
10658
10659 if (fr != self->last_freq || q != self->last_q)
10660 {
10661 self->last_freq = fr;
10662 self->last_q = q;
10663 ButBP_compute_coeffs(self, fr, q);
10664 }
10665
10666 val = self->a0 * in[i] + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10667 self->x2 = self->x1;
10668 self->x1 = in[i];
10669 self->y2 = self->y1;
10670 self->data[i] = self->y1 = val;
10671 }
10672 }
10673
10674 static void
10675 ButBP_filters_aa(ButBP *self)
10676 {
10677 MYFLT val, fr, q;
10678 int i;
10679 MYFLT *in = Stream_getData((Stream *)self->input_stream);
10680 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
10681 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
10682
10683 for (i = 0; i < self->bufsize; i++)
10684 {
10685 fr = freq[i];
10686 q = qst[i];
10687
10688 if (fr != self->last_freq || q != self->last_q)
10689 {
10690 self->last_freq = fr;
10691 self->last_q = q;
10692 ButBP_compute_coeffs(self, fr, q);
10693 }
10694
10695 val = self->a0 * in[i] + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
10696 self->x2 = self->x1;
10697 self->x1 = in[i];
10698 self->y2 = self->y1;
10699 self->data[i] = self->y1 = val;
10700 }
10701 }
10702
10703 static void ButBP_postprocessing_ii(ButBP *self) { POST_PROCESSING_II };
10704 static void ButBP_postprocessing_ai(ButBP *self) { POST_PROCESSING_AI };
10705 static void ButBP_postprocessing_ia(ButBP *self) { POST_PROCESSING_IA };
10706 static void ButBP_postprocessing_aa(ButBP *self) { POST_PROCESSING_AA };
10707 static void ButBP_postprocessing_ireva(ButBP *self) { POST_PROCESSING_IREVA };
10708 static void ButBP_postprocessing_areva(ButBP *self) { POST_PROCESSING_AREVA };
10709 static void ButBP_postprocessing_revai(ButBP *self) { POST_PROCESSING_REVAI };
10710 static void ButBP_postprocessing_revaa(ButBP *self) { POST_PROCESSING_REVAA };
10711 static void ButBP_postprocessing_revareva(ButBP *self) { POST_PROCESSING_REVAREVA };
10712
10713 static void
10714 ButBP_setProcMode(ButBP *self)
10715 {
10716 int procmode, muladdmode;
10717 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
10718 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
10719
10720 switch (procmode)
10721 {
10722 case 0:
10723 self->proc_func_ptr = ButBP_filters_ii;
10724 break;
10725
10726 case 1:
10727 self->proc_func_ptr = ButBP_filters_ai;
10728 break;
10729
10730 case 10:
10731 self->proc_func_ptr = ButBP_filters_ia;
10732 break;
10733
10734 case 11:
10735 self->proc_func_ptr = ButBP_filters_aa;
10736 break;
10737 }
10738
10739 switch (muladdmode)
10740 {
10741 case 0:
10742 self->muladd_func_ptr = ButBP_postprocessing_ii;
10743 break;
10744
10745 case 1:
10746 self->muladd_func_ptr = ButBP_postprocessing_ai;
10747 break;
10748
10749 case 2:
10750 self->muladd_func_ptr = ButBP_postprocessing_revai;
10751 break;
10752
10753 case 10:
10754 self->muladd_func_ptr = ButBP_postprocessing_ia;
10755 break;
10756
10757 case 11:
10758 self->muladd_func_ptr = ButBP_postprocessing_aa;
10759 break;
10760
10761 case 12:
10762 self->muladd_func_ptr = ButBP_postprocessing_revaa;
10763 break;
10764
10765 case 20:
10766 self->muladd_func_ptr = ButBP_postprocessing_ireva;
10767 break;
10768
10769 case 21:
10770 self->muladd_func_ptr = ButBP_postprocessing_areva;
10771 break;
10772
10773 case 22:
10774 self->muladd_func_ptr = ButBP_postprocessing_revareva;
10775 break;
10776 }
10777 }
10778
10779 static void
10780 ButBP_compute_next_data_frame(ButBP *self)
10781 {
10782 (*self->proc_func_ptr)(self);
10783 (*self->muladd_func_ptr)(self);
10784 }
10785
10786 static int
10787 ButBP_traverse(ButBP *self, visitproc visit, void *arg)
10788 {
10789 pyo_VISIT
10790 Py_VISIT(self->input);
10791 Py_VISIT(self->freq);
10792 Py_VISIT(self->q);
10793 return 0;
10794 }
10795
10796 static int
10797 ButBP_clear(ButBP *self)
10798 {
10799 pyo_CLEAR
10800 Py_CLEAR(self->input);
10801 Py_CLEAR(self->freq);
10802 Py_CLEAR(self->q);
10803 return 0;
10804 }
10805
10806 static void
10807 ButBP_dealloc(ButBP* self)
10808 {
10809 pyo_DEALLOC
10810 ButBP_clear(self);
10811 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
10812 Py_TYPE(self)->tp_free((PyObject*)self);
10813 }
10814
10815 static PyObject *
10816 ButBP_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10817 {
10818 int i;
10819 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
10820 ButBP *self;
10821 self = (ButBP *)type->tp_alloc(type, 0);
10822
10823 self->freq = PyFloat_FromDouble(1000);
10824 self->q = PyFloat_FromDouble(1);
10825 self->last_freq = self->last_q = -1.0;
10826 self->x1 = self->x2 = self->y1 = self->y2 = 0.0;
10827 self->a0 = self->a2 = self->b1 = self->b2 = 0.0;
10828 self->modebuffer[0] = 0;
10829 self->modebuffer[1] = 0;
10830 self->modebuffer[2] = 0;
10831 self->modebuffer[3] = 0;
10832
10833 INIT_OBJECT_COMMON
10834
10835 self->nyquist = (MYFLT)self->sr * 0.49;
10836 self->piOnSr = PI / (MYFLT)self->sr;
10837
10838 Stream_setFunctionPtr(self->stream, ButBP_compute_next_data_frame);
10839 self->mode_func_ptr = ButBP_setProcMode;
10840
10841 static char *kwlist[] = {"input", "freq", "q", "mul", "add", NULL};
10842
10843 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &qtmp, &multmp, &addtmp))
10844 Py_RETURN_NONE;
10845
10846 INIT_INPUT_STREAM
10847
10848 if (freqtmp)
10849 {
10850 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
10851 }
10852
10853 if (qtmp)
10854 {
10855 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
10856 }
10857
10858 if (multmp)
10859 {
10860 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
10861 }
10862
10863 if (addtmp)
10864 {
10865 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
10866 }
10867
10868 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
10869
10870 (*self->mode_func_ptr)(self);
10871
10872 return (PyObject *)self;
10873 }
10874
10875 static PyObject * ButBP_getServer(ButBP* self) { GET_SERVER };
10876 static PyObject * ButBP_getStream(ButBP* self) { GET_STREAM };
10877 static PyObject * ButBP_setMul(ButBP *self, PyObject *arg) { SET_MUL };
10878 static PyObject * ButBP_setAdd(ButBP *self, PyObject *arg) { SET_ADD };
10879 static PyObject * ButBP_setSub(ButBP *self, PyObject *arg) { SET_SUB };
10880 static PyObject * ButBP_setDiv(ButBP *self, PyObject *arg) { SET_DIV };
10881
10882 static PyObject * ButBP_play(ButBP *self, PyObject *args, PyObject *kwds) { PLAY };
10883 static PyObject * ButBP_out(ButBP *self, PyObject *args, PyObject *kwds) { OUT };
10884 static PyObject * ButBP_stop(ButBP *self, PyObject *args, PyObject *kwds) { STOP };
10885
10886 static PyObject * ButBP_multiply(ButBP *self, PyObject *arg) { MULTIPLY };
10887 static PyObject * ButBP_inplace_multiply(ButBP *self, PyObject *arg) { INPLACE_MULTIPLY };
10888 static PyObject * ButBP_add(ButBP *self, PyObject *arg) { ADD };
10889 static PyObject * ButBP_inplace_add(ButBP *self, PyObject *arg) { INPLACE_ADD };
10890 static PyObject * ButBP_sub(ButBP *self, PyObject *arg) { SUB };
10891 static PyObject * ButBP_inplace_sub(ButBP *self, PyObject *arg) { INPLACE_SUB };
10892 static PyObject * ButBP_div(ButBP *self, PyObject *arg) { DIV };
10893 static PyObject * ButBP_inplace_div(ButBP *self, PyObject *arg) { INPLACE_DIV };
10894
10895 static PyObject * ButBP_setFreq(ButBP *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
10896 static PyObject * ButBP_setQ(ButBP *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
10897
10898 static PyMemberDef ButBP_members[] =
10899 {
10900 {"server", T_OBJECT_EX, offsetof(ButBP, server), 0, "Pyo server."},
10901 {"stream", T_OBJECT_EX, offsetof(ButBP, stream), 0, "Stream object."},
10902 {"input", T_OBJECT_EX, offsetof(ButBP, input), 0, "Input sound object."},
10903 {"freq", T_OBJECT_EX, offsetof(ButBP, freq), 0, "Cutoff frequency in cycle per second."},
10904 {"q", T_OBJECT_EX, offsetof(ButBP, q), 0, "Q factor."},
10905 {"mul", T_OBJECT_EX, offsetof(ButBP, mul), 0, "Mul factor."},
10906 {"add", T_OBJECT_EX, offsetof(ButBP, add), 0, "Add factor."},
10907 {NULL} /* Sentinel */
10908 };
10909
10910 static PyMethodDef ButBP_methods[] =
10911 {
10912 {"getServer", (PyCFunction)ButBP_getServer, METH_NOARGS, "Returns server object."},
10913 {"_getStream", (PyCFunction)ButBP_getStream, METH_NOARGS, "Returns stream object."},
10914 {"play", (PyCFunction)ButBP_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
10915 {"out", (PyCFunction)ButBP_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
10916 {"stop", (PyCFunction)ButBP_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
10917 {"setFreq", (PyCFunction)ButBP_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
10918 {"setQ", (PyCFunction)ButBP_setQ, METH_O, "Sets filter Q factor."},
10919 {"setMul", (PyCFunction)ButBP_setMul, METH_O, "Sets oscillator mul factor."},
10920 {"setAdd", (PyCFunction)ButBP_setAdd, METH_O, "Sets oscillator add factor."},
10921 {"setSub", (PyCFunction)ButBP_setSub, METH_O, "Sets inverse add factor."},
10922 {"setDiv", (PyCFunction)ButBP_setDiv, METH_O, "Sets inverse mul factor."},
10923 {NULL} /* Sentinel */
10924 };
10925
10926 static PyNumberMethods ButBP_as_number =
10927 {
10928 (binaryfunc)ButBP_add, /*nb_add*/
10929 (binaryfunc)ButBP_sub, /*nb_subtract*/
10930 (binaryfunc)ButBP_multiply, /*nb_multiply*/
10931 0, /*nb_remainder*/
10932 0, /*nb_divmod*/
10933 0, /*nb_power*/
10934 0, /*nb_neg*/
10935 0, /*nb_pos*/
10936 0, /*(unaryfunc)array_abs,*/
10937 0, /*nb_nonzero*/
10938 0, /*nb_invert*/
10939 0, /*nb_lshift*/
10940 0, /*nb_rshift*/
10941 0, /*nb_and*/
10942 0, /*nb_xor*/
10943 0, /*nb_or*/
10944 0, /*nb_int*/
10945 0, /*nb_long*/
10946 0, /*nb_float*/
10947 (binaryfunc)ButBP_inplace_add, /*inplace_add*/
10948 (binaryfunc)ButBP_inplace_sub, /*inplace_subtract*/
10949 (binaryfunc)ButBP_inplace_multiply, /*inplace_multiply*/
10950 0, /*inplace_remainder*/
10951 0, /*inplace_power*/
10952 0, /*inplace_lshift*/
10953 0, /*inplace_rshift*/
10954 0, /*inplace_and*/
10955 0, /*inplace_xor*/
10956 0, /*inplace_or*/
10957 0, /*nb_floor_divide*/
10958 (binaryfunc)ButBP_div, /*nb_true_divide*/
10959 0, /*nb_inplace_floor_divide*/
10960 (binaryfunc)ButBP_inplace_div, /*nb_inplace_true_divide*/
10961 0, /* nb_index */
10962 };
10963
10964 PyTypeObject ButBPType =
10965 {
10966 PyVarObject_HEAD_INIT(NULL, 0)
10967 "_pyo.ButBP_base", /*tp_name*/
10968 sizeof(ButBP), /*tp_basicsize*/
10969 0, /*tp_itemsize*/
10970 (destructor)ButBP_dealloc, /*tp_dealloc*/
10971 0, /*tp_print*/
10972 0, /*tp_getattr*/
10973 0, /*tp_setattr*/
10974 0, /*tp_as_async (tp_compare in Python 2)*/
10975 0, /*tp_repr*/
10976 &ButBP_as_number, /*tp_as_number*/
10977 0, /*tp_as_sequence*/
10978 0, /*tp_as_mapping*/
10979 0, /*tp_hash */
10980 0, /*tp_call*/
10981 0, /*tp_str*/
10982 0, /*tp_getattro*/
10983 0, /*tp_setattro*/
10984 0, /*tp_as_buffer*/
10985 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
10986 "ButBP objects. Second-order Butterworth bandpass filter.", /* tp_doc */
10987 (traverseproc)ButBP_traverse, /* tp_traverse */
10988 (inquiry)ButBP_clear, /* tp_clear */
10989 0, /* tp_richcompare */
10990 0, /* tp_weaklistoffset */
10991 0, /* tp_iter */
10992 0, /* tp_iternext */
10993 ButBP_methods, /* tp_methods */
10994 ButBP_members, /* tp_members */
10995 0, /* tp_getset */
10996 0, /* tp_base */
10997 0, /* tp_dict */
10998 0, /* tp_descr_get */
10999 0, /* tp_descr_set */
11000 0, /* tp_dictoffset */
11001 0, /* tp_init */
11002 0, /* tp_alloc */
11003 ButBP_new, /* tp_new */
11004 };
11005
11006 /************/
11007 /* ButBR */
11008 /************/
11009 typedef struct
11010 {
11011 pyo_audio_HEAD
11012 PyObject *input;
11013 Stream *input_stream;
11014 PyObject *freq;
11015 Stream *freq_stream;
11016 PyObject *q;
11017 Stream *q_stream;
11018 int modebuffer[4]; // need at least 2 slots for mul & add
11019 MYFLT nyquist;
11020 MYFLT last_freq;
11021 MYFLT last_q;
11022 MYFLT piOnSr;
11023 // sample memories
11024 MYFLT x1;
11025 MYFLT x2;
11026 MYFLT y1;
11027 MYFLT y2;
11028 // coefficients
11029 MYFLT a0;
11030 MYFLT a1;
11031 MYFLT a2;
11032 MYFLT b1;
11033 MYFLT b2;
11034 } ButBR;
11035
11036 static void
11037 ButBR_compute_coeffs(ButBR *self, MYFLT freq, MYFLT q)
11038 {
11039 MYFLT bw, c, d;
11040
11041 if (freq < 1.0)
11042 freq = 1.0;
11043 else if (freq > self->nyquist)
11044 freq = self->nyquist;
11045
11046 if (q < 1.0)
11047 q = 1.0;
11048
11049 bw = freq / q;
11050 c = MYTAN(self->piOnSr * bw);
11051 d = 2.0 * MYCOS(2.0 * self->piOnSr * freq);
11052
11053 self->a0 = self->a2 = 1.0 / (1.0 + c);
11054 self->a1 = self->b1 = -self->a0 * d;
11055 self->b2 = self->a0 * (1.0 - c);
11056 }
11057
11058 static void
11059 ButBR_filters_ii(ButBR *self)
11060 {
11061 MYFLT val, fr, q;
11062 int i;
11063 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11064 fr = PyFloat_AS_DOUBLE(self->freq);
11065 q = PyFloat_AS_DOUBLE(self->q);
11066
11067 if (fr != self->last_freq || q != self->last_q)
11068 {
11069 self->last_freq = fr;
11070 self->last_q = q;
11071 ButBR_compute_coeffs(self, fr, q);
11072 }
11073
11074 for (i = 0; i < self->bufsize; i++)
11075 {
11076 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
11077 self->x2 = self->x1;
11078 self->x1 = in[i];
11079 self->y2 = self->y1;
11080 self->data[i] = self->y1 = val;
11081 }
11082 }
11083
11084 static void
11085 ButBR_filters_ai(ButBR *self)
11086 {
11087 MYFLT val, fr, q;
11088 int i;
11089 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11090 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
11091 q = PyFloat_AS_DOUBLE(self->q);
11092
11093 for (i = 0; i < self->bufsize; i++)
11094 {
11095 fr = freq[i];
11096
11097 if (fr != self->last_freq || q != self->last_q)
11098 {
11099 self->last_freq = fr;
11100 self->last_q = q;
11101 ButBR_compute_coeffs(self, fr, q);
11102 }
11103
11104 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
11105 self->x2 = self->x1;
11106 self->x1 = in[i];
11107 self->y2 = self->y1;
11108 self->data[i] = self->y1 = val;
11109 }
11110 }
11111
11112 static void
11113 ButBR_filters_ia(ButBR *self)
11114 {
11115 MYFLT val, fr, q;
11116 int i;
11117 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11118 fr = PyFloat_AS_DOUBLE(self->freq);
11119 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
11120
11121 for (i = 0; i < self->bufsize; i++)
11122 {
11123 q = qst[i];
11124
11125 if (fr != self->last_freq || q != self->last_q)
11126 {
11127 self->last_freq = fr;
11128 self->last_q = q;
11129 ButBR_compute_coeffs(self, fr, q);
11130 }
11131
11132 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
11133 self->x2 = self->x1;
11134 self->x1 = in[i];
11135 self->y2 = self->y1;
11136 self->data[i] = self->y1 = val;
11137 }
11138 }
11139
11140 static void
11141 ButBR_filters_aa(ButBR *self)
11142 {
11143 MYFLT val, fr, q;
11144 int i;
11145 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11146 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
11147 MYFLT *qst = Stream_getData((Stream *)self->q_stream);
11148
11149 for (i = 0; i < self->bufsize; i++)
11150 {
11151 fr = freq[i];
11152 q = qst[i];
11153
11154 if (fr != self->last_freq || q != self->last_q)
11155 {
11156 self->last_freq = fr;
11157 self->last_q = q;
11158 ButBR_compute_coeffs(self, fr, q);
11159 }
11160
11161 val = self->a0 * in[i] + self->a1 * self->x1 + self->a2 * self->x2 - self->b1 * self->y1 - self->b2 * self->y2;
11162 self->x2 = self->x1;
11163 self->x1 = in[i];
11164 self->y2 = self->y1;
11165 self->data[i] = self->y1 = val;
11166 }
11167 }
11168
11169 static void ButBR_postprocessing_ii(ButBR *self) { POST_PROCESSING_II };
11170 static void ButBR_postprocessing_ai(ButBR *self) { POST_PROCESSING_AI };
11171 static void ButBR_postprocessing_ia(ButBR *self) { POST_PROCESSING_IA };
11172 static void ButBR_postprocessing_aa(ButBR *self) { POST_PROCESSING_AA };
11173 static void ButBR_postprocessing_ireva(ButBR *self) { POST_PROCESSING_IREVA };
11174 static void ButBR_postprocessing_areva(ButBR *self) { POST_PROCESSING_AREVA };
11175 static void ButBR_postprocessing_revai(ButBR *self) { POST_PROCESSING_REVAI };
11176 static void ButBR_postprocessing_revaa(ButBR *self) { POST_PROCESSING_REVAA };
11177 static void ButBR_postprocessing_revareva(ButBR *self) { POST_PROCESSING_REVAREVA };
11178
11179 static void
11180 ButBR_setProcMode(ButBR *self)
11181 {
11182 int procmode, muladdmode;
11183 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
11184 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
11185
11186 switch (procmode)
11187 {
11188 case 0:
11189 self->proc_func_ptr = ButBR_filters_ii;
11190 break;
11191
11192 case 1:
11193 self->proc_func_ptr = ButBR_filters_ai;
11194 break;
11195
11196 case 10:
11197 self->proc_func_ptr = ButBR_filters_ia;
11198 break;
11199
11200 case 11:
11201 self->proc_func_ptr = ButBR_filters_aa;
11202 break;
11203 }
11204
11205 switch (muladdmode)
11206 {
11207 case 0:
11208 self->muladd_func_ptr = ButBR_postprocessing_ii;
11209 break;
11210
11211 case 1:
11212 self->muladd_func_ptr = ButBR_postprocessing_ai;
11213 break;
11214
11215 case 2:
11216 self->muladd_func_ptr = ButBR_postprocessing_revai;
11217 break;
11218
11219 case 10:
11220 self->muladd_func_ptr = ButBR_postprocessing_ia;
11221 break;
11222
11223 case 11:
11224 self->muladd_func_ptr = ButBR_postprocessing_aa;
11225 break;
11226
11227 case 12:
11228 self->muladd_func_ptr = ButBR_postprocessing_revaa;
11229 break;
11230
11231 case 20:
11232 self->muladd_func_ptr = ButBR_postprocessing_ireva;
11233 break;
11234
11235 case 21:
11236 self->muladd_func_ptr = ButBR_postprocessing_areva;
11237 break;
11238
11239 case 22:
11240 self->muladd_func_ptr = ButBR_postprocessing_revareva;
11241 break;
11242 }
11243 }
11244
11245 static void
11246 ButBR_compute_next_data_frame(ButBR *self)
11247 {
11248 (*self->proc_func_ptr)(self);
11249 (*self->muladd_func_ptr)(self);
11250 }
11251
11252 static int
11253 ButBR_traverse(ButBR *self, visitproc visit, void *arg)
11254 {
11255 pyo_VISIT
11256 Py_VISIT(self->input);
11257 Py_VISIT(self->freq);
11258 Py_VISIT(self->q);
11259 return 0;
11260 }
11261
11262 static int
11263 ButBR_clear(ButBR *self)
11264 {
11265 pyo_CLEAR
11266 Py_CLEAR(self->input);
11267 Py_CLEAR(self->freq);
11268 Py_CLEAR(self->q);
11269 return 0;
11270 }
11271
11272 static void
11273 ButBR_dealloc(ButBR* self)
11274 {
11275 pyo_DEALLOC
11276 ButBR_clear(self);
11277 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
11278 Py_TYPE(self)->tp_free((PyObject*)self);
11279 }
11280
11281 static PyObject *
11282 ButBR_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11283 {
11284 int i;
11285 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *qtmp = NULL, *multmp = NULL, *addtmp = NULL;
11286 ButBR *self;
11287 self = (ButBR *)type->tp_alloc(type, 0);
11288
11289 self->freq = PyFloat_FromDouble(1000);
11290 self->q = PyFloat_FromDouble(1);
11291 self->last_freq = self->last_q = -1.0;
11292 self->x1 = self->x2 = self->y1 = self->y2 = 0.0;
11293 self->a0 = self->a1 = self->a2 = self->b1 = self->b2 = 0.0;
11294 self->modebuffer[0] = 0;
11295 self->modebuffer[1] = 0;
11296 self->modebuffer[2] = 0;
11297 self->modebuffer[3] = 0;
11298
11299 INIT_OBJECT_COMMON
11300
11301 self->nyquist = (MYFLT)self->sr * 0.49;
11302 self->piOnSr = PI / (MYFLT)self->sr;
11303
11304 Stream_setFunctionPtr(self->stream, ButBR_compute_next_data_frame);
11305 self->mode_func_ptr = ButBR_setProcMode;
11306
11307 static char *kwlist[] = {"input", "freq", "q", "mul", "add", NULL};
11308
11309 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &qtmp, &multmp, &addtmp))
11310 Py_RETURN_NONE;
11311
11312 INIT_INPUT_STREAM
11313
11314 if (freqtmp)
11315 {
11316 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
11317 }
11318
11319 if (qtmp)
11320 {
11321 PyObject_CallMethod((PyObject *)self, "setQ", "O", qtmp);
11322 }
11323
11324 if (multmp)
11325 {
11326 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
11327 }
11328
11329 if (addtmp)
11330 {
11331 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
11332 }
11333
11334 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
11335
11336 (*self->mode_func_ptr)(self);
11337
11338 return (PyObject *)self;
11339 }
11340
11341 static PyObject * ButBR_getServer(ButBR* self) { GET_SERVER };
11342 static PyObject * ButBR_getStream(ButBR* self) { GET_STREAM };
11343 static PyObject * ButBR_setMul(ButBR *self, PyObject *arg) { SET_MUL };
11344 static PyObject * ButBR_setAdd(ButBR *self, PyObject *arg) { SET_ADD };
11345 static PyObject * ButBR_setSub(ButBR *self, PyObject *arg) { SET_SUB };
11346 static PyObject * ButBR_setDiv(ButBR *self, PyObject *arg) { SET_DIV };
11347
11348 static PyObject * ButBR_play(ButBR *self, PyObject *args, PyObject *kwds) { PLAY };
11349 static PyObject * ButBR_out(ButBR *self, PyObject *args, PyObject *kwds) { OUT };
11350 static PyObject * ButBR_stop(ButBR *self, PyObject *args, PyObject *kwds) { STOP };
11351
11352 static PyObject * ButBR_multiply(ButBR *self, PyObject *arg) { MULTIPLY };
11353 static PyObject * ButBR_inplace_multiply(ButBR *self, PyObject *arg) { INPLACE_MULTIPLY };
11354 static PyObject * ButBR_add(ButBR *self, PyObject *arg) { ADD };
11355 static PyObject * ButBR_inplace_add(ButBR *self, PyObject *arg) { INPLACE_ADD };
11356 static PyObject * ButBR_sub(ButBR *self, PyObject *arg) { SUB };
11357 static PyObject * ButBR_inplace_sub(ButBR *self, PyObject *arg) { INPLACE_SUB };
11358 static PyObject * ButBR_div(ButBR *self, PyObject *arg) { DIV };
11359 static PyObject * ButBR_inplace_div(ButBR *self, PyObject *arg) { INPLACE_DIV };
11360
11361 static PyObject * ButBR_setFreq(ButBR *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
11362 static PyObject * ButBR_setQ(ButBR *self, PyObject *arg) { SET_PARAM(self->q, self->q_stream, 3); }
11363
11364 static PyMemberDef ButBR_members[] =
11365 {
11366 {"server", T_OBJECT_EX, offsetof(ButBR, server), 0, "Pyo server."},
11367 {"stream", T_OBJECT_EX, offsetof(ButBR, stream), 0, "Stream object."},
11368 {"input", T_OBJECT_EX, offsetof(ButBR, input), 0, "Input sound object."},
11369 {"freq", T_OBJECT_EX, offsetof(ButBR, freq), 0, "Cutoff frequency in cycle per second."},
11370 {"q", T_OBJECT_EX, offsetof(ButBR, q), 0, "Q factor."},
11371 {"mul", T_OBJECT_EX, offsetof(ButBR, mul), 0, "Mul factor."},
11372 {"add", T_OBJECT_EX, offsetof(ButBR, add), 0, "Add factor."},
11373 {NULL} /* Sentinel */
11374 };
11375
11376 static PyMethodDef ButBR_methods[] =
11377 {
11378 {"getServer", (PyCFunction)ButBR_getServer, METH_NOARGS, "Returns server object."},
11379 {"_getStream", (PyCFunction)ButBR_getStream, METH_NOARGS, "Returns stream object."},
11380 {"play", (PyCFunction)ButBR_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
11381 {"out", (PyCFunction)ButBR_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
11382 {"stop", (PyCFunction)ButBR_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
11383 {"setFreq", (PyCFunction)ButBR_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
11384 {"setQ", (PyCFunction)ButBR_setQ, METH_O, "Sets filter Q factor."},
11385 {"setMul", (PyCFunction)ButBR_setMul, METH_O, "Sets oscillator mul factor."},
11386 {"setAdd", (PyCFunction)ButBR_setAdd, METH_O, "Sets oscillator add factor."},
11387 {"setSub", (PyCFunction)ButBR_setSub, METH_O, "Sets inverse add factor."},
11388 {"setDiv", (PyCFunction)ButBR_setDiv, METH_O, "Sets inverse mul factor."},
11389 {NULL} /* Sentinel */
11390 };
11391
11392 static PyNumberMethods ButBR_as_number =
11393 {
11394 (binaryfunc)ButBR_add, /*nb_add*/
11395 (binaryfunc)ButBR_sub, /*nb_subtract*/
11396 (binaryfunc)ButBR_multiply, /*nb_multiply*/
11397 0, /*nb_remainder*/
11398 0, /*nb_divmod*/
11399 0, /*nb_power*/
11400 0, /*nb_neg*/
11401 0, /*nb_pos*/
11402 0, /*(unaryfunc)array_abs,*/
11403 0, /*nb_nonzero*/
11404 0, /*nb_invert*/
11405 0, /*nb_lshift*/
11406 0, /*nb_rshift*/
11407 0, /*nb_and*/
11408 0, /*nb_xor*/
11409 0, /*nb_or*/
11410 0, /*nb_int*/
11411 0, /*nb_long*/
11412 0, /*nb_float*/
11413 (binaryfunc)ButBR_inplace_add, /*inplace_add*/
11414 (binaryfunc)ButBR_inplace_sub, /*inplace_subtract*/
11415 (binaryfunc)ButBR_inplace_multiply, /*inplace_multiply*/
11416 0, /*inplace_remainder*/
11417 0, /*inplace_power*/
11418 0, /*inplace_lshift*/
11419 0, /*inplace_rshift*/
11420 0, /*inplace_and*/
11421 0, /*inplace_xor*/
11422 0, /*inplace_or*/
11423 0, /*nb_floor_divide*/
11424 (binaryfunc)ButBR_div, /*nb_true_divide*/
11425 0, /*nb_inplace_floor_divide*/
11426 (binaryfunc)ButBR_inplace_div, /*nb_inplace_true_divide*/
11427 0, /* nb_index */
11428 };
11429
11430 PyTypeObject ButBRType =
11431 {
11432 PyVarObject_HEAD_INIT(NULL, 0)
11433 "_pyo.ButBR_base", /*tp_name*/
11434 sizeof(ButBR), /*tp_basicsize*/
11435 0, /*tp_itemsize*/
11436 (destructor)ButBR_dealloc, /*tp_dealloc*/
11437 0, /*tp_print*/
11438 0, /*tp_getattr*/
11439 0, /*tp_setattr*/
11440 0, /*tp_as_async (tp_compare in Python 2)*/
11441 0, /*tp_repr*/
11442 &ButBR_as_number, /*tp_as_number*/
11443 0, /*tp_as_sequence*/
11444 0, /*tp_as_mapping*/
11445 0, /*tp_hash */
11446 0, /*tp_call*/
11447 0, /*tp_str*/
11448 0, /*tp_getattro*/
11449 0, /*tp_setattro*/
11450 0, /*tp_as_buffer*/
11451 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
11452 "ButBR objects. Second-order Butterworth band-reject filter.", /* tp_doc */
11453 (traverseproc)ButBR_traverse, /* tp_traverse */
11454 (inquiry)ButBR_clear, /* tp_clear */
11455 0, /* tp_richcompare */
11456 0, /* tp_weaklistoffset */
11457 0, /* tp_iter */
11458 0, /* tp_iternext */
11459 ButBR_methods, /* tp_methods */
11460 ButBR_members, /* tp_members */
11461 0, /* tp_getset */
11462 0, /* tp_base */
11463 0, /* tp_dict */
11464 0, /* tp_descr_get */
11465 0, /* tp_descr_set */
11466 0, /* tp_dictoffset */
11467 0, /* tp_init */
11468 0, /* tp_alloc */
11469 ButBR_new, /* tp_new */
11470 };
11471
11472 /****************/
11473 /** ComplexRes **/
11474 /****************/
11475 typedef struct
11476 {
11477 pyo_audio_HEAD
11478 PyObject *input;
11479 Stream *input_stream;
11480 PyObject *freq;
11481 Stream *freq_stream;
11482 PyObject *decay;
11483 Stream *decay_stream;
11484 int modebuffer[4]; // need at least 2 slots for mul & add
11485 MYFLT last_freq;
11486 MYFLT last_decay;
11487 MYFLT oneOnSr;
11488 // variables
11489 MYFLT res;
11490 MYFLT norm;
11491 MYFLT coeffx;
11492 MYFLT coeffy;
11493 // sample memories
11494 MYFLT x;
11495 MYFLT y;
11496 } ComplexRes;
11497
11498 static void
11499 ComplexRes_filters_ii(ComplexRes *self)
11500 {
11501 int i;
11502 MYFLT ang, x, y;
11503 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11504 MYFLT freq = PyFloat_AS_DOUBLE(self->freq);
11505 MYFLT decay = PyFloat_AS_DOUBLE(self->decay);
11506
11507 if (decay <= 0.0001)
11508 decay = 0.0001;
11509
11510 if (decay != self->last_decay || freq != self->last_freq)
11511 {
11512 self->res = MYEXP(-1.0 / (decay * self->sr));
11513 //self->norm = (1.0-self->res*self->res)/self->res;
11514 self->last_decay = decay;
11515 ang = (freq * self->oneOnSr) * TWOPI;
11516 self->coeffx = self->res * MYCOS(ang);
11517 self->coeffy = self->res * MYSIN(ang);
11518 self->last_freq = freq;
11519 }
11520
11521 for (i = 0; i < self->bufsize; i++)
11522 {
11523 x = self->coeffx * self->x - self->coeffy * self->y + in[i];
11524 y = self->coeffy * self->x + self->coeffx * self->y;
11525 self->data[i] = y * self->norm;
11526 self->x = x;
11527 self->y = y;
11528 }
11529 }
11530
11531 static void
11532 ComplexRes_filters_ai(ComplexRes *self)
11533 {
11534 int i, check = 0;
11535 MYFLT freq, ang, x, y;
11536 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11537 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
11538 MYFLT decay = PyFloat_AS_DOUBLE(self->decay);
11539
11540 if (decay <= 0.0001)
11541 decay = 0.0001;
11542
11543 if (decay != self->last_decay)
11544 {
11545 self->res = MYEXP(-1.0 / (decay * self->sr));
11546 //self->norm = (1.0-self->res*self->res)/self->res;
11547 self->last_decay = decay;
11548 check = 1;
11549 }
11550
11551 for (i = 0; i < self->bufsize; i++)
11552 {
11553 freq = fr[i];
11554
11555 if (freq != self->last_freq || check)
11556 {
11557 ang = (freq * self->oneOnSr) * TWOPI;
11558 self->coeffx = self->res * MYCOS(ang);
11559 self->coeffy = self->res * MYSIN(ang);
11560 self->last_freq = freq;
11561 check = 0;
11562 }
11563
11564 x = self->coeffx * self->x - self->coeffy * self->y + in[i];
11565 y = self->coeffy * self->x + self->coeffx * self->y;
11566 self->data[i] = y * self->norm;
11567 self->x = x;
11568 self->y = y;
11569 }
11570 }
11571
11572 static void
11573 ComplexRes_filters_ia(ComplexRes *self)
11574 {
11575 int i;
11576 MYFLT decay, ang, x, y;
11577 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11578 MYFLT freq = PyFloat_AS_DOUBLE(self->freq);
11579 MYFLT *dec = Stream_getData((Stream *)self->decay_stream);
11580
11581 for (i = 0; i < self->bufsize; i++)
11582 {
11583 decay = dec[i];
11584
11585 if (decay <= 0.0001)
11586 decay = 0.0001;
11587
11588 if (freq != self->last_freq || decay != self->last_decay)
11589 {
11590 self->res = MYEXP(-1.0 / (decay * self->sr));
11591 //self->norm = (1.0-self->res*self->res)/self->res;
11592 self->last_decay = decay;
11593 ang = (freq * self->oneOnSr) * TWOPI;
11594 self->coeffx = self->res * MYCOS(ang);
11595 self->coeffy = self->res * MYSIN(ang);
11596 self->last_freq = freq;
11597 }
11598
11599 x = self->coeffx * self->x - self->coeffy * self->y + in[i];
11600 y = self->coeffy * self->x + self->coeffx * self->y;
11601 self->data[i] = y * self->norm;
11602 self->x = x;
11603 self->y = y;
11604 }
11605 }
11606
11607 static void
11608 ComplexRes_filters_aa(ComplexRes *self)
11609 {
11610 int i;
11611 MYFLT freq, decay, ang, x, y;
11612 MYFLT *in = Stream_getData((Stream *)self->input_stream);
11613 MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
11614 MYFLT *dec = Stream_getData((Stream *)self->decay_stream);
11615
11616 for (i = 0; i < self->bufsize; i++)
11617 {
11618 freq = fr[i];
11619 decay = dec[i];
11620
11621 if (decay <= 0.0001)
11622 decay = 0.0001;
11623
11624 if (freq != self->last_freq || decay != self->last_decay)
11625 {
11626 self->res = MYEXP(-1.0 / (decay * self->sr));
11627 //self->norm = (1.0-self->res*self->res)/self->res;
11628 self->last_decay = decay;
11629 ang = (freq * self->oneOnSr) * TWOPI;
11630 self->coeffx = self->res * MYCOS(ang);
11631 self->coeffy = self->res * MYSIN(ang);
11632 self->last_freq = freq;
11633 }
11634
11635 x = self->coeffx * self->x - self->coeffy * self->y + in[i];
11636 y = self->coeffy * self->x + self->coeffx * self->y;
11637 self->data[i] = y * self->norm;
11638 self->x = x;
11639 self->y = y;
11640 }
11641 }
11642
11643 static void ComplexRes_postprocessing_ii(ComplexRes *self) { POST_PROCESSING_II };
11644 static void ComplexRes_postprocessing_ai(ComplexRes *self) { POST_PROCESSING_AI };
11645 static void ComplexRes_postprocessing_ia(ComplexRes *self) { POST_PROCESSING_IA };
11646 static void ComplexRes_postprocessing_aa(ComplexRes *self) { POST_PROCESSING_AA };
11647 static void ComplexRes_postprocessing_ireva(ComplexRes *self) { POST_PROCESSING_IREVA };
11648 static void ComplexRes_postprocessing_areva(ComplexRes *self) { POST_PROCESSING_AREVA };
11649 static void ComplexRes_postprocessing_revai(ComplexRes *self) { POST_PROCESSING_REVAI };
11650 static void ComplexRes_postprocessing_revaa(ComplexRes *self) { POST_PROCESSING_REVAA };
11651 static void ComplexRes_postprocessing_revareva(ComplexRes *self) { POST_PROCESSING_REVAREVA };
11652
11653 static void
11654 ComplexRes_setProcMode(ComplexRes *self)
11655 {
11656 int procmode, muladdmode;
11657 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
11658 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
11659
11660 switch (procmode)
11661 {
11662 case 0:
11663 self->proc_func_ptr = ComplexRes_filters_ii;
11664 break;
11665
11666 case 1:
11667 self->proc_func_ptr = ComplexRes_filters_ai;
11668 break;
11669
11670 case 10:
11671 self->proc_func_ptr = ComplexRes_filters_ia;
11672 break;
11673
11674 case 11:
11675 self->proc_func_ptr = ComplexRes_filters_aa;
11676 break;
11677 }
11678
11679 switch (muladdmode)
11680 {
11681 case 0:
11682 self->muladd_func_ptr = ComplexRes_postprocessing_ii;
11683 break;
11684
11685 case 1:
11686 self->muladd_func_ptr = ComplexRes_postprocessing_ai;
11687 break;
11688
11689 case 2:
11690 self->muladd_func_ptr = ComplexRes_postprocessing_revai;
11691 break;
11692
11693 case 10:
11694 self->muladd_func_ptr = ComplexRes_postprocessing_ia;
11695 break;
11696
11697 case 11:
11698 self->muladd_func_ptr = ComplexRes_postprocessing_aa;
11699 break;
11700
11701 case 12:
11702 self->muladd_func_ptr = ComplexRes_postprocessing_revaa;
11703 break;
11704
11705 case 20:
11706 self->muladd_func_ptr = ComplexRes_postprocessing_ireva;
11707 break;
11708
11709 case 21:
11710 self->muladd_func_ptr = ComplexRes_postprocessing_areva;
11711 break;
11712
11713 case 22:
11714 self->muladd_func_ptr = ComplexRes_postprocessing_revareva;
11715 break;
11716 }
11717 }
11718
11719 static void
11720 ComplexRes_compute_next_data_frame(ComplexRes *self)
11721 {
11722 (*self->proc_func_ptr)(self);
11723 (*self->muladd_func_ptr)(self);
11724 }
11725
11726 static int
11727 ComplexRes_traverse(ComplexRes *self, visitproc visit, void *arg)
11728 {
11729 pyo_VISIT
11730 Py_VISIT(self->input);
11731 Py_VISIT(self->freq);
11732 Py_VISIT(self->decay);
11733 return 0;
11734 }
11735
11736 static int
11737 ComplexRes_clear(ComplexRes *self)
11738 {
11739 pyo_CLEAR
11740 Py_CLEAR(self->input);
11741 Py_CLEAR(self->freq);
11742 Py_CLEAR(self->decay);
11743 return 0;
11744 }
11745
11746 static void
11747 ComplexRes_dealloc(ComplexRes* self)
11748 {
11749 pyo_DEALLOC
11750 ComplexRes_clear(self);
11751 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
11752 Py_TYPE(self)->tp_free((PyObject*)self);
11753 }
11754
11755 static PyObject *
11756 ComplexRes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11757 {
11758 int i;
11759 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *decaytmp = NULL, *multmp = NULL, *addtmp = NULL;
11760 ComplexRes *self;
11761 self = (ComplexRes *)type->tp_alloc(type, 0);
11762
11763 self->freq = PyFloat_FromDouble(1000);
11764 self->decay = PyFloat_FromDouble(.25);
11765 self->last_freq = self->last_decay = -1.0;
11766 self->x = self->y = 0.0;
11767 self->res = 1.0;
11768 self->norm = 0.01; /* normalization factor fixed at -40 dB */
11769 self->coeffx = self->coeffy = 0.0;
11770 self->modebuffer[0] = 0;
11771 self->modebuffer[1] = 0;
11772 self->modebuffer[2] = 0;
11773 self->modebuffer[3] = 0;
11774
11775 INIT_OBJECT_COMMON
11776
11777 self->oneOnSr = 1.0 / self->sr;
11778
11779 Stream_setFunctionPtr(self->stream, ComplexRes_compute_next_data_frame);
11780 self->mode_func_ptr = ComplexRes_setProcMode;
11781
11782 static char *kwlist[] = {"input", "freq", "decay", "mul", "add", NULL};
11783
11784 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &decaytmp, &multmp, &addtmp))
11785 Py_RETURN_NONE;
11786
11787 INIT_INPUT_STREAM
11788
11789 if (freqtmp)
11790 {
11791 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
11792 }
11793
11794 if (decaytmp)
11795 {
11796 PyObject_CallMethod((PyObject *)self, "setDecay", "O", decaytmp);
11797 }
11798
11799 if (multmp)
11800 {
11801 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
11802 }
11803
11804 if (addtmp)
11805 {
11806 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
11807 }
11808
11809 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
11810
11811 (*self->mode_func_ptr)(self);
11812
11813 return (PyObject *)self;
11814 }
11815
11816 static PyObject * ComplexRes_getServer(ComplexRes* self) { GET_SERVER };
11817 static PyObject * ComplexRes_getStream(ComplexRes* self) { GET_STREAM };
11818 static PyObject * ComplexRes_setMul(ComplexRes *self, PyObject *arg) { SET_MUL };
11819 static PyObject * ComplexRes_setAdd(ComplexRes *self, PyObject *arg) { SET_ADD };
11820 static PyObject * ComplexRes_setSub(ComplexRes *self, PyObject *arg) { SET_SUB };
11821 static PyObject * ComplexRes_setDiv(ComplexRes *self, PyObject *arg) { SET_DIV };
11822
11823 static PyObject * ComplexRes_play(ComplexRes *self, PyObject *args, PyObject *kwds) { PLAY };
11824 static PyObject * ComplexRes_out(ComplexRes *self, PyObject *args, PyObject *kwds) { OUT };
11825 static PyObject * ComplexRes_stop(ComplexRes *self, PyObject *args, PyObject *kwds) { STOP };
11826
11827 static PyObject * ComplexRes_multiply(ComplexRes *self, PyObject *arg) { MULTIPLY };
11828 static PyObject * ComplexRes_inplace_multiply(ComplexRes *self, PyObject *arg) { INPLACE_MULTIPLY };
11829 static PyObject * ComplexRes_add(ComplexRes *self, PyObject *arg) { ADD };
11830 static PyObject * ComplexRes_inplace_add(ComplexRes *self, PyObject *arg) { INPLACE_ADD };
11831 static PyObject * ComplexRes_sub(ComplexRes *self, PyObject *arg) { SUB };
11832 static PyObject * ComplexRes_inplace_sub(ComplexRes *self, PyObject *arg) { INPLACE_SUB };
11833 static PyObject * ComplexRes_div(ComplexRes *self, PyObject *arg) { DIV };
11834 static PyObject * ComplexRes_inplace_div(ComplexRes *self, PyObject *arg) { INPLACE_DIV };
11835
11836 static PyObject * ComplexRes_setFreq(ComplexRes *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
11837 static PyObject * ComplexRes_setDecay(ComplexRes *self, PyObject *arg) { SET_PARAM(self->decay, self->decay_stream, 3); }
11838
11839 static PyMemberDef ComplexRes_members[] =
11840 {
11841 {"server", T_OBJECT_EX, offsetof(ComplexRes, server), 0, "Pyo server."},
11842 {"stream", T_OBJECT_EX, offsetof(ComplexRes, stream), 0, "Stream object."},
11843 {"input", T_OBJECT_EX, offsetof(ComplexRes, input), 0, "Input sound object."},
11844 {"freq", T_OBJECT_EX, offsetof(ComplexRes, freq), 0, "Center frequency in cycle per second."},
11845 {"decay", T_OBJECT_EX, offsetof(ComplexRes, decay), 0, "Decaying envelope time in seconds."},
11846 {"mul", T_OBJECT_EX, offsetof(ComplexRes, mul), 0, "Mul factor."},
11847 {"add", T_OBJECT_EX, offsetof(ComplexRes, add), 0, "Add factor."},
11848 {NULL} /* Sentinel */
11849 };
11850
11851 static PyMethodDef ComplexRes_methods[] =
11852 {
11853 {"getServer", (PyCFunction)ComplexRes_getServer, METH_NOARGS, "Returns server object."},
11854 {"_getStream", (PyCFunction)ComplexRes_getStream, METH_NOARGS, "Returns stream object."},
11855 {"play", (PyCFunction)ComplexRes_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
11856 {"out", (PyCFunction)ComplexRes_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
11857 {"stop", (PyCFunction)ComplexRes_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
11858 {"setFreq", (PyCFunction)ComplexRes_setFreq, METH_O, "Sets filter center frequency in cycle per second."},
11859 {"setDecay", (PyCFunction)ComplexRes_setDecay, METH_O, "Sets filter decaying envelope time."},
11860 {"setMul", (PyCFunction)ComplexRes_setMul, METH_O, "Sets oscillator mul factor."},
11861 {"setAdd", (PyCFunction)ComplexRes_setAdd, METH_O, "Sets oscillator add factor."},
11862 {"setSub", (PyCFunction)ComplexRes_setSub, METH_O, "Sets inverse add factor."},
11863 {"setDiv", (PyCFunction)ComplexRes_setDiv, METH_O, "Sets inverse mul factor."},
11864 {NULL} /* Sentinel */
11865 };
11866
11867 static PyNumberMethods ComplexRes_as_number =
11868 {
11869 (binaryfunc)ComplexRes_add, /*nb_add*/
11870 (binaryfunc)ComplexRes_sub, /*nb_subtract*/
11871 (binaryfunc)ComplexRes_multiply, /*nb_multiply*/
11872 0, /*nb_remainder*/
11873 0, /*nb_divmod*/
11874 0, /*nb_power*/
11875 0, /*nb_neg*/
11876 0, /*nb_pos*/
11877 0, /*(unaryfunc)array_abs,*/
11878 0, /*nb_nonzero*/
11879 0, /*nb_invert*/
11880 0, /*nb_lshift*/
11881 0, /*nb_rshift*/
11882 0, /*nb_and*/
11883 0, /*nb_xor*/
11884 0, /*nb_or*/
11885 0, /*nb_int*/
11886 0, /*nb_long*/
11887 0, /*nb_float*/
11888 (binaryfunc)ComplexRes_inplace_add, /*inplace_add*/
11889 (binaryfunc)ComplexRes_inplace_sub, /*inplace_subtract*/
11890 (binaryfunc)ComplexRes_inplace_multiply, /*inplace_multiply*/
11891 0, /*inplace_remainder*/
11892 0, /*inplace_power*/
11893 0, /*inplace_lshift*/
11894 0, /*inplace_rshift*/
11895 0, /*inplace_and*/
11896 0, /*inplace_xor*/
11897 0, /*inplace_or*/
11898 0, /*nb_floor_divide*/
11899 (binaryfunc)ComplexRes_div, /*nb_true_divide*/
11900 0, /*nb_inplace_floor_divide*/
11901 (binaryfunc)ComplexRes_inplace_div, /*nb_inplace_true_divide*/
11902 0, /* nb_index */
11903 };
11904
11905 PyTypeObject ComplexResType =
11906 {
11907 PyVarObject_HEAD_INIT(NULL, 0)
11908 "_pyo.ComplexRes_base", /*tp_name*/
11909 sizeof(ComplexRes), /*tp_basicsize*/
11910 0, /*tp_itemsize*/
11911 (destructor)ComplexRes_dealloc, /*tp_dealloc*/
11912 0, /*tp_print*/
11913 0, /*tp_getattr*/
11914 0, /*tp_setattr*/
11915 0, /*tp_as_async (tp_compare in Python 2)*/
11916 0, /*tp_repr*/
11917 &ComplexRes_as_number, /*tp_as_number*/
11918 0, /*tp_as_sequence*/
11919 0, /*tp_as_mapping*/
11920 0, /*tp_hash */
11921 0, /*tp_call*/
11922 0, /*tp_str*/
11923 0, /*tp_getattro*/
11924 0, /*tp_setattro*/
11925 0, /*tp_as_buffer*/
11926 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
11927 "ComplexRes objects. Second order allpass filter.", /* tp_doc */
11928 (traverseproc)ComplexRes_traverse, /* tp_traverse */
11929 (inquiry)ComplexRes_clear, /* tp_clear */
11930 0, /* tp_richcompare */
11931 0, /* tp_weaklistoffset */
11932 0, /* tp_iter */
11933 0, /* tp_iternext */
11934 ComplexRes_methods, /* tp_methods */
11935 ComplexRes_members, /* tp_members */
11936 0, /* tp_getset */
11937 0, /* tp_base */
11938 0, /* tp_dict */
11939 0, /* tp_descr_get */
11940 0, /* tp_descr_set */
11941 0, /* tp_dictoffset */
11942 0, /* tp_init */
11943 0, /* tp_alloc */
11944 ComplexRes_new, /* tp_new */
11945 };
11946
11947 typedef struct
11948 {
11949 pyo_audio_HEAD
11950 PyObject *input;
11951 Stream *input_stream;
11952 PyObject *freq;
11953 Stream *freq_stream;
11954 PyObject *res;
11955 Stream *res_stream;
11956 int modebuffer[4]; // need at least 2 slots for mul & add
11957 MYFLT nyquist;
11958 MYFLT last_freq;
11959 MYFLT last_res;
11960 // sample memories
11961 MYFLT y1;
11962 MYFLT y2;
11963 MYFLT y3;
11964 MYFLT y4;
11965 MYFLT oldX;
11966 MYFLT oldY1;
11967 MYFLT oldY2;
11968 MYFLT oldY3;
11969 MYFLT oneOverSr;
11970 // coefficients
11971 MYFLT r;
11972 MYFLT p;
11973 MYFLT k;
11974 } MoogLP;
11975
11976 static void
11977 MoogLP_compute_coeffs(MoogLP *self, MYFLT freq, MYFLT res)
11978 {
11979 MYFLT f, fi, t, t2;
11980
11981 if (freq < 0.1)
11982 freq = 0.1;
11983 else if (freq > self->nyquist)
11984 freq = self->nyquist;
11985
11986 if (res < 0.0)
11987 res = 0.0;
11988 else if (res > 10.0)
11989 res = 10.0;
11990
11991 f = (freq + freq) * self->oneOverSr;
11992 fi = 1.0 - f;
11993 self->p = f * (1.8 - 0.8 * f);
11994 self->k = 2.0 * MYSIN(f * PI * 0.5) - 1.0;
11995 t = (1.0 - self->p) * 1.386249;
11996 t2 = 12.0 + t * t;
11997 self->r = res * 0.5 * (t2 + 6.0 * t) / (t2 - 6.0 * t);
11998 /* Resonance compensation according to normalized frequency. */
11999 self->r *= fi * fi * fi * 0.9 + 0.1;
12000 }
12001
12002 static void
12003 MoogLP_filters_ii(MoogLP *self)
12004 {
12005 MYFLT x, fr, res;
12006 int i;
12007 MYFLT *in = Stream_getData((Stream *)self->input_stream);
12008 fr = PyFloat_AS_DOUBLE(self->freq);
12009 res = PyFloat_AS_DOUBLE(self->res);
12010
12011 if (fr != self->last_freq || res != self->last_res)
12012 {
12013 self->last_freq = fr;
12014 self->last_res = res;
12015 MoogLP_compute_coeffs(self, fr, res);
12016 }
12017
12018 for (i = 0; i < self->bufsize; i++)
12019 {
12020 x = in[i] - self->r * self->y4;
12021 self->y1 = (x + self->oldX) * self->p - self->k * self->y1;
12022 self->y2 = (self->y1 + self->oldY1) * self->p - self->k * self->y2;
12023 self->y3 = (self->y2 + self->oldY2) * self->p - self->k * self->y3;
12024 self->y4 = (self->y3 + self->oldY3) * self->p - self->k * self->y4;
12025 self->y4 -= (self->y4 * self->y4 * self->y4) * 0.16666666666666666;
12026 self->oldX = x;
12027 self->oldY1 = self->y1;
12028 self->oldY2 = self->y2;
12029 self->oldY3 = self->y3;
12030 self->data[i] = self->y4;
12031 }
12032 }
12033
12034 static void
12035 MoogLP_filters_ai(MoogLP *self)
12036 {
12037 MYFLT x, fr, res;
12038 int i;
12039 MYFLT *in = Stream_getData((Stream *)self->input_stream);
12040 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
12041 res = PyFloat_AS_DOUBLE(self->res);
12042
12043 for (i = 0; i < self->bufsize; i++)
12044 {
12045 fr = freq[i];
12046
12047 if (fr != self->last_freq || res != self->last_res)
12048 {
12049 self->last_freq = fr;
12050 self->last_res = res;
12051 MoogLP_compute_coeffs(self, fr, res);
12052 }
12053
12054 x = in[i] - self->r * self->y4;
12055 self->y1 = (x + self->oldX) * self->p - self->k * self->y1;
12056 self->y2 = (self->y1 + self->oldY1) * self->p - self->k * self->y2;
12057 self->y3 = (self->y2 + self->oldY2) * self->p - self->k * self->y3;
12058 self->y4 = (self->y3 + self->oldY3) * self->p - self->k * self->y4;
12059 self->y4 -= (self->y4 * self->y4 * self->y4) * 0.16666666666666666;
12060 self->oldX = x;
12061 self->oldY1 = self->y1;
12062 self->oldY2 = self->y2;
12063 self->oldY3 = self->y3;
12064 self->data[i] = self->y4;
12065 }
12066 }
12067
12068 static void
12069 MoogLP_filters_ia(MoogLP *self)
12070 {
12071 MYFLT x, fr, res;
12072 int i;
12073 MYFLT *in = Stream_getData((Stream *)self->input_stream);
12074 fr = PyFloat_AS_DOUBLE(self->freq);
12075 MYFLT *rz = Stream_getData((Stream *)self->res_stream);
12076
12077 for (i = 0; i < self->bufsize; i++)
12078 {
12079 res = rz[i];
12080
12081 if (fr != self->last_freq || res != self->last_res)
12082 {
12083 self->last_freq = fr;
12084 self->last_res = res;
12085 MoogLP_compute_coeffs(self, fr, res);
12086 }
12087
12088 x = in[i] - self->r * self->y4;
12089 self->y1 = (x + self->oldX) * self->p - self->k * self->y1;
12090 self->y2 = (self->y1 + self->oldY1) * self->p - self->k * self->y2;
12091 self->y3 = (self->y2 + self->oldY2) * self->p - self->k * self->y3;
12092 self->y4 = (self->y3 + self->oldY3) * self->p - self->k * self->y4;
12093 self->y4 -= (self->y4 * self->y4 * self->y4) * 0.16666666666666666;
12094 self->oldX = x;
12095 self->oldY1 = self->y1;
12096 self->oldY2 = self->y2;
12097 self->oldY3 = self->y3;
12098 self->data[i] = self->y4;
12099 }
12100 }
12101
12102 static void
12103 MoogLP_filters_aa(MoogLP *self)
12104 {
12105 MYFLT x, fr, res;
12106 int i;
12107 MYFLT *in = Stream_getData((Stream *)self->input_stream);
12108 MYFLT *freq = Stream_getData((Stream *)self->freq_stream);
12109 MYFLT *rz = Stream_getData((Stream *)self->res_stream);
12110
12111 for (i = 0; i < self->bufsize; i++)
12112 {
12113 fr = freq[i];
12114 res = rz[i];
12115
12116 if (fr != self->last_freq || res != self->last_res)
12117 {
12118 self->last_freq = fr;
12119 self->last_res = res;
12120 MoogLP_compute_coeffs(self, fr, res);
12121 }
12122
12123 x = in[i] - self->r * self->y4;
12124 self->y1 = (x + self->oldX) * self->p - self->k * self->y1;
12125 self->y2 = (self->y1 + self->oldY1) * self->p - self->k * self->y2;
12126 self->y3 = (self->y2 + self->oldY2) * self->p - self->k * self->y3;
12127 self->y4 = (self->y3 + self->oldY3) * self->p - self->k * self->y4;
12128 self->y4 -= (self->y4 * self->y4 * self->y4) * 0.16666666666666666;
12129 self->oldX = x;
12130 self->oldY1 = self->y1;
12131 self->oldY2 = self->y2;
12132 self->oldY3 = self->y3;
12133 self->data[i] = self->y4;
12134 }
12135 }
12136
12137 static void MoogLP_postprocessing_ii(MoogLP *self) { POST_PROCESSING_II };
12138 static void MoogLP_postprocessing_ai(MoogLP *self) { POST_PROCESSING_AI };
12139 static void MoogLP_postprocessing_ia(MoogLP *self) { POST_PROCESSING_IA };
12140 static void MoogLP_postprocessing_aa(MoogLP *self) { POST_PROCESSING_AA };
12141 static void MoogLP_postprocessing_ireva(MoogLP *self) { POST_PROCESSING_IREVA };
12142 static void MoogLP_postprocessing_areva(MoogLP *self) { POST_PROCESSING_AREVA };
12143 static void MoogLP_postprocessing_revai(MoogLP *self) { POST_PROCESSING_REVAI };
12144 static void MoogLP_postprocessing_revaa(MoogLP *self) { POST_PROCESSING_REVAA };
12145 static void MoogLP_postprocessing_revareva(MoogLP *self) { POST_PROCESSING_REVAREVA };
12146
12147 static void
12148 MoogLP_setProcMode(MoogLP *self)
12149 {
12150 int procmode, muladdmode;
12151 procmode = self->modebuffer[2] + self->modebuffer[3] * 10;
12152 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
12153
12154 switch (procmode)
12155 {
12156 case 0:
12157 self->proc_func_ptr = MoogLP_filters_ii;
12158 break;
12159
12160 case 1:
12161 self->proc_func_ptr = MoogLP_filters_ai;
12162 break;
12163
12164 case 10:
12165 self->proc_func_ptr = MoogLP_filters_ia;
12166 break;
12167
12168 case 11:
12169 self->proc_func_ptr = MoogLP_filters_aa;
12170 break;
12171 }
12172
12173 switch (muladdmode)
12174 {
12175 case 0:
12176 self->muladd_func_ptr = MoogLP_postprocessing_ii;
12177 break;
12178
12179 case 1:
12180 self->muladd_func_ptr = MoogLP_postprocessing_ai;
12181 break;
12182
12183 case 2:
12184 self->muladd_func_ptr = MoogLP_postprocessing_revai;
12185 break;
12186
12187 case 10:
12188 self->muladd_func_ptr = MoogLP_postprocessing_ia;
12189 break;
12190
12191 case 11:
12192 self->muladd_func_ptr = MoogLP_postprocessing_aa;
12193 break;
12194
12195 case 12:
12196 self->muladd_func_ptr = MoogLP_postprocessing_revaa;
12197 break;
12198
12199 case 20:
12200 self->muladd_func_ptr = MoogLP_postprocessing_ireva;
12201 break;
12202
12203 case 21:
12204 self->muladd_func_ptr = MoogLP_postprocessing_areva;
12205 break;
12206
12207 case 22:
12208 self->muladd_func_ptr = MoogLP_postprocessing_revareva;
12209 break;
12210 }
12211 }
12212
12213 static void
12214 MoogLP_compute_next_data_frame(MoogLP *self)
12215 {
12216 (*self->proc_func_ptr)(self);
12217 (*self->muladd_func_ptr)(self);
12218 }
12219
12220 static int
12221 MoogLP_traverse(MoogLP *self, visitproc visit, void *arg)
12222 {
12223 pyo_VISIT
12224 Py_VISIT(self->input);
12225 Py_VISIT(self->freq);
12226 Py_VISIT(self->res);
12227 return 0;
12228 }
12229
12230 static int
12231 MoogLP_clear(MoogLP *self)
12232 {
12233 pyo_CLEAR
12234 Py_CLEAR(self->input);
12235 Py_CLEAR(self->freq);
12236 Py_CLEAR(self->res);
12237 return 0;
12238 }
12239
12240 static void
12241 MoogLP_dealloc(MoogLP* self)
12242 {
12243 pyo_DEALLOC
12244 MoogLP_clear(self);
12245 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
12246 Py_TYPE(self)->tp_free((PyObject*)self);
12247 }
12248
12249 static PyObject *
12250 MoogLP_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
12251 {
12252 int i;
12253 PyObject *inputtmp, *input_streamtmp, *freqtmp = NULL, *restmp = NULL, *multmp = NULL, *addtmp = NULL;
12254 MoogLP *self;
12255 self = (MoogLP *)type->tp_alloc(type, 0);
12256
12257 self->freq = PyFloat_FromDouble(1000);
12258 self->res = PyFloat_FromDouble(1);
12259 self->last_freq = self->last_res = -1.0;
12260 self->y1 = self->y2 = self->y3 = self->y4 = self->oldX = self->oldY1 = self->oldY2 = self->oldY3 - 0.0;
12261 self->modebuffer[0] = 0;
12262 self->modebuffer[1] = 0;
12263 self->modebuffer[2] = 0;
12264 self->modebuffer[3] = 0;
12265
12266 INIT_OBJECT_COMMON
12267
12268 self->nyquist = (MYFLT)self->sr * 0.49;
12269 self->oneOverSr = 1.0 / (MYFLT)self->sr;
12270
12271 Stream_setFunctionPtr(self->stream, MoogLP_compute_next_data_frame);
12272 self->mode_func_ptr = MoogLP_setProcMode;
12273
12274 static char *kwlist[] = {"input", "freq", "res", "mul", "add", NULL};
12275
12276 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &inputtmp, &freqtmp, &restmp, &multmp, &addtmp))
12277 Py_RETURN_NONE;
12278
12279 INIT_INPUT_STREAM
12280
12281 if (freqtmp)
12282 {
12283 PyObject_CallMethod((PyObject *)self, "setFreq", "O", freqtmp);
12284 }
12285
12286 if (restmp)
12287 {
12288 PyObject_CallMethod((PyObject *)self, "setRes", "O", restmp);
12289 }
12290
12291 if (multmp)
12292 {
12293 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
12294 }
12295
12296 if (addtmp)
12297 {
12298 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
12299 }
12300
12301 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
12302
12303 (*self->mode_func_ptr)(self);
12304
12305 return (PyObject *)self;
12306 }
12307
12308 static PyObject * MoogLP_getServer(MoogLP* self) { GET_SERVER };
12309 static PyObject * MoogLP_getStream(MoogLP* self) { GET_STREAM };
12310 static PyObject * MoogLP_setMul(MoogLP *self, PyObject *arg) { SET_MUL };
12311 static PyObject * MoogLP_setAdd(MoogLP *self, PyObject *arg) { SET_ADD };
12312 static PyObject * MoogLP_setSub(MoogLP *self, PyObject *arg) { SET_SUB };
12313 static PyObject * MoogLP_setDiv(MoogLP *self, PyObject *arg) { SET_DIV };
12314
12315 static PyObject * MoogLP_play(MoogLP *self, PyObject *args, PyObject *kwds) { PLAY };
12316 static PyObject * MoogLP_out(MoogLP *self, PyObject *args, PyObject *kwds) { OUT };
12317 static PyObject * MoogLP_stop(MoogLP *self, PyObject *args, PyObject *kwds) { STOP };
12318
12319 static PyObject * MoogLP_multiply(MoogLP *self, PyObject *arg) { MULTIPLY };
12320 static PyObject * MoogLP_inplace_multiply(MoogLP *self, PyObject *arg) { INPLACE_MULTIPLY };
12321 static PyObject * MoogLP_add(MoogLP *self, PyObject *arg) { ADD };
12322 static PyObject * MoogLP_inplace_add(MoogLP *self, PyObject *arg) { INPLACE_ADD };
12323 static PyObject * MoogLP_sub(MoogLP *self, PyObject *arg) { SUB };
12324 static PyObject * MoogLP_inplace_sub(MoogLP *self, PyObject *arg) { INPLACE_SUB };
12325 static PyObject * MoogLP_div(MoogLP *self, PyObject *arg) { DIV };
12326 static PyObject * MoogLP_inplace_div(MoogLP *self, PyObject *arg) { INPLACE_DIV };
12327
12328 static PyObject * MoogLP_setFreq(MoogLP *self, PyObject *arg) { SET_PARAM(self->freq, self->freq_stream, 2); }
12329 static PyObject * MoogLP_setRes(MoogLP *self, PyObject *arg) { SET_PARAM(self->res, self->res_stream, 3); }
12330
12331 static PyMemberDef MoogLP_members[] =
12332 {
12333 {"server", T_OBJECT_EX, offsetof(MoogLP, server), 0, "Pyo server."},
12334 {"stream", T_OBJECT_EX, offsetof(MoogLP, stream), 0, "Stream object."},
12335 {"input", T_OBJECT_EX, offsetof(MoogLP, input), 0, "Input sound object."},
12336 {"freq", T_OBJECT_EX, offsetof(MoogLP, freq), 0, "Cutoff frequency in cycle per second."},
12337 {"res", T_OBJECT_EX, offsetof(MoogLP, res), 0, "Resonance factor."},
12338 {"mul", T_OBJECT_EX, offsetof(MoogLP, mul), 0, "Mul factor."},
12339 {"add", T_OBJECT_EX, offsetof(MoogLP, add), 0, "Add factor."},
12340 {NULL} /* Sentinel */
12341 };
12342
12343 static PyMethodDef MoogLP_methods[] =
12344 {
12345 {"getServer", (PyCFunction)MoogLP_getServer, METH_NOARGS, "Returns server object."},
12346 {"_getStream", (PyCFunction)MoogLP_getStream, METH_NOARGS, "Returns stream object."},
12347 {"play", (PyCFunction)MoogLP_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
12348 {"out", (PyCFunction)MoogLP_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
12349 {"stop", (PyCFunction)MoogLP_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
12350 {"setFreq", (PyCFunction)MoogLP_setFreq, METH_O, "Sets filter cutoff frequency in cycle per second."},
12351 {"setRes", (PyCFunction)MoogLP_setRes, METH_O, "Sets filter's resonance."},
12352 {"setMul", (PyCFunction)MoogLP_setMul, METH_O, "Sets oscillator mul factor."},
12353 {"setAdd", (PyCFunction)MoogLP_setAdd, METH_O, "Sets oscillator add factor."},
12354 {"setSub", (PyCFunction)MoogLP_setSub, METH_O, "Sets inverse add factor."},
12355 {"setDiv", (PyCFunction)MoogLP_setDiv, METH_O, "Sets inverse mul factor."},
12356 {NULL} /* Sentinel */
12357 };
12358
12359 static PyNumberMethods MoogLP_as_number =
12360 {
12361 (binaryfunc)MoogLP_add, /*nb_add*/
12362 (binaryfunc)MoogLP_sub, /*nb_subtract*/
12363 (binaryfunc)MoogLP_multiply, /*nb_multiply*/
12364 0, /*nb_remainder*/
12365 0, /*nb_divmod*/
12366 0, /*nb_power*/
12367 0, /*nb_neg*/
12368 0, /*nb_pos*/
12369 0, /*(unaryfunc)array_abs,*/
12370 0, /*nb_nonzero*/
12371 0, /*nb_invert*/
12372 0, /*nb_lshift*/
12373 0, /*nb_rshift*/
12374 0, /*nb_and*/
12375 0, /*nb_xor*/
12376 0, /*nb_or*/
12377 0, /*nb_int*/
12378 0, /*nb_long*/
12379 0, /*nb_float*/
12380 (binaryfunc)MoogLP_inplace_add, /*inplace_add*/
12381 (binaryfunc)MoogLP_inplace_sub, /*inplace_subtract*/
12382 (binaryfunc)MoogLP_inplace_multiply, /*inplace_multiply*/
12383 0, /*inplace_remainder*/
12384 0, /*inplace_power*/
12385 0, /*inplace_lshift*/
12386 0, /*inplace_rshift*/
12387 0, /*inplace_and*/
12388 0, /*inplace_xor*/
12389 0, /*inplace_or*/
12390 0, /*nb_floor_divide*/
12391 (binaryfunc)MoogLP_div, /*nb_true_divide*/
12392 0, /*nb_inplace_floor_divide*/
12393 (binaryfunc)MoogLP_inplace_div, /*nb_inplace_true_divide*/
12394 0, /* nb_index */
12395 };
12396
12397 PyTypeObject MoogLPType =
12398 {
12399 PyVarObject_HEAD_INIT(NULL, 0)
12400 "_pyo.MoogLP_base", /*tp_name*/
12401 sizeof(MoogLP), /*tp_basicsize*/
12402 0, /*tp_itemsize*/
12403 (destructor)MoogLP_dealloc, /*tp_dealloc*/
12404 0, /*tp_print*/
12405 0, /*tp_getattr*/
12406 0, /*tp_setattr*/
12407 0, /*tp_as_async (tp_compare in Python 2)*/
12408 0, /*tp_repr*/
12409 &MoogLP_as_number, /*tp_as_number*/
12410 0, /*tp_as_sequence*/
12411 0, /*tp_as_mapping*/
12412 0, /*tp_hash */
12413 0, /*tp_call*/
12414 0, /*tp_str*/
12415 0, /*tp_getattro*/
12416 0, /*tp_setattro*/
12417 0, /*tp_as_buffer*/
12418 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
12419 "MoogLP objects. Second-order resonant bandpass filter.", /* tp_doc */
12420 (traverseproc)MoogLP_traverse, /* tp_traverse */
12421 (inquiry)MoogLP_clear, /* tp_clear */
12422 0, /* tp_richcompare */
12423 0, /* tp_weaklistoffset */
12424 0, /* tp_iter */
12425 0, /* tp_iternext */
12426 MoogLP_methods, /* tp_methods */
12427 MoogLP_members, /* tp_members */
12428 0, /* tp_getset */
12429 0, /* tp_base */
12430 0, /* tp_dict */
12431 0, /* tp_descr_get */
12432 0, /* tp_descr_set */
12433 0, /* tp_dictoffset */
12434 0, /* tp_init */
12435 0, /* tp_alloc */
12436 MoogLP_new, /* tp_new */
12437 };