~ubuntu-branches/ubuntu/vivid/pd-ekext/vivid

1 by Hans-Christoph Steiner
Import upstream version 0.1.1
1
/* takes a map like 0 1 3 4 7 and a route. if the route and the input are  */
2
/* non-zero in the map, then the route is output from the object */
3
#include "m_pd.h"
4
#include <math.h>
5
#include <string.h>
6
#define MAXENTRIES 512
7
#define LASTENTRY 511
8
9
static t_class *valve_class;
10
11
typedef struct _valve
12
{
13
  t_object x_obj;
14
15
  t_atom *map;
16
  int bufsize;
17
18
  t_float input, router, max;
19
  t_outlet *routed, *notrouted;
20
21
  int flush;
22
} t_valve;
23
24
static void valve_map(t_valve *x, t_symbol *s, int n, t_atom *map)
25
{
26
  if (x->map) {
27
    freebytes(x->map, x->bufsize * sizeof(t_atom));
28
    x->map = 0;
29
    x->bufsize = 0;
30
  }
31
32
  x->map = copybytes(map, n * sizeof(t_atom));
33
  x->bufsize = n;
34
}
35
36
void valve_float(t_valve *x, t_floatarg fin)
37
{
38
  if (x->map) {
39
    int arg, arga, argb;
40
    arga = argb = 0;
41
    float testa, testb;
42
    testa = testb = 0;
43
    x->input = fin;
44
    arg = (int)x->input;
45
    testa = fin < 0 ? 0 : atom_getfloatarg(arg, x->bufsize, x->map);
46
    testb = x->router < 0 ? 0 : atom_getfloatarg(x->router, x->bufsize, x->map);
47
    arga = (int)testa;
48
    argb = (int)testb;
49
    if(arga && argb) 
50
      {
51
	outlet_float(x->routed, x->router);
52
      }
53
    else if (!argb)
54
      {
55
	outlet_float(x->notrouted, argb);
56
      }
57
    else if (!arga && argb)
58
      {
59
	outlet_float(x->notrouted, arga);
60
      }
61
  }
62
}
63
64
void valve_set(t_valve *x, t_floatarg fmap, t_floatarg fval)
65
{
66
  if(fmap < x->bufsize && fmap >= 0)
67
    {
68
      int imap = (int)fmap;
69
      SETFLOAT(&x->map[imap], fval);
70
      x->max = fmap > x->max ? fmap : x->max;
71
    }
72
}
73
74
void valve_clear(t_valve *x)
75
{
76
  if (x->map) {
77
    freebytes(x->map, x->bufsize * sizeof(t_atom));
78
    x->map = 0;
79
    x->bufsize = 0;
80
  }
81
}
82
83
void valve_debug(t_valve *x)
84
{
85
  int i;
86
  for(i=0;i<x->bufsize;i++) {
87
    float element = atom_getfloatarg(i, x->bufsize, x->map);
88
    post("element %d = %d", i, element);
89
  }
90
  post("max = %d", x->max);
91
}  
92
93
void *valve_new(t_floatarg f) 
94
{
95
  t_valve *x = (t_valve *)pd_new(valve_class);
96
  x->max = 0;
97
  int i;
98
  x->map = 0;
99
  x->bufsize = 0;
100
101
  floatinlet_new(&x->x_obj, &x->router);
102
103
  x->routed = outlet_new(&x->x_obj, &s_float);
104
  x->notrouted = outlet_new(&x->x_obj, &s_float);
105
  return (void *)x;
106
}
107
108
void valve_setup(void) 
109
{
110
  valve_class = class_new(gensym("valve"),
111
  (t_newmethod)valve_new,
112
  0, sizeof(t_valve),
113
  0, A_DEFFLOAT, 0);
114
  post("|¬~¬~¬~¬~¬~¬valve~¬~¬~¬~¬~¬~¬|");
115
  post("|~>^^^integer map router^^^<¬|");
116
  post("|¬~¬~¬Edward Kelly 2007~¬~¬~¬|");
117
118
  class_addfloat(valve_class, valve_float);
119
  class_addmethod(valve_class, (t_method)valve_set, gensym("set"), A_DEFFLOAT, A_DEFFLOAT, 0);
120
  class_addmethod(valve_class, (t_method)valve_map, gensym("map"), A_GIMME, 0);
121
  class_addmethod(valve_class, (t_method)valve_clear, gensym("clear"), A_DEFFLOAT, 0);
122
  class_addmethod(valve_class, (t_method)valve_debug, gensym("debug"), A_DEFFLOAT, 0);
123
}