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.

105 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 ParameterID& idToUse,
  21. const String& nameToUse,
  22. NormalisableRange<float> r,
  23. float def,
  24. const AudioParameterFloatAttributes& attributes)
  25. : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
  26. range (r),
  27. value (def),
  28. valueDefault (def),
  29. stringFromValueFunction (attributes.getStringFromValueFunction()),
  30. valueFromStringFunction (attributes.getValueFromStringFunction())
  31. {
  32. if (stringFromValueFunction == nullptr)
  33. {
  34. auto numDecimalPlacesToDisplay = [this]
  35. {
  36. int numDecimalPlaces = 7;
  37. if (range.interval != 0.0f)
  38. {
  39. if (approximatelyEqual (std::abs (range.interval - std::floor (range.interval)), 0.0f))
  40. return 0;
  41. auto v = std::abs (roundToInt (range.interval * pow (10, numDecimalPlaces)));
  42. while ((v % 10) == 0 && numDecimalPlaces > 0)
  43. {
  44. --numDecimalPlaces;
  45. v /= 10;
  46. }
  47. }
  48. return numDecimalPlaces;
  49. }();
  50. stringFromValueFunction = [numDecimalPlacesToDisplay] (float v, int length)
  51. {
  52. String asText (v, numDecimalPlacesToDisplay);
  53. return length > 0 ? asText.substring (0, length) : asText;
  54. };
  55. }
  56. if (valueFromStringFunction == nullptr)
  57. valueFromStringFunction = [] (const String& text) { return text.getFloatValue(); };
  58. }
  59. AudioParameterFloat::AudioParameterFloat (const ParameterID& pid, const String& nm, float minValue, float maxValue, float def)
  60. : AudioParameterFloat (pid, nm, { minValue, maxValue, 0.01f }, def)
  61. {
  62. }
  63. AudioParameterFloat::~AudioParameterFloat()
  64. {
  65. #if __cpp_lib_atomic_is_always_lock_free
  66. static_assert (std::atomic<float>::is_always_lock_free,
  67. "AudioParameterFloat requires a lock-free std::atomic<float>");
  68. #endif
  69. }
  70. float AudioParameterFloat::getValue() const { return convertTo0to1 (value); }
  71. void AudioParameterFloat::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
  72. float AudioParameterFloat::getDefaultValue() const { return convertTo0to1 (valueDefault); }
  73. int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); }
  74. String AudioParameterFloat::getText (float v, int length) const { return stringFromValueFunction (convertFrom0to1 (v), length); }
  75. float AudioParameterFloat::getValueForText (const String& text) const { return convertTo0to1 (valueFromStringFunction (text)); }
  76. void AudioParameterFloat::valueChanged (float) {}
  77. AudioParameterFloat& AudioParameterFloat::operator= (float newValue)
  78. {
  79. if (value != newValue)
  80. setValueNotifyingHost (convertTo0to1 (newValue));
  81. return *this;
  82. }
  83. } // namespace juce