diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
commit | 513389b4c1bc8afe4b2dc9947c534bfeb105e3da (patch) | |
tree | 10e673b35651ac567fed2eda0c679c7ade64cbc6 /apps/plugins/pdbox/PDa/src/x_qlist.c | |
parent | 95fa7f6a2ef466444fbe3fe87efc6d5db6b77b36 (diff) | |
download | rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.tar.gz rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.zip |
Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin project of Wincent Balin. Stripped some non-sourcefiles and added a rockbox readme that needs a bit more info from Wincent. Is added to CATEGORIES and viewers, but not yet to SUBDIRS (ie doesn't build yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21044 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/x_qlist.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/src/x_qlist.c | 690 |
1 files changed, 690 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/x_qlist.c b/apps/plugins/pdbox/PDa/src/x_qlist.c new file mode 100644 index 0000000000..e2c0605087 --- /dev/null +++ b/apps/plugins/pdbox/PDa/src/x_qlist.c | |||
@@ -0,0 +1,690 @@ | |||
1 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
2 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
3 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
4 | |||
5 | #include "m_pd.h" | ||
6 | #include <string.h> | ||
7 | #ifdef UNIX | ||
8 | #include <unistd.h> | ||
9 | #endif | ||
10 | #ifdef MSW | ||
11 | #include <io.h> | ||
12 | #endif | ||
13 | |||
14 | typedef struct _qlist | ||
15 | { | ||
16 | t_object x_ob; | ||
17 | t_outlet *x_bangout; | ||
18 | void *x_binbuf; | ||
19 | int x_onset; /* playback position */ | ||
20 | t_clock *x_clock; | ||
21 | float x_tempo; | ||
22 | double x_whenclockset; | ||
23 | float x_clockdelay; | ||
24 | t_symbol *x_dir; | ||
25 | t_canvas *x_canvas; | ||
26 | int x_reentered; | ||
27 | } t_qlist; | ||
28 | |||
29 | static void qlist_tick(t_qlist *x); | ||
30 | |||
31 | static t_class *qlist_class; | ||
32 | |||
33 | static void *qlist_new( void) | ||
34 | { | ||
35 | t_symbol *name, *filename = 0; | ||
36 | t_qlist *x = (t_qlist *)pd_new(qlist_class); | ||
37 | x->x_binbuf = binbuf_new(); | ||
38 | x->x_clock = clock_new(x, (t_method)qlist_tick); | ||
39 | outlet_new(&x->x_ob, &s_list); | ||
40 | x->x_bangout = outlet_new(&x->x_ob, &s_bang); | ||
41 | x->x_onset = 0x7fffffff; | ||
42 | x->x_tempo = 1; | ||
43 | x->x_whenclockset = 0; | ||
44 | x->x_clockdelay = 0; | ||
45 | x->x_canvas = canvas_getcurrent(); | ||
46 | x->x_reentered = 0; | ||
47 | return (x); | ||
48 | } | ||
49 | |||
50 | static void qlist_rewind(t_qlist *x) | ||
51 | { | ||
52 | x->x_onset = 0; | ||
53 | if (x->x_clock) clock_unset(x->x_clock); | ||
54 | x->x_whenclockset = 0; | ||
55 | x->x_reentered = 1; | ||
56 | } | ||
57 | |||
58 | static void qlist_donext(t_qlist *x, int drop, int automatic) | ||
59 | { | ||
60 | t_pd *target = 0; | ||
61 | while (1) | ||
62 | { | ||
63 | int argc = binbuf_getnatom(x->x_binbuf), | ||
64 | count, onset = x->x_onset, onset2, wasreentered; | ||
65 | t_atom *argv = binbuf_getvec(x->x_binbuf); | ||
66 | t_atom *ap = argv + onset, *ap2; | ||
67 | if (onset >= argc) goto end; | ||
68 | while (ap->a_type == A_SEMI || ap->a_type == A_COMMA) | ||
69 | { | ||
70 | if (ap->a_type == A_SEMI) target = 0; | ||
71 | onset++, ap++; | ||
72 | if (onset >= argc) goto end; | ||
73 | } | ||
74 | |||
75 | if (!target && ap->a_type == A_FLOAT) | ||
76 | { | ||
77 | ap2 = ap + 1; | ||
78 | onset2 = onset + 1; | ||
79 | while (onset2 < argc && ap2->a_type == A_FLOAT) | ||
80 | onset2++, ap2++; | ||
81 | x->x_onset = onset2; | ||
82 | if (automatic) | ||
83 | { | ||
84 | clock_delay(x->x_clock, | ||
85 | x->x_clockdelay = ap->a_w.w_float * x->x_tempo); | ||
86 | x->x_whenclockset = clock_getsystime(); | ||
87 | } | ||
88 | else outlet_list(x->x_ob.ob_outlet, 0, onset2-onset, ap); | ||
89 | return; | ||
90 | } | ||
91 | ap2 = ap + 1; | ||
92 | onset2 = onset + 1; | ||
93 | while (onset2 < argc && | ||
94 | (ap2->a_type == A_FLOAT || ap2->a_type == A_SYMBOL)) | ||
95 | onset2++, ap2++; | ||
96 | x->x_onset = onset2; | ||
97 | count = onset2 - onset; | ||
98 | if (!target) | ||
99 | { | ||
100 | if (ap->a_type != A_SYMBOL) continue; | ||
101 | else if (!(target = ap->a_w.w_symbol->s_thing)) | ||
102 | { | ||
103 | error("qlist: %s: no such object", ap->a_w.w_symbol->s_name); | ||
104 | continue; | ||
105 | } | ||
106 | ap++; | ||
107 | onset++; | ||
108 | count--; | ||
109 | if (!count) | ||
110 | { | ||
111 | x->x_onset = onset2; | ||
112 | continue; | ||
113 | } | ||
114 | } | ||
115 | wasreentered = x->x_reentered; | ||
116 | x->x_reentered = 0; | ||
117 | if (!drop) | ||
118 | { | ||
119 | if (ap->a_type == A_FLOAT) | ||
120 | typedmess(target, &s_list, count, ap); | ||
121 | else if (ap->a_type == A_SYMBOL) | ||
122 | typedmess(target, ap->a_w.w_symbol, count-1, ap+1); | ||
123 | } | ||
124 | if (x->x_reentered) | ||
125 | return; | ||
126 | x->x_reentered = wasreentered; | ||
127 | } /* while (1); never falls through */ | ||
128 | |||
129 | end: | ||
130 | x->x_onset = 0x7fffffff; | ||
131 | outlet_bang(x->x_bangout); | ||
132 | x->x_whenclockset = 0; | ||
133 | } | ||
134 | |||
135 | static void qlist_next(t_qlist *x, t_floatarg drop) | ||
136 | { | ||
137 | qlist_donext(x, drop != 0, 0); | ||
138 | } | ||
139 | |||
140 | static void qlist_bang(t_qlist *x) | ||
141 | { | ||
142 | qlist_rewind(x); | ||
143 | qlist_donext(x, 0, 1); | ||
144 | } | ||
145 | |||
146 | static void qlist_tick(t_qlist *x) | ||
147 | { | ||
148 | x->x_whenclockset = 0; | ||
149 | qlist_donext(x, 0, 1); | ||
150 | } | ||
151 | |||
152 | static void qlist_add(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
153 | { | ||
154 | t_atom a; | ||
155 | SETSEMI(&a); | ||
156 | binbuf_add(x->x_binbuf, ac, av); | ||
157 | binbuf_add(x->x_binbuf, 1, &a); | ||
158 | } | ||
159 | |||
160 | static void qlist_add2(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
161 | { | ||
162 | binbuf_add(x->x_binbuf, ac, av); | ||
163 | } | ||
164 | |||
165 | static void qlist_clear(t_qlist *x) | ||
166 | { | ||
167 | qlist_rewind(x); | ||
168 | binbuf_clear(x->x_binbuf); | ||
169 | } | ||
170 | |||
171 | static void qlist_set(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
172 | { | ||
173 | qlist_clear(x); | ||
174 | qlist_add(x, s, ac, av); | ||
175 | } | ||
176 | |||
177 | static void qlist_read(t_qlist *x, t_symbol *filename, t_symbol *format) | ||
178 | { | ||
179 | int cr = 0; | ||
180 | if (!strcmp(format->s_name, "cr")) | ||
181 | cr = 1; | ||
182 | else if (*format->s_name) | ||
183 | error("qlist_read: unknown flag: %s", format->s_name); | ||
184 | |||
185 | if (binbuf_read_via_path(x->x_binbuf, filename->s_name, | ||
186 | canvas_getdir(x->x_canvas)->s_name, cr)) | ||
187 | error("%s: read failed", filename->s_name); | ||
188 | x->x_onset = 0x7fffffff; | ||
189 | x->x_reentered = 1; | ||
190 | } | ||
191 | |||
192 | static void qlist_write(t_qlist *x, t_symbol *filename, t_symbol *format) | ||
193 | { | ||
194 | int cr = 0; | ||
195 | char buf[MAXPDSTRING]; | ||
196 | canvas_makefilename(x->x_canvas, filename->s_name, | ||
197 | buf, MAXPDSTRING); | ||
198 | if (!strcmp(format->s_name, "cr")) | ||
199 | cr = 1; | ||
200 | else if (*format->s_name) | ||
201 | error("qlist_read: unknown flag: %s", format->s_name); | ||
202 | if (binbuf_write(x->x_binbuf, buf, "", cr)) | ||
203 | error("%s: write failed", filename->s_name); | ||
204 | } | ||
205 | |||
206 | static void qlist_print(t_qlist *x) | ||
207 | { | ||
208 | post("--------- textfile or qlist contents: -----------"); | ||
209 | binbuf_print(x->x_binbuf); | ||
210 | } | ||
211 | |||
212 | static void qlist_tempo(t_qlist *x, t_float f) | ||
213 | { | ||
214 | float newtempo; | ||
215 | if (f < 1e-20) f = 1e-20; | ||
216 | else if (f > 1e20) f = 1e20; | ||
217 | newtempo = 1./f; | ||
218 | if (x->x_whenclockset != 0) | ||
219 | { | ||
220 | float elapsed = clock_gettimesince(x->x_whenclockset); | ||
221 | float left = x->x_clockdelay - elapsed; | ||
222 | if (left < 0) left = 0; | ||
223 | left *= newtempo / x->x_tempo; | ||
224 | clock_delay(x->x_clock, left); | ||
225 | } | ||
226 | x->x_tempo = newtempo; | ||
227 | } | ||
228 | |||
229 | static void qlist_free(t_qlist *x) | ||
230 | { | ||
231 | binbuf_free(x->x_binbuf); | ||
232 | if (x->x_clock) clock_free(x->x_clock); | ||
233 | } | ||
234 | |||
235 | /* -------------------- textfile ------------------------------- */ | ||
236 | |||
237 | static t_class *textfile_class; | ||
238 | typedef t_qlist t_textfile; | ||
239 | |||
240 | static void *textfile_new( void) | ||
241 | { | ||
242 | t_symbol *name, *filename = 0; | ||
243 | t_textfile *x = (t_textfile *)pd_new(textfile_class); | ||
244 | x->x_binbuf = binbuf_new(); | ||
245 | outlet_new(&x->x_ob, &s_list); | ||
246 | x->x_bangout = outlet_new(&x->x_ob, &s_bang); | ||
247 | x->x_onset = 0x7fffffff; | ||
248 | x->x_reentered = 0; | ||
249 | x->x_tempo = 1; | ||
250 | x->x_whenclockset = 0; | ||
251 | x->x_clockdelay = 0; | ||
252 | x->x_clock = NULL; | ||
253 | x->x_canvas = canvas_getcurrent(); | ||
254 | return (x); | ||
255 | } | ||
256 | |||
257 | static void textfile_bang(t_textfile *x) | ||
258 | { | ||
259 | int argc = binbuf_getnatom(x->x_binbuf), | ||
260 | count, onset = x->x_onset, onset2; | ||
261 | t_atom *argv = binbuf_getvec(x->x_binbuf); | ||
262 | t_atom *ap = argv + onset, *ap2; | ||
263 | while (onset < argc && | ||
264 | (ap->a_type == A_SEMI || ap->a_type == A_COMMA)) | ||
265 | onset++, ap++; | ||
266 | onset2 = onset; | ||
267 | ap2 = ap; | ||
268 | while (onset2 < argc && | ||
269 | (ap2->a_type != A_SEMI && ap2->a_type != A_COMMA)) | ||
270 | onset2++, ap2++; | ||
271 | if (onset2 > onset) | ||
272 | { | ||
273 | x->x_onset = onset2; | ||
274 | if (ap->a_type == A_SYMBOL) | ||
275 | outlet_anything(x->x_ob.ob_outlet, ap->a_w.w_symbol, | ||
276 | onset2-onset-1, ap+1); | ||
277 | else outlet_list(x->x_ob.ob_outlet, 0, onset2-onset, ap); | ||
278 | } | ||
279 | else | ||
280 | { | ||
281 | x->x_onset = 0x7fffffff; | ||
282 | outlet_bang(x->x_bangout); | ||
283 | } | ||
284 | } | ||
285 | |||
286 | static void textfile_rewind(t_qlist *x) | ||
287 | { | ||
288 | x->x_onset = 0; | ||
289 | } | ||
290 | |||
291 | static void textfile_free(t_textfile *x) | ||
292 | { | ||
293 | binbuf_free(x->x_binbuf); | ||
294 | } | ||
295 | |||
296 | /* ---------------- global setup function -------------------- */ | ||
297 | |||
298 | void x_qlist_setup(void ) | ||
299 | { | ||
300 | qlist_class = class_new(gensym("qlist"), (t_newmethod)qlist_new, | ||
301 | (t_method)qlist_free, sizeof(t_qlist), 0, 0); | ||
302 | class_addmethod(qlist_class, (t_method)qlist_rewind, gensym("rewind"), 0); | ||
303 | class_addmethod(qlist_class, (t_method)qlist_next, | ||
304 | gensym("next"), A_DEFFLOAT, 0); | ||
305 | class_addmethod(qlist_class, (t_method)qlist_set, gensym("set"), | ||
306 | A_GIMME, 0); | ||
307 | class_addmethod(qlist_class, (t_method)qlist_clear, gensym("clear"), 0); | ||
308 | class_addmethod(qlist_class, (t_method)qlist_add, gensym("add"), | ||
309 | A_GIMME, 0); | ||
310 | class_addmethod(qlist_class, (t_method)qlist_add2, gensym("add2"), | ||
311 | A_GIMME, 0); | ||
312 | class_addmethod(qlist_class, (t_method)qlist_add, gensym("append"), | ||
313 | A_GIMME, 0); | ||
314 | class_addmethod(qlist_class, (t_method)qlist_read, gensym("read"), | ||
315 | A_SYMBOL, A_DEFSYM, 0); | ||
316 | class_addmethod(qlist_class, (t_method)qlist_write, gensym("write"), | ||
317 | A_SYMBOL, A_DEFSYM, 0); | ||
318 | class_addmethod(qlist_class, (t_method)qlist_print, gensym("print"), | ||
319 | A_DEFSYM, 0); | ||
320 | class_addmethod(qlist_class, (t_method)qlist_tempo, | ||
321 | gensym("tempo"), A_FLOAT, 0); | ||
322 | class_addbang(qlist_class, qlist_bang); | ||
323 | |||
324 | textfile_class = class_new(gensym("textfile"), (t_newmethod)textfile_new, | ||
325 | (t_method)textfile_free, sizeof(t_textfile), 0, 0); | ||
326 | class_addmethod(textfile_class, (t_method)textfile_rewind, gensym("rewind"), | ||
327 | 0); | ||
328 | class_addmethod(textfile_class, (t_method)qlist_set, gensym("set"), | ||
329 | A_GIMME, 0); | ||
330 | class_addmethod(textfile_class, (t_method)qlist_clear, gensym("clear"), 0); | ||
331 | class_addmethod(textfile_class, (t_method)qlist_add, gensym("add"), | ||
332 | A_GIMME, 0); | ||
333 | class_addmethod(textfile_class, (t_method)qlist_add2, gensym("add2"), | ||
334 | A_GIMME, 0); | ||
335 | class_addmethod(textfile_class, (t_method)qlist_add, gensym("append"), | ||
336 | A_GIMME, 0); | ||
337 | class_addmethod(textfile_class, (t_method)qlist_read, gensym("read"), | ||
338 | A_SYMBOL, A_DEFSYM, 0); | ||
339 | class_addmethod(textfile_class, (t_method)qlist_write, gensym("write"), | ||
340 | A_SYMBOL, A_DEFSYM, 0); | ||
341 | class_addmethod(textfile_class, (t_method)qlist_print, gensym("print"), | ||
342 | A_DEFSYM, 0); | ||
343 | class_addbang(textfile_class, textfile_bang); | ||
344 | } | ||
345 | |||
346 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
347 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
348 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
349 | |||
350 | #include "m_pd.h" | ||
351 | #include <string.h> | ||
352 | #ifdef UNIX | ||
353 | #include <unistd.h> | ||
354 | #endif | ||
355 | #ifdef MSW | ||
356 | #include <io.h> | ||
357 | #endif | ||
358 | |||
359 | typedef struct _qlist | ||
360 | { | ||
361 | t_object x_ob; | ||
362 | t_outlet *x_bangout; | ||
363 | void *x_binbuf; | ||
364 | int x_onset; /* playback position */ | ||
365 | t_clock *x_clock; | ||
366 | float x_tempo; | ||
367 | double x_whenclockset; | ||
368 | float x_clockdelay; | ||
369 | t_symbol *x_dir; | ||
370 | t_canvas *x_canvas; | ||
371 | int x_reentered; | ||
372 | } t_qlist; | ||
373 | |||
374 | static void qlist_tick(t_qlist *x); | ||
375 | |||
376 | static t_class *qlist_class; | ||
377 | |||
378 | static void *qlist_new( void) | ||
379 | { | ||
380 | t_symbol *name, *filename = 0; | ||
381 | t_qlist *x = (t_qlist *)pd_new(qlist_class); | ||
382 | x->x_binbuf = binbuf_new(); | ||
383 | x->x_clock = clock_new(x, (t_method)qlist_tick); | ||
384 | outlet_new(&x->x_ob, &s_list); | ||
385 | x->x_bangout = outlet_new(&x->x_ob, &s_bang); | ||
386 | x->x_onset = 0x7fffffff; | ||
387 | x->x_tempo = 1; | ||
388 | x->x_whenclockset = 0; | ||
389 | x->x_clockdelay = 0; | ||
390 | x->x_canvas = canvas_getcurrent(); | ||
391 | x->x_reentered = 0; | ||
392 | return (x); | ||
393 | } | ||
394 | |||
395 | static void qlist_rewind(t_qlist *x) | ||
396 | { | ||
397 | x->x_onset = 0; | ||
398 | if (x->x_clock) clock_unset(x->x_clock); | ||
399 | x->x_whenclockset = 0; | ||
400 | x->x_reentered = 1; | ||
401 | } | ||
402 | |||
403 | static void qlist_donext(t_qlist *x, int drop, int automatic) | ||
404 | { | ||
405 | t_pd *target = 0; | ||
406 | while (1) | ||
407 | { | ||
408 | int argc = binbuf_getnatom(x->x_binbuf), | ||
409 | count, onset = x->x_onset, onset2, wasreentered; | ||
410 | t_atom *argv = binbuf_getvec(x->x_binbuf); | ||
411 | t_atom *ap = argv + onset, *ap2; | ||
412 | if (onset >= argc) goto end; | ||
413 | while (ap->a_type == A_SEMI || ap->a_type == A_COMMA) | ||
414 | { | ||
415 | if (ap->a_type == A_SEMI) target = 0; | ||
416 | onset++, ap++; | ||
417 | if (onset >= argc) goto end; | ||
418 | } | ||
419 | |||
420 | if (!target && ap->a_type == A_FLOAT) | ||
421 | { | ||
422 | ap2 = ap + 1; | ||
423 | onset2 = onset + 1; | ||
424 | while (onset2 < argc && ap2->a_type == A_FLOAT) | ||
425 | onset2++, ap2++; | ||
426 | x->x_onset = onset2; | ||
427 | if (automatic) | ||
428 | { | ||
429 | clock_delay(x->x_clock, | ||
430 | x->x_clockdelay = ap->a_w.w_float * x->x_tempo); | ||
431 | x->x_whenclockset = clock_getsystime(); | ||
432 | } | ||
433 | else outlet_list(x->x_ob.ob_outlet, 0, onset2-onset, ap); | ||
434 | return; | ||
435 | } | ||
436 | ap2 = ap + 1; | ||
437 | onset2 = onset + 1; | ||
438 | while (onset2 < argc && | ||
439 | (ap2->a_type == A_FLOAT || ap2->a_type == A_SYMBOL)) | ||
440 | onset2++, ap2++; | ||
441 | x->x_onset = onset2; | ||
442 | count = onset2 - onset; | ||
443 | if (!target) | ||
444 | { | ||
445 | if (ap->a_type != A_SYMBOL) continue; | ||
446 | else if (!(target = ap->a_w.w_symbol->s_thing)) | ||
447 | { | ||
448 | error("qlist: %s: no such object", ap->a_w.w_symbol->s_name); | ||
449 | continue; | ||
450 | } | ||
451 | ap++; | ||
452 | onset++; | ||
453 | count--; | ||
454 | if (!count) | ||
455 | { | ||
456 | x->x_onset = onset2; | ||
457 | continue; | ||
458 | } | ||
459 | } | ||
460 | wasreentered = x->x_reentered; | ||
461 | x->x_reentered = 0; | ||
462 | if (!drop) | ||
463 | { | ||
464 | if (ap->a_type == A_FLOAT) | ||
465 | typedmess(target, &s_list, count, ap); | ||
466 | else if (ap->a_type == A_SYMBOL) | ||
467 | typedmess(target, ap->a_w.w_symbol, count-1, ap+1); | ||
468 | } | ||
469 | if (x->x_reentered) | ||
470 | return; | ||
471 | x->x_reentered = wasreentered; | ||
472 | } /* while (1); never falls through */ | ||
473 | |||
474 | end: | ||
475 | x->x_onset = 0x7fffffff; | ||
476 | outlet_bang(x->x_bangout); | ||
477 | x->x_whenclockset = 0; | ||
478 | } | ||
479 | |||
480 | static void qlist_next(t_qlist *x, t_floatarg drop) | ||
481 | { | ||
482 | qlist_donext(x, drop != 0, 0); | ||
483 | } | ||
484 | |||
485 | static void qlist_bang(t_qlist *x) | ||
486 | { | ||
487 | qlist_rewind(x); | ||
488 | qlist_donext(x, 0, 1); | ||
489 | } | ||
490 | |||
491 | static void qlist_tick(t_qlist *x) | ||
492 | { | ||
493 | x->x_whenclockset = 0; | ||
494 | qlist_donext(x, 0, 1); | ||
495 | } | ||
496 | |||
497 | static void qlist_add(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
498 | { | ||
499 | t_atom a; | ||
500 | SETSEMI(&a); | ||
501 | binbuf_add(x->x_binbuf, ac, av); | ||
502 | binbuf_add(x->x_binbuf, 1, &a); | ||
503 | } | ||
504 | |||
505 | static void qlist_add2(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
506 | { | ||
507 | binbuf_add(x->x_binbuf, ac, av); | ||
508 | } | ||
509 | |||
510 | static void qlist_clear(t_qlist *x) | ||
511 | { | ||
512 | qlist_rewind(x); | ||
513 | binbuf_clear(x->x_binbuf); | ||
514 | } | ||
515 | |||
516 | static void qlist_set(t_qlist *x, t_symbol *s, int ac, t_atom *av) | ||
517 | { | ||
518 | qlist_clear(x); | ||
519 | qlist_add(x, s, ac, av); | ||
520 | } | ||
521 | |||
522 | static void qlist_read(t_qlist *x, t_symbol *filename, t_symbol *format) | ||
523 | { | ||
524 | int cr = 0; | ||
525 | if (!strcmp(format->s_name, "cr")) | ||
526 | cr = 1; | ||
527 | else if (*format->s_name) | ||
528 | error("qlist_read: unknown flag: %s", format->s_name); | ||
529 | |||
530 | if (binbuf_read_via_path(x->x_binbuf, filename->s_name, | ||
531 | canvas_getdir(x->x_canvas)->s_name, cr)) | ||
532 | error("%s: read failed", filename->s_name); | ||
533 | x->x_onset = 0x7fffffff; | ||
534 | x->x_reentered = 1; | ||
535 | } | ||
536 | |||
537 | static void qlist_write(t_qlist *x, t_symbol *filename, t_symbol *format) | ||
538 | { | ||
539 | int cr = 0; | ||
540 | char buf[MAXPDSTRING]; | ||
541 | canvas_makefilename(x->x_canvas, filename->s_name, | ||
542 | buf, MAXPDSTRING); | ||
543 | if (!strcmp(format->s_name, "cr")) | ||
544 | cr = 1; | ||
545 | else if (*format->s_name) | ||
546 | error("qlist_read: unknown flag: %s", format->s_name); | ||
547 | if (binbuf_write(x->x_binbuf, buf, "", cr)) | ||
548 | error("%s: write failed", filename->s_name); | ||
549 | } | ||
550 | |||
551 | static void qlist_print(t_qlist *x) | ||
552 | { | ||
553 | post("--------- textfile or qlist contents: -----------"); | ||
554 | binbuf_print(x->x_binbuf); | ||
555 | } | ||
556 | |||
557 | static void qlist_tempo(t_qlist *x, t_float f) | ||
558 | { | ||
559 | float newtempo; | ||
560 | if (f < 1e-20) f = 1e-20; | ||
561 | else if (f > 1e20) f = 1e20; | ||
562 | newtempo = 1./f; | ||
563 | if (x->x_whenclockset != 0) | ||
564 | { | ||
565 | float elapsed = clock_gettimesince(x->x_whenclockset); | ||
566 | float left = x->x_clockdelay - elapsed; | ||
567 | if (left < 0) left = 0; | ||
568 | left *= newtempo / x->x_tempo; | ||
569 | clock_delay(x->x_clock, left); | ||
570 | } | ||
571 | x->x_tempo = newtempo; | ||
572 | } | ||
573 | |||
574 | static void qlist_free(t_qlist *x) | ||
575 | { | ||
576 | binbuf_free(x->x_binbuf); | ||
577 | if (x->x_clock) clock_free(x->x_clock); | ||
578 | } | ||
579 | |||
580 | /* -------------------- textfile ------------------------------- */ | ||
581 | |||
582 | static t_class *textfile_class; | ||
583 | typedef t_qlist t_textfile; | ||
584 | |||
585 | static void *textfile_new( void) | ||
586 | { | ||
587 | t_symbol *name, *filename = 0; | ||
588 | t_textfile *x = (t_textfile *)pd_new(textfile_class); | ||
589 | x->x_binbuf = binbuf_new(); | ||
590 | outlet_new(&x->x_ob, &s_list); | ||
591 | x->x_bangout = outlet_new(&x->x_ob, &s_bang); | ||
592 | x->x_onset = 0x7fffffff; | ||
593 | x->x_reentered = 0; | ||
594 | x->x_tempo = 1; | ||
595 | x->x_whenclockset = 0; | ||
596 | x->x_clockdelay = 0; | ||
597 | x->x_clock = NULL; | ||
598 | x->x_canvas = canvas_getcurrent(); | ||
599 | return (x); | ||
600 | } | ||
601 | |||
602 | static void textfile_bang(t_textfile *x) | ||
603 | { | ||
604 | int argc = binbuf_getnatom(x->x_binbuf), | ||
605 | count, onset = x->x_onset, onset2; | ||
606 | t_atom *argv = binbuf_getvec(x->x_binbuf); | ||
607 | t_atom *ap = argv + onset, *ap2; | ||
608 | while (onset < argc && | ||
609 | (ap->a_type == A_SEMI || ap->a_type == A_COMMA)) | ||
610 | onset++, ap++; | ||
611 | onset2 = onset; | ||
612 | ap2 = ap; | ||
613 | while (onset2 < argc && | ||
614 | (ap2->a_type != A_SEMI && ap2->a_type != A_COMMA)) | ||
615 | onset2++, ap2++; | ||
616 | if (onset2 > onset) | ||
617 | { | ||
618 | x->x_onset = onset2; | ||
619 | if (ap->a_type == A_SYMBOL) | ||
620 | outlet_anything(x->x_ob.ob_outlet, ap->a_w.w_symbol, | ||
621 | onset2-onset-1, ap+1); | ||
622 | else outlet_list(x->x_ob.ob_outlet, 0, onset2-onset, ap); | ||
623 | } | ||
624 | else | ||
625 | { | ||
626 | x->x_onset = 0x7fffffff; | ||
627 | outlet_bang(x->x_bangout); | ||
628 | } | ||
629 | } | ||
630 | |||
631 | static void textfile_rewind(t_qlist *x) | ||
632 | { | ||
633 | x->x_onset = 0; | ||
634 | } | ||
635 | |||
636 | static void textfile_free(t_textfile *x) | ||
637 | { | ||
638 | binbuf_free(x->x_binbuf); | ||
639 | } | ||
640 | |||
641 | /* ---------------- global setup function -------------------- */ | ||
642 | |||
643 | void x_qlist_setup(void ) | ||
644 | { | ||
645 | qlist_class = class_new(gensym("qlist"), (t_newmethod)qlist_new, | ||
646 | (t_method)qlist_free, sizeof(t_qlist), 0, 0); | ||
647 | class_addmethod(qlist_class, (t_method)qlist_rewind, gensym("rewind"), 0); | ||
648 | class_addmethod(qlist_class, (t_method)qlist_next, | ||
649 | gensym("next"), A_DEFFLOAT, 0); | ||
650 | class_addmethod(qlist_class, (t_method)qlist_set, gensym("set"), | ||
651 | A_GIMME, 0); | ||
652 | class_addmethod(qlist_class, (t_method)qlist_clear, gensym("clear"), 0); | ||
653 | class_addmethod(qlist_class, (t_method)qlist_add, gensym("add"), | ||
654 | A_GIMME, 0); | ||
655 | class_addmethod(qlist_class, (t_method)qlist_add2, gensym("add2"), | ||
656 | A_GIMME, 0); | ||
657 | class_addmethod(qlist_class, (t_method)qlist_add, gensym("append"), | ||
658 | A_GIMME, 0); | ||
659 | class_addmethod(qlist_class, (t_method)qlist_read, gensym("read"), | ||
660 | A_SYMBOL, A_DEFSYM, 0); | ||
661 | class_addmethod(qlist_class, (t_method)qlist_write, gensym("write"), | ||
662 | A_SYMBOL, A_DEFSYM, 0); | ||
663 | class_addmethod(qlist_class, (t_method)qlist_print, gensym("print"), | ||
664 | A_DEFSYM, 0); | ||
665 | class_addmethod(qlist_class, (t_method)qlist_tempo, | ||
666 | gensym("tempo"), A_FLOAT, 0); | ||
667 | class_addbang(qlist_class, qlist_bang); | ||
668 | |||
669 | textfile_class = class_new(gensym("textfile"), (t_newmethod)textfile_new, | ||
670 | (t_method)textfile_free, sizeof(t_textfile), 0, 0); | ||
671 | class_addmethod(textfile_class, (t_method)textfile_rewind, gensym("rewind"), | ||
672 | 0); | ||
673 | class_addmethod(textfile_class, (t_method)qlist_set, gensym("set"), | ||
674 | A_GIMME, 0); | ||
675 | class_addmethod(textfile_class, (t_method)qlist_clear, gensym("clear"), 0); | ||
676 | class_addmethod(textfile_class, (t_method)qlist_add, gensym("add"), | ||
677 | A_GIMME, 0); | ||
678 | class_addmethod(textfile_class, (t_method)qlist_add2, gensym("add2"), | ||
679 | A_GIMME, 0); | ||
680 | class_addmethod(textfile_class, (t_method)qlist_add, gensym("append"), | ||
681 | A_GIMME, 0); | ||
682 | class_addmethod(textfile_class, (t_method)qlist_read, gensym("read"), | ||
683 | A_SYMBOL, A_DEFSYM, 0); | ||
684 | class_addmethod(textfile_class, (t_method)qlist_write, gensym("write"), | ||
685 | A_SYMBOL, A_DEFSYM, 0); | ||
686 | class_addmethod(textfile_class, (t_method)qlist_print, gensym("print"), | ||
687 | A_DEFSYM, 0); | ||
688 | class_addbang(textfile_class, textfile_bang); | ||
689 | } | ||
690 | |||