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.

119 lines
5.9KB

  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. /**
  21. @internal
  22. Holds common attributes of audio parameters.
  23. CRTP is used here because we want the Attributes types for each parameter
  24. (Float, Bool, Choice, Int) to be distinct and extensible in the future.
  25. i.e. the identifiers AudioParameterFloatAttributes and RangedAudioParameterAttributes<float>
  26. should not be interchangable because we might need to add float-specific attributes in
  27. the future. Users should not refer directly to RangedAudioParameterAttributes.
  28. */
  29. template <typename Derived, typename Value>
  30. class RangedAudioParameterAttributes
  31. {
  32. using This = RangedAudioParameterAttributes;
  33. public:
  34. using Category = AudioProcessorParameter::Category;
  35. using StringFromValue = std::function<String (Value, int)>;
  36. using ValueFromString = std::function<Value (const String&)>;
  37. /** An optional lambda function that converts a non-normalised value to a string with a maximum length. This may be used by hosts to display the parameter's value. */
  38. JUCE_NODISCARD auto withStringFromValueFunction (StringFromValue x) const { return withMember (asDerived(), &Derived::stringFromValue, std::move (x)); }
  39. /** An optional lambda function that parses a string and converts it into a non-normalised value. Some hosts use this to allow users to type in parameter values. */
  40. JUCE_NODISCARD auto withValueFromStringFunction (ValueFromString x) const { return withMember (asDerived(), &Derived::valueFromString, std::move (x)); }
  41. /** See AudioProcessorParameterWithIDAttributes::withLabel() */
  42. JUCE_NODISCARD auto withLabel (String x) const { return withMember (asDerived(), &Derived::attributes, attributes.withLabel (std::move (x))); }
  43. /** See AudioProcessorParameterWithIDAttributes::withCategory() */
  44. JUCE_NODISCARD auto withCategory (Category x) const { return withMember (asDerived(), &Derived::attributes, attributes.withCategory (std::move (x))); }
  45. /** See AudioProcessorParameter::isMetaParameter() */
  46. JUCE_NODISCARD auto withMeta (bool x) const { return withMember (asDerived(), &Derived::attributes, attributes.withMeta (std::move (x))); }
  47. /** See AudioProcessorParameter::isAutomatable() */
  48. JUCE_NODISCARD auto withAutomatable (bool x) const { return withMember (asDerived(), &Derived::attributes, attributes.withAutomatable (std::move (x))); }
  49. /** See AudioProcessorParameter::isOrientationInverted() */
  50. JUCE_NODISCARD auto withInverted (bool x) const { return withMember (asDerived(), &Derived::attributes, attributes.withInverted (std::move (x))); }
  51. /** An optional lambda function that converts a non-normalised value to a string with a maximum length. This may be used by hosts to display the parameter's value. */
  52. JUCE_NODISCARD const auto& getStringFromValueFunction() const { return stringFromValue; }
  53. /** An optional lambda function that parses a string and converts it into a non-normalised value. Some hosts use this to allow users to type in parameter values. */
  54. JUCE_NODISCARD const auto& getValueFromStringFunction() const { return valueFromString; }
  55. /** Gets attributes that would also apply to an AudioProcessorParameterWithID */
  56. JUCE_NODISCARD const auto& getAudioProcessorParameterWithIDAttributes() const { return attributes; }
  57. private:
  58. auto& asDerived() const { return *static_cast<const Derived*> (this); }
  59. AudioProcessorParameterWithIDAttributes attributes;
  60. StringFromValue stringFromValue;
  61. ValueFromString valueFromString;
  62. };
  63. //==============================================================================
  64. /**
  65. This abstract base class is used by some AudioProcessorParameter helper classes.
  66. @see AudioParameterFloat, AudioParameterInt, AudioParameterBool, AudioParameterChoice
  67. @tags{Audio}
  68. */
  69. class JUCE_API RangedAudioParameter : public AudioProcessorParameterWithID
  70. {
  71. public:
  72. using AudioProcessorParameterWithID::AudioProcessorParameterWithID;
  73. /** Returns the range of values that the parameter can take. */
  74. virtual const NormalisableRange<float>& getNormalisableRange() const = 0;
  75. /** Returns the number of steps for this parameter based on the normalisable range's interval.
  76. If you are using lambda functions to define the normalisable range's snapping behaviour
  77. then you should override this function so that it returns the number of snapping points.
  78. */
  79. int getNumSteps() const override;
  80. /** Normalises and snaps a value based on the normalisable range. */
  81. float convertTo0to1 (float v) const noexcept;
  82. /** Denormalises and snaps a value based on the normalisable range. */
  83. float convertFrom0to1 (float v) const noexcept;
  84. };
  85. } // namespace juce