Add the identifying header
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / moog~.c
blob111010cb13341d3bca0ec678f146910df56a9fc0
1 /* (C) Guenter Geiger <geiger@epy.co.at> */
4 #include "math.h"
5 #ifdef ROCKBOX
6 #include "../src/m_pd.h"
7 #else
8 #include "../src/m_pd.h"
9 #endif
11 /* ----------------------------- moog ----------------------------- */
12 static t_class *moog_class;
15 typedef struct _moog
17 t_object x_obj;
18 t_pd in2;
19 t_sample x_1,x_2,x_3,x_4;
20 t_sample y_1,y_2,y_3,y_4;
21 } t_moog;
23 static void moog_reset(t_moog *x)
25 x->x_1 = x->x_2 = x->x_3 = x->x_4 = 0;
26 x->y_1 = x->y_2 = x->y_3 = x->y_4 = 0;
32 static void *moog_new(t_symbol *s, int argc, t_atom *argv)
34 #ifdef ROCKBOX
35 (void) s;
36 (void) argv;
37 #endif
38 if (argc > 1) post("moog~: extra arguments ignored");
40 t_moog *x = (t_moog *)pd_new(moog_class);
41 outlet_new(&x->x_obj, &s_signal);
42 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
43 inlet_new(&x->x_obj, &x->in2, &s_signal, &s_signal);
44 moog_reset(x);
45 return (x);
53 static t_sample calc_k(t_sample f,t_sample k) {
54 if (k>itofix(4)) k = itofix(4);
55 if (k < 0) k = 0;
56 if (f <= itofix(3800)) return k;
57 k = k - mult(0.5,(f-idiv(itofix(3800),itofix(4300))));
58 return k;
61 t_int *moog_perform(t_int *w)
63 t_moog* x = (t_moog*) (w[1]);
64 t_sample *in1 = (t_sample *)(w[2]);
65 t_sample *p = (t_sample *)(w[3]);
66 t_sample *k = (t_sample *)(w[4]);
68 t_sample *out = (t_sample *)(w[5]);
69 int n = (int)(w[6]);
70 t_sample in;
71 t_sample pt,pt1;
73 t_sample x1 = x->x_1;
74 t_sample x2 = x->x_2;
75 t_sample x3 = x->x_3;
76 t_sample x4 = x->x_4;
77 t_sample ys1 = x->y_1;
78 t_sample ys2 = x->y_2;
79 t_sample ys3 = x->y_3;
80 t_sample ys4 = x->y_4;
83 while (n--) {
84 if (*p > itofix(8140)) *p = itofix(8140);
85 *k = calc_k(*p,*k);
86 pt =*p;
87 pt1=mult((pt+1),ftofix(0.76923077));
88 in = *in1++ - mult(*k,ys4);
89 ys1 = mult(pt1,in) + mult(0.3,x1) - mult(pt,ys1);
90 x1 = in;
91 ys2 = mult(pt1,ys1) + mult(0.3,x2) - mult(pt,ys2);
92 x2 = ys1;
93 ys3 = mult(pt1,ys2) + mult(0.3,x3) - mult(pt,ys3);
94 x3 = ys2;
95 ys4 = mult(pt1,ys3) + mult(0.3,x4) - mult(pt,ys4);
96 x4 = ys3;
97 *out++ = ys4;
101 x->y_1 = ys1;
102 x->y_2 = ys2;
103 x->y_3 = ys3;
104 x->y_4 = ys4;
105 x->x_1 = x1;
106 x->x_2 = x2;
107 x->x_3 = x3;
108 x->x_4 = x4;
110 return (w+7);
114 #define CLIP(x) x = ((x) > 1.0 ? (1.0) : (x))
116 t_int *moog_perf8(t_int *w)
118 t_moog* x = (t_moog*) (w[1]);
119 t_sample *in1 = (t_sample *)(w[2]);
120 t_sample *p = (t_sample *)(w[3]);
121 t_sample *k = (t_sample *)(w[4]);
122 t_sample *out = (t_sample *)(w[5]);
123 int n = (int)(w[6]);
125 t_sample x1 = x->x_1;
126 t_sample x2 = x->x_2;
127 t_sample x3 = x->x_3;
128 t_sample x4 = x->x_4;
129 t_sample ys1 = x->y_1;
130 t_sample ys2 = x->y_2;
131 t_sample ys3 = x->y_3;
132 t_sample ys4 = x->y_4;
134 #ifndef ROCKBOX
135 t_sample temp,temp2;
136 #endif
137 t_sample pt,pt1;
138 t_sample in;
140 while (n--) {
141 if (*p > itofix(8140)) *p = itofix(8140);
142 *k = calc_k(*p,*k);
144 pt =mult(*p, ftofix(0.01*0.0140845)) - ftofix(0.9999999f);
145 pt1=mult((pt+itofix(1)),ftofix(0.76923077));
146 in = *in1++ - mult(*k,ys4);
147 ys1 = mult(pt1,(in + mult(ftofix(0.3),x1))) - mult(pt,ys1);
148 x1 = in;
149 ys2 = mult(pt1,(ys1 + mult(0.3,x2))) - mult(pt,ys2);
150 x2 = ys1;
151 ys3 = mult(pt1,(ys2 + mult(0.3,x3))) - mult(pt,ys3);
152 x3 = ys2;
153 ys4 = mult(pt1,(ys3 + mult(0.3,x4))) - mult(pt,ys4);
154 x4 = ys3;
155 *out++ = ys4;
157 p++;k++;
160 x->y_1 = ys1;
161 x->y_2 = ys2;
162 x->y_3 = ys3;
163 x->y_4 = ys4;
164 x->x_1 = x1;
165 x->x_2 = x2;
166 x->x_3 = x3;
167 x->x_4 = x4;
169 return (w+7);
172 void dsp_add_moog(t_moog *x, t_sample *in1, t_sample *in2, t_sample *in3, t_sample *out, int n)
174 if (n&7)
175 dsp_add(moog_perform, 6,(t_int)x, in1,in2,in3, out, n);
176 else
177 dsp_add(moog_perf8, 6,(t_int) x, in1, in2, in3, out, n);
180 static void moog_dsp(t_moog *x, t_signal **sp)
182 dsp_add_moog(x,sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec,sp[0]->s_n);
186 void moog_tilde_setup(void)
188 moog_class = class_new(gensym("moog~"), (t_newmethod)moog_new, 0,
189 sizeof(t_moog), 0, A_GIMME, 0);
190 class_addmethod(moog_class, nullfn, gensym("signal"), 0);
191 class_addmethod(moog_class, (t_method)moog_reset, gensym("reset"), 0);
192 class_addmethod(moog_class, (t_method)moog_dsp, gensym("dsp"), A_NULL);