AAX SDK 2.6.1
Avid Audio Extensions Development Kit
Loading...
Searching...
No Matches
AAX_CPieceWiseLinearTaperDelegate.h
Go to the documentation of this file.
1/*================================================================================================*/
2/*
3 *
4 * Copyright 2014-2017, 2019, 2023 Avid Technology, Inc.
5 * All rights reserved.
6 *
7 * CONFIDENTIAL: this document contains confidential information of Avid. Do
8 * not disclose to any third party. Use of the information contained in this
9 * document is subject to an Avid SDK license.
10 *
11 */
12
19/*================================================================================================*/
20
21
22#ifndef AAX_CPIECEWISELINEARTAPERDELEGATE_H
23#define AAX_CPIECEWISELINEARTAPERDELEGATE_H
24
25#include "AAX_ITaperDelegate.h"
26#include "AAX.h" //for types
27
28#include <cmath> //for floor()
29
30
54template <typename T, int32_t RealPrecision=100>
56{
57public:
66 AAX_CPieceWiseLinearTaperDelegate(const double* normalizedValues, const T* realValues, int32_t numValues);
67
68 AAX_CPieceWiseLinearTaperDelegate(const AAX_CPieceWiseLinearTaperDelegate& other); //Explicit copy constructor because there are internal arrays.
70
71 //Virtual AAX_ITaperDelegate Overrides
73 T GetMinimumValue() const AAX_OVERRIDE { return mMinValue; }
74 T GetMaximumValue() const AAX_OVERRIDE { return mMaxValue; }
75 T ConstrainRealValue(T value) const AAX_OVERRIDE;
76 T NormalizedToReal(double normalizedValue) const AAX_OVERRIDE;
77 double RealToNormalized(T realValue) const AAX_OVERRIDE;
78
79protected:
80 T Round(double iValue) const;
81
82private:
83 double* mNormalizedValues;
84 T* mRealValues;
85 int32_t mNumValues;
86 T mMinValue; //Really just an optimization
87 T mMaxValue; //Really just an optimization
88};
89
90template <typename T, int32_t RealPrecision>
92{
93 if (RealPrecision > 0)
94 return static_cast<T>(floor(iValue * RealPrecision + 0.5) / RealPrecision);
95 else
96 return static_cast<T>(iValue);
97}
98
99template <typename T, int32_t RealPrecision>
100AAX_CPieceWiseLinearTaperDelegate<T, RealPrecision>::AAX_CPieceWiseLinearTaperDelegate(const double* normalizedValues, const T* realValues, int32_t numValues) : AAX_ITaperDelegate<T>(),
101 mNormalizedValues(0),
102 mRealValues(0),
103 mNumValues(0),
104 mMinValue(0),
105 mMaxValue(0)
106{
107 mNormalizedValues = new double[numValues];
108 mRealValues = new T[numValues];
109 mNumValues = numValues;
110
111 if (numValues > 0)
112 {
113 mMaxValue = realValues[0];
114 mMinValue = realValues[0];
115 }
116 for (int32_t i=0; i< numValues; i++)
117 {
118 mNormalizedValues[i] = normalizedValues[i];
119 mRealValues[i] = realValues[i];
120 if (mRealValues[i] > mMaxValue)
121 mMaxValue = mRealValues[i];
122 if (mRealValues[i] < mMinValue)
123 mMinValue = mRealValues[i];
124 }
125}
126
127template <typename T, int32_t RealPrecision>
129 mNormalizedValues(0),
130 mRealValues(0),
131 mNumValues(0),
132 mMinValue(0),
133 mMaxValue(0)
134{
135 mNormalizedValues = new double[other.mNumValues];
136 mRealValues = new T[other.mNumValues];
137 mNumValues = other.mNumValues;
138 mMaxValue = other.mMaxValue;
139 mMinValue = other.mMinValue;
140 for (int32_t i=0; i< mNumValues; i++)
141 {
142 mNormalizedValues[i] = other.mNormalizedValues[i];
143 mRealValues[i] = other.mRealValues[i];
144 }
145}
146
147template <typename T, int32_t RealPrecision>
149{
150 mNumValues = 0;
151 delete [] mNormalizedValues;
152 delete [] mRealValues;
153}
154
155
156template <typename T, int32_t RealPrecision>
158{
159 return new AAX_CPieceWiseLinearTaperDelegate(*this);
160}
161
162template <typename T, int32_t RealPrecision>
164{
165 if (mMinValue == mMaxValue)
166 return mMinValue;
167
168 if (RealPrecision)
169 value = Round(value); //reduce the precision to get proper rounding behavior with integers.
170
171 const T& highValue = mMaxValue > mMinValue ? mMaxValue : mMinValue;
172 const T& lowValue = mMaxValue > mMinValue ? mMinValue : mMaxValue;
173
174 if (value > highValue)
175 return highValue;
176 if (value < lowValue)
177 return lowValue;
178
179 return value;
180}
181
182template <typename T, int32_t RealPrecision>
184{
185
186 // Clip to normalized range.
187 if (normalizedValue > 1.0)
188 normalizedValue = 1.0;
189 if (normalizedValue < 0.0)
190 normalizedValue = 0.0;
191
192 // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
193 int32_t mLowerIndex = 0;
194 int32_t mUpperIndex = 0;
195 for (int32_t i=1;i<mNumValues;i++)
196 {
197 mUpperIndex++;
198 if (mNormalizedValues[i] >= normalizedValue)
199 break;
200 mLowerIndex++;
201 }
202
203 // Do the interpolation.
204 double delta = normalizedValue - mNormalizedValues[mLowerIndex];
205 double slope = double(mRealValues[mUpperIndex] - mRealValues[mLowerIndex]) / (mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]);
206 double interpolatedValue = mRealValues[mLowerIndex] + (delta * slope);
207 return ConstrainRealValue(static_cast<T>(interpolatedValue));
208}
209
210template <typename T, int32_t RealPrecision>
212{
213 realValue = ConstrainRealValue(realValue);
214
215 // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
216 int32_t mLowerIndex = 0;
217 int32_t mUpperIndex = 0;
218 if (mRealValues[0] < mRealValues[mNumValues-1])
219 {
220 //Increasing real values (positive slope)
221 for (int32_t i=1;i<mNumValues;i++)
222 {
223 mUpperIndex++;
224 if (mRealValues[i] >= realValue)
225 break;
226 mLowerIndex++;
227 }
228 }
229 else
230 {
231 //Decreasing real values (negative slope)
232 for (int32_t i=1;i<mNumValues;i++)
233 {
234 mUpperIndex++;
235 if (mRealValues[i] <= realValue)
236 break;
237 mLowerIndex++;
238 }
239 }
240
241 // Do the interpolation.
242 double delta = realValue - mRealValues[mLowerIndex];
243 double slope = (mRealValues[mUpperIndex] == mRealValues[mLowerIndex]) ? 0.5 : double(mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]) / (mRealValues[mUpperIndex] - mRealValues[mLowerIndex]);
244 double interpolatedValue = mNormalizedValues[mLowerIndex] + (delta * slope);
245 return static_cast<T>(interpolatedValue);
246}
247
248
249
250
251#endif //AAX_CPIECEWISELINEARTAPERDELEGATE_H
Defines the taper conversion behavior for a parameter.
Various utility definitions for AAX.
#define AAX_OVERRIDE
override keyword macro
Definition: AAX.h:151
A piece-wise linear taper conforming to AAX_ITaperDelegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:56
T GetMaximumValue() const AAX_OVERRIDE
Returns the taper's maximum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:74
AAX_CPieceWiseLinearTaperDelegate(const double *normalizedValues, const T *realValues, int32_t numValues)
Constructs a Piece-wise Linear Taper with paired normalized and real values.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:100
T GetMinimumValue() const AAX_OVERRIDE
Returns the taper's minimum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:73
~AAX_CPieceWiseLinearTaperDelegate()
Definition: AAX_CPieceWiseLinearTaperDelegate.h:148
double RealToNormalized(T realValue) const AAX_OVERRIDE
Normalizes a real parameter value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:211
T Round(double iValue) const
Definition: AAX_CPieceWiseLinearTaperDelegate.h:91
T NormalizedToReal(double normalizedValue) const AAX_OVERRIDE
Converts a normalized value to a real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:183
AAX_CPieceWiseLinearTaperDelegate< T, RealPrecision > * Clone() const AAX_OVERRIDE
Constructs and returns a copy of the taper delegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:157
T ConstrainRealValue(T value) const AAX_OVERRIDE
Applies a contraint to the value and returns the constrained value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:163
Definition: AAX_ITaperDelegate.h:86