aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/triangle.c
blob: 703e5bab62ea98ab0b0872f9217395124d481caf (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
/* 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"

#define TRIANGLE_DEFPHASE  0.5
#define TRIANGLE_DEFLO    -1.0
#define TRIANGLE_DEFHI     1.0

typedef struct _triangle
{
    t_sic      x_sic;
    float      x_low;
    float      x_range;
} t_triangle;

static t_class *triangle_class;

static void triangle_lo(t_triangle *x, t_floatarg f)
{
    float high = x->x_low + x->x_range;
    x->x_low = f;
    x->x_range = high - x->x_low;
}

static void triangle_hi(t_triangle *x, t_floatarg f)
{
    x->x_range = f - x->x_low;
}

/* LATER optimize */
static t_int *triangle_perform(t_int *w)
{
    t_triangle *x = (t_triangle *)(w[1]);
    int nblock = (int)(w[2]);
    t_float *in1 = (t_float *)(w[3]);
    t_float *in2 = (t_float *)(w[4]);
    t_float *out = (t_float *)(w[5]);
    float low = x->x_low;
    float range = x->x_range;
    while (nblock--)
    {
	float ph = *in1++;
	float peakph = *in2++;
	/* CHECKED ph wrapped */
	if (ph < 0.)
	    ph -= (int)ph - 1.;
	else if (ph > 1.)
	    ph -= (int)ph;
	/* CHECKED peakph clipped  */
	if (peakph < 0.)
	    peakph = 0.;
	else if (peakph > 1.)
	    peakph = 1.;

	if (ph < peakph)
	    ph /= peakph;
	else if (peakph < 1.)
	    ph = (1. - ph) / (1. - peakph);
	else
	    ph = 0.;
	*out++ = low + ph * range;
    }
    return (w + 6);
}

static void triangle_dsp(t_triangle *x, t_signal **sp)
{
    dsp_add(triangle_perform, 5, x, sp[0]->s_n,
	    sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec);
}

static void *triangle_new(t_symbol *s, int ac, t_atom *av)
{
    t_triangle *x = (t_triangle *)pd_new(triangle_class);
    sic_inlet((t_sic *)x, 1, TRIANGLE_DEFPHASE, 0, ac, av);
    outlet_new((t_object *)x, &s_signal);
    x->x_low = TRIANGLE_DEFLO;
    x->x_range = (TRIANGLE_DEFHI - TRIANGLE_DEFLO);
    return (x);
}

void triangle_tilde_setup(void)
{
    triangle_class = class_new(gensym("triangle~"),
			       (t_newmethod)triangle_new, 0,
			       sizeof(t_triangle), 0, A_GIMME, 0);
    sic_setup(triangle_class, triangle_dsp, SIC_FLOATTOSIGNAL);
    class_addmethod(triangle_class, (t_method)triangle_lo,
		    gensym("lo"), A_DEFFLOAT, 0);  /* CHECKED */
    class_addmethod(triangle_class, (t_method)triangle_hi,
		    gensym("hi"), A_DEFFLOAT, 0);  /* CHECKED */
}