aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/Decode.c
blob: 0d6223798de97aa7d9cb7ba9086008697ec12845 (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
/* Copyright (c) 2002-2005 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.  */

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

#define DECODE_C74MAXOUTS  8  /* CHECKED (does it make any sense?) */
#define DECODE_DEFOUTS     1

typedef struct _Decode
{
    t_object    x_ob;
    int         x_numouts;
    int         x_onout;
    int         x_allon;   /* submaster switch */
    int         x_alloff;  /* master switch */
    t_outlet  **x_outs;
    t_outlet   *x_outbuf[DECODE_C74MAXOUTS];
} t_Decode;

static t_class *Decode_class;

/* CHECKED: all outlets deliver after any action */
/* CHECKED: outlets output in right-to-left order */

static void Decode_deliver(t_Decode *x)
{
    int i = x->x_numouts;
    if (x->x_alloff)
	while (i--) outlet_float(x->x_outs[i], 0);
    else if (x->x_allon)
	while (i--) outlet_float(x->x_outs[i], 1);
    else
	while (i--) outlet_float(x->x_outs[i], (i == x->x_onout ? 1 : 0));
}

static void Decode_float(t_Decode *x, t_floatarg f)
{
    int val = (int)f;
    /* CHECKED: out-of-range input is clipped, not ignored */
    if (val < 0)
	val = 0;
    else if (val >= x->x_numouts)
	val = x->x_numouts - 1;
    /* CHECKED: while in all-off mode, input is stored, not ignored */
    x->x_onout = val;
    Decode_deliver(x);
}

static void Decode_allon(t_Decode *x, t_floatarg f)
{
    x->x_allon = (f != 0);
    Decode_deliver(x);
}

static void Decode_alloff(t_Decode *x, t_floatarg f)
{
    x->x_alloff = (f != 0);
    Decode_deliver(x);
}

static void Decode_free(t_Decode *x)
{
    if (x->x_outs != x->x_outbuf)
	freebytes(x->x_outs, x->x_numouts * sizeof(*x->x_outs));
}

static void *Decode_new(t_floatarg val)
{
    t_Decode *x;
    int i, nouts = (int)val;
    t_outlet **outs;
    if (nouts < 1)
	nouts = DECODE_DEFOUTS;
    if (nouts > DECODE_C74MAXOUTS)
    {
	fittermax_rangewarning(Decode_class, DECODE_C74MAXOUTS, "outlets");
	if (!(outs = (t_outlet **)getbytes(nouts * sizeof(*outs))))
	    return (0);
    }
    else outs = 0;
    x = (t_Decode *)pd_new(Decode_class);
    x->x_numouts = nouts;
    x->x_outs = (outs ? outs : x->x_outbuf);
    x->x_onout = 0;
    x->x_allon = 0;
    x->x_alloff = 0;
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft1"));
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft2"));
    for (i = 0; i < nouts; i++)
	x->x_outs[i] = outlet_new((t_object *)x, &s_float);
    return (x);
}

void Decode_setup(void)
{
    Decode_class = class_new(gensym("Decode"),
			     (t_newmethod)Decode_new,
			     (t_method)Decode_free,
			     sizeof(t_Decode), 0, A_DEFFLOAT, 0);
    class_addfloat(Decode_class, Decode_float);
    class_addmethod(Decode_class, (t_method)Decode_allon,
		    gensym("ft1"), A_FLOAT, 0);
    class_addmethod(Decode_class, (t_method)Decode_alloff,
		    gensym("ft2"), A_FLOAT, 0); 
    fitter_setup(Decode_class, 0);
}