pd~.c 21.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
  pd~.c - embed a Pd process within Pd or Max.

  Copyright 2008 Miller Puckette
  BSD license; see README.txt in this distribution for details.
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef NT
#pragma warning (disable: 4305 4244)
#endif
Miller Puckette's avatar
Miller Puckette committed
21

22
23
24
25
26
27
28
29
30
#ifdef MSP
#include "ext.h"
#include "z_dsp.h"
#include "math.h"
#include "ext_support.h"
#include "ext_proto.h"
#include "ext_obex.h"

typedef double t_floatarg;
31
32
#define w_symbol w_sym
#define A_SYMBOL A_SYM
33
34
#define getbytes t_getbytes
#define freebytes t_freebytes
Miller Puckette's avatar
Miller Puckette committed
35
36
37
38
#define ERROR error(
void *pd_tilde_class;
#define MAXPDSTRING 4096
#define DEFDACBLKSIZE 64
39
40
41
42
43
44
45
#endif /* MSP */

#ifdef PD
#include "m_pd.h"
#include "s_stuff.h"
static t_class *pd_tilde_class;
char *class_gethelpdir(t_class *c);
Miller Puckette's avatar
Miller Puckette committed
46
47
#define ERROR pd_error(x, 

48
49
50
51
52
53
54
55
56
57
58
59
#endif

#ifdef __linux__
#ifdef __x86_64__
static char pd_tilde_dllextent[] = ".l_ia64",
    pd_tilde_dllextent2[] = ".pd_linux";
#else
static char pd_tilde_dllextent[] = ".l_i386",
    pd_tilde_dllextent2[] = ".pd_linux";
#endif
#endif
#ifdef __APPLE__
Miller Puckette's avatar
Miller Puckette committed
60
static char pd_tilde_dllextent[] = ".d_fat",
61
62
63
64
65
66
67
68
69
70
71
72
    pd_tilde_dllextent2[] = ".pd_darwin";
#endif

/* ------------------------ pd_tilde~ ----------------------------- */

#define MSGBUFSIZE 65536

typedef struct _pd_tilde
{
#ifdef PD
    t_object x_obj;
    t_clock *x_clock;
Miller Puckette's avatar
Miller Puckette committed
73
    t_outlet *x_outlet1;        /* for messages back from subproc */
74
    t_canvas *x_canvas;
75
76
77
#endif /* PD */
#ifdef MSP
    t_pxobject x_obj;
Miller Puckette's avatar
Miller Puckette committed
78
    void *x_outlet1;
79
80
81
82
83
84
85
86
87
88
    void *x_clock;
#endif /* MSP */
    FILE *x_infd;
    FILE *x_outfd;
    char *x_msgbuf;
    int x_msgbufsize;
    int x_infill;
    int x_childpid;
    int x_ninsig;
    int x_noutsig;
89
90
91
92
    int x_fifo;
    float x_sr;
    t_symbol *x_pddir;
    t_symbol *x_schedlibdir;
93
94
95
96
97
98
99
100
101
102
103
104
    t_sample **x_insig;
    t_sample **x_outsig;
} t_pd_tilde;

#ifdef MSP
static void *pd_tilde_new(t_symbol *s, long ac, t_atom *av);
static void pd_tilde_tick(t_pd_tilde *x);
static t_int *pd_tilde_perform(t_int *w);
static void pd_tilde_dsp(t_pd_tilde *x, t_signal **sp);
void pd_tilde_assist(t_pd_tilde *x, void *b, long m, long a, char *s);
static void pd_tilde_free(t_pd_tilde *x);
void pd_tilde_setup(void);
Miller Puckette's avatar
Miller Puckette committed
105
int main();
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
void pd_tilde_minvel_set(t_pd_tilde *x, void *attr, long ac, t_atom *av);
char *strcpy(char *s1, const char *s2);
#endif

