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. #pragma once
  18. #if JUCE_COMPILER_SUPPORTS_LAMBDAS
  19. /**
  20. This class contains a ValueTree which is used to manage an AudioProcessor's entire state.
  21. It has its own internal class of parameter object which are linked to values
  22. within its ValueTree, and which are each identified by a string ID.
  23. You can get access to the underlying ValueTree object via the state member variable,
  24. so you can add extra properties to it as necessary.
  25. It also provides some utility child classes for connecting parameters directly to
  26. GUI controls like sliders.
  27. To use:
  28. 1) Create an AudioProcessorValueTreeState, and give it some parameters using createParameter().
  29. 2) Initialise the state member variable with a type name.
  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. AudioProcessorParameterWithID* createAndAddParameter (const String& parameterID,
  62. const String& parameterName,
  63. const 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. AudioProcessorParameterWithID* 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. This must be initialised after all calls to createAndAddParameter().
  96. You can replace this with your own ValueTree object, and can add properties and
  97. children to the tree. This class will automatically add children for each of the
  98. parameter objects that are created by createParameter().
  99. */
  100. ValueTree state;
  101. /** Provides access to the undo manager that this object is using. */
  102. UndoManager* const undoManager;
  103. //==============================================================================
  104. /** An object of this class maintains a connection between a Slider and a parameter
  105. in an AudioProcessorValueTreeState.
  106. During the lifetime of this SliderAttachment object, it keeps the two things in
  107. sync, making it easy to connect a slider to a parameter. When this object is
  108. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  109. and Slider aren't deleted before this object!
  110. */
  111. class JUCE_API SliderAttachment
  112. {
  113. public:
  114. SliderAttachment (AudioProcessorValueTreeState& stateToControl,
  115. const String& parameterID,
  116. Slider& sliderToControl);
  117. ~SliderAttachment();
  118. private:
  119. struct Pimpl;
  120. friend struct ContainerDeletePolicy<Pimpl>;
  121. ScopedPointer<Pimpl> pimpl;
  122. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  123. };
  124. //==============================================================================
  125. /** An object of this class maintains a connection between a ComboBox and a parameter
  126. in an AudioProcessorValueTreeState.
  127. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  128. sync, making it easy to connect a combo box to a parameter. When this object is
  129. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  130. and ComboBox aren't deleted before this object!
  131. */
  132. class JUCE_API ComboBoxAttachment
  133. {
  134. public:
  135. ComboBoxAttachment (AudioProcessorValueTreeState& stateToControl,
  136. const String& parameterID,
  137. ComboBox& comboBoxToControl);
  138. ~ComboBoxAttachment();
  139. private:
  140. struct Pimpl;
  141. friend struct ContainerDeletePolicy<Pimpl>;
  142. ScopedPointer<Pimpl> pimpl;
  143. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  144. };
  145. //==============================================================================
  146. /** An object of this class maintains a connection between a Button and a parameter
  147. in an AudioProcessorValueTreeState.
  148. During the lifetime of this ButtonAttachment object, it keeps the two things in
  149. sync, making it easy to connect a button to a parameter. When this object is
  150. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  151. and Button aren't deleted before this object!
  152. */
  153. class JUCE_API ButtonAttachment
  154. {
  155. public:
  156. ButtonAttachment (AudioProcessorValueTreeState& stateToControl,
  157. const String& parameterID,
  158. Button& buttonToControl);
  159. ~ButtonAttachment();
  160. private:
  161. struct Pimpl;
  162. friend struct ContainerDeletePolicy<Pimpl>;
  163. ScopedPointer<Pimpl> pimpl;
  164. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  165. };
  166. private:
  167. //==============================================================================
  168. struct Parameter;
  169. friend struct Parameter;
  170. ValueTree getOrCreateChildValueTree (const String&);
  171. void timerCallback() override;
  172. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  173. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  174. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  175. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  176. void valueTreeParentChanged (ValueTree&) override;
  177. void valueTreeRedirected (ValueTree&) override;
  178. void updateParameterConnectionsToChildTrees();
  179. Identifier valueType, valuePropertyID, idPropertyID;
  180. bool updatingConnections;
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  182. };
  183. #endif