aboutsummaryrefslogtreecommitdiff
path: root/itov.c
blob: c2c027b3560d5a63eba76c555d85b507662f52ee (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
#include "defines.h"

/*--------------- itov ---------------*/


static t_class *itov_class;

typedef struct _itov
{
    t_object x_obj;
	t_outlet *f_out1;
	t_outlet *f_out2;
	t_outlet *f_out3;
	t_float m_lo;
	t_float m_hi;
	t_float m_scale;
	int m_nbins;
} t_itov;


static void itov_perform_float(t_itov *x, t_float j)
{
	t_float i;
	j-=1.0f;
	j=(j>0)?(j<x->m_nbins?j:x->m_nbins-1):0; // limit without IF

//	j=(int)((f-x->m_lo)*x->m_scale);
	i=(j/x->m_scale)-x->m_lo;

	outlet_float(x->f_out2, i);

}

static void itov_perform_list(t_itov *x, t_symbol *s, int argc, t_atom *argv)
{


}

static void itov_set(t_itov *x, t_float lo, t_float hi, t_float nbins)
{
	if (nbins<1)
	{
		nbins=1;
		logpost(x, 2, "[itov] minimum number of bins is 1");
	}
	if (hi<=lo)
	{
		logpost(x, 2, "[itov] higher bound (%g) must be higher than lower bound (%g)",
                hi, lo);	
		hi=lo+1.0f;
	}

	x->m_hi=hi;
	x->m_lo=lo;
	x->m_nbins=(int)nbins;
	x->m_scale=(t_float)x->m_nbins/(hi-lo);
}

static void *itov_new(t_float lo, t_float hi, t_float nbins)
{
	t_itov *x=(t_itov *)pd_new(itov_class);
	x->f_out1=outlet_new(&x->x_obj, gensym("float"));
	x->f_out2=outlet_new(&x->x_obj, gensym("float"));
	x->f_out3=outlet_new(&x->x_obj, gensym("float"));

	x->m_nbins=0;
	itov_set(x, lo, hi, nbins);
	return (void *)x;
}

static void itov_free(t_itov *x)
{
}

void itov_setup(void)
{
    itov_class = class_new(gensym("itov"),
    	(t_newmethod)itov_new, (t_method)itov_free,
		sizeof(t_itov), 
		CLASS_DEFAULT,
	    A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT,0);

//    class_addlist(itov_class, (t_method)itov_perform_list);
    class_addfloat(itov_class, (t_method)itov_perform_float);
}