diff options
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/vcf~.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/intern/vcf~.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/vcf~.c b/apps/plugins/pdbox/PDa/intern/vcf~.c new file mode 100644 index 0000000000..1ff09812a1 --- /dev/null +++ b/apps/plugins/pdbox/PDa/intern/vcf~.c | |||
@@ -0,0 +1,238 @@ | |||
1 | #include <m_pd.h> | ||
2 | #include <m_fixed.h> | ||
3 | #include "cos_table.h" | ||
4 | |||
5 | /* ---------------- vcf~ - 2-pole bandpass filter. ----------------- */ | ||
6 | /* GG: complex resonator with signal frequency control | ||
7 | this time using the bigger cos_table without interpolation | ||
8 | really have to switch to a separate fixpoint format sometime | ||
9 | */ | ||
10 | |||
11 | typedef struct vcfctl | ||
12 | { | ||
13 | t_sample c_re; | ||
14 | t_sample c_im; | ||
15 | t_sample c_q; | ||
16 | t_sample c_isr; | ||
17 | } t_vcfctl; | ||
18 | |||
19 | typedef struct sigvcf | ||
20 | { | ||
21 | t_object x_obj; | ||
22 | t_vcfctl x_cspace; | ||
23 | t_vcfctl *x_ctl; | ||
24 | float x_f; | ||
25 | } t_sigvcf; | ||
26 | |||
27 | t_class *sigvcf_class; | ||
28 | |||
29 | static void *sigvcf_new(t_floatarg q) | ||
30 | { | ||
31 | t_sigvcf *x = (t_sigvcf *)pd_new(sigvcf_class); | ||
32 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
33 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1")); | ||
34 | outlet_new(&x->x_obj, gensym("signal")); | ||
35 | outlet_new(&x->x_obj, gensym("signal")); | ||
36 | x->x_ctl = &x->x_cspace; | ||
37 | x->x_cspace.c_re = 0; | ||
38 | x->x_cspace.c_im = 0; | ||
39 | x->x_cspace.c_q = ftofix(q); | ||
40 | x->x_cspace.c_isr = 0; | ||
41 | x->x_f = 0; | ||
42 | return (x); | ||
43 | } | ||
44 | |||
45 | static void sigvcf_ft1(t_sigvcf *x, t_floatarg f) | ||
46 | { | ||
47 | x->x_ctl->c_q = (f > 0 ? ftofix(f) : 0); | ||
48 | } | ||
49 | |||
50 | static t_int *sigvcf_perform(t_int *w) | ||
51 | { | ||
52 | t_sample *in1 = (t_sample *)(w[1]); | ||
53 | t_sample *in2 = (t_sample *)(w[2]); | ||
54 | t_sample *out1 = (t_sample *)(w[3]); | ||
55 | t_sample *out2 = (t_sample *)(w[4]); | ||
56 | t_vcfctl *c = (t_vcfctl *)(w[5]); | ||
57 | int n = (t_int)(w[6]); | ||
58 | int i; | ||
59 | t_sample re = c->c_re, re2; | ||
60 | t_sample im = c->c_im; | ||
61 | t_sample q = c->c_q; | ||
62 | t_sample qinv = (q > 0 ? idiv(ftofix(1.0),q) : 0); | ||
63 | t_sample ampcorrect = ftofix(2.0f) - idiv(ftofix(2.0f) , (q + ftofix(2.0f))); | ||
64 | t_sample isr = c->c_isr; | ||
65 | t_sample coefr, coefi; | ||
66 | t_sample *tab = cos_table; | ||
67 | t_sample oneminusr,cfindx,cf,r; | ||
68 | |||
69 | for (i = 0; i < n; i++) | ||
70 | { | ||
71 | cf = mult(*in2++,isr); | ||
72 | if (cf < 0) cf = 0; | ||
73 | cfindx = mult(cf,ftofix(0.15915494))>>(fix1-ILOGCOSTABSIZE); /* 1/2*PI */ | ||
74 | r = (qinv > 0 ? ftofix(1.01) - mult(cf,qinv) : 0); | ||
75 | |||
76 | if (r < 0) r = 0; | ||
77 | oneminusr = ftofix(1.02f) - r; /* hand adapted */ | ||
78 | |||
79 | /* r*cos(cf) */ | ||
80 | coefr = mult(r,tab[cfindx]); | ||
81 | |||
82 | /* r*sin(cf) */ | ||
83 | cfindx-=(ICOSTABSIZE>>2); | ||
84 | cfindx += cfindx < 0 ? ICOSTABSIZE:0; | ||
85 | coefi = mult(r,tab[cfindx]); | ||
86 | |||
87 | re2 = re; | ||
88 | *out1++ = re = mult(ampcorrect,mult(oneminusr,*in1++)) | ||
89 | + mult(coefr,re2) - mult(coefi, im); | ||
90 | *out2++ = im = mult(coefi,re2) + mult(coefr,im); | ||
91 | } | ||
92 | c->c_re = re; | ||
93 | c->c_im = im; | ||
94 | return (w+7); | ||
95 | } | ||
96 | |||
97 | static void sigvcf_dsp(t_sigvcf *x, t_signal **sp) | ||
98 | { | ||
99 | /* TODO sr is hardcoded */ | ||
100 | x->x_ctl->c_isr = ftofix(0.0001424758); | ||
101 | // idiv(ftofix(6.28318),ftofix(sp[0]->s_sr)); | ||
102 | post("%f",fixtof(x->x_ctl->c_isr)); | ||
103 | dsp_add(sigvcf_perform, 6, | ||
104 | sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, | ||
105 | x->x_ctl, sp[0]->s_n); | ||
106 | |||
107 | } | ||
108 | |||
109 | void vcf_tilde_setup(void) | ||
110 | { | ||
111 | sigvcf_class = class_new(gensym("vcf~"), (t_newmethod)sigvcf_new, 0, | ||
112 | sizeof(t_sigvcf), 0, A_DEFFLOAT, 0); | ||
113 | CLASS_MAINSIGNALIN(sigvcf_class, t_sigvcf, x_f); | ||
114 | class_addmethod(sigvcf_class, (t_method)sigvcf_dsp, gensym("dsp"), 0); | ||
115 | class_addmethod(sigvcf_class, (t_method)sigvcf_ft1, | ||
116 | gensym("ft1"), A_FLOAT, 0); | ||
117 | class_sethelpsymbol(sigvcf_class, gensym("lop~-help.pd")); | ||
118 | } | ||
119 | |||
120 | #include <m_pd.h> | ||
121 | #include <m_fixed.h> | ||
122 | #include "cos_table.h" | ||
123 | |||
124 | /* ---------------- vcf~ - 2-pole bandpass filter. ----------------- */ | ||
125 | /* GG: complex resonator with signal frequency control | ||
126 | this time using the bigger cos_table without interpolation | ||
127 | really have to switch to a separate fixpoint format sometime | ||
128 | */ | ||
129 | |||
130 | typedef struct vcfctl | ||
131 | { | ||
132 | t_sample c_re; | ||
133 | t_sample c_im; | ||
134 | t_sample c_q; | ||
135 | t_sample c_isr; | ||
136 | } t_vcfctl; | ||
137 | |||
138 | typedef struct sigvcf | ||
139 | { | ||
140 | t_object x_obj; | ||
141 | t_vcfctl x_cspace; | ||
142 | t_vcfctl *x_ctl; | ||
143 | float x_f; | ||
144 | } t_sigvcf; | ||
145 | |||
146 | t_class *sigvcf_class; | ||
147 | |||
148 | static void *sigvcf_new(t_floatarg q) | ||
149 | { | ||
150 | t_sigvcf *x = (t_sigvcf *)pd_new(sigvcf_class); | ||
151 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); | ||
152 | inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1")); | ||
153 | outlet_new(&x->x_obj, gensym("signal")); | ||
154 | outlet_new(&x->x_obj, gensym("signal")); | ||
155 | x->x_ctl = &x->x_cspace; | ||
156 | x->x_cspace.c_re = 0; | ||
157 | x->x_cspace.c_im = 0; | ||
158 | x->x_cspace.c_q = ftofix(q); | ||
159 | x->x_cspace.c_isr = 0; | ||
160 | x->x_f = 0; | ||
161 | return (x); | ||
162 | } | ||
163 | |||
164 | static void sigvcf_ft1(t_sigvcf *x, t_floatarg f) | ||
165 | { | ||
166 | x->x_ctl->c_q = (f > 0 ? ftofix(f) : 0); | ||
167 | } | ||
168 | |||
169 | static t_int *sigvcf_perform(t_int *w) | ||
170 | { | ||
171 | t_sample *in1 = (t_sample *)(w[1]); | ||
172 | t_sample *in2 = (t_sample *)(w[2]); | ||
173 | t_sample *out1 = (t_sample *)(w[3]); | ||
174 | t_sample *out2 = (t_sample *)(w[4]); | ||
175 | t_vcfctl *c = (t_vcfctl *)(w[5]); | ||
176 | int n = (t_int)(w[6]); | ||
177 | int i; | ||
178 | t_sample re = c->c_re, re2; | ||
179 | t_sample im = c->c_im; | ||
180 | t_sample q = c->c_q; | ||
181 | t_sample qinv = (q > 0 ? idiv(ftofix(1.0),q) : 0); | ||
182 | t_sample ampcorrect = ftofix(2.0f) - idiv(ftofix(2.0f) , (q + ftofix(2.0f))); | ||
183 | t_sample isr = c->c_isr; | ||
184 | t_sample coefr, coefi; | ||
185 | t_sample *tab = cos_table; | ||
186 | t_sample oneminusr,cfindx,cf,r; | ||
187 | |||
188 | for (i = 0; i < n; i++) | ||
189 | { | ||
190 | cf = mult(*in2++,isr); | ||
191 | if (cf < 0) cf = 0; | ||
192 | cfindx = mult(cf,ftofix(0.15915494))>>(fix1-ILOGCOSTABSIZE); /* 1/2*PI */ | ||
193 | r = (qinv > 0 ? ftofix(1.01) - mult(cf,qinv) : 0); | ||
194 | |||
195 | if (r < 0) r = 0; | ||
196 | oneminusr = ftofix(1.02f) - r; /* hand adapted */ | ||
197 | |||
198 | /* r*cos(cf) */ | ||
199 | coefr = mult(r,tab[cfindx]); | ||
200 | |||
201 | /* r*sin(cf) */ | ||
202 | cfindx-=(ICOSTABSIZE>>2); | ||
203 | cfindx += cfindx < 0 ? ICOSTABSIZE:0; | ||
204 | coefi = mult(r,tab[cfindx]); | ||
205 | |||
206 | re2 = re; | ||
207 | *out1++ = re = mult(ampcorrect,mult(oneminusr,*in1++)) | ||
208 | + mult(coefr,re2) - mult(coefi, im); | ||
209 | *out2++ = im = mult(coefi,re2) + mult(coefr,im); | ||
210 | } | ||
211 | c->c_re = re; | ||
212 | c->c_im = im; | ||
213 | return (w+7); | ||
214 | } | ||
215 | |||
216 | static void sigvcf_dsp(t_sigvcf *x, t_signal **sp) | ||
217 | { | ||
218 | /* TODO sr is hardcoded */ | ||
219 | x->x_ctl->c_isr = ftofix(0.0001424758); | ||
220 | // idiv(ftofix(6.28318),ftofix(sp[0]->s_sr)); | ||
221 | post("%f",fixtof(x->x_ctl->c_isr)); | ||
222 | dsp_add(sigvcf_perform, 6, | ||
223 | sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, | ||
224 | x->x_ctl, sp[0]->s_n); | ||
225 | |||
226 | } | ||
227 | |||
228 | void vcf_tilde_setup(void) | ||
229 | { | ||
230 | sigvcf_class = class_new(gensym("vcf~"), (t_newmethod)sigvcf_new, 0, | ||
231 | sizeof(t_sigvcf), 0, A_DEFFLOAT, 0); | ||
232 | CLASS_MAINSIGNALIN(sigvcf_class, t_sigvcf, x_f); | ||
233 | class_addmethod(sigvcf_class, (t_method)sigvcf_dsp, gensym("dsp"), 0); | ||
234 | class_addmethod(sigvcf_class, (t_method)sigvcf_ft1, | ||
235 | gensym("ft1"), A_FLOAT, 0); | ||
236 | class_sethelpsymbol(sigvcf_class, gensym("lop~-help.pd")); | ||
237 | } | ||
238 | |||