Audio plugin host https://kx.studio/carla
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.

juce_AudioParameterFloat.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  21. NormalisableRange<float> r, float def,
  22. const String& labelToUse, Category categoryToUse,
  23. std::function<String (float, int)> stringFromValue,
  24. std::function<float (const String&)> valueFromString)
  25. : RangedAudioParameter (idToUse, nameToUse, labelToUse, categoryToUse),
  26. range (r), value (def), defaultValue (def),
  27. stringFromValueFunction (stringFromValue),
  28. valueFromStringFunction (valueFromString)
  29. {
  30. if (stringFromValueFunction == nullptr)
  31. {
  32. auto numDecimalPlacesToDisplay = [this]
  33. {
  34. int numDecimalPlaces = 7;
  35. if (range.interval != 0.0f)
  36. {
  37. if (approximatelyEqual (std::abs (range.interval - std::floor (range.interval)), 0.0f))
  38. return 0;
  39. auto v = std::abs (roundToInt (range.interval * pow (10, numDecimalPlaces)));
  40. while ((v % 10) == 0 && numDecimalPlaces > 0)
  41. {
  42. --numDecimalPlaces;
  43. v /= 10;
  44. }
  45. }
  46. return numDecimalPlaces;
  47. }();
  48. stringFromValueFunction = [numDecimalPlacesToDisplay] (float v, int length)
  49. {
  50. String asText (v, numDecimalPlacesToDisplay);
  51. return length > 0 ? asText.substring (0, length) : asText;
  52. };
  53. }
  54. if (valueFromStringFunction == nullptr)
  55. valueFromStringFunction = [] (const String& text) { return text.getFloatValue(); };
  56. }
  57. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  58. : AudioParameterFloat (pid, nm, { minValue, maxValue, 0.01f }, def)
  59. {
  60. }
  61. AudioParameterFloat::~AudioParameterFloat()
  62. {
  63. #if __cpp_lib_atomic_is_always_lock_free
  64. static_assert (std::atomic<float>::is_always_lock_free,
  65. "AudioParameterFloat requires a lock-free std::atomic<float>");
  66. #endif
  67. }
  68. float AudioParameterFloat::getValue() const { return convertTo0to1 (value); }
  69. void AudioParameterFloat::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
  70. float AudioParameterFloat::getDefaultValue() const { return convertTo0to1 (defaultValue); }
  71. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  72. String AudioParameterFloat::getText (float v, int length) const { return stringFromValueFunction (convertFrom0to1 (v), length); }
  73. float AudioParameterFloat::getValueForText (const String& text) const { return convertTo0to1 (valueFromStringFunction (text)); }
  74. void AudioParameterFloat::valueChanged (float) {}
  75. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  76. {
  77. if (value != newValue)
  78. setValueNotifyingHost (convertTo0to1 (newValue));
  79. return *this;
  80. }
  81. } // namespace juce