summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/intern/line~.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/line~.c')
-rw-r--r--apps/plugins/pdbox/PDa/intern/line~.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/line~.c b/apps/plugins/pdbox/PDa/intern/line~.c
new file mode 100644
index 0000000000..ac7af8721e
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/intern/line~.c
@@ -0,0 +1,202 @@
1#include <m_pd.h>
2#include <m_fixed.h>
3
4static t_class *line_class;
5
6typedef struct _line
7{
8 t_object x_obj;
9 t_sample x_target;
10 t_sample x_value;
11 t_sample x_biginc;
12 t_sample x_inc;
13 t_sample x_1overn;
14 t_sample x_msectodsptick;
15 t_floatarg x_inletvalue;
16 t_floatarg x_inletwas;
17 int x_ticksleft;
18 int x_retarget;
19} t_line;
20
21static t_int *line_perform(t_int *w)
22{
23 t_line *x = (t_line *)(w[1]);
24 t_sample *out = (t_sample *)(w[2]);
25 int n = (int)(w[3]);
26 t_sample f = x->x_value;
27
28 if (x->x_retarget)
29 {
30 int nticks = mult(ftofix(x->x_inletwas),x->x_msectodsptick);
31 if (!nticks) nticks = itofix(1);
32 x->x_ticksleft = fixtoi(nticks);
33 x->x_biginc = (x->x_target - x->x_value);
34 x->x_biginc = idiv(x->x_biginc,nticks);
35 x->x_inc = mult(x->x_1overn, x->x_biginc);
36 x->x_retarget = 0;
37 }
38 if (x->x_ticksleft)
39 {
40 t_sample f = x->x_value;
41 while (n--) *out++ = f, f += x->x_inc;
42 x->x_value += x->x_biginc;
43 x->x_ticksleft--;
44 }
45 else
46 {
47 x->x_value = x->x_target;
48 while (n--) *out++ = x->x_value;
49 }
50 return (w+4);
51}
52
53static void line_float(t_line *x, t_float f)
54{
55 if (x->x_inletvalue <= 0)
56 {
57 x->x_target = x->x_value = ftofix(f);
58 x->x_ticksleft = x->x_retarget = 0;
59 }
60 else
61 {
62 x->x_target = ftofix(f);
63 x->x_retarget = 1;
64 x->x_inletwas = x->x_inletvalue;
65 x->x_inletvalue = 0;
66 }
67}
68
69static void line_stop(t_line *x)
70{
71 x->x_target = x->x_value;
72 x->x_ticksleft = x->x_retarget = 0;
73}
74
75static void line_dsp(t_line *x, t_signal **sp)
76{
77 dsp_add(line_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
78 x->x_1overn = ftofix(1)/sp[0]->s_n;
79 x->x_msectodsptick = idiv(sp[0]->s_sr, (1000 * sp[0]->s_n));
80}
81
82static void *line_new(void)
83{
84 t_line *x = (t_line *)pd_new(line_class);
85 outlet_new(&x->x_obj, gensym("signal"));
86 floatinlet_new(&x->x_obj, &x->x_inletvalue);
87 x->x_ticksleft = x->x_retarget = 0;
88 x->x_value = x->x_target = x->x_inletvalue = x->x_inletwas = 0;
89 return (x);
90}
91
92void line_tilde_setup(void)
93{
94 line_class = class_new(gensym("line~"), line_new, 0,
95 sizeof(t_line), 0, 0);
96 class_addfloat(line_class, (t_method)line_float);
97 class_addmethod(line_class, (t_method)line_dsp, gensym("dsp"), 0);
98 class_addmethod(line_class, (t_method)line_stop, gensym("stop"), 0);
99}
100
101
102#include <m_pd.h>
103#include <m_fixed.h>
104
105static t_class *line_class;
106
107typedef struct _line
108{
109 t_object x_obj;
110 t_sample x_target;
111 t_sample x_value;
112 t_sample x_biginc;
113 t_sample x_inc;
114 t_sample x_1overn;
115 t_sample x_msectodsptick;
116 t_floatarg x_inletvalue;
117 t_floatarg x_inletwas;
118 int x_ticksleft;
119 int x_retarget;
120} t_line;
121
122static t_int *line_perform(t_int *w)
123{
124 t_line *x = (t_line *)(w[1]);
125 t_sample *out = (t_sample *)(w[2]);
126 int n = (int)(w[3]);
127 t_sample f = x->x_value;
128
129 if (x->x_retarget)
130 {
131 int nticks = mult(ftofix(x->x_inletwas),x->x_msectodsptick);
132 if (!nticks) nticks = itofix(1);
133 x->x_ticksleft = fixtoi(nticks);
134 x->x_biginc = (x->x_target - x->x_value);
135 x->x_biginc = idiv(x->x_biginc,nticks);
136 x->x_inc = mult(x->x_1overn, x->x_biginc);
137 x->x_retarget = 0;
138 }
139 if (x->x_ticksleft)
140 {
141 t_sample f = x->x_value;
142 while (n--) *out++ = f, f += x->x_inc;
143 x->x_value += x->x_biginc;
144 x->x_ticksleft--;
145 }
146 else
147 {
148 x->x_value = x->x_target;
149 while (n--) *out++ = x->x_value;
150 }
151 return (w+4);
152}
153
154static void line_float(t_line *x, t_float f)
155{
156 if (x->x_inletvalue <= 0)
157 {
158 x->x_target = x->x_value = ftofix(f);
159 x->x_ticksleft = x->x_retarget = 0;
160 }
161 else
162 {
163 x->x_target = ftofix(f);
164 x->x_retarget = 1;
165 x->x_inletwas = x->x_inletvalue;
166 x->x_inletvalue = 0;
167 }
168}
169
170static void line_stop(t_line *x)
171{
172 x->x_target = x->x_value;
173 x->x_ticksleft = x->x_retarget = 0;
174}
175
176static void line_dsp(t_line *x, t_signal **sp)
177{
178 dsp_add(line_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
179 x->x_1overn = ftofix(1)/sp[0]->s_n;
180 x->x_msectodsptick = idiv(sp[0]->s_sr, (1000 * sp[0]->s_n));
181}
182
183static void *line_new(void)
184{
185 t_line *x = (t_line *)pd_new(line_class);
186 outlet_new(&x->x_obj, gensym("signal"));
187 floatinlet_new(&x->x_obj, &x->x_inletvalue);
188 x->x_ticksleft = x->x_retarget = 0;
189 x->x_value = x->x_target = x->x_inletvalue = x->x_inletwas = 0;
190 return (x);
191}
192
193void line_tilde_setup(void)
194{
195 line_class = class_new(gensym("line~"), line_new, 0,
196 sizeof(t_line), 0, 0);
197 class_addfloat(line_class, (t_method)line_float);
198 class_addmethod(line_class, (t_method)line_dsp, gensym("dsp"), 0);
199 class_addmethod(line_class, (t_method)line_stop, gensym("stop"), 0);
200}
201
202