aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/count.c
blob: 2a36f441132d26b2c7ab5a672ae5c47987bf94ca (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
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include "m_pd.h"
#include "sickle/sic.h"

#define COUNTMAXINT 0x7fffffff 

typedef struct _count
{
    t_sic  x_sic;
    int    x_min;
    int    x_max;
    int    x_limit;
    int    x_on;
    int    x_autoreset;
    int    x_count;  /* MAYBE use 64 bits (if 13.5 hours is not enough...) */
} t_count;

static t_class *count_class;

static void count_bang(t_count *x)
{
    x->x_count = x->x_min;
    x->x_on = 1;
}

static void count_min(t_count *x, t_floatarg f)
{
    x->x_min = (int)f;
    count_bang(x);
}

static void count_max(t_count *x, t_floatarg f)
{
    x->x_max = (int)f;
    /* MAYBE use 64 bits */
    x->x_limit = (x->x_max == 0 ? COUNTMAXINT
		  : x->x_max - 1);  /* CHECKED */
    count_bang(x);
}

static void count_autoreset(t_count *x, t_floatarg f)
{
    x->x_autoreset = (f != 0);
}

static void count_float(t_count *x, t_floatarg f)
{
    x->x_count = x->x_min = (int)f;
    x->x_on = 1;
}

static void count_list(t_count *x, t_symbol *s, int ac, t_atom *av)
{
    int i;
    if (ac > 4) ac = 4;
    for (i = 0; i < ac; i++)
	if (av[i].a_type != A_FLOAT) break;
    switch (i)
    {
    case 4:
	count_autoreset(x, av[3].a_w.w_float);
    case 3:
	x->x_on = (av[2].a_w.w_float != 0);
    case 2:
	count_max(x, av[1].a_w.w_float);
    case 1:
	count_min(x, av[0].a_w.w_float);
    default:
	x->x_count = x->x_min;
    }
}

/* changes minimum (and optional the maximum) without changing current value */
static void count_set(t_count *x, t_symbol *s, int ac, t_atom *av)
{
    if (ac > 0)
    {
	if (av[0].a_type == A_FLOAT) {
	    x->x_min = (int)av[0].a_w.w_float;
	}
    }
    if (ac > 1)
    {
	if (av[1].a_type == A_FLOAT) 
	{
	    x->x_max = (int)av[1].a_w.w_float;
	    x->x_limit = (x->x_max == 0 ? COUNTMAXINT
		: x->x_max - 1);  /* CHECKED */
	}
    } 
}

static void count_stop(t_count *x)
{
    x->x_count = x->x_min;
    x->x_on = 0;
}

static t_int *count_perform(t_int *w)
{
    t_count *x = (t_count *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *out = (t_float *)(w[3]);
    int count = x->x_count;
    int limit = x->x_limit;
    if (x->x_on)
    {
	while (nblock--)
	{
	    if (count > limit) count = x->x_min;
	    *out++ = (t_float)count++;
	}
    }
    else
	while (nblock--) *out++ = count;
    x->x_count = count;
    return (w + 4);
}

static void count_dsp(t_count *x, t_signal **sp)
{
    if (x->x_autoreset) count_bang(x);
    dsp_add(count_perform, 3, x, sp[0]->s_n, sp[0]->s_vec);
}

static void *count_new(t_floatarg minval, t_floatarg maxval,
		       t_floatarg onflag, t_floatarg autoflag)
{
    t_count *x = (t_count *)pd_new(count_class);
    count_min(x, minval);
    count_max(x, maxval);
    x->x_on = (onflag != 0);
    count_autoreset(x, autoflag);
    x->x_count = x->x_min;
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft1"));
    outlet_new((t_object *)x, &s_signal);
    return (x);
}

void count_tilde_setup(void)
{
    count_class = class_new(gensym("count~"),
			    (t_newmethod)count_new, 0,
			    sizeof(t_count), 0,
			    A_DEFFLOAT, A_DEFFLOAT,
			    A_DEFFLOAT, A_DEFFLOAT, 0);
    sic_setup(count_class, count_dsp, SIC_NOMAINSIGNALIN);
    class_addbang(count_class, count_bang);
    class_addfloat(count_class, count_float);
    class_addlist(count_class, count_list);
    class_addmethod(count_class, (t_method)count_max,
		    gensym("ft1"), A_FLOAT, 0);
    class_addmethod(count_class, (t_method)count_autoreset,
		    gensym("autoreset"), A_FLOAT, 0);
    class_addmethod(count_class, (t_method)count_min,
		    gensym("min"), A_FLOAT, 0);
    class_addmethod(count_class, (t_method)count_set,
		    gensym("set"), A_GIMME, 0);
    class_addmethod(count_class, (t_method)count_stop, gensym("stop"), 0);
}