aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/edge.c
blob: b206f6ba6a9c8eab0b9890b815727cf49365ab85 (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
/* 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 _edge
{
    t_sic      x_sic;
    t_float    x_last;
    int        x_zeroleft;
    int        x_zerohit;
    t_outlet  *x_out2;
    t_clock   *x_clock;
} t_edge;

static t_class *edge_class;

static void edge_tick(t_edge *x)
{
    /* CHECKED both may fire simultaneously */
    if (x->x_zeroleft)
    {
	outlet_bang(((t_object *)x)->ob_outlet);
	x->x_zeroleft = 0;
    }
    if (x->x_zerohit)
    {
	outlet_bang(x->x_out2);
	x->x_zerohit = 0;
    }
}

static t_int *edge_perform(t_int *w)
{
    t_edge *x = (t_edge *)(w[1]); 
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    t_float last = x->x_last;
    while (nblock--)
    {
	float f = *in++;
	if (last == 0.)
	{
	    if (f != 0.)
	    {
		x->x_zeroleft = 1;
		if (x->x_zerohit)
		{
		    clock_delay(x->x_clock, 0);
		    x->x_last = in[nblock - 1];
		    return (w + 4);
		}
	    }
	}
	else
	{
	    if (f == 0.)
	    {
		x->x_zerohit = 1;
		if (x->x_zeroleft)
		{
		    clock_delay(x->x_clock, 0);
		    x->x_last = in[nblock - 1];
		    return (w + 4);
		}
	    }
	}
	last = f;
    }
    if (x->x_zeroleft || x->x_zerohit) clock_delay(x->x_clock, 0);
    x->x_last = last;
    return (w + 4);
}

static void edge_dsp(t_edge *x, t_signal **sp)
{
    dsp_add(edge_perform, 3, x, sp[0]->s_n, sp[0]->s_vec);
}

static void edge_free(t_edge *x)
{
    if (x->x_clock) clock_free(x->x_clock);
}

static void *edge_new(t_floatarg f)
{
    t_edge *x = (t_edge *)pd_new(edge_class);
    x->x_last = 0.;  /* CHECKED fires at startup */
    x->x_zeroleft = x->x_zerohit = 0;
    outlet_new((t_object *)x, &s_bang);
    x->x_out2 = outlet_new((t_object *)x, &s_bang);
    x->x_clock = clock_new(x, (t_method)edge_tick);
    return (x);
}

void edge_tilde_setup(void)
{
    edge_class = class_new(gensym("edge~"),
			   (t_newmethod)edge_new,
			   (t_method)edge_free,
			   sizeof(t_edge), 0,
			   A_DEFFLOAT, 0);
    sic_setup(edge_class, edge_dsp, SIC_FLOATTOSIGNAL);
}