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.

230 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_AUDIOPROCESSORVALUETREESTATE_H_INCLUDED
  18. #define JUCE_AUDIOPROCESSORVALUETREESTATE_H_INCLUDED
  19. #if JUCE_COMPILER_SUPPORTS_LAMBDAS || defined (DOXYGEN)
  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. To use: Create a AudioProcessorValueTreeState, and give it some parameters
  25. using createParameter().
  26. You can get access to the underlying ValueTree object via the state member variable,
  27. so you can add extra properties to it as necessary.
  28. It also provides some utility child classes for connecting parameters directly to
  29. GUI controls like sliders.
  30. */
  31. class JUCE_API AudioProcessorValueTreeState : private Timer,
  32. private ValueTree::Listener
  33. {
  34. public:
  35. /** Creates a state object for a given processor.
  36. The UndoManager is optional and can be a nullptr.
  37. After creating your state object, you should add parameters with the
  38. createAndAddParameter() method. Note that each AudioProcessorValueTreeState
  39. should be attached to only one processor, and must have the same lifetime as the
  40. processor, as they will have dependencies on each other.
  41. */
  42. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  43. UndoManager* undoManagerToUse);
  44. /** Destructor. */
  45. ~AudioProcessorValueTreeState();
  46. /** Creates and returns a new parameter object for controlling a parameter
  47. with the given ID.
  48. Calling this will create and add a special type of AudioProcessorParameter to the
  49. AudioProcessor to which this state is attached.
  50. @param parameterID A unique string ID for the new parameter
  51. @param parameterName The name that the parameter will return from AudioProcessorParameter::getName()
  52. @param labelText The label that the parameter will return from AudioProcessorParameter::getLabel()
  53. @param valueRange A mapping that will be used to determine the value range which this parameter uses
  54. @param defaultValue A default value for the parameter (in non-normalised units)
  55. @param valueToTextFunction A function that will convert a non-normalised value to a string for the
  56. AudioProcessorParameter::getText() method. This can be nullptr to use the
  57. default implementation
  58. @param textToValueFunction The inverse of valueToTextFunction
  59. @returns the parameter object that was created
  60. */
  61. AudioProcessorParameter* createAndAddParameter (String parameterID,
  62. String parameterName,
  63. String labelText,
  64. NormalisableRange<float> valueRange,
  65. float defaultValue,
  66. std::function<String (float)> valueToTextFunction,
  67. std::function<float (const String&)> textToValueFunction);
  68. /** Returns a parameter by its ID string. */
  69. AudioProcessorParameter* getParameter (StringRef parameterID) const noexcept;
  70. /** Returns a pointer to a floating point representation of a particular
  71. parameter which a realtime process can read to find out its current value.
  72. */
  73. float* getRawParameterValue (StringRef parameterID) const noexcept;
  74. /** A listener class that can be attached to an AudioProcessorValueTreeState.
  75. Use AudioProcessorValueTreeState::addParameterListener() to register a callback.
  76. */
  77. struct JUCE_API Listener
  78. {
  79. Listener();
  80. virtual ~Listener();
  81. /** This callback method is called by the AudioProcessorValueTreeState when a parameter changes. */
  82. virtual void parameterChanged (const String& parameterID, float newValue) = 0;
  83. };
  84. /** Attaches a callback to one of the parameters, which will be called when the parameter changes. */
  85. void addParameterListener (StringRef parameterID, Listener* listener);
  86. /** Removes a callback that was previously added with addParameterCallback(). */
  87. void removeParameterListener (StringRef parameterID, Listener* listener);
  88. /** Returns a Value object that can be used to control a particular parameter. */
  89. Value getParameterAsValue (StringRef parameterID) const;
  90. /** Returns the range that was set when the given parameter was created. */
  91. NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
  92. /** A reference to the processor with which this state is associated. */
  93. AudioProcessor& processor;
  94. /** The state of the whole processor.
  95. You can replace this with your own ValueTree object, and can add properties and
  96. children to the tree. This class will automatically add children for each of the
  97. parameter objects that are created by createParameter().
  98. */
  99. ValueTree state;
  100. /** Provides access to the undo manager that this object is using. */
  101. UndoManager* const undoManager;
  102. //==============================================================================
  103. /** An object of this class maintains a connection between a Slider and a parameter
  104. in an AudioProcessorValueTreeState.
  105. During the lifetime of this SliderAttachment object, it keeps the two things in
  106. sync, making it easy to connect a slider to a parameter. When this object is
  107. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  108. and Slider aren't deleted before this object!
  109. */
  110. class JUCE_API SliderAttachment
  111. {
  112. public:
  113. SliderAttachment (AudioProcessorValueTreeState& stateToControl,
  114. const String& parameterID,
  115. Slider& sliderToControl);
  116. ~SliderAttachment();
  117. private:
  118. struct Pimpl;
  119. friend struct ContainerDeletePolicy<Pimpl>;
  120. ScopedPointer<Pimpl> pimpl;
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  122. };
  123. //==============================================================================
  124. /** An object of this class maintains a connection between a ComboBox and a parameter
  125. in an AudioProcessorValueTreeState.
  126. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  127. sync, making it easy to connect a combo box to a parameter. When this object is
  128. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  129. and ComboBox aren't deleted before this object!
  130. */
  131. class JUCE_API ComboBoxAttachment
  132. {
  133. public:
  134. ComboBoxAttachment (AudioProcessorValueTreeState& stateToControl,
  135. const String& parameterID,
  136. ComboBox& comboBoxToControl);
  137. ~ComboBoxAttachment();
  138. private:
  139. struct Pimpl;
  140. friend struct ContainerDeletePolicy<Pimpl>;
  141. ScopedPointer<Pimpl> pimpl;
  142. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  143. };
  144. //==============================================================================
  145. /** An object of this class maintains a connection between a Button and a parameter
  146. in an AudioProcessorValueTreeState.
  147. During the lifetime of this ButtonAttachment object, it keeps the two things in
  148. sync, making it easy to connect a button to a parameter. When this object is
  149. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  150. and Button aren't deleted before this object!
  151. */
  152. class JUCE_API ButtonAttachment
  153. {
  154. public:
  155. ButtonAttachment (AudioProcessorValueTreeState& stateToControl,
  156. const String& parameterID,
  157. Button& buttonToControl);
  158. ~ButtonAttachment();
  159. private:
  160. struct Pimpl;
  161. friend struct ContainerDeletePolicy<Pimpl>;
  162. ScopedPointer<Pimpl> pimpl;
  163. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  164. };
  165. private:
  166. //==============================================================================
  167. struct Parameter;
  168. friend struct Parameter;
  169. ValueTree getOrCreateChildValueTree (const String&);
  170. void timerCallback() override;
  171. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  172. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  173. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  174. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  175. void valueTreeParentChanged (ValueTree&) override;
  176. void valueTreeRedirected (ValueTree&) override;
  177. void updateParameterConnectionsToChildTrees();
  178. Identifier valueType, valuePropertyID, idPropertyID;
  179. bool updatingConnections;
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  181. };
  182. #endif
  183. #endif // JUCE_AUDIOPROCESSORVALUETREESTATE_H_INCLUDED