static void pd_tilde_tick(t_pd_tilde *x);
static void pd_tilde_close(t_pd_tilde *x)
{
    if (x->x_outfd)
        fclose(x->x_outfd);
    if (x->x_infd)
        fclose(x->x_infd);
    if (x->x_childpid > 0)
        waitpid(x->x_childpid, 0, 0);
    if (x->x_msgbuf)
        free(x->x_msgbuf);
    x->x_infd = x->x_outfd = 0;
    x->x_childpid = -1;
    x->x_msgbuf = 0;
    x->x_msgbufsize = 0;
}

static void pd_tilde_readmessages(t_pd_tilde *x)
{
    int gotsomething = 0, setclock = 0, wasempty = (x->x_infill == 0);
    FILE *infd = x->x_infd;
    while (1)
    {
        int c = getc(infd);
        if (c == EOF)
        {
Miller Puckette's avatar
Miller Puckette committed
136
            ERROR "pd~: %s", strerror(errno));
137
138
139
140
141
142
143
144
            pd_tilde_close(x);
            break;
        }
        if (x->x_infill >= x->x_msgbufsize)
        {
            char *z = realloc(x->x_msgbuf, x->x_msgbufsize+MSGBUFSIZE);
            if (!z)
            {
Miller Puckette's avatar
Miller Puckette committed
145
                ERROR "pd~: failed to grow input buffer");
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
                pd_tilde_close(x);
                break;
            }
            x->x_msgbuf = z;
            x->x_msgbufsize += MSGBUFSIZE;
        }
        x->x_msgbuf[x->x_infill++] = c;
        if (c == ';')
        {
            if (!gotsomething)
                break;
            gotsomething = 0;
        }
        else if (!isspace(c))
            gotsomething = setclock = 1;
    }
    if (setclock)
        clock_delay(x->x_clock, 0);
    else if (wasempty)
        x->x_infill = 0;
}

static void pd_tilde_donew(t_pd_tilde *x, char *pddir, char *schedlibdir,
169
170
    char *patchdir, char *pdargs, int ninsig, int noutsig, int fifo,
    float samplerate)
171
172
173
174
175
176
177
178
179
{
    int i, pid, pipe1[2], pipe2[2];
    char cmdbuf[MAXPDSTRING], pdexecbuf[MAXPDSTRING], schedbuf[MAXPDSTRING];
    struct stat statbuf;
    x->x_infd = x->x_outfd = 0;
    x->x_childpid = -1;
    snprintf(pdexecbuf, MAXPDSTRING, "%s/bin/pd", pddir);
    if (stat(pdexecbuf, &statbuf) < 0)
    {
Miller Puckette's avatar
Miller Puckette committed
180
        snprintf(pdexecbuf, MAXPDSTRING, "%s/../../../bin/pd", pddir);
Miller Puckette's avatar
Miller Puckette committed
181
182
        if (stat(pdexecbuf, &statbuf) < 0)
        {
Miller Puckette's avatar
Miller Puckette committed
183
184
185
186
187
188
            snprintf(pdexecbuf, MAXPDSTRING, "%s/pd", pddir);
            if (stat(pdexecbuf, &statbuf) < 0)
            {
                ERROR "pd~: can't stat %s", pdexecbuf);
                goto fail1;
            }
Miller Puckette's avatar
Miller Puckette committed
189
        }
190
191
192
193
194
195
196
197
198
    }
    snprintf(schedbuf, MAXPDSTRING, "%s/pdsched%s", schedlibdir, 
        pd_tilde_dllextent);
    if (stat(schedbuf, &statbuf) < 0)
    {
        snprintf(schedbuf, MAXPDSTRING, "%s/pdsched%s", schedlibdir, 
            pd_tilde_dllextent2);
        if (stat(schedbuf, &statbuf) < 0)
        {
Miller Puckette's avatar
Miller Puckette committed
199
            ERROR "pd~: can't stat %s", schedbuf);
200
            goto fail1;
Miller Puckette's avatar
Miller Puckette committed
201
        }       
202
    }
Miller Puckette's avatar
Miller Puckette committed
203
    snprintf(cmdbuf, MAXPDSTRING,
204
205
"%s -schedlib %s/pdsched -path %s -inchannels %d -outchannels %d -r %g %s\n",
        pdexecbuf, schedlibdir, patchdir, ninsig, noutsig, samplerate, pdargs);
