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.

171 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. // This file contains the implementations of the various AudioParameter[XYZ] classes..
  20. AudioProcessorParameterWithID::AudioProcessorParameterWithID (const String& idToUse,
  21. const String& nameToUse,
  22. const String& labelToUse,
  23. AudioProcessorParameter::Category categoryToUse)
  24. : paramID (idToUse), name (nameToUse), label (labelToUse), category (categoryToUse) {}
  25. AudioProcessorParameterWithID::~AudioProcessorParameterWithID() {}
  26. String AudioProcessorParameterWithID::getName (int maximumStringLength) const { return name.substring (0, maximumStringLength); }
  27. String AudioProcessorParameterWithID::getLabel() const { return label; }
  28. AudioProcessorParameter::Category AudioProcessorParameterWithID::getCategory() const { return category; }
  29. //==============================================================================
  30. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  31. NormalisableRange<float> r, float def,
  32. const String& labelToUse, Category categoryToUse)
  33. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse, categoryToUse),
  34. range (r), value (def), defaultValue (def)
  35. {
  36. }
  37. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  38. : AudioProcessorParameterWithID (pid, nm), range (minValue, maxValue), value (def), defaultValue (def)
  39. {
  40. }
  41. AudioParameterFloat::~AudioParameterFloat() {}
  42. float AudioParameterFloat::getValue() const { return range.convertTo0to1 (value); }
  43. void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); }
  44. float AudioParameterFloat::getDefaultValue() const { return range.convertTo0to1 (defaultValue); }
  45. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  46. float AudioParameterFloat::getValueForText (const String& text) const { return range.convertTo0to1 (text.getFloatValue()); }
  47. String AudioParameterFloat::getText (float v, int length) const
  48. {
  49. String asText (range.convertFrom0to1 (v), 2);
  50. return length > 0 ? asText.substring (0, length) : asText;
  51. }
  52. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  53. {
  54. if (value != newValue)
  55. setValueNotifyingHost (range.convertTo0to1 (newValue));
  56. return *this;
  57. }
  58. //==============================================================================
  59. AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameToUse,
  60. int mn, int mx, int def,
  61. const String& labelToUse)
  62. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  63. minValue (mn), maxValue (mx),
  64. value ((float) def),
  65. defaultValue (convertTo0to1 (def))
  66. {
  67. jassert (minValue < maxValue); // must have a non-zero range of values!
  68. }
  69. AudioParameterInt::~AudioParameterInt() {}
  70. int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); }
  71. float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) (maxValue - minValue); }
  72. int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) (maxValue - minValue)) + minValue)); }
  73. float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); }
  74. void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  75. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  76. int AudioParameterInt::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  77. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); }
  78. String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); }
  79. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  80. {
  81. if (get() != newValue)
  82. setValueNotifyingHost (convertTo0to1 (newValue));
  83. return *this;
  84. }
  85. //==============================================================================
  86. AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nameToUse,
  87. bool def, const String& labelToUse)
  88. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  89. value (def ? 1.0f : 0.0f),
  90. defaultValue (value)
  91. {
  92. }
  93. AudioParameterBool::~AudioParameterBool() {}
  94. float AudioParameterBool::getValue() const { return value; }
  95. void AudioParameterBool::setValue (float newValue) { value = newValue; }
  96. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  97. int AudioParameterBool::getNumSteps() const { return 2; }
  98. bool AudioParameterBool::isDiscrete() const { return true; }
  99. float AudioParameterBool::getValueForText (const String& text) const { return text.getIntValue() != 0 ? 1.0f : 0.0f; }
  100. String AudioParameterBool::getText (float v, int /*length*/) const { return String ((int) (v > 0.5f ? 1 : 0)); }
  101. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  102. {
  103. if (get() != newValue)
  104. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  105. return *this;
  106. }
  107. //==============================================================================
  108. AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& nameToUse,
  109. const StringArray& c, int def, const String& labelToUse)
  110. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), choices (c),
  111. value ((float) def),
  112. defaultValue (convertTo0to1 (def))
  113. {
  114. jassert (choices.size() > 0); // you must supply an actual set of items to choose from!
  115. }
  116. AudioParameterChoice::~AudioParameterChoice() {}
  117. int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, choices.size() - 1, v); }
  118. float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, (v + 0.5f) / (float) choices.size()); }
  119. int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange ((int) (v * (float) choices.size())); }
  120. float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); }
  121. void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }
  122. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  123. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  124. bool AudioParameterChoice::isDiscrete() const { return true; }
  125. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); }
  126. String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; }
  127. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  128. {
  129. if (getIndex() != newValue)
  130. setValueNotifyingHost (convertTo0to1 (newValue));
  131. return *this;
  132. }