aboutsummaryrefslogtreecommitdiff
path: root/osc/pipelist.c
blob: 026a3666d8f58494433c2f07811ccbadcbb0b740 (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
/* pipelist.c 20070711 Martin Peach based on pipe from x_time.c */
/* 20080706 added anything method for meta-messages */
#include "m_pd.h" 
/* -------------------------- pipe -------------------------- */

static t_class *pipelist_class;

typedef struct _hang
{
    t_clock             *h_clock;
    struct _hang        *h_next;
    struct _pipelist    *h_owner;
    int                 h_n; /* number of atoms in h_list */
    t_atom              *h_atoms; /* pointer to a list of h_n t_atoms */
} t_hang;

typedef struct _pipelist
{
    t_object    x_obj;
    float       x_deltime;
    t_outlet    *x_pipelistout;
    t_hang      *x_hang;
} t_pipelist;

static void *pipelist_new(t_symbol *s, int argc, t_atom *argv);
static void pipelist_hang_free(t_hang *h);
static void pipelist_hang_tick(t_hang *h);
static void pipelist_any_hang_tick(t_hang *h);
static void pipelist_list(t_pipelist *x, t_symbol *s, int ac, t_atom *av);
static void pipelist_anything(t_pipelist *x, t_symbol *s, int ac, t_atom *av);
static void pipelist_flush(t_pipelist *x);
static void pipelist_clear(t_pipelist *x);
void pipelist_setup(void);

static void *pipelist_new(t_symbol *s, int argc, t_atom *argv)
{
    t_pipelist  *x = (t_pipelist *)pd_new(pipelist_class);
    float       deltime;

    if (argc)
    { /* We accept one argument to set the delay time, ignore any further args */
        if (argv[0].a_type != A_FLOAT)
        {
            char stupid[80];
            atom_string(&argv[argc-1], stupid, 79);
            post("pipelist: %s: bad time delay value", stupid);
            deltime = 0;
        }
        else deltime = argv[argc-1].a_w.w_float;
    }
    else deltime = 0;

    x->x_pipelistout = outlet_new(&x->x_obj, &s_list);
    floatinlet_new(&x->x_obj, &x->x_deltime);
    x->x_hang = NULL;
    x->x_deltime = deltime;
    return (x);
}

static void pipelist_hang_free(t_hang *h)
{
    freebytes(h->h_atoms, h->h_n*sizeof(t_atom));
    clock_free(h->h_clock);
    freebytes(h, sizeof(t_hang));
}

static void pipelist_hang_tick(t_hang *h)
{
    t_pipelist  *x = h->h_owner;
    t_hang      *h2, *h3;

    /* excise h from the linked list of hangs */
    if (x->x_hang == h) x->x_hang = h->h_next;
    else for (h2 = x->x_hang; ((h3 = h2->h_next)!=NULL); h2 = h3)
    {
        if (h3 == h)
        {
            h2->h_next = h3->h_next;
            break;
        }
    }
    /* output h's list */
    outlet_list(x->x_pipelistout, &s_list, h->h_n, h->h_atoms);
    /* free h */
    pipelist_hang_free(h);
}

static void pipelist_any_hang_tick(t_hang *h)
{
    t_pipelist  *x = h->h_owner;
    t_hang      *h2, *h3;

    /* excise h from the linked list of hangs */
    if (x->x_hang == h) x->x_hang = h->h_next;
    else for (h2 = x->x_hang; ((h3 = h2->h_next)!=NULL); h2 = h3)
    {
        if (h3 == h)
        {
            h2->h_next = h3->h_next;
            break;
        }
    }
    /* output h's atoms */
    outlet_anything(x->x_pipelistout, h->h_atoms[0].a_w.w_symbol, h->h_n-1, &h->h_atoms[1]);
    /* free h */
    pipelist_hang_free(h);
}

static void pipelist_list(t_pipelist *x, t_symbol *s, int ac, t_atom *av)
{
    if (x->x_deltime > 0)
    { /* if delay is real, save the list for output in delay milliseconds */
        t_hang *h;
        int i;

        h = (t_hang *)getbytes(sizeof(t_hang));
        h->h_n = ac;
        h->h_atoms = (t_atom *)getbytes(h->h_n*sizeof(t_atom));

        for (i = 0; i < h->h_n; ++i)
            h->h_atoms[i] = av[i];
        h->h_next = x->x_hang;
        x->x_hang = h;
        h->h_owner = x;
        h->h_clock = clock_new(h, (t_method)pipelist_hang_tick);
        clock_delay(h->h_clock, (x->x_deltime >= 0 ? x->x_deltime : 0));
    }
    /* otherwise just pass the list straight through  */
    else outlet_list(x->x_pipelistout, &s_list, ac, av);
}

static void pipelist_anything(t_pipelist *x, t_symbol *s, int ac, t_atom *av)
{
    if (x->x_deltime > 0)
    { /* if delay is real, save the list for output in delay milliseconds */
        t_hang *h;
        int i;

        h = (t_hang *)getbytes(sizeof(t_hang));
        h->h_n = ac+1;
        h->h_atoms = (t_atom *)getbytes(h->h_n*sizeof(t_atom));
        SETSYMBOL(&h->h_atoms[0], s);
        for (i = 1; i < h->h_n; ++i)
            h->h_atoms[i] = av[i-1];
        h->h_next = x->x_hang;
        x->x_hang = h;
        h->h_owner = x;
        h->h_clock = clock_new(h, (t_method)pipelist_any_hang_tick);
        clock_delay(h->h_clock, (x->x_deltime >= 0 ? x->x_deltime : 0));
    }
    /* otherwise just pass it straight through  */
    else outlet_anything(x->x_pipelistout, s, ac, av);
}

static void pipelist_flush(t_pipelist *x)
{
    while (x->x_hang) pipelist_hang_tick(x->x_hang);
}

static void pipelist_clear(t_pipelist *x)
{
    t_hang *hang;
    while ((hang = x->x_hang) != NULL)
    {
        x->x_hang = hang->h_next;
        pipelist_hang_free(hang);
    }
}

void pipelist_setup(void)
{
    pipelist_class = class_new(gensym("pipelist"), 
        (t_newmethod)pipelist_new, (t_method)pipelist_clear,
        sizeof(t_pipelist), 0, A_GIMME, 0);
    class_addlist(pipelist_class, pipelist_list);
    class_addanything(pipelist_class, pipelist_anything);
    class_addmethod(pipelist_class, (t_method)pipelist_flush, gensym("flush"), 0);
    class_addmethod(pipelist_class, (t_method)pipelist_clear, gensym("clear"), 0);
}

/* end of pipelist.c*/