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.

145 lines
6.3KB

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