You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Params.h 6.2KB

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