Jamoma API  0.6.0.a19
j.delta.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternals
4  *
5  * @brief j.delta - External for Jamoma: j.delta - Calculate 1st/2nd order differences and velocity.
6  *
7  * @details This object replaces tl.delta, tl.delta2 and tl.velocity from the tl.objects distribution.
8  *
9  * @authors Trond Lossius, Tim Place, Jan Schacher, François-Eudes Chanfrault
10  *
11  * @copyright Copyright © 2006, Trond Lossius @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 /*
17  <---MAXREF
18 
19  /maxref/object/name
20  j.delta
21 
22  /maxref/object/digest
23  Calculate difference between incomming numbers
24 
25  /maxref/object/description
26  <o>j.delta</o> calculates the difference between the last incomming value x[n]
27  and the previous value received:<br/>
28  delta = x[n]-x[n-1]
29 
30 
31 
32  /maxref/metadata/author
33  Trond Lossius, Tim Place, Jan Schacher, François-Eudes Chanfrault
34 
35  /maxref/metadata/tag
36  Jamoma Max Math
37 
38 
39 
40  /maxref/inletlist/0
41  Calculate Difference, int, float
42 
43  /maxref/outletlist/0
44  Delta Value
45 
46  /maxref/outletlist/1
47  Dumpout
48 
49 
50 
51  /maxref/argument/
52  None
53 
54 
55 
56  /maxref/message/float
57  Caculate difference using between this and the previous number received.
58 
59  /maxref/message/int
60  Converted to float
61 
62 
63 
64  /maxref/example/image
65  j.delta.gif
66 
67  /maxref/example/caption
68  Calculate difference between the last two numbers received.
69 
70 
71 
72  /maxref/seealso/object/name
73  j.delta2
74 
75  /maxref/seealso/object/description
76  Calculate the 2nd order difference of incomming numbers.
77 
78  /maxref/seealso/object/name
79  j.velocity
80 
81  /maxref/seealso/object/description
82  Calculate velocity (rate of change per second) for incomming values.
83 
84  MAXREF--->
85 */
86 
87 #include "JamomaForMax.h"
88 
89 #define nonzero(x) ((x > 0) ? x : 1.)
90 
91 typedef struct _delta{ ///< Data structure for this object
92  t_object ob; ///< Must always be the first field; used by Max
93  float x0; ///< Most recerntly received value
94  float x1; ///< Previous value
95  float x2; ///< 2nd last value received
96  float delta; ///< 1st order differential
97  float delta2; ///< 2nd order differential
98  long lasttime; ///< Time code for previous value received
99  char clearflag; ///< Flag indicating that history has been cleared
100  void *outlets[3]; ///< Pointer to outlet. Need one for each outlet
101 } t_delta;
102 
103 // Prototypes for methods: need a method for each incoming message
104 void *delta_new(void);
105 void delta_bang(t_delta *x);
106 void delta_int(t_delta *x, long n);
107 void delta_float(t_delta *x, double f);
108 void delta_clear(t_delta *x);
109 void delta_set(t_delta *x, Symbol *s, long ac, t_atom *setval);
110 void delta_assist(t_delta *x, void *b, long msg, long arg, char *dst);
111 
112 // Globals
113 t_class *this_class; // Required. Global pointing to this class
114 
115 
116 
117 /************************************************************************************/
118 // Main() Function
119 
120 int JAMOMA_EXPORT_MAXOBJ main(void)
121 {
122  t_class *c;
123 
124  common_symbols_init();
125 
126  // Define our class
127  c = class_new("j.delta",(method)delta_new, (method)0L, sizeof(t_delta), (method)0L, 0L, 0);
128 
129  // Make methods accessible for our class:
130  class_addmethod(c, (method)delta_bang, "bang", A_CANT, 0);
131  class_addmethod(c, (method)delta_int, "int", A_LONG, 0);
132  class_addmethod(c, (method)delta_float, "float", A_FLOAT, 0);
133  class_addmethod(c, (method)delta_assist, "assist", A_CANT, 0);
134  class_addmethod(c, (method)delta_set, "set", A_GIMME, 0);
135  class_addmethod(c, (method)delta_clear, "clear", 0);
136  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
137 
138  // Finalize our class
139  class_register(CLASS_BOX, c);
140  this_class = c;
141  return 0;
142 }
143 
144 #pragma mark -
145 #pragma mark Object life
146 /************************************************************************************/
147 // Object Life
148 
149 void *delta_new(void)
150 {
151  t_delta *x;
152  x = (t_delta *)object_alloc(this_class); // create the new instance and return a pointer to it
153  if (x) {
154  // create inlets and outlets
155  object_obex_store((void *)x, _sym_dumpout, (object *)outlet_new(x,NULL)); // dumpout
156  x->outlets[2] = floatout(x); // velocity
157  x->outlets[1] = floatout(x); // 2nd order difference
158  x->outlets[0] = floatout(x); // 1st order difference
159 
160  delta_clear(x); // initilaize instance
161  }
162  return (x);
163 }
164 
165 
166 #pragma mark -
167 #pragma mark methods
168 /************************************************************************************/
169 // Methods bound to input/inlets
170 
171 
172 // BANG input
173 void delta_bang(t_delta *x)
174 {
175  long thistime;
176  float velocity;
177 
178  thistime = gettime();
179  velocity = (1000 * (x->delta) ) / (nonzero(thistime - x->lasttime));
180  x->lasttime = thistime;
181 
182  outlet_float(x->outlets[2], velocity);
183  outlet_float(x->outlets[1], x->delta2);
184  outlet_float(x->outlets[0], x->delta);
185 }
186 
187 
188 // INT input
189 void delta_int(t_delta *x, long n)
190 {
191  double f;
192 
193  f = (double)n;
194  delta_float(x,f);
195 }
196 
197 
198 // FLOAT input
199 void delta_float(t_delta *x, double f)
200 {
201  if (x->clearflag) {
202  x->x1 = f;
203  x->x0 = f;
204  x->clearflag = 0;
205  }
206  x->x2 = x->x1;
207  x->x1 = x->x0;
208  x->x0 = f;
209 
210  x->delta = x->x0 - x->x1;
211  x->delta2 = x->x0 - 2*x->x1 + x->x2;
212 
213  delta_bang(x);
214 }
215 
216 
217 // SET input
218 void delta_set(t_delta *x, t_symbol *s, long argc, t_atom *argv)
219 {
220  float f;
221 
222  if (argc)
223  f = atom_getfloat(argv);
224  if (x->clearflag) {
225  x->x1 = f;
226  x->x0 = f;
227  x->clearflag = 0;
228  }
229  x->x2 = x->x1;
230  x->x1 = x->x0;
231  x->x0 = f;
232  x->lasttime = gettime();
233 
234  x->delta = x->x0 - x->x1;
235  x->delta2 = x->x0 - 2*x->x1 + x->x2;
236 }
237 
238 
239 // CLEAR input
240 void delta_clear(t_delta *x)
241 {
242  x->clearflag = 1;
243  x->delta = 0;
244  x->delta2 = 0;
245  x->lasttime = gettime();
246 }
247 
248 
249 // Method for Assistance Messages
250 void delta_assist(t_delta *x, void *b, long msg, long arg, char *dst) // Display assistance messages
251 {
252  if (msg==1)
253  {
254  switch(arg)
255  {
256  case 0:
257  strcpy(dst, "(int/float) function value");
258  break;
259  }
260  }
261  else if (msg==2)
262  {
263  switch(arg)
264  {
265  case 0:
266  strcpy(dst, "(float) 1st order difference");
267  break;
268  case 1:
269  strcpy(dst, "(float) 2nd order difference");
270  break;
271  case 2:
272  strcpy(dst, "(float) velocity");
273  break;
274  case 3:
275  strcpy(dst, "dumpout");
276  break;
277  }
278  }
279 }
Various utilities for interfacing with Max that are not specific to JamomaModular as such...
int JAMOMA_EXPORT_MAXOBJ main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.delta.cpp:120
t_class * this_class
Required. Global pointing to this class.
Definition: j.envexp.cpp:108