Jamoma API  0.6.0.a19
TTFunction.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFunctionLib
4  *
5  * @brief #TTFunction is a generalized function wrapper for Jamoma DSP
6  *
7  * @details All functions will map incoming values in the normalised range [0,1] to output values in the normalised range [0,1].
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2010 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 
17 #include "TTFunction.h"
18 
19 #define thisTTClass TTFunction
20 #define thisTTClassName "function"
21 #define thisTTClassTags "dspFunctionLib, audio, processor"
22 
23 
24 TT_AUDIO_CONSTRUCTOR,
25  mFunctionObject(NULL),
26  mNumPoints(8192),
27  mPadding(0)
28 {
33 
34  addMessageWithArguments(getFunctions);
35 
36  setAttributeValue(TT("function"), TT("linear"));
37  setAttributeValue(TT("mode"), TT("generate"));
38 }
39 
40 
41 TTFunction::~TTFunction()
42 {
43  delete mFunctionObject;
44 }
45 
46 
48 {
49  TTErr err;
50 
51  mFunction = function;
53  if (!err)
54  err = fill();
55  return err;
56 }
57 
58 
59 TTErr TTFunction::fill()
60 {
61  mLookupTable.assign(mNumPoints, 0.0);
62  if (mFunctionObject) {
63  TTInt32 numPoints = mNumPoints-(mPadding*2);
64 
65  TTLimitMin<TTInt32>(numPoints, 0);
66  for (TTInt32 i=0; i<numPoints; i++)
68  }
69  else
70  mLookupTable.assign(mNumPoints, 0.0);
71 
72  return kTTErrNone;
73 }
74 
75 
77 {
78  return doSetNumPoints(numPoints);
79 }
80 
81 
82 TTErr TTFunction::doSetNumPoints(const TTUInt32 numPoints)
83 {
84  mNumPoints = numPoints;
85  mLookupTable.resize(mNumPoints);
86  return fill();
87 }
88 
89 
91 {
92  mPadding = padding;
93  return fill();
94 }
95 
96 
98 {
99  mMode = mode;
100  if (mMode == TT("apply")) {
101  setProcessMethod(processApply);
102  setCalculateMethod(lookupValue);
103  }
104  else if (mMode == TT("generate")) {
107  }
108  else {
109  setProcessMethod(processLookup);
110  setCalculateMethod(lookupValue);
111  }
112  return kTTErrNone;
113 }
114 
115 
116 TTErr TTFunction::getFunctions(const TTValue&, TTValue& listOfWindowTypesToReturn)
117 {
118  return TTObject::GetRegisteredClassNamesForTags(listOfWindowTypesToReturn, TT("function"));
119 }
120 
121 
122 #if 0
123 #pragma mark -
124 #pragma mark Process Routines
125 #endif
126 
127 
129 {
130  return mFunctionObject->calculate(x, y);
131 }
132 
133 
134 inline TTErr TTFunction::lookupValue(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt data)
135 {
136  y = mLookupTable[TTClip(x, 0.0, 1.0) * mNumPoints];
137  return kTTErrNone;
138 }
139 
140 
142 {
143  return mFunctionObject->process(inputs, outputs);
144 }
145 
146 
147 TTErr TTFunction::processLookup(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
148 {
149  TT_WRAP_CALCULATE_METHOD(lookupValue)
150 }
151 
152 
153 TTErr TTFunction::processApply(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
154 {
155  TTAudioSignal& in = inputs->getSignal(0);
156  TTAudioSignal& out = outputs->getSignal(0);
157  TTUInt16 vs = in.getVectorSizeAsInt();
158  TTSampleValuePtr inSample;
159  TTSampleValuePtr outSample;
160  TTChannelCount numChannels = TTAudioSignal::getMinChannelCount(in, out);
161  TTChannelCount channel;
162 
163  // In 'apply' mode we automatically update the lookup table size to the vector size
164  // This is slow, but hopefully only happens once (if ever)
165  if (vs != mNumPoints)
166  doSetNumPoints(vs);
167 
168  for (channel=0; channel<numChannels; channel++) {
169  inSample = in.mSampleVectors[channel];
170  outSample = out.mSampleVectors[channel];
171 
172  for (TTUInt32 i=0; i<vs; i++)
173  *outSample++ = (*inSample++) * mLookupTable[i];
174  }
175  return kTTErrNone;
176 }
177 
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr setPadding(const TTValue &padding)
zero-padding applied to lookup table
Definition: TTFunction.cpp:90
TTErr setNumPoints(const TTValue &numPoints)
set lookup table size
Definition: TTFunction.cpp:76
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt data)
y = f(x) for a single value
Definition: TTFunction.cpp:128
TTErr calculate(const TTFloat64 &x, TTFloat64 &y)
Calculate a single sample of output for a single sample of input.
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
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTSymbol mMode
Options: 'generate', 'lookup', 'apply'.
Definition: TTFunction.h:34
Symbol type.
Definition: TTBase.h:282
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTAudioObjectBasePtr mFunctionObject
The actual window function object for mFunction.
Definition: TTFunction.h:31
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTUInt32 mPadding
for generating the lookup table - (e.g. padded welch window padded by 16 zeros on both sides) ...
Definition: TTFunction.h:35
TTSampleVector mLookupTable
Cached table of size mNumPoints.
Definition: TTFunction.h:33
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
static TTChannelCount getMinChannelCount(const TTAudioSignal &signal1, const TTAudioSignal &signal2)
Use this class method to determine the least number of channels the two signals have in common...
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTErr process(TTAudioSignal &in, TTAudioSignal &out)
Process the input signal, resulting in an output signal.
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
TTUInt32 mNumPoints
Number of Points in the lookup table.
Definition: TTFunction.h:32
A simple container for an array of TTAudioSignal pointers.
TTErr setMode(const TTValue &mode)
set process method
Definition: TTFunction.cpp:97
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr processGenerate(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
A standard audio processing method as used by Jamoma DSP objects.
Definition: TTFunction.cpp:141
TTSymbol mFunction
Name of the window function to use.
Definition: TTFunction.h:30
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTFunction is a generalized function wrapper for Jamoma DSP
TTErr setFunction(const TTValue &function)
choose the window function
Definition: TTFunction.cpp:47
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
32-bit unsigned integer, range is 0 through 4,294,967,295.
Definition: TTBase.h:278
TTErr getFunctions(const TTValue &, TTValue &listOfWindowTypesToReturn)
return a list of all the available window shapes
Definition: TTFunction.cpp:116
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34