aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/bangbang.c
blob: aa8363d352c4c59b1ad1d64cc4d852c717a90f52 (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
/* 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 a modified 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 BANGBANG_MINOUTS      1
#define BANGBANG_C74MAXOUTS  40  /* CHECKED (just clipped without warning) */
#define BANGBANG_DEFOUTS      2

typedef struct _bangbang
{
    t_object    x_ob;
    int         x_nouts;
    t_outlet  **x_outs;
    t_outlet   *x_outbuf[BANGBANG_DEFOUTS];
} t_bangbang;

static t_class *bangbang_class;

static void bangbang_bang(t_bangbang *x)
{
    int i = x->x_nouts;
    while (i--) outlet_bang(x->x_outs[i]);
}

static void bangbang_anything(t_bangbang *x, t_symbol *s, int ac, t_atom *av)
{
    bangbang_bang(x);
}

static void bangbang_free(t_bangbang *x)
{
    if (x->x_outs != x->x_outbuf)
	freebytes(x->x_outs, x->x_nouts * sizeof(*x->x_outs));
}

static void *bangbang_new(t_floatarg val)
{
    t_bangbang *x;
    int i, nouts = (int)val;
    t_outlet **outs;
    if (nouts < BANGBANG_MINOUTS)
	nouts = BANGBANG_DEFOUTS;
    if (nouts > BANGBANG_C74MAXOUTS)
	fittermax_rangewarning(bangbang_class, BANGBANG_C74MAXOUTS, "outlets");
    if (nouts > BANGBANG_DEFOUTS)
    {
	if (!(outs = (t_outlet **)getbytes(nouts * sizeof(*outs))))
	    return (0);
    }
    else outs = 0;
    x = (t_bangbang *)pd_new(bangbang_class);
    x->x_nouts = nouts;
    x->x_outs = (outs ? outs : x->x_outbuf);
    for (i = 0; i < nouts; i++)
	x->x_outs[i] = outlet_new((t_object *)x, &s_bang);
    return (x);
}

void bangbang_setup(void)
{
    bangbang_class = class_new(gensym("bangbang"),
			       (t_newmethod)bangbang_new,
			       (t_method)bangbang_free,
			       sizeof(t_bangbang), 0, A_DEFFLOAT, 0);
    class_addbang(bangbang_class, bangbang_bang);
    class_addanything(bangbang_class, bangbang_anything);
    fitter_setup(bangbang_class, 0);
}