aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/counter.c
blob: 73cd841cbc9e0f52ecd551403d0c3f6b9189a8c1 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (c) 2002-2004 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* This is an entirely rewritten version of Joseph A. Sarlo's code.
   The most important changes are listed in "pd-lib-notes.txt" file.  */

/* Beware -- the max reference manual page for the counter object
   reflects mostly opcode max features.  Apparently counter works
   differently in cycling max (e.g. inlets 3 and 4).  But I am sick
   of checking -- I will not bother, until there is some feedback. */

#include "m_pd.h"
#include "common/loud.h"

#define COUNTER_UP      0
#define COUNTER_DOWN    1
#define COUNTER_UPDOWN  2
#define COUNTER_DEFMAX  0x7fffffff  /* CHECKED (man says otherwise) */

typedef struct _counter
{
    t_object   x_ob;
    int        x_count;
    int        x_maxcount;
    int        x_dir;
    int        x_inc;
    int        x_min;
    int        x_max;
    int        x_carrybang;
    int        x_minhitflag;
    int        x_maxhitflag;
    t_pd      *x_proxies[4];
    t_outlet  *x_out2;
    t_outlet  *x_out3;
    t_outlet  *x_out4;
} t_counter;

typedef struct _counter_proxy
{
    t_object    p_ob;
    t_counter  *p_master;
    void      (*p_bangmethod)(t_counter *x);
    void      (*p_floatmethod)(t_counter *x, t_float f);
} t_counter_proxy;

static t_class *counter_class;
static t_class *counter_proxy_class;

static void counter_up(t_counter *x)
{
    x->x_dir = COUNTER_UP;
    x->x_inc = 1;
}

static void counter_down(t_counter *x)
{
    /* CHECKED: no explicit minimum needed */
    x->x_dir = COUNTER_DOWN;
    x->x_inc = -1;
}

static void counter_updown(t_counter *x)
{
    /* CHECKED: neither explicit maximum, nor minimum needed */
    x->x_dir = COUNTER_UPDOWN;
    /* CHECKED: x->x_inc unchanged (continuation) */
}

static void counter_dir(t_counter *x, t_floatarg f)
{
    switch ((int)f)
    {
    case COUNTER_UP:
        counter_up(x);
	break;
    case COUNTER_DOWN:
        counter_down(x);
	break;
    case COUNTER_UPDOWN:
        counter_updown(x);
	break;
    default:
        counter_up(x);  /* CHECKED: invalid == default */
	/* CHECKED: no warning */
    }
}

static void counter_dobang(t_counter *x, int notjam)
{
    int offmin = 0, offmax = 0, onmin = 0, onmax = 0;
    /* CHECKED: carry-off is not sent if min >= max */
    /* LATER rethink (this is a hack) */
    if (x->x_min < x->x_max)
	offmin = x->x_minhitflag, offmax = x->x_maxhitflag;
    x->x_minhitflag = x->x_maxhitflag = 0;

    if (x->x_count < x->x_min)
    {
	if (x->x_inc == 1)
	{
	    /* min has changed, which should imply x->x_count == x->x_min */
	    bug("counter_dobang (count < min)");
	}
	else if (x->x_dir == COUNTER_UPDOWN)
	{
	    x->x_inc = 1;
	    if ((x->x_count = x->x_min + 1) > x->x_max) x->x_count = x->x_min;
	}
	else if ((x->x_count = x->x_max) < x->x_min) x->x_count = x->x_min;
    }
    else if (x->x_count > x->x_max)
    {
	if (x->x_inc == -1)
	{
	    /* CHECKED: ignored */
	}
	else if (x->x_dir == COUNTER_UPDOWN)
	{
	    x->x_inc = -1;
	    if ((x->x_count = x->x_max - 1) < x->x_min) x->x_count = x->x_min;
	}
	else x->x_count = x->x_min;
    }

    if (x->x_count == x->x_min && x->x_inc == -1)
    {
	/* CHECKED: 'jam' inhibits middle outlets (unless carry-off)
	   carry-on is never sent if max < min, but sent if max == min */
	if (notjam
	    && x->x_min <= x->x_max)  /* LATER rethink (this is a hack) */
	    onmin = 1;
    }
    else if (x->x_count == x->x_max && x->x_inc == 1)
    {
	/* CHECKED: this counter is never reset (and goes up to INT_MAX)
	   -- neither after dir change, nor after max change */
	x->x_maxcount++;  /* CHECKED: 'jam' does the increment */
	outlet_float(x->x_out4, x->x_maxcount);
	/* CHECKED: 'jam' inhibits middle outlets (unless carry-off)
	   carry-on is never sent if max < min, but sent if max == min */
	if (notjam
	    && x->x_min <= x->x_max)  /* LATER rethink (this is a hack) */
	    onmax = 1;
    }

    /* CHECKED: outlets deliver in right-to-left order */
    if (onmax)
    {
	if (x->x_carrybang) outlet_bang(x->x_out3);
	else
	{
	    outlet_float(x->x_out3, 1);
	    x->x_maxhitflag = 1;
	}
    }
    else if (offmax) outlet_float(x->x_out3, 0);
    else if (onmin)
    {
	if (x->x_carrybang) outlet_bang(x->x_out2);
	else
	{
	    outlet_float(x->x_out2, 1);
	    x->x_minhitflag = 1;
	}
    }
    else if (offmin) outlet_float(x->x_out2, 0);

    outlet_float(((t_object *)x)->ob_outlet, x->x_count);
}

