aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/anal.c
blob: 477eb20e40e4623016d941b4cb50d863e670e33b (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
/* 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 <string.h>
#include "m_pd.h"
#include "common/loud.h"

#define ANAL_DEFSIZE  128
#define ANAL_MAXSIZE  1024

typedef struct _anal
{
    t_object  x_ob;
    int       x_value;  /* negative, if initialized or reset */
    int       x_size;
    int       x_bytesize;
    int      *x_table;
    /* CHECKED: the 'weight' count is a signed short (2 bytes),
       wrapping into negative domain without any warning.
       LATER consider complaining at == SHRT_MAX. */
} t_anal;

static t_class *anal_class;

static void anal_reset(t_anal *x)
{
    x->x_value = -1;
}

static void anal_clear(t_anal *x)
{
    memset(x->x_table, 0, x->x_bytesize);
}

static void anal_float(t_anal *x, t_float f)
{
    int value;
    if (loud_checkint((t_pd *)x, f, &value, &s_float))  /* CHECKED */
    {
	/* CHECKED: any input negative or >= size is ignored with an error
	   -- there is no output, and a previous state is kept. */
	if (value >= 0 && value < x->x_size)
	{
	    if (x->x_value >= 0)
	    {
		t_atom at[3];
		int ndx = x->x_value * x->x_size + value;
		x->x_table[ndx]++;
		SETFLOAT(at, x->x_value);
		SETFLOAT(&at[1], value);
		SETFLOAT(&at[2], x->x_table[ndx]);
		outlet_list(((t_object *)x)->ob_outlet, &s_list, 3, at);
	    }
	    x->x_value = value;
	}
	else loud_error((t_pd *)x, "%d outside of table bounds", value);
    }
}

static void anal_free(t_anal *x)
{
    if (x->x_table)
	freebytes(x->x_table, x->x_bytesize);
}

static void *anal_new(t_floatarg f)
{
    t_anal *x;
    int size = (int)f;
    int bytesize;
    int *table;
    if (size < 1)
	/* CHECKED: 1 is allowed (such an object rejects any nonzero input) */
	size = ANAL_DEFSIZE;
    else if (size > ANAL_MAXSIZE)
    {
	/* CHECKED: */
	loud_warning(&anal_class, "size too large, using %d", ANAL_MAXSIZE);
	size = ANAL_MAXSIZE;  /* LATER switch into a 'sparse' mode */
    }
    /* CHECKED: actually the bytesize is size * size * sizeof(short),
       and it shows up in */
    bytesize = size * size * sizeof(*table);
    if (!(table = (int *)getbytes(bytesize)))
	return (0);
    x = (t_anal *)pd_new(anal_class);
    x->x_size = size;
    x->x_bytesize = bytesize;
    x->x_table = table;
    outlet_new((t_object *)x, &s_list);
    anal_reset(x);
    anal_clear(x);
    return (x);
}

void anal_setup(void)
{
    anal_class = class_new(gensym("anal"),
			   (t_newmethod)anal_new,
			   (t_method)anal_free,
			   sizeof(t_anal), 0, A_DEFFLOAT, 0);
    class_addfloat(anal_class, anal_float);
    class_addmethod(anal_class, (t_method)anal_reset,
		    gensym("reset"), 0);
    class_addmethod(anal_class, (t_method)anal_clear,
		    gensym("clear"), 0);
}