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.

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