~ubuntu-branches/ubuntu/saucy/pd-smlib/saucy

« back to all changes in this revision

Viewing changes to bp.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans-Christoph Steiner
  • Date: 2010-11-10 15:17:58 UTC
  • Revision ID: james.westby@ubuntu.com-20101110151758-3acjf69kiudo3gh4
Tags: upstream-0.12.1
ImportĀ upstreamĀ versionĀ 0.12.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "defines.h"
 
2
 
 
3
/*--------------- bp ---------------*/
 
4
 
 
5
typedef struct bpctl
 
6
{
 
7
    float c_x1;
 
8
    float c_x2;
 
9
    float c_coef1;
 
10
    float c_coef2;
 
11
    float c_gain;
 
12
} t_bpctl;
 
13
 
 
14
typedef struct bp
 
15
{
 
16
    t_object x_obj;
 
17
    float x_freq;
 
18
    float x_q;
 
19
    t_bpctl x_cspace;
 
20
    t_bpctl *x_ctl;
 
21
    float x_f;
 
22
} t_bp;
 
23
 
 
24
t_class *bp_class;
 
25
 
 
26
static void bp_docoef(t_bp *x, t_floatarg f, t_floatarg q);
 
27
 
 
28
static void *bp_new(t_floatarg f, t_floatarg q)
 
29
{
 
30
    t_bp *x = (t_bp *)pd_new(bp_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("float"));
 
34
    x->x_ctl = &x->x_cspace;
 
35
    x->x_cspace.c_x1 = 0;
 
36
    x->x_cspace.c_x2 = 0;
 
37
    bp_docoef(x, f, q);
 
38
    x->x_f = 0;
 
39
    return (x);
 
40
}
 
41
 
 
42
static float bp_qcos(float f)
 
43
{
 
44
    if (f >= -(0.5f*3.14159f) && f <= 0.5f*3.14159f)
 
45
    {
 
46
        float g = f*f;
 
47
        return (((g*g*g * (-1.0f/720.0f) + g*g*(1.0f/24.0f)) - g*0.5f) + 1);
 
48
    }
 
49
    else return (0);
 
50
}
 
51
 
 
52
static void bp_docoef(t_bp *x, t_floatarg f, t_floatarg q)
 
53
{
 
54
    float r, oneminusr, omega;
 
55
    if (f < 0.0001f) f = 0.0001f;
 
56
    if (q < 0) q = 0;
 
57
    x->x_freq = f;
 
58
    x->x_q = q;
 
59
    omega = f * (2.0f * 3.14159f);
 
60
    if (q < 0.001) oneminusr = 1.0f;
 
61
    else oneminusr = omega/q;
 
62
    if (oneminusr > 1.0f) oneminusr = 1.0f;
 
63
    r = 1.0f - oneminusr;
 
64
    x->x_ctl->c_coef1 = 2.0f * bp_qcos(omega) * r;
 
65
    x->x_ctl->c_coef2 = - r * r;
 
66
    x->x_ctl->c_gain = 2 * oneminusr * (oneminusr + r * omega);
 
67
    /* post("r %f, omega %f, coef1 %f, coef2 %f",
 
68
        r, omega, x->x_ctl->c_coef1, x->x_ctl->c_coef2); */
 
69
}
 
70
 
 
71
static void bp_ft1(t_bp *x, t_floatarg f)
 
72
{
 
73
    bp_docoef(x, f, x->x_q);
 
74
}
 
75
 
 
76
static void bp_ft2(t_bp *x, t_floatarg q)
 
77
{
 
78
    bp_docoef(x, x->x_freq, q);
 
79
}
 
80
 
 
81
static void bp_clear(t_bp *x, t_floatarg q)
 
82
{
 
83
    x->x_ctl->c_x1 = x->x_ctl->c_x2 = 0;
 
84
}
 
85
 
 
86
static void bp_perform(t_bp *x, t_float in)
 
87
{
 
88
        float out;
 
89
    t_bpctl *c = x->x_ctl;
 
90
    float last = c->c_x1;
 
91
    float prev = c->c_x2;
 
92
    float coef1 = c->c_coef1;
 
93
    float coef2 = c->c_coef2;
 
94
    float gain = c->c_gain;
 
95
 
 
96
        float output =  in + coef1 * last + coef2 * prev;
 
97
        out = gain * output;
 
98
 
 
99
        prev = last;
 
100
        last = output;
 
101
 
 
102
        /* NAN protect */
 
103
    if (!((last <= 0) || (last >= 0)))
 
104
        last = 0;
 
105
    if (!((prev <= 0) || (prev >= 0)))
 
106
        prev = 0;
 
107
    c->c_x1 = last;
 
108
    c->c_x2 = prev;
 
109
 
 
110
    outlet_float(x->x_obj.ob_outlet, out);
 
111
}
 
112
 
 
113
void bp_setup(void)
 
114
{
 
115
    bp_class = class_new(gensym("bp"), (t_newmethod)bp_new, 0,
 
116
        sizeof(t_bp), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
 
117
    class_addfloat(bp_class, (t_method)bp_perform);
 
118
    class_addmethod(bp_class, (t_method)bp_ft1,
 
119
        gensym("ft1"), A_FLOAT, 0);
 
120
    class_addmethod(bp_class, (t_method)bp_ft2,
 
121
        gensym("ft2"), A_FLOAT, 0);
 
122
    class_addmethod(bp_class, (t_method)bp_clear, gensym("clear"), 0);
 
123
}