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.

112 lines
5.7KB

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