206
#if 0
Miller Puckette's avatar
Miller Puckette committed
207
#ifdef PD
208
    fprintf(stderr, "%s", cmdbuf);
Miller Puckette's avatar
Miller Puckette committed
209
#endif
Miller Puckette's avatar
Miller Puckette committed
210
    post("cmd: %s", cmdbuf);
211
#endif
212
213
    if (pipe(pipe1) < 0)   
    {
Miller Puckette's avatar
Miller Puckette committed
214
        ERROR "pd~: can't create pipe");
215
216
217
218
        goto fail1;
    }
    if (pipe(pipe2) < 0)   
    {
Miller Puckette's avatar
Miller Puckette committed
219
        ERROR "pd~: can't create pipe");
220
221
222
223
        goto fail2;
    }
    if ((pid = fork()) < 0)
    {
Miller Puckette's avatar
Miller Puckette committed
224
        ERROR "pd~: can't fork");
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        goto fail3;
    }
    else if (pid == 0)
    {
        /* child process */
        if (pipe2[1] == 0)
        {
            dup2(pipe2[1], 20);
            close(pipe2[1]);
            pipe2[1] = 20;
        }
        dup2(pipe1[0], 0);
        dup2(pipe2[1], 1);
        if (pipe1[0] >= 2)
            close(pipe1[0]);
        if (pipe1[1] >= 2)
            close(pipe1[1]);
        if (pipe2[0] >= 2)
            close(pipe2[0]);
        if (pipe2[1] >= 2)
            close(pipe2[1]);
        execl("/bin/sh", "sh", "-c", cmdbuf, (char*)0);
        _exit(1);
    }
        /* OK, we're parent */
    close(pipe1[0]);
    close(pipe2[1]);
    x->x_outfd = fdopen(pipe1[1], "w");
    x->x_infd = fdopen(pipe2[0], "r");
    x->x_childpid = pid;
    for (i = 0; i < fifo; i++)
        fprintf(x->x_outfd, "%s", ";\n0;\n");
    fflush(x->x_outfd);
    if (!(x->x_msgbuf = calloc(MSGBUFSIZE, 1)))
    {
Miller Puckette's avatar
Miller Puckette committed
260
        ERROR "pd~: can't allocate message buffer");
261
262
263
264
        goto fail3;
    }
    x->x_msgbufsize = MSGBUFSIZE;
    x->x_infill = 0;
Miller Puckette's avatar
Miller Puckette committed
265
    /* fprintf(stderr, "read...\n"); */
266
    pd_tilde_readmessages(x);
Miller Puckette's avatar
Miller Puckette committed
267
    /* fprintf(stderr, "... done.\n"); */
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    return;
fail3:
    close(pipe2[0]);
    close(pipe2[1]);
    if (x->x_childpid > 0)
        waitpid(x->x_childpid, 0, 0);
fail2:
    close(pipe1[0]);
    close(pipe1[1]);
fail1:
    x->x_infd = x->x_outfd = 0;
    x->x_childpid = -1;
    return;
}

