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.

152 lines
6.5KB

  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. /** Properties of an AudioParameterChoice.
  21. @see AudioParameterChoice(), RangedAudioParameterAttributes()
  22. */
  23. class AudioParameterChoiceAttributes : public RangedAudioParameterAttributes<AudioParameterChoiceAttributes, int> {};
  24. //==============================================================================
  25. /**
  26. Provides a class of AudioProcessorParameter that can be used to select
  27. an indexed, named choice from a list.
  28. @see AudioParameterFloat, AudioParameterInt, AudioParameterBool
  29. @tags{Audio}
  30. */
  31. class JUCE_API AudioParameterChoice : public RangedAudioParameter
  32. {
  33. public:
  34. /** Creates a AudioParameterChoice with the specified parameters.
  35. Note that the attributes argument is optional and only needs to be
  36. supplied if you want to change options from their default values.
  37. Example usage:
  38. @code
  39. auto attributes = AudioParameterChoiceAttributes().withLabel ("selected");
  40. auto param = std::make_unique<AudioParameterChoice> ("paramID", "Parameter Name", StringArray { "a", "b", "c" }, 0, attributes);
  41. @endcode
  42. @param parameterID The parameter ID to use
  43. @param parameterName The parameter name to use
  44. @param choices The set of choices to use
  45. @param defaultItemIndex The index of the default choice
  46. @param attributes Optional characteristics
  47. */
  48. AudioParameterChoice (const ParameterID& parameterID,
  49. const String& parameterName,
  50. const StringArray& choices,
  51. int defaultItemIndex,
  52. const AudioParameterChoiceAttributes& attributes = {});
  53. /** Creates a AudioParameterChoice with the specified parameters.
  54. @param parameterID The parameter ID to use
  55. @param parameterName The parameter name to use
  56. @param choicesToUse The set of choices to use
  57. @param defaultItemIndex The index of the default choice
  58. @param parameterLabel An optional label for the parameter's value
  59. @param stringFromIndex An optional lambda function that converts a choice
  60. index to a string with a maximum length. This may
  61. be used by hosts to display the parameter's value.
  62. @param indexFromString An optional lambda function that parses a string and
  63. converts it into a choice index. Some hosts use this
  64. to allow users to type in parameter values.
  65. */
  66. [[deprecated ("Prefer the signature taking an Attributes argument")]]
  67. AudioParameterChoice (const ParameterID& parameterID,
  68. const String& parameterName,
  69. const StringArray& choicesToUse,
  70. int defaultItemIndex,
  71. const String& parameterLabel,
  72. std::function<String (int index, int maximumStringLength)> stringFromIndex = nullptr,
  73. std::function<int (const String& text)> indexFromString = nullptr)
  74. : AudioParameterChoice (parameterID,
  75. parameterName,
  76. choicesToUse,
  77. defaultItemIndex,
  78. AudioParameterChoiceAttributes().withLabel (parameterLabel)
  79. .withStringFromValueFunction (std::move (stringFromIndex))
  80. .withValueFromStringFunction (std::move (indexFromString)))
  81. {
  82. }
  83. /** Destructor. */
  84. ~AudioParameterChoice() override;
  85. /** Returns the current index of the selected item. */
  86. int getIndex() const noexcept { return roundToInt (value.load()); }
  87. /** Returns the current index of the selected item. */
  88. operator int() const noexcept { return getIndex(); }
  89. /** Returns the name of the currently selected item. */
  90. String getCurrentChoiceName() const noexcept { return choices[getIndex()]; }
  91. /** Returns the name of the currently selected item. */
  92. operator String() const noexcept { return getCurrentChoiceName(); }
  93. /** Changes the selected item to a new index. */
  94. AudioParameterChoice& operator= (int newValue);
  95. /** Returns the range of values that the parameter can take. */
  96. const NormalisableRange<float>& getNormalisableRange() const override { return range; }
  97. /** Provides access to the list of choices that this parameter is working with. */
  98. const StringArray choices;
  99. protected:
  100. /** Override this method if you are interested in receiving callbacks
  101. when the parameter value changes.
  102. */
  103. virtual void valueChanged (int newValue);
  104. private:
  105. //==============================================================================
  106. float getValue() const override;
  107. void setValue (float newValue) override;
  108. float getDefaultValue() const override;
  109. int getNumSteps() const override;
  110. bool isDiscrete() const override;
  111. String getText (float, int) const override;
  112. float getValueForText (const String&) const override;
  113. const NormalisableRange<float> range;
  114. std::atomic<float> value;
  115. const float defaultValue;
  116. std::function<String (int, int)> stringFromIndexFunction;
  117. std::function<int (const String&)> indexFromStringFunction;
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioParameterChoice)
  119. };
  120. } // namespace juce