aboutsummaryrefslogtreecommitdiff
path: root/rcosc~/rcosc~.c
blob: 0ab1c9de686d78c49f7686ef8d020f572bfc41e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* rcosc~.c by Martin Peach 20100331 */
/* Pd external emulating a resistor-capacitor-controlled oscillator */
/* The first control parameter is a time constant in seconds (or resistance X capacitance) */
/* The second control parameter is a threshold above which the cap is set to discharge */
/* The third control parameter is a threshold below which the cap is set to charge */

#include "m_pd.h"

static t_class *rcosc_tilde_class;

typedef struct _rcosc_tilde
{
    t_object    rc_obj;
    t_float     rc_f;
    t_float     rc_upper_threshold; /* if rc_dir is 1, charge to here */
    t_float     rc_lower_threshold; /* if rc_dir is 0, discharge to here */
    t_int       rc_dir;
    double      rc_node;
    double      rc_one;
    double      rc_zero;
    double      rc_sp;
    double      rc_slewmax;
} t_rcosc_tilde;

static t_int *rcosc_tilde_perform(t_int *w);
static void rcosc_tilde_dsp(t_rcosc_tilde *x, t_signal **sp);
static void *rcosc_tilde_new(t_floatarg f);

static t_int *rcosc_tilde_perform(t_int *w)
{
    t_rcosc_tilde   *x = (t_rcosc_tilde *)(w[1]);
    t_sample        *in = (t_sample *)(w[2]);
    t_sample        *out = (t_sample *)(w[3]);
    int             n = (int)(w[4]);
    double          slewrate, delta, node, overshoot;
    int             i, n_oversamples = 64;

//    if (x->rc_rc < x->rc_sp) slewrate = 1.0;    
//    else slewrate = x->rc_sp/x->rc_rc;
        
    while (n--)
    {
        slewrate =  x->rc_one/(n_oversamples*(x->rc_sp*(*in++)));
        if (slewrate < x->rc_zero) slewrate = x->rc_zero;
        else if (slewrate > x->rc_one) slewrate = x->rc_one;
        node = x->rc_node;
        for (i = 0; i < n_oversamples; ++i)
        {
            if (x->rc_dir)
            {
                delta = slewrate*(x->rc_one - x->rc_node);
                if (delta > x->rc_slewmax) delta = x->rc_slewmax;
                x->rc_node += delta;
                overshoot = x->rc_node - x->rc_upper_threshold;
                if (overshoot > 0)
                {
                    x->rc_dir = 0;
                    x->rc_node = x->rc_upper_threshold - overshoot; /* bounce */
                }
            }
            else
            {
                delta = slewrate*(x->rc_zero - x->rc_node);
                if (delta < -x->rc_slewmax) delta = -x->rc_slewmax;
                x->rc_node += delta;
                overshoot = x->rc_lower_threshold - x->rc_node;
                if (overshoot > 0)
                {
                    x->rc_dir = 1;
                    x->rc_node = x->rc_lower_threshold + overshoot; /* bounce */
                }
            }
        }
        *out++ = ((node + x->rc_node)*3)-3;
    }
    return (w+5);
}

static void rcosc_tilde_dsp(t_rcosc_tilde *x, t_signal **sp)
{
    x->rc_sp = sys_getsr();
    dsp_add(rcosc_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void *rcosc_tilde_new(t_floatarg f)
{
    t_rcosc_tilde *x = (t_rcosc_tilde *)pd_new(rcosc_tilde_class);

    x->rc_f = f;
    x->rc_node = 0.0;
    x->rc_upper_threshold = 0.666; /* if rc_dir is 1, charge to here */
    x->rc_lower_threshold = 0.333; /* if rc_dir is 0, discharge to here */
    x->rc_slewmax = 0.003;/* arbitrary slew rate limit */
    x->rc_dir = 1; /* start charging */
    x->rc_one = 1;
    x->rc_zero = 0;
  
    outlet_new(&x->rc_obj, &s_signal);

    post("rcosc~ 20100331 Martin Peach");
    return (void *)x;
}

void rcosc_tilde_setup(void)
{
    rcosc_tilde_class = class_new(gensym("rcosc~"),
        (t_newmethod)rcosc_tilde_new,
        0, sizeof(t_rcosc_tilde),
        CLASS_DEFAULT, 
        A_DEFFLOAT, 0);

    class_addmethod(rcosc_tilde_class, (t_method)rcosc_tilde_dsp, gensym("dsp"), 0);
    CLASS_MAINSIGNALIN(rcosc_tilde_class, t_rcosc_tilde, rc_f);
}

/* fin rcosc~.c */