aboutsummaryrefslogtreecommitdiff
path: root/zhzxh~/zhzxh~.c
blob: 59a80d24a0c9ecf61050e4a8a0644bba715b9501 (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
/* Copyright 2002 Benjamin R. Saylor
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "m_pd.h"

#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

static t_class *zhzxh_class;

typedef struct _zhzxh {
	t_object x_obj;
	t_float slope;
	t_float lastval;
	t_float srate;
	t_float f;
} t_zhzxh;

static t_int *zhzxh_perform(t_int *w)
{
	t_zhzxh *x = (t_zhzxh *)(w[1]);
	t_float *in = (t_float *)(w[2]);
	t_float *out = (t_float *)(w[3]);
	int n = (int)(w[4]);
	t_float f;

	t_float lastval = x->lastval;
	t_float delta = x->slope / x->srate;
	
	while (n--) {
		f = *(in++);
		if (lastval < f)
			*out = lastval + delta;
		else
			*out = lastval - delta;
		lastval = *(out++);
	}

	x->lastval = lastval;

	return (w+5);
}

static void zhzxh_dsp(t_zhzxh *x, t_signal **sp)
{
	dsp_add(zhzxh_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void *zhzxh_new(t_symbol *s, int argc, t_atom *argv)
{
	t_zhzxh *x = (t_zhzxh *)pd_new(zhzxh_class);
	x->slope = 1;
	x->lastval = 0;
	x->srate = sys_getsr();
	floatinlet_new(&x->x_obj, &x->slope);
	outlet_new(&x->x_obj, gensym("signal"));

	return (x);
}

void zhzxh_tilde_setup(void)
{
	zhzxh_class = class_new(gensym("zhzxh~"), (t_newmethod)zhzxh_new, 0, sizeof(t_zhzxh), 0, A_GIMME, 0);
	class_sethelpsymbol(zhzxh_class, gensym("help-zhzxh~.pd"));
	CLASS_MAINSIGNALIN(zhzxh_class, t_zhzxh, f);
	class_addmethod(zhzxh_class, (t_method)zhzxh_dsp, gensym("dsp"), 0);
}