Jamoma API  0.6.0.a19
DSP/extensions/GeneratorLib/source/TTRamp.cpp
1 /*
2  * TTBlue Ramp Generator
3  * Copyright © 2008, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTRamp.h"
10 
11 #define thisTTClass TTRamp
12 #define thisTTClassName "ramp"
13 #define thisTTClassTags "dspGeneratorLib, audio, generator"
14 
15 
16 TT_AUDIO_CONSTRUCTOR
17 , attrMode(TT("vector")), attrCurrentValue(0), attrDestinationValue(0), step(0), direction(0)
18 {
19  registerAttribute(TT("rampTime"), kTypeFloat64, &attrRampTime, (TTSetterMethod)&TTRamp::setRampTime);
20  registerAttribute(TT("startValue"), kTypeFloat64, &attrCurrentValue);
21  registerAttribute(TT("destinationValue"), kTypeFloat64, &attrDestinationValue);
22  registerAttribute(TT("mode"), kTypeSymbol, &attrMode, (TTSetterMethod)&TTRamp::setMode);
23 
24  addMessage(Stop);
25  addMessage(RampTimeInSamples);
26 
27  setAttributeValue(TT("mode"), TT("vector"));
28 }
29 
30 
31 TTRamp::~TTRamp()
32 {
33  ;
34 }
35 
36 
38 {
39  rampSamples = newValue;
40  if (rampSamples == 0) {
41  step = 0;
42  attrCurrentValue = attrDestinationValue;
43  direction = 0;
44  }
45  else {
46  attrRampTime = 1000.0 * (rampSamples / TTFloat32(sr));
47  setStep();
48  }
49  setupProcess();
50  return kTTErrNone;
51 }
52 
53 
55 {
56  attrRampTime = newValue;
58  step = 0;
59  attrCurrentValue = attrDestinationValue;
60  direction = 0;
61  }
62  else {
63  rampSamples = long((attrRampTime * 0.001) * sr);
64  setStep();
65  }
66  setupProcess();
67  return kTTErrNone;
68 }
69 
70 
71 TTErr TTRamp::setMode(const TTValue& newValue)
72 {
73  attrMode = newValue;
74  setupProcess();
75  return kTTErrNone;
76 }
77 
78 
80 {
81  step = 0;
82  return kTTErrNone;
83 }
84 
85 
87 {
88  if ((attrMode == TT("sample")) && (direction == kUP))
90  else if ((attrMode == TT("sample")) && (direction == kDOWN))
92  else if ((attrMode == TT("vector")) && (direction == kUP))
94  else
96 }
97 
98 
100 {
101  step = (attrDestinationValue - attrCurrentValue) / double(rampSamples - 1);
102  direction = (step < 0);
103 }
104 
105 
107 {
108  TTAudioSignal& out = outputs->getSignal(0);
109  TTSampleValue *outSample;
110  TTChannelCount numchannels = out.getNumChannelsAsInt();
111  TTChannelCount channel;
112 
113  for (channel=0; channel<numchannels; channel++) {
114  outSample = out.mSampleVectors[channel];
115  *outSample = attrCurrentValue;
116 
117  if (step) {
118  attrCurrentValue += (step * out.getVectorSizeAsInt());
119  if (attrCurrentValue <= attrDestinationValue) {
120  step = 0;
121  attrCurrentValue = attrDestinationValue; // clamp
122  }
123  }
124 
125  }
126  return kTTErrNone;
127 }
128 
129 
131 {
132  TTAudioSignal& out = outputs->getSignal(0);
133  TTSampleValue *outSample;
134  TTChannelCount numchannels = out.getNumChannelsAsInt();
135  TTChannelCount channel;
136 
137  for (channel=0; channel<numchannels; channel++) {
138  outSample = out.mSampleVectors[channel];
139  *outSample = attrCurrentValue;
140 
141  if (step) {
142  attrCurrentValue += (step * out.getVectorSizeAsInt());
143  if (attrCurrentValue >= attrDestinationValue) {
144  step = 0;
145  attrCurrentValue = attrDestinationValue; // clamp
146  }
147  }
148 
149  }
150  return kTTErrNone;
151 }
152 
153 
155 {
156  TTAudioSignal& out = outputs->getSignal(0);
157  TTSampleValue *outSample;
158  TTChannelCount numchannels = out.getNumChannelsAsInt();
159  TTChannelCount channel;
160  TTUInt16 vs;
161 
162  for (channel=0; channel<numchannels; channel++) {
163  vs = out.getVectorSizeAsInt();
164  outSample = out.mSampleVectors[channel];
165  while (vs--) {
166  *outSample++ = attrCurrentValue;
167 
168  if (step) {
169  attrCurrentValue += step;
170  if (attrCurrentValue <= attrDestinationValue) {
171  step = 0;
172  attrCurrentValue = attrDestinationValue; // clamp
173  }
174  }
175 
176  }
177  }
178  return kTTErrNone;
179 }
180 
181 
183 {
184  TTAudioSignal& out = outputs->getSignal(0);
185  TTSampleValue *outSample;
186  TTChannelCount numchannels = out.getNumChannelsAsInt();
187  TTChannelCount channel;
188  TTUInt16 vs;
189 
190  for (channel=0; channel<numchannels; channel++) {
191  vs = out.getVectorSizeAsInt();
192  outSample = out.mSampleVectors[channel];
193  while (vs--) {
194  *outSample++ = attrCurrentValue;
195 
196  if (step) {
197  attrCurrentValue += step;
198  if (attrCurrentValue >= attrDestinationValue) {
199  step = 0;
200  attrCurrentValue = attrDestinationValue; // clamp
201  }
202  }
203 
204  }
205  }
206  return kTTErrNone;
207 }
208 
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
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr setRampTime(const TTValue &newValue)
Trigger a ramp with a time set in milliseconds.
TTSymbol attrMode
mode: sample_accurate or vector_accurate
TTFOUNDATION_EXPORT const TTFloat64 kTTAntiDenormalValue
Constant used by the ttantidenormal function.
Definition: TTBase.cpp:30
TTErr processVectorAccurateUp(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method – This method is special! It only sets the first sample of the vector and does not us...
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTErr RampTimeInSamples(const TTValue &newValue)
Trigger a ramp with a time set in samples.
Symbol type.
Definition: TTBase.h:282
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
64-bit floating point
Definition: TTBase.h:272
TTFloat64 attrRampTime
ramp time in milliseconds
TTErr processSampleAccurateDown(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method.
float TTFloat32
32 bit floating point number
Definition: TTBase.h:187
TTErr Stop()
Stop the current ramp.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
A simple container for an array of TTAudioSignal pointers.
void setStep()
Internal method called when the ramp is triggered (by setting the rampTime)
TTErr processVectorAccurateDown(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method – This method is special! It only sets the first sample of the vector and does not us...
TTBoolean direction
0 = ramp up, 1 = ramp down
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr processSampleAccurateUp(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method.
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
No Error.
Definition: TTBase.h:343
TTErr setMode(const TTValue &value)
Setter for the mode attribute.
void setupProcess()
Internal method for setting which process method to use.
TTFloat64 TTSampleValue
A value representing a single audio sample.
Definition: TTBase.h:230
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTUInt32 rampSamples
ramp time in samples
TTUInt32 sr
Current sample rate being used by this object.