static void counter_bang(t_counter *x)
{
    x->x_count += x->x_inc;
    counter_dobang(x, 1);
}

static void counter_float(t_counter *x, t_float dummy)
{
    counter_bang(x);
}

/* CHECKED: out-of-range values are ignored */
/* CHECKED: 'down, set 3, up, bang' gives 5 */
static void counter_set(t_counter *x, t_floatarg f)
{
    int i = (int)f;
    if (i >= x->x_min && i <= x->x_max)
	x->x_count = i - x->x_inc;
}

/* CHECKED: out-of-range values are ignored */
static void counter_jam(t_counter *x, t_floatarg f)
{
    int i = (int)f;
    if (i >= x->x_min && i <= x->x_max)
    {
	x->x_count = i;
	counter_dobang(x, 0);
    }
}

/* CHECKED: sends max carry on/off in any mode  */
static void counter_inc(t_counter *x)
{
    int tempdir = x->x_dir;
    int tempinc = x->x_inc;
    counter_up(x);
    counter_bang(x);
    x->x_dir = tempdir;
    x->x_inc = tempinc;
}

/* CHECKED: sends min carry on/off in any mode */
static void counter_dec(t_counter *x)
{
    int tempdir = x->x_dir;
    int tempinc = x->x_inc;
    counter_down(x);
    counter_bang(x);
    x->x_dir = tempdir;
    x->x_inc = tempinc;
}

/* CHECKED: min can be set over max */
static void counter_min(t_counter *x, t_floatarg f)
{
    /* CHECKED: min change always sets count to min and bangs */
    /* do not use counter_jam() here -- avoid range checking */
    x->x_count = x->x_min = (int)f;
    counter_dobang(x, 0);
}

/* CHECKED: max can be set below min */
static void counter_max(t_counter *x, t_floatarg f)
{
    x->x_max = (int)f;
}

static void counter_carrybang(t_counter *x)
{
    x->x_carrybang = 1;
}

static void counter_carryint(t_counter *x)
{
    x->x_carrybang = 0;
}

/* CHECKED: up/down switch */
static void counter_bang1(t_counter *x)
{
    if (x->x_dir == COUNTER_UP)
        counter_down(x);
    else if (x->x_dir == COUNTER_DOWN)
        counter_up(x);
    else
	x->x_inc = -x->x_inc;  /* CHECKED */
}

/* CHECKED */
static void counter_bang2(t_counter *x)
{
    counter_set(x, x->x_min);
}

/* CHECKED: out-of-range values are accepted (LATER rethink) */
/* CHECKED: no resetting of min, nor of max (contrary to the man) */
/* CHECKED: 'down, float2 3, up, bang' gives 3 (LATER rethink) */
static void counter_float2(t_counter *x, t_floatarg f)
{
    counter_set(x, f);  /* FIXME */
}

/* CHECKED */
static void counter_bang3(t_counter *x)
{
    counter_jam(x, x->x_min);
}

/* CHECKED: out-of-range values are accepted (LATER rethink) */
/* CHECKED: no resetting of min, nor of max (contrary to the man) */
static void counter_float3(t_counter *x, t_floatarg f)
{
    counter_jam(x, f);  /* FIXME */
}

/* CHECKED */
static void counter_bang4(t_counter *x)
{
    counter_set(x, x->x_max);
}

static void counter_proxy_bang(t_counter_proxy *x)
{
    x->p_bangmethod(x->p_master);
}

static void counter_proxy_float(t_counter_proxy *x, t_float f)
{
    x->p_floatmethod(x->p_master, f);
}

