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.

96 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. AudioParameterFloat::AudioParameterFloat (const String& idToUse, const String& nameToUse,
  16. NormalisableRange<float> r, float def,
  17. const String& labelToUse, Category categoryToUse,
  18. std::function<String(float, int)> stringFromValue,
  19. std::function<float(const String&)> valueFromString)
  20. : RangedAudioParameter (idToUse, nameToUse, labelToUse, categoryToUse),
  21. range (r), value (def), defaultValue (def),
  22. stringFromValueFunction (stringFromValue),
  23. valueFromStringFunction (valueFromString)
  24. {
  25. if (stringFromValueFunction == nullptr)
  26. {
  27. auto numDecimalPlacesToDisplay = [this]
  28. {
  29. int numDecimalPlaces = 7;
  30. if (range.interval != 0.0f)
  31. {
  32. if (approximatelyEqual (std::abs (range.interval - (int) range.interval), 0.0f))
  33. return 0;
  34. auto v = std::abs (roundToInt (range.interval * pow (10, numDecimalPlaces)));
  35. while ((v % 10) == 0 && numDecimalPlaces > 0)
  36. {
  37. --numDecimalPlaces;
  38. v /= 10;
  39. }
  40. }
  41. return numDecimalPlaces;
  42. }();
  43. stringFromValueFunction = [numDecimalPlacesToDisplay] (float v, int length)
  44. {
  45. String asText (v, numDecimalPlacesToDisplay);
  46. return length > 0 ? asText.substring (0, length) : asText;
  47. };
  48. }
  49. if (valueFromStringFunction == nullptr)
  50. valueFromStringFunction = [] (const String& text) { return text.getFloatValue(); };
  51. }
  52. AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, float maxValue, float def)
  53. : AudioParameterFloat (pid, nm, { minValue, maxValue, 0.01f }, def)
  54. {
  55. }
  56. AudioParameterFloat::~AudioParameterFloat()
  57. {
  58. #if __cpp_lib_atomic_is_always_lock_free
  59. static_assert (std::atomic<float>::is_always_lock_free,
  60. "AudioParameterFloat requires a lock-free std::atomic<float>");
  61. #endif
  62. }
  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