aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/minmax.c
blob: 5bd0e3914767a2f2f82ca2770ec1a5965eae3f8d (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
/* 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 "m_pd.h"
#include "sickle/sic.h"

typedef struct _minmax
{
    t_sic      x_sic;
    t_float    x_min;
    t_float    x_max;
    t_outlet  *x_minout;
    t_outlet  *x_maxout;
} t_minmax;

static t_class *minmax_class;

static void minmax_bang(t_minmax *x)
{
    outlet_float(x->x_maxout, x->x_max);
    outlet_float(x->x_minout, x->x_min);
}

static void minmax_reset(t_minmax *x)
{
    x->x_min = x->x_max = 0;  /* CHECKME */
}

static t_int *minmax_perform(t_int *w)
{
    t_minmax *x = (t_minmax *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    t_float *outmin = (t_float *)(w[4]);
    t_float *outmax = (t_float *)(w[5]);
    t_float fmin = x->x_min;
    t_float fmax = x->x_max;
    while (nblock--)
    {
	t_float f = *in++;
	if (f < fmin) fmin = f;
	else if (f > fmax) fmax = f;
	*outmin++ = fmin;
	*outmax++ = fmax;
    }
    x->x_min = fmin;
    x->x_max = fmax;
    return (w + 6);
}

static void minmax_dsp(t_minmax *x, t_signal **sp)
{
    dsp_add(minmax_perform, 5, x, sp[0]->s_n, sp[0]->s_vec,
	    sp[1]->s_vec, sp[2]->s_vec);
}

static void *minmax_new(void)
{
    t_minmax *x = (t_minmax *)pd_new(minmax_class);
    outlet_new((t_object *)x, &s_signal);
    outlet_new((t_object *)x, &s_signal);
    x->x_minout = outlet_new((t_object *)x, &s_float);
    x->x_maxout = outlet_new((t_object *)x, &s_float);
    minmax_reset(x);
    return (x);
}

void minmax_tilde_setup(void)
{
    minmax_class = class_new(gensym("minmax~"),
			     (t_newmethod)minmax_new, 0,
			     sizeof(t_minmax), 0, 0);
    sic_setup(minmax_class, minmax_dsp, SIC_FLOATTOSIGNAL);
    class_addbang(minmax_class, minmax_bang);
    class_addmethod(minmax_class, (t_method)minmax_reset,
		    gensym("reset"), 0);
}