aboutsummaryrefslogtreecommitdiff
path: root/tabreadl.c
blob: e669bd838ecebdb9ce68591716ef038431dba190 (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
/*
Copyright (C) 2002 Antoine Rousseau 

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  

*/

#include "m_pd.h"
#include "math.h"

/* ---------- tabreadl: control, linear interpolating ------------------------ */

static t_class *tabreadl_class;

typedef struct _tabreadl
{
    t_object x_obj;
    t_symbol *x_arrayname;
} t_tabreadl;

static void tabreadl_float(t_tabreadl *x, t_float f)
{
    t_garray *a;
    int npoints;
    t_float *vec;

    if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
    	error("%s: no such array", x->x_arrayname->s_name);
    else if (!garray_getfloatarray(a, &npoints, &vec))
    	error("%s: bad template for tabreadl", x->x_arrayname->s_name);
    else
    {
    	int n ;
		float r,v;

    	if (f < 0) f = 0;
    	else if (f >= npoints) f = npoints - 1;
		n=f;
		if(npoints>1){
			r=f-n;
			v=vec[n]*(1-r)+vec[n+1]*r;
			outlet_float(x->x_obj.ob_outlet, v );
		}
    	else 
			outlet_float(x->x_obj.ob_outlet, (npoints ? vec[n] : 0));
    }
}

static void tabreadl_set(t_tabreadl *x, t_symbol *s)
{
    x->x_arrayname = s;
}

static void *tabreadl_new(t_symbol *s)
{
    t_tabreadl *x = (t_tabreadl *)pd_new(tabreadl_class);
    x->x_arrayname = s;
    outlet_new(&x->x_obj, &s_float);
    return (x);
}

void tabreadl_setup(void)
{
    tabreadl_class = class_new(gensym("tabreadl"), (t_newmethod)tabreadl_new,
    	0, sizeof(t_tabreadl), 0, A_DEFSYM, 0);
    class_addfloat(tabreadl_class, (t_method)tabreadl_float);
    class_addmethod(tabreadl_class, (t_method)tabreadl_set, gensym("set"),
    	A_SYMBOL, 0);

}