aboutsummaryrefslogtreecommitdiff
path: root/src/iterate.c
blob: 8fee7acd753c00bfce7040a6ebb2c4d154811065 (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
#include "m_pd.h"

static t_class *iterate_class;

typedef struct _iterate {
  t_object  x_obj;

  t_float start;
  t_float step;
  t_int iterating;
  t_outlet* outlet1;
  //t_outlet* outlet2;
} t_iterate;


static void iterate_bang(t_iterate *x)
{
  //x->i_count = x->i_down;
  x->iterating = 0;
  
}


static void iterate_start(t_iterate *x, t_float f) {
	
	x->start = f;
}


static void iterate_step(t_iterate *x, t_float f) {
	
	x->step = f;
}

static void iterate_float(t_iterate *x, t_float f)
{
  
  if ( f < 0 ) f = 0;
  
  int i;
  t_float value = x->start;
  x->iterating = 1;
  
  for ( i = 0; i < f; i++)   {
	  if ( !(x->iterating) ) break;
	  
	  outlet_float(x->outlet1, value);
	  value = value + x->step;
  }
  
}


static void iterate_free(t_iterate *x)
{
		
    
}

static void *iterate_new(t_symbol *s, int argc, t_atom *argv)
{
  t_iterate *x = (t_iterate *)pd_new(iterate_class);
  
  x->iterating = 0;
  if ( argc >= 2) {
	x->start =  atom_getfloat(argv);
	x->step =  atom_getfloat(argv+1);
  } else if ( argc == 1) {
	x->start =  atom_getfloat(argv);
	x->step = 1;
  } else {
	x->start = 0;
	x->step = 1;
  }
  
  floatinlet_new(&x->x_obj, &x->start);
  floatinlet_new(&x->x_obj, &x->step);
  x->outlet1 = outlet_new(&x->x_obj, &s_float);
  //x->outlet2 = outlet_new(&x->x_obj, &s_float);


  return (void *)x;
}

void iterate_setup(void) {
  iterate_class = class_new(gensym("iterate"),
        (t_newmethod)iterate_new,
        (t_method)iterate_free, sizeof(t_iterate),
        CLASS_DEFAULT, 
        A_GIMME, 0);

  class_addbang  (iterate_class, iterate_bang);
  class_addfloat (iterate_class, iterate_float);
  
  class_addmethod(iterate_class, 
        (t_method)iterate_start, gensym("start"),
        A_DEFFLOAT, 0);
		
  class_addmethod(iterate_class, 
        (t_method)iterate_step, gensym("step"),
        A_DEFFLOAT, 0);
  
  //class_addlist (iterate_class, iterate_list);
  
}