aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/average.c
blob: ea330c10c0f593e0aabe3f3e51c80c8776e1a215 (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
/* 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.  */

/* CHECKME no reset after changing of a window size? */
/* CHECKME overlap */

#include <math.h>
#include "m_pd.h"
#include "sickle/sic.h"

#if defined(NT) || defined(MACOSX)
/* cf pd/src/x_arithmetic.c */
#define sqrtf  sqrt
#endif

#define AVERAGE_DEFNPOINTS  100  /* CHECKME */
#define AVERAGE_DEFMODE     AVERAGE_BIPOLAR
enum { AVERAGE_BIPOLAR, AVERAGE_ABSOLUTE, AVERAGE_RMS };

typedef struct _average
{
    t_sic     x_sic;
    int       x_mode;
    float   (*x_sumfn)(t_float*, int, float);
    int       x_phase;
    int       x_npoints;
    float     x_result;
    float     x_accum;
    t_clock  *x_clock;
} t_average;

static t_class *average_class;

static void average_tick(t_average *x)
{
    outlet_float(((t_object *)x)->ob_outlet, x->x_result);
}

static float average_bipolarsum(t_float *in, int nxfer, float accum)
{
    while (nxfer--)
	accum += *in++;
    return (accum);
}

static float average_absolutesum(t_float *in, int nxfer, float accum)
{
    while (nxfer--)
    {
	float f = *in++;
	accum += (f >= 0 ? f : -f);
    }
    return (accum);
}

static float average_rmssum(t_float *in, int nxfer, float accum)
{
    while (nxfer--)
    {
	float f = *in++;
	accum += f * f;
    }
    return (accum);
}

static void average_setmode(t_average *x, int mode)
{
    if (mode == AVERAGE_BIPOLAR)
	x->x_sumfn = average_bipolarsum;
    else if (mode == AVERAGE_ABSOLUTE)
	x->x_sumfn = average_absolutesum;
    else if (mode == AVERAGE_RMS)
	x->x_sumfn = average_rmssum;
    else
    {
	bug("average_setmode");
	return;
    }
    x->x_mode = mode;
    x->x_phase = x->x_npoints;
    x->x_accum = 0;
}

static void average_float(t_average *x, t_float f)
{
    int i = (int)f;  /* CHECKME noninteger */
    if (i > 0)  /* CHECKME */
    {
	x->x_npoints = i;
	x->x_phase = x->x_npoints;
	x->x_accum = 0;
    }
}

static void average_bipolar(t_average *x)
{
    average_setmode(x, AVERAGE_BIPOLAR);
}

static void average_absolute(t_average *x)
{
    average_setmode(x, AVERAGE_ABSOLUTE);
}

static void average_rms(t_average *x)
{
    average_setmode(x, AVERAGE_RMS);
}

static t_int *average_perform(t_int *w)
{
    t_average *x = (t_average *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    float (*sumfn)(t_float*, int, float) = x->x_sumfn;
    int phase = x->x_phase;
    if (phase <= nblock)
    {
	float accum = (*sumfn)(in, phase, x->x_accum);
	nblock -= phase;
	if (x->x_mode == AVERAGE_RMS)
	    /* CHECKME scaling and FIXME */
	    x->x_result = sqrtf(accum / x->x_npoints);
	else
	    x->x_result = accum / x->x_npoints;
	clock_delay(x->x_clock, 0);
	x->x_accum = 0;
	if (nblock < x->x_npoints)
	    x->x_phase = x->x_npoints - nblock;
	else
	{
	    x->x_phase = x->x_npoints;
	    return (w + 4);
	}
    }
    else x->x_phase -= nblock;
    x->x_accum = (*sumfn)(in, nblock, x->x_accum);
    return (w + 4);
}

static void average_dsp(t_average *x, t_signal **sp)
{
    dsp_add(average_perform, 3, x, sp[0]->s_n, sp[0]->s_vec);
}

static void average_free(t_average *x)
{
    if (x->x_clock) clock_free(x->x_clock);
}

static void *average_new(t_symbol *s, t_floatarg f)
{
    t_average *x = (t_average *)pd_new(average_class);
    int i = (int)f;  /* CHECKME noninteger */
    int mode;
    /* CHECKED it looks like memory is allocated for the entire window,
       in tune with the refman's note about ``maximum averaging interval'' --
       needed for dynamic control over window size, or what? LATER rethink */
    x->x_npoints = (i > 0 ?  /* CHECKME */
		    i : AVERAGE_DEFNPOINTS);
    if (s == gensym("bipolar"))
	mode = AVERAGE_BIPOLAR;
    else if (s == gensym("absolute"))
	mode = AVERAGE_ABSOLUTE;
    else if (s == gensym("rms"))
	mode = AVERAGE_RMS;
    else
    {
	mode = AVERAGE_DEFMODE;
	/* CHECKME a warning if (s && s != &s_) */
    }
    average_setmode(x, mode);
    /* CHECKME if not x->x_phase = 0 */
    outlet_new((t_object *)x, &s_float);
    x->x_clock = clock_new(x, (t_method)average_tick);
    return (x);
}

void average_tilde_setup(void)
{
    average_class = class_new(gensym("average~"),
			      (t_newmethod)average_new,
			      (t_method)average_free,
			      sizeof(t_average), 0,
			      A_DEFFLOAT, A_DEFSYM, 0);
    sic_setup(average_class, average_dsp, average_float);
    class_addmethod(average_class, (t_method)average_bipolar,
		    gensym("bipolar"), 0);
    class_addmethod(average_class, (t_method)average_absolute,
		    gensym("absolute"), 0);
    class_addmethod(average_class, (t_method)average_rms,
		    gensym("rms"), 0);
}