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.

132 lines
5.6KB

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