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.

158 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 { return String (range.convertFrom0to1 (v), 2).substring (0, length); }
  38. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  39. {
  40. const float normalisedValue = range.convertTo0to1 (newValue);
  41. if (value != normalisedValue)
  42. setValueNotifyingHost (normalisedValue);
  43. return *this;
  44. }
  45. //==============================================================================
  46. AudioParameterInt::AudioParameterInt (String pid, String nm, int mn, int mx, int def)
  47. : AudioProcessorParameterWithID (pid, nm),
  48. minValue (mn), maxValue (mx),
  49. value ((float) def),
  50. defaultValue (convertTo0to1 (def))
  51. {
  52. jassert (minValue < maxValue); // must have a non-zero range of values!
  53. }
  54. AudioParameterInt::~AudioParameterInt() {}
  55. int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); }
  56. float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) (maxValue - minValue); }
  57. int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) (maxValue - minValue)) + minValue)); }
  58. float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); }
  59. void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  60. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  61. int AudioParameterInt::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  62. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); }
  63. String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); }
  64. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  65. {
  66. const float normalisedValue = convertTo0to1 (newValue);
  67. if (value != normalisedValue)
  68. setValueNotifyingHost (normalisedValue);
  69. return *this;
  70. }
  71. //==============================================================================
  72. AudioParameterBool::AudioParameterBool (String pid, String nm, bool def)
  73. : AudioProcessorParameterWithID (pid, nm),
  74. value (def ? 1.0f : 0.0f),
  75. defaultValue (value)
  76. {
  77. }
  78. AudioParameterBool::~AudioParameterBool() {}
  79. float AudioParameterBool::getValue() const { return value; }
  80. void AudioParameterBool::setValue (float newValue) { value = newValue; }
  81. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  82. int AudioParameterBool::getNumSteps() const { return 2; }
  83. float AudioParameterBool::getValueForText (const String& text) const { return text.getIntValue() != 0 ? 1.0f : 0.0f; }
  84. String AudioParameterBool::getText (float v, int /*length*/) const { return String ((int) (v > 0.5f ? 1 : 0)); }
  85. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  86. {
  87. const float normalisedValue = newValue ? 1.0f : 0.0f;
  88. if (value != normalisedValue)
  89. setValueNotifyingHost (normalisedValue);
  90. return *this;
  91. }
  92. //==============================================================================
  93. AudioParameterChoice::AudioParameterChoice (String pid, String nm, const StringArray& c, int def)
  94. : AudioProcessorParameterWithID (pid, nm), choices (c),
  95. value ((float) def),
  96. defaultValue (convertTo0to1 (def))
  97. {
  98. jassert (choices.size() > 0); // you must supply an actual set of items to choose from!
  99. }
  100. AudioParameterChoice::~AudioParameterChoice() {}
  101. int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, choices.size() - 1, v); }
  102. float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, (v + 0.5f) / (float) choices.size()); }
  103. int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange ((int) (v * (float) choices.size())); }
  104. float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); }
  105. void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  106. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  107. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  108. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); }
  109. String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; }
  110. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  111. {
  112. const float normalisedValue = convertTo0to1 (newValue);
  113. if (value != normalisedValue)
  114. setValueNotifyingHost (normalisedValue);
  115. return *this;
  116. }