AAX SDK 2.8.0
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-2024 Avid Technology, Inc.
5 * All rights reserved.
6 *
7 * This file is part of the Avid AAX SDK.
8 *
9 * The AAX SDK is subject to commercial or open-source licensing.
10 *
11 * By using the AAX SDK, you agree to the terms of both the Avid AAX SDK License
12 * Agreement and Avid Privacy Policy.
13 *
14 * AAX SDK License: https://developer.avid.com/aax
15 * Privacy Policy: https://www.avid.com/legal/privacy-policy-statement
16 *
17 * Or: You may also use this code under the terms of the GPL v3 (see
18 * www.gnu.org/licenses).
19 *
20 * THE AAX SDK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 * EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 * DISCLAIMED.
23 *
24 */
25
32/*================================================================================================*/
33
34
35#ifndef AAX_CPIECEWISELINEARTAPERDELEGATE_H
36#define AAX_CPIECEWISELINEARTAPERDELEGATE_H
37
38#include "AAX_ITaperDelegate.h"
39#include "AAX.h" //for types
40
41#include <cmath> //for floor()
42
43
67template <typename T, int32_t RealPrecision=100>
69{
70public:
79 AAX_CPieceWiseLinearTaperDelegate(const double* normalizedValues, const T* realValues, int32_t numValues);
80
81 AAX_CPieceWiseLinearTaperDelegate(const AAX_CPieceWiseLinearTaperDelegate& other); //Explicit copy constructor because there are internal arrays.
83
84 //Virtual AAX_ITaperDelegate Overrides
86 T GetMinimumValue() const AAX_OVERRIDE { return mMinValue; }
87 T GetMaximumValue() const AAX_OVERRIDE { return mMaxValue; }
88 T ConstrainRealValue(T value) const AAX_OVERRIDE;
89 T NormalizedToReal(double normalizedValue) const AAX_OVERRIDE;
90 double RealToNormalized(T realValue) const AAX_OVERRIDE;
91
92protected:
93 T Round(double iValue) const;
94
95private:
96 double* mNormalizedValues;
97 T* mRealValues;
98 int32_t mNumValues;
99 T mMinValue; //Really just an optimization
100 T mMaxValue; //Really just an optimization
101};
102
103template <typename T, int32_t RealPrecision>
105{
106 if (RealPrecision > 0)
107 return static_cast<T>(floor(iValue * RealPrecision + 0.5) / RealPrecision);
108 else
109 return static_cast<T>(iValue);
110}
111
112template <typename T, int32_t RealPrecision>
113AAX_CPieceWiseLinearTaperDelegate<T, RealPrecision>::AAX_CPieceWiseLinearTaperDelegate(const double* normalizedValues, const T* realValues, int32_t numValues) : AAX_ITaperDelegate<T>(),
114 mNormalizedValues(0),
115 mRealValues(0),
116 mNumValues(0),
117 mMinValue(0),
118 mMaxValue(0)
119{
120 mNormalizedValues = new double[numValues];
121 mRealValues = new T[numValues];
122 mNumValues = numValues;
123
124 if (numValues > 0)
125 {
126 mMaxValue = realValues[0];
127 mMinValue = realValues[0];
128 }
129 for (int32_t i=0; i< numValues; i++)
130 {
131 mNormalizedValues[i] = normalizedValues[i];
132 mRealValues[i] = realValues[i];
133 if (mRealValues[i] > mMaxValue)
134 mMaxValue = mRealValues[i];
135 if (mRealValues[i] < mMinValue)
136 mMinValue = mRealValues[i];
137 }
138}
139
140template <typename T, int32_t RealPrecision>
142 mNormalizedValues(0),
143 mRealValues(0),
144 mNumValues(0),
145 mMinValue(0),
146 mMaxValue(0)
147{
148 mNormalizedValues = new double[other.mNumValues];
149 mRealValues = new T[other.mNumValues];
150 mNumValues = other.mNumValues;
151 mMaxValue = other.mMaxValue;
152 mMinValue = other.mMinValue;
153 for (int32_t i=0; i< mNumValues; i++)
154 {
155 mNormalizedValues[i] = other.mNormalizedValues[i];
156 mRealValues[i] = other.mRealValues[i];
157 }
158}
159
160template <typename T, int32_t RealPrecision>
162{
163 mNumValues = 0;
164 delete [] mNormalizedValues;
165 delete [] mRealValues;
166}
167
168
169template <typename T, int32_t RealPrecision>
171{
172 return new AAX_CPieceWiseLinearTaperDelegate(*this);
173}
174
175template <typename T, int32_t RealPrecision>
177{
178 if (mMinValue == mMaxValue)
179 return mMinValue;
180
181 if (RealPrecision)
182 value = Round(value); //reduce the precision to get proper rounding behavior with integers.
183
184 const T& highValue = mMaxValue > mMinValue ? mMaxValue : mMinValue;
185 const T& lowValue = mMaxValue > mMinValue ? mMinValue : mMaxValue;
186
187 if (value > highValue)
188 return highValue;
189 if (value < lowValue)
190 return lowValue;
191
192 return value;
193}
194
195template <typename T, int32_t RealPrecision>
197{
198
199 // Clip to normalized range.
200 if (normalizedValue > 1.0)
201 normalizedValue = 1.0;
202 if (normalizedValue < 0.0)
203 normalizedValue = 0.0;
204
205 // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
206 int32_t mLowerIndex = 0;
207 int32_t mUpperIndex = 0;
208 for (int32_t i=1;i<mNumValues;i++)
209 {
210 mUpperIndex++;
211 if (mNormalizedValues[i] >= normalizedValue)
212 break;
213 mLowerIndex++;
214 }
215
216 // Do the interpolation.
217 double delta = normalizedValue - mNormalizedValues[mLowerIndex];
218 double slope = double(mRealValues[mUpperIndex] - mRealValues[mLowerIndex]) / (mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]);
219 double interpolatedValue = mRealValues[mLowerIndex] + (delta * slope);
220 return ConstrainRealValue(static_cast<T>(interpolatedValue));
221}
222
223template <typename T, int32_t RealPrecision>
225{
226 realValue = ConstrainRealValue(realValue);
227
228 // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
229 int32_t mLowerIndex = 0;
230 int32_t mUpperIndex = 0;
231 if (mRealValues[0] < mRealValues[mNumValues-1])
232 {
233 //Increasing real values (positive slope)
234 for (int32_t i=1;i<mNumValues;i++)
235 {
236 mUpperIndex++;
237 if (mRealValues[i] >= realValue)
238 break;
239 mLowerIndex++;
240 }
241 }
242 else
243 {
244 //Decreasing real values (negative slope)
245 for (int32_t i=1;i<mNumValues;i++)
246 {
247 mUpperIndex++;
248 if (mRealValues[i] <= realValue)
249 break;
250 mLowerIndex++;
251 }
252 }
253
254 // Do the interpolation.
255 double delta = realValue - mRealValues[mLowerIndex];
256 double slope = (mRealValues[mUpperIndex] == mRealValues[mLowerIndex]) ? 0.5 : double(mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]) / (mRealValues[mUpperIndex] - mRealValues[mLowerIndex]);
257 double interpolatedValue = mNormalizedValues[mLowerIndex] + (delta * slope);
258 return static_cast<T>(interpolatedValue);
259}
260
261
262
263
264#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:164
A piece-wise linear taper conforming to AAX_ITaperDelegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:69
T GetMaximumValue() const AAX_OVERRIDE
Returns the taper's maximum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:87
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:113
T GetMinimumValue() const AAX_OVERRIDE
Returns the taper's minimum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:86
~AAX_CPieceWiseLinearTaperDelegate()
Definition: AAX_CPieceWiseLinearTaperDelegate.h:161
double RealToNormalized(T realValue) const AAX_OVERRIDE
Normalizes a real parameter value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:224
T Round(double iValue) const
Definition: AAX_CPieceWiseLinearTaperDelegate.h:104
T NormalizedToReal(double normalizedValue) const AAX_OVERRIDE
Converts a normalized value to a real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:196
AAX_CPieceWiseLinearTaperDelegate< T, RealPrecision > * Clone() const AAX_OVERRIDE
Constructs and returns a copy of the taper delegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:170
T ConstrainRealValue(T value) const AAX_OVERRIDE
Applies a contraint to the value and returns the constrained value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:176
Definition: AAX_ITaperDelegate.h:99