AAX SDK  2.1.1
Avid Audio Extensions Development Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AAX_CPieceWiseLinearTaperDelegate.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  *
4  * Copyright (c) 2009 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 Technolgy, 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 { return mMinValue; }
77  T GetMaximumValue() const { return mMaxValue; }
78  T ConstrainRealValue(T value) const;
79  T NormalizedToReal(double normalizedValue) const;
80  double RealToNormalized(T realValue) const;
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 (RealPrecision)
169  value = Round(value); //reduce the precision to get proper rounding behavior with integers.
170 
171  if (value > mMaxValue)
172  return mMaxValue;
173  if (value < mMinValue)
174  return mMinValue;
175  return value;
176 }
177 
178 template <typename T, int32_t RealPrecision>
180 {
181 
182  // Clip to normalized range.
183  if (normalizedValue > 1.0)
184  normalizedValue = 1.0;
185  if (normalizedValue < 0.0)
186  normalizedValue = 0.0;
187 
188  // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
189  int32_t mLowerIndex = 0;
190  int32_t mUpperIndex = 0;
191  for (int32_t i=1;i<mNumValues;i++)
192  {
193  mUpperIndex++;
194  if (mNormalizedValues[i] >= normalizedValue)
195  break;
196  mLowerIndex++;
197  }
198 
199  // Do the interpolation.
200  double delta = normalizedValue - mNormalizedValues[mLowerIndex];
201  double slope = double(mRealValues[mUpperIndex] - mRealValues[mLowerIndex]) / (mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]);
202  double interpolatedValue = mRealValues[mLowerIndex] + (delta * slope);
203  return ConstrainRealValue(static_cast<T>(interpolatedValue));
204 }
205 
206 template <typename T, int32_t RealPrecision>
208 {
209  realValue = ConstrainRealValue(realValue);
210 
211  // This is basically linear interpolation so let's first find the bounding normalized points from our specified array.
212  int32_t mLowerIndex = 0;
213  int32_t mUpperIndex = 0;
214  if (mRealValues[0] < mRealValues[mNumValues-1])
215  {
216  //Increasing real values (positive slope)
217  for (int32_t i=1;i<mNumValues;i++)
218  {
219  mUpperIndex++;
220  if (mRealValues[i] >= realValue)
221  break;
222  mLowerIndex++;
223  }
224  }
225  else
226  {
227  //Decreasing real values (negative slope)
228  for (int32_t i=1;i<mNumValues;i++)
229  {
230  mUpperIndex++;
231  if (mRealValues[i] <= realValue)
232  break;
233  mLowerIndex++;
234  }
235  }
236 
237  // Do the interpolation.
238  double delta = realValue - mRealValues[mLowerIndex];
239  double slope = double(mNormalizedValues[mUpperIndex] - mNormalizedValues[mLowerIndex]) / (mRealValues[mUpperIndex] - mRealValues[mLowerIndex]);
240  double interpolatedValue = mNormalizedValues[mLowerIndex] + (delta * slope);
241  return static_cast<T>(interpolatedValue);
242 }
243 
244 
245 
246 
247 #endif //AAX_CPIECEWISELINEARTAPERDELEGATE_H
Various utility definitions for AAX.
T Round(double iValue) const
Definition: AAX_CPieceWiseLinearTaperDelegate.h:94
T GetMaximumValue() const
Returns the taper&#39;s maximum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:77
Defines the taper conversion behavior for a parameter.
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
Classes for conversion to and from normalized parameter values.
Definition: AAX_ITaperDelegate.h:88
AAX_CPieceWiseLinearTaperDelegate< T, RealPrecision > * Clone() const
Constructs and returns a copy of the taper delegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:160
T GetMinimumValue() const
Returns the taper&#39;s minimum real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:76
~AAX_CPieceWiseLinearTaperDelegate()
Definition: AAX_CPieceWiseLinearTaperDelegate.h:151
double RealToNormalized(T realValue) const
Normalizes a real parameter value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:207
A piece-wise linear taper conforming to AAX_ITaperDelegate.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:58
T ConstrainRealValue(T value) const
Applies a contraint to the value and returns the constrained value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:166
T NormalizedToReal(double normalizedValue) const
Converts a normalized value to a real value.
Definition: AAX_CPieceWiseLinearTaperDelegate.h:179