The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

165 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. // This file contains the implementations of the various AudioParameter[XYZ] classes..
  18. AudioProcessorParameterWithID::AudioProcessorParameterWithID (const String& idToUse,
  19. const String& nameToUse,
  20. const String& labelToUse)
  21. : paramID (idToUse), name (nameToUse), label (labelToUse) {}
  22. AudioProcessorParameterWithID::~AudioProcessorParameterWithID() {}
  23. String AudioProcessorParameterWithID::getName (int maximumStringLength) const { return name.substring (0, maximumStringLength); }
  24. String AudioProcessorParameterWithID::getLabel() const { return label; }
  25. //==============================================================================
  26. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  27. NormalisableRange<float> r, float def,
  28. const String& labelToUse)
  29. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  30. range (r), value (def), defaultValue (def)
  31. {
  32. }
  33. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  34. : AudioProcessorParameterWithID (pid, nm), range (minValue, maxValue), value (def), defaultValue (def)
  35. {
  36. }
  37. AudioParameterFloat::~AudioParameterFloat() {}
  38. float AudioParameterFloat::getValue() const { return range.convertTo0to1 (value); }
  39. void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); }
  40. float AudioParameterFloat::getDefaultValue() const { return range.convertTo0to1 (defaultValue); }
  41. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  42. float AudioParameterFloat::getValueForText (const String& text) const { return range.convertTo0to1 (text.getFloatValue()); }
  43. String AudioParameterFloat::getText (float v, int length) const
  44. {
  45. String asText (range.convertFrom0to1 (v), 2);
  46. return length > 0 ? asText.substring (0, length) : asText;
  47. }
  48. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  49. {
  50. if (value != newValue)
  51. setValueNotifyingHost (range.convertTo0to1 (newValue));
  52. return *this;
  53. }
  54. //==============================================================================
  55. AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameToUse,
  56. int mn, int mx, int def,
  57. const String& labelToUse)
  58. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  59. minValue (mn), maxValue (mx),
  60. value ((float) def),
  61. defaultValue (convertTo0to1 (def))
  62. {
  63. jassert (minValue < maxValue); // must have a non-zero range of values!
  64. }
  65. AudioParameterInt::~AudioParameterInt() {}
  66. int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); }
  67. float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) (maxValue - minValue); }
  68. int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) (maxValue - minValue)) + minValue)); }
  69. float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); }
  70. void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  71. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  72. int AudioParameterInt::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  73. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); }
  74. String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); }
  75. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  76. {
  77. if (get() != newValue)
  78. setValueNotifyingHost (convertTo0to1 (newValue));
  79. return *this;
  80. }
  81. //==============================================================================
  82. AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nameToUse,
  83. bool def, const String& labelToUse)
  84. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  85. value (def ? 1.0f : 0.0f),
  86. defaultValue (value)
  87. {
  88. }
  89. AudioParameterBool::~AudioParameterBool() {}
  90. float AudioParameterBool::getValue() const { return value; }
  91. void AudioParameterBool::setValue (float newValue) { value = newValue; }
  92. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  93. int AudioParameterBool::getNumSteps() const { return 2; }
  94. float AudioParameterBool::getValueForText (const String& text) const { return text.getIntValue() != 0 ? 1.0f : 0.0f; }
  95. String AudioParameterBool::getText (float v, int /*length*/) const { return String ((int) (v > 0.5f ? 1 : 0)); }
  96. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  97. {
  98. if (get() != newValue)
  99. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  100. return *this;
  101. }
  102. //==============================================================================
  103. AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& nameToUse,
  104. const StringArray& c, int def, const String& labelToUse)
  105. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), choices (c),
  106. value ((float) def),
  107. defaultValue (convertTo0to1 (def))
  108. {
  109. jassert (choices.size() > 0); // you must supply an actual set of items to choose from!
  110. }
  111. AudioParameterChoice::~AudioParameterChoice() {}
  112. int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, choices.size() - 1, v); }
  113. float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, (v + 0.5f) / (float) choices.size()); }
  114. int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange ((int) (v * (float) choices.size())); }
  115. float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); }
  116. void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  117. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  118. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  119. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); }
  120. String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; }
  121. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  122. {
  123. if (getIndex() != newValue)
  124. setValueNotifyingHost (convertTo0to1 (newValue));
  125. return *this;
  126. }