aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/rampsmooth.c
blob: 32be1d7ffccd05191a6e7705b47fb22507ed622e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include "m_pd.h"
#include "shared.h"
#include "sickle/sic.h"

/* LATER select the mode fitter-optionally */
/* #define RAMPSMOOTH_GEOMETRIC  /* geometric series (same as slide~) CHECKED */
#ifndef RAMPSMOOTH_GEOMETRIC
#define RAMPSMOOTH_LINEAR
#endif
#define RAMPSMOOTH_DEFNUP    0.
#define RAMPSMOOTH_DEFNDOWN  0.

typedef struct _rampsmooth
{
    t_sic    x_sic;
    int      x_nup;
    int      x_ndown;
    double   x_upcoef;
    double   x_downcoef;
    t_float  x_last;
#ifdef RAMPSMOOTH_LINEAR
    t_float  x_target;
    double   x_incr;
    int      x_nleft;
#endif
} t_rampsmooth;

static t_class *rampsmooth_class;

#ifdef RAMPSMOOTH_LINEAR
/* this is a true linear ramper */
static t_int *rampsmooth_perform(t_int *w)
{
    t_rampsmooth *x = (t_rampsmooth *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    t_float *out = (t_float *)(w[4]);
    t_float last = x->x_last;
    t_float target = x->x_target;
    double incr = x->x_incr;
    int nleft = x->x_nleft;
    while (nblock--)
    {
    	t_float f = *in++;
	if (f != target)
	{
	    target = f;
	    if (f > last)
	    {
		if (x->x_nup > 1)
		{
		    incr = (f - last) * x->x_upcoef;
		    nleft = x->x_nup;
		    *out++ = (last += incr);
		    continue;
		}
	    }
	    else if (f < last)
	    {
		if (x->x_ndown > 1)
		{
		    incr = (f - last) * x->x_downcoef;
		    nleft = x->x_ndown;
		    *out++ = (last += incr);
		    continue;
		}
	    }
	    incr = 0.;
	    nleft = 0;
	    *out++ = last = f;
	}
	else if (nleft > 0)
	{
	    *out++ = (last += incr);
	    if (--nleft == 0)
	    {
		incr = 0.;
		last = target;
	    }
	}
	else *out++ = target;
    }
    x->x_last = (PD_BIGORSMALL(last) ? 0. : last);
    x->x_target = (PD_BIGORSMALL(target) ? 0. : target);
    x->x_incr = incr;
    x->x_nleft = nleft;
    return (w + 5);
}

#else

/* this ramper generates a geometric series output for a single step input */
static t_int *rampsmooth_perform(t_int *w)
{
    t_rampsmooth *x = (t_rampsmooth *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    t_float *out = (t_float *)(w[4]);
    t_float last = x->x_last;
    while (nblock--)
    {
    	t_float f = *in++;
	if (f > last)
	{
	    if (x->x_nup > 1)
	    {
		*out++ = (last += (f - last) * x->x_upcoef);
		continue;
	    }
	}
	else if (f < last)
	{
	    if (x->x_ndown > 1)
	    {
		*out++ = (last += (f - last) * x->x_downcoef);
		continue;
	    }
	}
	*out++ = last = f;
    }
    x->x_last = (PD_BIGORSMALL(last) ? 0. : last);
    return (w + 5);
}
#endif

static void rampsmooth_dsp(t_rampsmooth *x, t_signal **sp)
{
    dsp_add(rampsmooth_perform, 4, x, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
}

static void rampsmooth_rampup(t_rampsmooth *x, t_floatarg f)
{
    int i = (int)f;
    if (i > 1)  /* CHECKME if 1 and 0 differ in any way */
    {
	x->x_nup = i;
	x->x_upcoef = 1. / (float)i;
    }
    else
    {
	x->x_nup = 0;
	x->x_upcoef = 0.;
    }
}

static void rampsmooth_rampdown(t_rampsmooth *x, t_floatarg f)
{
    int i = (int)f;
    if (i > 1)  /* CHECKME if 1 and 0 differ in any way */
    {
	x->x_ndown = i;
	x->x_downcoef = 1. / (float)i;
    }
    else
    {
	x->x_ndown = 0;
	x->x_downcoef = 0.;
    }
}

/* CHECKED */
static void rampsmooth_ramp(t_rampsmooth *x, t_floatarg f)
{
    rampsmooth_rampup(x, f);
    rampsmooth_rampdown(x, f);
}

static void *rampsmooth_new(t_symbol *s, int ac, t_atom *av)
{
    t_rampsmooth *x = (t_rampsmooth *)pd_new(rampsmooth_class);
    float f1 = RAMPSMOOTH_DEFNUP;
    float f2 = RAMPSMOOTH_DEFNDOWN;
    if (ac && av->a_type == A_FLOAT)
    {
	f1 = av->a_w.w_float;
	ac--; av++;
	if (ac && av->a_type == A_FLOAT)
	    f2 = av->a_w.w_float;
    }
    rampsmooth_rampup(x, f1);
    rampsmooth_rampdown(x, f2);
    x->x_last = 0.;
#ifdef RAMPSMOOTH_LINEAR
    x->x_target = 0.;
    x->x_incr = 0.;
    x->x_nleft = 0;
#endif
    outlet_new((t_object *)x, &s_signal);
    return (x);
}

void rampsmooth_tilde_setup(void)
{
    rampsmooth_class = class_new(gensym("rampsmooth~"),
				 (t_newmethod)rampsmooth_new, 0,
				 sizeof(t_rampsmooth), 0, A_GIMME, 0);
    sic_setup(rampsmooth_class, rampsmooth_dsp, SIC_FLOATTOSIGNAL);
    class_addmethod(rampsmooth_class, (t_method)rampsmooth_rampup,
		    gensym("rampup"), A_FLOAT, 0);
    class_addmethod(rampsmooth_class, (t_method)rampsmooth_rampdown,
		    gensym("rampdown"), A_FLOAT, 0);
    class_addmethod(rampsmooth_class, (t_method)rampsmooth_ramp,
		    gensym("ramp"), A_FLOAT, 0);
}