Jamoma API  0.6.0.a19
TTWindowFunction.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspWindowFunctionLib
4  *
5  * @brief Generalized Window Function Wrapper for Jamoma DSP
6  *
7  * @details
8  *
9  * @authors Tim Place, Nathan Wolek, Trond Lossius,
10  *
11  * @copyright Copyright © 2010 by Timothy 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 #include "TTWindowFunction.h"
17 
18 #define thisTTClass WindowFunction
19 #define thisTTClassName "WindowFunction"
20 #define thisTTClassTags "dspWindowFunctionLib, audio, processor"
21 
22 
23 TT_AUDIO_CONSTRUCTOR,
24  mFunctionObject(NULL),
25  mNumPoints(8192),
26  mPadding(0)
27 {
32 
33  addMessageWithArguments(getFunctions);
34  addMessageWithArguments(setParameter);
35 
36  setAttributeValue(TT("function"), TT("rectangular"));
37  setAttributeValue(TT("mode"), TT("lookup"));
38 }
39 
40 
41 WindowFunction::~WindowFunction()
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 WindowFunction::setParameter(const TTValue& aParameterValueForTheFunction, TTValue&)
60 {
61  TTErr err;
62 
63  if (aParameterValueForTheFunction.size() < 2)
65  else {
66  TTSymbol parameterName = aParameterValueForTheFunction;
67  TTValue v;
68 
69  v.copyFrom(aParameterValueForTheFunction, 1);
70  err = mFunctionObject->setAttributeValue(parameterName, v);
71  if (!err)
72  fill();
73  }
74 
75  return err;
76 }
77 
78 
80 {
81  mLookupTable.assign(mNumPoints, 0.0);
82  if (mFunctionObject) {
83  TTInt32 numPoints = mNumPoints-(mPadding*2);
84 
85  TTLimitMin<TTInt32>(numPoints, 0);
86  for (TTInt32 i=0; i<numPoints; i++)
88  }
89  else
90  mLookupTable.assign(mNumPoints, 0.0);
91 
92  return kTTErrNone;
93 }
94 
95 
97 {
98  return doSetNumPoints(numPoints);
99 }
100 
101 
103 {
104  mNumPoints = numPoints;
105  mLookupTable.resize(mNumPoints);
106  return fill();
107 }
108 
109 
111 {
112  mPadding = padding;
113  return fill();
114 }
115 
116 
118 {
119  mMode = mode;
120  if (mMode == TT("apply")) {
123  }
124  else if (mMode == TT("generate")) {
127  }
128  else {
131  }
132  return kTTErrNone;
133 }
134 
135 
136 TTErr WindowFunction::getFunctions(const TTValue&, TTValue& listOfWindowTypesToReturn)
137 {
138  return TTObject::GetRegisteredClassNamesForTags(listOfWindowTypesToReturn, TT("window"));
139 }
140 
141 
142 #if 0
143 #pragma mark -
144 #pragma mark Process Routines
145 #endif
146 
147 
149 {
150  return mFunctionObject->calculate(x, y);
151 }
152 
153 
155 {
156  y = mLookupTable[TTClip(x, 0.0, 1.0) * mNumPoints];
157  return kTTErrNone;
158 }
159 
160 
162 {
163  return mFunctionObject->process(inputs, outputs);
164 }
165 
166 
168 {
169  TT_WRAP_CALCULATE_METHOD(lookupValue)
170 }
171 
172 
174 {
175  TTAudioSignal& in = inputs->getSignal(0);
176  TTAudioSignal& out = outputs->getSignal(0);
177  TTUInt16 vs = in.getVectorSizeAsInt();
178  TTSampleValuePtr inSample;
179  TTSampleValuePtr outSample;
180  TTChannelCount numChannels = TTAudioSignal::getMinChannelCount(in, out);
181  TTChannelCount channel;
182 
183  // In 'apply' mode we automatically update the lookup table size to the vector size
184  // This is slow, but hopefully only happens once (if ever)
185  if (vs != mNumPoints)
186  doSetNumPoints(vs);
187 
188  for (channel=0; channel<numChannels; channel++) {
189  inSample = in.mSampleVectors[channel];
190  outSample = out.mSampleVectors[channel];
191 
192  for (TTUInt32 i=0; i<vs; i++)
193  *outSample++ = (*inSample++) * mLookupTable[i];
194  }
195  return kTTErrNone;
196 }
197 
TTUInt32 mPadding
for generating the lookup table - (e.g. padded welch window padded by 16 zeros on both sides) ...
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr getFunctions(const TTValue &, TTValue &listOfWindowTypesToReturn)
Return a list of all the available window functions.
TTAudioObjectBasePtr mFunctionObject
The actual window function object for mFunction.
TTErr processApply(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Apply window function to the inputs signal using window function values that are calculated on the fl...
void copyFrom(const TTValue &obj, TTUInt16 index)
Copy a value starting from an index until the last element.
Definition: TTValue.h:147
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTSymbol mFunction
Name of the window function to use.
size_type size() const noexcept
Return the number of elements.
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
The wrong number of values were passed to a method or attribute.
Definition: TTBase.h:350
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTErr processLookup(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Apply window function to the inputs signal using window function values from the stored lookup table...
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
Symbol type.
Definition: TTBase.h:282
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr doSetNumPoints(const TTUInt32 numPoints)
Set the lookup table size.
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
Generalized Window Function Wrapper for Jamoma DSP.
TTSampleVector mLookupTable
Cached table of size mNumPoints.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr setNumPoints(const TTValue &numPoints)
Set the lookup table size.
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt data)
Calculate a single window function value y = f(x).
TTErr setPadding(const TTValue &padding)
Zero-padding applied to lookup table.
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
TTErr setMode(const TTValue &mode)
Set the process method.
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.
TTSymbol mMode
Options: 'generate', 'lookup', 'apply'.
TTErr lookupValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt data)
Lookup a single function value y = f(x) from the stored table of window function values.
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
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTUInt32 mNumPoints
Number of Points in the lookup table.
#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
No Error.
Definition: TTBase.h:343
TTErr setParameter(const TTValue &aParameterValueForTheFunction, TTValue &)
Set an attribute of the internal window object (e.g.
TTErr fill()
Internal method for filling the lookup table.
TTErr setFunction(const TTValue &function)
Choose the window function.
TTErr processGenerate(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Generate a window function curve by means of calculations.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34