static t_int *pd_tilde_perform(t_int *w)
{
    t_pd_tilde *x = (t_pd_tilde *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
286
    int n = (int)(w[2]), i, j, numbuffill = 0, c;
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    char numbuf[80];
    FILE *infd = x->x_infd;
    if (!infd)
        goto zeroit;
    fprintf(x->x_outfd, ";\n");
    for (i = 0; i < x->x_ninsig; i++)
    {
        t_sample *fp = x->x_insig[i];
        for (j = 0; j < n; j++)
            fprintf(x->x_outfd, "%g\n", *fp++);
        for (; j < DEFDACBLKSIZE; j++)
            fprintf(x->x_outfd, "0\n");
    }
    fprintf(x->x_outfd, ";\n");
    fflush(x->x_outfd);
    i = j = 0;
    while (1)
    {
        while (1)
        {
            c = getc(infd);
            if (c == EOF)
            {
                if (errno)
Miller Puckette's avatar
Miller Puckette committed
311
312
                    ERROR "pd~: %s", strerror(errno));
                else ERROR "pd~: subprocess exited");
313
314
315
316
317
318
319
320
321
322
323
324
325
                pd_tilde_close(x);
                goto zeroit;
            }
            else if (!isspace(c) && c != ';')
            {
                if (numbuffill < (80-1))
                    numbuf[numbuffill++] = c;
            }
            else
            {
                t_sample z;
                if (numbuffill)
                {
Miller Puckette's avatar
Miller Puckette committed
326
                    numbuf[numbuffill] = 0;
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
                    if (sscanf(numbuf, "%f", &z) < 1)
                        continue;
                    if (i < x->x_noutsig)
                        x->x_outsig[i][j] = z;
                    if (++j >= DEFDACBLKSIZE)
                        j = 0, i++;
                }
                numbuffill = 0;
                break;
            }
        }
        /* message terminated */
        if (c == ';')
            break;
    }
    for (; i < x->x_noutsig; i++, j = 0)
    {
        for (; j < DEFDACBLKSIZE; j++)
            x->x_outsig[i][j] = 0;
    }
    pd_tilde_readmessages(x);
    return (w+3);
zeroit:
    for (i = 0; i < x->x_noutsig; i++)
    {
        for (j = 0; j < DEFDACBLKSIZE; j++)
            x->x_outsig[i][j] = 0;
    }
    return (w+3);
}

static void pd_tilde_dsp(t_pd_tilde *x, t_signal **sp)
{
    int i, n = (x->x_ninsig || x->x_noutsig ? sp[0]->s_n : 1);
    t_sample **g;
        
    for (i = 0, g = x->x_insig; i < x->x_ninsig; i++, g++)
        *g = (*(sp++))->s_vec;
    
    for (i = 0, g = x->x_outsig; i < x->x_noutsig; i++, g++)
        *g = (*(sp++))->s_vec;
    
    dsp_add(pd_tilde_perform, 2, x, n);
}

372
373
374
static void pd_tilde_pdtilde(t_pd_tilde *x, t_symbol *s,
    int argc, t_atom *argv)
{
Miller Puckette's avatar
Miller Puckette committed
375
376
    t_symbol *sel = ((argc > 0 && argv->a_type == A_SYMBOL) ?
        argv->a_w.w_symbol : gensym("?"));
377
    char *patchdir;
378
379
    if (sel == gensym("start"))
    {
Miller Puckette's avatar
Miller Puckette committed
380
        char pdargstring[MAXPDSTRING];
381
382
        if (x->x_infd)
            pd_tilde_close(x);
Miller Puckette's avatar
Miller Puckette committed
383
384
385
386
387
388
389
390
391
392
        pdargstring[0] = 0;
        argc--; argv++;
#ifdef PD
        while (argc--)
        {
            atom_string(argv++, pdargstring + strlen(pdargstring), 
                MAXPDSTRING - strlen(pdargstring));
            if (strlen(pdargstring) < MAXPDSTRING-1)
                strcat(pdargstring, " ");
        }
393
        patchdir = canvas_getdir(x->x_canvas)->s_name;
Miller Puckette's avatar
Miller Puckette committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#endif
#ifdef MAX
        while (argc--)
        {
            if (argv->a_type == A_SYM)
                strncat(pdargstring, argv->a_w.w_sym->s_name,
                    MAXPDSTRING - strlen(pdargstring)-3);
            else if (argv->a_type == A_LONG)
                snprintf(pdargstring+strlen(pdargstring),
                    MAXPDSTRING - strlen(pdargstring)-3, "%ld",
                        argv->a_w.w_long);
            else if (argv->a_type == A_FLOAT)
                snprintf(pdargstring+strlen(pdargstring),
                    MAXPDSTRING - strlen(pdargstring)-3, "%f",
                        argv->a_w.w_float);
            strcat(pdargstring, " ");
            argv++;
        }
412
        patchdir = ".";
Miller Puckette's avatar
Miller Puckette committed
413
414
#endif
        pd_tilde_donew(x, x->x_pddir->s_name, x->x_schedlibdir->s_name,
415
416
            patchdir, pdargstring, x->x_ninsig, x->x_noutsig, x->x_fifo,
                x->x_sr);
417
    }
Miller Puckette's avatar
Miller Puckette committed
418
    else if (sel == gensym("stop"))
419
420
421
422
423
424
425
    {
        if (x->x_infd)
            pd_tilde_close(x);
    }
    else ERROR "pd~: unknown control message: %s", sel->s_name);
}

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
static void pd_tilde_free(t_pd_tilde *x)
{
#ifdef MSP
    dsp_free((t_pxobject *)x);
#endif
    pd_tilde_close(x);
    clock_free(x->x_clock);
}

