1 /******************************************************************************* 2 3 "A Collection of Useful C++ Classes for Digital Signal Processing" 4 By Vinnie Falco 5 6 Official project location: 7 https://github.com/vinniefalco/DSPFilters 8 9 See Documentation.cpp for contact information, notes, and bibliography. 10 11 -------------------------------------------------------------------------------- 12 13 License: MIT License (http://www.opensource.org/licenses/mit-license.php) 14 Copyright (c) 2009 by Vinnie Falco 15 16 Permission is hereby granted, free of charge, to any person obtaining a copy 17 of this software and associated documentation files (the "Software"), to deal 18 in the Software without restriction, including without limitation the rights 19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 copies of the Software, and to permit persons to whom the Software is 21 furnished to do so, subject to the following conditions: 22 23 The above copyright notice and this permission notice shall be included in 24 all copies or substantial portions of the Software. 25 26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 THE SOFTWARE. 33 34 *******************************************************************************/ 35 36 #ifndef DSPFILTERS_PARAMS_H 37 #define DSPFILTERS_PARAMS_H 38 39 #include "DspFilters/Common.h" 40 #include "DspFilters/Types.h" 41 42 namespace Dsp { 43 44 /* 45 * System for abstracting parameterizable filter specifications. 46 * 47 * This provides a "GUI-friendly" layer to the filters. Note that 48 * it is not necessary to use this layer, it is possible to instantiate 49 * the filters and their associated processing state directly, 50 * and bypass the overhead for this API if it is not needed. 51 * 52 */ 53 54 // Unique IDs to help identify parameters 55 enum ParamID 56 { 57 idSampleRate, 58 idFrequency, 59 idQ, 60 idBandwidth, 61 idBandwidthHz, 62 idGain, 63 idSlope, 64 idOrder, 65 idRippleDb, 66 idStopDb, 67 idRolloff, 68 69 idPoleRho, 70 idPoleTheta, 71 idZeroRho, 72 idZeroTheta, 73 74 idPoleReal, 75 idZeroReal 76 }; 77 78 enum 79 { 80 maxParameters = 8 81 }; 82 83 struct Params 84 { clearParams85 void clear () 86 { 87 for (int i = 0; i < maxParameters; ++i) 88 value[i] = 0; 89 } 90 91 double& operator[] (int index) 92 { 93 return value[index]; 94 } 95 96 const double& operator[] (int index) const 97 { 98 return value[index]; 99 } 100 101 double value[maxParameters]; 102 }; 103 104 // 105 // Provides meta-information about a filter parameter 106 // to achieve run-time introspection. 107 // 108 class ParamInfo 109 { 110 public: 111 typedef double (ParamInfo::*toControlValue_t) (double) const; 112 typedef double (ParamInfo::*toNativeValue_t) (double) const; 113 typedef std::string (ParamInfo::*toString_t) (double) const; 114 115 // dont use this one 116 ParamInfo (); // throws std::logic_error 117 ParamInfo(ParamID id,const char * szLabel,const char * szName,double arg1,double arg2,double defaultNativeValue,toControlValue_t toControlValue_proc,toNativeValue_t toNativeValue_proc,toString_t toString_proc)118 ParamInfo (ParamID id, 119 const char* szLabel, 120 const char* szName, 121 double arg1, 122 double arg2, 123 double defaultNativeValue, 124 toControlValue_t toControlValue_proc, 125 toNativeValue_t toNativeValue_proc, 126 toString_t toString_proc) 127 : m_id (id) 128 , m_szLabel (szLabel) 129 , m_szName (szName) 130 , m_arg1 (arg1) 131 , m_arg2 (arg2) 132 , m_defaultNativeValue (defaultNativeValue) 133 , m_toControlValue (toControlValue_proc) 134 , m_toNativeValue (toNativeValue_proc) 135 , m_toString (toString_proc) 136 { 137 } 138 139 // Used to identify well-known parameters (like cutoff frequency) getId()140 ParamID getId () const 141 { 142 return m_id; 143 } 144 145 // Returns a short label suitable for placement on a control getLabel()146 const char* getLabel () const 147 { 148 return m_szLabel; 149 } 150 151 // Returns the full name getName()152 const char* getName () const 153 { 154 return m_szName; 155 } 156 getDefaultValue()157 double getDefaultValue () const 158 { 159 return m_defaultNativeValue; 160 } 161 162 // 163 // Control value is always in the range [0..1] 164 // toControlValue(double nativeValue)165 double toControlValue (double nativeValue) const 166 { 167 return (this->*m_toControlValue) (nativeValue); 168 } 169 170 // 171 // Native value is in filter-specific units. For example, 172 // cutoff frequency would probably be in Hertz. 173 // toNativeValue(double controlValue)174 double toNativeValue (double controlValue) const 175 { 176 return (this->*m_toNativeValue) (controlValue); 177 } 178 toString(double nativeValue)179 std::string toString (double nativeValue) const 180 { 181 return (this->*m_toString) (nativeValue); 182 } 183 184 double clamp (double nativeValue) const; 185 186 // 187 // These routines are used as function pointers when 188 // constructing the various ParamInfo used by filters 189 // 190 191 double Int_toControlValue (double nativeValue) const; 192 double Int_toNativeValue (double controlValue) const; 193 194 double Real_toControlValue (double nativeValue) const; 195 double Real_toNativeValue (double controlValue) const; 196 197 double Log_toControlValue (double nativeValue) const; 198 double Log_toNativeValue (double controlValue) const; 199 200 double Pow2_toControlValue (double nativeValue) const; 201 double Pow2_toNativeValue (double controlValue) const; 202 203 std::string Int_toString (double nativeValue) const; 204 std::string Hz_toString (double nativeValue) const; 205 std::string Real_toString (double nativeValue) const; 206 std::string Db_toString (double nativeValue) const; 207 208 // 209 // Creates the specified ParamInfo 210 // 211 212 static ParamInfo defaultSampleRateParam (); 213 static ParamInfo defaultCutoffFrequencyParam (); 214 static ParamInfo defaultCenterFrequencyParam (); 215 static ParamInfo defaultQParam (); 216 static ParamInfo defaultBandwidthParam (); 217 static ParamInfo defaultBandwidthHzParam (); 218 static ParamInfo defaultGainParam (); 219 static ParamInfo defaultSlopeParam (); 220 static ParamInfo defaultRippleDbParam (); 221 static ParamInfo defaultStopDbParam (); 222 static ParamInfo defaultRolloffParam (); 223 static ParamInfo defaultPoleRhoParam (); 224 static ParamInfo defaultPoleThetaParam (); 225 static ParamInfo defaultZeroRhoParam (); 226 static ParamInfo defaultZeroThetaParam (); 227 static ParamInfo defaultPoleRealParam (); 228 static ParamInfo defaultZeroRealParam (); 229 230 private: 231 ParamID m_id; 232 const char* m_szLabel; 233 const char* m_szName; 234 double m_arg1; 235 double m_arg2; 236 double m_defaultNativeValue; 237 toControlValue_t m_toControlValue; 238 toNativeValue_t m_toNativeValue; 239 toString_t m_toString; 240 }; 241 242 } 243 244 #endif 245