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.

139 lines
5.8KB

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