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.

280 lines
13KB

  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. namespace juce
  20. {
  21. /**
  22. This class contains a ValueTree which is used to manage an AudioProcessor's entire state.
  23. It has its own internal class of parameter object which are linked to values
  24. within its ValueTree, and which are each identified by a string ID.
  25. You can get access to the underlying ValueTree object via the state member variable,
  26. so you can add extra properties to it as necessary.
  27. It also provides some utility child classes for connecting parameters directly to
  28. GUI controls like sliders.
  29. To use:
  30. 1) Create an AudioProcessorValueTreeState, and give it some parameters using createAndAddParameter().
  31. 2) Initialise the state member variable with a type name.
  32. @tags{Audio}
  33. */
  34. class JUCE_API AudioProcessorValueTreeState : private Timer,
  35. private ValueTree::Listener
  36. {
  37. public:
  38. /** Creates a state object for a given processor.
  39. The UndoManager is optional and can be a nullptr.
  40. After creating your state object, you should add parameters with the
  41. createAndAddParameter() method. Note that each AudioProcessorValueTreeState
  42. should be attached to only one processor, and must have the same lifetime as the
  43. processor, as they will have dependencies on each other.
  44. */
  45. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  46. UndoManager* undoManagerToUse);
  47. /** Destructor. */
  48. ~AudioProcessorValueTreeState();
  49. /** Creates and returns a new parameter object for controlling a parameter
  50. with the given ID.
  51. Calling this will create and add a special type of AudioProcessorParameter to the
  52. AudioProcessor to which this state is attached.
  53. @param parameterID A unique string ID for the new parameter
  54. @param parameterName The name that the parameter will return from AudioProcessorParameter::getName()
  55. @param labelText The label that the parameter will return from AudioProcessorParameter::getLabel()
  56. @param valueRange A mapping that will be used to determine the value range which this parameter uses
  57. @param defaultValue A default value for the parameter (in non-normalised units)
  58. @param valueToTextFunction A function that will convert a non-normalised value to a string for the
  59. AudioProcessorParameter::getText() method. This can be nullptr to use the
  60. default implementation
  61. @param textToValueFunction The inverse of valueToTextFunction
  62. @param isMetaParameter Set this value to true if this should be a meta parameter
  63. @param isAutomatableParameter Set this value to false if this parameter should not be automatable
  64. @param isDiscrete Set this value to true to make this parameter take discrete values in a host.
  65. @see AudioProcessorParameter::isDiscrete
  66. @param category Which category the parameter should use.
  67. @see AudioProcessorParameter::Category
  68. @param isBoolean Set this value to true to make this parameter appear as a boolean toggle in
  69. a hosts view of your plug-ins parameters
  70. @see AudioProcessorParameter::isBoolean
  71. @returns the parameter object that was created
  72. */
  73. AudioProcessorParameterWithID* createAndAddParameter (const String& parameterID,
  74. const String& parameterName,
  75. const String& labelText,
  76. NormalisableRange<float> valueRange,
  77. float defaultValue,
  78. std::function<String (float)> valueToTextFunction,
  79. std::function<float (const String&)> textToValueFunction,
  80. bool isMetaParameter = false,
  81. bool isAutomatableParameter = true,
  82. bool isDiscrete = false,
  83. AudioProcessorParameter::Category category
  84. = AudioProcessorParameter::genericParameter,
  85. bool isBoolean = false);
  86. /** Returns a parameter by its ID string. */
  87. AudioProcessorParameterWithID* getParameter (StringRef parameterID) const noexcept;
  88. /** Returns a pointer to a floating point representation of a particular
  89. parameter which a realtime process can read to find out its current value.
  90. */
  91. float* getRawParameterValue (StringRef parameterID) const noexcept;
  92. /** A listener class that can be attached to an AudioProcessorValueTreeState.
  93. Use AudioProcessorValueTreeState::addParameterListener() to register a callback.
  94. */
  95. struct JUCE_API Listener
  96. {
  97. Listener();
  98. virtual ~Listener();
  99. /** This callback method is called by the AudioProcessorValueTreeState when a parameter changes. */
  100. virtual void parameterChanged (const String& parameterID, float newValue) = 0;
  101. };
  102. /** Attaches a callback to one of the parameters, which will be called when the parameter changes. */
  103. void addParameterListener (StringRef parameterID, Listener* listener);
  104. /** Removes a callback that was previously added with addParameterCallback(). */
  105. void removeParameterListener (StringRef parameterID, Listener* listener);
  106. /** Returns a Value object that can be used to control a particular parameter. */
  107. Value getParameterAsValue (StringRef parameterID) const;
  108. /** Returns the range that was set when the given parameter was created. */
  109. NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
  110. /** Returns a copy of the state value tree.
  111. The AudioProcessorValueTreeState's ValueTree is updated internally on the
  112. message thread, but there may be cases when you may want to access the state
  113. from a different thread (getStateInformation is a good example). This method
  114. flushes all pending audio parameter value updates and returns a copy of the
  115. state in a thread safe way.
  116. Note: This method uses locks to synchronise thread access, so whilst it is
  117. thread-safe, it is not realtime-safe. Do not call this method from within
  118. your audio processing code!
  119. */
  120. ValueTree copyState();
  121. /** Replaces the state value tree.
  122. The AudioProcessorValueTreeState's ValueTree is updated internally on the
  123. message thread, but there may be cases when you may want to modify the state
  124. from a different thread (setStateInformation is a good example). This method
  125. allows you to replace the state in a thread safe way.
  126. Note: This method uses locks to synchronise thread access, so whilst it is
  127. thread-safe, it is not realtime-safe. Do not call this method from within
  128. your audio processing code!
  129. */
  130. void replaceState (const ValueTree& newState);
  131. /** A reference to the processor with which this state is associated. */
  132. AudioProcessor& processor;
  133. /** The state of the whole processor.
  134. This must be initialised after all calls to createAndAddParameter().
  135. You can replace this with your own ValueTree object, and can add properties and
  136. children to the tree. This class will automatically add children for each of the
  137. parameter objects that are created by createAndAddParameter().
  138. */
  139. ValueTree state;
  140. /** Provides access to the undo manager that this object is using. */
  141. UndoManager* const undoManager;
  142. //==============================================================================
  143. /** An object of this class maintains a connection between a Slider and a parameter
  144. in an AudioProcessorValueTreeState.
  145. During the lifetime of this SliderAttachment object, it keeps the two things in
  146. sync, making it easy to connect a slider to a parameter. When this object is
  147. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  148. and Slider aren't deleted before this object!
  149. */
  150. class JUCE_API SliderAttachment
  151. {
  152. public:
  153. SliderAttachment (AudioProcessorValueTreeState& stateToControl,
  154. const String& parameterID,
  155. Slider& sliderToControl);
  156. ~SliderAttachment();
  157. private:
  158. struct Pimpl;
  159. friend struct ContainerDeletePolicy<Pimpl>;
  160. std::unique_ptr<Pimpl> pimpl;
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  162. };
  163. //==============================================================================
  164. /** An object of this class maintains a connection between a ComboBox and a parameter
  165. in an AudioProcessorValueTreeState.
  166. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  167. sync, making it easy to connect a combo box to a parameter. When this object is
  168. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  169. and ComboBox aren't deleted before this object!
  170. */
  171. class JUCE_API ComboBoxAttachment
  172. {
  173. public:
  174. ComboBoxAttachment (AudioProcessorValueTreeState& stateToControl,
  175. const String& parameterID,
  176. ComboBox& comboBoxToControl);
  177. ~ComboBoxAttachment();
  178. private:
  179. struct Pimpl;
  180. friend struct ContainerDeletePolicy<Pimpl>;
  181. std::unique_ptr<Pimpl> pimpl;
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  183. };
  184. //==============================================================================
  185. /** An object of this class maintains a connection between a Button and a parameter
  186. in an AudioProcessorValueTreeState.
  187. During the lifetime of this ButtonAttachment object, it keeps the two things in
  188. sync, making it easy to connect a button to a parameter. When this object is
  189. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  190. and Button aren't deleted before this object!
  191. */
  192. class JUCE_API ButtonAttachment
  193. {
  194. public:
  195. ButtonAttachment (AudioProcessorValueTreeState& stateToControl,
  196. const String& parameterID,
  197. Button& buttonToControl);
  198. ~ButtonAttachment();
  199. private:
  200. struct Pimpl;
  201. friend struct ContainerDeletePolicy<Pimpl>;
  202. std::unique_ptr<Pimpl> pimpl;
  203. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  204. };
  205. private:
  206. //==============================================================================
  207. struct Parameter;
  208. friend struct Parameter;
  209. ValueTree getOrCreateChildValueTree (const String&);
  210. bool flushParameterValuesToValueTree();
  211. void timerCallback() override;
  212. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  213. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  214. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  215. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  216. void valueTreeParentChanged (ValueTree&) override;
  217. void valueTreeRedirected (ValueTree&) override;
  218. void updateParameterConnectionsToChildTrees();
  219. Identifier valueType { "PARAM" },
  220. valuePropertyID { "value" },
  221. idPropertyID { "id" };
  222. CriticalSection valueTreeChanging;
  223. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  224. };
  225. } // namespace juce