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.

207 lines
9.7KB

  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. namespace juce
  20. {
  21. // This file contains the implementations of the various AudioParameter[XYZ] classes..
  22. AudioProcessorParameterWithID::AudioProcessorParameterWithID (const String& idToUse,
  23. const String& nameToUse,
  24. const String& labelToUse,
  25. AudioProcessorParameter::Category categoryToUse)
  26. : paramID (idToUse), name (nameToUse), label (labelToUse), category (categoryToUse) {}
  27. AudioProcessorParameterWithID::~AudioProcessorParameterWithID() {}
  28. String AudioProcessorParameterWithID::getName (int maximumStringLength) const { return name.substring (0, maximumStringLength); }
  29. String AudioProcessorParameterWithID::getLabel() const { return label; }
  30. AudioProcessorParameter::Category AudioProcessorParameterWithID::getCategory() const { return category; }
  31. //==============================================================================
  32. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  33. NormalisableRange<float> r, float def,
  34. const String& labelToUse, Category categoryToUse)
  35. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse, categoryToUse),
  36. range (r), value (def), defaultValue (def)
  37. {
  38. }
  39. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  40. : AudioProcessorParameterWithID (pid, nm), range (minValue, maxValue), value (def), defaultValue (def)
  41. {
  42. }
  43. AudioParameterFloat::~AudioParameterFloat() {}
  44. float AudioParameterFloat::getValue() const { return range.convertTo0to1 (value); }
  45. void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); valueChanged (get()); }
  46. float AudioParameterFloat::getDefaultValue() const { return range.convertTo0to1 (defaultValue); }
  47. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  48. float AudioParameterFloat::getValueForText (const String& text) const { return range.convertTo0to1 (text.getFloatValue()); }
  49. void AudioParameterFloat::valueChanged (float) {}
  50. String AudioParameterFloat::getText (float v, int length) const
  51. {
  52. String asText (range.convertFrom0to1 (v), 2);
  53. return length > 0 ? asText.substring (0, length) : asText;
  54. }
  55. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  56. {
  57. if (value != newValue)
  58. setValueNotifyingHost (range.convertTo0to1 (newValue));
  59. return *this;
  60. }
  61. //==============================================================================
  62. AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameToUse,
  63. int mn, int mx, int def,
  64. const String& labelToUse)
  65. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  66. minValue (mn), maxValue (mx), rangeOfValues (maxValue - minValue),
  67. value ((float) def),
  68. defaultValue (convertTo0to1 (def))
  69. {
  70. jassert (minValue < maxValue); // must have a non-zero range of values!
  71. }
  72. AudioParameterInt::~AudioParameterInt() {}
  73. int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); }
  74. float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) rangeOfValues; }
  75. int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) rangeOfValues) + minValue)); }
  76. float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); }
  77. void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); valueChanged (get()); }
  78. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  79. int AudioParameterInt::getNumSteps() const { return rangeOfValues + 1; }
  80. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); }
  81. String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); }
  82. void AudioParameterInt::valueChanged (int) {}
  83. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  84. {
  85. if (get() != newValue)
  86. setValueNotifyingHost (convertTo0to1 (newValue));
  87. return *this;
  88. }
  89. //==============================================================================
  90. AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nameToUse,
  91. bool def, const String& labelToUse)
  92. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse),
  93. value (def ? 1.0f : 0.0f),
  94. defaultValue (value)
  95. {
  96. onStrings.add (TRANS("on"));
  97. onStrings.add (TRANS("yes"));
  98. onStrings.add (TRANS("true"));
  99. offStrings.add (TRANS("off"));
  100. offStrings.add (TRANS("no"));
  101. offStrings.add (TRANS("false"));
  102. }
  103. AudioParameterBool::~AudioParameterBool() {}
  104. float AudioParameterBool::getValue() const { return value; }
  105. void AudioParameterBool::setValue (float newValue) { value = newValue; valueChanged (get()); }
  106. float AudioParameterBool::getDefaultValue() const { return defaultValue; }
  107. int AudioParameterBool::getNumSteps() const { return 2; }
  108. bool AudioParameterBool::isDiscrete() const { return true; }
  109. bool AudioParameterBool::isBoolean() const { return true; }
  110. void AudioParameterBool::valueChanged (bool) {}
  111. float AudioParameterBool::getValueForText (const String& text) const
  112. {
  113. String lowercaseText (text.toLowerCase());
  114. for (auto& testText : onStrings)
  115. if (lowercaseText == testText)
  116. return 1.0f;
  117. for (auto& testText : offStrings)
  118. if (lowercaseText == testText)
  119. return 0.0f;
  120. return text.getIntValue() != 0 ? 1.0f : 0.0f;
  121. }
  122. String AudioParameterBool::getText (float v, int /*length*/) const
  123. {
  124. return v < 0.5f ? TRANS("Off") : TRANS("On");
  125. }
  126. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  127. {
  128. if (get() != newValue)
  129. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  130. return *this;
  131. }
  132. //==============================================================================
  133. AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& nameToUse,
  134. const StringArray& c, int def, const String& labelToUse)
  135. : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), choices (c),
  136. value ((float) def),
  137. maxIndex (choices.size() - 1),
  138. defaultValue (convertTo0to1 (def))
  139. {
  140. jassert (choices.size() > 0); // you must supply an actual set of items to choose from!
  141. }
  142. AudioParameterChoice::~AudioParameterChoice() {}
  143. int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, maxIndex, v); }
  144. float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, v / (float) maxIndex); }
  145. int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt (v * (float) maxIndex)); }
  146. float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); }
  147. void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); valueChanged (getIndex()); }
  148. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  149. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  150. bool AudioParameterChoice::isDiscrete() const { return true; }
  151. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); }
  152. String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; }
  153. void AudioParameterChoice::valueChanged (int) {}
  154. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  155. {
  156. if (getIndex() != newValue)
  157. setValueNotifyingHost (convertTo0to1 (newValue));
  158. return *this;
  159. }
  160. } // namespace juce