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.

185 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. String getXmlName(const String& name)
  25. {
  26. String xmlName = name.replace("&", ":-amp-:")
  27. .replace("<",":-lt-:")
  28. .replace(">",":-gt-:")
  29. .replace("'",":-apos-:")
  30. .replace("\"",":-quot-:")
  31. .replace(".",":-46-:")
  32. .replace("/", ":-47-:")
  33. .replace("\\", ":-92-:")
  34. .replace(" ", ":-nbsp-:");
  35. return xmlName;
  36. }
  37. PluginParameter::PluginParameter()
  38. {
  39. init("parameter", // name
  40. UnitGeneric, // unit
  41. "A parameter", // description
  42. 1.0, // value
  43. 0.0, // min
  44. 1.0, // max
  45. 0.0, // default
  46. 1.0, // skew factor
  47. 0.1, // smooth coeff
  48. 0.01, // step
  49. String()); // unit suffix
  50. }
  51. PluginParameter::PluginParameter (const PluginParameter& other)
  52. {
  53. name = other.name;
  54. description = other.description;
  55. unitSuffix = other.unitSuffix;
  56. min = other.min;
  57. max = other.max;
  58. defaultValue = other.defaultValue;
  59. smoothCoeff = other.smoothCoeff;
  60. smoothValue = other.smoothValue;
  61. skewFactor = other.skewFactor;
  62. step = other.step;
  63. unit = other.unit;
  64. setValue (double (other.valueObject.getValue()));
  65. }
  66. void PluginParameter::init (const String& name_, ParameterUnit unit_, String description_,
  67. double value_, double min_, double max_, double default_,
  68. double skewFactor_, double smoothCoeff_, double step_, String unitSuffix_)
  69. {
  70. name = name_;
  71. unit = unit_;
  72. description = description_;
  73. min = min_;
  74. max = max_;
  75. setValue (value_);
  76. defaultValue = default_;
  77. smoothCoeff = smoothCoeff_;
  78. smoothValue = getValue();
  79. skewFactor = skewFactor_;
  80. step = step_;
  81. unitSuffix = unitSuffix_;
  82. // default label suffix's, these can be changed later
  83. switch (unit)
  84. {
  85. case UnitPercent: setUnitSuffix("%"); break;
  86. case UnitSeconds: setUnitSuffix("s"); break;
  87. case UnitPhase: setUnitSuffix(CharPointer_UTF8 ("\xc2\xb0")); break;
  88. case UnitHertz: setUnitSuffix("Hz"); break;
  89. case UnitDecibels: setUnitSuffix("dB"); break;
  90. case UnitDegrees: setUnitSuffix(CharPointer_UTF8 ("\xc2\xb0")); break;
  91. case UnitMeters: setUnitSuffix("m"); break;
  92. case UnitBPM: setUnitSuffix("BPM"); break;
  93. case UnitMilliseconds: setUnitSuffix("ms"); break;
  94. default: break;
  95. }
  96. }
  97. void PluginParameter::setValue (double value)
  98. {
  99. valueObject = jlimit (min, max, value);
  100. }
  101. void PluginParameter::setNormalisedValue (double normalisedValue)
  102. {
  103. setValue ((max - min) * jlimit (0.0, 1.0, normalisedValue) + min);
  104. }
  105. void PluginParameter::setUnitSuffix (String newSuffix)
  106. {
  107. unitSuffix = newSuffix;
  108. }
  109. void PluginParameter::smooth()
  110. {
  111. if (smoothValue != getValue())
  112. {
  113. if( (smoothCoeff == 1.0) || almostEqual (smoothValue, getValue()) )
  114. smoothValue = getValue();
  115. else
  116. smoothValue = ((getValue() - smoothValue) * smoothCoeff) + smoothValue;
  117. }
  118. }
  119. void PluginParameter::setSmoothCoeff (double newSmoothCoef)
  120. {
  121. smoothCoeff = newSmoothCoef;
  122. }
  123. void PluginParameter::setSkewFactor (double newSkewFactor)
  124. {
  125. skewFactor = newSkewFactor;
  126. }
  127. void PluginParameter::setSkewFactorFromMidPoint (const double valueToShowAtMidPoint)
  128. {
  129. if (max > min)
  130. skewFactor = log (0.5) / log ((valueToShowAtMidPoint - min) / (max - min));
  131. }
  132. void PluginParameter::setStep (double newStep)
  133. {
  134. step = newStep;
  135. }
  136. void PluginParameter::writeXml (XmlElement& xmlState)
  137. {
  138. xmlState.setAttribute (getXmlName(name), getValue());
  139. }
  140. void PluginParameter::readXml (const XmlElement* xmlState)
  141. {
  142. setValue (xmlState->getDoubleAttribute (getXmlName(name), getValue()));
  143. }
  144. void PluginParameter::setupSlider (Slider &slider)
  145. {
  146. slider.setRange (min, max, step);
  147. slider.setSkewFactor (skewFactor);
  148. slider.setValue (getValue(), dontSendNotification);
  149. slider.setTextValueSuffix (unitSuffix);
  150. }
  151. double PluginParameter::normaliseValue (double scaledValue)
  152. {
  153. return ((scaledValue - min) / (max - min));
  154. }