Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / intern / tabwrite~.c
blob47a49cd8335d19a69471aa7cf1803ed035a87053
2 #include <m_pd.h>
3 #include <m_fixed.h>
5 static t_class *tabwrite_tilde_class;
7 typedef struct _tabwrite_tilde
9 t_object x_obj;
10 int x_phase;
11 int x_nsampsintab;
12 t_sample *x_vec;
13 t_symbol *x_arrayname;
14 t_clock *x_clock;
15 float x_f;
16 } t_tabwrite_tilde;
18 static void tabwrite_tilde_tick(t_tabwrite_tilde *x);
20 static void *tabwrite_tilde_new(t_symbol *s)
22 t_tabwrite_tilde *x = (t_tabwrite_tilde *)pd_new(tabwrite_tilde_class);
23 x->x_clock = clock_new(x, (t_method)tabwrite_tilde_tick);
24 x->x_phase = 0x7fffffff;
25 x->x_arrayname = s;
26 x->x_f = 0;
27 return (x);
30 static t_int *tabwrite_tilde_perform(t_int *w)
32 t_tabwrite_tilde *x = (t_tabwrite_tilde *)(w[1]);
33 t_sample *in = (t_sample *)(w[2]);
34 int n = (int)(w[3]), phase = x->x_phase, endphase = x->x_nsampsintab;
35 if (!x->x_vec) goto bad;
37 if (endphase > phase)
39 int nxfer = endphase - phase;
40 t_sample *fp = x->x_vec + phase;
41 if (nxfer > n) nxfer = n;
42 phase += nxfer;
43 while (nxfer--)
45 t_sample f = *in++;
46 *fp++ = f;
48 if (phase >= endphase)
50 clock_delay(x->x_clock, 0);
51 phase = 0x7fffffff;
53 x->x_phase = phase;
55 bad:
56 return (w+4);
59 void tabwrite_tilde_set(t_tabwrite_tilde *x, t_symbol *s)
61 t_garray *a;
63 x->x_arrayname = s;
64 if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
66 if (*s->s_name) pd_error(x, "tabwrite~: %s: no such array",
67 x->x_arrayname->s_name);
68 x->x_vec = 0;
70 else if (!garray_getfloatarray(a, &x->x_nsampsintab, &x->x_vec))
72 error("%s: bad template for tabwrite~", x->x_arrayname->s_name);
73 x->x_vec = 0;
75 else garray_usedindsp(a);
78 static void tabwrite_tilde_dsp(t_tabwrite_tilde *x, t_signal **sp)
80 tabwrite_tilde_set(x, x->x_arrayname);
81 dsp_add(tabwrite_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
84 static void tabwrite_tilde_bang(t_tabwrite_tilde *x)
86 x->x_phase = 0;
89 static void tabwrite_tilde_float(t_tabwrite_tilde *x,t_float n)
91 if (n < x->x_nsampsintab)
92 x->x_phase = n;
93 else
94 x->x_phase = 0;
97 static void tabwrite_tilde_stop(t_tabwrite_tilde *x)
99 if (x->x_phase != 0x7fffffff)
101 tabwrite_tilde_tick(x);
102 x->x_phase = 0x7fffffff;
106 static void tabwrite_tilde_tick(t_tabwrite_tilde *x)
108 t_garray *a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class);
109 if (!a) bug("tabwrite_tilde_tick");
110 else garray_redraw(a);
113 static void tabwrite_tilde_free(t_tabwrite_tilde *x)
115 clock_free(x->x_clock);
118 void tabwrite_tilde_setup(void)
120 tabwrite_tilde_class = class_new(gensym("tabwrite~"),
121 (t_newmethod)tabwrite_tilde_new, (t_method)tabwrite_tilde_free,
122 sizeof(t_tabwrite_tilde), 0, A_DEFSYM, 0);
123 CLASS_MAINSIGNALIN(tabwrite_tilde_class, t_tabwrite_tilde, x_f);
124 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_dsp,
125 gensym("dsp"), 0);
126 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_set,
127 gensym("set"), A_SYMBOL, 0);
128 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_stop,
129 gensym("stop"), 0);
130 class_addbang(tabwrite_tilde_class, tabwrite_tilde_bang);
131 class_addfloat(tabwrite_tilde_class, tabwrite_tilde_float);
134 #include <m_pd.h>
135 #include <m_fixed.h>
137 static t_class *tabwrite_tilde_class;
139 typedef struct _tabwrite_tilde
141 t_object x_obj;
142 int x_phase;
143 int x_nsampsintab;
144 t_sample *x_vec;
145 t_symbol *x_arrayname;
146 t_clock *x_clock;
147 float x_f;
148 } t_tabwrite_tilde;
150 static void tabwrite_tilde_tick(t_tabwrite_tilde *x);
152 static void *tabwrite_tilde_new(t_symbol *s)
154 t_tabwrite_tilde *x = (t_tabwrite_tilde *)pd_new(tabwrite_tilde_class);
155 x->x_clock = clock_new(x, (t_method)tabwrite_tilde_tick);
156 x->x_phase = 0x7fffffff;
157 x->x_arrayname = s;
158 x->x_f = 0;
159 return (x);
162 static t_int *tabwrite_tilde_perform(t_int *w)
164 t_tabwrite_tilde *x = (t_tabwrite_tilde *)(w[1]);
165 t_sample *in = (t_sample *)(w[2]);
166 int n = (int)(w[3]), phase = x->x_phase, endphase = x->x_nsampsintab;
167 if (!x->x_vec) goto bad;
169 if (endphase > phase)
171 int nxfer = endphase - phase;
172 t_sample *fp = x->x_vec + phase;
173 if (nxfer > n) nxfer = n;
174 phase += nxfer;
175 while (nxfer--)
177 t_sample f = *in++;
178 *fp++ = f;
180 if (phase >= endphase)
182 clock_delay(x->x_clock, 0);
183 phase = 0x7fffffff;
185 x->x_phase = phase;
187 bad:
188 return (w+4);
191 void tabwrite_tilde_set(t_tabwrite_tilde *x, t_symbol *s)
193 t_garray *a;
195 x->x_arrayname = s;
196 if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
198 if (*s->s_name) pd_error(x, "tabwrite~: %s: no such array",
199 x->x_arrayname->s_name);
200 x->x_vec = 0;
202 else if (!garray_getfloatarray(a, &x->x_nsampsintab, &x->x_vec))
204 error("%s: bad template for tabwrite~", x->x_arrayname->s_name);
205 x->x_vec = 0;
207 else garray_usedindsp(a);
210 static void tabwrite_tilde_dsp(t_tabwrite_tilde *x, t_signal **sp)
212 tabwrite_tilde_set(x, x->x_arrayname);
213 dsp_add(tabwrite_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
216 static void tabwrite_tilde_bang(t_tabwrite_tilde *x)
218 x->x_phase = 0;
221 static void tabwrite_tilde_float(t_tabwrite_tilde *x,t_float n)
223 if (n < x->x_nsampsintab)
224 x->x_phase = n;
225 else
226 x->x_phase = 0;
229 static void tabwrite_tilde_stop(t_tabwrite_tilde *x)
231 if (x->x_phase != 0x7fffffff)
233 tabwrite_tilde_tick(x);
234 x->x_phase = 0x7fffffff;
238 static void tabwrite_tilde_tick(t_tabwrite_tilde *x)
240 t_garray *a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class);
241 if (!a) bug("tabwrite_tilde_tick");
242 else garray_redraw(a);
245 static void tabwrite_tilde_free(t_tabwrite_tilde *x)
247 clock_free(x->x_clock);
250 void tabwrite_tilde_setup(void)
252 tabwrite_tilde_class = class_new(gensym("tabwrite~"),
253 (t_newmethod)tabwrite_tilde_new, (t_method)tabwrite_tilde_free,
254 sizeof(t_tabwrite_tilde), 0, A_DEFSYM, 0);
255 CLASS_MAINSIGNALIN(tabwrite_tilde_class, t_tabwrite_tilde, x_f);
256 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_dsp,
257 gensym("dsp"), 0);
258 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_set,
259 gensym("set"), A_SYMBOL, 0);
260 class_addmethod(tabwrite_tilde_class, (t_method)tabwrite_tilde_stop,
261 gensym("stop"), 0);
262 class_addbang(tabwrite_tilde_class, tabwrite_tilde_bang);
263 class_addfloat(tabwrite_tilde_class, tabwrite_tilde_float);