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.

167 lines
8.5KB

  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. AudioProcessorParameter::Category categoryToUse)
  22. : paramID (idToUse), name (nameToUse), label (labelToUse), category (categoryToUse) {}
  23. AudioProcessorParameterWithID::~AudioProcessorParameterWithID() {}
  24. String AudioProcessorParameterWithID::getName (int maximumStringLength) const { return name.substring (0, maximumStringLength); }
  25. String AudioProcessorParameterWithID::getLabel() const { return label; }
  26. AudioProcessorParameter::Category AudioProcessorParameterWithID::getCategory() const { return category; }
  27. //==============================================================================
  28. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  29. NormalisableRange<float> r, float def,
  30. const String& labelToUse, Category categoryToUse)
  31. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse, categoryToUse),
  32. range (r), value (def), defaultValue (def)
  33. {
  34. }
  35. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  36. : AudioProcessorParameterWithID (pid, nm), range (minValue, maxValue), value (def), defaultValue (def)
  37. {
  38. }
  39. AudioParameterFloat::~AudioParameterFloat() {}
  40. float AudioParameterFloat::getValue() const { return range.convertTo0to1 (value); }
  41. void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); }
  42. float AudioParameterFloat::getDefaultValue() const { return range.convertTo0to1 (defaultValue); }
  43. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  44. float AudioParameterFloat::getValueForText (const String& text) const { return range.convertTo0to1 (text.getFloatValue()); }
  45. String AudioParameterFloat::getText (float v, int length) const
  46. {
  47. String asText (range.convertFrom0to1 (v), 2);
  48. return length > 0 ? asText.substring (0, length) : asText;
  49. }
  50. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  51. {
  52. if (value != newValue)
  53. setValueNotifyingHost (range.convertTo0to1 (newValue));
  54. return *this;
  55. }
  56. //==============================================================================
  57. AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameToUse,
  58. int mn, int mx, int def,
  59. const String& labelToUse)
  60. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  61. minValue (mn), maxValue (mx),
  62. value ((float) def),
  63. defaultValue (convertTo0to1 (def))
  64. {
  65. jassert (minValue < maxValue); // must have a non-zero range of values!
  66. }
  67. AudioParameterInt::~AudioParameterInt() {}
  68. int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); }
  69. float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) (maxValue - minValue); }
  70. int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) (maxValue - minValue)) + minValue)); }
  71. float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); }
  72. void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  73. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  74. int AudioParameterInt::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  75. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); }
  76. String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); }
  77. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  78. {
  79. if (get() != newValue)
  80. setValueNotifyingHost (convertTo0to1 (newValue));
  81. return *this;
  82. }
  83. //==============================================================================
  84. AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nameToUse,
  85. bool def, const String& labelToUse)
  86. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  87. value (def ? 1.0f : 0.0f),
  88. defaultValue (value)
  89. {
  90. }
  91. AudioParameterBool::~AudioParameterBool() {}
  92. float AudioParameterBool::getValue() const { return value; }
  93. void AudioParameterBool::setValue (float newValue) { value = newValue; }
  94. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  95. int AudioParameterBool::getNumSteps() const { return 2; }
  96. float AudioParameterBool::getValueForText (const String& text) const { return text.getIntValue() != 0 ? 1.0f : 0.0f; }
  97. String AudioParameterBool::getText (float v, int /*length*/) const { return String ((int) (v > 0.5f ? 1 : 0)); }
  98. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  99. {
  100. if (get() != newValue)
  101. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  102. return *this;
  103. }
  104. //==============================================================================
  105. AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& nameToUse,
  106. const StringArray& c, int def, const String& labelToUse)
  107. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), choices (c),
  108. value ((float) def),
  109. defaultValue (convertTo0to1 (def))
  110. {
  111. jassert (choices.size() > 0); // you must supply an actual set of items to choose from!
  112. }
  113. AudioParameterChoice::~AudioParameterChoice() {}
  114. int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, choices.size() - 1, v); }
  115. float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, (v + 0.5f) / (float) choices.size()); }
  116. int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange ((int) (v * (float) choices.size())); }
  117. float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); }
  118. void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  119. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  120. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  121. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); }
  122. String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; }
  123. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  124. {
  125. if (getIndex() != newValue)
  126. setValueNotifyingHost (convertTo0to1 (newValue));
  127. return *this;
  128. }