summaryrefslogtreecommitdiffstats
path: root/apps/plugins/pdbox/PDa/extra/zerox~.c
blob: f97f412308ea608b6ff17fd7fb335f23ee320164 (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
#include "m_pd.h"

static t_class *zerox_class;

typedef struct _zerox
{
    t_object x_obj; 
    t_sample x_f;
    t_int   x_zeros;
} t_zerox;


static t_int *zerox_perform(t_int *w)
{
    t_zerox* x = (t_zerox*)w[1];
    t_sample *in = (t_sample *)(w[2]);
    int n = (int)(w[3]) ;

    if (*in * x->x_f < 0) x->x_zeros++;
    n--;
    while (n--)
    {
    	float f = *(in++);
	x->x_zeros += f * *in < 0;
    }
    return (w+4);
}

static void zerox_dsp(t_zerox *x, t_signal **sp)
{
    dsp_add(zerox_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}


static void zerox_bang(t_zerox* x)
{
    outlet_float(x->x_obj.ob_outlet,x->x_zeros);
    x->x_zeros=0;
}

static void *zerox_new(void)
{
    t_zerox *x = (t_zerox *)pd_new(zerox_class);
    outlet_new(&x->x_obj, gensym("float"));
    x->x_f = 0;
    x->x_zeros=0;
    return (x);
}

void zerox_tilde_setup(void)
{
    zerox_class = class_new(gensym("zerox~"), (t_newmethod)zerox_new, 0,
    	sizeof(t_zerox), 0, A_DEFFLOAT, 0);
    CLASS_MAINSIGNALIN(zerox_class, t_zerox, x_f);
    class_addmethod(zerox_class, (t_method)zerox_dsp, gensym("dsp"), 0);
    class_addbang(zerox_class, (t_method)zerox_bang);
}
#include "m_pd.h"

static t_class *zerox_class;

typedef struct _zerox
{
    t_object x_obj; 
    t_sample x_f;
    t_int   x_zeros;
} t_zerox;


static t_int *zerox_perform(t_int *w)
{
    t_zerox* x = (t_zerox*)w[1];
    t_sample *in = (t_sample *)(w[2]);
    int n = (int)(w[3]) ;

    if (*in * x->x_f < 0) x->x_zeros++;
    n--;
    while (n--)
    {
    	float f = *(in++);
	x->x_zeros += f * *in < 0;
    }
    return (w+4);
}

static void zerox_dsp(t_zerox *x, t_signal **sp)
{
    dsp_add(zerox_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}


static void zerox_bang(t_zerox* x)
{
    outlet_float(x->x_obj.ob_outlet,x->x_zeros);
    x->x_zeros=0;
}

static void *zerox_new(void)
{
    t_zerox *x = (t_zerox *)pd_new(zerox_class);
    outlet_new(&x->x_obj, gensym("float"));
    x->x_f = 0;
    x->x_zeros=0;
    return (x);
}

void zerox_tilde_setup(void)
{
    zerox_class = class_new(gensym("zerox~"), (t_newmethod)zerox_new, 0,
    	sizeof(t_zerox), 0, A_DEFFLOAT, 0);
    CLASS_MAINSIGNALIN(zerox_class, t_zerox, x_f);
    class_addmethod(zerox_class, (t_method)zerox_dsp, gensym("dsp"), 0);
    class_addbang(zerox_class, (t_method)zerox_bang);
}