Jamoma API  0.6.0.a19
Max.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularMax
4  *
5  * @brief Max Clock class
6  *
7  * @details
8  *
9  * @authors Théo de la Hogue
10  *
11  * @copyright ©2013, 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 #include "Max.h"
17 
18 #define thisTTClass Max
19 #define thisTTClassName "max"
20 #define thisTTClassTags "clock, max"
21 
22 #define thisClockVersion "0.1"
23 #define thisClockAuthor "Theo de la Hogue"
24 #define thisClockStretchable NO
25 
26 extern "C" TT_EXTENSION_EXPORT TTErr TTLoadJamomaExtension_Max(void)
27 {
28  TTFoundationInit();
29  Max::registerClass();
30  return kTTErrNone;
31 }
32 
33 TT_CLOCK_CONSTRUCTOR,
34 mGranularity(20.0)
35 {
36  TT_CLOCK_INITIALIZE
37 
38  clock = clock_new(this, (method)&MaxClockCallback); // install the max timer
39 
40  addAttribute(Granularity, kTypeFloat32);
41 }
42 
43 Max::~Max()
44 {
45  clock_unset(clock);
46  object_free((t_object*)clock);
47 }
48 
49 TTErr Max::getParameterNames(TTValue& value)
50 {
51  value.clear();
52  value.append(TTSymbol("granularity"));
53 
54  return kTTErrNone;
55 }
56 
57 TTErr Max::Go()
58 {
59  // do we need to ramp at all ?
60  if (mDuration <= 0.) {
61 
62  mRunning = NO;
63  mPaused = NO;
64  mPosition = 0.;
65  mDate = 0.;
66  (mCallback)(mBaton, mPosition, mDate);
67 
68  // notify each observers
69  sendNotification(TTSymbol("ClockRunningChanged"), mRunning);
70  sendNotification(TTSymbol("ClockTicked"), TTValue(mPosition, mDate));
71  }
72  else {
73 
74  // how many grains this time means ?
75  numGrains = mDuration / mGranularity;
76  stepSize = 1.0 / numGrains;
77 
78  mRunning = YES;
79  mPaused = NO;
80  mPosition = 0.;
81  mDate = 0.;
82  (mCallback)(mBaton, mPosition, mDate);
83 
84  // notify each observers
85  sendNotification(TTSymbol("ClockRunningChanged"), mRunning);
86  sendNotification(TTSymbol("ClockTicked"), TTValue(mPosition, mDate));
87 
88  // schedule first tick
89  setclock_fdelay(NULL, clock, mGranularity);
90  }
91 
92  return kTTErrNone;
93 }
94 
95 TTErr Max::Stop()
96 {
97  clock_unset(clock);
98  mRunning = NO;
99  mPaused = NO;
100 
101  // notify each observers
102  sendNotification(TTSymbol("ClockRunningChanged"), mRunning);
103 
104  return kTTErrNone;
105 }
106 
107 TTErr Max::Pause()
108 {
109  mPaused = YES;
110 
111  return kTTErrNone;
112 }
113 
114 TTErr Max::Resume()
115 {
116  mPaused = NO;
117 
118  return kTTErrNone;
119 }
120 
121 TTErr Max::Tick()
122 {
123  if (mRunning || !mPaused) {
124 
125 #ifdef SHEDULER_DEBUG
126  cout << "Max::Tick -- numGrain = " << numGrains << endl;
127 #endif
128 
129  // Go to the the next step
130  numGrains--;
131 
132  // Safety measure at end in case of accumulated numeric errors
133  if (numGrains <= 0.) {
134 
135  mRunning = NO;
136  mPaused = NO;
137  mPosition = 1.0;
138  mDate = mDuration;
139 
140  (mCallback)(mBaton, mPosition, mDate);
141 
142  // notify each observers
143  sendNotification(TTSymbol("ClockRunningChanged"), mRunning);
144  sendNotification(TTSymbol("ClockTicked"), TTValue(mPosition, mDate));
145  }
146  else {
147 
148  mPosition += stepSize;
149  mDate = mDuration * mPosition;
150 
151  (mCallback)(mBaton, mPosition, mDate);
152 
153  // notify each observers
154  sendNotification(TTSymbol("ClockTicked"), TTValue(mPosition, mDate));
155 
156  // Set the clock to fire again
157  setclock_fdelay(NULL, clock, mGranularity);
158  }
159  }
160 
161  return kTTErrNone;
162 }
163 
164 void MaxClockCallback(Max* aMaxClock)
165 {
166  aMaxClock->Tick();
167 }
Max Clock class.
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
void MaxClockCallback(Max *aMaxClock)
Called by the Max queue, and provided to the qelem – needs to have a C interface.
Definition: Max.cpp:164
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
32-bit floating point
Definition: TTBase.h:271
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34