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