The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

236 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. /**
  21. This class contains a ValueTree which is used to manage an AudioProcessor's entire state.
  22. It has its own internal class of parameter object which are linked to values
  23. within its ValueTree, and which are each identified by a string ID.
  24. You can get access to the underlying ValueTree object via the state member variable,
  25. so you can add extra properties to it as necessary.
  26. It also provides some utility child classes for connecting parameters directly to
  27. GUI controls like sliders.
  28. To use:
  29. 1) Create an AudioProcessorValueTreeState, and give it some parameters using createAndAddParameter().
  30. 2) Initialise the state member variable with a type name.
  31. */
  32. class JUCE_API AudioProcessorValueTreeState : private Timer,
  33. private ValueTree::Listener
  34. {
  35. public:
  36. /** Creates a state object for a given processor.
  37. The UndoManager is optional and can be a nullptr.
  38. After creating your state object, you should add parameters with the
  39. createAndAddParameter() method. Note that each AudioProcessorValueTreeState
  40. should be attached to only one processor, and must have the same lifetime as the
  41. processor, as they will have dependencies on each other.
  42. */
  43. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  44. UndoManager* undoManagerToUse);
  45. /** Destructor. */
  46. ~AudioProcessorValueTreeState();
  47. /** Creates and returns a new parameter object for controlling a parameter
  48. with the given ID.
  49. Calling this will create and add a special type of AudioProcessorParameter to the
  50. AudioProcessor to which this state is attached.
  51. @param parameterID A unique string ID for the new parameter
  52. @param parameterName The name that the parameter will return from AudioProcessorParameter::getName()
  53. @param labelText The label that the parameter will return from AudioProcessorParameter::getLabel()
  54. @param valueRange A mapping that will be used to determine the value range which this parameter uses
  55. @param defaultValue A default value for the parameter (in non-normalised units)
  56. @param valueToTextFunction A function that will convert a non-normalised value to a string for the
  57. AudioProcessorParameter::getText() method. This can be nullptr to use the
  58. default implementation
  59. @param textToValueFunction The inverse of valueToTextFunction
  60. @param isMetaParameter Set this value to true if this should be a meta parameter
  61. @param isAutomatableParameter Set this value to false if this parameter should not be automatable
  62. @param isDiscrete Set this value to true to make this parameter take discrete values in a host.
  63. @see AudioProcessorParameter::isDiscrete
  64. @returns the parameter object that was created
  65. */
  66. AudioProcessorParameterWithID* createAndAddParameter (const String& parameterID,
  67. const String& parameterName,
  68. const String& labelText,
  69. NormalisableRange<float> valueRange,
  70. float defaultValue,
  71. std::function<String (float)> valueToTextFunction,
  72. std::function<float (const String&)> textToValueFunction,
  73. bool isMetaParameter = false,
  74. bool isAutomatableParameter = true,
  75. bool isDiscrete = false);
  76. /** Returns a parameter by its ID string. */
  77. AudioProcessorParameterWithID* getParameter (StringRef parameterID) const noexcept;
  78. /** Returns a pointer to a floating point representation of a particular
  79. parameter which a realtime process can read to find out its current value.
  80. */
  81. float* getRawParameterValue (StringRef parameterID) const noexcept;
  82. /** A listener class that can be attached to an AudioProcessorValueTreeState.
  83. Use AudioProcessorValueTreeState::addParameterListener() to register a callback.
  84. */
  85. struct JUCE_API Listener
  86. {
  87. Listener();
  88. virtual ~Listener();
  89. /** This callback method is called by the AudioProcessorValueTreeState when a parameter changes. */
  90. virtual void parameterChanged (const String& parameterID, float newValue) = 0;
  91. };
  92. /** Attaches a callback to one of the parameters, which will be called when the parameter changes. */
  93. void addParameterListener (StringRef parameterID, Listener* listener);
  94. /** Removes a callback that was previously added with addParameterCallback(). */
  95. void removeParameterListener (StringRef parameterID, Listener* listener);
  96. /** Returns a Value object that can be used to control a particular parameter. */
  97. Value getParameterAsValue (StringRef parameterID) const;
  98. /** Returns the range that was set when the given parameter was created. */
  99. NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
  100. /** A reference to the processor with which this state is associated. */
  101. AudioProcessor& processor;
  102. /** The state of the whole processor.
  103. This must be initialised after all calls to createAndAddParameter().
  104. You can replace this with your own ValueTree object, and can add properties and
  105. children to the tree. This class will automatically add children for each of the
  106. parameter objects that are created by createAndAddParameter().
  107. */
  108. ValueTree state;
  109. /** Provides access to the undo manager that this object is using. */
  110. UndoManager* const undoManager;
  111. //==============================================================================
  112. /** An object of this class maintains a connection between a Slider and a parameter
  113. in an AudioProcessorValueTreeState.
  114. During the lifetime of this SliderAttachment object, it keeps the two things in
  115. sync, making it easy to connect a slider to a parameter. When this object is
  116. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  117. and Slider aren't deleted before this object!
  118. */
  119. class JUCE_API SliderAttachment
  120. {
  121. public:
  122. SliderAttachment (AudioProcessorValueTreeState& stateToControl,
  123. const String& parameterID,
  124. Slider& sliderToControl);
  125. ~SliderAttachment();
  126. private:
  127. struct Pimpl;
  128. friend struct ContainerDeletePolicy<Pimpl>;
  129. ScopedPointer<Pimpl> pimpl;
  130. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  131. };
  132. //==============================================================================
  133. /** An object of this class maintains a connection between a ComboBox and a parameter
  134. in an AudioProcessorValueTreeState.
  135. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  136. sync, making it easy to connect a combo box to a parameter. When this object is
  137. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  138. and ComboBox aren't deleted before this object!
  139. */
  140. class JUCE_API ComboBoxAttachment
  141. {
  142. public:
  143. ComboBoxAttachment (AudioProcessorValueTreeState& stateToControl,
  144. const String& parameterID,
  145. ComboBox& comboBoxToControl);
  146. ~ComboBoxAttachment();
  147. private:
  148. struct Pimpl;
  149. friend struct ContainerDeletePolicy<Pimpl>;
  150. ScopedPointer<Pimpl> pimpl;
  151. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  152. };
  153. //==============================================================================
  154. /** An object of this class maintains a connection between a Button and a parameter
  155. in an AudioProcessorValueTreeState.
  156. During the lifetime of this ButtonAttachment object, it keeps the two things in
  157. sync, making it easy to connect a button to a parameter. When this object is
  158. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  159. and Button aren't deleted before this object!
  160. */
  161. class JUCE_API ButtonAttachment
  162. {
  163. public:
  164. ButtonAttachment (AudioProcessorValueTreeState& stateToControl,
  165. const String& parameterID,
  166. Button& buttonToControl);
  167. ~ButtonAttachment();
  168. private:
  169. struct Pimpl;
  170. friend struct ContainerDeletePolicy<Pimpl>;
  171. ScopedPointer<Pimpl> pimpl;
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  173. };
  174. private:
  175. //==============================================================================
  176. struct Parameter;
  177. friend struct Parameter;
  178. ValueTree getOrCreateChildValueTree (const String&);
  179. void timerCallback() override;
  180. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  181. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  182. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  183. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  184. void valueTreeParentChanged (ValueTree&) override;
  185. void valueTreeRedirected (ValueTree&) override;
  186. void updateParameterConnectionsToChildTrees();
  187. Identifier valueType, valuePropertyID, idPropertyID;
  188. bool updatingConnections;
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  190. };