/* -------------------------- Pd glue ------------------------- */
#ifdef PD

static void pd_tilde_tick(t_pd_tilde *x)
{
    int messstart = 0, i, n;
    t_atom *vec;
442
443
444
445
    t_binbuf *b;
    if (!x->x_msgbuf)
        return;
    b = binbuf_new();
446
447
448
449
450
451
452
453
    binbuf_text(b, x->x_msgbuf, x->x_infill);
    /* binbuf_print(b); */
    n = binbuf_getnatom(b);
    vec = binbuf_getvec(b);
    for (i = 0; i < n; i++)
    {
        if (vec[i].a_type == A_SEMI)
        {
Miller Puckette's avatar
Miller Puckette committed
454
455
456
457
458
            if (i > messstart && vec[messstart].a_type == A_SYMBOL)
                outlet_anything(x->x_outlet1, vec[messstart].a_w.w_symbol,
                    i-(messstart+1), vec+(messstart+1));
            else if (i > messstart)
                outlet_list(x->x_outlet1, 0, i-messstart, vec+messstart);
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
            messstart = i+1;
        }
    }
    binbuf_free(b);
    x->x_infill = 0;
}

static void pd_tilde_anything(t_pd_tilde *x, t_symbol *s,
    int argc, t_atom *argv)
{
    char msgbuf[MAXPDSTRING], *sp, *ep = msgbuf+MAXPDSTRING;
    if (!x->x_outfd)
        return;
    msgbuf[0] = 0;
    strncpy(msgbuf, s->s_name, MAXPDSTRING);
    msgbuf[MAXPDSTRING-1] = 0;
    sp = msgbuf + strlen(msgbuf);
    while (argc--)
    {
        if (sp < ep-1)
            sp[0] = ' ', sp[1] = 0, sp++;
        atom_string(argv++, sp, ep-sp);
        sp += strlen(sp);
    }
    fprintf(x->x_outfd, "%s;\n", msgbuf);
}

static void *pd_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
    t_pd_tilde *x = (t_pd_tilde *)pd_new(pd_tilde_class);
Miller Puckette's avatar
Miller Puckette committed
489
    int ninsig = 2, noutsig = 2, j, fifo = 5;
490
491
    float sr = sys_getsr();
    t_sample **g;
Miller Puckette's avatar
Miller Puckette committed
492
    t_symbol *pddir = sys_guidir,
493
        *scheddir = gensym(class_gethelpdir(pd_tilde_class));
