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.

98 lines
3.9KB

  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. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  22. NormalisableRange<float> r, float def,
  23. const String& labelToUse, Category categoryToUse,
  24. std::function<String (float, int)> stringFromValue,
  25. std::function<float (const String&)> valueFromString)
  26. : RangedAudioParameter (idToUse, nameToUse, labelToUse, categoryToUse),
  27. range (r), value (def), defaultValue (def),
  28. stringFromValueFunction (stringFromValue),
  29. valueFromStringFunction (valueFromString)
  30. {
  31. if (stringFromValueFunction == nullptr)
  32. {
  33. auto numDecimalPlacesToDisplay = [this]
  34. {
  35. int numDecimalPlaces = 7;
  36. if (range.interval != 0.0f)
  37. {
  38. if (approximatelyEqual (std::abs (range.interval - (int) range.interval), 0.0f))
  39. return 0;
  40. auto v = std::abs (roundToInt (range.interval * pow (10, numDecimalPlaces)));
  41. while ((v % 10) == 0 && numDecimalPlaces > 0)
  42. {
  43. --numDecimalPlaces;
  44. v /= 10;
  45. }
  46. }
  47. return numDecimalPlaces;
  48. }();
  49. stringFromValueFunction = [numDecimalPlacesToDisplay] (float v, int length)
  50. {
  51. String asText (v, numDecimalPlacesToDisplay);
  52. return length > 0 ? asText.substring (0, length) : asText;
  53. };
  54. }
  55. if (valueFromStringFunction == nullptr)
  56. valueFromStringFunction = [] (const String& text) { return text.getFloatValue(); };
  57. }
  58. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  59. : AudioParameterFloat (pid, nm, { minValue, maxValue, 0.01f }, def)
  60. {
  61. }
  62. AudioParameterFloat::~AudioParameterFloat() {}
  63. float AudioParameterFloat::getValue() const { return convertTo0to1 (value); }
  64. void AudioParameterFloat::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
  65. float AudioParameterFloat::getDefaultValue() const { return convertTo0to1 (defaultValue); }
  66. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  67. String AudioParameterFloat::getText (float v, int length) const { return stringFromValueFunction (convertFrom0to1 (v), length); }
  68. float AudioParameterFloat::getValueForText (const String& text) const { return convertTo0to1 (valueFromStringFunction (text)); }
  69. void AudioParameterFloat::valueChanged (float) {}
  70. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  71. {
  72. if (value != newValue)
  73. setValueNotifyingHost (convertTo0to1 (newValue));
  74. return *this;
  75. }
  76. } // namespace juce