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.

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