1 #include "../src/m_pd.h"
2 #include <../src/m_fixed.h>
26 static void sigbp_docoef(t_sigbp
*x
, t_floatarg f
, t_floatarg q
);
28 static void *sigbp_new(t_floatarg f
, t_floatarg q
)
30 t_sigbp
*x
= (t_sigbp
*)pd_new(sigbp_class
);
31 inlet_new(&x
->x_obj
, &x
->x_obj
.ob_pd
, gensym("float"), gensym("ft1"));
32 inlet_new(&x
->x_obj
, &x
->x_obj
.ob_pd
, gensym("float"), gensym("ft2"));
33 outlet_new(&x
->x_obj
, gensym("signal"));
35 x
->x_ctl
= &x
->x_cspace
;
38 sigbp_docoef(x
, f
, q
);
43 static float sigbp_qcos(float f
)
45 if (f
>= -(0.5f
*3.14159f
) && f
<= 0.5f
*3.14159f
)
48 return (((g
*g
*g
* (-1.0f
/720.0f
) + g
*g
*(1.0f
/24.0f
)) - g
*0.5) + 1);
53 static void sigbp_docoef(t_sigbp
*x
, t_floatarg f
, t_floatarg q
)
55 float r
, oneminusr
, omega
;
56 if (f
< 0.001) f
= 10;
60 omega
= f
* (2.0f
* 3.14159f
) / x
->x_sr
;
61 if (q
< 0.001) oneminusr
= 1.0f
;
62 else oneminusr
= omega
/q
;
63 if (oneminusr
> 1.0f
) oneminusr
= 1.0f
;
65 x
->x_ctl
->c_coef1
= ftofix(2.0f
* sigbp_qcos(omega
) * r
);
66 x
->x_ctl
->c_coef2
= ftofix(- r
* r
);
67 x
->x_ctl
->c_gain
= ftofix(2 * oneminusr
* (oneminusr
+ r
* omega
));
68 /* post("r %f, omega %f, coef1 %f, coef2 %f",
69 r, omega, x->x_ctl->c_coef1, x->x_ctl->c_coef2); */
72 static void sigbp_ft1(t_sigbp
*x
, t_floatarg f
)
74 sigbp_docoef(x
, f
, x
->x_q
);
77 static void sigbp_ft2(t_sigbp
*x
, t_floatarg q
)
79 sigbp_docoef(x
, x
->x_freq
, q
);
82 static void sigbp_clear(t_sigbp
*x
, t_floatarg q
)
87 x
->x_ctl
->c_x1
= x
->x_ctl
->c_x2
= 0;
90 static t_int
*sigbp_perform(t_int
*w
)
92 t_sample
*in
= (t_sample
*)(w
[1]);
93 t_sample
*out
= (t_sample
*)(w
[2]);
94 t_bpctl
*c
= (t_bpctl
*)(w
[3]);
95 int n
= (t_int
)(w
[4]);
97 t_sample last
= c
->c_x1
;
98 t_sample prev
= c
->c_x2
;
99 t_sample coef1
= c
->c_coef1
;
100 t_sample coef2
= c
->c_coef2
;
101 t_sample gain
= c
->c_gain
;
102 for (i
= 0; i
< n
; i
++)
104 t_sample output
= *in
++ + mult(coef1
,last
) + mult(coef2
,prev
);
105 *out
++ = mult(gain
,output
);
109 if (PD_BADFLOAT(last
))
111 if (PD_BADFLOAT(prev
))
118 static void sigbp_dsp(t_sigbp
*x
, t_signal
**sp
)
120 x
->x_sr
= sp
[0]->s_sr
;
121 sigbp_docoef(x
, x
->x_freq
, x
->x_q
);
122 dsp_add(sigbp_perform
, 4,
123 sp
[0]->s_vec
, sp
[1]->s_vec
,
124 x
->x_ctl
, sp
[0]->s_n
);
128 void bp_tilde_setup(void)
130 sigbp_class
= class_new(gensym("bp~"), (t_newmethod
)sigbp_new
, 0,
131 sizeof(t_sigbp
), 0, A_DEFFLOAT
, A_DEFFLOAT
, 0);
132 CLASS_MAINSIGNALIN(sigbp_class
, t_sigbp
, x_f
);
133 class_addmethod(sigbp_class
, (t_method
)sigbp_dsp
, gensym("dsp"), 0);
134 class_addmethod(sigbp_class
, (t_method
)sigbp_ft1
,
135 gensym("ft1"), A_FLOAT
, 0);
136 class_addmethod(sigbp_class
, (t_method
)sigbp_ft2
,
137 gensym("ft2"), A_FLOAT
, 0);
138 class_addmethod(sigbp_class
, (t_method
)sigbp_clear
, gensym("clear"), 0);
139 class_sethelpsymbol(sigbp_class
, gensym("lop~-help.pd"));