Jamoma API  0.6.0.a19
j.ramp.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternals
4  *
5  * @brief j.ramp : Ramp values using Jamoma's RampLib library
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright © 2006 by Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
18 
19 enum outlets {
20  k_outlet_value = 0,
21  k_outlet_dumpout,
22  num_outlets
23 };
24 
25 // This is used to store extra data
26 typedef struct extra {
27  TTValuePtr currentValue;
28 } t_extra;
29 #define EXTRA ((t_extra*)x->extra)
30 
31 // Prototypes
32 
33 /** Wrap the TTRamp class as a Max object.
34  @param c The class to be wrapped
35  @see WrappedInputClass_new, WrappedInputClass_free
36  */
37 void WrapTTRampClass(WrappedClassPtr c);
38 
39 /** Wrapper for the j.ramp constructor class, called when an instance is created.
40  @param self Pointer to this object.
41  @param argc The number of arguments passed to the object.
42  @param argv Pointer to an array of atoms passed to the object.
43  @see WrappedInputClass_free
44  */
45 void WrappedRampClass_new(TTPtr self, long argc, t_atom* argv);
46 
47 /** Wrapper for the j.ramp destructor class, called when an instance is deleted.
48  @param self Pointer to this object.
49  @see WrappedInputClass_new
50  */
51 void WrappedRampClass_free(TTPtr self);
52 
53 /** Method for Assistance Messages */
54 void ramp_assist(TTPtr self, void *b, long msg, long arg, char *dst);
55 
56 /** Get a new value now. */
57 void ramp_bang(TTPtr self);
58 
59 /** Method for int input, instantly updates current value of the object. */
60 void ramp_int(TTPtr self, long n);
61 
62 /** Method for float input, instantly updates current value of the object. */
63 void ramp_float(TTPtr self, double f);
64 
65 /** Set current value while surpressing new value(s from being output. */
66 void ramp_set(TTPtr self, t_symbol *msg, long argc, t_atom *argv);
67 
68 /** Stop ongoing ramp */
69 void ramp_stop(TTPtr self);
70 
71 /** Method for list input <value(s), "ramp", ramptime> */
72 void ramp_list(TTPtr self, t_symbol *msg, long argc, t_atom *argv);
73 
74 /** Return the ramped values out */
75 void ramp_return_value(TTPtr self, t_symbol *msg, long argc, t_atom *argv);
76 
77 /** Get or set value of scheduler parameter */
78 void ramp_schedulerParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv);
79 
80 /** Get or set value of function parameter */
81 void ramp_functionParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv);
82 
83 
84 #pragma mark -
85 #pragma mark main
86 /************************************************************************************/
87 // Class Definition
88 
89 int C74_EXPORT main(void)
90 {
91  ModularSpec *spec = new ModularSpec;
92  spec->_wrap = &WrapTTRampClass;
93  spec->_new = &WrappedRampClass_new;
94  spec->_free = &WrappedRampClass_free;
95  spec->_any = NULL;
96 
97  return wrapTTModularClassAsMaxClass(kTTSym_Ramp, "j.ramp", NULL, spec);
98 }
99 
100 void WrapTTRampClass(WrappedClassPtr c)
101 {
102  class_addmethod(c->maxClass, (method)ramp_assist, "assist", A_CANT, 0);
103 
104  class_addmethod(c->maxClass, (method)ramp_return_value, "return_value", A_CANT, 0);
105 
106  class_addmethod(c->maxClass, (method)ramp_bang, "bang", 0);
107  class_addmethod(c->maxClass, (method)ramp_int, "int", A_DEFLONG, 0);
108  class_addmethod(c->maxClass, (method)ramp_float, "float", A_DEFFLOAT, 0);
109  class_addmethod(c->maxClass, (method)ramp_list, "list", A_GIMME, 0);
110 
111  class_addmethod(c->maxClass, (method)ramp_set, "set", A_GIMME, 0);
112  class_addmethod(c->maxClass, (method)ramp_stop, "stop", 0);
113 
114  class_addmethod(c->maxClass, (method)ramp_schedulerParameter, "drive/parameter/value",A_GIMME, 0);
115  class_addmethod(c->maxClass, (method)ramp_functionParameter, "function/parameter/value", A_GIMME, 0);
116 }
117 
118 
119 #pragma mark -
120 #pragma mark life cycle
121 /************************************************************************************/
122 // Object Life
123 
124 void WrappedRampClass_new(TTPtr self, long argc, t_atom* argv)
125 {
127 
128  // create the wrapped TTRamp instance
129  jamoma_ramp_create((t_object*)x, x->wrappedObject);
130 
131  // create an outlet for ramped value
132  x->outlets = (TTHandle)sysmem_newptr(sizeof(TTPtr));
133  x->outlets[k_outlet_value] = outlet_new(x, 0L);
134 
135  // Set default drive
136  x->wrappedObject.set("drive", TTSymbol("max"));
137 
138  // Prepare extra data
139  x->extra = (t_extra*)malloc(sizeof(t_extra));
140  EXTRA->currentValue = new TTValue(0.);
141 
142  // Now set specified attributes, if any
143  attr_args_process(x, argc, argv);
144 }
145 
147 {
149 
150  delete EXTRA->currentValue;
151  EXTRA->currentValue = NULL;
152 
153  free(EXTRA);
154 }
155 
156 #pragma mark -
157 #pragma mark methods
158 /************************************************************************************/
159 // Methods bound to input/inlets
160 
161 // Method for Assistance Messages
162 void ramp_assist(TTPtr self, void *b, long msg, long arg, char *dst)
163 {
164  if (msg==1) // Inlet
165  strcpy(dst, "input");
166  else { // Outlets
167  switch(arg) {
168  case k_outlet_value:
169  strcpy(dst, "ramping value");
170  break;
171  case k_outlet_dumpout:
172  strcpy(dst, "dumpout");
173  break;
174  }
175  }
176 }
177 
178 // BANG -- fire an output -- useful for the async unit
179 void ramp_bang(TTPtr self)
180 {
182 
183  x->wrappedObject.send(kTTSym_Tick);
184 }
185 
186 
187 // INT INPUT
188 void ramp_int(TTPtr self, long value)
189 {
191  TTValue none;
192 
193  x->wrappedObject.send("Set", TTFloat64(value), none);
194 
195  *(EXTRA->currentValue) = TTFloat64(value);
196  outlet_float(x->outlets[k_outlet_value], value);
197 }
198 
199 
200 // FLOAT INPUT
201 void ramp_float(TTPtr self, double value)
202 {
204  TTValue none;
205 
206  x->wrappedObject.send("Set", TTFloat64(value), none);
207 
208  *(EXTRA->currentValue) = TTFloat64(value);
209  outlet_float(x->outlets[k_outlet_value], value);
210 }
211 
212 
213 // SET LIST INPUT
214 void ramp_set(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
215 {
217  TTValue v, none;
218 
219  jamoma_ttvalue_from_Atom(v, _sym_nothing, argc, argv);
220 
221  x->wrappedObject.send("Set", v, none);
222  *(EXTRA->currentValue) = v;
223 }
224 
225 
226 // STOP RAMP
227 void ramp_stop(TTPtr self)
228 {
230 
231  x->wrappedObject.send("Stop");
232 }
233 
234 
235 // LIST INPUT <value, ramptime>
236 void ramp_list(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
237 {
239  short i;
240  short ramp_keyword_index = -1;
241  TTValue v, none;
242 
243  // parse the incoming atom
244  for (i = 0; i < argc; i++) {
245 
246  // 'ramp' key word detection
247  if (argv[i].a_type == A_SYM) {
248 
249  if (atom_getsym(argv+i) == gensym("ramp")) {
250  ramp_keyword_index = i;
251  break;
252  }
253  }
254 
255  v.append(TTFloat64(atom_getfloat(argv+i)));
256  }
257 
258  if (ramp_keyword_index == -1) { // just a list w/o ramp information
259 
260  x->wrappedObject.send("Set", v, none);
261  *(EXTRA->currentValue) = v;
262  outlet_anything(x->outlets[k_outlet_value], _sym_list, argc, argv);
263  }
264  else {
265 
266  if (argc != (ramp_keyword_index + 2)) { // "ramp" is not the second last list member
267 
268  error("invalid syntax -- wrong number of args following the 'ramp' keyword");
269  return;
270  }
271  else { // "ramp" is the second last list member, so we start ramping
272 
273  if (EXTRA->currentValue->size() == v.size())
274  x->wrappedObject.send("Set", *(EXTRA->currentValue), none);
275 
276  x->wrappedObject.send("Target", v, none);
277  x->wrappedObject.send("Go", TTFloat64(atom_getfloat(argv+argc-1)), none);
278  }
279  }
280 }
281 
282 void ramp_return_value(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
283 {
285 
286  // keep current value in memory
287  if (EXTRA->currentValue != NULL)
288  jamoma_ttvalue_from_Atom(*(EXTRA->currentValue), msg, argc, argv);
289 
290  outlet_anything(x->outlets[k_outlet_value], msg, argc, argv);
291 }
292 
293 #pragma mark -
294 #pragma mark attributes of attributes
295 /************************************************************************************/
296 // Attributes of attributes
297 void ramp_schedulerParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
298 {
300  TTValue v;
301  long ac = 0;
302  t_atom *av = NULL;
303 
304  if (!argc) {
305  error("j.ramp: not enough arguments to get or set scheduler/parameter/value");
306  return;
307  }
308 
309  // 1 argument : get the value
310  if (argc == 1) {
311 
312  v = TTSymbol(atom_getsym(argv)->s_name);
313  x->wrappedObject.get("driveParameterValue", v);
314 
315  v.prepend(TTSymbol(atom_getsym(argv)->s_name));
316  jamoma_ttvalue_to_Atom(v, &ac, &av);
317 
318  object_obex_dumpout(x, gensym("drive/parameter/value"), ac, av);
319  return;
320  }
321 
322  // 2 or more arguments : set the value
323  jamoma_ttvalue_from_Atom(v, _sym_nothing, argc, argv);
324 
325  x->wrappedObject.set("driveParameterValue", v);
326 }
327 
328 void ramp_functionParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
329 {
331  TTValue v;
332  long ac = 0;
333  t_atom *av = NULL;
334 
335  if (!argc) {
336  error("j.ramp: not enough arguments to get or set function/parameter/value");
337  return;
338  }
339 
340  // 1 argument : get the value
341  if (argc == 1) {
342 
343  v = TTSymbol(atom_getsym(argv)->s_name);
344  x->wrappedObject.get("functionParameterValue", v);
345 
346  v.prepend(TTSymbol(atom_getsym(argv)->s_name));
347  jamoma_ttvalue_to_Atom(v, &ac, &av);
348 
349  object_obex_dumpout(x, gensym("function/parameter/value"), ac, av);
350  return;
351  }
352 
353  // 2 or more arguments : set the value
354  jamoma_ttvalue_from_Atom(v, _sym_nothing, argc, argv);
355 
356  x->wrappedObject.set("functionParameterValue", v);
357 }
void ramp_set(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Set current value while surpressing new value(s from being output.
Definition: j.ramp.cpp:214
void WrappedRampClass_new(TTPtr self, long argc, t_atom *argv)
Wrapper for the j.ramp constructor class, called when an instance is created.
Definition: j.ramp.cpp:124
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
TTHandle outlets
an array of outlet
TTErr wrapTTModularClassAsMaxClass(TTSymbol &ttblueClassName, const char *maxClassName, WrappedClassPtr *c, ModularSpec *specificities)
Wrap a Jamoma class as a Max class.
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.ramp.cpp:89
void ramp_schedulerParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Get or set value of scheduler parameter.
Definition: j.ramp.cpp:297
size_type size() const noexcept
Return the number of elements.
Data structure for storing extra data.
void * extra
used to keep very specific things
void ramp_stop(TTPtr self)
Stop ongoing ramp.
Definition: j.ramp.cpp:227
void ramp_return_value(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Return the ramped values out.
Definition: j.ramp.cpp:282
void WrapTTRampClass(WrappedClassPtr c)
Wrap the TTRamp class as a Max object.
Definition: j.ramp.cpp:100
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
void prepend(const TTValue &aValueToPrepend)
Insert another TTValue before the first element.
Definition: TTValue.h:162
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTErr get(const TTSymbol aName, T &aReturnedValue) const
Get an attribute value for an object.
void JAMOMA_EXPORT jamoma_ttvalue_to_Atom(const TTValue &v, long *argc, t_atom **argv)
Make an Atom array from a TTValue.
TTErr set(const TTSymbol aName, T aValue)
Set an attribute value for an object.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr JAMOMA_EXPORT jamoma_ramp_create(t_object *x, TTObject &returnedRamp)
Create a TTRamp object.
TTObject wrappedObject
The instance of the Jamoma object we are wrapping.
void JAMOMA_EXPORT jamoma_ttvalue_from_Atom(TTValue &v, t_symbol *msg, long argc, const t_atom *argv)
Make a TTValue from Atom array.
void ramp_functionParameter(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Get or set value of function parameter.
Definition: j.ramp.cpp:328
Data Structure for this object.
Wraps Jamoma Core classes as objects for Max/MSP.
void ramp_int(TTPtr self, long n)
Method for int input, instantly updates current value of the object.
Definition: j.ramp.cpp:188
void ramp_list(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Method for list input
Definition: j.ramp.cpp:236
void ramp_bang(TTPtr self)
Get a new value now.
Definition: j.ramp.cpp:179
void ramp_float(TTPtr self, double f)
Method for float input, instantly updates current value of the object.
Definition: j.ramp.cpp:201
void ramp_assist(TTPtr self, void *b, long msg, long arg, char *dst)
Method for Assistance Messages.
Definition: j.ramp.cpp:162
void WrappedRampClass_free(TTPtr self)
Wrapper for the j.ramp destructor class, called when an instance is deleted.
Definition: j.ramp.cpp:146
WrappedModularInstance * WrappedModularInstancePtr
Pointer to a wrapped instance of our object.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34