Miller Puckette's avatar
Miller Puckette committed
494
    /* fprintf(stderr, "pd %s, sched %s\n", pddir->s_name, scheddir->s_name); */
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
    while (argc > 0)
    {
        t_symbol *firstarg = atom_getsymbolarg(0, argc, argv);
        if (!strcmp(firstarg->s_name, "-sr") && argc > 1)
        {
            sr = atom_getfloatarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else if (!strcmp(firstarg->s_name, "-ninsig") && argc > 1)
        {
            ninsig = atom_getfloatarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else if (!strcmp(firstarg->s_name, "-noutsig") && argc > 1)
        {
            noutsig = atom_getfloatarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else if (!strcmp(firstarg->s_name, "-fifo") && argc > 1)
        {
            fifo = atom_getfloatarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else if (!strcmp(firstarg->s_name, "-pddir") && argc > 1)
        {
            pddir = atom_getsymbolarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else if (!strcmp(firstarg->s_name, "-scheddir") && argc > 1)
        {
            scheddir = atom_getsymbolarg(1, argc, argv);
            argc -= 2; argv += 2;
        }
        else break;
    }
Miller Puckette's avatar
Miller Puckette committed
530
531

    if (argc)
532
    {
Miller Puckette's avatar
Miller Puckette committed
533
534
535
536
        pd_error(x,
"usage: pd~ [-sr #] [-ninsig #] [-noutsig #] [-fifo #] [-pddir <>]");
        post(
"... [-scheddir <>]");
537
    }
Miller Puckette's avatar
Miller Puckette committed
538

539
540
541
542
543
    x->x_clock = clock_new(x, (t_method)pd_tilde_tick);
    x->x_insig = (t_sample **)getbytes(ninsig * sizeof(*x->x_insig));
    x->x_outsig = (t_sample **)getbytes(noutsig * sizeof(*x->x_outsig));
    x->x_ninsig = ninsig;
    x->x_noutsig = noutsig;
544
545
546
547
548
549
550
551
552
    x->x_fifo = fifo;
    x->x_sr = sr;
    x->x_pddir = pddir;
    x->x_schedlibdir = scheddir;
    x->x_infd = 0;
    x->x_outfd = 0;
    x->x_outfd = 0;
    x->x_childpid = -1;
    x->x_msgbuf = 0;
553
    x->x_canvas = canvas_getcurrent();
554
555
    for (j = 1, g = x->x_insig; j < ninsig; j++, g++)
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
Miller Puckette's avatar
Miller Puckette committed
556
    x->x_outlet1 = outlet_new(&x->x_obj, 0);
557
558
    for (j = 0, g = x->x_outsig; j < noutsig; j++, g++)
        outlet_new(&x->x_obj, &s_signal);
Miller Puckette's avatar
Miller Puckette committed
559
    signal(SIGPIPE, SIG_IGN);
560
561
562
563
564
565
566
567
568
569

    return (x);
}

void pd_tilde_setup(void)
{
    pd_tilde_class = class_new(gensym("pd~"), (t_newmethod)pd_tilde_new,
        (t_method)pd_tilde_free, sizeof(t_pd_tilde), 0, A_GIMME, 0);
    class_addmethod(pd_tilde_class, nullfn, gensym("signal"), 0);
    class_addmethod(pd_tilde_class, (t_method)pd_tilde_dsp, gensym("dsp"), 0);
570
    class_addmethod(pd_tilde_class, (t_method)pd_tilde_pdtilde, gensym("pd~"), A_GIMME, 0);
571
    class_addanything(pd_tilde_class, pd_tilde_anything);
Miller Puckette's avatar
Miller Puckette committed
572
    post("pd~ version 0.2");
573
574
575
576
577
578
}
#endif

/* -------------------------- MSP glue ------------------------- */
#ifdef MSP

Miller Puckette's avatar
Miller Puckette committed
579
580
581
582
583
584
#define LOTS 10000

static void pd_tilde_tick(t_pd_tilde *x)
{
    int messstart = 0, i, n = 0;
    t_atom vec[LOTS];
585
586
587
588
589
    long z1 = 0, z2 = 0;
    void *b;
    if (!x->x_msgbuf)
        return;
    b = binbuf_new();
Miller Puckette's avatar
Miller Puckette committed
590
591
592
593
    binbuf_text(b, &x->x_msgbuf, x->x_infill);
    /* binbuf_print(b); */
    while (!binbuf_getatom(b, &z1, &z2, vec+n))
    if (++n >= LOTS)
Miller Puckette's avatar
Miller Puckette committed
594
        break;
Miller Puckette's avatar
Miller Puckette committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
    for (i = 0; i < n; i++)
    {
        if (vec[i].a_type == A_SEMI)
        {
            if (i > messstart + 1)
            {
                void *whom;
                if (vec[messstart].a_type != A_SYM)
                    ERROR "pd_tilde_tick got non-symbol?");
                else if (!(whom = vec[messstart].a_w.w_sym->s_thing))
                    ERROR "%s: no such object",
                        vec[messstart].a_w.w_sym->s_name);
                else if (vec[messstart+1].a_type == A_SYM)
                    typedmess(whom, vec[messstart+1].a_w.w_sym,
                        i-messstart-2, vec+(messstart+2));
Miller Puckette's avatar
Miller Puckette committed
610
611
612
613
                else if (vec[messstart+1].a_type == A_FLOAT && i == messstart+2)
                    typedmess(whom, gensym("float"), 1, vec+(messstart+1));
                else if (vec[messstart+1].a_type == A_LONG && i == messstart+2)
                    typedmess(whom, gensym("int"), 1, vec+(messstart+1));
Miller Puckette's avatar
Miller Puckette committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
                else typedmess(whom, gensym("list"),
                    i-messstart-1, vec+(messstart+1));
            }
            messstart = i+1;
        }
    }
    binbuf_free(b);
    x->x_infill = 0;
}

static void pd_tilde_anything(t_pd_tilde *x, t_symbol *s,
    long ac, t_atom *av)
{
    char msgbuf[MAXPDSTRING], *sp, *ep = msgbuf+MAXPDSTRING;
    if (!x->x_outfd)
        return;
    msgbuf[0] = 0;
    strncpy(msgbuf, s->s_name, MAXPDSTRING);
    msgbuf[MAXPDSTRING-1] = 0;
    sp = msgbuf + strlen(msgbuf);
    while (ac--)
    {
        if (sp < ep-1)
            sp[0] = ' ', sp[1] = 0, sp++;
Miller Puckette's avatar
Miller Puckette committed
638
639
640
641
642
643
644
645
646
        if (sp < ep - 80)
        {
            if (av->a_type == A_SYM && strlen(av->a_w.w_sym->s_name) < ep - sp-20)
                strcpy(sp, av->a_w.w_sym->s_name);
            else if (av->a_type == A_LONG)
                sprintf(sp, "%ld" ,av->a_w.w_long);
            else if (av->a_type == A_FLOAT)
                sprintf(sp, "%g" ,av->a_w.w_float);
        }
Miller Puckette's avatar
Miller Puckette committed
647
        sp += strlen(sp);
Miller Puckette's avatar
Miller Puckette committed
648
        av++;
Miller Puckette's avatar
Miller Puckette committed
649
650
651
652
653
    }
    fprintf(x->x_outfd, "%s;\n", msgbuf);
}

int main()
654
655
656
657
658
659
660
{       
    t_class *c;

    c = class_new("pd_tilde~", (method)pd_tilde_new, (method)pd_tilde_free, sizeof(t_pd_tilde), (method)0L, A_GIMME, 0);

    class_addmethod(c, (method)pd_tilde_dsp, "dsp", A_CANT, 0);
    class_addmethod(c, (method)pd_tilde_assist, "assist", A_CANT, 0);
661
662
    class_addmethod(c, (method)pd_tilde_pdtilde, "pd~", A_GIMME, 0);
    class_addmethod(c, (method)pd_tilde_anything, "anything", A_GIMME, 0);
663
664
665
666
    class_dspinit(c);

    class_register(CLASS_BOX, c);
    pd_tilde_class = c;
Miller Puckette's avatar
Miller Puckette committed
667
668
    post("pd~ version 0.2");
    return (0);
669
670
671
672
}

static void *pd_tilde_new(t_symbol *s, long ac, t_atom *av)
{
Miller Puckette's avatar
Miller Puckette committed
673
674
675
    int ninsig = 2, noutsig = 2, fifo = 5, j;
    float sr = sys_getsr();
    t_symbol *pddir = gensym("."), *scheddir = gensym(".");
676
677
    t_pd_tilde *x;

Miller Puckette's avatar
Miller Puckette committed
678
679
680
    if (x = (t_pd_tilde *)object_alloc(pd_tilde_class))
    {
        while (ac > 0 && av[0].a_type == A_SYM)
Miller Puckette's avatar
Miller Puckette committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
        {
            char *flag = av[0].a_w.w_sym->s_name;
            if (!strcmp(flag, "-sr") && ac > 1)
            {
                sr = (av[1].a_type == A_FLOAT ? av[1].a_w.w_float :
                    (av[1].a_type == A_LONG ? av[1].a_w.w_long : 0));
                ac -= 2; av += 2;
            }
            else if (!strcmp(flag, "-ninsig") && ac > 1)
            {
                ninsig = (av[1].a_type == A_FLOAT ? av[1].a_w.w_float :
                    (av[1].a_type == A_LONG ? av[1].a_w.w_long : 0));
                ac -= 2; av += 2;
            }
            else if (!strcmp(flag, "-noutsig") && ac > 1)
            {
                noutsig = (av[1].a_type == A_FLOAT ? av[1].a_w.w_float :
                    (av[1].a_type == A_LONG ? av[1].a_w.w_long : 0));
                ac -= 2; av += 2;
            }
            else if (!strcmp(flag, "-fifo") && ac > 1)
            {
                fifo = (av[1].a_type == A_FLOAT ? av[1].a_w.w_float :
                    (av[1].a_type == A_LONG ? av[1].a_w.w_long : 0));
                ac -= 2; av += 2;
            }
            else if (!strcmp(flag, "-pddir") && ac > 1)
            {
                pddir = (av[1].a_type == A_SYM ? av[1].a_w.w_sym : gensym("."));
                ac -= 2; av += 2;
            }
            else if (!strcmp(flag, "-scheddir") && ac > 1)
            {
                scheddir = (av[1].a_type == A_SYM ? av[1].a_w.w_sym : gensym("."));
                ac -= 2; av += 2;
            }
            else break;
        }
Miller Puckette's avatar
Miller Puckette committed
719
720
        if (scheddir == gensym(".") && pddir != gensym("."))
        {
Miller Puckette's avatar
Miller Puckette committed
721
            char *pds = pddir->s_name, scheddirstring[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
722
723
            int l = strlen(pds);
            if (l >= 4 && (!strcmp(pds+l-3, "bin") || !strcmp(pds+l-4, "bin/")))
Miller Puckette's avatar
Miller Puckette committed
724
725
726
                snprintf(scheddirstring, MAXPDSTRING, "%s/../extra/pd~", pds);
            else snprintf(scheddirstring, MAXPDSTRING, "%s/extra/pd~", pds);
            scheddir = gensym(scheddirstring);
Miller Puckette's avatar
Miller Puckette committed
727
        }
Miller Puckette's avatar
Miller Puckette committed
728
729
730
731
        if (ac)
            post("pd~: warning: ignoring extra arguments");
        post("pd~: pddir %s scheddir %s",
            pddir->s_name, scheddir->s_name);
Miller Puckette's avatar
Miller Puckette committed
732
        dsp_setup((t_pxobject *)x, ninsig);
Miller Puckette's avatar
Miller Puckette committed
733
734
        for (j = 0; j < noutsig; j++)
            outlet_new((t_pxobject *)x, "signal");
735
        x->x_clock = clock_new(x, (method)pd_tilde_tick);
Miller Puckette's avatar
Miller Puckette committed
736
737
738
739
        x->x_insig = (t_sample **)getbytes(ninsig * sizeof(*x->x_insig));
        x->x_outsig = (t_sample **)getbytes(noutsig * sizeof(*x->x_outsig));
        x->x_ninsig = ninsig;
        x->x_noutsig = noutsig;
740
741
742
743
744
745
746
747
748
        x->x_fifo = fifo;
        x->x_sr = sr;
        x->x_pddir = pddir;
        x->x_schedlibdir = scheddir;
        x->x_infd = 0;
        x->x_outfd = 0;
        x->x_outfd = 0;
        x->x_childpid = -1;
        x->x_msgbuf = 0;
749
750
751
752
753
754
755
756
757
    }
    return (x);
}

void pd_tilde_assist(t_pd_tilde *x, void *b, long m, long a, char *s)
{
}

#endif /* MSP */