static void counter_free(t_counter *x)
{
    int i;
    for (i = 0; i < 4; i++)
	if (x->x_proxies[i]) pd_free(x->x_proxies[i]);
}

static void *counter_new(t_floatarg f1, t_floatarg f2, t_floatarg f3)
{
    t_counter *x = (t_counter *)pd_new(counter_class);
    t_counter_proxy **pp = (t_counter_proxy **)x->x_proxies;
    int i1 = (int)f1;
    int i2 = (int)f2;
    int i3 = (int)f3;
    int i;
    shared_usecompatibility();
    if (shared_getmaxcompatibility())
    {
	static int warned = 0;
	if (!warned)
	{
	    post("warning: counter is not fully compatible,\
 please report differences");
	    warned = 1;
	}
    }
    x->x_dir = COUNTER_UP;
    x->x_inc = 1;  /* previous value required by counter_dir() */
    x->x_min = 0;
    x->x_max = COUNTER_DEFMAX;
    if (i3) x->x_dir = i1, x->x_min = i2, x->x_max = i3;
    else if (i2) x->x_min = i1, x->x_max = i2;
    else if (i1) x->x_max = i1;
    x->x_carrybang = 0;  /* CHECKED */
    x->x_minhitflag = x->x_maxhitflag = 0;
    x->x_maxcount = 0;
    counter_dir(x, x->x_dir);
    /* CHECKED: [counter 1 <min> <max>] starts from <max> */
    x->x_count = (x->x_dir == COUNTER_DOWN ? x->x_max : x->x_min);
    for (i = 0; i < 4; i++)
    {
	x->x_proxies[i] = pd_new(counter_proxy_class);
	((t_counter_proxy *)x->x_proxies[i])->p_master = x;
	inlet_new((t_object *)x, x->x_proxies[i], 0, 0);
    }
    (*pp)->p_bangmethod = counter_bang1;
    (*pp++)->p_floatmethod = counter_dir;  /* CHECKED: same as dir */
    (*pp)->p_bangmethod = counter_bang2;
    (*pp++)->p_floatmethod = counter_float2;
    (*pp)->p_bangmethod = counter_bang3;
    (*pp++)->p_floatmethod = counter_float3;
    (*pp)->p_bangmethod = counter_bang4;
    (*pp++)->p_floatmethod = counter_max;  /* CHECKED: same as max */
    outlet_new((t_object *)x, &s_float);
    x->x_out2 = outlet_new((t_object *)x, &s_anything);  /* float/bang */
    x->x_out3 = outlet_new((t_object *)x, &s_anything);  /* float/bang */
    x->x_out4 = outlet_new((t_object *)x, &s_float);
    return (x);
}

void counter_setup(void)
{
    counter_class = class_new(gensym("counter"),
			      (t_newmethod)counter_new,
			      (t_method)counter_free,
			      sizeof(t_counter), 0,
			      A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addbang(counter_class, counter_bang);
    class_addfloat(counter_class, counter_float);
    class_addmethod(counter_class, (t_method)counter_bang,
		    gensym("next"), 0);
    class_addmethod(counter_class, (t_method)counter_set,
		    gensym("set"), A_FLOAT, 0);
    class_addmethod(counter_class, (t_method)counter_set,
		    gensym("goto"), A_FLOAT, 0);
    class_addmethod(counter_class, (t_method)counter_jam,
		    gensym("jam"), A_FLOAT, 0);
    class_addmethod(counter_class, (t_method)counter_up,
		    gensym("up"), 0);
    class_addmethod(counter_class, (t_method)counter_down,
		    gensym("down"), 0);
    class_addmethod(counter_class, (t_method)counter_updown,
		    gensym("updown"), 0);
    class_addmethod(counter_class, (t_method)counter_inc,
		    gensym("inc"), 0);
    class_addmethod(counter_class, (t_method)counter_dec,
		    gensym("dec"), 0);
    class_addmethod(counter_class, (t_method)counter_min,
		    gensym("min"), A_FLOAT, 0);
    class_addmethod(counter_class, (t_method)counter_max,
		    gensym("max"), A_FLOAT, 0);
    class_addmethod(counter_class, (t_method)counter_carrybang,
		    gensym("carrybang"), 0);
    class_addmethod(counter_class, (t_method)counter_carryint,
		    gensym("carryint"), 0);
    counter_proxy_class = class_new(gensym("_counter_proxy"), 0, 0,
				    sizeof(t_counter_proxy),
				    CLASS_PD | CLASS_NOINLET, 0);
    class_addbang(counter_proxy_class, counter_proxy_bang);
    class_addfloat(counter_proxy_class, counter_proxy_float);
}