aboutsummaryrefslogtreecommitdiff
path: root/tripleLine.c
blob: 1817f1b8ef66c4812cdd689982b31d4ffb59614b (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
/*
 * Copyright (c) 1997-1999 Mark Danks.
 * 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"

/* -------------------------- tripleLine ------------------------------ */

/* instance structure */

static t_class *tripleLine_class;

typedef struct _tripleLine
{
	t_object    x_obj;			/* obligatory object header */
	t_clock		*clock;
	float		grain;
	float		destTime;
	float		curTime;
	float		curVal[3];
	float		destVal[3];
	float		stepVal[3];
	t_outlet    *t_out1;	    /* the outlet */
} t_tripleLine;

static void tripleTick(t_tripleLine *x)
{
	t_atom argv[3];
	int i;
	x->curTime += x->grain;
	if (x->curTime >= x->destTime)
	{
		for (i = 0; i < 3; i++)
		{
			x->curVal[i] = x->destVal[i];
			SETFLOAT((&argv[i]), x->curVal[i]);
		}
	}
	else
	{
		for (i = 0; i < 3; i++)
		{
			x->curVal[i] += x->stepVal[i];
			SETFLOAT((&argv[i]), x->curVal[i]);
		}
	   	clock_delay(x->clock, x->grain);
	}

	outlet_list(x->t_out1, &s_list, 3, argv);
}

static void setLineParameters(t_tripleLine *x, t_symbol *s, int argc, t_atom *argv)
{
    float numCounts;
	int i;
    t_atom newargv[3];
	float time = x->destTime;
	
	clock_unset(x->clock);
	
	if (argc == 4) time = atom_getfloat(&argv[3]);
	else if (argc != 3)
	{
		error("tripleLine: requires 3 or 4 args");
		return;
	}

	x->destTime = time;
	numCounts = time / x->grain;
	if (x->destTime <= 0.)
	{
		x->curTime = 0;
		for (i = 0; i < 3; i++)
		{
			x->curVal[i] = x->destVal[i] = atom_getfloat(&argv[i]);
			SETFLOAT((&newargv[i]), x->curVal[i]);
		}
		outlet_list(x->t_out1, &s_list, 3, newargv);
		return;
	}
	else
	{
		for (i = 0; i < 3; i++)
		{
			x->destVal[i] = atom_getfloat(&argv[i]);
			x->stepVal[i] = (x->destVal[i] - x->curVal[i]) / numCounts;
			SETFLOAT((&newargv[i]), x->curVal[i]);
		}
	}
	x->curTime = 0;

   	clock_delay(x->clock, x->grain);
	outlet_list(x->t_out1, &s_list, 3, newargv);
}

static void tripleLine_free(t_tripleLine *x)
{
    clock_free(x->clock);
}

static void *tripleLine_new(t_floatarg time, t_floatarg grain) /* init vals in struc */
{
	int i;
    t_tripleLine *x = (t_tripleLine *)pd_new(tripleLine_class);
	x->curTime = 0;
	x->destTime = time;
    x->clock = clock_new(x, (t_method)tripleTick);
	for (i = 0; i < 3; i++)
	{
		x->curVal[i] = x->destVal[i] = x->stepVal[i] = 0.;
	}
	if (grain <= 0) grain = 50;
	x->grain = (float)grain;

    x->t_out1 = outlet_new(&x->x_obj, 0);
    return(x);
}

void tripleLine_setup(void)
{
    tripleLine_class = class_new(gensym("tripleLine"), (t_newmethod)tripleLine_new,
			(t_method)tripleLine_free, sizeof(t_tripleLine), 0, A_DEFFLOAT, A_DEFFLOAT, 0);

    class_addmethod(tripleLine_class, (t_method)setLineParameters,
    	    &s_list, A_GIMME, A_NULL); 

#if PD_MINOR_VERSION < 37 
	class_sethelpsymbol(tripleLine_class, gensym("tripleLine-help.pd"));
#endif
}