Jamoma API  0.6.0.a19
Modular/library/source/TTRamp.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularLibrary
4  *
5  * @brief Ramp Class
6  *
7  * @details
8  *
9  * @authors Tim Place, Théo de la Hogue
10  *
11  * @copyright © 2008, Tim Place, Théo de la Hogue @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 #include "TTRamp.h"
18 
19 #define thisTTClass TTRamp
20 #define thisTTClassName "Ramp"
21 #define thisTTClassTags "ramp"
22 
23 TT_MODULAR_CONSTRUCTOR,
24 mDrive(kTTSymEmpty),
25 #ifndef TT_NO_DSP
26 mFunction(kTTSymEmpty),
27 #endif
28 mRampTime(0.),
29 mNumValues(0),
30 mCallback(NULL),
31 mBaton(NULL),
32 startValue(NULL),
33 targetValue(NULL),
34 currentValue(NULL)
35 {
36  TTAttributePtr anAttribute;
37 
38  if (arguments.size() == 2) {
39 
40  mCallback = TTRampCallback((TTPtr)arguments[0]);
41  mBaton = (TTPtr)arguments[1];
42  }
43 
45  registerAttribute(TTSymbol("driveLibrary"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getDriveLibrary, NULL);
46  registerAttribute(TTSymbol("driveParameters"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getDriveParameters, NULL);
47  registerAttribute(TTSymbol("driveParameterValue"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getDriveParameterValue, (TTSetterMethod)& TTRamp::setDriveParameterValue);
48 
49  this->findAttribute(TTSymbol("driveParameterValue"), &anAttribute);
50  anAttribute->sethidden(YES);
51 
52 #ifndef TT_NO_DSP
54  registerAttribute(TTSymbol("functionLibrary"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getFunctionLibrary, NULL);
55  registerAttribute(TTSymbol("functionParameters"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getFunctionParameters, NULL);
56  registerAttribute(TTSymbol("functionParameterValue"), kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getFunctionParameterValue, (TTSetterMethod)& TTRamp::setFunctionParameterValue);
57 
58  this->findAttribute(TTSymbol("functionParameterValue"), &anAttribute);
59  anAttribute->sethidden(YES);
60 #endif
61 
62  addAttribute(RampTime, kTypeFloat64);
63 
64  registerAttribute(kTTSym_running, kTypeLocalValue, NULL, (TTGetterMethod)& TTRamp::getRunning);
65 
70  addMessage(Tick);
71  addMessage(Stop);
72 
73  setAttributeValue(kTTSym_function, TTSymbol("linear"));
74 }
75 
76 TTRamp::~TTRamp()
77 {
78  if (mDriveUnit.valid())
79  mDriveUnit.send(kTTSym_Stop);
80 
81  delete [] currentValue;
82  delete [] targetValue;
83  delete [] startValue;
84 }
85 
86 TTErr TTRamp::getRunning(TTValue& value)
87 {
88  if (mDriveUnit.valid())
89  return mDriveUnit.get(kTTSym_running, value);
90  else
91  return kTTErrGeneric;
92 }
93 
94 TTErr TTRamp::setDrive(const TTValue& inputValue)
95 {
96  TTSymbol newDriveName;
97  TTValue args;
98 
99  newDriveName = inputValue[0];
100 
101  if (newDriveName == mDrive)
102  return kTTErrNone;
103 
104  mDrive = newDriveName;
105 
106  if (mDriveUnit.valid())
107  mDriveUnit = TTObject();
108 
109  if (TTClockLib::isClockNameAvailable(mDrive))
110  return kTTErrGeneric;
111 
112  if (mDrive != kTTSymEmpty && mDrive != kTTSym_none) {
113 
115  args.append((TTPtr)this); // we have to store this as a pointer
116 
117  mDriveUnit = TTObject(mDrive, args);
118  return kTTErrNone;
119  }
120 
121  return kTTErrGeneric;
122 }
123 
124 TTErr TTRamp::getDriveLibrary(TTValue& value)
125 {
127  return kTTErrNone;
128 }
129 
130 TTErr TTRamp::getDriveParameters(TTValue& value)
131 {
132  if (mDriveUnit.valid()) {
133  mDriveUnit.get("parameterNames", value);
134  return kTTErrNone;
135  }
136 
137  return kTTErrGeneric;
138 }
139 
140 TTErr TTRamp::getDriveParameterValue(TTValue& value)
141 {
142  TTSymbol parameterName;
143 
144  if (value.size() == 1) {
145 
146  if (value[0].type() == kTypeSymbol) {
147 
148  parameterName = value[0];
149 
150  if (mDriveUnit.valid())
151  return mDriveUnit.get(parameterName, value);
152  }
153  }
154 
155  return kTTErrGeneric;
156 }
157 
158 TTErr TTRamp::setDriveParameterValue(const TTValue& value)
159 {
160  TTSymbol parameterName;
161  TTValue newValue;
162 
163  if (value.size() > 1) {
164 
165  if (value[0].type() == kTypeSymbol) {
166 
167  parameterName = value[0];
168  newValue.copyFrom(value, 1);
169 
170  return mDriveUnit.set(parameterName, newValue);
171  }
172  }
173 
174  return kTTErrGeneric;
175 }
176 #ifndef TT_NO_DSP
177 TTErr TTRamp::setFunction(const TTValue& inputValue)
178 {
179  TTSymbol newFunctionName;
180 
181  newFunctionName = inputValue[0];
182 
183  if (newFunctionName == TTSymbol("none"))
184  newFunctionName = TTSymbol("linear");
185 
186  if (newFunctionName == mFunction)
187  return kTTErrNone;
188 
189  mFunction = newFunctionName;
190 
191  if (mFunctionUnit.valid())
193 
194  mFunctionUnit = TTObject(mFunction, 1); // for 1 channel only
195  return kTTErrNone;
196 }
197 
198 TTErr TTRamp::getFunctionLibrary(TTValue& value)
199 {
200  TTObject::GetRegisteredClassNamesForTags(value, kTTSym_function);
201  return kTTErrNone;
202 }
203 
204 TTErr TTRamp::getFunctionParameters(TTValue& value)
205 {
206  TTValue names;
207  TTSymbol aName;
208 
209  if (mFunctionUnit.valid()) {
210 
211  mFunctionUnit.attributes(names);
212 
213  value.clear();
214  for (TTUInt32 i = 0; i < names.size(); i++) {
215 
216  aName = names[i];
217 
218  if (aName == kTTSym_bypass || aName == kTTSym_mute || aName == kTTSym_maxNumChannels || aName == kTTSym_sampleRate)
219  continue; // don't publish these parameters
220 
221  value.append(aName);
222  }
223 
224  return kTTErrNone;
225  }
226 
227  return kTTErrGeneric;
228 }
229 
230 TTErr TTRamp::getFunctionParameterValue(TTValue& value)
231 {
232  TTSymbol parameterName;
233 
234  if (mFunctionUnit.valid()) {
235 
236  if (value.size() == 1) {
237 
238  if (value[0].type() == kTypeSymbol) {
239 
240  parameterName = value[0];
241 
242  return mFunctionUnit.get(parameterName, value);
243  }
244  }
245  }
246 
247  return kTTErrGeneric;
248 }
249 
250 TTErr TTRamp::setFunctionParameterValue(const TTValue& value)
251 {
252  TTSymbol parameterName;
253  TTValue newValue;
254 
255  if (mFunctionUnit.valid()) {
256 
257  if (value.size() > 1) {
258 
259  if (value[0].type() == kTypeSymbol) {
260 
261  parameterName = value[0];
262  newValue.copyFrom(value, 1);
263 
264  return mFunctionUnit.set(parameterName, newValue);
265  }
266  }
267  }
268 
269  return kTTErrGeneric;
270 }
271 #endif
272 TTErr TTRamp::Set(const TTValue& inputValue, TTValue& outputValue)
273 {
274  TTUInt32 i;
275 
276  if (mDriveUnit.valid()) {
277 
278  mDriveUnit.send(kTTSym_Stop);
279 
280  mNumValues = inputValue.size();
281 
282  startValue = new TTFloat64[mNumValues];
283 
284  for (i = 0; i < mNumValues; i++)
285  startValue[i] = TTFloat64(inputValue[i]);
286 
287  return kTTErrNone;
288  }
289 
290  return kTTErrGeneric;
291 }
292 
293 TTErr TTRamp::Target(const TTValue& inputValue, TTValue& outputValue)
294 {
295  TTUInt32 i;
296 
297  if (mDriveUnit.valid()) {
298 
299  mDriveUnit.send(kTTSym_Stop);
300 
301  if (mNumValues == inputValue.size()) {
302 
303  currentValue = new TTFloat64[mNumValues];
304  targetValue = new TTFloat64[mNumValues];
305 
306  for (i = 0; i < mNumValues; i++) {
307  currentValue[i] = startValue[i];
308  targetValue[i] = TTFloat64(inputValue[i]);
309  }
310 
311  return kTTErrNone;
312  }
313  }
314 
315  return kTTErrGeneric;
316 }
317 
318 TTErr TTRamp::Go(const TTValue& inputValue, TTValue& outputValue)
319 {
320  if (mDriveUnit.valid()) {
321 
322  if (inputValue.size() == 1) {
323 
324  mRampTime = inputValue[0];
325 
326  mDriveUnit.set("duration", mRampTime);
327 
328  if (startValue && currentValue && targetValue)
329  return mDriveUnit.send(kTTSym_Go);
330  }
331  }
332 
333  return kTTErrGeneric;
334 }
335 
336 TTErr TTRamp::Slide(const TTValue& inputValue, TTValue& outputValue)
337 {
338  if (inputValue.size() == 1) {
339 
340  if (inputValue[0].type() == kTypeFloat64) {
341 
342  if (startValue && currentValue && targetValue) {
343  TTRampDriveCallback(TTPtr(this), inputValue[0], 0.);
344  return kTTErrNone;
345  }
346  }
347  }
348 
349  return kTTErrGeneric;
350 }
351 
352 TTErr TTRamp::Tick()
353 {
354  if (mDriveUnit.valid())
355  return mDriveUnit.send(kTTSym_Tick);
356  else
357  return kTTErrGeneric;
358 }
359 
361 {
362  mRampTime = 0.;
363 
364  if (mDriveUnit.valid())
365  return mDriveUnit.send(kTTSym_Stop);
366  else
367  return kTTErrGeneric;
368 }
369 
370 void TTRampDriveCallback(TTPtr object, TTFloat64 position, TTFloat64 date)
371 {
372  TTRampPtr aRamp = (TTRampPtr)object;
373  TTUInt32 i;
374  TTFloat64 mapped;
375  TTFloat64 *current = aRamp->currentValue;
376  TTFloat64 *target = aRamp->targetValue;
377  TTFloat64 *start = aRamp->startValue;
378 
379 #ifndef TT_NO_DSP
380  if (aRamp->mFunctionUnit.valid()) {
381 
382  TTAudioObjectBasePtr(aRamp->mFunctionUnit.instance())->calculate(position, mapped);
383 
384  for (i = 0; i < aRamp->mNumValues; i++)
385  current[i] = start[i] + ((target[i] - start[i]) * mapped);
386 
387  (aRamp->mCallback)(aRamp->mBaton, aRamp->mNumValues, current);
388  }
389 
390 #else
391 
392  mapped = position;
393 
394  for (i = 0; i < aRamp->mNumValues; i++)
395  current[i] = start[i] + ((target[i] - start[i]) * mapped);
396 
397  (aRamp->mCallback)(aRamp->mBaton, aRamp->mNumValues, current);
398 #endif
399 }
TTErr(TTObjectBase::* TTSetterMethod)(const TTAttribute &attribute, const TTValue &value)
A type that can be used to store a pointer to a message for an object.
Definition: TTObjectBase.h:78
void TTRampDriveCallback(TTPtr object, TTFloat64 position, TTFloat64 date)
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
TTRamp generates a linear traversal from 0.0 to 1.0 over the course of a specified time period...
TTObject mFunctionUnit
The actual function unit object defined by mFunction // TODO: Jamomacore #294 : Ease the access of th...
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
void copyFrom(const TTValue &obj, TTUInt16 index)
Copy a value starting from an index until the last element.
Definition: TTValue.h:147
friend void TTMODULAR_EXPORT TTRampDriveCallback(TTPtr object, TTFloat64 position, TTFloat64 date)
< It is public in order to be able to extend clock or function unit parameters as attributes of anoth...
Create and use Jamoma object instances.
Definition: TTObject.h:29
size_type size() const noexcept
Return the number of elements.
static TTErr GetRegisteredClassNamesForTags(TTValue &classNames, const TTValue &searchTags)
Query TTEnvironment for names of all registered TTObjectBase classes that share specific tags...
Definition: TTObject.cpp:78
This class represents a single attribute, as used by the TTObjectBase class.
Definition: TTAttribute.h:79
Symbol type.
Definition: TTBase.h:282
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
This is a special type used by TTAttribute to indicate that a value is a TTValue and is locally maint...
Definition: TTBase.h:286
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.
TTAudioObjectBase * TTAudioObjectBasePtr
Pointer to a TTAudioObjectBase.
64-bit floating point
Definition: TTBase.h:272
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 Stop()
Stop the current ramp.
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
void attributes(TTValue &returnedAttributeNames) const
Return a list of names of the available attributes.
Definition: TTObject.cpp:111
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTErr sethidden(const TTValue &newHiddenFlag)
Set the hidden flag for the attribute, determining if this attribute is private/invisible to the outs...
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
TTObjectBase * instance() const
Return a direct pointer to the internal instance.
Definition: TTObject.cpp:105
No Error.
Definition: TTBase.h:343
TTBoolean valid() const
Determine if the object contained by this TTObject is truly ready for use.
Definition: TTObject.cpp:179
TTErr(TTObjectBase::* TTGetterMethod)(const TTAttribute &attribute, TTValue &value)
A type that can be used to store a pointer to a message for an object.
Definition: TTObjectBase.h:73
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTObject mDriveUnit
< It is public in order to be able to extend clock or function unit parameters as attributes of anoth...